]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/scsi/megaraid/megaraid_sas.c
[SCSI] megaraid_sas: report system PDs to OS
[thirdparty/linux.git] / drivers / scsi / megaraid / megaraid_sas.c
CommitLineData
c4a3e0a5
BS
1/*
2 *
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
f28cd7cf 5 * Copyright (c) 2003-2005 LSI Corporation.
c4a3e0a5
BS
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * FILE : megaraid_sas.c
6610a6b3 13 * Version : v00.00.04.01-rc1
c4a3e0a5
BS
14 *
15 * Authors:
cc5968c8
SP
16 * (email-id : megaraidlinux@lsi.com)
17 * Sreenivas Bagalkote
18 * Sumant Patro
19 * Bo Yang
c4a3e0a5
BS
20 *
21 * List of supported controllers
22 *
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
25 */
26
27#include <linux/kernel.h>
28#include <linux/types.h>
29#include <linux/pci.h>
30#include <linux/list.h>
c4a3e0a5
BS
31#include <linux/moduleparam.h>
32#include <linux/module.h>
33#include <linux/spinlock.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
f2b9857e 36#include <linux/smp_lock.h>
c4a3e0a5
BS
37#include <linux/uio.h>
38#include <asm/uaccess.h>
43399236 39#include <linux/fs.h>
c4a3e0a5 40#include <linux/compat.h>
cf62a0a5 41#include <linux/blkdev.h>
0b950672 42#include <linux/mutex.h>
c3518837 43#include <linux/poll.h>
c4a3e0a5
BS
44
45#include <scsi/scsi.h>
46#include <scsi/scsi_cmnd.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h>
49#include "megaraid_sas.h"
50
ad84db2e 51/*
52 * poll_mode_io:1- schedule complete completion from q cmd
53 */
54static unsigned int poll_mode_io;
55module_param_named(poll_mode_io, poll_mode_io, int, 0);
56MODULE_PARM_DESC(poll_mode_io,
57 "Complete cmds from IO path, (default=0)");
58
c4a3e0a5
BS
59MODULE_LICENSE("GPL");
60MODULE_VERSION(MEGASAS_VERSION);
3d6d174a 61MODULE_AUTHOR("megaraidlinux@lsi.com");
f28cd7cf 62MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
c4a3e0a5
BS
63
64/*
65 * PCI ID table for all supported controllers
66 */
67static struct pci_device_id megasas_pci_table[] = {
68
f3d7271c
HK
69 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
70 /* xscale IOP */
71 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
72 /* ppc IOP */
af7a5647 73 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
74 /* ppc IOP */
6610a6b3
YB
75 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
76 /* gen2*/
77 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
78 /* gen2*/
87911122
YB
79 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
80 /* skinny*/
81 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
82 /* skinny*/
f3d7271c
HK
83 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
84 /* xscale IOP, vega */
85 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
86 /* xscale IOP */
87 {}
c4a3e0a5
BS
88};
89
90MODULE_DEVICE_TABLE(pci, megasas_pci_table);
91
92static int megasas_mgmt_majorno;
93static struct megasas_mgmt_info megasas_mgmt_info;
94static struct fasync_struct *megasas_async_queue;
0b950672 95static DEFINE_MUTEX(megasas_async_queue_mutex);
c4a3e0a5 96
c3518837
YB
97static int megasas_poll_wait_aen;
98static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
72c4fd36 99static u32 support_poll_for_event;
658dcedb
SP
100static u32 megasas_dbg_lvl;
101
c3518837
YB
102/* define lock for aen poll */
103spinlock_t poll_aen_lock;
104
7343eb65 105static void
106megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
107 u8 alt_status);
108
c4a3e0a5
BS
109/**
110 * megasas_get_cmd - Get a command from the free pool
111 * @instance: Adapter soft state
112 *
113 * Returns a free command from the pool
114 */
858119e1 115static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
c4a3e0a5
BS
116 *instance)
117{
118 unsigned long flags;
119 struct megasas_cmd *cmd = NULL;
120
121 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
122
123 if (!list_empty(&instance->cmd_pool)) {
124 cmd = list_entry((&instance->cmd_pool)->next,
125 struct megasas_cmd, list);
126 list_del_init(&cmd->list);
127 } else {
128 printk(KERN_ERR "megasas: Command pool empty!\n");
129 }
130
131 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
132 return cmd;
133}
134
135/**
136 * megasas_return_cmd - Return a cmd to free command pool
137 * @instance: Adapter soft state
138 * @cmd: Command packet to be returned to free command pool
139 */
140static inline void
141megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
146
147 cmd->scmd = NULL;
148 list_add_tail(&cmd->list, &instance->cmd_pool);
149
150 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
151}
152
1341c939
SP
153
154/**
155* The following functions are defined for xscale
156* (deviceid : 1064R, PERC5) controllers
157*/
158
c4a3e0a5 159/**
1341c939 160 * megasas_enable_intr_xscale - Enables interrupts
c4a3e0a5
BS
161 * @regs: MFI register set
162 */
163static inline void
1341c939 164megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
c4a3e0a5
BS
165{
166 writel(1, &(regs)->outbound_intr_mask);
167
168 /* Dummy readl to force pci flush */
169 readl(&regs->outbound_intr_mask);
170}
171
b274cab7
SP
172/**
173 * megasas_disable_intr_xscale -Disables interrupt
174 * @regs: MFI register set
175 */
176static inline void
177megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
178{
179 u32 mask = 0x1f;
180 writel(mask, &regs->outbound_intr_mask);
181 /* Dummy readl to force pci flush */
182 readl(&regs->outbound_intr_mask);
183}
184
1341c939
SP
185/**
186 * megasas_read_fw_status_reg_xscale - returns the current FW status value
187 * @regs: MFI register set
188 */
189static u32
190megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
191{
192 return readl(&(regs)->outbound_msg_0);
193}
194/**
195 * megasas_clear_interrupt_xscale - Check & clear interrupt
196 * @regs: MFI register set
197 */
198static int
199megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
200{
201 u32 status;
202 /*
203 * Check if it is our interrupt
204 */
205 status = readl(&regs->outbound_intr_status);
206
207 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
208 return 1;
209 }
210
211 /*
212 * Clear the interrupt by writing back the same value
213 */
214 writel(status, &regs->outbound_intr_status);
215
06f579de
YB
216 /* Dummy readl to force pci flush */
217 readl(&regs->outbound_intr_status);
218
1341c939
SP
219 return 0;
220}
221
222/**
223 * megasas_fire_cmd_xscale - Sends command to the FW
224 * @frame_phys_addr : Physical address of cmd
225 * @frame_count : Number of frames for the command
226 * @regs : MFI register set
227 */
228static inline void
229megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
230{
231 writel((frame_phys_addr >> 3)|(frame_count),
232 &(regs)->inbound_queue_port);
233}
234
235static struct megasas_instance_template megasas_instance_template_xscale = {
236
237 .fire_cmd = megasas_fire_cmd_xscale,
238 .enable_intr = megasas_enable_intr_xscale,
b274cab7 239 .disable_intr = megasas_disable_intr_xscale,
1341c939
SP
240 .clear_intr = megasas_clear_intr_xscale,
241 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
242};
243
244/**
245* This is the end of set of functions & definitions specific
246* to xscale (deviceid : 1064R, PERC5) controllers
247*/
248
f9876f0b
SP
249/**
250* The following functions are defined for ppc (deviceid : 0x60)
251* controllers
252*/
253
254/**
255 * megasas_enable_intr_ppc - Enables interrupts
256 * @regs: MFI register set
257 */
258static inline void
259megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
260{
261 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
262
263 writel(~0x80000004, &(regs)->outbound_intr_mask);
264
265 /* Dummy readl to force pci flush */
266 readl(&regs->outbound_intr_mask);
267}
268
b274cab7
SP
269/**
270 * megasas_disable_intr_ppc - Disable interrupt
271 * @regs: MFI register set
272 */
273static inline void
274megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
275{
276 u32 mask = 0xFFFFFFFF;
277 writel(mask, &regs->outbound_intr_mask);
278 /* Dummy readl to force pci flush */
279 readl(&regs->outbound_intr_mask);
280}
281
f9876f0b
SP
282/**
283 * megasas_read_fw_status_reg_ppc - returns the current FW status value
284 * @regs: MFI register set
285 */
286static u32
287megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
288{
289 return readl(&(regs)->outbound_scratch_pad);
290}
291
292/**
293 * megasas_clear_interrupt_ppc - Check & clear interrupt
294 * @regs: MFI register set
295 */
296static int
297megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
298{
299 u32 status;
300 /*
301 * Check if it is our interrupt
302 */
303 status = readl(&regs->outbound_intr_status);
304
305 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
306 return 1;
307 }
308
309 /*
310 * Clear the interrupt by writing back the same value
311 */
312 writel(status, &regs->outbound_doorbell_clear);
313
06f579de
YB
314 /* Dummy readl to force pci flush */
315 readl(&regs->outbound_doorbell_clear);
316
f9876f0b
SP
317 return 0;
318}
319/**
320 * megasas_fire_cmd_ppc - Sends command to the FW
321 * @frame_phys_addr : Physical address of cmd
322 * @frame_count : Number of frames for the command
323 * @regs : MFI register set
324 */
325static inline void
326megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
327{
328 writel((frame_phys_addr | (frame_count<<1))|1,
329 &(regs)->inbound_queue_port);
330}
331
332static struct megasas_instance_template megasas_instance_template_ppc = {
333
334 .fire_cmd = megasas_fire_cmd_ppc,
335 .enable_intr = megasas_enable_intr_ppc,
b274cab7 336 .disable_intr = megasas_disable_intr_ppc,
f9876f0b
SP
337 .clear_intr = megasas_clear_intr_ppc,
338 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
339};
340
87911122
YB
341/**
342 * megasas_enable_intr_skinny - Enables interrupts
343 * @regs: MFI register set
344 */
345static inline void
346megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
347{
348 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
349
350 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
351
352 /* Dummy readl to force pci flush */
353 readl(&regs->outbound_intr_mask);
354}
355
356/**
357 * megasas_disable_intr_skinny - Disables interrupt
358 * @regs: MFI register set
359 */
360static inline void
361megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
362{
363 u32 mask = 0xFFFFFFFF;
364 writel(mask, &regs->outbound_intr_mask);
365 /* Dummy readl to force pci flush */
366 readl(&regs->outbound_intr_mask);
367}
368
369/**
370 * megasas_read_fw_status_reg_skinny - returns the current FW status value
371 * @regs: MFI register set
372 */
373static u32
374megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
375{
376 return readl(&(regs)->outbound_scratch_pad);
377}
378
379/**
380 * megasas_clear_interrupt_skinny - Check & clear interrupt
381 * @regs: MFI register set
382 */
383static int
384megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
385{
386 u32 status;
387 /*
388 * Check if it is our interrupt
389 */
390 status = readl(&regs->outbound_intr_status);
391
392 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
393 return 1;
394 }
395
396 /*
397 * Clear the interrupt by writing back the same value
398 */
399 writel(status, &regs->outbound_intr_status);
400
401 /*
402 * dummy read to flush PCI
403 */
404 readl(&regs->outbound_intr_status);
405
406 return 0;
407}
408
409/**
410 * megasas_fire_cmd_skinny - Sends command to the FW
411 * @frame_phys_addr : Physical address of cmd
412 * @frame_count : Number of frames for the command
413 * @regs : MFI register set
414 */
415static inline void
416megasas_fire_cmd_skinny(dma_addr_t frame_phys_addr, u32 frame_count,
417 struct megasas_register_set __iomem *regs)
418{
419 writel(0, &(regs)->inbound_high_queue_port);
420 writel((frame_phys_addr | (frame_count<<1))|1,
421 &(regs)->inbound_low_queue_port);
422}
423
424static struct megasas_instance_template megasas_instance_template_skinny = {
425
426 .fire_cmd = megasas_fire_cmd_skinny,
427 .enable_intr = megasas_enable_intr_skinny,
428 .disable_intr = megasas_disable_intr_skinny,
429 .clear_intr = megasas_clear_intr_skinny,
430 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
431};
432
433
6610a6b3
YB
434/**
435* The following functions are defined for gen2 (deviceid : 0x78 0x79)
436* controllers
437*/
438
439/**
440 * megasas_enable_intr_gen2 - Enables interrupts
441 * @regs: MFI register set
442 */
443static inline void
444megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
445{
446 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
447
448 /* write ~0x00000005 (4 & 1) to the intr mask*/
449 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
450
451 /* Dummy readl to force pci flush */
452 readl(&regs->outbound_intr_mask);
453}
454
455/**
456 * megasas_disable_intr_gen2 - Disables interrupt
457 * @regs: MFI register set
458 */
459static inline void
460megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
461{
462 u32 mask = 0xFFFFFFFF;
463 writel(mask, &regs->outbound_intr_mask);
464 /* Dummy readl to force pci flush */
465 readl(&regs->outbound_intr_mask);
466}
467
468/**
469 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
470 * @regs: MFI register set
471 */
472static u32
473megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
474{
475 return readl(&(regs)->outbound_scratch_pad);
476}
477
478/**
479 * megasas_clear_interrupt_gen2 - Check & clear interrupt
480 * @regs: MFI register set
481 */
482static int
483megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
484{
485 u32 status;
486 /*
487 * Check if it is our interrupt
488 */
489 status = readl(&regs->outbound_intr_status);
490
491 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
492 return 1;
493
494 /*
495 * Clear the interrupt by writing back the same value
496 */
497 writel(status, &regs->outbound_doorbell_clear);
498
499 /* Dummy readl to force pci flush */
500 readl(&regs->outbound_intr_status);
501
502 return 0;
503}
504/**
505 * megasas_fire_cmd_gen2 - Sends command to the FW
506 * @frame_phys_addr : Physical address of cmd
507 * @frame_count : Number of frames for the command
508 * @regs : MFI register set
509 */
510static inline void
511megasas_fire_cmd_gen2(dma_addr_t frame_phys_addr, u32 frame_count,
512 struct megasas_register_set __iomem *regs)
513{
514 writel((frame_phys_addr | (frame_count<<1))|1,
515 &(regs)->inbound_queue_port);
516}
517
518static struct megasas_instance_template megasas_instance_template_gen2 = {
519
520 .fire_cmd = megasas_fire_cmd_gen2,
521 .enable_intr = megasas_enable_intr_gen2,
522 .disable_intr = megasas_disable_intr_gen2,
523 .clear_intr = megasas_clear_intr_gen2,
524 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
525};
526
f9876f0b
SP
527/**
528* This is the end of set of functions & definitions
529* specific to ppc (deviceid : 0x60) controllers
530*/
531
c4a3e0a5
BS
532/**
533 * megasas_issue_polled - Issues a polling command
534 * @instance: Adapter soft state
535 * @cmd: Command packet to be issued
536 *
537 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
538 */
539static int
540megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
541{
542 int i;
543 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
544
545 struct megasas_header *frame_hdr = &cmd->frame->hdr;
546
547 frame_hdr->cmd_status = 0xFF;
548 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
549
550 /*
551 * Issue the frame using inbound queue port
552 */
1341c939 553 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
c4a3e0a5
BS
554
555 /*
556 * Wait for cmd_status to change
557 */
558 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
559 rmb();
560 msleep(1);
561 }
562
563 if (frame_hdr->cmd_status == 0xff)
564 return -ETIME;
565
566 return 0;
567}
568
569/**
570 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
571 * @instance: Adapter soft state
572 * @cmd: Command to be issued
573 *
574 * This function waits on an event for the command to be returned from ISR.
2a3681e5 575 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
c4a3e0a5
BS
576 * Used to issue ioctl commands.
577 */
578static int
579megasas_issue_blocked_cmd(struct megasas_instance *instance,
580 struct megasas_cmd *cmd)
581{
582 cmd->cmd_status = ENODATA;
583
1341c939 584 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
c4a3e0a5 585
2a3681e5
SP
586 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
587 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
c4a3e0a5
BS
588
589 return 0;
590}
591
592/**
593 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
594 * @instance: Adapter soft state
595 * @cmd_to_abort: Previously issued cmd to be aborted
596 *
597 * MFI firmware can abort previously issued AEN comamnd (automatic event
598 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
2a3681e5
SP
599 * cmd and waits for return status.
600 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
c4a3e0a5
BS
601 */
602static int
603megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
604 struct megasas_cmd *cmd_to_abort)
605{
606 struct megasas_cmd *cmd;
607 struct megasas_abort_frame *abort_fr;
608
609 cmd = megasas_get_cmd(instance);
610
611 if (!cmd)
612 return -1;
613
614 abort_fr = &cmd->frame->abort;
615
616 /*
617 * Prepare and issue the abort frame
618 */
619 abort_fr->cmd = MFI_CMD_ABORT;
620 abort_fr->cmd_status = 0xFF;
621 abort_fr->flags = 0;
622 abort_fr->abort_context = cmd_to_abort->index;
623 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
624 abort_fr->abort_mfi_phys_addr_hi = 0;
625
626 cmd->sync_cmd = 1;
627 cmd->cmd_status = 0xFF;
628
1341c939 629 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
c4a3e0a5
BS
630
631 /*
632 * Wait for this cmd to complete
633 */
2a3681e5
SP
634 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
635 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
c4a3e0a5
BS
636
637 megasas_return_cmd(instance, cmd);
638 return 0;
639}
640
641/**
642 * megasas_make_sgl32 - Prepares 32-bit SGL
643 * @instance: Adapter soft state
644 * @scp: SCSI command from the mid-layer
645 * @mfi_sgl: SGL to be filled in
646 *
647 * If successful, this function returns the number of SG elements. Otherwise,
648 * it returnes -1.
649 */
858119e1 650static int
c4a3e0a5
BS
651megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
652 union megasas_sgl *mfi_sgl)
653{
654 int i;
655 int sge_count;
656 struct scatterlist *os_sgl;
657
155d98f0
FT
658 sge_count = scsi_dma_map(scp);
659 BUG_ON(sge_count < 0);
c4a3e0a5 660
155d98f0
FT
661 if (sge_count) {
662 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
663 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
664 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
665 }
c4a3e0a5 666 }
c4a3e0a5
BS
667 return sge_count;
668}
669
670/**
671 * megasas_make_sgl64 - Prepares 64-bit SGL
672 * @instance: Adapter soft state
673 * @scp: SCSI command from the mid-layer
674 * @mfi_sgl: SGL to be filled in
675 *
676 * If successful, this function returns the number of SG elements. Otherwise,
677 * it returnes -1.
678 */
858119e1 679static int
c4a3e0a5
BS
680megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
681 union megasas_sgl *mfi_sgl)
682{
683 int i;
684 int sge_count;
685 struct scatterlist *os_sgl;
686
155d98f0
FT
687 sge_count = scsi_dma_map(scp);
688 BUG_ON(sge_count < 0);
c4a3e0a5 689
155d98f0
FT
690 if (sge_count) {
691 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
692 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
693 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
694 }
c4a3e0a5 695 }
c4a3e0a5
BS
696 return sge_count;
697}
698
b1df99d9
SP
699 /**
700 * megasas_get_frame_count - Computes the number of frames
d532dbe2 701 * @frame_type : type of frame- io or pthru frame
b1df99d9
SP
702 * @sge_count : number of sg elements
703 *
704 * Returns the number of frames required for numnber of sge's (sge_count)
705 */
706
d532dbe2 707static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
b1df99d9
SP
708{
709 int num_cnt;
710 int sge_bytes;
711 u32 sge_sz;
712 u32 frame_count=0;
713
714 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
715 sizeof(struct megasas_sge32);
716
717 /*
d532dbe2 718 * Main frame can contain 2 SGEs for 64-bit SGLs and
719 * 3 SGEs for 32-bit SGLs for ldio &
720 * 1 SGEs for 64-bit SGLs and
721 * 2 SGEs for 32-bit SGLs for pthru frame
722 */
723 if (unlikely(frame_type == PTHRU_FRAME)) {
724 if (IS_DMA64)
725 num_cnt = sge_count - 1;
726 else
727 num_cnt = sge_count - 2;
728 } else {
729 if (IS_DMA64)
730 num_cnt = sge_count - 2;
731 else
732 num_cnt = sge_count - 3;
733 }
b1df99d9
SP
734
735 if(num_cnt>0){
736 sge_bytes = sge_sz * num_cnt;
737
738 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
739 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
740 }
741 /* Main frame */
742 frame_count +=1;
743
744 if (frame_count > 7)
745 frame_count = 8;
746 return frame_count;
747}
748
c4a3e0a5
BS
749/**
750 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
751 * @instance: Adapter soft state
752 * @scp: SCSI command
753 * @cmd: Command to be prepared in
754 *
755 * This function prepares CDB commands. These are typcially pass-through
756 * commands to the devices.
757 */
858119e1 758static int
c4a3e0a5
BS
759megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
760 struct megasas_cmd *cmd)
761{
c4a3e0a5
BS
762 u32 is_logical;
763 u32 device_id;
764 u16 flags = 0;
765 struct megasas_pthru_frame *pthru;
766
767 is_logical = MEGASAS_IS_LOGICAL(scp);
768 device_id = MEGASAS_DEV_INDEX(instance, scp);
769 pthru = (struct megasas_pthru_frame *)cmd->frame;
770
771 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
772 flags = MFI_FRAME_DIR_WRITE;
773 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
774 flags = MFI_FRAME_DIR_READ;
775 else if (scp->sc_data_direction == PCI_DMA_NONE)
776 flags = MFI_FRAME_DIR_NONE;
777
778 /*
779 * Prepare the DCDB frame
780 */
781 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
782 pthru->cmd_status = 0x0;
783 pthru->scsi_status = 0x0;
784 pthru->target_id = device_id;
785 pthru->lun = scp->device->lun;
786 pthru->cdb_len = scp->cmd_len;
787 pthru->timeout = 0;
788 pthru->flags = flags;
155d98f0 789 pthru->data_xfer_len = scsi_bufflen(scp);
c4a3e0a5
BS
790
791 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
792
8d568253
YB
793 /*
794 * If the command is for the tape device, set the
795 * pthru timeout to the os layer timeout value.
796 */
797 if (scp->device->type == TYPE_TAPE) {
798 if ((scp->request->timeout / HZ) > 0xFFFF)
799 pthru->timeout = 0xFFFF;
800 else
801 pthru->timeout = scp->request->timeout / HZ;
802 }
803
c4a3e0a5
BS
804 /*
805 * Construct SGL
806 */
c4a3e0a5
BS
807 if (IS_DMA64) {
808 pthru->flags |= MFI_FRAME_SGL64;
809 pthru->sge_count = megasas_make_sgl64(instance, scp,
810 &pthru->sgl);
811 } else
812 pthru->sge_count = megasas_make_sgl32(instance, scp,
813 &pthru->sgl);
814
815 /*
816 * Sense info specific
817 */
818 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
819 pthru->sense_buf_phys_addr_hi = 0;
820 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
821
c4a3e0a5
BS
822 /*
823 * Compute the total number of frames this command consumes. FW uses
824 * this number to pull sufficient number of frames from host memory.
825 */
d532dbe2 826 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
827 PTHRU_FRAME);
c4a3e0a5
BS
828
829 return cmd->frame_count;
830}
831
832/**
833 * megasas_build_ldio - Prepares IOs to logical devices
834 * @instance: Adapter soft state
835 * @scp: SCSI command
fd589a8f 836 * @cmd: Command to be prepared
c4a3e0a5
BS
837 *
838 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
839 */
858119e1 840static int
c4a3e0a5
BS
841megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
842 struct megasas_cmd *cmd)
843{
c4a3e0a5
BS
844 u32 device_id;
845 u8 sc = scp->cmnd[0];
846 u16 flags = 0;
847 struct megasas_io_frame *ldio;
848
849 device_id = MEGASAS_DEV_INDEX(instance, scp);
850 ldio = (struct megasas_io_frame *)cmd->frame;
851
852 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
853 flags = MFI_FRAME_DIR_WRITE;
854 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
855 flags = MFI_FRAME_DIR_READ;
856
857 /*
b1df99d9 858 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
c4a3e0a5
BS
859 */
860 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
861 ldio->cmd_status = 0x0;
862 ldio->scsi_status = 0x0;
863 ldio->target_id = device_id;
864 ldio->timeout = 0;
865 ldio->reserved_0 = 0;
866 ldio->pad_0 = 0;
867 ldio->flags = flags;
868 ldio->start_lba_hi = 0;
869 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
870
871 /*
872 * 6-byte READ(0x08) or WRITE(0x0A) cdb
873 */
874 if (scp->cmd_len == 6) {
875 ldio->lba_count = (u32) scp->cmnd[4];
876 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
877 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
878
879 ldio->start_lba_lo &= 0x1FFFFF;
880 }
881
882 /*
883 * 10-byte READ(0x28) or WRITE(0x2A) cdb
884 */
885 else if (scp->cmd_len == 10) {
886 ldio->lba_count = (u32) scp->cmnd[8] |
887 ((u32) scp->cmnd[7] << 8);
888 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
889 ((u32) scp->cmnd[3] << 16) |
890 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
891 }
892
893 /*
894 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
895 */
896 else if (scp->cmd_len == 12) {
897 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
898 ((u32) scp->cmnd[7] << 16) |
899 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
900
901 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
902 ((u32) scp->cmnd[3] << 16) |
903 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
904 }
905
906 /*
907 * 16-byte READ(0x88) or WRITE(0x8A) cdb
908 */
909 else if (scp->cmd_len == 16) {
910 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
911 ((u32) scp->cmnd[11] << 16) |
912 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
913
914 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
915 ((u32) scp->cmnd[7] << 16) |
916 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
917
918 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
919 ((u32) scp->cmnd[3] << 16) |
920 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
921
922 }
923
924 /*
925 * Construct SGL
926 */
c4a3e0a5
BS
927 if (IS_DMA64) {
928 ldio->flags |= MFI_FRAME_SGL64;
929 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
930 } else
931 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
932
933 /*
934 * Sense info specific
935 */
936 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
937 ldio->sense_buf_phys_addr_hi = 0;
938 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
939
b1df99d9
SP
940 /*
941 * Compute the total number of frames this command consumes. FW uses
942 * this number to pull sufficient number of frames from host memory.
943 */
d532dbe2 944 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
c4a3e0a5
BS
945
946 return cmd->frame_count;
947}
948
949/**
cb59aa6a
SP
950 * megasas_is_ldio - Checks if the cmd is for logical drive
951 * @scmd: SCSI command
952 *
953 * Called by megasas_queue_command to find out if the command to be queued
954 * is a logical drive command
c4a3e0a5 955 */
cb59aa6a 956static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
c4a3e0a5 957{
cb59aa6a
SP
958 if (!MEGASAS_IS_LOGICAL(cmd))
959 return 0;
960 switch (cmd->cmnd[0]) {
961 case READ_10:
962 case WRITE_10:
963 case READ_12:
964 case WRITE_12:
965 case READ_6:
966 case WRITE_6:
967 case READ_16:
968 case WRITE_16:
969 return 1;
970 default:
971 return 0;
c4a3e0a5 972 }
c4a3e0a5
BS
973}
974
658dcedb
SP
975 /**
976 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
977 * in FW
978 * @instance: Adapter soft state
979 */
980static inline void
981megasas_dump_pending_frames(struct megasas_instance *instance)
982{
983 struct megasas_cmd *cmd;
984 int i,n;
985 union megasas_sgl *mfi_sgl;
986 struct megasas_io_frame *ldio;
987 struct megasas_pthru_frame *pthru;
988 u32 sgcount;
989 u32 max_cmd = instance->max_fw_cmds;
990
991 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
992 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
993 if (IS_DMA64)
994 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
995 else
996 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
997
998 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
999 for (i = 0; i < max_cmd; i++) {
1000 cmd = instance->cmd_list[i];
1001 if(!cmd->scmd)
1002 continue;
1003 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1004 if (megasas_is_ldio(cmd->scmd)){
1005 ldio = (struct megasas_io_frame *)cmd->frame;
1006 mfi_sgl = &ldio->sgl;
1007 sgcount = ldio->sge_count;
1008 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);
1009 }
1010 else {
1011 pthru = (struct megasas_pthru_frame *) cmd->frame;
1012 mfi_sgl = &pthru->sgl;
1013 sgcount = pthru->sge_count;
1014 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);
1015 }
1016 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1017 for (n = 0; n < sgcount; n++){
1018 if (IS_DMA64)
1019 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) ;
1020 else
1021 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1022 }
1023 }
1024 printk(KERN_ERR "\n");
1025 } /*for max_cmd*/
1026 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1027 for (i = 0; i < max_cmd; i++) {
1028
1029 cmd = instance->cmd_list[i];
1030
1031 if(cmd->sync_cmd == 1){
1032 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1033 }
1034 }
1035 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1036}
1037
c4a3e0a5
BS
1038/**
1039 * megasas_queue_command - Queue entry point
1040 * @scmd: SCSI command to be queued
1041 * @done: Callback entry point
1042 */
1043static int
1044megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1045{
1046 u32 frame_count;
c4a3e0a5
BS
1047 struct megasas_cmd *cmd;
1048 struct megasas_instance *instance;
1049
1050 instance = (struct megasas_instance *)
1051 scmd->device->host->hostdata;
af37acfb
SP
1052
1053 /* Don't process if we have already declared adapter dead */
1054 if (instance->hw_crit_error)
1055 return SCSI_MLQUEUE_HOST_BUSY;
1056
c4a3e0a5
BS
1057 scmd->scsi_done = done;
1058 scmd->result = 0;
1059
cb59aa6a
SP
1060 if (MEGASAS_IS_LOGICAL(scmd) &&
1061 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1062 scmd->result = DID_BAD_TARGET << 16;
1063 goto out_done;
c4a3e0a5
BS
1064 }
1065
02b01e01
SP
1066 switch (scmd->cmnd[0]) {
1067 case SYNCHRONIZE_CACHE:
1068 /*
1069 * FW takes care of flush cache on its own
1070 * No need to send it down
1071 */
1072 scmd->result = DID_OK << 16;
1073 goto out_done;
1074 default:
1075 break;
1076 }
1077
cb59aa6a
SP
1078 cmd = megasas_get_cmd(instance);
1079 if (!cmd)
1080 return SCSI_MLQUEUE_HOST_BUSY;
1081
1082 /*
1083 * Logical drive command
1084 */
1085 if (megasas_is_ldio(scmd))
1086 frame_count = megasas_build_ldio(instance, scmd, cmd);
1087 else
1088 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1089
1090 if (!frame_count)
1091 goto out_return_cmd;
1092
c4a3e0a5 1093 cmd->scmd = scmd;
05e9ebbe 1094 scmd->SCp.ptr = (char *)cmd;
c4a3e0a5
BS
1095
1096 /*
1097 * Issue the command to the FW
1098 */
e4a082c7 1099 atomic_inc(&instance->fw_outstanding);
c4a3e0a5 1100
1341c939 1101 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
ad84db2e 1102 /*
1103 * Check if we have pend cmds to be completed
1104 */
1105 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1106 tasklet_schedule(&instance->isr_tasklet);
1107
c4a3e0a5
BS
1108
1109 return 0;
cb59aa6a
SP
1110
1111 out_return_cmd:
1112 megasas_return_cmd(instance, cmd);
1113 out_done:
1114 done(scmd);
1115 return 0;
c4a3e0a5
BS
1116}
1117
044833b5
YB
1118static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1119{
1120 int i;
1121
1122 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1123
1124 if ((megasas_mgmt_info.instance[i]) &&
1125 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1126 return megasas_mgmt_info.instance[i];
1127 }
1128
1129 return NULL;
1130}
1131
147aab6a
CH
1132static int megasas_slave_configure(struct scsi_device *sdev)
1133{
044833b5
YB
1134 u16 pd_index = 0;
1135 struct megasas_instance *instance ;
1136
1137 instance = megasas_lookup_instance(sdev->host->host_no);
1138
147aab6a 1139 /*
044833b5
YB
1140 * Don't export physical disk devices to the disk driver.
1141 *
1142 * FIXME: Currently we don't export them to the midlayer at all.
1143 * That will be fixed once LSI engineers have audited the
1144 * firmware for possible issues.
1145 */
1146 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1147 sdev->type == TYPE_DISK) {
1148 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1149 sdev->id;
1150 if (instance->pd_list[pd_index].driveState ==
1151 MR_PD_STATE_SYSTEM) {
1152 blk_queue_rq_timeout(sdev->request_queue,
1153 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1154 return 0;
1155 }
147aab6a 1156 return -ENXIO;
044833b5 1157 }
e5b3a65f
CH
1158
1159 /*
044833b5
YB
1160 * The RAID firmware may require extended timeouts.
1161 */
1162 blk_queue_rq_timeout(sdev->request_queue,
1163 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1164 return 0;
1165}
1166
1167static int megasas_slave_alloc(struct scsi_device *sdev)
1168{
1169 u16 pd_index = 0;
1170 struct megasas_instance *instance ;
1171 instance = megasas_lookup_instance(sdev->host->host_no);
1172 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1173 (sdev->type == TYPE_DISK)) {
1174 /*
1175 * Open the OS scan to the SYSTEM PD
1176 */
1177 pd_index =
1178 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1179 sdev->id;
1180 if ((instance->pd_list[pd_index].driveState ==
1181 MR_PD_STATE_SYSTEM) &&
1182 (instance->pd_list[pd_index].driveType ==
1183 TYPE_DISK)) {
1184 return 0;
1185 }
1186 return -ENXIO;
1187 }
147aab6a
CH
1188 return 0;
1189}
1190
7343eb65 1191/**
1192 * megasas_complete_cmd_dpc - Returns FW's controller structure
1193 * @instance_addr: Address of adapter soft state
1194 *
1195 * Tasklet to complete cmds
1196 */
1197static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1198{
1199 u32 producer;
1200 u32 consumer;
1201 u32 context;
1202 struct megasas_cmd *cmd;
1203 struct megasas_instance *instance =
1204 (struct megasas_instance *)instance_addr;
1205 unsigned long flags;
1206
1207 /* If we have already declared adapter dead, donot complete cmds */
1208 if (instance->hw_crit_error)
1209 return;
1210
1211 spin_lock_irqsave(&instance->completion_lock, flags);
1212
1213 producer = *instance->producer;
1214 consumer = *instance->consumer;
1215
1216 while (consumer != producer) {
1217 context = instance->reply_queue[consumer];
1218
1219 cmd = instance->cmd_list[context];
1220
1221 megasas_complete_cmd(instance, cmd, DID_OK);
1222
1223 consumer++;
1224 if (consumer == (instance->max_fw_cmds + 1)) {
1225 consumer = 0;
1226 }
1227 }
1228
1229 *instance->consumer = producer;
1230
1231 spin_unlock_irqrestore(&instance->completion_lock, flags);
1232
1233 /*
1234 * Check if we can restore can_queue
1235 */
1236 if (instance->flag & MEGASAS_FW_BUSY
1237 && time_after(jiffies, instance->last_time + 5 * HZ)
1238 && atomic_read(&instance->fw_outstanding) < 17) {
1239
1240 spin_lock_irqsave(instance->host->host_lock, flags);
1241 instance->flag &= ~MEGASAS_FW_BUSY;
1242 instance->host->can_queue =
1243 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1244
1245 spin_unlock_irqrestore(instance->host->host_lock, flags);
1246 }
1247}
1248
c4a3e0a5
BS
1249/**
1250 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1251 * @instance: Adapter soft state
1252 *
1253 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1254 * complete all its outstanding commands. Returns error if one or more IOs
1255 * are pending after this time period. It also marks the controller dead.
1256 */
1257static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1258{
1259 int i;
1260 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1261
1262 for (i = 0; i < wait_time; i++) {
1263
e4a082c7
SP
1264 int outstanding = atomic_read(&instance->fw_outstanding);
1265
1266 if (!outstanding)
c4a3e0a5
BS
1267 break;
1268
1269 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1270 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
e4a082c7 1271 "commands to complete\n",i,outstanding);
7343eb65 1272 /*
1273 * Call cmd completion routine. Cmd to be
1274 * be completed directly without depending on isr.
1275 */
1276 megasas_complete_cmd_dpc((unsigned long)instance);
c4a3e0a5
BS
1277 }
1278
1279 msleep(1000);
1280 }
1281
e4a082c7 1282 if (atomic_read(&instance->fw_outstanding)) {
e3bbff9f
SP
1283 /*
1284 * Send signal to FW to stop processing any pending cmds.
1285 * The controller will be taken offline by the OS now.
1286 */
1287 writel(MFI_STOP_ADP,
1288 &instance->reg_set->inbound_doorbell);
658dcedb 1289 megasas_dump_pending_frames(instance);
c4a3e0a5
BS
1290 instance->hw_crit_error = 1;
1291 return FAILED;
1292 }
1293
1294 return SUCCESS;
1295}
1296
1297/**
1298 * megasas_generic_reset - Generic reset routine
1299 * @scmd: Mid-layer SCSI command
1300 *
1301 * This routine implements a generic reset handler for device, bus and host
1302 * reset requests. Device, bus and host specific reset handlers can use this
1303 * function after they do their specific tasks.
1304 */
1305static int megasas_generic_reset(struct scsi_cmnd *scmd)
1306{
1307 int ret_val;
1308 struct megasas_instance *instance;
1309
1310 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1311
05e9ebbe
SP
1312 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1313 scmd->serial_number, scmd->cmnd[0], scmd->retries);
c4a3e0a5
BS
1314
1315 if (instance->hw_crit_error) {
1316 printk(KERN_ERR "megasas: cannot recover from previous reset "
1317 "failures\n");
1318 return FAILED;
1319 }
1320
c4a3e0a5 1321 ret_val = megasas_wait_for_outstanding(instance);
c4a3e0a5
BS
1322 if (ret_val == SUCCESS)
1323 printk(KERN_NOTICE "megasas: reset successful \n");
1324 else
1325 printk(KERN_ERR "megasas: failed to do reset\n");
1326
c4a3e0a5
BS
1327 return ret_val;
1328}
1329
05e9ebbe
SP
1330/**
1331 * megasas_reset_timer - quiesce the adapter if required
1332 * @scmd: scsi cmnd
1333 *
1334 * Sets the FW busy flag and reduces the host->can_queue if the
1335 * cmd has not been completed within the timeout period.
1336 */
1337static enum
242f9dcb 1338blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
05e9ebbe
SP
1339{
1340 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1341 struct megasas_instance *instance;
1342 unsigned long flags;
1343
1344 if (time_after(jiffies, scmd->jiffies_at_alloc +
1345 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
242f9dcb 1346 return BLK_EH_NOT_HANDLED;
05e9ebbe
SP
1347 }
1348
1349 instance = cmd->instance;
1350 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1351 /* FW is busy, throttle IO */
1352 spin_lock_irqsave(instance->host->host_lock, flags);
1353
1354 instance->host->can_queue = 16;
1355 instance->last_time = jiffies;
1356 instance->flag |= MEGASAS_FW_BUSY;
1357
1358 spin_unlock_irqrestore(instance->host->host_lock, flags);
1359 }
242f9dcb 1360 return BLK_EH_RESET_TIMER;
05e9ebbe
SP
1361}
1362
c4a3e0a5
BS
1363/**
1364 * megasas_reset_device - Device reset handler entry point
1365 */
1366static int megasas_reset_device(struct scsi_cmnd *scmd)
1367{
1368 int ret;
1369
1370 /*
1371 * First wait for all commands to complete
1372 */
1373 ret = megasas_generic_reset(scmd);
1374
1375 return ret;
1376}
1377
1378/**
1379 * megasas_reset_bus_host - Bus & host reset handler entry point
1380 */
1381static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1382{
1383 int ret;
1384
1385 /*
80682fa9 1386 * First wait for all commands to complete
c4a3e0a5
BS
1387 */
1388 ret = megasas_generic_reset(scmd);
1389
1390 return ret;
1391}
1392
cf62a0a5
SP
1393/**
1394 * megasas_bios_param - Returns disk geometry for a disk
1395 * @sdev: device handle
1396 * @bdev: block device
1397 * @capacity: drive capacity
1398 * @geom: geometry parameters
1399 */
1400static int
1401megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1402 sector_t capacity, int geom[])
1403{
1404 int heads;
1405 int sectors;
1406 sector_t cylinders;
1407 unsigned long tmp;
1408 /* Default heads (64) & sectors (32) */
1409 heads = 64;
1410 sectors = 32;
1411
1412 tmp = heads * sectors;
1413 cylinders = capacity;
1414
1415 sector_div(cylinders, tmp);
1416
1417 /*
1418 * Handle extended translation size for logical drives > 1Gb
1419 */
1420
1421 if (capacity >= 0x200000) {
1422 heads = 255;
1423 sectors = 63;
1424 tmp = heads*sectors;
1425 cylinders = capacity;
1426 sector_div(cylinders, tmp);
1427 }
1428
1429 geom[0] = heads;
1430 geom[1] = sectors;
1431 geom[2] = cylinders;
1432
1433 return 0;
1434}
1435
c4a3e0a5
BS
1436/**
1437 * megasas_service_aen - Processes an event notification
1438 * @instance: Adapter soft state
1439 * @cmd: AEN command completed by the ISR
1440 *
1441 * For AEN, driver sends a command down to FW that is held by the FW till an
1442 * event occurs. When an event of interest occurs, FW completes the command
1443 * that it was previously holding.
1444 *
1445 * This routines sends SIGIO signal to processes that have registered with the
1446 * driver for AEN.
1447 */
1448static void
1449megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1450{
c3518837 1451 unsigned long flags;
c4a3e0a5
BS
1452 /*
1453 * Don't signal app if it is just an aborted previously registered aen
1454 */
c3518837
YB
1455 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1456 spin_lock_irqsave(&poll_aen_lock, flags);
1457 megasas_poll_wait_aen = 1;
1458 spin_unlock_irqrestore(&poll_aen_lock, flags);
1459 wake_up(&megasas_poll_wait);
c4a3e0a5 1460 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
c3518837 1461 }
c4a3e0a5
BS
1462 else
1463 cmd->abort_aen = 0;
1464
1465 instance->aen_cmd = NULL;
1466 megasas_return_cmd(instance, cmd);
1467}
1468
1469/*
1470 * Scsi host template for megaraid_sas driver
1471 */
1472static struct scsi_host_template megasas_template = {
1473
1474 .module = THIS_MODULE,
f28cd7cf 1475 .name = "LSI SAS based MegaRAID driver",
c4a3e0a5 1476 .proc_name = "megaraid_sas",
147aab6a 1477 .slave_configure = megasas_slave_configure,
044833b5 1478 .slave_alloc = megasas_slave_alloc,
c4a3e0a5
BS
1479 .queuecommand = megasas_queue_command,
1480 .eh_device_reset_handler = megasas_reset_device,
1481 .eh_bus_reset_handler = megasas_reset_bus_host,
1482 .eh_host_reset_handler = megasas_reset_bus_host,
05e9ebbe 1483 .eh_timed_out = megasas_reset_timer,
cf62a0a5 1484 .bios_param = megasas_bios_param,
c4a3e0a5
BS
1485 .use_clustering = ENABLE_CLUSTERING,
1486};
1487
1488/**
1489 * megasas_complete_int_cmd - Completes an internal command
1490 * @instance: Adapter soft state
1491 * @cmd: Command to be completed
1492 *
1493 * The megasas_issue_blocked_cmd() function waits for a command to complete
1494 * after it issues a command. This function wakes up that waiting routine by
1495 * calling wake_up() on the wait queue.
1496 */
1497static void
1498megasas_complete_int_cmd(struct megasas_instance *instance,
1499 struct megasas_cmd *cmd)
1500{
1501 cmd->cmd_status = cmd->frame->io.cmd_status;
1502
1503 if (cmd->cmd_status == ENODATA) {
1504 cmd->cmd_status = 0;
1505 }
1506 wake_up(&instance->int_cmd_wait_q);
1507}
1508
1509/**
1510 * megasas_complete_abort - Completes aborting a command
1511 * @instance: Adapter soft state
1512 * @cmd: Cmd that was issued to abort another cmd
1513 *
1514 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1515 * after it issues an abort on a previously issued command. This function
1516 * wakes up all functions waiting on the same wait queue.
1517 */
1518static void
1519megasas_complete_abort(struct megasas_instance *instance,
1520 struct megasas_cmd *cmd)
1521{
1522 if (cmd->sync_cmd) {
1523 cmd->sync_cmd = 0;
1524 cmd->cmd_status = 0;
1525 wake_up(&instance->abort_cmd_wait_q);
1526 }
1527
1528 return;
1529}
1530
c4a3e0a5
BS
1531/**
1532 * megasas_complete_cmd - Completes a command
1533 * @instance: Adapter soft state
1534 * @cmd: Command to be completed
1535 * @alt_status: If non-zero, use this value as status to
1536 * SCSI mid-layer instead of the value returned
1537 * by the FW. This should be used if caller wants
1538 * an alternate status (as in the case of aborted
1539 * commands)
1540 */
858119e1 1541static void
c4a3e0a5
BS
1542megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1543 u8 alt_status)
1544{
1545 int exception = 0;
1546 struct megasas_header *hdr = &cmd->frame->hdr;
c3518837 1547 unsigned long flags;
c4a3e0a5 1548
05e9ebbe
SP
1549 if (cmd->scmd)
1550 cmd->scmd->SCp.ptr = NULL;
c4a3e0a5
BS
1551
1552 switch (hdr->cmd) {
1553
1554 case MFI_CMD_PD_SCSI_IO:
1555 case MFI_CMD_LD_SCSI_IO:
1556
1557 /*
1558 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1559 * issued either through an IO path or an IOCTL path. If it
1560 * was via IOCTL, we will send it to internal completion.
1561 */
1562 if (cmd->sync_cmd) {
1563 cmd->sync_cmd = 0;
1564 megasas_complete_int_cmd(instance, cmd);
1565 break;
1566 }
1567
c4a3e0a5
BS
1568 case MFI_CMD_LD_READ:
1569 case MFI_CMD_LD_WRITE:
1570
1571 if (alt_status) {
1572 cmd->scmd->result = alt_status << 16;
1573 exception = 1;
1574 }
1575
1576 if (exception) {
1577
e4a082c7 1578 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 1579
155d98f0 1580 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
1581 cmd->scmd->scsi_done(cmd->scmd);
1582 megasas_return_cmd(instance, cmd);
1583
1584 break;
1585 }
1586
1587 switch (hdr->cmd_status) {
1588
1589 case MFI_STAT_OK:
1590 cmd->scmd->result = DID_OK << 16;
1591 break;
1592
1593 case MFI_STAT_SCSI_IO_FAILED:
1594 case MFI_STAT_LD_INIT_IN_PROGRESS:
1595 cmd->scmd->result =
1596 (DID_ERROR << 16) | hdr->scsi_status;
1597 break;
1598
1599 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1600
1601 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1602
1603 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1604 memset(cmd->scmd->sense_buffer, 0,
1605 SCSI_SENSE_BUFFERSIZE);
1606 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1607 hdr->sense_len);
1608
1609 cmd->scmd->result |= DRIVER_SENSE << 24;
1610 }
1611
1612 break;
1613
1614 case MFI_STAT_LD_OFFLINE:
1615 case MFI_STAT_DEVICE_NOT_FOUND:
1616 cmd->scmd->result = DID_BAD_TARGET << 16;
1617 break;
1618
1619 default:
1620 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1621 hdr->cmd_status);
1622 cmd->scmd->result = DID_ERROR << 16;
1623 break;
1624 }
1625
e4a082c7 1626 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 1627
155d98f0 1628 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
1629 cmd->scmd->scsi_done(cmd->scmd);
1630 megasas_return_cmd(instance, cmd);
1631
1632 break;
1633
1634 case MFI_CMD_SMP:
1635 case MFI_CMD_STP:
1636 case MFI_CMD_DCMD:
c3518837
YB
1637 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1638 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1639 spin_lock_irqsave(&poll_aen_lock, flags);
1640 megasas_poll_wait_aen = 0;
1641 spin_unlock_irqrestore(&poll_aen_lock, flags);
1642 }
c4a3e0a5
BS
1643
1644 /*
1645 * See if got an event notification
1646 */
1647 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1648 megasas_service_aen(instance, cmd);
1649 else
1650 megasas_complete_int_cmd(instance, cmd);
1651
1652 break;
1653
1654 case MFI_CMD_ABORT:
1655 /*
1656 * Cmd issued to abort another cmd returned
1657 */
1658 megasas_complete_abort(instance, cmd);
1659 break;
1660
1661 default:
1662 printk("megasas: Unknown command completed! [0x%X]\n",
1663 hdr->cmd);
1664 break;
1665 }
1666}
1667
1668/**
1669 * megasas_deplete_reply_queue - Processes all completed commands
1670 * @instance: Adapter soft state
1671 * @alt_status: Alternate status to be returned to
1672 * SCSI mid-layer instead of the status
1673 * returned by the FW
1674 */
858119e1 1675static int
c4a3e0a5
BS
1676megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1677{
c4a3e0a5
BS
1678 /*
1679 * Check if it is our interrupt
1341c939 1680 * Clear the interrupt
c4a3e0a5 1681 */
1341c939 1682 if(instance->instancet->clear_intr(instance->reg_set))
c4a3e0a5 1683 return IRQ_NONE;
c4a3e0a5 1684
af37acfb
SP
1685 if (instance->hw_crit_error)
1686 goto out_done;
5d018ad0
SP
1687 /*
1688 * Schedule the tasklet for cmd completion
1689 */
1690 tasklet_schedule(&instance->isr_tasklet);
af37acfb 1691out_done:
c4a3e0a5
BS
1692 return IRQ_HANDLED;
1693}
1694
1695/**
1696 * megasas_isr - isr entry point
1697 */
7d12e780 1698static irqreturn_t megasas_isr(int irq, void *devp)
c4a3e0a5
BS
1699{
1700 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1701 DID_OK);
1702}
1703
1704/**
1705 * megasas_transition_to_ready - Move the FW to READY state
1341c939 1706 * @instance: Adapter soft state
c4a3e0a5
BS
1707 *
1708 * During the initialization, FW passes can potentially be in any one of
1709 * several possible states. If the FW in operational, waiting-for-handshake
1710 * states, driver must take steps to bring it to ready state. Otherwise, it
1711 * has to wait for the ready state.
1712 */
1713static int
1341c939 1714megasas_transition_to_ready(struct megasas_instance* instance)
c4a3e0a5
BS
1715{
1716 int i;
1717 u8 max_wait;
1718 u32 fw_state;
1719 u32 cur_state;
1720
1341c939 1721 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
c4a3e0a5 1722
e3bbff9f
SP
1723 if (fw_state != MFI_STATE_READY)
1724 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1725 " state\n");
1726
c4a3e0a5
BS
1727 while (fw_state != MFI_STATE_READY) {
1728
c4a3e0a5
BS
1729 switch (fw_state) {
1730
1731 case MFI_STATE_FAULT:
1732
1733 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1734 return -ENODEV;
1735
1736 case MFI_STATE_WAIT_HANDSHAKE:
1737 /*
1738 * Set the CLR bit in inbound doorbell
1739 */
87911122
YB
1740 if ((instance->pdev->device == \
1741 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1742 (instance->pdev->device ==
1743 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1744
1745 writel(
1746 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1747 &instance->reg_set->reserved_0[0]);
1748 } else {
1749 writel(
1750 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1751 &instance->reg_set->inbound_doorbell);
1752 }
c4a3e0a5
BS
1753
1754 max_wait = 2;
1755 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1756 break;
1757
e3bbff9f 1758 case MFI_STATE_BOOT_MESSAGE_PENDING:
87911122
YB
1759 if ((instance->pdev->device ==
1760 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1761 (instance->pdev->device ==
1762 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1763 writel(MFI_INIT_HOTPLUG,
1764 &instance->reg_set->reserved_0[0]);
1765 } else
1766 writel(MFI_INIT_HOTPLUG,
1767 &instance->reg_set->inbound_doorbell);
e3bbff9f
SP
1768
1769 max_wait = 10;
1770 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1771 break;
1772
c4a3e0a5
BS
1773 case MFI_STATE_OPERATIONAL:
1774 /*
e3bbff9f 1775 * Bring it to READY state; assuming max wait 10 secs
c4a3e0a5 1776 */
b274cab7 1777 instance->instancet->disable_intr(instance->reg_set);
87911122
YB
1778 if ((instance->pdev->device ==
1779 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1780 (instance->pdev->device ==
1781 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1782 writel(MFI_RESET_FLAGS,
1783 &instance->reg_set->reserved_0[0]);
1784 } else
1785 writel(MFI_RESET_FLAGS,
1786 &instance->reg_set->inbound_doorbell);
c4a3e0a5 1787
af7a5647 1788 max_wait = 60;
c4a3e0a5
BS
1789 cur_state = MFI_STATE_OPERATIONAL;
1790 break;
1791
1792 case MFI_STATE_UNDEFINED:
1793 /*
1794 * This state should not last for more than 2 seconds
1795 */
1796 max_wait = 2;
1797 cur_state = MFI_STATE_UNDEFINED;
1798 break;
1799
1800 case MFI_STATE_BB_INIT:
1801 max_wait = 2;
1802 cur_state = MFI_STATE_BB_INIT;
1803 break;
1804
1805 case MFI_STATE_FW_INIT:
1806 max_wait = 20;
1807 cur_state = MFI_STATE_FW_INIT;
1808 break;
1809
1810 case MFI_STATE_FW_INIT_2:
1811 max_wait = 20;
1812 cur_state = MFI_STATE_FW_INIT_2;
1813 break;
1814
1815 case MFI_STATE_DEVICE_SCAN:
1816 max_wait = 20;
1817 cur_state = MFI_STATE_DEVICE_SCAN;
1818 break;
1819
1820 case MFI_STATE_FLUSH_CACHE:
1821 max_wait = 20;
1822 cur_state = MFI_STATE_FLUSH_CACHE;
1823 break;
1824
1825 default:
1826 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1827 fw_state);
1828 return -ENODEV;
1829 }
1830
1831 /*
1832 * The cur_state should not last for more than max_wait secs
1833 */
1834 for (i = 0; i < (max_wait * 1000); i++) {
1341c939
SP
1835 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1836 MFI_STATE_MASK ;
c4a3e0a5
BS
1837
1838 if (fw_state == cur_state) {
1839 msleep(1);
1840 } else
1841 break;
1842 }
1843
1844 /*
1845 * Return error if fw_state hasn't changed after max_wait
1846 */
1847 if (fw_state == cur_state) {
1848 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1849 "in %d secs\n", fw_state, max_wait);
1850 return -ENODEV;
1851 }
1852 };
e3bbff9f 1853 printk(KERN_INFO "megasas: FW now in Ready state\n");
c4a3e0a5
BS
1854
1855 return 0;
1856}
1857
1858/**
1859 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1860 * @instance: Adapter soft state
1861 */
1862static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1863{
1864 int i;
1865 u32 max_cmd = instance->max_fw_cmds;
1866 struct megasas_cmd *cmd;
1867
1868 if (!instance->frame_dma_pool)
1869 return;
1870
1871 /*
1872 * Return all frames to pool
1873 */
1874 for (i = 0; i < max_cmd; i++) {
1875
1876 cmd = instance->cmd_list[i];
1877
1878 if (cmd->frame)
1879 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1880 cmd->frame_phys_addr);
1881
1882 if (cmd->sense)
e3bbff9f 1883 pci_pool_free(instance->sense_dma_pool, cmd->sense,
c4a3e0a5
BS
1884 cmd->sense_phys_addr);
1885 }
1886
1887 /*
1888 * Now destroy the pool itself
1889 */
1890 pci_pool_destroy(instance->frame_dma_pool);
1891 pci_pool_destroy(instance->sense_dma_pool);
1892
1893 instance->frame_dma_pool = NULL;
1894 instance->sense_dma_pool = NULL;
1895}
1896
1897/**
1898 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1899 * @instance: Adapter soft state
1900 *
1901 * Each command packet has an embedded DMA memory buffer that is used for
1902 * filling MFI frame and the SG list that immediately follows the frame. This
1903 * function creates those DMA memory buffers for each command packet by using
1904 * PCI pool facility.
1905 */
1906static int megasas_create_frame_pool(struct megasas_instance *instance)
1907{
1908 int i;
1909 u32 max_cmd;
1910 u32 sge_sz;
1911 u32 sgl_sz;
1912 u32 total_sz;
1913 u32 frame_count;
1914 struct megasas_cmd *cmd;
1915
1916 max_cmd = instance->max_fw_cmds;
1917
1918 /*
1919 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1920 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1921 */
1922 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1923 sizeof(struct megasas_sge32);
1924
1925 /*
1926 * Calculated the number of 64byte frames required for SGL
1927 */
1928 sgl_sz = sge_sz * instance->max_num_sge;
1929 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1930
1931 /*
1932 * We need one extra frame for the MFI command
1933 */
1934 frame_count++;
1935
1936 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1937 /*
1938 * Use DMA pool facility provided by PCI layer
1939 */
1940 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1941 instance->pdev, total_sz, 64,
1942 0);
1943
1944 if (!instance->frame_dma_pool) {
1945 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1946 return -ENOMEM;
1947 }
1948
1949 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1950 instance->pdev, 128, 4, 0);
1951
1952 if (!instance->sense_dma_pool) {
1953 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1954
1955 pci_pool_destroy(instance->frame_dma_pool);
1956 instance->frame_dma_pool = NULL;
1957
1958 return -ENOMEM;
1959 }
1960
1961 /*
1962 * Allocate and attach a frame to each of the commands in cmd_list.
1963 * By making cmd->index as the context instead of the &cmd, we can
1964 * always use 32bit context regardless of the architecture
1965 */
1966 for (i = 0; i < max_cmd; i++) {
1967
1968 cmd = instance->cmd_list[i];
1969
1970 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1971 GFP_KERNEL, &cmd->frame_phys_addr);
1972
1973 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1974 GFP_KERNEL, &cmd->sense_phys_addr);
1975
1976 /*
1977 * megasas_teardown_frame_pool() takes care of freeing
1978 * whatever has been allocated
1979 */
1980 if (!cmd->frame || !cmd->sense) {
1981 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1982 megasas_teardown_frame_pool(instance);
1983 return -ENOMEM;
1984 }
1985
1986 cmd->frame->io.context = cmd->index;
1987 }
1988
1989 return 0;
1990}
1991
1992/**
1993 * megasas_free_cmds - Free all the cmds in the free cmd pool
1994 * @instance: Adapter soft state
1995 */
1996static void megasas_free_cmds(struct megasas_instance *instance)
1997{
1998 int i;
1999 /* First free the MFI frame pool */
2000 megasas_teardown_frame_pool(instance);
2001
2002 /* Free all the commands in the cmd_list */
2003 for (i = 0; i < instance->max_fw_cmds; i++)
2004 kfree(instance->cmd_list[i]);
2005
2006 /* Free the cmd_list buffer itself */
2007 kfree(instance->cmd_list);
2008 instance->cmd_list = NULL;
2009
2010 INIT_LIST_HEAD(&instance->cmd_pool);
2011}
2012
2013/**
2014 * megasas_alloc_cmds - Allocates the command packets
2015 * @instance: Adapter soft state
2016 *
2017 * Each command that is issued to the FW, whether IO commands from the OS or
2018 * internal commands like IOCTLs, are wrapped in local data structure called
2019 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2020 * the FW.
2021 *
2022 * Each frame has a 32-bit field called context (tag). This context is used
2023 * to get back the megasas_cmd from the frame when a frame gets completed in
2024 * the ISR. Typically the address of the megasas_cmd itself would be used as
2025 * the context. But we wanted to keep the differences between 32 and 64 bit
2026 * systems to the mininum. We always use 32 bit integers for the context. In
2027 * this driver, the 32 bit values are the indices into an array cmd_list.
2028 * This array is used only to look up the megasas_cmd given the context. The
2029 * free commands themselves are maintained in a linked list called cmd_pool.
2030 */
2031static int megasas_alloc_cmds(struct megasas_instance *instance)
2032{
2033 int i;
2034 int j;
2035 u32 max_cmd;
2036 struct megasas_cmd *cmd;
2037
2038 max_cmd = instance->max_fw_cmds;
2039
2040 /*
2041 * instance->cmd_list is an array of struct megasas_cmd pointers.
2042 * Allocate the dynamic array first and then allocate individual
2043 * commands.
2044 */
dd00cc48 2045 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
c4a3e0a5
BS
2046
2047 if (!instance->cmd_list) {
2048 printk(KERN_DEBUG "megasas: out of memory\n");
2049 return -ENOMEM;
2050 }
2051
c4a3e0a5
BS
2052
2053 for (i = 0; i < max_cmd; i++) {
2054 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2055 GFP_KERNEL);
2056
2057 if (!instance->cmd_list[i]) {
2058
2059 for (j = 0; j < i; j++)
2060 kfree(instance->cmd_list[j]);
2061
2062 kfree(instance->cmd_list);
2063 instance->cmd_list = NULL;
2064
2065 return -ENOMEM;
2066 }
2067 }
2068
2069 /*
2070 * Add all the commands to command pool (instance->cmd_pool)
2071 */
2072 for (i = 0; i < max_cmd; i++) {
2073 cmd = instance->cmd_list[i];
2074 memset(cmd, 0, sizeof(struct megasas_cmd));
2075 cmd->index = i;
2076 cmd->instance = instance;
2077
2078 list_add_tail(&cmd->list, &instance->cmd_pool);
2079 }
2080
2081 /*
2082 * Create a frame pool and assign one frame to each cmd
2083 */
2084 if (megasas_create_frame_pool(instance)) {
2085 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2086 megasas_free_cmds(instance);
2087 }
2088
2089 return 0;
2090}
2091
81e403ce
YB
2092/*
2093 * megasas_get_pd_list_info - Returns FW's pd_list structure
2094 * @instance: Adapter soft state
2095 * @pd_list: pd_list structure
2096 *
2097 * Issues an internal command (DCMD) to get the FW's controller PD
2098 * list structure. This information is mainly used to find out SYSTEM
2099 * supported by the FW.
2100 */
2101static int
2102megasas_get_pd_list(struct megasas_instance *instance)
2103{
2104 int ret = 0, pd_index = 0;
2105 struct megasas_cmd *cmd;
2106 struct megasas_dcmd_frame *dcmd;
2107 struct MR_PD_LIST *ci;
2108 struct MR_PD_ADDRESS *pd_addr;
2109 dma_addr_t ci_h = 0;
2110
2111 cmd = megasas_get_cmd(instance);
2112
2113 if (!cmd) {
2114 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2115 return -ENOMEM;
2116 }
2117
2118 dcmd = &cmd->frame->dcmd;
2119
2120 ci = pci_alloc_consistent(instance->pdev,
2121 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2122
2123 if (!ci) {
2124 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2125 megasas_return_cmd(instance, cmd);
2126 return -ENOMEM;
2127 }
2128
2129 memset(ci, 0, sizeof(*ci));
2130 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2131
2132 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2133 dcmd->mbox.b[1] = 0;
2134 dcmd->cmd = MFI_CMD_DCMD;
2135 dcmd->cmd_status = 0xFF;
2136 dcmd->sge_count = 1;
2137 dcmd->flags = MFI_FRAME_DIR_READ;
2138 dcmd->timeout = 0;
2139 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2140 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2141 dcmd->sgl.sge32[0].phys_addr = ci_h;
2142 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2143
2144 if (!megasas_issue_polled(instance, cmd)) {
2145 ret = 0;
2146 } else {
2147 ret = -1;
2148 }
2149
2150 /*
2151 * the following function will get the instance PD LIST.
2152 */
2153
2154 pd_addr = ci->addr;
2155
2156 if ( ret == 0 &&
2157 (ci->count <
2158 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2159
2160 memset(instance->pd_list, 0,
2161 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2162
2163 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2164
2165 instance->pd_list[pd_addr->deviceId].tid =
2166 pd_addr->deviceId;
2167 instance->pd_list[pd_addr->deviceId].driveType =
2168 pd_addr->scsiDevType;
2169 instance->pd_list[pd_addr->deviceId].driveState =
2170 MR_PD_STATE_SYSTEM;
2171 pd_addr++;
2172 }
2173 }
2174
2175 pci_free_consistent(instance->pdev,
2176 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2177 ci, ci_h);
2178 megasas_return_cmd(instance, cmd);
2179
2180 return ret;
2181}
2182
2183
c4a3e0a5
BS
2184/**
2185 * megasas_get_controller_info - Returns FW's controller structure
2186 * @instance: Adapter soft state
2187 * @ctrl_info: Controller information structure
2188 *
2189 * Issues an internal command (DCMD) to get the FW's controller structure.
2190 * This information is mainly used to find out the maximum IO transfer per
2191 * command supported by the FW.
2192 */
2193static int
2194megasas_get_ctrl_info(struct megasas_instance *instance,
2195 struct megasas_ctrl_info *ctrl_info)
2196{
2197 int ret = 0;
2198 struct megasas_cmd *cmd;
2199 struct megasas_dcmd_frame *dcmd;
2200 struct megasas_ctrl_info *ci;
2201 dma_addr_t ci_h = 0;
2202
2203 cmd = megasas_get_cmd(instance);
2204
2205 if (!cmd) {
2206 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2207 return -ENOMEM;
2208 }
2209
2210 dcmd = &cmd->frame->dcmd;
2211
2212 ci = pci_alloc_consistent(instance->pdev,
2213 sizeof(struct megasas_ctrl_info), &ci_h);
2214
2215 if (!ci) {
2216 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2217 megasas_return_cmd(instance, cmd);
2218 return -ENOMEM;
2219 }
2220
2221 memset(ci, 0, sizeof(*ci));
2222 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2223
2224 dcmd->cmd = MFI_CMD_DCMD;
2225 dcmd->cmd_status = 0xFF;
2226 dcmd->sge_count = 1;
2227 dcmd->flags = MFI_FRAME_DIR_READ;
2228 dcmd->timeout = 0;
2229 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2230 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2231 dcmd->sgl.sge32[0].phys_addr = ci_h;
2232 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2233
2234 if (!megasas_issue_polled(instance, cmd)) {
2235 ret = 0;
2236 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2237 } else {
2238 ret = -1;
2239 }
2240
2241 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2242 ci, ci_h);
2243
2244 megasas_return_cmd(instance, cmd);
2245 return ret;
2246}
2247
31ea7088 2248/**
2249 * megasas_issue_init_mfi - Initializes the FW
2250 * @instance: Adapter soft state
2251 *
2252 * Issues the INIT MFI cmd
2253 */
2254static int
2255megasas_issue_init_mfi(struct megasas_instance *instance)
2256{
2257 u32 context;
2258
2259 struct megasas_cmd *cmd;
2260
2261 struct megasas_init_frame *init_frame;
2262 struct megasas_init_queue_info *initq_info;
2263 dma_addr_t init_frame_h;
2264 dma_addr_t initq_info_h;
2265
2266 /*
2267 * Prepare a init frame. Note the init frame points to queue info
2268 * structure. Each frame has SGL allocated after first 64 bytes. For
2269 * this frame - since we don't need any SGL - we use SGL's space as
2270 * queue info structure
2271 *
2272 * We will not get a NULL command below. We just created the pool.
2273 */
2274 cmd = megasas_get_cmd(instance);
2275
2276 init_frame = (struct megasas_init_frame *)cmd->frame;
2277 initq_info = (struct megasas_init_queue_info *)
2278 ((unsigned long)init_frame + 64);
2279
2280 init_frame_h = cmd->frame_phys_addr;
2281 initq_info_h = init_frame_h + 64;
2282
2283 context = init_frame->context;
2284 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2285 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2286 init_frame->context = context;
2287
2288 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2289 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2290
2291 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2292 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2293
2294 init_frame->cmd = MFI_CMD_INIT;
2295 init_frame->cmd_status = 0xFF;
2296 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2297
2298 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2299
2300 /*
2301 * disable the intr before firing the init frame to FW
2302 */
2303 instance->instancet->disable_intr(instance->reg_set);
2304
2305 /*
2306 * Issue the init frame in polled mode
2307 */
2308
2309 if (megasas_issue_polled(instance, cmd)) {
2310 printk(KERN_ERR "megasas: Failed to init firmware\n");
2311 megasas_return_cmd(instance, cmd);
2312 goto fail_fw_init;
2313 }
2314
2315 megasas_return_cmd(instance, cmd);
2316
2317 return 0;
2318
2319fail_fw_init:
2320 return -EINVAL;
2321}
2322
ad84db2e 2323/**
2324 * megasas_start_timer - Initializes a timer object
2325 * @instance: Adapter soft state
2326 * @timer: timer object to be initialized
2327 * @fn: timer function
2328 * @interval: time interval between timer function call
2329 */
2330static inline void
2331megasas_start_timer(struct megasas_instance *instance,
2332 struct timer_list *timer,
2333 void *fn, unsigned long interval)
2334{
2335 init_timer(timer);
2336 timer->expires = jiffies + interval;
2337 timer->data = (unsigned long)instance;
2338 timer->function = fn;
2339 add_timer(timer);
2340}
2341
2342/**
2343 * megasas_io_completion_timer - Timer fn
2344 * @instance_addr: Address of adapter soft state
2345 *
2346 * Schedules tasklet for cmd completion
2347 * if poll_mode_io is set
2348 */
2349static void
2350megasas_io_completion_timer(unsigned long instance_addr)
2351{
2352 struct megasas_instance *instance =
2353 (struct megasas_instance *)instance_addr;
2354
2355 if (atomic_read(&instance->fw_outstanding))
2356 tasklet_schedule(&instance->isr_tasklet);
2357
2358 /* Restart timer */
2359 if (poll_mode_io)
2360 mod_timer(&instance->io_completion_timer,
2361 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2362}
2363
c4a3e0a5
BS
2364/**
2365 * megasas_init_mfi - Initializes the FW
2366 * @instance: Adapter soft state
2367 *
2368 * This is the main function for initializing MFI firmware.
2369 */
2370static int megasas_init_mfi(struct megasas_instance *instance)
2371{
2372 u32 context_sz;
2373 u32 reply_q_sz;
2374 u32 max_sectors_1;
2375 u32 max_sectors_2;
14faea9f 2376 u32 tmp_sectors;
c4a3e0a5 2377 struct megasas_register_set __iomem *reg_set;
c4a3e0a5 2378 struct megasas_ctrl_info *ctrl_info;
c4a3e0a5
BS
2379 /*
2380 * Map the message registers
2381 */
6610a6b3 2382 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
87911122
YB
2383 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2384 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6610a6b3
YB
2385 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2386 instance->base_addr = pci_resource_start(instance->pdev, 1);
2387 } else {
2388 instance->base_addr = pci_resource_start(instance->pdev, 0);
2389 }
c4a3e0a5 2390
f28cd7cf 2391 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
c4a3e0a5
BS
2392 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2393 return -EBUSY;
2394 }
2395
2396 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2397
2398 if (!instance->reg_set) {
2399 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2400 goto fail_ioremap;
2401 }
2402
2403 reg_set = instance->reg_set;
2404
f9876f0b
SP
2405 switch(instance->pdev->device)
2406 {
af7a5647 2407 case PCI_DEVICE_ID_LSI_SAS1078R:
2408 case PCI_DEVICE_ID_LSI_SAS1078DE:
f9876f0b
SP
2409 instance->instancet = &megasas_instance_template_ppc;
2410 break;
6610a6b3
YB
2411 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2412 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2413 instance->instancet = &megasas_instance_template_gen2;
2414 break;
87911122
YB
2415 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2416 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2417 instance->instancet = &megasas_instance_template_skinny;
2418 break;
f9876f0b
SP
2419 case PCI_DEVICE_ID_LSI_SAS1064R:
2420 case PCI_DEVICE_ID_DELL_PERC5:
2421 default:
2422 instance->instancet = &megasas_instance_template_xscale;
2423 break;
2424 }
1341c939 2425
c4a3e0a5
BS
2426 /*
2427 * We expect the FW state to be READY
2428 */
1341c939 2429 if (megasas_transition_to_ready(instance))
c4a3e0a5
BS
2430 goto fail_ready_state;
2431
2432 /*
2433 * Get various operational parameters from status register
2434 */
1341c939 2435 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
e3bbff9f
SP
2436 /*
2437 * Reduce the max supported cmds by 1. This is to ensure that the
2438 * reply_q_sz (1 more than the max cmd that driver may send)
2439 * does not exceed max cmds that the FW can support
2440 */
2441 instance->max_fw_cmds = instance->max_fw_cmds-1;
1341c939
SP
2442 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2443 0x10;
c4a3e0a5
BS
2444 /*
2445 * Create a pool of commands
2446 */
2447 if (megasas_alloc_cmds(instance))
2448 goto fail_alloc_cmds;
2449
2450 /*
2451 * Allocate memory for reply queue. Length of reply queue should
2452 * be _one_ more than the maximum commands handled by the firmware.
2453 *
2454 * Note: When FW completes commands, it places corresponding contex
2455 * values in this circular reply queue. This circular queue is a fairly
2456 * typical producer-consumer queue. FW is the producer (of completed
2457 * commands) and the driver is the consumer.
2458 */
2459 context_sz = sizeof(u32);
2460 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2461
2462 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2463 reply_q_sz,
2464 &instance->reply_queue_h);
2465
2466 if (!instance->reply_queue) {
2467 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2468 goto fail_reply_queue;
2469 }
2470
31ea7088 2471 if (megasas_issue_init_mfi(instance))
c4a3e0a5 2472 goto fail_fw_init;
c4a3e0a5 2473
81e403ce
YB
2474 memset(instance->pd_list, 0 ,
2475 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
2476 megasas_get_pd_list(instance);
2477
c4a3e0a5
BS
2478 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2479
2480 /*
2481 * Compute the max allowed sectors per IO: The controller info has two
2482 * limits on max sectors. Driver should use the minimum of these two.
2483 *
2484 * 1 << stripe_sz_ops.min = max sectors per strip
2485 *
2486 * Note that older firmwares ( < FW ver 30) didn't report information
2487 * to calculate max_sectors_1. So the number ended up as zero always.
2488 */
14faea9f 2489 tmp_sectors = 0;
c4a3e0a5
BS
2490 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2491
2492 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2493 ctrl_info->max_strips_per_io;
2494 max_sectors_2 = ctrl_info->max_request_size;
2495
14faea9f 2496 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2497 }
2498
2499 instance->max_sectors_per_req = instance->max_num_sge *
2500 PAGE_SIZE / 512;
2501 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2502 instance->max_sectors_per_req = tmp_sectors;
c4a3e0a5
BS
2503
2504 kfree(ctrl_info);
2505
5d018ad0
SP
2506 /*
2507 * Setup tasklet for cmd completion
2508 */
2509
ad84db2e 2510 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2511 (unsigned long)instance);
2512
2513 /* Initialize the cmd completion timer */
2514 if (poll_mode_io)
2515 megasas_start_timer(instance, &instance->io_completion_timer,
2516 megasas_io_completion_timer,
2517 MEGASAS_COMPLETION_TIMER_INTERVAL);
c4a3e0a5
BS
2518 return 0;
2519
2520 fail_fw_init:
c4a3e0a5
BS
2521
2522 pci_free_consistent(instance->pdev, reply_q_sz,
2523 instance->reply_queue, instance->reply_queue_h);
2524 fail_reply_queue:
2525 megasas_free_cmds(instance);
2526
2527 fail_alloc_cmds:
2528 fail_ready_state:
2529 iounmap(instance->reg_set);
2530
2531 fail_ioremap:
2532 pci_release_regions(instance->pdev);
2533
2534 return -EINVAL;
2535}
2536
2537/**
2538 * megasas_release_mfi - Reverses the FW initialization
2539 * @intance: Adapter soft state
2540 */
2541static void megasas_release_mfi(struct megasas_instance *instance)
2542{
2543 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2544
2545 pci_free_consistent(instance->pdev, reply_q_sz,
2546 instance->reply_queue, instance->reply_queue_h);
2547
2548 megasas_free_cmds(instance);
2549
2550 iounmap(instance->reg_set);
2551
2552 pci_release_regions(instance->pdev);
2553}
2554
2555/**
2556 * megasas_get_seq_num - Gets latest event sequence numbers
2557 * @instance: Adapter soft state
2558 * @eli: FW event log sequence numbers information
2559 *
2560 * FW maintains a log of all events in a non-volatile area. Upper layers would
2561 * usually find out the latest sequence number of the events, the seq number at
2562 * the boot etc. They would "read" all the events below the latest seq number
2563 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2564 * number), they would subsribe to AEN (asynchronous event notification) and
2565 * wait for the events to happen.
2566 */
2567static int
2568megasas_get_seq_num(struct megasas_instance *instance,
2569 struct megasas_evt_log_info *eli)
2570{
2571 struct megasas_cmd *cmd;
2572 struct megasas_dcmd_frame *dcmd;
2573 struct megasas_evt_log_info *el_info;
2574 dma_addr_t el_info_h = 0;
2575
2576 cmd = megasas_get_cmd(instance);
2577
2578 if (!cmd) {
2579 return -ENOMEM;
2580 }
2581
2582 dcmd = &cmd->frame->dcmd;
2583 el_info = pci_alloc_consistent(instance->pdev,
2584 sizeof(struct megasas_evt_log_info),
2585 &el_info_h);
2586
2587 if (!el_info) {
2588 megasas_return_cmd(instance, cmd);
2589 return -ENOMEM;
2590 }
2591
2592 memset(el_info, 0, sizeof(*el_info));
2593 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2594
2595 dcmd->cmd = MFI_CMD_DCMD;
2596 dcmd->cmd_status = 0x0;
2597 dcmd->sge_count = 1;
2598 dcmd->flags = MFI_FRAME_DIR_READ;
2599 dcmd->timeout = 0;
2600 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2601 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2602 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2603 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2604
2605 megasas_issue_blocked_cmd(instance, cmd);
2606
2607 /*
2608 * Copy the data back into callers buffer
2609 */
2610 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2611
2612 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2613 el_info, el_info_h);
2614
2615 megasas_return_cmd(instance, cmd);
2616
2617 return 0;
2618}
2619
2620/**
2621 * megasas_register_aen - Registers for asynchronous event notification
2622 * @instance: Adapter soft state
2623 * @seq_num: The starting sequence number
2624 * @class_locale: Class of the event
2625 *
2626 * This function subscribes for AEN for events beyond the @seq_num. It requests
2627 * to be notified if and only if the event is of type @class_locale
2628 */
2629static int
2630megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2631 u32 class_locale_word)
2632{
2633 int ret_val;
2634 struct megasas_cmd *cmd;
2635 struct megasas_dcmd_frame *dcmd;
2636 union megasas_evt_class_locale curr_aen;
2637 union megasas_evt_class_locale prev_aen;
2638
2639 /*
2640 * If there an AEN pending already (aen_cmd), check if the
2641 * class_locale of that pending AEN is inclusive of the new
2642 * AEN request we currently have. If it is, then we don't have
2643 * to do anything. In other words, whichever events the current
2644 * AEN request is subscribing to, have already been subscribed
2645 * to.
2646 *
2647 * If the old_cmd is _not_ inclusive, then we have to abort
2648 * that command, form a class_locale that is superset of both
2649 * old and current and re-issue to the FW
2650 */
2651
2652 curr_aen.word = class_locale_word;
2653
2654 if (instance->aen_cmd) {
2655
2656 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2657
2658 /*
2659 * A class whose enum value is smaller is inclusive of all
2660 * higher values. If a PROGRESS (= -1) was previously
2661 * registered, then a new registration requests for higher
2662 * classes need not be sent to FW. They are automatically
2663 * included.
2664 *
2665 * Locale numbers don't have such hierarchy. They are bitmap
2666 * values
2667 */
2668 if ((prev_aen.members.class <= curr_aen.members.class) &&
2669 !((prev_aen.members.locale & curr_aen.members.locale) ^
2670 curr_aen.members.locale)) {
2671 /*
2672 * Previously issued event registration includes
2673 * current request. Nothing to do.
2674 */
2675 return 0;
2676 } else {
2677 curr_aen.members.locale |= prev_aen.members.locale;
2678
2679 if (prev_aen.members.class < curr_aen.members.class)
2680 curr_aen.members.class = prev_aen.members.class;
2681
2682 instance->aen_cmd->abort_aen = 1;
2683 ret_val = megasas_issue_blocked_abort_cmd(instance,
2684 instance->
2685 aen_cmd);
2686
2687 if (ret_val) {
2688 printk(KERN_DEBUG "megasas: Failed to abort "
2689 "previous AEN command\n");
2690 return ret_val;
2691 }
2692 }
2693 }
2694
2695 cmd = megasas_get_cmd(instance);
2696
2697 if (!cmd)
2698 return -ENOMEM;
2699
2700 dcmd = &cmd->frame->dcmd;
2701
2702 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2703
2704 /*
2705 * Prepare DCMD for aen registration
2706 */
2707 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2708
2709 dcmd->cmd = MFI_CMD_DCMD;
2710 dcmd->cmd_status = 0x0;
2711 dcmd->sge_count = 1;
2712 dcmd->flags = MFI_FRAME_DIR_READ;
2713 dcmd->timeout = 0;
2714 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2715 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2716 dcmd->mbox.w[0] = seq_num;
2717 dcmd->mbox.w[1] = curr_aen.word;
2718 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2719 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2720
2721 /*
2722 * Store reference to the cmd used to register for AEN. When an
2723 * application wants us to register for AEN, we have to abort this
2724 * cmd and re-register with a new EVENT LOCALE supplied by that app
2725 */
2726 instance->aen_cmd = cmd;
2727
2728 /*
2729 * Issue the aen registration frame
2730 */
1341c939 2731 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
c4a3e0a5
BS
2732
2733 return 0;
2734}
2735
2736/**
2737 * megasas_start_aen - Subscribes to AEN during driver load time
2738 * @instance: Adapter soft state
2739 */
2740static int megasas_start_aen(struct megasas_instance *instance)
2741{
2742 struct megasas_evt_log_info eli;
2743 union megasas_evt_class_locale class_locale;
2744
2745 /*
2746 * Get the latest sequence number from FW
2747 */
2748 memset(&eli, 0, sizeof(eli));
2749
2750 if (megasas_get_seq_num(instance, &eli))
2751 return -1;
2752
2753 /*
2754 * Register AEN with FW for latest sequence number plus 1
2755 */
2756 class_locale.members.reserved = 0;
2757 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2758 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2759
2760 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2761 class_locale.word);
2762}
2763
2764/**
2765 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2766 * @instance: Adapter soft state
2767 */
2768static int megasas_io_attach(struct megasas_instance *instance)
2769{
2770 struct Scsi_Host *host = instance->host;
2771
2772 /*
2773 * Export parameters required by SCSI mid-layer
2774 */
2775 host->irq = instance->pdev->irq;
2776 host->unique_id = instance->unique_id;
2777 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2778 host->this_id = instance->init_id;
2779 host->sg_tablesize = instance->max_num_sge;
2780 host->max_sectors = instance->max_sectors_per_req;
2781 host->cmd_per_lun = 128;
2782 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2783 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2784 host->max_lun = MEGASAS_MAX_LUN;
122da302 2785 host->max_cmd_len = 16;
c4a3e0a5
BS
2786
2787 /*
2788 * Notify the mid-layer about the new controller
2789 */
2790 if (scsi_add_host(host, &instance->pdev->dev)) {
2791 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2792 return -ENODEV;
2793 }
2794
2795 /*
2796 * Trigger SCSI to scan our drives
2797 */
2798 scsi_scan_host(host);
2799 return 0;
2800}
2801
31ea7088 2802static int
2803megasas_set_dma_mask(struct pci_dev *pdev)
2804{
2805 /*
2806 * All our contollers are capable of performing 64-bit DMA
2807 */
2808 if (IS_DMA64) {
6a35528a 2809 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
31ea7088 2810
284901a9 2811 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 2812 goto fail_set_dma_mask;
2813 }
2814 } else {
284901a9 2815 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 2816 goto fail_set_dma_mask;
2817 }
2818 return 0;
2819
2820fail_set_dma_mask:
2821 return 1;
2822}
2823
c4a3e0a5
BS
2824/**
2825 * megasas_probe_one - PCI hotplug entry point
2826 * @pdev: PCI device structure
2827 * @id: PCI ids of supported hotplugged adapter
2828 */
2829static int __devinit
2830megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2831{
2832 int rval;
2833 struct Scsi_Host *host;
2834 struct megasas_instance *instance;
2835
2836 /*
2837 * Announce PCI information
2838 */
2839 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2840 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2841 pdev->subsystem_device);
2842
2843 printk("bus %d:slot %d:func %d\n",
2844 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2845
2846 /*
2847 * PCI prepping: enable device set bus mastering and dma mask
2848 */
2849 rval = pci_enable_device(pdev);
2850
2851 if (rval) {
2852 return rval;
2853 }
2854
2855 pci_set_master(pdev);
2856
31ea7088 2857 if (megasas_set_dma_mask(pdev))
2858 goto fail_set_dma_mask;
c4a3e0a5
BS
2859
2860 host = scsi_host_alloc(&megasas_template,
2861 sizeof(struct megasas_instance));
2862
2863 if (!host) {
2864 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2865 goto fail_alloc_instance;
2866 }
2867
2868 instance = (struct megasas_instance *)host->hostdata;
2869 memset(instance, 0, sizeof(*instance));
2870
2871 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2872 &instance->producer_h);
2873 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2874 &instance->consumer_h);
2875
2876 if (!instance->producer || !instance->consumer) {
2877 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2878 "producer, consumer\n");
2879 goto fail_alloc_dma_buf;
2880 }
2881
2882 *instance->producer = 0;
2883 *instance->consumer = 0;
c3518837 2884 megasas_poll_wait_aen = 0;
c4a3e0a5
BS
2885
2886 instance->evt_detail = pci_alloc_consistent(pdev,
2887 sizeof(struct
2888 megasas_evt_detail),
2889 &instance->evt_detail_h);
2890
2891 if (!instance->evt_detail) {
2892 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2893 "event detail structure\n");
2894 goto fail_alloc_dma_buf;
2895 }
2896
2897 /*
2898 * Initialize locks and queues
2899 */
2900 INIT_LIST_HEAD(&instance->cmd_pool);
2901
e4a082c7
SP
2902 atomic_set(&instance->fw_outstanding,0);
2903
c4a3e0a5
BS
2904 init_waitqueue_head(&instance->int_cmd_wait_q);
2905 init_waitqueue_head(&instance->abort_cmd_wait_q);
2906
2907 spin_lock_init(&instance->cmd_pool_lock);
7343eb65 2908 spin_lock_init(&instance->completion_lock);
c3518837 2909 spin_lock_init(&poll_aen_lock);
c4a3e0a5 2910
e5a69e27 2911 mutex_init(&instance->aen_mutex);
c4a3e0a5
BS
2912 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2913
2914 /*
2915 * Initialize PCI related and misc parameters
2916 */
2917 instance->pdev = pdev;
2918 instance->host = host;
2919 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2920 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2921
658dcedb 2922 megasas_dbg_lvl = 0;
05e9ebbe 2923 instance->flag = 0;
c3518837 2924 instance->unload = 0;
05e9ebbe 2925 instance->last_time = 0;
658dcedb 2926
c4a3e0a5
BS
2927 /*
2928 * Initialize MFI Firmware
2929 */
2930 if (megasas_init_mfi(instance))
2931 goto fail_init_mfi;
2932
2933 /*
2934 * Register IRQ
2935 */
1d6f359a 2936 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
c4a3e0a5
BS
2937 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2938 goto fail_irq;
2939 }
2940
1341c939 2941 instance->instancet->enable_intr(instance->reg_set);
c4a3e0a5
BS
2942
2943 /*
2944 * Store instance in PCI softstate
2945 */
2946 pci_set_drvdata(pdev, instance);
2947
2948 /*
2949 * Add this controller to megasas_mgmt_info structure so that it
2950 * can be exported to management applications
2951 */
2952 megasas_mgmt_info.count++;
2953 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2954 megasas_mgmt_info.max_index++;
2955
2956 /*
2957 * Initiate AEN (Asynchronous Event Notification)
2958 */
2959 if (megasas_start_aen(instance)) {
2960 printk(KERN_DEBUG "megasas: start aen failed\n");
2961 goto fail_start_aen;
2962 }
2963
2964 /*
2965 * Register with SCSI mid-layer
2966 */
2967 if (megasas_io_attach(instance))
2968 goto fail_io_attach;
2969
2970 return 0;
2971
2972 fail_start_aen:
2973 fail_io_attach:
2974 megasas_mgmt_info.count--;
2975 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2976 megasas_mgmt_info.max_index--;
2977
2978 pci_set_drvdata(pdev, NULL);
b274cab7 2979 instance->instancet->disable_intr(instance->reg_set);
c4a3e0a5
BS
2980 free_irq(instance->pdev->irq, instance);
2981
2982 megasas_release_mfi(instance);
2983
2984 fail_irq:
2985 fail_init_mfi:
2986 fail_alloc_dma_buf:
2987 if (instance->evt_detail)
2988 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2989 instance->evt_detail,
2990 instance->evt_detail_h);
2991
2992 if (instance->producer)
2993 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2994 instance->producer_h);
2995 if (instance->consumer)
2996 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2997 instance->consumer_h);
2998 scsi_host_put(host);
2999
3000 fail_alloc_instance:
3001 fail_set_dma_mask:
3002 pci_disable_device(pdev);
3003
3004 return -ENODEV;
3005}
3006
3007/**
3008 * megasas_flush_cache - Requests FW to flush all its caches
3009 * @instance: Adapter soft state
3010 */
3011static void megasas_flush_cache(struct megasas_instance *instance)
3012{
3013 struct megasas_cmd *cmd;
3014 struct megasas_dcmd_frame *dcmd;
3015
3016 cmd = megasas_get_cmd(instance);
3017
3018 if (!cmd)
3019 return;
3020
3021 dcmd = &cmd->frame->dcmd;
3022
3023 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3024
3025 dcmd->cmd = MFI_CMD_DCMD;
3026 dcmd->cmd_status = 0x0;
3027 dcmd->sge_count = 0;
3028 dcmd->flags = MFI_FRAME_DIR_NONE;
3029 dcmd->timeout = 0;
3030 dcmd->data_xfer_len = 0;
3031 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3032 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3033
3034 megasas_issue_blocked_cmd(instance, cmd);
3035
3036 megasas_return_cmd(instance, cmd);
3037
3038 return;
3039}
3040
3041/**
3042 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3043 * @instance: Adapter soft state
31ea7088 3044 * @opcode: Shutdown/Hibernate
c4a3e0a5 3045 */
31ea7088 3046static void megasas_shutdown_controller(struct megasas_instance *instance,
3047 u32 opcode)
c4a3e0a5
BS
3048{
3049 struct megasas_cmd *cmd;
3050 struct megasas_dcmd_frame *dcmd;
3051
3052 cmd = megasas_get_cmd(instance);
3053
3054 if (!cmd)
3055 return;
3056
3057 if (instance->aen_cmd)
3058 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3059
3060 dcmd = &cmd->frame->dcmd;
3061
3062 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3063
3064 dcmd->cmd = MFI_CMD_DCMD;
3065 dcmd->cmd_status = 0x0;
3066 dcmd->sge_count = 0;
3067 dcmd->flags = MFI_FRAME_DIR_NONE;
3068 dcmd->timeout = 0;
3069 dcmd->data_xfer_len = 0;
31ea7088 3070 dcmd->opcode = opcode;
c4a3e0a5
BS
3071
3072 megasas_issue_blocked_cmd(instance, cmd);
3073
3074 megasas_return_cmd(instance, cmd);
3075
3076 return;
3077}
3078
33139b21 3079#ifdef CONFIG_PM
31ea7088 3080/**
ad84db2e 3081 * megasas_suspend - driver suspend entry point
3082 * @pdev: PCI device structure
31ea7088 3083 * @state: PCI power state to suspend routine
3084 */
33139b21 3085static int
31ea7088 3086megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3087{
3088 struct Scsi_Host *host;
3089 struct megasas_instance *instance;
3090
3091 instance = pci_get_drvdata(pdev);
3092 host = instance->host;
3093
ad84db2e 3094 if (poll_mode_io)
3095 del_timer_sync(&instance->io_completion_timer);
3096
31ea7088 3097 megasas_flush_cache(instance);
3098 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
3099 tasklet_kill(&instance->isr_tasklet);
3100
3101 pci_set_drvdata(instance->pdev, instance);
3102 instance->instancet->disable_intr(instance->reg_set);
3103 free_irq(instance->pdev->irq, instance);
3104
3105 pci_save_state(pdev);
3106 pci_disable_device(pdev);
3107
3108 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3109
3110 return 0;
3111}
3112
3113/**
3114 * megasas_resume- driver resume entry point
3115 * @pdev: PCI device structure
3116 */
33139b21 3117static int
31ea7088 3118megasas_resume(struct pci_dev *pdev)
3119{
3120 int rval;
3121 struct Scsi_Host *host;
3122 struct megasas_instance *instance;
3123
3124 instance = pci_get_drvdata(pdev);
3125 host = instance->host;
3126 pci_set_power_state(pdev, PCI_D0);
3127 pci_enable_wake(pdev, PCI_D0, 0);
3128 pci_restore_state(pdev);
3129
3130 /*
3131 * PCI prepping: enable device set bus mastering and dma mask
3132 */
3133 rval = pci_enable_device(pdev);
3134
3135 if (rval) {
3136 printk(KERN_ERR "megasas: Enable device failed\n");
3137 return rval;
3138 }
3139
3140 pci_set_master(pdev);
3141
3142 if (megasas_set_dma_mask(pdev))
3143 goto fail_set_dma_mask;
3144
3145 /*
3146 * Initialize MFI Firmware
3147 */
3148
3149 *instance->producer = 0;
3150 *instance->consumer = 0;
3151
3152 atomic_set(&instance->fw_outstanding, 0);
3153
3154 /*
3155 * We expect the FW state to be READY
3156 */
3157 if (megasas_transition_to_ready(instance))
3158 goto fail_ready_state;
3159
3160 if (megasas_issue_init_mfi(instance))
3161 goto fail_init_mfi;
3162
3163 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3164 (unsigned long)instance);
3165
3166 /*
3167 * Register IRQ
3168 */
3169 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3170 "megasas", instance)) {
3171 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3172 goto fail_irq;
3173 }
3174
3175 instance->instancet->enable_intr(instance->reg_set);
3176
3177 /*
3178 * Initiate AEN (Asynchronous Event Notification)
3179 */
3180 if (megasas_start_aen(instance))
3181 printk(KERN_ERR "megasas: Start AEN failed\n");
3182
ad84db2e 3183 /* Initialize the cmd completion timer */
3184 if (poll_mode_io)
3185 megasas_start_timer(instance, &instance->io_completion_timer,
3186 megasas_io_completion_timer,
3187 MEGASAS_COMPLETION_TIMER_INTERVAL);
31ea7088 3188 return 0;
3189
3190fail_irq:
3191fail_init_mfi:
3192 if (instance->evt_detail)
3193 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3194 instance->evt_detail,
3195 instance->evt_detail_h);
3196
3197 if (instance->producer)
3198 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3199 instance->producer_h);
3200 if (instance->consumer)
3201 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3202 instance->consumer_h);
3203 scsi_host_put(host);
3204
3205fail_set_dma_mask:
3206fail_ready_state:
3207
3208 pci_disable_device(pdev);
3209
3210 return -ENODEV;
3211}
33139b21
JS
3212#else
3213#define megasas_suspend NULL
3214#define megasas_resume NULL
3215#endif
31ea7088 3216
c4a3e0a5
BS
3217/**
3218 * megasas_detach_one - PCI hot"un"plug entry point
3219 * @pdev: PCI device structure
3220 */
33139b21 3221static void __devexit megasas_detach_one(struct pci_dev *pdev)
c4a3e0a5
BS
3222{
3223 int i;
3224 struct Scsi_Host *host;
3225 struct megasas_instance *instance;
3226
3227 instance = pci_get_drvdata(pdev);
c3518837 3228 instance->unload = 1;
c4a3e0a5
BS
3229 host = instance->host;
3230
ad84db2e 3231 if (poll_mode_io)
3232 del_timer_sync(&instance->io_completion_timer);
3233
c4a3e0a5
BS
3234 scsi_remove_host(instance->host);
3235 megasas_flush_cache(instance);
31ea7088 3236 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5d018ad0 3237 tasklet_kill(&instance->isr_tasklet);
c4a3e0a5
BS
3238
3239 /*
3240 * Take the instance off the instance array. Note that we will not
3241 * decrement the max_index. We let this array be sparse array
3242 */
3243 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3244 if (megasas_mgmt_info.instance[i] == instance) {
3245 megasas_mgmt_info.count--;
3246 megasas_mgmt_info.instance[i] = NULL;
3247
3248 break;
3249 }
3250 }
3251
3252 pci_set_drvdata(instance->pdev, NULL);
3253
b274cab7 3254 instance->instancet->disable_intr(instance->reg_set);
c4a3e0a5
BS
3255
3256 free_irq(instance->pdev->irq, instance);
3257
3258 megasas_release_mfi(instance);
3259
3260 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3261 instance->evt_detail, instance->evt_detail_h);
3262
3263 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3264 instance->producer_h);
3265
3266 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3267 instance->consumer_h);
3268
3269 scsi_host_put(host);
3270
3271 pci_set_drvdata(pdev, NULL);
3272
3273 pci_disable_device(pdev);
3274
3275 return;
3276}
3277
3278/**
3279 * megasas_shutdown - Shutdown entry point
3280 * @device: Generic device structure
3281 */
3282static void megasas_shutdown(struct pci_dev *pdev)
3283{
3284 struct megasas_instance *instance = pci_get_drvdata(pdev);
3285 megasas_flush_cache(instance);
530e6fc1 3286 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
c4a3e0a5
BS
3287}
3288
3289/**
3290 * megasas_mgmt_open - char node "open" entry point
3291 */
3292static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3293{
f2b9857e 3294 cycle_kernel_lock();
c4a3e0a5
BS
3295 /*
3296 * Allow only those users with admin rights
3297 */
3298 if (!capable(CAP_SYS_ADMIN))
3299 return -EACCES;
3300
3301 return 0;
3302}
3303
c4a3e0a5
BS
3304/**
3305 * megasas_mgmt_fasync - Async notifier registration from applications
3306 *
3307 * This function adds the calling process to a driver global queue. When an
3308 * event occurs, SIGIO will be sent to all processes in this queue.
3309 */
3310static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3311{
3312 int rc;
3313
0b950672 3314 mutex_lock(&megasas_async_queue_mutex);
c4a3e0a5
BS
3315
3316 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3317
0b950672 3318 mutex_unlock(&megasas_async_queue_mutex);
c4a3e0a5
BS
3319
3320 if (rc >= 0) {
3321 /* For sanity check when we get ioctl */
3322 filep->private_data = filep;
3323 return 0;
3324 }
3325
3326 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3327
3328 return rc;
3329}
3330
c3518837
YB
3331/**
3332 * megasas_mgmt_poll - char node "poll" entry point
3333 * */
3334static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3335{
3336 unsigned int mask;
3337 unsigned long flags;
3338 poll_wait(file, &megasas_poll_wait, wait);
3339 spin_lock_irqsave(&poll_aen_lock, flags);
3340 if (megasas_poll_wait_aen)
3341 mask = (POLLIN | POLLRDNORM);
3342 else
3343 mask = 0;
3344 spin_unlock_irqrestore(&poll_aen_lock, flags);
3345 return mask;
3346}
3347
c4a3e0a5
BS
3348/**
3349 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3350 * @instance: Adapter soft state
3351 * @argp: User's ioctl packet
3352 */
3353static int
3354megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3355 struct megasas_iocpacket __user * user_ioc,
3356 struct megasas_iocpacket *ioc)
3357{
3358 struct megasas_sge32 *kern_sge32;
3359 struct megasas_cmd *cmd;
3360 void *kbuff_arr[MAX_IOCTL_SGE];
3361 dma_addr_t buf_handle = 0;
3362 int error = 0, i;
3363 void *sense = NULL;
3364 dma_addr_t sense_handle;
3365 u32 *sense_ptr;
3366
3367 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3368
3369 if (ioc->sge_count > MAX_IOCTL_SGE) {
3370 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3371 ioc->sge_count, MAX_IOCTL_SGE);
3372 return -EINVAL;
3373 }
3374
3375 cmd = megasas_get_cmd(instance);
3376 if (!cmd) {
3377 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3378 return -ENOMEM;
3379 }
3380
3381 /*
3382 * User's IOCTL packet has 2 frames (maximum). Copy those two
3383 * frames into our cmd's frames. cmd->frame's context will get
3384 * overwritten when we copy from user's frames. So set that value
3385 * alone separately
3386 */
3387 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3388 cmd->frame->hdr.context = cmd->index;
c3518837 3389 cmd->frame->hdr.pad_0 = 0;
c4a3e0a5
BS
3390
3391 /*
3392 * The management interface between applications and the fw uses
3393 * MFI frames. E.g, RAID configuration changes, LD property changes
3394 * etc are accomplishes through different kinds of MFI frames. The
3395 * driver needs to care only about substituting user buffers with
3396 * kernel buffers in SGLs. The location of SGL is embedded in the
3397 * struct iocpacket itself.
3398 */
3399 kern_sge32 = (struct megasas_sge32 *)
3400 ((unsigned long)cmd->frame + ioc->sgl_off);
3401
3402 /*
3403 * For each user buffer, create a mirror buffer and copy in
3404 */
3405 for (i = 0; i < ioc->sge_count; i++) {
9f35fa8a 3406 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
c4a3e0a5 3407 ioc->sgl[i].iov_len,
9f35fa8a 3408 &buf_handle, GFP_KERNEL);
c4a3e0a5
BS
3409 if (!kbuff_arr[i]) {
3410 printk(KERN_DEBUG "megasas: Failed to alloc "
3411 "kernel SGL buffer for IOCTL \n");
3412 error = -ENOMEM;
3413 goto out;
3414 }
3415
3416 /*
3417 * We don't change the dma_coherent_mask, so
3418 * pci_alloc_consistent only returns 32bit addresses
3419 */
3420 kern_sge32[i].phys_addr = (u32) buf_handle;
3421 kern_sge32[i].length = ioc->sgl[i].iov_len;
3422
3423 /*
3424 * We created a kernel buffer corresponding to the
3425 * user buffer. Now copy in from the user buffer
3426 */
3427 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3428 (u32) (ioc->sgl[i].iov_len))) {
3429 error = -EFAULT;
3430 goto out;
3431 }
3432 }
3433
3434 if (ioc->sense_len) {
9f35fa8a
SP
3435 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3436 &sense_handle, GFP_KERNEL);
c4a3e0a5
BS
3437 if (!sense) {
3438 error = -ENOMEM;
3439 goto out;
3440 }
3441
3442 sense_ptr =
3443 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3444 *sense_ptr = sense_handle;
3445 }
3446
3447 /*
3448 * Set the sync_cmd flag so that the ISR knows not to complete this
3449 * cmd to the SCSI mid-layer
3450 */
3451 cmd->sync_cmd = 1;
3452 megasas_issue_blocked_cmd(instance, cmd);
3453 cmd->sync_cmd = 0;
3454
3455 /*
3456 * copy out the kernel buffers to user buffers
3457 */
3458 for (i = 0; i < ioc->sge_count; i++) {
3459 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3460 ioc->sgl[i].iov_len)) {
3461 error = -EFAULT;
3462 goto out;
3463 }
3464 }
3465
3466 /*
3467 * copy out the sense
3468 */
3469 if (ioc->sense_len) {
3470 /*
b70a41e0 3471 * sense_ptr points to the location that has the user
c4a3e0a5
BS
3472 * sense buffer address
3473 */
b70a41e0 3474 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3475 ioc->sense_off);
c4a3e0a5 3476
b70a41e0 3477 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3478 sense, ioc->sense_len)) {
b10c36a5 3479 printk(KERN_ERR "megasas: Failed to copy out to user "
3480 "sense data\n");
c4a3e0a5
BS
3481 error = -EFAULT;
3482 goto out;
3483 }
3484 }
3485
3486 /*
3487 * copy the status codes returned by the fw
3488 */
3489 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3490 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3491 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3492 error = -EFAULT;
3493 }
3494
3495 out:
3496 if (sense) {
9f35fa8a 3497 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
c4a3e0a5
BS
3498 sense, sense_handle);
3499 }
3500
3501 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
9f35fa8a 3502 dma_free_coherent(&instance->pdev->dev,
c4a3e0a5
BS
3503 kern_sge32[i].length,
3504 kbuff_arr[i], kern_sge32[i].phys_addr);
3505 }
3506
3507 megasas_return_cmd(instance, cmd);
3508 return error;
3509}
3510
c4a3e0a5
BS
3511static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3512{
3513 struct megasas_iocpacket __user *user_ioc =
3514 (struct megasas_iocpacket __user *)arg;
3515 struct megasas_iocpacket *ioc;
3516 struct megasas_instance *instance;
3517 int error;
3518
3519 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3520 if (!ioc)
3521 return -ENOMEM;
3522
3523 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3524 error = -EFAULT;
3525 goto out_kfree_ioc;
3526 }
3527
3528 instance = megasas_lookup_instance(ioc->host_no);
3529 if (!instance) {
3530 error = -ENODEV;
3531 goto out_kfree_ioc;
3532 }
3533
3534 /*
3535 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3536 */
3537 if (down_interruptible(&instance->ioctl_sem)) {
3538 error = -ERESTARTSYS;
3539 goto out_kfree_ioc;
3540 }
3541 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3542 up(&instance->ioctl_sem);
3543
3544 out_kfree_ioc:
3545 kfree(ioc);
3546 return error;
3547}
3548
3549static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3550{
3551 struct megasas_instance *instance;
3552 struct megasas_aen aen;
3553 int error;
3554
3555 if (file->private_data != file) {
3556 printk(KERN_DEBUG "megasas: fasync_helper was not "
3557 "called first\n");
3558 return -EINVAL;
3559 }
3560
3561 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3562 return -EFAULT;
3563
3564 instance = megasas_lookup_instance(aen.host_no);
3565
3566 if (!instance)
3567 return -ENODEV;
3568
e5a69e27 3569 mutex_lock(&instance->aen_mutex);
c4a3e0a5
BS
3570 error = megasas_register_aen(instance, aen.seq_num,
3571 aen.class_locale_word);
e5a69e27 3572 mutex_unlock(&instance->aen_mutex);
c4a3e0a5
BS
3573 return error;
3574}
3575
3576/**
3577 * megasas_mgmt_ioctl - char node ioctl entry point
3578 */
3579static long
3580megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3581{
3582 switch (cmd) {
3583 case MEGASAS_IOC_FIRMWARE:
3584 return megasas_mgmt_ioctl_fw(file, arg);
3585
3586 case MEGASAS_IOC_GET_AEN:
3587 return megasas_mgmt_ioctl_aen(file, arg);
3588 }
3589
3590 return -ENOTTY;
3591}
3592
3593#ifdef CONFIG_COMPAT
3594static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3595{
3596 struct compat_megasas_iocpacket __user *cioc =
3597 (struct compat_megasas_iocpacket __user *)arg;
3598 struct megasas_iocpacket __user *ioc =
3599 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3600 int i;
3601 int error = 0;
3602
83aabc1b
JG
3603 if (clear_user(ioc, sizeof(*ioc)))
3604 return -EFAULT;
c4a3e0a5
BS
3605
3606 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3607 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3608 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3609 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3610 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3611 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3612 return -EFAULT;
3613
3614 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3615 compat_uptr_t ptr;
3616
3617 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3618 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3619 copy_in_user(&ioc->sgl[i].iov_len,
3620 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3621 return -EFAULT;
3622 }
3623
3624 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3625
3626 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3627 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3628 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3629 return -EFAULT;
3630 }
3631 return error;
3632}
3633
3634static long
3635megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3636 unsigned long arg)
3637{
3638 switch (cmd) {
cb59aa6a
SP
3639 case MEGASAS_IOC_FIRMWARE32:
3640 return megasas_mgmt_compat_ioctl_fw(file, arg);
c4a3e0a5
BS
3641 case MEGASAS_IOC_GET_AEN:
3642 return megasas_mgmt_ioctl_aen(file, arg);
3643 }
3644
3645 return -ENOTTY;
3646}
3647#endif
3648
3649/*
3650 * File operations structure for management interface
3651 */
00977a59 3652static const struct file_operations megasas_mgmt_fops = {
c4a3e0a5
BS
3653 .owner = THIS_MODULE,
3654 .open = megasas_mgmt_open,
c4a3e0a5
BS
3655 .fasync = megasas_mgmt_fasync,
3656 .unlocked_ioctl = megasas_mgmt_ioctl,
c3518837 3657 .poll = megasas_mgmt_poll,
c4a3e0a5
BS
3658#ifdef CONFIG_COMPAT
3659 .compat_ioctl = megasas_mgmt_compat_ioctl,
3660#endif
3661};
3662
3663/*
3664 * PCI hotplug support registration structure
3665 */
3666static struct pci_driver megasas_pci_driver = {
3667
3668 .name = "megaraid_sas",
3669 .id_table = megasas_pci_table,
3670 .probe = megasas_probe_one,
3671 .remove = __devexit_p(megasas_detach_one),
31ea7088 3672 .suspend = megasas_suspend,
3673 .resume = megasas_resume,
c4a3e0a5
BS
3674 .shutdown = megasas_shutdown,
3675};
3676
3677/*
3678 * Sysfs driver attributes
3679 */
3680static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3681{
3682 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3683 MEGASAS_VERSION);
3684}
3685
3686static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3687
3688static ssize_t
3689megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3690{
3691 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3692 MEGASAS_RELDATE);
3693}
3694
3695static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3696 NULL);
3697
72c4fd36
YB
3698static ssize_t
3699megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
3700{
3701 return sprintf(buf, "%u\n", support_poll_for_event);
3702}
3703
3704static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
3705 megasas_sysfs_show_support_poll_for_event, NULL);
3706
658dcedb
SP
3707static ssize_t
3708megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3709{
ad84db2e 3710 return sprintf(buf, "%u\n", megasas_dbg_lvl);
658dcedb
SP
3711}
3712
3713static ssize_t
3714megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3715{
3716 int retval = count;
3717 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3718 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3719 retval = -EINVAL;
3720 }
3721 return retval;
3722}
3723
66dca9b8 3724static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
ad84db2e 3725 megasas_sysfs_set_dbg_lvl);
3726
3727static ssize_t
3728megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3729{
3730 return sprintf(buf, "%u\n", poll_mode_io);
3731}
3732
3733static ssize_t
3734megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3735 const char *buf, size_t count)
3736{
3737 int retval = count;
3738 int tmp = poll_mode_io;
3739 int i;
3740 struct megasas_instance *instance;
3741
3742 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3743 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3744 retval = -EINVAL;
3745 }
3746
3747 /*
3748 * Check if poll_mode_io is already set or is same as previous value
3749 */
3750 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3751 goto out;
3752
3753 if (poll_mode_io) {
3754 /*
3755 * Start timers for all adapters
3756 */
3757 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3758 instance = megasas_mgmt_info.instance[i];
3759 if (instance) {
3760 megasas_start_timer(instance,
3761 &instance->io_completion_timer,
3762 megasas_io_completion_timer,
3763 MEGASAS_COMPLETION_TIMER_INTERVAL);
3764 }
3765 }
3766 } else {
3767 /*
3768 * Delete timers for all adapters
3769 */
3770 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3771 instance = megasas_mgmt_info.instance[i];
3772 if (instance)
3773 del_timer_sync(&instance->io_completion_timer);
3774 }
3775 }
3776
3777out:
3778 return retval;
3779}
3780
3781static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3782 megasas_sysfs_show_poll_mode_io,
3783 megasas_sysfs_set_poll_mode_io);
658dcedb 3784
c4a3e0a5
BS
3785/**
3786 * megasas_init - Driver load entry point
3787 */
3788static int __init megasas_init(void)
3789{
3790 int rval;
3791
3792 /*
3793 * Announce driver version and other information
3794 */
3795 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3796 MEGASAS_EXT_VERSION);
3797
72c4fd36
YB
3798 support_poll_for_event = 2;
3799
c4a3e0a5
BS
3800 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3801
3802 /*
3803 * Register character device node
3804 */
3805 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3806
3807 if (rval < 0) {
3808 printk(KERN_DEBUG "megasas: failed to open device node\n");
3809 return rval;
3810 }
3811
3812 megasas_mgmt_majorno = rval;
3813
3814 /*
3815 * Register ourselves as PCI hotplug module
3816 */
4041b9cd 3817 rval = pci_register_driver(&megasas_pci_driver);
c4a3e0a5
BS
3818
3819 if (rval) {
3820 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
83aabc1b
JG
3821 goto err_pcidrv;
3822 }
3823
3824 rval = driver_create_file(&megasas_pci_driver.driver,
3825 &driver_attr_version);
3826 if (rval)
3827 goto err_dcf_attr_ver;
3828 rval = driver_create_file(&megasas_pci_driver.driver,
3829 &driver_attr_release_date);
3830 if (rval)
3831 goto err_dcf_rel_date;
72c4fd36
YB
3832
3833 rval = driver_create_file(&megasas_pci_driver.driver,
3834 &driver_attr_support_poll_for_event);
3835 if (rval)
3836 goto err_dcf_support_poll_for_event;
3837
83aabc1b
JG
3838 rval = driver_create_file(&megasas_pci_driver.driver,
3839 &driver_attr_dbg_lvl);
3840 if (rval)
3841 goto err_dcf_dbg_lvl;
ad84db2e 3842 rval = driver_create_file(&megasas_pci_driver.driver,
3843 &driver_attr_poll_mode_io);
3844 if (rval)
3845 goto err_dcf_poll_mode_io;
c4a3e0a5
BS
3846
3847 return rval;
ad84db2e 3848
3849err_dcf_poll_mode_io:
3850 driver_remove_file(&megasas_pci_driver.driver,
3851 &driver_attr_dbg_lvl);
83aabc1b 3852err_dcf_dbg_lvl:
72c4fd36
YB
3853 driver_remove_file(&megasas_pci_driver.driver,
3854 &driver_attr_support_poll_for_event);
3855
3856err_dcf_support_poll_for_event:
83aabc1b
JG
3857 driver_remove_file(&megasas_pci_driver.driver,
3858 &driver_attr_release_date);
72c4fd36 3859
83aabc1b
JG
3860err_dcf_rel_date:
3861 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3862err_dcf_attr_ver:
3863 pci_unregister_driver(&megasas_pci_driver);
3864err_pcidrv:
3865 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3866 return rval;
c4a3e0a5
BS
3867}
3868
3869/**
3870 * megasas_exit - Driver unload entry point
3871 */
3872static void __exit megasas_exit(void)
3873{
ad84db2e 3874 driver_remove_file(&megasas_pci_driver.driver,
3875 &driver_attr_poll_mode_io);
658dcedb
SP
3876 driver_remove_file(&megasas_pci_driver.driver,
3877 &driver_attr_dbg_lvl);
83aabc1b
JG
3878 driver_remove_file(&megasas_pci_driver.driver,
3879 &driver_attr_release_date);
3880 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
c4a3e0a5
BS
3881
3882 pci_unregister_driver(&megasas_pci_driver);
3883 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3884}
3885
3886module_init(megasas_init);
3887module_exit(megasas_exit);