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