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