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