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