]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/megaraid/megaraid_sas.c
[SCSI] megaraid_sas: infrastructure to get PDs from FW
[net-next-2.6.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
147aab6a
CH
1118static int megasas_slave_configure(struct scsi_device *sdev)
1119{
1120 /*
1121 * Don't export physical disk devices to the disk driver.
1122 *
1123 * FIXME: Currently we don't export them to the midlayer at all.
1124 * That will be fixed once LSI engineers have audited the
1125 * firmware for possible issues.
1126 */
1127 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
1128 return -ENXIO;
e5b3a65f
CH
1129
1130 /*
1131 * The RAID firmware may require extended timeouts.
1132 */
1133 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
ee1ab9e9
JB
1134 blk_queue_rq_timeout(sdev->request_queue,
1135 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
147aab6a
CH
1136 return 0;
1137}
1138
7343eb65 1139/**
1140 * megasas_complete_cmd_dpc - Returns FW's controller structure
1141 * @instance_addr: Address of adapter soft state
1142 *
1143 * Tasklet to complete cmds
1144 */
1145static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1146{
1147 u32 producer;
1148 u32 consumer;
1149 u32 context;
1150 struct megasas_cmd *cmd;
1151 struct megasas_instance *instance =
1152 (struct megasas_instance *)instance_addr;
1153 unsigned long flags;
1154
1155 /* If we have already declared adapter dead, donot complete cmds */
1156 if (instance->hw_crit_error)
1157 return;
1158
1159 spin_lock_irqsave(&instance->completion_lock, flags);
1160
1161 producer = *instance->producer;
1162 consumer = *instance->consumer;
1163
1164 while (consumer != producer) {
1165 context = instance->reply_queue[consumer];
1166
1167 cmd = instance->cmd_list[context];
1168
1169 megasas_complete_cmd(instance, cmd, DID_OK);
1170
1171 consumer++;
1172 if (consumer == (instance->max_fw_cmds + 1)) {
1173 consumer = 0;
1174 }
1175 }
1176
1177 *instance->consumer = producer;
1178
1179 spin_unlock_irqrestore(&instance->completion_lock, flags);
1180
1181 /*
1182 * Check if we can restore can_queue
1183 */
1184 if (instance->flag & MEGASAS_FW_BUSY
1185 && time_after(jiffies, instance->last_time + 5 * HZ)
1186 && atomic_read(&instance->fw_outstanding) < 17) {
1187
1188 spin_lock_irqsave(instance->host->host_lock, flags);
1189 instance->flag &= ~MEGASAS_FW_BUSY;
1190 instance->host->can_queue =
1191 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1192
1193 spin_unlock_irqrestore(instance->host->host_lock, flags);
1194 }
1195}
1196
c4a3e0a5
BS
1197/**
1198 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1199 * @instance: Adapter soft state
1200 *
1201 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1202 * complete all its outstanding commands. Returns error if one or more IOs
1203 * are pending after this time period. It also marks the controller dead.
1204 */
1205static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1206{
1207 int i;
1208 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1209
1210 for (i = 0; i < wait_time; i++) {
1211
e4a082c7
SP
1212 int outstanding = atomic_read(&instance->fw_outstanding);
1213
1214 if (!outstanding)
c4a3e0a5
BS
1215 break;
1216
1217 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1218 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
e4a082c7 1219 "commands to complete\n",i,outstanding);
7343eb65 1220 /*
1221 * Call cmd completion routine. Cmd to be
1222 * be completed directly without depending on isr.
1223 */
1224 megasas_complete_cmd_dpc((unsigned long)instance);
c4a3e0a5
BS
1225 }
1226
1227 msleep(1000);
1228 }
1229
e4a082c7 1230 if (atomic_read(&instance->fw_outstanding)) {
e3bbff9f
SP
1231 /*
1232 * Send signal to FW to stop processing any pending cmds.
1233 * The controller will be taken offline by the OS now.
1234 */
1235 writel(MFI_STOP_ADP,
1236 &instance->reg_set->inbound_doorbell);
658dcedb 1237 megasas_dump_pending_frames(instance);
c4a3e0a5
BS
1238 instance->hw_crit_error = 1;
1239 return FAILED;
1240 }
1241
1242 return SUCCESS;
1243}
1244
1245/**
1246 * megasas_generic_reset - Generic reset routine
1247 * @scmd: Mid-layer SCSI command
1248 *
1249 * This routine implements a generic reset handler for device, bus and host
1250 * reset requests. Device, bus and host specific reset handlers can use this
1251 * function after they do their specific tasks.
1252 */
1253static int megasas_generic_reset(struct scsi_cmnd *scmd)
1254{
1255 int ret_val;
1256 struct megasas_instance *instance;
1257
1258 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1259
05e9ebbe
SP
1260 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1261 scmd->serial_number, scmd->cmnd[0], scmd->retries);
c4a3e0a5
BS
1262
1263 if (instance->hw_crit_error) {
1264 printk(KERN_ERR "megasas: cannot recover from previous reset "
1265 "failures\n");
1266 return FAILED;
1267 }
1268
c4a3e0a5 1269 ret_val = megasas_wait_for_outstanding(instance);
c4a3e0a5
BS
1270 if (ret_val == SUCCESS)
1271 printk(KERN_NOTICE "megasas: reset successful \n");
1272 else
1273 printk(KERN_ERR "megasas: failed to do reset\n");
1274
c4a3e0a5
BS
1275 return ret_val;
1276}
1277
05e9ebbe
SP
1278/**
1279 * megasas_reset_timer - quiesce the adapter if required
1280 * @scmd: scsi cmnd
1281 *
1282 * Sets the FW busy flag and reduces the host->can_queue if the
1283 * cmd has not been completed within the timeout period.
1284 */
1285static enum
242f9dcb 1286blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
05e9ebbe
SP
1287{
1288 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1289 struct megasas_instance *instance;
1290 unsigned long flags;
1291
1292 if (time_after(jiffies, scmd->jiffies_at_alloc +
1293 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
242f9dcb 1294 return BLK_EH_NOT_HANDLED;
05e9ebbe
SP
1295 }
1296
1297 instance = cmd->instance;
1298 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1299 /* FW is busy, throttle IO */
1300 spin_lock_irqsave(instance->host->host_lock, flags);
1301
1302 instance->host->can_queue = 16;
1303 instance->last_time = jiffies;
1304 instance->flag |= MEGASAS_FW_BUSY;
1305
1306 spin_unlock_irqrestore(instance->host->host_lock, flags);
1307 }
242f9dcb 1308 return BLK_EH_RESET_TIMER;
05e9ebbe
SP
1309}
1310
c4a3e0a5
BS
1311/**
1312 * megasas_reset_device - Device reset handler entry point
1313 */
1314static int megasas_reset_device(struct scsi_cmnd *scmd)
1315{
1316 int ret;
1317
1318 /*
1319 * First wait for all commands to complete
1320 */
1321 ret = megasas_generic_reset(scmd);
1322
1323 return ret;
1324}
1325
1326/**
1327 * megasas_reset_bus_host - Bus & host reset handler entry point
1328 */
1329static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1330{
1331 int ret;
1332
1333 /*
80682fa9 1334 * First wait for all commands to complete
c4a3e0a5
BS
1335 */
1336 ret = megasas_generic_reset(scmd);
1337
1338 return ret;
1339}
1340
cf62a0a5
SP
1341/**
1342 * megasas_bios_param - Returns disk geometry for a disk
1343 * @sdev: device handle
1344 * @bdev: block device
1345 * @capacity: drive capacity
1346 * @geom: geometry parameters
1347 */
1348static int
1349megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1350 sector_t capacity, int geom[])
1351{
1352 int heads;
1353 int sectors;
1354 sector_t cylinders;
1355 unsigned long tmp;
1356 /* Default heads (64) & sectors (32) */
1357 heads = 64;
1358 sectors = 32;
1359
1360 tmp = heads * sectors;
1361 cylinders = capacity;
1362
1363 sector_div(cylinders, tmp);
1364
1365 /*
1366 * Handle extended translation size for logical drives > 1Gb
1367 */
1368
1369 if (capacity >= 0x200000) {
1370 heads = 255;
1371 sectors = 63;
1372 tmp = heads*sectors;
1373 cylinders = capacity;
1374 sector_div(cylinders, tmp);
1375 }
1376
1377 geom[0] = heads;
1378 geom[1] = sectors;
1379 geom[2] = cylinders;
1380
1381 return 0;
1382}
1383
c4a3e0a5
BS
1384/**
1385 * megasas_service_aen - Processes an event notification
1386 * @instance: Adapter soft state
1387 * @cmd: AEN command completed by the ISR
1388 *
1389 * For AEN, driver sends a command down to FW that is held by the FW till an
1390 * event occurs. When an event of interest occurs, FW completes the command
1391 * that it was previously holding.
1392 *
1393 * This routines sends SIGIO signal to processes that have registered with the
1394 * driver for AEN.
1395 */
1396static void
1397megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1398{
c3518837 1399 unsigned long flags;
c4a3e0a5
BS
1400 /*
1401 * Don't signal app if it is just an aborted previously registered aen
1402 */
c3518837
YB
1403 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1404 spin_lock_irqsave(&poll_aen_lock, flags);
1405 megasas_poll_wait_aen = 1;
1406 spin_unlock_irqrestore(&poll_aen_lock, flags);
1407 wake_up(&megasas_poll_wait);
c4a3e0a5 1408 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
c3518837 1409 }
c4a3e0a5
BS
1410 else
1411 cmd->abort_aen = 0;
1412
1413 instance->aen_cmd = NULL;
1414 megasas_return_cmd(instance, cmd);
1415}
1416
1417/*
1418 * Scsi host template for megaraid_sas driver
1419 */
1420static struct scsi_host_template megasas_template = {
1421
1422 .module = THIS_MODULE,
f28cd7cf 1423 .name = "LSI SAS based MegaRAID driver",
c4a3e0a5 1424 .proc_name = "megaraid_sas",
147aab6a 1425 .slave_configure = megasas_slave_configure,
c4a3e0a5
BS
1426 .queuecommand = megasas_queue_command,
1427 .eh_device_reset_handler = megasas_reset_device,
1428 .eh_bus_reset_handler = megasas_reset_bus_host,
1429 .eh_host_reset_handler = megasas_reset_bus_host,
05e9ebbe 1430 .eh_timed_out = megasas_reset_timer,
cf62a0a5 1431 .bios_param = megasas_bios_param,
c4a3e0a5
BS
1432 .use_clustering = ENABLE_CLUSTERING,
1433};
1434
1435/**
1436 * megasas_complete_int_cmd - Completes an internal command
1437 * @instance: Adapter soft state
1438 * @cmd: Command to be completed
1439 *
1440 * The megasas_issue_blocked_cmd() function waits for a command to complete
1441 * after it issues a command. This function wakes up that waiting routine by
1442 * calling wake_up() on the wait queue.
1443 */
1444static void
1445megasas_complete_int_cmd(struct megasas_instance *instance,
1446 struct megasas_cmd *cmd)
1447{
1448 cmd->cmd_status = cmd->frame->io.cmd_status;
1449
1450 if (cmd->cmd_status == ENODATA) {
1451 cmd->cmd_status = 0;
1452 }
1453 wake_up(&instance->int_cmd_wait_q);
1454}
1455
1456/**
1457 * megasas_complete_abort - Completes aborting a command
1458 * @instance: Adapter soft state
1459 * @cmd: Cmd that was issued to abort another cmd
1460 *
1461 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1462 * after it issues an abort on a previously issued command. This function
1463 * wakes up all functions waiting on the same wait queue.
1464 */
1465static void
1466megasas_complete_abort(struct megasas_instance *instance,
1467 struct megasas_cmd *cmd)
1468{
1469 if (cmd->sync_cmd) {
1470 cmd->sync_cmd = 0;
1471 cmd->cmd_status = 0;
1472 wake_up(&instance->abort_cmd_wait_q);
1473 }
1474
1475 return;
1476}
1477
c4a3e0a5
BS
1478/**
1479 * megasas_complete_cmd - Completes a command
1480 * @instance: Adapter soft state
1481 * @cmd: Command to be completed
1482 * @alt_status: If non-zero, use this value as status to
1483 * SCSI mid-layer instead of the value returned
1484 * by the FW. This should be used if caller wants
1485 * an alternate status (as in the case of aborted
1486 * commands)
1487 */
858119e1 1488static void
c4a3e0a5
BS
1489megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1490 u8 alt_status)
1491{
1492 int exception = 0;
1493 struct megasas_header *hdr = &cmd->frame->hdr;
c3518837 1494 unsigned long flags;
c4a3e0a5 1495
05e9ebbe
SP
1496 if (cmd->scmd)
1497 cmd->scmd->SCp.ptr = NULL;
c4a3e0a5
BS
1498
1499 switch (hdr->cmd) {
1500
1501 case MFI_CMD_PD_SCSI_IO:
1502 case MFI_CMD_LD_SCSI_IO:
1503
1504 /*
1505 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1506 * issued either through an IO path or an IOCTL path. If it
1507 * was via IOCTL, we will send it to internal completion.
1508 */
1509 if (cmd->sync_cmd) {
1510 cmd->sync_cmd = 0;
1511 megasas_complete_int_cmd(instance, cmd);
1512 break;
1513 }
1514
c4a3e0a5
BS
1515 case MFI_CMD_LD_READ:
1516 case MFI_CMD_LD_WRITE:
1517
1518 if (alt_status) {
1519 cmd->scmd->result = alt_status << 16;
1520 exception = 1;
1521 }
1522
1523 if (exception) {
1524
e4a082c7 1525 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 1526
155d98f0 1527 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
1528 cmd->scmd->scsi_done(cmd->scmd);
1529 megasas_return_cmd(instance, cmd);
1530
1531 break;
1532 }
1533
1534 switch (hdr->cmd_status) {
1535
1536 case MFI_STAT_OK:
1537 cmd->scmd->result = DID_OK << 16;
1538 break;
1539
1540 case MFI_STAT_SCSI_IO_FAILED:
1541 case MFI_STAT_LD_INIT_IN_PROGRESS:
1542 cmd->scmd->result =
1543 (DID_ERROR << 16) | hdr->scsi_status;
1544 break;
1545
1546 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1547
1548 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1549
1550 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1551 memset(cmd->scmd->sense_buffer, 0,
1552 SCSI_SENSE_BUFFERSIZE);
1553 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1554 hdr->sense_len);
1555
1556 cmd->scmd->result |= DRIVER_SENSE << 24;
1557 }
1558
1559 break;
1560
1561 case MFI_STAT_LD_OFFLINE:
1562 case MFI_STAT_DEVICE_NOT_FOUND:
1563 cmd->scmd->result = DID_BAD_TARGET << 16;
1564 break;
1565
1566 default:
1567 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1568 hdr->cmd_status);
1569 cmd->scmd->result = DID_ERROR << 16;
1570 break;
1571 }
1572
e4a082c7 1573 atomic_dec(&instance->fw_outstanding);
c4a3e0a5 1574
155d98f0 1575 scsi_dma_unmap(cmd->scmd);
c4a3e0a5
BS
1576 cmd->scmd->scsi_done(cmd->scmd);
1577 megasas_return_cmd(instance, cmd);
1578
1579 break;
1580
1581 case MFI_CMD_SMP:
1582 case MFI_CMD_STP:
1583 case MFI_CMD_DCMD:
c3518837
YB
1584 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1585 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1586 spin_lock_irqsave(&poll_aen_lock, flags);
1587 megasas_poll_wait_aen = 0;
1588 spin_unlock_irqrestore(&poll_aen_lock, flags);
1589 }
c4a3e0a5
BS
1590
1591 /*
1592 * See if got an event notification
1593 */
1594 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1595 megasas_service_aen(instance, cmd);
1596 else
1597 megasas_complete_int_cmd(instance, cmd);
1598
1599 break;
1600
1601 case MFI_CMD_ABORT:
1602 /*
1603 * Cmd issued to abort another cmd returned
1604 */
1605 megasas_complete_abort(instance, cmd);
1606 break;
1607
1608 default:
1609 printk("megasas: Unknown command completed! [0x%X]\n",
1610 hdr->cmd);
1611 break;
1612 }
1613}
1614
1615/**
1616 * megasas_deplete_reply_queue - Processes all completed commands
1617 * @instance: Adapter soft state
1618 * @alt_status: Alternate status to be returned to
1619 * SCSI mid-layer instead of the status
1620 * returned by the FW
1621 */
858119e1 1622static int
c4a3e0a5
BS
1623megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1624{
c4a3e0a5
BS
1625 /*
1626 * Check if it is our interrupt
1341c939 1627 * Clear the interrupt
c4a3e0a5 1628 */
1341c939 1629 if(instance->instancet->clear_intr(instance->reg_set))
c4a3e0a5 1630 return IRQ_NONE;
c4a3e0a5 1631
af37acfb
SP
1632 if (instance->hw_crit_error)
1633 goto out_done;
5d018ad0
SP
1634 /*
1635 * Schedule the tasklet for cmd completion
1636 */
1637 tasklet_schedule(&instance->isr_tasklet);
af37acfb 1638out_done:
c4a3e0a5
BS
1639 return IRQ_HANDLED;
1640}
1641
1642/**
1643 * megasas_isr - isr entry point
1644 */
7d12e780 1645static irqreturn_t megasas_isr(int irq, void *devp)
c4a3e0a5
BS
1646{
1647 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1648 DID_OK);
1649}
1650
1651/**
1652 * megasas_transition_to_ready - Move the FW to READY state
1341c939 1653 * @instance: Adapter soft state
c4a3e0a5
BS
1654 *
1655 * During the initialization, FW passes can potentially be in any one of
1656 * several possible states. If the FW in operational, waiting-for-handshake
1657 * states, driver must take steps to bring it to ready state. Otherwise, it
1658 * has to wait for the ready state.
1659 */
1660static int
1341c939 1661megasas_transition_to_ready(struct megasas_instance* instance)
c4a3e0a5
BS
1662{
1663 int i;
1664 u8 max_wait;
1665 u32 fw_state;
1666 u32 cur_state;
1667
1341c939 1668 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
c4a3e0a5 1669
e3bbff9f
SP
1670 if (fw_state != MFI_STATE_READY)
1671 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1672 " state\n");
1673
c4a3e0a5
BS
1674 while (fw_state != MFI_STATE_READY) {
1675
c4a3e0a5
BS
1676 switch (fw_state) {
1677
1678 case MFI_STATE_FAULT:
1679
1680 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1681 return -ENODEV;
1682
1683 case MFI_STATE_WAIT_HANDSHAKE:
1684 /*
1685 * Set the CLR bit in inbound doorbell
1686 */
87911122
YB
1687 if ((instance->pdev->device == \
1688 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1689 (instance->pdev->device ==
1690 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1691
1692 writel(
1693 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1694 &instance->reg_set->reserved_0[0]);
1695 } else {
1696 writel(
1697 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1698 &instance->reg_set->inbound_doorbell);
1699 }
c4a3e0a5
BS
1700
1701 max_wait = 2;
1702 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1703 break;
1704
e3bbff9f 1705 case MFI_STATE_BOOT_MESSAGE_PENDING:
87911122
YB
1706 if ((instance->pdev->device ==
1707 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1708 (instance->pdev->device ==
1709 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1710 writel(MFI_INIT_HOTPLUG,
1711 &instance->reg_set->reserved_0[0]);
1712 } else
1713 writel(MFI_INIT_HOTPLUG,
1714 &instance->reg_set->inbound_doorbell);
e3bbff9f
SP
1715
1716 max_wait = 10;
1717 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1718 break;
1719
c4a3e0a5
BS
1720 case MFI_STATE_OPERATIONAL:
1721 /*
e3bbff9f 1722 * Bring it to READY state; assuming max wait 10 secs
c4a3e0a5 1723 */
b274cab7 1724 instance->instancet->disable_intr(instance->reg_set);
87911122
YB
1725 if ((instance->pdev->device ==
1726 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1727 (instance->pdev->device ==
1728 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1729 writel(MFI_RESET_FLAGS,
1730 &instance->reg_set->reserved_0[0]);
1731 } else
1732 writel(MFI_RESET_FLAGS,
1733 &instance->reg_set->inbound_doorbell);
c4a3e0a5 1734
af7a5647 1735 max_wait = 60;
c4a3e0a5
BS
1736 cur_state = MFI_STATE_OPERATIONAL;
1737 break;
1738
1739 case MFI_STATE_UNDEFINED:
1740 /*
1741 * This state should not last for more than 2 seconds
1742 */
1743 max_wait = 2;
1744 cur_state = MFI_STATE_UNDEFINED;
1745 break;
1746
1747 case MFI_STATE_BB_INIT:
1748 max_wait = 2;
1749 cur_state = MFI_STATE_BB_INIT;
1750 break;
1751
1752 case MFI_STATE_FW_INIT:
1753 max_wait = 20;
1754 cur_state = MFI_STATE_FW_INIT;
1755 break;
1756
1757 case MFI_STATE_FW_INIT_2:
1758 max_wait = 20;
1759 cur_state = MFI_STATE_FW_INIT_2;
1760 break;
1761
1762 case MFI_STATE_DEVICE_SCAN:
1763 max_wait = 20;
1764 cur_state = MFI_STATE_DEVICE_SCAN;
1765 break;
1766
1767 case MFI_STATE_FLUSH_CACHE:
1768 max_wait = 20;
1769 cur_state = MFI_STATE_FLUSH_CACHE;
1770 break;
1771
1772 default:
1773 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1774 fw_state);
1775 return -ENODEV;
1776 }
1777
1778 /*
1779 * The cur_state should not last for more than max_wait secs
1780 */
1781 for (i = 0; i < (max_wait * 1000); i++) {
1341c939
SP
1782 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1783 MFI_STATE_MASK ;
c4a3e0a5
BS
1784
1785 if (fw_state == cur_state) {
1786 msleep(1);
1787 } else
1788 break;
1789 }
1790
1791 /*
1792 * Return error if fw_state hasn't changed after max_wait
1793 */
1794 if (fw_state == cur_state) {
1795 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1796 "in %d secs\n", fw_state, max_wait);
1797 return -ENODEV;
1798 }
1799 };
e3bbff9f 1800 printk(KERN_INFO "megasas: FW now in Ready state\n");
c4a3e0a5
BS
1801
1802 return 0;
1803}
1804
1805/**
1806 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1807 * @instance: Adapter soft state
1808 */
1809static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1810{
1811 int i;
1812 u32 max_cmd = instance->max_fw_cmds;
1813 struct megasas_cmd *cmd;
1814
1815 if (!instance->frame_dma_pool)
1816 return;
1817
1818 /*
1819 * Return all frames to pool
1820 */
1821 for (i = 0; i < max_cmd; i++) {
1822
1823 cmd = instance->cmd_list[i];
1824
1825 if (cmd->frame)
1826 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1827 cmd->frame_phys_addr);
1828
1829 if (cmd->sense)
e3bbff9f 1830 pci_pool_free(instance->sense_dma_pool, cmd->sense,
c4a3e0a5
BS
1831 cmd->sense_phys_addr);
1832 }
1833
1834 /*
1835 * Now destroy the pool itself
1836 */
1837 pci_pool_destroy(instance->frame_dma_pool);
1838 pci_pool_destroy(instance->sense_dma_pool);
1839
1840 instance->frame_dma_pool = NULL;
1841 instance->sense_dma_pool = NULL;
1842}
1843
1844/**
1845 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1846 * @instance: Adapter soft state
1847 *
1848 * Each command packet has an embedded DMA memory buffer that is used for
1849 * filling MFI frame and the SG list that immediately follows the frame. This
1850 * function creates those DMA memory buffers for each command packet by using
1851 * PCI pool facility.
1852 */
1853static int megasas_create_frame_pool(struct megasas_instance *instance)
1854{
1855 int i;
1856 u32 max_cmd;
1857 u32 sge_sz;
1858 u32 sgl_sz;
1859 u32 total_sz;
1860 u32 frame_count;
1861 struct megasas_cmd *cmd;
1862
1863 max_cmd = instance->max_fw_cmds;
1864
1865 /*
1866 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1867 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1868 */
1869 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1870 sizeof(struct megasas_sge32);
1871
1872 /*
1873 * Calculated the number of 64byte frames required for SGL
1874 */
1875 sgl_sz = sge_sz * instance->max_num_sge;
1876 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1877
1878 /*
1879 * We need one extra frame for the MFI command
1880 */
1881 frame_count++;
1882
1883 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1884 /*
1885 * Use DMA pool facility provided by PCI layer
1886 */
1887 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1888 instance->pdev, total_sz, 64,
1889 0);
1890
1891 if (!instance->frame_dma_pool) {
1892 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1893 return -ENOMEM;
1894 }
1895
1896 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1897 instance->pdev, 128, 4, 0);
1898
1899 if (!instance->sense_dma_pool) {
1900 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1901
1902 pci_pool_destroy(instance->frame_dma_pool);
1903 instance->frame_dma_pool = NULL;
1904
1905 return -ENOMEM;
1906 }
1907
1908 /*
1909 * Allocate and attach a frame to each of the commands in cmd_list.
1910 * By making cmd->index as the context instead of the &cmd, we can
1911 * always use 32bit context regardless of the architecture
1912 */
1913 for (i = 0; i < max_cmd; i++) {
1914
1915 cmd = instance->cmd_list[i];
1916
1917 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1918 GFP_KERNEL, &cmd->frame_phys_addr);
1919
1920 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1921 GFP_KERNEL, &cmd->sense_phys_addr);
1922
1923 /*
1924 * megasas_teardown_frame_pool() takes care of freeing
1925 * whatever has been allocated
1926 */
1927 if (!cmd->frame || !cmd->sense) {
1928 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1929 megasas_teardown_frame_pool(instance);
1930 return -ENOMEM;
1931 }
1932
1933 cmd->frame->io.context = cmd->index;
1934 }
1935
1936 return 0;
1937}
1938
1939/**
1940 * megasas_free_cmds - Free all the cmds in the free cmd pool
1941 * @instance: Adapter soft state
1942 */
1943static void megasas_free_cmds(struct megasas_instance *instance)
1944{
1945 int i;
1946 /* First free the MFI frame pool */
1947 megasas_teardown_frame_pool(instance);
1948
1949 /* Free all the commands in the cmd_list */
1950 for (i = 0; i < instance->max_fw_cmds; i++)
1951 kfree(instance->cmd_list[i]);
1952
1953 /* Free the cmd_list buffer itself */
1954 kfree(instance->cmd_list);
1955 instance->cmd_list = NULL;
1956
1957 INIT_LIST_HEAD(&instance->cmd_pool);
1958}
1959
1960/**
1961 * megasas_alloc_cmds - Allocates the command packets
1962 * @instance: Adapter soft state
1963 *
1964 * Each command that is issued to the FW, whether IO commands from the OS or
1965 * internal commands like IOCTLs, are wrapped in local data structure called
1966 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1967 * the FW.
1968 *
1969 * Each frame has a 32-bit field called context (tag). This context is used
1970 * to get back the megasas_cmd from the frame when a frame gets completed in
1971 * the ISR. Typically the address of the megasas_cmd itself would be used as
1972 * the context. But we wanted to keep the differences between 32 and 64 bit
1973 * systems to the mininum. We always use 32 bit integers for the context. In
1974 * this driver, the 32 bit values are the indices into an array cmd_list.
1975 * This array is used only to look up the megasas_cmd given the context. The
1976 * free commands themselves are maintained in a linked list called cmd_pool.
1977 */
1978static int megasas_alloc_cmds(struct megasas_instance *instance)
1979{
1980 int i;
1981 int j;
1982 u32 max_cmd;
1983 struct megasas_cmd *cmd;
1984
1985 max_cmd = instance->max_fw_cmds;
1986
1987 /*
1988 * instance->cmd_list is an array of struct megasas_cmd pointers.
1989 * Allocate the dynamic array first and then allocate individual
1990 * commands.
1991 */
dd00cc48 1992 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
c4a3e0a5
BS
1993
1994 if (!instance->cmd_list) {
1995 printk(KERN_DEBUG "megasas: out of memory\n");
1996 return -ENOMEM;
1997 }
1998
c4a3e0a5
BS
1999
2000 for (i = 0; i < max_cmd; i++) {
2001 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2002 GFP_KERNEL);
2003
2004 if (!instance->cmd_list[i]) {
2005
2006 for (j = 0; j < i; j++)
2007 kfree(instance->cmd_list[j]);
2008
2009 kfree(instance->cmd_list);
2010 instance->cmd_list = NULL;
2011
2012 return -ENOMEM;
2013 }
2014 }
2015
2016 /*
2017 * Add all the commands to command pool (instance->cmd_pool)
2018 */
2019 for (i = 0; i < max_cmd; i++) {
2020 cmd = instance->cmd_list[i];
2021 memset(cmd, 0, sizeof(struct megasas_cmd));
2022 cmd->index = i;
2023 cmd->instance = instance;
2024
2025 list_add_tail(&cmd->list, &instance->cmd_pool);
2026 }
2027
2028 /*
2029 * Create a frame pool and assign one frame to each cmd
2030 */
2031 if (megasas_create_frame_pool(instance)) {
2032 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2033 megasas_free_cmds(instance);
2034 }
2035
2036 return 0;
2037}
2038
81e403ce
YB
2039/*
2040 * megasas_get_pd_list_info - Returns FW's pd_list structure
2041 * @instance: Adapter soft state
2042 * @pd_list: pd_list structure
2043 *
2044 * Issues an internal command (DCMD) to get the FW's controller PD
2045 * list structure. This information is mainly used to find out SYSTEM
2046 * supported by the FW.
2047 */
2048static int
2049megasas_get_pd_list(struct megasas_instance *instance)
2050{
2051 int ret = 0, pd_index = 0;
2052 struct megasas_cmd *cmd;
2053 struct megasas_dcmd_frame *dcmd;
2054 struct MR_PD_LIST *ci;
2055 struct MR_PD_ADDRESS *pd_addr;
2056 dma_addr_t ci_h = 0;
2057
2058 cmd = megasas_get_cmd(instance);
2059
2060 if (!cmd) {
2061 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2062 return -ENOMEM;
2063 }
2064
2065 dcmd = &cmd->frame->dcmd;
2066
2067 ci = pci_alloc_consistent(instance->pdev,
2068 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2069
2070 if (!ci) {
2071 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2072 megasas_return_cmd(instance, cmd);
2073 return -ENOMEM;
2074 }
2075
2076 memset(ci, 0, sizeof(*ci));
2077 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2078
2079 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2080 dcmd->mbox.b[1] = 0;
2081 dcmd->cmd = MFI_CMD_DCMD;
2082 dcmd->cmd_status = 0xFF;
2083 dcmd->sge_count = 1;
2084 dcmd->flags = MFI_FRAME_DIR_READ;
2085 dcmd->timeout = 0;
2086 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2087 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2088 dcmd->sgl.sge32[0].phys_addr = ci_h;
2089 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2090
2091 if (!megasas_issue_polled(instance, cmd)) {
2092 ret = 0;
2093 } else {
2094 ret = -1;
2095 }
2096
2097 /*
2098 * the following function will get the instance PD LIST.
2099 */
2100
2101 pd_addr = ci->addr;
2102
2103 if ( ret == 0 &&
2104 (ci->count <
2105 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2106
2107 memset(instance->pd_list, 0,
2108 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2109
2110 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2111
2112 instance->pd_list[pd_addr->deviceId].tid =
2113 pd_addr->deviceId;
2114 instance->pd_list[pd_addr->deviceId].driveType =
2115 pd_addr->scsiDevType;
2116 instance->pd_list[pd_addr->deviceId].driveState =
2117 MR_PD_STATE_SYSTEM;
2118 pd_addr++;
2119 }
2120 }
2121
2122 pci_free_consistent(instance->pdev,
2123 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2124 ci, ci_h);
2125 megasas_return_cmd(instance, cmd);
2126
2127 return ret;
2128}
2129
2130
c4a3e0a5
BS
2131/**
2132 * megasas_get_controller_info - Returns FW's controller structure
2133 * @instance: Adapter soft state
2134 * @ctrl_info: Controller information structure
2135 *
2136 * Issues an internal command (DCMD) to get the FW's controller structure.
2137 * This information is mainly used to find out the maximum IO transfer per
2138 * command supported by the FW.
2139 */
2140static int
2141megasas_get_ctrl_info(struct megasas_instance *instance,
2142 struct megasas_ctrl_info *ctrl_info)
2143{
2144 int ret = 0;
2145 struct megasas_cmd *cmd;
2146 struct megasas_dcmd_frame *dcmd;
2147 struct megasas_ctrl_info *ci;
2148 dma_addr_t ci_h = 0;
2149
2150 cmd = megasas_get_cmd(instance);
2151
2152 if (!cmd) {
2153 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2154 return -ENOMEM;
2155 }
2156
2157 dcmd = &cmd->frame->dcmd;
2158
2159 ci = pci_alloc_consistent(instance->pdev,
2160 sizeof(struct megasas_ctrl_info), &ci_h);
2161
2162 if (!ci) {
2163 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2164 megasas_return_cmd(instance, cmd);
2165 return -ENOMEM;
2166 }
2167
2168 memset(ci, 0, sizeof(*ci));
2169 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2170
2171 dcmd->cmd = MFI_CMD_DCMD;
2172 dcmd->cmd_status = 0xFF;
2173 dcmd->sge_count = 1;
2174 dcmd->flags = MFI_FRAME_DIR_READ;
2175 dcmd->timeout = 0;
2176 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2177 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2178 dcmd->sgl.sge32[0].phys_addr = ci_h;
2179 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2180
2181 if (!megasas_issue_polled(instance, cmd)) {
2182 ret = 0;
2183 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2184 } else {
2185 ret = -1;
2186 }
2187
2188 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2189 ci, ci_h);
2190
2191 megasas_return_cmd(instance, cmd);
2192 return ret;
2193}
2194
31ea7088 2195/**
2196 * megasas_issue_init_mfi - Initializes the FW
2197 * @instance: Adapter soft state
2198 *
2199 * Issues the INIT MFI cmd
2200 */
2201static int
2202megasas_issue_init_mfi(struct megasas_instance *instance)
2203{
2204 u32 context;
2205
2206 struct megasas_cmd *cmd;
2207
2208 struct megasas_init_frame *init_frame;
2209 struct megasas_init_queue_info *initq_info;
2210 dma_addr_t init_frame_h;
2211 dma_addr_t initq_info_h;
2212
2213 /*
2214 * Prepare a init frame. Note the init frame points to queue info
2215 * structure. Each frame has SGL allocated after first 64 bytes. For
2216 * this frame - since we don't need any SGL - we use SGL's space as
2217 * queue info structure
2218 *
2219 * We will not get a NULL command below. We just created the pool.
2220 */
2221 cmd = megasas_get_cmd(instance);
2222
2223 init_frame = (struct megasas_init_frame *)cmd->frame;
2224 initq_info = (struct megasas_init_queue_info *)
2225 ((unsigned long)init_frame + 64);
2226
2227 init_frame_h = cmd->frame_phys_addr;
2228 initq_info_h = init_frame_h + 64;
2229
2230 context = init_frame->context;
2231 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2232 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2233 init_frame->context = context;
2234
2235 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2236 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2237
2238 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2239 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2240
2241 init_frame->cmd = MFI_CMD_INIT;
2242 init_frame->cmd_status = 0xFF;
2243 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2244
2245 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2246
2247 /*
2248 * disable the intr before firing the init frame to FW
2249 */
2250 instance->instancet->disable_intr(instance->reg_set);
2251
2252 /*
2253 * Issue the init frame in polled mode
2254 */
2255
2256 if (megasas_issue_polled(instance, cmd)) {
2257 printk(KERN_ERR "megasas: Failed to init firmware\n");
2258 megasas_return_cmd(instance, cmd);
2259 goto fail_fw_init;
2260 }
2261
2262 megasas_return_cmd(instance, cmd);
2263
2264 return 0;
2265
2266fail_fw_init:
2267 return -EINVAL;
2268}
2269
ad84db2e 2270/**
2271 * megasas_start_timer - Initializes a timer object
2272 * @instance: Adapter soft state
2273 * @timer: timer object to be initialized
2274 * @fn: timer function
2275 * @interval: time interval between timer function call
2276 */
2277static inline void
2278megasas_start_timer(struct megasas_instance *instance,
2279 struct timer_list *timer,
2280 void *fn, unsigned long interval)
2281{
2282 init_timer(timer);
2283 timer->expires = jiffies + interval;
2284 timer->data = (unsigned long)instance;
2285 timer->function = fn;
2286 add_timer(timer);
2287}
2288
2289/**
2290 * megasas_io_completion_timer - Timer fn
2291 * @instance_addr: Address of adapter soft state
2292 *
2293 * Schedules tasklet for cmd completion
2294 * if poll_mode_io is set
2295 */
2296static void
2297megasas_io_completion_timer(unsigned long instance_addr)
2298{
2299 struct megasas_instance *instance =
2300 (struct megasas_instance *)instance_addr;
2301
2302 if (atomic_read(&instance->fw_outstanding))
2303 tasklet_schedule(&instance->isr_tasklet);
2304
2305 /* Restart timer */
2306 if (poll_mode_io)
2307 mod_timer(&instance->io_completion_timer,
2308 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2309}
2310
c4a3e0a5
BS
2311/**
2312 * megasas_init_mfi - Initializes the FW
2313 * @instance: Adapter soft state
2314 *
2315 * This is the main function for initializing MFI firmware.
2316 */
2317static int megasas_init_mfi(struct megasas_instance *instance)
2318{
2319 u32 context_sz;
2320 u32 reply_q_sz;
2321 u32 max_sectors_1;
2322 u32 max_sectors_2;
14faea9f 2323 u32 tmp_sectors;
c4a3e0a5 2324 struct megasas_register_set __iomem *reg_set;
c4a3e0a5 2325 struct megasas_ctrl_info *ctrl_info;
c4a3e0a5
BS
2326 /*
2327 * Map the message registers
2328 */
6610a6b3 2329 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
87911122
YB
2330 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2331 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6610a6b3
YB
2332 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2333 instance->base_addr = pci_resource_start(instance->pdev, 1);
2334 } else {
2335 instance->base_addr = pci_resource_start(instance->pdev, 0);
2336 }
c4a3e0a5 2337
f28cd7cf 2338 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
c4a3e0a5
BS
2339 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2340 return -EBUSY;
2341 }
2342
2343 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2344
2345 if (!instance->reg_set) {
2346 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2347 goto fail_ioremap;
2348 }
2349
2350 reg_set = instance->reg_set;
2351
f9876f0b
SP
2352 switch(instance->pdev->device)
2353 {
af7a5647 2354 case PCI_DEVICE_ID_LSI_SAS1078R:
2355 case PCI_DEVICE_ID_LSI_SAS1078DE:
f9876f0b
SP
2356 instance->instancet = &megasas_instance_template_ppc;
2357 break;
6610a6b3
YB
2358 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2359 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2360 instance->instancet = &megasas_instance_template_gen2;
2361 break;
87911122
YB
2362 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2363 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2364 instance->instancet = &megasas_instance_template_skinny;
2365 break;
f9876f0b
SP
2366 case PCI_DEVICE_ID_LSI_SAS1064R:
2367 case PCI_DEVICE_ID_DELL_PERC5:
2368 default:
2369 instance->instancet = &megasas_instance_template_xscale;
2370 break;
2371 }
1341c939 2372
c4a3e0a5
BS
2373 /*
2374 * We expect the FW state to be READY
2375 */
1341c939 2376 if (megasas_transition_to_ready(instance))
c4a3e0a5
BS
2377 goto fail_ready_state;
2378
2379 /*
2380 * Get various operational parameters from status register
2381 */
1341c939 2382 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
e3bbff9f
SP
2383 /*
2384 * Reduce the max supported cmds by 1. This is to ensure that the
2385 * reply_q_sz (1 more than the max cmd that driver may send)
2386 * does not exceed max cmds that the FW can support
2387 */
2388 instance->max_fw_cmds = instance->max_fw_cmds-1;
1341c939
SP
2389 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2390 0x10;
c4a3e0a5
BS
2391 /*
2392 * Create a pool of commands
2393 */
2394 if (megasas_alloc_cmds(instance))
2395 goto fail_alloc_cmds;
2396
2397 /*
2398 * Allocate memory for reply queue. Length of reply queue should
2399 * be _one_ more than the maximum commands handled by the firmware.
2400 *
2401 * Note: When FW completes commands, it places corresponding contex
2402 * values in this circular reply queue. This circular queue is a fairly
2403 * typical producer-consumer queue. FW is the producer (of completed
2404 * commands) and the driver is the consumer.
2405 */
2406 context_sz = sizeof(u32);
2407 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2408
2409 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2410 reply_q_sz,
2411 &instance->reply_queue_h);
2412
2413 if (!instance->reply_queue) {
2414 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2415 goto fail_reply_queue;
2416 }
2417
31ea7088 2418 if (megasas_issue_init_mfi(instance))
c4a3e0a5 2419 goto fail_fw_init;
c4a3e0a5 2420
81e403ce
YB
2421 memset(instance->pd_list, 0 ,
2422 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
2423 megasas_get_pd_list(instance);
2424
c4a3e0a5
BS
2425 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2426
2427 /*
2428 * Compute the max allowed sectors per IO: The controller info has two
2429 * limits on max sectors. Driver should use the minimum of these two.
2430 *
2431 * 1 << stripe_sz_ops.min = max sectors per strip
2432 *
2433 * Note that older firmwares ( < FW ver 30) didn't report information
2434 * to calculate max_sectors_1. So the number ended up as zero always.
2435 */
14faea9f 2436 tmp_sectors = 0;
c4a3e0a5
BS
2437 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2438
2439 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2440 ctrl_info->max_strips_per_io;
2441 max_sectors_2 = ctrl_info->max_request_size;
2442
14faea9f 2443 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2444 }
2445
2446 instance->max_sectors_per_req = instance->max_num_sge *
2447 PAGE_SIZE / 512;
2448 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2449 instance->max_sectors_per_req = tmp_sectors;
c4a3e0a5
BS
2450
2451 kfree(ctrl_info);
2452
5d018ad0
SP
2453 /*
2454 * Setup tasklet for cmd completion
2455 */
2456
ad84db2e 2457 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2458 (unsigned long)instance);
2459
2460 /* Initialize the cmd completion timer */
2461 if (poll_mode_io)
2462 megasas_start_timer(instance, &instance->io_completion_timer,
2463 megasas_io_completion_timer,
2464 MEGASAS_COMPLETION_TIMER_INTERVAL);
c4a3e0a5
BS
2465 return 0;
2466
2467 fail_fw_init:
c4a3e0a5
BS
2468
2469 pci_free_consistent(instance->pdev, reply_q_sz,
2470 instance->reply_queue, instance->reply_queue_h);
2471 fail_reply_queue:
2472 megasas_free_cmds(instance);
2473
2474 fail_alloc_cmds:
2475 fail_ready_state:
2476 iounmap(instance->reg_set);
2477
2478 fail_ioremap:
2479 pci_release_regions(instance->pdev);
2480
2481 return -EINVAL;
2482}
2483
2484/**
2485 * megasas_release_mfi - Reverses the FW initialization
2486 * @intance: Adapter soft state
2487 */
2488static void megasas_release_mfi(struct megasas_instance *instance)
2489{
2490 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2491
2492 pci_free_consistent(instance->pdev, reply_q_sz,
2493 instance->reply_queue, instance->reply_queue_h);
2494
2495 megasas_free_cmds(instance);
2496
2497 iounmap(instance->reg_set);
2498
2499 pci_release_regions(instance->pdev);
2500}
2501
2502/**
2503 * megasas_get_seq_num - Gets latest event sequence numbers
2504 * @instance: Adapter soft state
2505 * @eli: FW event log sequence numbers information
2506 *
2507 * FW maintains a log of all events in a non-volatile area. Upper layers would
2508 * usually find out the latest sequence number of the events, the seq number at
2509 * the boot etc. They would "read" all the events below the latest seq number
2510 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2511 * number), they would subsribe to AEN (asynchronous event notification) and
2512 * wait for the events to happen.
2513 */
2514static int
2515megasas_get_seq_num(struct megasas_instance *instance,
2516 struct megasas_evt_log_info *eli)
2517{
2518 struct megasas_cmd *cmd;
2519 struct megasas_dcmd_frame *dcmd;
2520 struct megasas_evt_log_info *el_info;
2521 dma_addr_t el_info_h = 0;
2522
2523 cmd = megasas_get_cmd(instance);
2524
2525 if (!cmd) {
2526 return -ENOMEM;
2527 }
2528
2529 dcmd = &cmd->frame->dcmd;
2530 el_info = pci_alloc_consistent(instance->pdev,
2531 sizeof(struct megasas_evt_log_info),
2532 &el_info_h);
2533
2534 if (!el_info) {
2535 megasas_return_cmd(instance, cmd);
2536 return -ENOMEM;
2537 }
2538
2539 memset(el_info, 0, sizeof(*el_info));
2540 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2541
2542 dcmd->cmd = MFI_CMD_DCMD;
2543 dcmd->cmd_status = 0x0;
2544 dcmd->sge_count = 1;
2545 dcmd->flags = MFI_FRAME_DIR_READ;
2546 dcmd->timeout = 0;
2547 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2548 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2549 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2550 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2551
2552 megasas_issue_blocked_cmd(instance, cmd);
2553
2554 /*
2555 * Copy the data back into callers buffer
2556 */
2557 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2558
2559 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2560 el_info, el_info_h);
2561
2562 megasas_return_cmd(instance, cmd);
2563
2564 return 0;
2565}
2566
2567/**
2568 * megasas_register_aen - Registers for asynchronous event notification
2569 * @instance: Adapter soft state
2570 * @seq_num: The starting sequence number
2571 * @class_locale: Class of the event
2572 *
2573 * This function subscribes for AEN for events beyond the @seq_num. It requests
2574 * to be notified if and only if the event is of type @class_locale
2575 */
2576static int
2577megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2578 u32 class_locale_word)
2579{
2580 int ret_val;
2581 struct megasas_cmd *cmd;
2582 struct megasas_dcmd_frame *dcmd;
2583 union megasas_evt_class_locale curr_aen;
2584 union megasas_evt_class_locale prev_aen;
2585
2586 /*
2587 * If there an AEN pending already (aen_cmd), check if the
2588 * class_locale of that pending AEN is inclusive of the new
2589 * AEN request we currently have. If it is, then we don't have
2590 * to do anything. In other words, whichever events the current
2591 * AEN request is subscribing to, have already been subscribed
2592 * to.
2593 *
2594 * If the old_cmd is _not_ inclusive, then we have to abort
2595 * that command, form a class_locale that is superset of both
2596 * old and current and re-issue to the FW
2597 */
2598
2599 curr_aen.word = class_locale_word;
2600
2601 if (instance->aen_cmd) {
2602
2603 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2604
2605 /*
2606 * A class whose enum value is smaller is inclusive of all
2607 * higher values. If a PROGRESS (= -1) was previously
2608 * registered, then a new registration requests for higher
2609 * classes need not be sent to FW. They are automatically
2610 * included.
2611 *
2612 * Locale numbers don't have such hierarchy. They are bitmap
2613 * values
2614 */
2615 if ((prev_aen.members.class <= curr_aen.members.class) &&
2616 !((prev_aen.members.locale & curr_aen.members.locale) ^
2617 curr_aen.members.locale)) {
2618 /*
2619 * Previously issued event registration includes
2620 * current request. Nothing to do.
2621 */
2622 return 0;
2623 } else {
2624 curr_aen.members.locale |= prev_aen.members.locale;
2625
2626 if (prev_aen.members.class < curr_aen.members.class)
2627 curr_aen.members.class = prev_aen.members.class;
2628
2629 instance->aen_cmd->abort_aen = 1;
2630 ret_val = megasas_issue_blocked_abort_cmd(instance,
2631 instance->
2632 aen_cmd);
2633
2634 if (ret_val) {
2635 printk(KERN_DEBUG "megasas: Failed to abort "
2636 "previous AEN command\n");
2637 return ret_val;
2638 }
2639 }
2640 }
2641
2642 cmd = megasas_get_cmd(instance);
2643
2644 if (!cmd)
2645 return -ENOMEM;
2646
2647 dcmd = &cmd->frame->dcmd;
2648
2649 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2650
2651 /*
2652 * Prepare DCMD for aen registration
2653 */
2654 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2655
2656 dcmd->cmd = MFI_CMD_DCMD;
2657 dcmd->cmd_status = 0x0;
2658 dcmd->sge_count = 1;
2659 dcmd->flags = MFI_FRAME_DIR_READ;
2660 dcmd->timeout = 0;
2661 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2662 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2663 dcmd->mbox.w[0] = seq_num;
2664 dcmd->mbox.w[1] = curr_aen.word;
2665 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2666 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2667
2668 /*
2669 * Store reference to the cmd used to register for AEN. When an
2670 * application wants us to register for AEN, we have to abort this
2671 * cmd and re-register with a new EVENT LOCALE supplied by that app
2672 */
2673 instance->aen_cmd = cmd;
2674
2675 /*
2676 * Issue the aen registration frame
2677 */
1341c939 2678 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
c4a3e0a5
BS
2679
2680 return 0;
2681}
2682
2683/**
2684 * megasas_start_aen - Subscribes to AEN during driver load time
2685 * @instance: Adapter soft state
2686 */
2687static int megasas_start_aen(struct megasas_instance *instance)
2688{
2689 struct megasas_evt_log_info eli;
2690 union megasas_evt_class_locale class_locale;
2691
2692 /*
2693 * Get the latest sequence number from FW
2694 */
2695 memset(&eli, 0, sizeof(eli));
2696
2697 if (megasas_get_seq_num(instance, &eli))
2698 return -1;
2699
2700 /*
2701 * Register AEN with FW for latest sequence number plus 1
2702 */
2703 class_locale.members.reserved = 0;
2704 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2705 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2706
2707 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2708 class_locale.word);
2709}
2710
2711/**
2712 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2713 * @instance: Adapter soft state
2714 */
2715static int megasas_io_attach(struct megasas_instance *instance)
2716{
2717 struct Scsi_Host *host = instance->host;
2718
2719 /*
2720 * Export parameters required by SCSI mid-layer
2721 */
2722 host->irq = instance->pdev->irq;
2723 host->unique_id = instance->unique_id;
2724 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2725 host->this_id = instance->init_id;
2726 host->sg_tablesize = instance->max_num_sge;
2727 host->max_sectors = instance->max_sectors_per_req;
2728 host->cmd_per_lun = 128;
2729 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2730 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2731 host->max_lun = MEGASAS_MAX_LUN;
122da302 2732 host->max_cmd_len = 16;
c4a3e0a5
BS
2733
2734 /*
2735 * Notify the mid-layer about the new controller
2736 */
2737 if (scsi_add_host(host, &instance->pdev->dev)) {
2738 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2739 return -ENODEV;
2740 }
2741
2742 /*
2743 * Trigger SCSI to scan our drives
2744 */
2745 scsi_scan_host(host);
2746 return 0;
2747}
2748
31ea7088 2749static int
2750megasas_set_dma_mask(struct pci_dev *pdev)
2751{
2752 /*
2753 * All our contollers are capable of performing 64-bit DMA
2754 */
2755 if (IS_DMA64) {
6a35528a 2756 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
31ea7088 2757
284901a9 2758 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 2759 goto fail_set_dma_mask;
2760 }
2761 } else {
284901a9 2762 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
31ea7088 2763 goto fail_set_dma_mask;
2764 }
2765 return 0;
2766
2767fail_set_dma_mask:
2768 return 1;
2769}
2770
c4a3e0a5
BS
2771/**
2772 * megasas_probe_one - PCI hotplug entry point
2773 * @pdev: PCI device structure
2774 * @id: PCI ids of supported hotplugged adapter
2775 */
2776static int __devinit
2777megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2778{
2779 int rval;
2780 struct Scsi_Host *host;
2781 struct megasas_instance *instance;
2782
2783 /*
2784 * Announce PCI information
2785 */
2786 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2787 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2788 pdev->subsystem_device);
2789
2790 printk("bus %d:slot %d:func %d\n",
2791 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2792
2793 /*
2794 * PCI prepping: enable device set bus mastering and dma mask
2795 */
2796 rval = pci_enable_device(pdev);
2797
2798 if (rval) {
2799 return rval;
2800 }
2801
2802 pci_set_master(pdev);
2803
31ea7088 2804 if (megasas_set_dma_mask(pdev))
2805 goto fail_set_dma_mask;
c4a3e0a5
BS
2806
2807 host = scsi_host_alloc(&megasas_template,
2808 sizeof(struct megasas_instance));
2809
2810 if (!host) {
2811 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2812 goto fail_alloc_instance;
2813 }
2814
2815 instance = (struct megasas_instance *)host->hostdata;
2816 memset(instance, 0, sizeof(*instance));
2817
2818 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2819 &instance->producer_h);
2820 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2821 &instance->consumer_h);
2822
2823 if (!instance->producer || !instance->consumer) {
2824 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2825 "producer, consumer\n");
2826 goto fail_alloc_dma_buf;
2827 }
2828
2829 *instance->producer = 0;
2830 *instance->consumer = 0;
c3518837 2831 megasas_poll_wait_aen = 0;
c4a3e0a5
BS
2832
2833 instance->evt_detail = pci_alloc_consistent(pdev,
2834 sizeof(struct
2835 megasas_evt_detail),
2836 &instance->evt_detail_h);
2837
2838 if (!instance->evt_detail) {
2839 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2840 "event detail structure\n");
2841 goto fail_alloc_dma_buf;
2842 }
2843
2844 /*
2845 * Initialize locks and queues
2846 */
2847 INIT_LIST_HEAD(&instance->cmd_pool);
2848
e4a082c7
SP
2849 atomic_set(&instance->fw_outstanding,0);
2850
c4a3e0a5
BS
2851 init_waitqueue_head(&instance->int_cmd_wait_q);
2852 init_waitqueue_head(&instance->abort_cmd_wait_q);
2853
2854 spin_lock_init(&instance->cmd_pool_lock);
7343eb65 2855 spin_lock_init(&instance->completion_lock);
c3518837 2856 spin_lock_init(&poll_aen_lock);
c4a3e0a5 2857
e5a69e27 2858 mutex_init(&instance->aen_mutex);
c4a3e0a5
BS
2859 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2860
2861 /*
2862 * Initialize PCI related and misc parameters
2863 */
2864 instance->pdev = pdev;
2865 instance->host = host;
2866 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2867 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2868
658dcedb 2869 megasas_dbg_lvl = 0;
05e9ebbe 2870 instance->flag = 0;
c3518837 2871 instance->unload = 0;
05e9ebbe 2872 instance->last_time = 0;
658dcedb 2873
c4a3e0a5
BS
2874 /*
2875 * Initialize MFI Firmware
2876 */
2877 if (megasas_init_mfi(instance))
2878 goto fail_init_mfi;
2879
2880 /*
2881 * Register IRQ
2882 */
1d6f359a 2883 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
c4a3e0a5
BS
2884 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2885 goto fail_irq;
2886 }
2887
1341c939 2888 instance->instancet->enable_intr(instance->reg_set);
c4a3e0a5
BS
2889
2890 /*
2891 * Store instance in PCI softstate
2892 */
2893 pci_set_drvdata(pdev, instance);
2894
2895 /*
2896 * Add this controller to megasas_mgmt_info structure so that it
2897 * can be exported to management applications
2898 */
2899 megasas_mgmt_info.count++;
2900 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2901 megasas_mgmt_info.max_index++;
2902
2903 /*
2904 * Initiate AEN (Asynchronous Event Notification)
2905 */
2906 if (megasas_start_aen(instance)) {
2907 printk(KERN_DEBUG "megasas: start aen failed\n");
2908 goto fail_start_aen;
2909 }
2910
2911 /*
2912 * Register with SCSI mid-layer
2913 */
2914 if (megasas_io_attach(instance))
2915 goto fail_io_attach;
2916
2917 return 0;
2918
2919 fail_start_aen:
2920 fail_io_attach:
2921 megasas_mgmt_info.count--;
2922 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2923 megasas_mgmt_info.max_index--;
2924
2925 pci_set_drvdata(pdev, NULL);
b274cab7 2926 instance->instancet->disable_intr(instance->reg_set);
c4a3e0a5
BS
2927 free_irq(instance->pdev->irq, instance);
2928
2929 megasas_release_mfi(instance);
2930
2931 fail_irq:
2932 fail_init_mfi:
2933 fail_alloc_dma_buf:
2934 if (instance->evt_detail)
2935 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2936 instance->evt_detail,
2937 instance->evt_detail_h);
2938
2939 if (instance->producer)
2940 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2941 instance->producer_h);
2942 if (instance->consumer)
2943 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2944 instance->consumer_h);
2945 scsi_host_put(host);
2946
2947 fail_alloc_instance:
2948 fail_set_dma_mask:
2949 pci_disable_device(pdev);
2950
2951 return -ENODEV;
2952}
2953
2954/**
2955 * megasas_flush_cache - Requests FW to flush all its caches
2956 * @instance: Adapter soft state
2957 */
2958static void megasas_flush_cache(struct megasas_instance *instance)
2959{
2960 struct megasas_cmd *cmd;
2961 struct megasas_dcmd_frame *dcmd;
2962
2963 cmd = megasas_get_cmd(instance);
2964
2965 if (!cmd)
2966 return;
2967
2968 dcmd = &cmd->frame->dcmd;
2969
2970 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2971
2972 dcmd->cmd = MFI_CMD_DCMD;
2973 dcmd->cmd_status = 0x0;
2974 dcmd->sge_count = 0;
2975 dcmd->flags = MFI_FRAME_DIR_NONE;
2976 dcmd->timeout = 0;
2977 dcmd->data_xfer_len = 0;
2978 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2979 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2980
2981 megasas_issue_blocked_cmd(instance, cmd);
2982
2983 megasas_return_cmd(instance, cmd);
2984
2985 return;
2986}
2987
2988/**
2989 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2990 * @instance: Adapter soft state
31ea7088 2991 * @opcode: Shutdown/Hibernate
c4a3e0a5 2992 */
31ea7088 2993static void megasas_shutdown_controller(struct megasas_instance *instance,
2994 u32 opcode)
c4a3e0a5
BS
2995{
2996 struct megasas_cmd *cmd;
2997 struct megasas_dcmd_frame *dcmd;
2998
2999 cmd = megasas_get_cmd(instance);
3000
3001 if (!cmd)
3002 return;
3003
3004 if (instance->aen_cmd)
3005 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3006
3007 dcmd = &cmd->frame->dcmd;
3008
3009 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3010
3011 dcmd->cmd = MFI_CMD_DCMD;
3012 dcmd->cmd_status = 0x0;
3013 dcmd->sge_count = 0;
3014 dcmd->flags = MFI_FRAME_DIR_NONE;
3015 dcmd->timeout = 0;
3016 dcmd->data_xfer_len = 0;
31ea7088 3017 dcmd->opcode = opcode;
c4a3e0a5
BS
3018
3019 megasas_issue_blocked_cmd(instance, cmd);
3020
3021 megasas_return_cmd(instance, cmd);
3022
3023 return;
3024}
3025
33139b21 3026#ifdef CONFIG_PM
31ea7088 3027/**
ad84db2e 3028 * megasas_suspend - driver suspend entry point
3029 * @pdev: PCI device structure
31ea7088 3030 * @state: PCI power state to suspend routine
3031 */
33139b21 3032static int
31ea7088 3033megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3034{
3035 struct Scsi_Host *host;
3036 struct megasas_instance *instance;
3037
3038 instance = pci_get_drvdata(pdev);
3039 host = instance->host;
3040
ad84db2e 3041 if (poll_mode_io)
3042 del_timer_sync(&instance->io_completion_timer);
3043
31ea7088 3044 megasas_flush_cache(instance);
3045 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
3046 tasklet_kill(&instance->isr_tasklet);
3047
3048 pci_set_drvdata(instance->pdev, instance);
3049 instance->instancet->disable_intr(instance->reg_set);
3050 free_irq(instance->pdev->irq, instance);
3051
3052 pci_save_state(pdev);
3053 pci_disable_device(pdev);
3054
3055 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3056
3057 return 0;
3058}
3059
3060/**
3061 * megasas_resume- driver resume entry point
3062 * @pdev: PCI device structure
3063 */
33139b21 3064static int
31ea7088 3065megasas_resume(struct pci_dev *pdev)
3066{
3067 int rval;
3068 struct Scsi_Host *host;
3069 struct megasas_instance *instance;
3070
3071 instance = pci_get_drvdata(pdev);
3072 host = instance->host;
3073 pci_set_power_state(pdev, PCI_D0);
3074 pci_enable_wake(pdev, PCI_D0, 0);
3075 pci_restore_state(pdev);
3076
3077 /*
3078 * PCI prepping: enable device set bus mastering and dma mask
3079 */
3080 rval = pci_enable_device(pdev);
3081
3082 if (rval) {
3083 printk(KERN_ERR "megasas: Enable device failed\n");
3084 return rval;
3085 }
3086
3087 pci_set_master(pdev);
3088
3089 if (megasas_set_dma_mask(pdev))
3090 goto fail_set_dma_mask;
3091
3092 /*
3093 * Initialize MFI Firmware
3094 */
3095
3096 *instance->producer = 0;
3097 *instance->consumer = 0;
3098
3099 atomic_set(&instance->fw_outstanding, 0);
3100
3101 /*
3102 * We expect the FW state to be READY
3103 */
3104 if (megasas_transition_to_ready(instance))
3105 goto fail_ready_state;
3106
3107 if (megasas_issue_init_mfi(instance))
3108 goto fail_init_mfi;
3109
3110 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3111 (unsigned long)instance);
3112
3113 /*
3114 * Register IRQ
3115 */
3116 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3117 "megasas", instance)) {
3118 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3119 goto fail_irq;
3120 }
3121
3122 instance->instancet->enable_intr(instance->reg_set);
3123
3124 /*
3125 * Initiate AEN (Asynchronous Event Notification)
3126 */
3127 if (megasas_start_aen(instance))
3128 printk(KERN_ERR "megasas: Start AEN failed\n");
3129
ad84db2e 3130 /* Initialize the cmd completion timer */
3131 if (poll_mode_io)
3132 megasas_start_timer(instance, &instance->io_completion_timer,
3133 megasas_io_completion_timer,
3134 MEGASAS_COMPLETION_TIMER_INTERVAL);
31ea7088 3135 return 0;
3136
3137fail_irq:
3138fail_init_mfi:
3139 if (instance->evt_detail)
3140 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3141 instance->evt_detail,
3142 instance->evt_detail_h);
3143
3144 if (instance->producer)
3145 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3146 instance->producer_h);
3147 if (instance->consumer)
3148 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3149 instance->consumer_h);
3150 scsi_host_put(host);
3151
3152fail_set_dma_mask:
3153fail_ready_state:
3154
3155 pci_disable_device(pdev);
3156
3157 return -ENODEV;
3158}
33139b21
JS
3159#else
3160#define megasas_suspend NULL
3161#define megasas_resume NULL
3162#endif
31ea7088 3163
c4a3e0a5
BS
3164/**
3165 * megasas_detach_one - PCI hot"un"plug entry point
3166 * @pdev: PCI device structure
3167 */
33139b21 3168static void __devexit megasas_detach_one(struct pci_dev *pdev)
c4a3e0a5
BS
3169{
3170 int i;
3171 struct Scsi_Host *host;
3172 struct megasas_instance *instance;
3173
3174 instance = pci_get_drvdata(pdev);
c3518837 3175 instance->unload = 1;
c4a3e0a5
BS
3176 host = instance->host;
3177
ad84db2e 3178 if (poll_mode_io)
3179 del_timer_sync(&instance->io_completion_timer);
3180
c4a3e0a5
BS
3181 scsi_remove_host(instance->host);
3182 megasas_flush_cache(instance);
31ea7088 3183 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5d018ad0 3184 tasklet_kill(&instance->isr_tasklet);
c4a3e0a5
BS
3185
3186 /*
3187 * Take the instance off the instance array. Note that we will not
3188 * decrement the max_index. We let this array be sparse array
3189 */
3190 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3191 if (megasas_mgmt_info.instance[i] == instance) {
3192 megasas_mgmt_info.count--;
3193 megasas_mgmt_info.instance[i] = NULL;
3194
3195 break;
3196 }
3197 }
3198
3199 pci_set_drvdata(instance->pdev, NULL);
3200
b274cab7 3201 instance->instancet->disable_intr(instance->reg_set);
c4a3e0a5
BS
3202
3203 free_irq(instance->pdev->irq, instance);
3204
3205 megasas_release_mfi(instance);
3206
3207 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3208 instance->evt_detail, instance->evt_detail_h);
3209
3210 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3211 instance->producer_h);
3212
3213 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3214 instance->consumer_h);
3215
3216 scsi_host_put(host);
3217
3218 pci_set_drvdata(pdev, NULL);
3219
3220 pci_disable_device(pdev);
3221
3222 return;
3223}
3224
3225/**
3226 * megasas_shutdown - Shutdown entry point
3227 * @device: Generic device structure
3228 */
3229static void megasas_shutdown(struct pci_dev *pdev)
3230{
3231 struct megasas_instance *instance = pci_get_drvdata(pdev);
3232 megasas_flush_cache(instance);
530e6fc1 3233 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
c4a3e0a5
BS
3234}
3235
3236/**
3237 * megasas_mgmt_open - char node "open" entry point
3238 */
3239static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3240{
f2b9857e 3241 cycle_kernel_lock();
c4a3e0a5
BS
3242 /*
3243 * Allow only those users with admin rights
3244 */
3245 if (!capable(CAP_SYS_ADMIN))
3246 return -EACCES;
3247
3248 return 0;
3249}
3250
c4a3e0a5
BS
3251/**
3252 * megasas_mgmt_fasync - Async notifier registration from applications
3253 *
3254 * This function adds the calling process to a driver global queue. When an
3255 * event occurs, SIGIO will be sent to all processes in this queue.
3256 */
3257static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3258{
3259 int rc;
3260
0b950672 3261 mutex_lock(&megasas_async_queue_mutex);
c4a3e0a5
BS
3262
3263 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3264
0b950672 3265 mutex_unlock(&megasas_async_queue_mutex);
c4a3e0a5
BS
3266
3267 if (rc >= 0) {
3268 /* For sanity check when we get ioctl */
3269 filep->private_data = filep;
3270 return 0;
3271 }
3272
3273 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3274
3275 return rc;
3276}
3277
c3518837
YB
3278/**
3279 * megasas_mgmt_poll - char node "poll" entry point
3280 * */
3281static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3282{
3283 unsigned int mask;
3284 unsigned long flags;
3285 poll_wait(file, &megasas_poll_wait, wait);
3286 spin_lock_irqsave(&poll_aen_lock, flags);
3287 if (megasas_poll_wait_aen)
3288 mask = (POLLIN | POLLRDNORM);
3289 else
3290 mask = 0;
3291 spin_unlock_irqrestore(&poll_aen_lock, flags);
3292 return mask;
3293}
3294
c4a3e0a5
BS
3295/**
3296 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3297 * @instance: Adapter soft state
3298 * @argp: User's ioctl packet
3299 */
3300static int
3301megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3302 struct megasas_iocpacket __user * user_ioc,
3303 struct megasas_iocpacket *ioc)
3304{
3305 struct megasas_sge32 *kern_sge32;
3306 struct megasas_cmd *cmd;
3307 void *kbuff_arr[MAX_IOCTL_SGE];
3308 dma_addr_t buf_handle = 0;
3309 int error = 0, i;
3310 void *sense = NULL;
3311 dma_addr_t sense_handle;
3312 u32 *sense_ptr;
3313
3314 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3315
3316 if (ioc->sge_count > MAX_IOCTL_SGE) {
3317 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3318 ioc->sge_count, MAX_IOCTL_SGE);
3319 return -EINVAL;
3320 }
3321
3322 cmd = megasas_get_cmd(instance);
3323 if (!cmd) {
3324 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3325 return -ENOMEM;
3326 }
3327
3328 /*
3329 * User's IOCTL packet has 2 frames (maximum). Copy those two
3330 * frames into our cmd's frames. cmd->frame's context will get
3331 * overwritten when we copy from user's frames. So set that value
3332 * alone separately
3333 */
3334 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3335 cmd->frame->hdr.context = cmd->index;
c3518837 3336 cmd->frame->hdr.pad_0 = 0;
c4a3e0a5
BS
3337
3338 /*
3339 * The management interface between applications and the fw uses
3340 * MFI frames. E.g, RAID configuration changes, LD property changes
3341 * etc are accomplishes through different kinds of MFI frames. The
3342 * driver needs to care only about substituting user buffers with
3343 * kernel buffers in SGLs. The location of SGL is embedded in the
3344 * struct iocpacket itself.
3345 */
3346 kern_sge32 = (struct megasas_sge32 *)
3347 ((unsigned long)cmd->frame + ioc->sgl_off);
3348
3349 /*
3350 * For each user buffer, create a mirror buffer and copy in
3351 */
3352 for (i = 0; i < ioc->sge_count; i++) {
9f35fa8a 3353 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
c4a3e0a5 3354 ioc->sgl[i].iov_len,
9f35fa8a 3355 &buf_handle, GFP_KERNEL);
c4a3e0a5
BS
3356 if (!kbuff_arr[i]) {
3357 printk(KERN_DEBUG "megasas: Failed to alloc "
3358 "kernel SGL buffer for IOCTL \n");
3359 error = -ENOMEM;
3360 goto out;
3361 }
3362
3363 /*
3364 * We don't change the dma_coherent_mask, so
3365 * pci_alloc_consistent only returns 32bit addresses
3366 */
3367 kern_sge32[i].phys_addr = (u32) buf_handle;
3368 kern_sge32[i].length = ioc->sgl[i].iov_len;
3369
3370 /*
3371 * We created a kernel buffer corresponding to the
3372 * user buffer. Now copy in from the user buffer
3373 */
3374 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3375 (u32) (ioc->sgl[i].iov_len))) {
3376 error = -EFAULT;
3377 goto out;
3378 }
3379 }
3380
3381 if (ioc->sense_len) {
9f35fa8a
SP
3382 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3383 &sense_handle, GFP_KERNEL);
c4a3e0a5
BS
3384 if (!sense) {
3385 error = -ENOMEM;
3386 goto out;
3387 }
3388
3389 sense_ptr =
3390 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3391 *sense_ptr = sense_handle;
3392 }
3393
3394 /*
3395 * Set the sync_cmd flag so that the ISR knows not to complete this
3396 * cmd to the SCSI mid-layer
3397 */
3398 cmd->sync_cmd = 1;
3399 megasas_issue_blocked_cmd(instance, cmd);
3400 cmd->sync_cmd = 0;
3401
3402 /*
3403 * copy out the kernel buffers to user buffers
3404 */
3405 for (i = 0; i < ioc->sge_count; i++) {
3406 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3407 ioc->sgl[i].iov_len)) {
3408 error = -EFAULT;
3409 goto out;
3410 }
3411 }
3412
3413 /*
3414 * copy out the sense
3415 */
3416 if (ioc->sense_len) {
3417 /*
b70a41e0 3418 * sense_ptr points to the location that has the user
c4a3e0a5
BS
3419 * sense buffer address
3420 */
b70a41e0 3421 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3422 ioc->sense_off);
c4a3e0a5 3423
b70a41e0 3424 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3425 sense, ioc->sense_len)) {
b10c36a5 3426 printk(KERN_ERR "megasas: Failed to copy out to user "
3427 "sense data\n");
c4a3e0a5
BS
3428 error = -EFAULT;
3429 goto out;
3430 }
3431 }
3432
3433 /*
3434 * copy the status codes returned by the fw
3435 */
3436 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3437 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3438 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3439 error = -EFAULT;
3440 }
3441
3442 out:
3443 if (sense) {
9f35fa8a 3444 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
c4a3e0a5
BS
3445 sense, sense_handle);
3446 }
3447
3448 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
9f35fa8a 3449 dma_free_coherent(&instance->pdev->dev,
c4a3e0a5
BS
3450 kern_sge32[i].length,
3451 kbuff_arr[i], kern_sge32[i].phys_addr);
3452 }
3453
3454 megasas_return_cmd(instance, cmd);
3455 return error;
3456}
3457
3458static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3459{
3460 int i;
3461
3462 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3463
3464 if ((megasas_mgmt_info.instance[i]) &&
3465 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3466 return megasas_mgmt_info.instance[i];
3467 }
3468
3469 return NULL;
3470}
3471
3472static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3473{
3474 struct megasas_iocpacket __user *user_ioc =
3475 (struct megasas_iocpacket __user *)arg;
3476 struct megasas_iocpacket *ioc;
3477 struct megasas_instance *instance;
3478 int error;
3479
3480 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3481 if (!ioc)
3482 return -ENOMEM;
3483
3484 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3485 error = -EFAULT;
3486 goto out_kfree_ioc;
3487 }
3488
3489 instance = megasas_lookup_instance(ioc->host_no);
3490 if (!instance) {
3491 error = -ENODEV;
3492 goto out_kfree_ioc;
3493 }
3494
3495 /*
3496 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3497 */
3498 if (down_interruptible(&instance->ioctl_sem)) {
3499 error = -ERESTARTSYS;
3500 goto out_kfree_ioc;
3501 }
3502 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3503 up(&instance->ioctl_sem);
3504
3505 out_kfree_ioc:
3506 kfree(ioc);
3507 return error;
3508}
3509
3510static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3511{
3512 struct megasas_instance *instance;
3513 struct megasas_aen aen;
3514 int error;
3515
3516 if (file->private_data != file) {
3517 printk(KERN_DEBUG "megasas: fasync_helper was not "
3518 "called first\n");
3519 return -EINVAL;
3520 }
3521
3522 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3523 return -EFAULT;
3524
3525 instance = megasas_lookup_instance(aen.host_no);
3526
3527 if (!instance)
3528 return -ENODEV;
3529
e5a69e27 3530 mutex_lock(&instance->aen_mutex);
c4a3e0a5
BS
3531 error = megasas_register_aen(instance, aen.seq_num,
3532 aen.class_locale_word);
e5a69e27 3533 mutex_unlock(&instance->aen_mutex);
c4a3e0a5
BS
3534 return error;
3535}
3536
3537/**
3538 * megasas_mgmt_ioctl - char node ioctl entry point
3539 */
3540static long
3541megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3542{
3543 switch (cmd) {
3544 case MEGASAS_IOC_FIRMWARE:
3545 return megasas_mgmt_ioctl_fw(file, arg);
3546
3547 case MEGASAS_IOC_GET_AEN:
3548 return megasas_mgmt_ioctl_aen(file, arg);
3549 }
3550
3551 return -ENOTTY;
3552}
3553
3554#ifdef CONFIG_COMPAT
3555static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3556{
3557 struct compat_megasas_iocpacket __user *cioc =
3558 (struct compat_megasas_iocpacket __user *)arg;
3559 struct megasas_iocpacket __user *ioc =
3560 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3561 int i;
3562 int error = 0;
3563
83aabc1b
JG
3564 if (clear_user(ioc, sizeof(*ioc)))
3565 return -EFAULT;
c4a3e0a5
BS
3566
3567 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3568 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3569 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3570 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3571 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3572 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3573 return -EFAULT;
3574
3575 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3576 compat_uptr_t ptr;
3577
3578 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3579 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3580 copy_in_user(&ioc->sgl[i].iov_len,
3581 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3582 return -EFAULT;
3583 }
3584
3585 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3586
3587 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3588 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3589 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3590 return -EFAULT;
3591 }
3592 return error;
3593}
3594
3595static long
3596megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3597 unsigned long arg)
3598{
3599 switch (cmd) {
cb59aa6a
SP
3600 case MEGASAS_IOC_FIRMWARE32:
3601 return megasas_mgmt_compat_ioctl_fw(file, arg);
c4a3e0a5
BS
3602 case MEGASAS_IOC_GET_AEN:
3603 return megasas_mgmt_ioctl_aen(file, arg);
3604 }
3605
3606 return -ENOTTY;
3607}
3608#endif
3609
3610/*
3611 * File operations structure for management interface
3612 */
00977a59 3613static const struct file_operations megasas_mgmt_fops = {
c4a3e0a5
BS
3614 .owner = THIS_MODULE,
3615 .open = megasas_mgmt_open,
c4a3e0a5
BS
3616 .fasync = megasas_mgmt_fasync,
3617 .unlocked_ioctl = megasas_mgmt_ioctl,
c3518837 3618 .poll = megasas_mgmt_poll,
c4a3e0a5
BS
3619#ifdef CONFIG_COMPAT
3620 .compat_ioctl = megasas_mgmt_compat_ioctl,
3621#endif
3622};
3623
3624/*
3625 * PCI hotplug support registration structure
3626 */
3627static struct pci_driver megasas_pci_driver = {
3628
3629 .name = "megaraid_sas",
3630 .id_table = megasas_pci_table,
3631 .probe = megasas_probe_one,
3632 .remove = __devexit_p(megasas_detach_one),
31ea7088 3633 .suspend = megasas_suspend,
3634 .resume = megasas_resume,
c4a3e0a5
BS
3635 .shutdown = megasas_shutdown,
3636};
3637
3638/*
3639 * Sysfs driver attributes
3640 */
3641static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3642{
3643 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3644 MEGASAS_VERSION);
3645}
3646
3647static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3648
3649static ssize_t
3650megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3651{
3652 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3653 MEGASAS_RELDATE);
3654}
3655
3656static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3657 NULL);
3658
72c4fd36
YB
3659static ssize_t
3660megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
3661{
3662 return sprintf(buf, "%u\n", support_poll_for_event);
3663}
3664
3665static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
3666 megasas_sysfs_show_support_poll_for_event, NULL);
3667
658dcedb
SP
3668static ssize_t
3669megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3670{
ad84db2e 3671 return sprintf(buf, "%u\n", megasas_dbg_lvl);
658dcedb
SP
3672}
3673
3674static ssize_t
3675megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3676{
3677 int retval = count;
3678 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3679 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3680 retval = -EINVAL;
3681 }
3682 return retval;
3683}
3684
66dca9b8 3685static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
ad84db2e 3686 megasas_sysfs_set_dbg_lvl);
3687
3688static ssize_t
3689megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3690{
3691 return sprintf(buf, "%u\n", poll_mode_io);
3692}
3693
3694static ssize_t
3695megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3696 const char *buf, size_t count)
3697{
3698 int retval = count;
3699 int tmp = poll_mode_io;
3700 int i;
3701 struct megasas_instance *instance;
3702
3703 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3704 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3705 retval = -EINVAL;
3706 }
3707
3708 /*
3709 * Check if poll_mode_io is already set or is same as previous value
3710 */
3711 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3712 goto out;
3713
3714 if (poll_mode_io) {
3715 /*
3716 * Start timers for all adapters
3717 */
3718 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3719 instance = megasas_mgmt_info.instance[i];
3720 if (instance) {
3721 megasas_start_timer(instance,
3722 &instance->io_completion_timer,
3723 megasas_io_completion_timer,
3724 MEGASAS_COMPLETION_TIMER_INTERVAL);
3725 }
3726 }
3727 } else {
3728 /*
3729 * Delete timers for all adapters
3730 */
3731 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3732 instance = megasas_mgmt_info.instance[i];
3733 if (instance)
3734 del_timer_sync(&instance->io_completion_timer);
3735 }
3736 }
3737
3738out:
3739 return retval;
3740}
3741
3742static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3743 megasas_sysfs_show_poll_mode_io,
3744 megasas_sysfs_set_poll_mode_io);
658dcedb 3745
c4a3e0a5
BS
3746/**
3747 * megasas_init - Driver load entry point
3748 */
3749static int __init megasas_init(void)
3750{
3751 int rval;
3752
3753 /*
3754 * Announce driver version and other information
3755 */
3756 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3757 MEGASAS_EXT_VERSION);
3758
72c4fd36
YB
3759 support_poll_for_event = 2;
3760
c4a3e0a5
BS
3761 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3762
3763 /*
3764 * Register character device node
3765 */
3766 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3767
3768 if (rval < 0) {
3769 printk(KERN_DEBUG "megasas: failed to open device node\n");
3770 return rval;
3771 }
3772
3773 megasas_mgmt_majorno = rval;
3774
3775 /*
3776 * Register ourselves as PCI hotplug module
3777 */
4041b9cd 3778 rval = pci_register_driver(&megasas_pci_driver);
c4a3e0a5
BS
3779
3780 if (rval) {
3781 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
83aabc1b
JG
3782 goto err_pcidrv;
3783 }
3784
3785 rval = driver_create_file(&megasas_pci_driver.driver,
3786 &driver_attr_version);
3787 if (rval)
3788 goto err_dcf_attr_ver;
3789 rval = driver_create_file(&megasas_pci_driver.driver,
3790 &driver_attr_release_date);
3791 if (rval)
3792 goto err_dcf_rel_date;
72c4fd36
YB
3793
3794 rval = driver_create_file(&megasas_pci_driver.driver,
3795 &driver_attr_support_poll_for_event);
3796 if (rval)
3797 goto err_dcf_support_poll_for_event;
3798
83aabc1b
JG
3799 rval = driver_create_file(&megasas_pci_driver.driver,
3800 &driver_attr_dbg_lvl);
3801 if (rval)
3802 goto err_dcf_dbg_lvl;
ad84db2e 3803 rval = driver_create_file(&megasas_pci_driver.driver,
3804 &driver_attr_poll_mode_io);
3805 if (rval)
3806 goto err_dcf_poll_mode_io;
c4a3e0a5
BS
3807
3808 return rval;
ad84db2e 3809
3810err_dcf_poll_mode_io:
3811 driver_remove_file(&megasas_pci_driver.driver,
3812 &driver_attr_dbg_lvl);
83aabc1b 3813err_dcf_dbg_lvl:
72c4fd36
YB
3814 driver_remove_file(&megasas_pci_driver.driver,
3815 &driver_attr_support_poll_for_event);
3816
3817err_dcf_support_poll_for_event:
83aabc1b
JG
3818 driver_remove_file(&megasas_pci_driver.driver,
3819 &driver_attr_release_date);
72c4fd36 3820
83aabc1b
JG
3821err_dcf_rel_date:
3822 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3823err_dcf_attr_ver:
3824 pci_unregister_driver(&megasas_pci_driver);
3825err_pcidrv:
3826 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3827 return rval;
c4a3e0a5
BS
3828}
3829
3830/**
3831 * megasas_exit - Driver unload entry point
3832 */
3833static void __exit megasas_exit(void)
3834{
ad84db2e 3835 driver_remove_file(&megasas_pci_driver.driver,
3836 &driver_attr_poll_mode_io);
658dcedb
SP
3837 driver_remove_file(&megasas_pci_driver.driver,
3838 &driver_attr_dbg_lvl);
83aabc1b
JG
3839 driver_remove_file(&megasas_pci_driver.driver,
3840 &driver_attr_release_date);
3841 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
c4a3e0a5
BS
3842
3843 pci_unregister_driver(&megasas_pci_driver);
3844 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3845}
3846
3847module_init(megasas_init);
3848module_exit(megasas_exit);