]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/scsi/megaraid/megaraid_sas_base.c
[SCSI] megaraid_sas: Use lowest memory bar for SR-IOV VF support
[thirdparty/linux.git] / drivers / scsi / megaraid / megaraid_sas_base.c
CommitLineData
c4a3e0a5 1/*
3f1530c1 2 * Linux MegaRAID driver for SAS based RAID controllers
c4a3e0a5 3 *
3f1530c1 4 * Copyright (c) 2009-2011 LSI Corporation.
c4a3e0a5 5 *
3f1530c1
AR
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
c4a3e0a5 10 *
3f1530c1
AR
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
c4a3e0a5 15 *
3f1530c1
AR
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c4a3e0a5 19 *
3f1530c1
AR
20 * FILE: megaraid_sas_base.c
21 * Version : v00.00.05.29-rc1
c4a3e0a5 22 *
3f1530c1
AR
23 * Authors: LSI Corporation
24 * Sreenivas Bagalkote
25 * Sumant Patro
26 * Bo Yang
c4a3e0a5 27 *
3f1530c1
AR
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
AR
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
AR
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
AR
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;
b6d5d880
AR
3239 unsigned long bar_list;
3240
3241 /* Find first memory bar */
3242 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3243 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3244 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3245 if (pci_request_selected_regions(instance->pdev, instance->bar,
3246 "megasas: LSI")) {
c4a3e0a5
BS
3247 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3248 return -EBUSY;
3249 }
3250
3251 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3252
3253 if (!instance->reg_set) {
3254 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3255 goto fail_ioremap;
3256 }
3257
3258 reg_set = instance->reg_set;
3259
f9876f0b
SP
3260 switch(instance->pdev->device)
3261 {
af7a5647 3262 case PCI_DEVICE_ID_LSI_SAS1078R:
3263 case PCI_DEVICE_ID_LSI_SAS1078DE:
f9876f0b
SP
3264 instance->instancet = &megasas_instance_template_ppc;
3265 break;
6610a6b3
YB
3266 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3267 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3268 instance->instancet = &megasas_instance_template_gen2;
3269 break;
87911122
YB
3270 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3271 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3272 instance->instancet = &megasas_instance_template_skinny;
3273 break;
f9876f0b
SP
3274 case PCI_DEVICE_ID_LSI_SAS1064R:
3275 case PCI_DEVICE_ID_DELL_PERC5:
3276 default:
3277 instance->instancet = &megasas_instance_template_xscale;
3278 break;
3279 }
1341c939 3280
c4a3e0a5
BS
3281 /*
3282 * We expect the FW state to be READY
3283 */
1341c939 3284 if (megasas_transition_to_ready(instance))
c4a3e0a5
BS
3285 goto fail_ready_state;
3286
3287 /*
3288 * Get various operational parameters from status register
3289 */
1341c939 3290 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
e3bbff9f
SP
3291 /*
3292 * Reduce the max supported cmds by 1. This is to ensure that the
3293 * reply_q_sz (1 more than the max cmd that driver may send)
3294 * does not exceed max cmds that the FW can support
3295 */
3296 instance->max_fw_cmds = instance->max_fw_cmds-1;
0d49016b 3297 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1341c939 3298 0x10;
c4a3e0a5
BS
3299 /*
3300 * Create a pool of commands
3301 */
3302 if (megasas_alloc_cmds(instance))
3303 goto fail_alloc_cmds;
3304
3305 /*
3306 * Allocate memory for reply queue. Length of reply queue should
3307 * be _one_ more than the maximum commands handled by the firmware.
3308 *
3309 * Note: When FW completes commands, it places corresponding contex
3310 * values in this circular reply queue. This circular queue is a fairly
3311 * typical producer-consumer queue. FW is the producer (of completed
3312 * commands) and the driver is the consumer.
3313 */
3314 context_sz = sizeof(u32);
3315 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3316
3317 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3318 reply_q_sz,
3319 &instance->reply_queue_h);
3320
3321 if (!instance->reply_queue) {
3322 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3323 goto fail_reply_queue;
3324 }
3325
31ea7088 3326 if (megasas_issue_init_mfi(instance))
c4a3e0a5 3327 goto fail_fw_init;
c4a3e0a5 3328
39a98554 3329 instance->fw_support_ieee = 0;
3330 instance->fw_support_ieee =
3331 (instance->instancet->read_fw_status_reg(reg_set) &
3332 0x04000000);
3333
3334 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3335 instance->fw_support_ieee);
3336
3337 if (instance->fw_support_ieee)
3338 instance->flag_ieee = 1;
3339
3340 /** for passthrough
3341 * the following function will get the PD LIST.
3342 */
3343
81e403ce
YB
3344 memset(instance->pd_list, 0 ,
3345 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3346 megasas_get_pd_list(instance);
3347
bdc6fb8d
YB
3348 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3349 megasas_get_ld_list(instance);
3350
c4a3e0a5
BS
3351 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3352
3353 /*
3354 * Compute the max allowed sectors per IO: The controller info has two
3355 * limits on max sectors. Driver should use the minimum of these two.
3356 *
3357 * 1 << stripe_sz_ops.min = max sectors per strip
3358 *
3359 * Note that older firmwares ( < FW ver 30) didn't report information
3360 * to calculate max_sectors_1. So the number ended up as zero always.
3361 */
14faea9f 3362 tmp_sectors = 0;
c4a3e0a5
BS
3363 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3364
3365 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3366 ctrl_info->max_strips_per_io;
3367 max_sectors_2 = ctrl_info->max_request_size;
3368
14faea9f 3369 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
39a98554 3370 instance->disableOnlineCtrlReset =
3371 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
14faea9f 3372 }
3373
3374 instance->max_sectors_per_req = instance->max_num_sge *
3375 PAGE_SIZE / 512;
3376 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3377 instance->max_sectors_per_req = tmp_sectors;
c4a3e0a5
BS
3378
3379 kfree(ctrl_info);
3380
5d018ad0
SP
3381 /*
3382 * Setup tasklet for cmd completion
3383 */
3384
ad84db2e 3385 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3386 (unsigned long)instance);
3387
3388 /* Initialize the cmd completion timer */
3389 if (poll_mode_io)
3390 megasas_start_timer(instance, &instance->io_completion_timer,
3391 megasas_io_completion_timer,
3392 MEGASAS_COMPLETION_TIMER_INTERVAL);
c4a3e0a5
BS
3393 return 0;
3394
3395 fail_fw_init:
c4a3e0a5
BS
3396
3397 pci_free_consistent(instance->pdev, reply_q_sz,
3398 instance->reply_queue, instance->reply_queue_h);
3399 fail_reply_queue:
3400 megasas_free_cmds(instance);
3401
3402 fail_alloc_cmds:
3403 fail_ready_state:
3404 iounmap(instance->reg_set);
3405
3406 fail_ioremap:
b6d5d880 3407 pci_release_selected_regions(instance->pdev, instance->bar);
c4a3e0a5
BS
3408
3409 return -EINVAL;
3410}
3411
3412/**
3413 * megasas_release_mfi - Reverses the FW initialization
3414 * @intance: Adapter soft state
3415 */
3416static void megasas_release_mfi(struct megasas_instance *instance)
3417{
3418 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
3419
3420 pci_free_consistent(instance->pdev, reply_q_sz,
3421 instance->reply_queue, instance->reply_queue_h);
3422
3423 megasas_free_cmds(instance);
3424
3425 iounmap(instance->reg_set);
3426
b6d5d880 3427 pci_release_selected_regions(instance->pdev, instance->bar);
c4a3e0a5
BS
3428}
3429
3430/**
3431 * megasas_get_seq_num - Gets latest event sequence numbers
3432 * @instance: Adapter soft state
3433 * @eli: FW event log sequence numbers information
3434 *
3435 * FW maintains a log of all events in a non-volatile area. Upper layers would
3436 * usually find out the latest sequence number of the events, the seq number at
3437 * the boot etc. They would "read" all the events below the latest seq number
3438 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3439 * number), they would subsribe to AEN (asynchronous event notification) and
3440 * wait for the events to happen.
3441 */
3442static int
3443megasas_get_seq_num(struct megasas_instance *instance,
3444 struct megasas_evt_log_info *eli)
3445{
3446 struct megasas_cmd *cmd;
3447 struct megasas_dcmd_frame *dcmd;
3448 struct megasas_evt_log_info *el_info;
3449 dma_addr_t el_info_h = 0;
3450
3451 cmd = megasas_get_cmd(instance);
3452
3453 if (!cmd) {
3454 return -ENOMEM;
3455 }
3456
3457 dcmd = &cmd->frame->dcmd;
3458 el_info = pci_alloc_consistent(instance->pdev,
3459 sizeof(struct megasas_evt_log_info),
3460 &el_info_h);
3461
3462 if (!el_info) {
3463 megasas_return_cmd(instance, cmd);
3464 return -ENOMEM;
3465 }
3466
3467 memset(el_info, 0, sizeof(*el_info));
3468 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3469
3470 dcmd->cmd = MFI_CMD_DCMD;
3471 dcmd->cmd_status = 0x0;
3472 dcmd->sge_count = 1;
3473 dcmd->flags = MFI_FRAME_DIR_READ;
3474 dcmd->timeout = 0;
780a3762 3475 dcmd->pad_0 = 0;
c4a3e0a5
BS
3476 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3477 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3478 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3479 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3480
3481 megasas_issue_blocked_cmd(instance, cmd);
3482
3483 /*
3484 * Copy the data back into callers buffer
3485 */
3486 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3487
3488 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3489 el_info, el_info_h);
3490
3491 megasas_return_cmd(instance, cmd);
3492
3493 return 0;
3494}
3495
3496/**
3497 * megasas_register_aen - Registers for asynchronous event notification
3498 * @instance: Adapter soft state
3499 * @seq_num: The starting sequence number
3500 * @class_locale: Class of the event
3501 *
3502 * This function subscribes for AEN for events beyond the @seq_num. It requests
3503 * to be notified if and only if the event is of type @class_locale
3504 */
3505static int
3506megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3507 u32 class_locale_word)
3508{
3509 int ret_val;
3510 struct megasas_cmd *cmd;
3511 struct megasas_dcmd_frame *dcmd;
3512 union megasas_evt_class_locale curr_aen;
3513 union megasas_evt_class_locale prev_aen;
3514
3515 /*
3516 * If there an AEN pending already (aen_cmd), check if the
3517 * class_locale of that pending AEN is inclusive of the new
3518 * AEN request we currently have. If it is, then we don't have
3519 * to do anything. In other words, whichever events the current
3520 * AEN request is subscribing to, have already been subscribed
3521 * to.
3522 *
3523 * If the old_cmd is _not_ inclusive, then we have to abort
3524 * that command, form a class_locale that is superset of both
3525 * old and current and re-issue to the FW
3526 */
3527
3528 curr_aen.word = class_locale_word;
3529
3530 if (instance->aen_cmd) {
3531
3532 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3533
3534 /*
3535 * A class whose enum value is smaller is inclusive of all
3536 * higher values. If a PROGRESS (= -1) was previously
3537 * registered, then a new registration requests for higher
3538 * classes need not be sent to FW. They are automatically
3539 * included.
3540 *
3541 * Locale numbers don't have such hierarchy. They are bitmap
3542 * values
3543 */
3544 if ((prev_aen.members.class <= curr_aen.members.class) &&
3545 !((prev_aen.members.locale & curr_aen.members.locale) ^
3546 curr_aen.members.locale)) {
3547 /*
3548 * Previously issued event registration includes
3549 * current request. Nothing to do.
3550 */
3551 return 0;
3552 } else {
3553 curr_aen.members.locale |= prev_aen.members.locale;
3554
3555 if (prev_aen.members.class < curr_aen.members.class)
3556 curr_aen.members.class = prev_aen.members.class;
3557
3558 instance->aen_cmd->abort_aen = 1;
3559 ret_val = megasas_issue_blocked_abort_cmd(instance,
3560 instance->
3561 aen_cmd);
3562
3563 if (ret_val) {
3564 printk(KERN_DEBUG "megasas: Failed to abort "
3565 "previous AEN command\n");
3566 return ret_val;
3567 }
3568 }
3569 }
3570
3571 cmd = megasas_get_cmd(instance);
3572
3573 if (!cmd)
3574 return -ENOMEM;
3575
3576 dcmd = &cmd->frame->dcmd;
3577
3578 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3579
3580 /*
3581 * Prepare DCMD for aen registration
3582 */
3583 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3584
3585 dcmd->cmd = MFI_CMD_DCMD;
3586 dcmd->cmd_status = 0x0;
3587 dcmd->sge_count = 1;
3588 dcmd->flags = MFI_FRAME_DIR_READ;
3589 dcmd->timeout = 0;
780a3762 3590 dcmd->pad_0 = 0;
39a98554 3591 instance->last_seq_num = seq_num;
c4a3e0a5
BS
3592 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3593 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3594 dcmd->mbox.w[0] = seq_num;
3595 dcmd->mbox.w[1] = curr_aen.word;
3596 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3597 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3598
f4c9a131
YB
3599 if (instance->aen_cmd != NULL) {
3600 megasas_return_cmd(instance, cmd);
3601 return 0;
3602 }
3603
c4a3e0a5
BS
3604 /*
3605 * Store reference to the cmd used to register for AEN. When an
3606 * application wants us to register for AEN, we have to abort this
3607 * cmd and re-register with a new EVENT LOCALE supplied by that app
3608 */
3609 instance->aen_cmd = cmd;
3610
3611 /*
3612 * Issue the aen registration frame
3613 */
0c79e681
YB
3614 instance->instancet->fire_cmd(instance,
3615 cmd->frame_phys_addr, 0, instance->reg_set);
c4a3e0a5
BS
3616
3617 return 0;
3618}
3619
3620/**
3621 * megasas_start_aen - Subscribes to AEN during driver load time
3622 * @instance: Adapter soft state
3623 */
3624static int megasas_start_aen(struct megasas_instance *instance)
3625{
3626 struct megasas_evt_log_info eli;
3627 union megasas_evt_class_locale class_locale;
3628
3629 /*
3630 * Get the latest sequence number from FW
3631 */
3632 memset(&eli, 0, sizeof(eli));
3633
3634 if (megasas_get_seq_num(instance, &eli))
3635 return -1;
3636
3637 /*
3638 * Register AEN with FW for latest sequence number plus 1
3639 */
3640 class_locale.members.reserved = 0;
3641 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3642 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3643
3644 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3645 class_locale.word);
3646}
3647
3648/**
3649 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3650 * @instance: Adapter soft state
3651 */
3652static int megasas_io_attach(struct megasas_instance *instance)
3653{
3654 struct Scsi_Host *host = instance->host;
3655
3656 /*
3657 * Export parameters required by SCSI mid-layer
3658 */
3659 host->irq = instance->pdev->irq;
3660 host->unique_id = instance->unique_id;
7bebf5c7
YB
3661 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3662 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3663 host->can_queue =
3664 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3665 } else
3666 host->can_queue =
3667 instance->max_fw_cmds - MEGASAS_INT_CMDS;
c4a3e0a5
BS
3668 host->this_id = instance->init_id;
3669 host->sg_tablesize = instance->max_num_sge;
1fd10685
YB
3670 /*
3671 * Check if the module parameter value for max_sectors can be used
3672 */
3673 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3674 instance->max_sectors_per_req = max_sectors;
3675 else {
3676 if (max_sectors) {
3677 if (((instance->pdev->device ==
3678 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3679 (instance->pdev->device ==
3680 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3681 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3682 instance->max_sectors_per_req = max_sectors;
3683 } else {
3684 printk(KERN_INFO "megasas: max_sectors should be > 0"
3685 "and <= %d (or < 1MB for GEN2 controller)\n",
3686 instance->max_sectors_per_req);
3687 }
3688 }
3689 }
3690
c4a3e0a5
BS
3691 host->max_sectors = instance->max_sectors_per_req;
3692 host->cmd_per_lun = 128;
3693 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3694 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3695 host->max_lun = MEGASAS_MAX_LUN;
122da302 3696 host->max_cmd_len = 16;
c4a3e0a5
BS
3697
3698 /*
3699 * Notify the mid-layer about the new controller
3700 */
3701 if (scsi_add_host(host, &instance->pdev->dev)) {
3702 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3703 return -ENODEV;
3704 }
3705
3706 /*
3707 * Trigger SCSI to scan our drives
3708 */
3709 scsi_scan_host(host);
3710 return 0;
3711}
3712
31ea7088 3713static int
3714megasas_set_dma_mask(struct pci_dev *pdev)
3715{
3716 /*
3717 * All our contollers are capable of performing 64-bit DMA
3718 */
3719 if (IS_DMA64) {
6a35528a 3720 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
31ea7088 3721
284901a9 3722 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 3723 goto fail_set_dma_mask;
3724 }
3725 } else {
284901a9 3726 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 3727 goto fail_set_dma_mask;
3728 }
3729 return 0;
3730
3731fail_set_dma_mask:
3732 return 1;
3733}
3734
c4a3e0a5
BS
3735/**
3736 * megasas_probe_one - PCI hotplug entry point
3737 * @pdev: PCI device structure
0d49016b 3738 * @id: PCI ids of supported hotplugged adapter
c4a3e0a5
BS
3739 */
3740static int __devinit
3741megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3742{
3743 int rval;
3744 struct Scsi_Host *host;
3745 struct megasas_instance *instance;
3746
3747 /*
3748 * Announce PCI information
3749 */
3750 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3751 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3752 pdev->subsystem_device);
3753
3754 printk("bus %d:slot %d:func %d\n",
3755 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3756
3757 /*
3758 * PCI prepping: enable device set bus mastering and dma mask
3759 */
aeab3fd7 3760 rval = pci_enable_device_mem(pdev);
c4a3e0a5
BS
3761
3762 if (rval) {
3763 return rval;
3764 }
3765
3766 pci_set_master(pdev);
3767
31ea7088 3768 if (megasas_set_dma_mask(pdev))
3769 goto fail_set_dma_mask;
c4a3e0a5
BS
3770
3771 host = scsi_host_alloc(&megasas_template,
3772 sizeof(struct megasas_instance));
3773
3774 if (!host) {
3775 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3776 goto fail_alloc_instance;
3777 }
3778
3779 instance = (struct megasas_instance *)host->hostdata;
3780 memset(instance, 0, sizeof(*instance));
39a98554 3781 atomic_set( &instance->fw_reset_no_pci_access, 0 );
c4a3e0a5
BS
3782
3783 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3784 &instance->producer_h);
3785 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3786 &instance->consumer_h);
3787
3788 if (!instance->producer || !instance->consumer) {
3789 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3790 "producer, consumer\n");
3791 goto fail_alloc_dma_buf;
3792 }
3793
3794 *instance->producer = 0;
3795 *instance->consumer = 0;
c3518837 3796 megasas_poll_wait_aen = 0;
f4c9a131 3797 instance->flag_ieee = 0;
7e8a75f4 3798 instance->ev = NULL;
39a98554 3799 instance->issuepend_done = 1;
3800 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3801 megasas_poll_wait_aen = 0;
c4a3e0a5
BS
3802
3803 instance->evt_detail = pci_alloc_consistent(pdev,
3804 sizeof(struct
3805 megasas_evt_detail),
3806 &instance->evt_detail_h);
3807
3808 if (!instance->evt_detail) {
3809 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3810 "event detail structure\n");
3811 goto fail_alloc_dma_buf;
3812 }
3813
3814 /*
3815 * Initialize locks and queues
3816 */
3817 INIT_LIST_HEAD(&instance->cmd_pool);
39a98554 3818 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
c4a3e0a5 3819
e4a082c7
SP
3820 atomic_set(&instance->fw_outstanding,0);
3821
c4a3e0a5
BS
3822 init_waitqueue_head(&instance->int_cmd_wait_q);
3823 init_waitqueue_head(&instance->abort_cmd_wait_q);
3824
3825 spin_lock_init(&instance->cmd_pool_lock);
39a98554 3826 spin_lock_init(&instance->hba_lock);
7343eb65 3827 spin_lock_init(&instance->completion_lock);
c3518837 3828 spin_lock_init(&poll_aen_lock);
c4a3e0a5 3829
e5a69e27 3830 mutex_init(&instance->aen_mutex);
c4a3e0a5
BS
3831
3832 /*
3833 * Initialize PCI related and misc parameters
3834 */
3835 instance->pdev = pdev;
3836 instance->host = host;
3837 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3838 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3839
7bebf5c7
YB
3840 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3841 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
f4c9a131 3842 instance->flag_ieee = 1;
7bebf5c7
YB
3843 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3844 } else
3845 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3846
658dcedb 3847 megasas_dbg_lvl = 0;
05e9ebbe 3848 instance->flag = 0;
0c79e681 3849 instance->unload = 1;
05e9ebbe 3850 instance->last_time = 0;
39a98554 3851 instance->disableOnlineCtrlReset = 1;
3852
3853 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
658dcedb 3854
c4a3e0a5
BS
3855 /*
3856 * Initialize MFI Firmware
3857 */
3858 if (megasas_init_mfi(instance))
3859 goto fail_init_mfi;
3860
80d9da98
AR
3861 /* Try to enable MSI-X */
3862 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078R) &&
3863 (instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078DE) &&
3864 (instance->pdev->device != PCI_DEVICE_ID_LSI_VERDE_ZCR) &&
3865 !msix_disable && !pci_enable_msix(instance->pdev,
3866 &instance->msixentry, 1))
3867 instance->msi_flag = 1;
3868
c4a3e0a5
BS
3869 /*
3870 * Register IRQ
3871 */
80d9da98
AR
3872 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
3873 pdev->irq, megasas_isr,
3874 IRQF_SHARED, "megasas", instance)) {
c4a3e0a5
BS
3875 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3876 goto fail_irq;
3877 }
3878
1341c939 3879 instance->instancet->enable_intr(instance->reg_set);
c4a3e0a5
BS
3880
3881 /*
3882 * Store instance in PCI softstate
3883 */
3884 pci_set_drvdata(pdev, instance);
3885
3886 /*
3887 * Add this controller to megasas_mgmt_info structure so that it
3888 * can be exported to management applications
3889 */
3890 megasas_mgmt_info.count++;
3891 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3892 megasas_mgmt_info.max_index++;
3893
3894 /*
3895 * Initiate AEN (Asynchronous Event Notification)
3896 */
3897 if (megasas_start_aen(instance)) {
3898 printk(KERN_DEBUG "megasas: start aen failed\n");
3899 goto fail_start_aen;
3900 }
3901
3902 /*
3903 * Register with SCSI mid-layer
3904 */
3905 if (megasas_io_attach(instance))
3906 goto fail_io_attach;
3907
0c79e681 3908 instance->unload = 0;
c4a3e0a5
BS
3909 return 0;
3910
3911 fail_start_aen:
3912 fail_io_attach:
3913 megasas_mgmt_info.count--;
3914 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3915 megasas_mgmt_info.max_index--;
3916
3917 pci_set_drvdata(pdev, NULL);
b274cab7 3918 instance->instancet->disable_intr(instance->reg_set);
80d9da98
AR
3919 free_irq(instance->msi_flag ? instance->msixentry.vector :
3920 instance->pdev->irq, instance);
3921 if (instance->msi_flag)
3922 pci_disable_msix(instance->pdev);
c4a3e0a5
BS
3923 megasas_release_mfi(instance);
3924
3925 fail_irq:
3926 fail_init_mfi:
3927 fail_alloc_dma_buf:
3928 if (instance->evt_detail)
3929 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3930 instance->evt_detail,
3931 instance->evt_detail_h);
3932
3933 if (instance->producer)
3934 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3935 instance->producer_h);
3936 if (instance->consumer)
3937 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3938 instance->consumer_h);
3939 scsi_host_put(host);
3940
3941 fail_alloc_instance:
3942 fail_set_dma_mask:
3943 pci_disable_device(pdev);
3944
3945 return -ENODEV;
3946}
3947
3948/**
3949 * megasas_flush_cache - Requests FW to flush all its caches
3950 * @instance: Adapter soft state
3951 */
3952static void megasas_flush_cache(struct megasas_instance *instance)
3953{
3954 struct megasas_cmd *cmd;
3955 struct megasas_dcmd_frame *dcmd;
3956
39a98554 3957 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3958 return;
3959
c4a3e0a5
BS
3960 cmd = megasas_get_cmd(instance);
3961
3962 if (!cmd)
3963 return;
3964
3965 dcmd = &cmd->frame->dcmd;
3966
3967 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3968
3969 dcmd->cmd = MFI_CMD_DCMD;
3970 dcmd->cmd_status = 0x0;
3971 dcmd->sge_count = 0;
3972 dcmd->flags = MFI_FRAME_DIR_NONE;
3973 dcmd->timeout = 0;
780a3762 3974 dcmd->pad_0 = 0;
c4a3e0a5
BS
3975 dcmd->data_xfer_len = 0;
3976 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3977 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3978
3979 megasas_issue_blocked_cmd(instance, cmd);
3980
3981 megasas_return_cmd(instance, cmd);
3982
3983 return;
3984}
3985
3986/**
3987 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3988 * @instance: Adapter soft state
31ea7088 3989 * @opcode: Shutdown/Hibernate
c4a3e0a5 3990 */
31ea7088 3991static void megasas_shutdown_controller(struct megasas_instance *instance,
3992 u32 opcode)
c4a3e0a5
BS
3993{
3994 struct megasas_cmd *cmd;
3995 struct megasas_dcmd_frame *dcmd;
3996
39a98554 3997 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3998 return;
3999
c4a3e0a5
BS
4000 cmd = megasas_get_cmd(instance);
4001
4002 if (!cmd)
4003 return;
4004
4005 if (instance->aen_cmd)
4006 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4007
4008 dcmd = &cmd->frame->dcmd;
4009
4010 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4011
4012 dcmd->cmd = MFI_CMD_DCMD;
4013 dcmd->cmd_status = 0x0;
4014 dcmd->sge_count = 0;
4015 dcmd->flags = MFI_FRAME_DIR_NONE;
4016 dcmd->timeout = 0;
780a3762 4017 dcmd->pad_0 = 0;
c4a3e0a5 4018 dcmd->data_xfer_len = 0;
31ea7088 4019 dcmd->opcode = opcode;
c4a3e0a5
BS
4020
4021 megasas_issue_blocked_cmd(instance, cmd);
4022
4023 megasas_return_cmd(instance, cmd);
4024
4025 return;
4026}
4027
33139b21 4028#ifdef CONFIG_PM
31ea7088 4029/**
ad84db2e 4030 * megasas_suspend - driver suspend entry point
4031 * @pdev: PCI device structure
31ea7088 4032 * @state: PCI power state to suspend routine
4033 */
33139b21 4034static int
31ea7088 4035megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4036{
4037 struct Scsi_Host *host;
4038 struct megasas_instance *instance;
4039
4040 instance = pci_get_drvdata(pdev);
4041 host = instance->host;
0c79e681 4042 instance->unload = 1;
31ea7088 4043
ad84db2e 4044 if (poll_mode_io)
4045 del_timer_sync(&instance->io_completion_timer);
4046
31ea7088 4047 megasas_flush_cache(instance);
4048 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7e8a75f4
YB
4049
4050 /* cancel the delayed work if this work still in queue */
4051 if (instance->ev != NULL) {
4052 struct megasas_aen_event *ev = instance->ev;
4053 cancel_delayed_work(
4054 (struct delayed_work *)&ev->hotplug_work);
4055 flush_scheduled_work();
4056 instance->ev = NULL;
4057 }
4058
31ea7088 4059 tasklet_kill(&instance->isr_tasklet);
4060
4061 pci_set_drvdata(instance->pdev, instance);
4062 instance->instancet->disable_intr(instance->reg_set);
80d9da98
AR
4063 free_irq(instance->msi_flag ? instance->msixentry.vector :
4064 instance->pdev->irq, instance);
4065 if (instance->msi_flag)
4066 pci_disable_msix(instance->pdev);
31ea7088 4067
4068 pci_save_state(pdev);
4069 pci_disable_device(pdev);
4070
4071 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4072
4073 return 0;
4074}
4075
4076/**
4077 * megasas_resume- driver resume entry point
4078 * @pdev: PCI device structure
4079 */
33139b21 4080static int
31ea7088 4081megasas_resume(struct pci_dev *pdev)
4082{
4083 int rval;
4084 struct Scsi_Host *host;
4085 struct megasas_instance *instance;
4086
4087 instance = pci_get_drvdata(pdev);
4088 host = instance->host;
4089 pci_set_power_state(pdev, PCI_D0);
4090 pci_enable_wake(pdev, PCI_D0, 0);
4091 pci_restore_state(pdev);
4092
4093 /*
4094 * PCI prepping: enable device set bus mastering and dma mask
4095 */
aeab3fd7 4096 rval = pci_enable_device_mem(pdev);
31ea7088 4097
4098 if (rval) {
4099 printk(KERN_ERR "megasas: Enable device failed\n");
4100 return rval;
4101 }
4102
4103 pci_set_master(pdev);
4104
4105 if (megasas_set_dma_mask(pdev))
4106 goto fail_set_dma_mask;
4107
4108 /*
4109 * Initialize MFI Firmware
4110 */
4111
4112 *instance->producer = 0;
4113 *instance->consumer = 0;
4114
4115 atomic_set(&instance->fw_outstanding, 0);
4116
4117 /*
4118 * We expect the FW state to be READY
4119 */
4120 if (megasas_transition_to_ready(instance))
4121 goto fail_ready_state;
4122
4123 if (megasas_issue_init_mfi(instance))
4124 goto fail_init_mfi;
4125
4126 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
4127 (unsigned long)instance);
4128
80d9da98
AR
4129 /* Now re-enable MSI-X */
4130 if (instance->msi_flag)
4131 pci_enable_msix(instance->pdev, &instance->msixentry, 1);
4132
31ea7088 4133 /*
4134 * Register IRQ
4135 */
80d9da98
AR
4136 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
4137 pdev->irq, megasas_isr,
4138 IRQF_SHARED, "megasas", instance)) {
31ea7088 4139 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4140 goto fail_irq;
4141 }
4142
4143 instance->instancet->enable_intr(instance->reg_set);
4144
4145 /*
4146 * Initiate AEN (Asynchronous Event Notification)
4147 */
4148 if (megasas_start_aen(instance))
4149 printk(KERN_ERR "megasas: Start AEN failed\n");
4150
ad84db2e 4151 /* Initialize the cmd completion timer */
4152 if (poll_mode_io)
4153 megasas_start_timer(instance, &instance->io_completion_timer,
4154 megasas_io_completion_timer,
4155 MEGASAS_COMPLETION_TIMER_INTERVAL);
0c79e681
YB
4156 instance->unload = 0;
4157
31ea7088 4158 return 0;
4159
4160fail_irq:
4161fail_init_mfi:
4162 if (instance->evt_detail)
4163 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4164 instance->evt_detail,
4165 instance->evt_detail_h);
4166
4167 if (instance->producer)
4168 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4169 instance->producer_h);
4170 if (instance->consumer)
4171 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4172 instance->consumer_h);
4173 scsi_host_put(host);
4174
4175fail_set_dma_mask:
4176fail_ready_state:
4177
4178 pci_disable_device(pdev);
4179
4180 return -ENODEV;
4181}
33139b21
JS
4182#else
4183#define megasas_suspend NULL
4184#define megasas_resume NULL
4185#endif
31ea7088 4186
c4a3e0a5
BS
4187/**
4188 * megasas_detach_one - PCI hot"un"plug entry point
4189 * @pdev: PCI device structure
4190 */
33139b21 4191static void __devexit megasas_detach_one(struct pci_dev *pdev)
c4a3e0a5
BS
4192{
4193 int i;
4194 struct Scsi_Host *host;
4195 struct megasas_instance *instance;
4196
4197 instance = pci_get_drvdata(pdev);
c3518837 4198 instance->unload = 1;
c4a3e0a5
BS
4199 host = instance->host;
4200
ad84db2e 4201 if (poll_mode_io)
4202 del_timer_sync(&instance->io_completion_timer);
4203
c4a3e0a5
BS
4204 scsi_remove_host(instance->host);
4205 megasas_flush_cache(instance);
31ea7088 4206 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7e8a75f4
YB
4207
4208 /* cancel the delayed work if this work still in queue*/
4209 if (instance->ev != NULL) {
4210 struct megasas_aen_event *ev = instance->ev;
4211 cancel_delayed_work(
4212 (struct delayed_work *)&ev->hotplug_work);
4213 flush_scheduled_work();
4214 instance->ev = NULL;
4215 }
4216
5d018ad0 4217 tasklet_kill(&instance->isr_tasklet);
c4a3e0a5
BS
4218
4219 /*
4220 * Take the instance off the instance array. Note that we will not
4221 * decrement the max_index. We let this array be sparse array
4222 */
4223 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4224 if (megasas_mgmt_info.instance[i] == instance) {
4225 megasas_mgmt_info.count--;
4226 megasas_mgmt_info.instance[i] = NULL;
4227
4228 break;
4229 }
4230 }
4231
4232 pci_set_drvdata(instance->pdev, NULL);
4233
b274cab7 4234 instance->instancet->disable_intr(instance->reg_set);
c4a3e0a5 4235
80d9da98
AR
4236 free_irq(instance->msi_flag ? instance->msixentry.vector :
4237 instance->pdev->irq, instance);
4238 if (instance->msi_flag)
4239 pci_disable_msix(instance->pdev);
c4a3e0a5
BS
4240
4241 megasas_release_mfi(instance);
4242
4243 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4244 instance->evt_detail, instance->evt_detail_h);
4245
4246 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4247 instance->producer_h);
4248
4249 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4250 instance->consumer_h);
4251
4252 scsi_host_put(host);
4253
4254 pci_set_drvdata(pdev, NULL);
4255
4256 pci_disable_device(pdev);
4257
4258 return;
4259}
4260
4261/**
4262 * megasas_shutdown - Shutdown entry point
4263 * @device: Generic device structure
4264 */
4265static void megasas_shutdown(struct pci_dev *pdev)
4266{
4267 struct megasas_instance *instance = pci_get_drvdata(pdev);
0c79e681 4268 instance->unload = 1;
c4a3e0a5 4269 megasas_flush_cache(instance);
530e6fc1 4270 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
c4a3e0a5
BS
4271}
4272
4273/**
4274 * megasas_mgmt_open - char node "open" entry point
4275 */
4276static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4277{
4278 /*
4279 * Allow only those users with admin rights
4280 */
4281 if (!capable(CAP_SYS_ADMIN))
4282 return -EACCES;
4283
4284 return 0;
4285}
4286
c4a3e0a5
BS
4287/**
4288 * megasas_mgmt_fasync - Async notifier registration from applications
4289 *
4290 * This function adds the calling process to a driver global queue. When an
4291 * event occurs, SIGIO will be sent to all processes in this queue.
4292 */
4293static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4294{
4295 int rc;
4296
0b950672 4297 mutex_lock(&megasas_async_queue_mutex);
c4a3e0a5
BS
4298
4299 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4300
0b950672 4301 mutex_unlock(&megasas_async_queue_mutex);
c4a3e0a5
BS
4302
4303 if (rc >= 0) {
4304 /* For sanity check when we get ioctl */
4305 filep->private_data = filep;
4306 return 0;
4307 }
4308
4309 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4310
4311 return rc;
4312}
4313
c3518837
YB
4314/**
4315 * megasas_mgmt_poll - char node "poll" entry point
4316 * */
4317static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4318{
4319 unsigned int mask;
4320 unsigned long flags;
4321 poll_wait(file, &megasas_poll_wait, wait);
4322 spin_lock_irqsave(&poll_aen_lock, flags);
4323 if (megasas_poll_wait_aen)
4324 mask = (POLLIN | POLLRDNORM);
4325 else
4326 mask = 0;
4327 spin_unlock_irqrestore(&poll_aen_lock, flags);
4328 return mask;
4329}
4330
c4a3e0a5
BS
4331/**
4332 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4333 * @instance: Adapter soft state
4334 * @argp: User's ioctl packet
4335 */
4336static int
4337megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4338 struct megasas_iocpacket __user * user_ioc,
4339 struct megasas_iocpacket *ioc)
4340{
4341 struct megasas_sge32 *kern_sge32;
4342 struct megasas_cmd *cmd;
4343 void *kbuff_arr[MAX_IOCTL_SGE];
4344 dma_addr_t buf_handle = 0;
4345 int error = 0, i;
4346 void *sense = NULL;
4347 dma_addr_t sense_handle;
7b2519af 4348 unsigned long *sense_ptr;
c4a3e0a5
BS
4349
4350 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4351
4352 if (ioc->sge_count > MAX_IOCTL_SGE) {
4353 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4354 ioc->sge_count, MAX_IOCTL_SGE);
4355 return -EINVAL;
4356 }
4357
4358 cmd = megasas_get_cmd(instance);
4359 if (!cmd) {
4360 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4361 return -ENOMEM;
4362 }
4363
4364 /*
4365 * User's IOCTL packet has 2 frames (maximum). Copy those two
4366 * frames into our cmd's frames. cmd->frame's context will get
4367 * overwritten when we copy from user's frames. So set that value
4368 * alone separately
4369 */
4370 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4371 cmd->frame->hdr.context = cmd->index;
c3518837 4372 cmd->frame->hdr.pad_0 = 0;
c4a3e0a5
BS
4373
4374 /*
4375 * The management interface between applications and the fw uses
4376 * MFI frames. E.g, RAID configuration changes, LD property changes
4377 * etc are accomplishes through different kinds of MFI frames. The
4378 * driver needs to care only about substituting user buffers with
4379 * kernel buffers in SGLs. The location of SGL is embedded in the
4380 * struct iocpacket itself.
4381 */
4382 kern_sge32 = (struct megasas_sge32 *)
4383 ((unsigned long)cmd->frame + ioc->sgl_off);
4384
4385 /*
4386 * For each user buffer, create a mirror buffer and copy in
4387 */
4388 for (i = 0; i < ioc->sge_count; i++) {
9f35fa8a 4389 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
c4a3e0a5 4390 ioc->sgl[i].iov_len,
9f35fa8a 4391 &buf_handle, GFP_KERNEL);
c4a3e0a5
BS
4392 if (!kbuff_arr[i]) {
4393 printk(KERN_DEBUG "megasas: Failed to alloc "
4394 "kernel SGL buffer for IOCTL \n");
4395 error = -ENOMEM;
4396 goto out;
4397 }
4398
4399 /*
4400 * We don't change the dma_coherent_mask, so
4401 * pci_alloc_consistent only returns 32bit addresses
4402 */
4403 kern_sge32[i].phys_addr = (u32) buf_handle;
4404 kern_sge32[i].length = ioc->sgl[i].iov_len;
4405
4406 /*
4407 * We created a kernel buffer corresponding to the
4408 * user buffer. Now copy in from the user buffer
4409 */
4410 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4411 (u32) (ioc->sgl[i].iov_len))) {
4412 error = -EFAULT;
4413 goto out;
4414 }
4415 }
4416
4417 if (ioc->sense_len) {
9f35fa8a
SP
4418 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4419 &sense_handle, GFP_KERNEL);
c4a3e0a5
BS
4420 if (!sense) {
4421 error = -ENOMEM;
4422 goto out;
4423 }
4424
4425 sense_ptr =
7b2519af 4426 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
c4a3e0a5
BS
4427 *sense_ptr = sense_handle;
4428 }
4429
4430 /*
4431 * Set the sync_cmd flag so that the ISR knows not to complete this
4432 * cmd to the SCSI mid-layer
4433 */
4434 cmd->sync_cmd = 1;
4435 megasas_issue_blocked_cmd(instance, cmd);
4436 cmd->sync_cmd = 0;
4437
4438 /*
4439 * copy out the kernel buffers to user buffers
4440 */
4441 for (i = 0; i < ioc->sge_count; i++) {
4442 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4443 ioc->sgl[i].iov_len)) {
4444 error = -EFAULT;
4445 goto out;
4446 }
4447 }
4448
4449 /*
4450 * copy out the sense
4451 */
4452 if (ioc->sense_len) {
4453 /*
b70a41e0 4454 * sense_ptr points to the location that has the user
c4a3e0a5
BS
4455 * sense buffer address
4456 */
7b2519af
YB
4457 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4458 ioc->sense_off);
c4a3e0a5 4459
b70a41e0 4460 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4461 sense, ioc->sense_len)) {
b10c36a5 4462 printk(KERN_ERR "megasas: Failed to copy out to user "
4463 "sense data\n");
c4a3e0a5
BS
4464 error = -EFAULT;
4465 goto out;
4466 }
4467 }
4468
4469 /*
4470 * copy the status codes returned by the fw
4471 */
4472 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4473 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4474 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4475 error = -EFAULT;
4476 }
4477
4478 out:
4479 if (sense) {
9f35fa8a 4480 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
c4a3e0a5
BS
4481 sense, sense_handle);
4482 }
4483
4484 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
9f35fa8a 4485 dma_free_coherent(&instance->pdev->dev,
c4a3e0a5
BS
4486 kern_sge32[i].length,
4487 kbuff_arr[i], kern_sge32[i].phys_addr);
4488 }
4489
4490 megasas_return_cmd(instance, cmd);
4491 return error;
4492}
4493
c4a3e0a5
BS
4494static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4495{
4496 struct megasas_iocpacket __user *user_ioc =
4497 (struct megasas_iocpacket __user *)arg;
4498 struct megasas_iocpacket *ioc;
4499 struct megasas_instance *instance;
4500 int error;
39a98554 4501 int i;
4502 unsigned long flags;
4503 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
4504
4505 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4506 if (!ioc)
4507 return -ENOMEM;
4508
4509 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4510 error = -EFAULT;
4511 goto out_kfree_ioc;
4512 }
4513
4514 instance = megasas_lookup_instance(ioc->host_no);
4515 if (!instance) {
4516 error = -ENODEV;
4517 goto out_kfree_ioc;
4518 }
4519
39a98554 4520 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4521 printk(KERN_ERR "Controller in crit error\n");
0c79e681
YB
4522 error = -ENODEV;
4523 goto out_kfree_ioc;
4524 }
4525
4526 if (instance->unload == 1) {
4527 error = -ENODEV;
4528 goto out_kfree_ioc;
4529 }
4530
c4a3e0a5
BS
4531 /*
4532 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4533 */
4534 if (down_interruptible(&instance->ioctl_sem)) {
4535 error = -ERESTARTSYS;
4536 goto out_kfree_ioc;
4537 }
39a98554 4538
4539 for (i = 0; i < wait_time; i++) {
4540
4541 spin_lock_irqsave(&instance->hba_lock, flags);
4542 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4543 spin_unlock_irqrestore(&instance->hba_lock, flags);
4544 break;
4545 }
4546 spin_unlock_irqrestore(&instance->hba_lock, flags);
4547
4548 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4549 printk(KERN_NOTICE "megasas: waiting"
4550 "for controller reset to finish\n");
4551 }
4552
4553 msleep(1000);
4554 }
4555
4556 spin_lock_irqsave(&instance->hba_lock, flags);
4557 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4558 spin_unlock_irqrestore(&instance->hba_lock, flags);
4559
4560 printk(KERN_ERR "megaraid_sas: timed out while"
4561 "waiting for HBA to recover\n");
4562 error = -ENODEV;
4563 goto out_kfree_ioc;
4564 }
4565 spin_unlock_irqrestore(&instance->hba_lock, flags);
4566
c4a3e0a5
BS
4567 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4568 up(&instance->ioctl_sem);
4569
4570 out_kfree_ioc:
4571 kfree(ioc);
4572 return error;
4573}
4574
4575static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4576{
4577 struct megasas_instance *instance;
4578 struct megasas_aen aen;
4579 int error;
39a98554 4580 int i;
4581 unsigned long flags;
4582 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
c4a3e0a5
BS
4583
4584 if (file->private_data != file) {
4585 printk(KERN_DEBUG "megasas: fasync_helper was not "
4586 "called first\n");
4587 return -EINVAL;
4588 }
4589
4590 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4591 return -EFAULT;
4592
4593 instance = megasas_lookup_instance(aen.host_no);
4594
4595 if (!instance)
4596 return -ENODEV;
4597
39a98554 4598 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4599 return -ENODEV;
0c79e681
YB
4600 }
4601
4602 if (instance->unload == 1) {
4603 return -ENODEV;
4604 }
4605
39a98554 4606 for (i = 0; i < wait_time; i++) {
4607
4608 spin_lock_irqsave(&instance->hba_lock, flags);
4609 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4610 spin_unlock_irqrestore(&instance->hba_lock,
4611 flags);
4612 break;
4613 }
4614
4615 spin_unlock_irqrestore(&instance->hba_lock, flags);
4616
4617 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4618 printk(KERN_NOTICE "megasas: waiting for"
4619 "controller reset to finish\n");
4620 }
4621
4622 msleep(1000);
4623 }
4624
4625 spin_lock_irqsave(&instance->hba_lock, flags);
4626 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4627 spin_unlock_irqrestore(&instance->hba_lock, flags);
4628 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4629 "for HBA to recover.\n");
4630 return -ENODEV;
4631 }
4632 spin_unlock_irqrestore(&instance->hba_lock, flags);
4633
e5a69e27 4634 mutex_lock(&instance->aen_mutex);
c4a3e0a5
BS
4635 error = megasas_register_aen(instance, aen.seq_num,
4636 aen.class_locale_word);
e5a69e27 4637 mutex_unlock(&instance->aen_mutex);
c4a3e0a5
BS
4638 return error;
4639}
4640
4641/**
4642 * megasas_mgmt_ioctl - char node ioctl entry point
4643 */
4644static long
4645megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4646{
4647 switch (cmd) {
4648 case MEGASAS_IOC_FIRMWARE:
4649 return megasas_mgmt_ioctl_fw(file, arg);
4650
4651 case MEGASAS_IOC_GET_AEN:
4652 return megasas_mgmt_ioctl_aen(file, arg);
4653 }
4654
4655 return -ENOTTY;
4656}
4657
4658#ifdef CONFIG_COMPAT
4659static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4660{
4661 struct compat_megasas_iocpacket __user *cioc =
4662 (struct compat_megasas_iocpacket __user *)arg;
4663 struct megasas_iocpacket __user *ioc =
4664 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4665 int i;
4666 int error = 0;
b3dc1a21 4667 compat_uptr_t ptr;
c4a3e0a5 4668
83aabc1b
JG
4669 if (clear_user(ioc, sizeof(*ioc)))
4670 return -EFAULT;
c4a3e0a5
BS
4671
4672 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4673 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4674 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4675 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4676 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4677 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4678 return -EFAULT;
4679
b3dc1a21
TH
4680 /*
4681 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4682 * sense_len is not null, so prepare the 64bit value under
4683 * the same condition.
4684 */
4685 if (ioc->sense_len) {
4686 void __user **sense_ioc_ptr =
4687 (void __user **)(ioc->frame.raw + ioc->sense_off);
4688 compat_uptr_t *sense_cioc_ptr =
4689 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4690 if (get_user(ptr, sense_cioc_ptr) ||
4691 put_user(compat_ptr(ptr), sense_ioc_ptr))
4692 return -EFAULT;
4693 }
c4a3e0a5 4694
b3dc1a21 4695 for (i = 0; i < MAX_IOCTL_SGE; i++) {
c4a3e0a5
BS
4696 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4697 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4698 copy_in_user(&ioc->sgl[i].iov_len,
4699 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4700 return -EFAULT;
4701 }
4702
4703 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4704
4705 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4706 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4707 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4708 return -EFAULT;
4709 }
4710 return error;
4711}
4712
4713static long
4714megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4715 unsigned long arg)
4716{
4717 switch (cmd) {
cb59aa6a
SP
4718 case MEGASAS_IOC_FIRMWARE32:
4719 return megasas_mgmt_compat_ioctl_fw(file, arg);
c4a3e0a5
BS
4720 case MEGASAS_IOC_GET_AEN:
4721 return megasas_mgmt_ioctl_aen(file, arg);
4722 }
4723
4724 return -ENOTTY;
4725}
4726#endif
4727
4728/*
4729 * File operations structure for management interface
4730 */
00977a59 4731static const struct file_operations megasas_mgmt_fops = {
c4a3e0a5
BS
4732 .owner = THIS_MODULE,
4733 .open = megasas_mgmt_open,
c4a3e0a5
BS
4734 .fasync = megasas_mgmt_fasync,
4735 .unlocked_ioctl = megasas_mgmt_ioctl,
c3518837 4736 .poll = megasas_mgmt_poll,
c4a3e0a5
BS
4737#ifdef CONFIG_COMPAT
4738 .compat_ioctl = megasas_mgmt_compat_ioctl,
4739#endif
6038f373 4740 .llseek = noop_llseek,
c4a3e0a5
BS
4741};
4742
4743/*
4744 * PCI hotplug support registration structure
4745 */
4746static struct pci_driver megasas_pci_driver = {
4747
4748 .name = "megaraid_sas",
4749 .id_table = megasas_pci_table,
4750 .probe = megasas_probe_one,
4751 .remove = __devexit_p(megasas_detach_one),
31ea7088 4752 .suspend = megasas_suspend,
4753 .resume = megasas_resume,
c4a3e0a5
BS
4754 .shutdown = megasas_shutdown,
4755};
4756
4757/*
4758 * Sysfs driver attributes
4759 */
4760static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4761{
4762 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4763 MEGASAS_VERSION);
4764}
4765
4766static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4767
4768static ssize_t
4769megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4770{
4771 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4772 MEGASAS_RELDATE);
4773}
4774
4775static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
4776 NULL);
4777
72c4fd36
YB
4778static ssize_t
4779megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4780{
4781 return sprintf(buf, "%u\n", support_poll_for_event);
4782}
4783
4784static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4785 megasas_sysfs_show_support_poll_for_event, NULL);
4786
837f5fe8
YB
4787 static ssize_t
4788megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
4789{
4790 return sprintf(buf, "%u\n", support_device_change);
4791}
4792
4793static DRIVER_ATTR(support_device_change, S_IRUGO,
4794 megasas_sysfs_show_support_device_change, NULL);
4795
658dcedb
SP
4796static ssize_t
4797megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4798{
ad84db2e 4799 return sprintf(buf, "%u\n", megasas_dbg_lvl);
658dcedb
SP
4800}
4801
4802static ssize_t
4803megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4804{
4805 int retval = count;
4806 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4807 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4808 retval = -EINVAL;
4809 }
4810 return retval;
4811}
4812
66dca9b8 4813static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
ad84db2e 4814 megasas_sysfs_set_dbg_lvl);
4815
4816static ssize_t
4817megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4818{
4819 return sprintf(buf, "%u\n", poll_mode_io);
4820}
4821
4822static ssize_t
4823megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4824 const char *buf, size_t count)
4825{
4826 int retval = count;
4827 int tmp = poll_mode_io;
4828 int i;
4829 struct megasas_instance *instance;
4830
4831 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4832 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4833 retval = -EINVAL;
4834 }
4835
4836 /*
4837 * Check if poll_mode_io is already set or is same as previous value
4838 */
4839 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4840 goto out;
4841
4842 if (poll_mode_io) {
4843 /*
4844 * Start timers for all adapters
4845 */
4846 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4847 instance = megasas_mgmt_info.instance[i];
4848 if (instance) {
4849 megasas_start_timer(instance,
4850 &instance->io_completion_timer,
4851 megasas_io_completion_timer,
4852 MEGASAS_COMPLETION_TIMER_INTERVAL);
4853 }
4854 }
4855 } else {
4856 /*
4857 * Delete timers for all adapters
4858 */
4859 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4860 instance = megasas_mgmt_info.instance[i];
4861 if (instance)
4862 del_timer_sync(&instance->io_completion_timer);
4863 }
4864 }
4865
4866out:
4867 return retval;
4868}
4869
7e8a75f4
YB
4870static void
4871megasas_aen_polling(struct work_struct *work)
4872{
4873 struct megasas_aen_event *ev =
4874 container_of(work, struct megasas_aen_event, hotplug_work);
4875 struct megasas_instance *instance = ev->instance;
4876 union megasas_evt_class_locale class_locale;
4877 struct Scsi_Host *host;
4878 struct scsi_device *sdev1;
4879 u16 pd_index = 0;
c9786842 4880 u16 ld_index = 0;
7e8a75f4
YB
4881 int i, j, doscan = 0;
4882 u32 seq_num;
4883 int error;
4884
4885 if (!instance) {
4886 printk(KERN_ERR "invalid instance!\n");
4887 kfree(ev);
4888 return;
4889 }
4890 instance->ev = NULL;
4891 host = instance->host;
4892 if (instance->evt_detail) {
4893
4894 switch (instance->evt_detail->code) {
4895 case MR_EVT_PD_INSERTED:
c9786842
YB
4896 if (megasas_get_pd_list(instance) == 0) {
4897 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4898 for (j = 0;
4899 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4900 j++) {
4901
4902 pd_index =
4903 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4904
4905 sdev1 =
4906 scsi_device_lookup(host, i, j, 0);
4907
4908 if (instance->pd_list[pd_index].driveState
4909 == MR_PD_STATE_SYSTEM) {
4910 if (!sdev1) {
4911 scsi_add_device(host, i, j, 0);
4912 }
4913
4914 if (sdev1)
4915 scsi_device_put(sdev1);
4916 }
4917 }
4918 }
4919 }
4920 doscan = 0;
4921 break;
4922
7e8a75f4 4923 case MR_EVT_PD_REMOVED:
c9786842
YB
4924 if (megasas_get_pd_list(instance) == 0) {
4925 megasas_get_pd_list(instance);
4926 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4927 for (j = 0;
4928 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4929 j++) {
4930
4931 pd_index =
4932 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4933
4934 sdev1 =
4935 scsi_device_lookup(host, i, j, 0);
4936
4937 if (instance->pd_list[pd_index].driveState
4938 == MR_PD_STATE_SYSTEM) {
4939 if (sdev1) {
4940 scsi_device_put(sdev1);
4941 }
4942 } else {
4943 if (sdev1) {
4944 scsi_remove_device(sdev1);
4945 scsi_device_put(sdev1);
4946 }
4947 }
4948 }
4949 }
4950 }
4951 doscan = 0;
4952 break;
4953
4954 case MR_EVT_LD_OFFLINE:
4955 case MR_EVT_LD_DELETED:
4956 megasas_get_ld_list(instance);
4957 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4958 for (j = 0;
4959 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4960 j++) {
4961
4962 ld_index =
4963 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4964
4965 sdev1 = scsi_device_lookup(host,
4966 i + MEGASAS_MAX_LD_CHANNELS,
4967 j,
4968 0);
4969
4970 if (instance->ld_ids[ld_index] != 0xff) {
4971 if (sdev1) {
4972 scsi_device_put(sdev1);
4973 }
4974 } else {
4975 if (sdev1) {
4976 scsi_remove_device(sdev1);
4977 scsi_device_put(sdev1);
4978 }
4979 }
4980 }
4981 }
4982 doscan = 0;
4983 break;
4984 case MR_EVT_LD_CREATED:
4985 megasas_get_ld_list(instance);
4986 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4987 for (j = 0;
4988 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4989 j++) {
4990 ld_index =
4991 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4992
4993 sdev1 = scsi_device_lookup(host,
4994 i+MEGASAS_MAX_LD_CHANNELS,
4995 j, 0);
4996
4997 if (instance->ld_ids[ld_index] !=
4998 0xff) {
4999 if (!sdev1) {
5000 scsi_add_device(host,
5001 i + 2,
5002 j, 0);
5003 }
5004 }
5005 if (sdev1) {
5006 scsi_device_put(sdev1);
5007 }
5008 }
5009 }
5010 doscan = 0;
5011 break;
7e8a75f4 5012 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
c9786842 5013 case MR_EVT_FOREIGN_CFG_IMPORTED:
7e8a75f4
YB
5014 doscan = 1;
5015 break;
5016 default:
5017 doscan = 0;
5018 break;
5019 }
5020 } else {
5021 printk(KERN_ERR "invalid evt_detail!\n");
5022 kfree(ev);
5023 return;
5024 }
5025
5026 if (doscan) {
5027 printk(KERN_INFO "scanning ...\n");
5028 megasas_get_pd_list(instance);
5029 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5030 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5031 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5032 sdev1 = scsi_device_lookup(host, i, j, 0);
5033 if (instance->pd_list[pd_index].driveState ==
5034 MR_PD_STATE_SYSTEM) {
5035 if (!sdev1) {
5036 scsi_add_device(host, i, j, 0);
5037 }
5038 if (sdev1)
5039 scsi_device_put(sdev1);
5040 } else {
5041 if (sdev1) {
5042 scsi_remove_device(sdev1);
5043 scsi_device_put(sdev1);
5044 }
5045 }
5046 }
5047 }
c9786842
YB
5048
5049 megasas_get_ld_list(instance);
5050 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5051 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5052 ld_index =
5053 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5054
5055 sdev1 = scsi_device_lookup(host,
5056 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5057 if (instance->ld_ids[ld_index] != 0xff) {
5058 if (!sdev1) {
5059 scsi_add_device(host,
5060 i+2,
5061 j, 0);
5062 } else {
5063 scsi_device_put(sdev1);
5064 }
5065 } else {
5066 if (sdev1) {
5067 scsi_remove_device(sdev1);
5068 scsi_device_put(sdev1);
5069 }
5070 }
5071 }
5072 }
7e8a75f4
YB
5073 }
5074
5075 if ( instance->aen_cmd != NULL ) {
5076 kfree(ev);
5077 return ;
5078 }
5079
5080 seq_num = instance->evt_detail->seq_num + 1;
5081
5082 /* Register AEN with FW for latest sequence number plus 1 */
5083 class_locale.members.reserved = 0;
5084 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5085 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5086 mutex_lock(&instance->aen_mutex);
5087 error = megasas_register_aen(instance, seq_num,
5088 class_locale.word);
5089 mutex_unlock(&instance->aen_mutex);
5090
5091 if (error)
5092 printk(KERN_ERR "register aen failed error %x\n", error);
5093
5094 kfree(ev);
5095}
5096
5097
bb7d3f24 5098static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
ad84db2e 5099 megasas_sysfs_show_poll_mode_io,
5100 megasas_sysfs_set_poll_mode_io);
658dcedb 5101
c4a3e0a5
BS
5102/**
5103 * megasas_init - Driver load entry point
5104 */
5105static int __init megasas_init(void)
5106{
5107 int rval;
5108
5109 /*
5110 * Announce driver version and other information
5111 */
5112 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5113 MEGASAS_EXT_VERSION);
5114
72c4fd36 5115 support_poll_for_event = 2;
837f5fe8 5116 support_device_change = 1;
72c4fd36 5117
c4a3e0a5
BS
5118 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5119
5120 /*
5121 * Register character device node
5122 */
5123 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5124
5125 if (rval < 0) {
5126 printk(KERN_DEBUG "megasas: failed to open device node\n");
5127 return rval;
5128 }
5129
5130 megasas_mgmt_majorno = rval;
5131
5132 /*
5133 * Register ourselves as PCI hotplug module
5134 */
4041b9cd 5135 rval = pci_register_driver(&megasas_pci_driver);
c4a3e0a5
BS
5136
5137 if (rval) {
5138 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
83aabc1b
JG
5139 goto err_pcidrv;
5140 }
5141
5142 rval = driver_create_file(&megasas_pci_driver.driver,
5143 &driver_attr_version);
5144 if (rval)
5145 goto err_dcf_attr_ver;
5146 rval = driver_create_file(&megasas_pci_driver.driver,
5147 &driver_attr_release_date);
5148 if (rval)
5149 goto err_dcf_rel_date;
72c4fd36
YB
5150
5151 rval = driver_create_file(&megasas_pci_driver.driver,
5152 &driver_attr_support_poll_for_event);
5153 if (rval)
5154 goto err_dcf_support_poll_for_event;
5155
83aabc1b
JG
5156 rval = driver_create_file(&megasas_pci_driver.driver,
5157 &driver_attr_dbg_lvl);
5158 if (rval)
5159 goto err_dcf_dbg_lvl;
ad84db2e 5160 rval = driver_create_file(&megasas_pci_driver.driver,
5161 &driver_attr_poll_mode_io);
5162 if (rval)
5163 goto err_dcf_poll_mode_io;
c4a3e0a5 5164
837f5fe8
YB
5165 rval = driver_create_file(&megasas_pci_driver.driver,
5166 &driver_attr_support_device_change);
5167 if (rval)
5168 goto err_dcf_support_device_change;
5169
c4a3e0a5 5170 return rval;
ad84db2e 5171
837f5fe8
YB
5172err_dcf_support_device_change:
5173 driver_remove_file(&megasas_pci_driver.driver,
5174 &driver_attr_poll_mode_io);
5175
ad84db2e 5176err_dcf_poll_mode_io:
5177 driver_remove_file(&megasas_pci_driver.driver,
5178 &driver_attr_dbg_lvl);
83aabc1b 5179err_dcf_dbg_lvl:
72c4fd36
YB
5180 driver_remove_file(&megasas_pci_driver.driver,
5181 &driver_attr_support_poll_for_event);
5182
5183err_dcf_support_poll_for_event:
83aabc1b
JG
5184 driver_remove_file(&megasas_pci_driver.driver,
5185 &driver_attr_release_date);
72c4fd36 5186
83aabc1b
JG
5187err_dcf_rel_date:
5188 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5189err_dcf_attr_ver:
5190 pci_unregister_driver(&megasas_pci_driver);
5191err_pcidrv:
5192 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
0d49016b 5193 return rval;
c4a3e0a5
BS
5194}
5195
5196/**
5197 * megasas_exit - Driver unload entry point
5198 */
5199static void __exit megasas_exit(void)
5200{
ad84db2e 5201 driver_remove_file(&megasas_pci_driver.driver,
5202 &driver_attr_poll_mode_io);
658dcedb
SP
5203 driver_remove_file(&megasas_pci_driver.driver,
5204 &driver_attr_dbg_lvl);
837f5fe8
YB
5205 driver_remove_file(&megasas_pci_driver.driver,
5206 &driver_attr_support_poll_for_event);
5207 driver_remove_file(&megasas_pci_driver.driver,
5208 &driver_attr_support_device_change);
83aabc1b
JG
5209 driver_remove_file(&megasas_pci_driver.driver,
5210 &driver_attr_release_date);
5211 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
c4a3e0a5
BS
5212
5213 pci_unregister_driver(&megasas_pci_driver);
5214 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5215}
5216
5217module_init(megasas_init);
5218module_exit(megasas_exit);