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