2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Abstract: Contains all routines for control of the AFA comm layer
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/blkdev.h>
41 #include <asm/semaphore.h>
42 #include <asm/uaccess.h>
47 * ioctl_send_fib - send a FIB from userspace
48 * @dev: adapter is being processed
49 * @arg: arguments to the ioctl call
51 * This routine sends a fib to the adapter on behalf of a user level
54 # define AAC_DEBUG_PREAMBLE KERN_INFO
55 # define AAC_DEBUG_POSTAMBLE
57 static int ioctl_send_fib(struct aac_dev
* dev
, void __user
*arg
)
61 struct hw_fib
* hw_fib
= (struct hw_fib
*)0;
62 dma_addr_t hw_fib_pa
= (dma_addr_t
)0LL;
66 fibptr
= aac_fib_alloc(dev
);
71 kfib
= fibptr
->hw_fib
;
73 * First copy in the header so that we can check the size field.
75 if (copy_from_user((void *)kfib
, arg
, sizeof(struct aac_fibhdr
))) {
80 * Since we copy based on the fib header size, make sure that we
81 * will not overrun the buffer when we copy the memory. Return
82 * an error if we would.
84 size
= le16_to_cpu(kfib
->header
.Size
) + sizeof(struct aac_fibhdr
);
85 if (size
< le16_to_cpu(kfib
->header
.SenderSize
))
86 size
= le16_to_cpu(kfib
->header
.SenderSize
);
87 if (size
> dev
->max_fib_size
) {
92 /* Highjack the hw_fib */
93 hw_fib
= fibptr
->hw_fib
;
94 hw_fib_pa
= fibptr
->hw_fib_pa
;
95 fibptr
->hw_fib
= kfib
= pci_alloc_consistent(dev
->pdev
, size
, &fibptr
->hw_fib_pa
);
96 memset(((char *)kfib
) + dev
->max_fib_size
, 0, size
- dev
->max_fib_size
);
97 memcpy(kfib
, hw_fib
, dev
->max_fib_size
);
100 if (copy_from_user(kfib
, arg
, size
)) {
105 if (kfib
->header
.Command
== cpu_to_le16(TakeABreakPt
)) {
106 aac_adapter_interrupt(dev
);
108 * Since we didn't really send a fib, zero out the state to allow
109 * cleanup code not to assert.
111 kfib
->header
.XferState
= 0;
113 retval
= aac_fib_send(le16_to_cpu(kfib
->header
.Command
), fibptr
,
114 le16_to_cpu(kfib
->header
.Size
) , FsaNormal
,
119 if (aac_fib_complete(fibptr
) != 0) {
125 * Make sure that the size returned by the adapter (which includes
126 * the header) is less than or equal to the size of a fib, so we
127 * don't corrupt application data. Then copy that size to the user
128 * buffer. (Don't try to add the header information again, since it
129 * was already included by the adapter.)
133 if (copy_to_user(arg
, (void *)kfib
, size
))
137 pci_free_consistent(dev
->pdev
, size
, kfib
, fibptr
->hw_fib_pa
);
138 fibptr
->hw_fib_pa
= hw_fib_pa
;
139 fibptr
->hw_fib
= hw_fib
;
141 aac_fib_free(fibptr
);
146 * open_getadapter_fib - Get the next fib
148 * This routine will get the next Fib, if available, from the AdapterFibContext
149 * passed in from the user.
152 static int open_getadapter_fib(struct aac_dev
* dev
, void __user
*arg
)
154 struct aac_fib_context
* fibctx
;
157 fibctx
= kmalloc(sizeof(struct aac_fib_context
), GFP_KERNEL
);
158 if (fibctx
== NULL
) {
162 struct list_head
* entry
;
163 struct aac_fib_context
* context
;
165 fibctx
->type
= FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT
;
166 fibctx
->size
= sizeof(struct aac_fib_context
);
168 * Yes yes, I know this could be an index, but we have a
169 * better guarantee of uniqueness for the locked loop below.
170 * Without the aid of a persistent history, this also helps
171 * reduce the chance that the opaque context would be reused.
173 fibctx
->unique
= (u32
)((ulong
)fibctx
& 0xFFFFFFFF);
175 * Initialize the mutex used to wait for the next AIF.
177 init_MUTEX_LOCKED(&fibctx
->wait_sem
);
180 * Initialize the fibs and set the count of fibs on
184 INIT_LIST_HEAD(&fibctx
->fib_list
);
185 fibctx
->jiffies
= jiffies
/HZ
;
187 * Now add this context onto the adapter's
188 * AdapterFibContext list.
190 spin_lock_irqsave(&dev
->fib_lock
, flags
);
191 /* Ensure that we have a unique identifier */
192 entry
= dev
->fib_list
.next
;
193 while (entry
!= &dev
->fib_list
) {
194 context
= list_entry(entry
, struct aac_fib_context
, next
);
195 if (context
->unique
== fibctx
->unique
) {
196 /* Not unique (32 bits) */
198 entry
= dev
->fib_list
.next
;
203 list_add_tail(&fibctx
->next
, &dev
->fib_list
);
204 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
205 if (copy_to_user(arg
, &fibctx
->unique
,
206 sizeof(fibctx
->unique
))) {
216 * next_getadapter_fib - get the next fib
217 * @dev: adapter to use
218 * @arg: ioctl argument
220 * This routine will get the next Fib, if available, from the AdapterFibContext
221 * passed in from the user.
224 static int next_getadapter_fib(struct aac_dev
* dev
, void __user
*arg
)
228 struct aac_fib_context
*fibctx
;
230 struct list_head
* entry
;
233 if(copy_from_user((void *)&f
, arg
, sizeof(struct fib_ioctl
)))
236 * Verify that the HANDLE passed in was a valid AdapterFibContext
238 * Search the list of AdapterFibContext addresses on the adapter
239 * to be sure this is a valid address
241 entry
= dev
->fib_list
.next
;
244 while (entry
!= &dev
->fib_list
) {
245 fibctx
= list_entry(entry
, struct aac_fib_context
, next
);
247 * Extract the AdapterFibContext from the Input parameters.
249 if (fibctx
->unique
== f
.fibctx
) { /* We found a winner */
256 dprintk ((KERN_INFO
"Fib Context not found\n"));
260 if((fibctx
->type
!= FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT
) ||
261 (fibctx
->size
!= sizeof(struct aac_fib_context
))) {
262 dprintk ((KERN_INFO
"Fib Context corrupt?\n"));
266 spin_lock_irqsave(&dev
->fib_lock
, flags
);
268 * If there are no fibs to send back, then either wait or return
272 if (!list_empty(&fibctx
->fib_list
)) {
273 struct list_head
* entry
;
275 * Pull the next fib from the fibs
277 entry
= fibctx
->fib_list
.next
;
280 fib
= list_entry(entry
, struct fib
, fiblink
);
282 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
283 if (copy_to_user(f
.fib
, fib
->hw_fib
, sizeof(struct hw_fib
))) {
289 * Free the space occupied by this copy of the fib.
295 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
297 if(down_interruptible(&fibctx
->wait_sem
) < 0) {
300 /* Lock again and retry */
301 spin_lock_irqsave(&dev
->fib_lock
, flags
);
308 fibctx
->jiffies
= jiffies
/HZ
;
312 int aac_close_fib_context(struct aac_dev
* dev
, struct aac_fib_context
* fibctx
)
317 * First free any FIBs that have not been consumed.
319 while (!list_empty(&fibctx
->fib_list
)) {
320 struct list_head
* entry
;
322 * Pull the next fib from the fibs
324 entry
= fibctx
->fib_list
.next
;
326 fib
= list_entry(entry
, struct fib
, fiblink
);
329 * Free the space occupied by this copy of the fib.
335 * Remove the Context from the AdapterFibContext List
337 list_del(&fibctx
->next
);
343 * Free the space occupied by the Context
350 * close_getadapter_fib - close down user fib context
352 * @arg: ioctl arguments
354 * This routine will close down the fibctx passed in from the user.
357 static int close_getadapter_fib(struct aac_dev
* dev
, void __user
*arg
)
359 struct aac_fib_context
*fibctx
;
362 struct list_head
* entry
;
365 * Verify that the HANDLE passed in was a valid AdapterFibContext
367 * Search the list of AdapterFibContext addresses on the adapter
368 * to be sure this is a valid address
371 entry
= dev
->fib_list
.next
;
374 while(entry
!= &dev
->fib_list
) {
375 fibctx
= list_entry(entry
, struct aac_fib_context
, next
);
377 * Extract the fibctx from the input parameters
379 if (fibctx
->unique
== (u32
)(unsigned long)arg
) {
380 /* We found a winner */
388 return 0; /* Already gone */
390 if((fibctx
->type
!= FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT
) ||
391 (fibctx
->size
!= sizeof(struct aac_fib_context
)))
393 spin_lock_irqsave(&dev
->fib_lock
, flags
);
394 status
= aac_close_fib_context(dev
, fibctx
);
395 spin_unlock_irqrestore(&dev
->fib_lock
, flags
);
400 * check_revision - close down user fib context
402 * @arg: ioctl arguments
404 * This routine returns the driver version.
405 * Under Linux, there have been no version incompatibilities, so this is
409 static int check_revision(struct aac_dev
*dev
, void __user
*arg
)
411 struct revision response
;
412 char *driver_version
= aac_driver_version
;
416 version
= (simple_strtol(driver_version
,
417 &driver_version
, 10) << 24) | 0x00000400;
418 version
+= simple_strtol(driver_version
+ 1, &driver_version
, 10) << 16;
419 version
+= simple_strtol(driver_version
+ 1, NULL
, 10);
420 response
.version
= cpu_to_le32(version
);
421 # if (defined(AAC_DRIVER_BUILD))
422 response
.build
= cpu_to_le32(AAC_DRIVER_BUILD
);
424 response
.build
= cpu_to_le32(9999);
427 if (copy_to_user(arg
, &response
, sizeof(response
)))
439 static int aac_send_raw_srb(struct aac_dev
* dev
, void __user
* arg
)
443 struct aac_srb
*srbcmd
= NULL
;
444 struct user_aac_srb
*user_srbcmd
= NULL
;
445 struct user_aac_srb __user
*user_srb
= arg
;
446 struct aac_srb_reply __user
*user_reply
;
447 struct aac_srb_reply
* reply
;
452 void __user
*sg_user
[32];
456 u32 actual_fibsize
= 0;
460 if (!capable(CAP_SYS_ADMIN
)){
461 dprintk((KERN_DEBUG
"aacraid: No permission to send raw srb\n"));
465 * Allocate and initialize a Fib then setup a BlockWrite command
467 if (!(srbfib
= aac_fib_alloc(dev
))) {
470 aac_fib_init(srbfib
);
472 srbcmd
= (struct aac_srb
*) fib_data(srbfib
);
474 memset(sg_list
, 0, sizeof(sg_list
)); /* cleanup may take issue */
475 if(copy_from_user(&fibsize
, &user_srb
->count
,sizeof(u32
))){
476 dprintk((KERN_DEBUG
"aacraid: Could not copy data size from user\n"));
481 if (fibsize
> (dev
->max_fib_size
- sizeof(struct aac_fibhdr
))) {
486 user_srbcmd
= kmalloc(fibsize
, GFP_KERNEL
);
488 dprintk((KERN_DEBUG
"aacraid: Could not make a copy of the srb\n"));
492 if(copy_from_user(user_srbcmd
, user_srb
,fibsize
)){
493 dprintk((KERN_DEBUG
"aacraid: Could not copy srb from user\n"));
498 user_reply
= arg
+fibsize
;
500 flags
= user_srbcmd
->flags
; /* from user in cpu order */
501 // Fix up srb for endian and force some values
503 srbcmd
->function
= cpu_to_le32(SRBF_ExecuteScsi
); // Force this
504 srbcmd
->channel
= cpu_to_le32(user_srbcmd
->channel
);
505 srbcmd
->id
= cpu_to_le32(user_srbcmd
->id
);
506 srbcmd
->lun
= cpu_to_le32(user_srbcmd
->lun
);
507 srbcmd
->timeout
= cpu_to_le32(user_srbcmd
->timeout
);
508 srbcmd
->flags
= cpu_to_le32(flags
);
509 srbcmd
->retry_limit
= 0; // Obsolete parameter
510 srbcmd
->cdb_size
= cpu_to_le32(user_srbcmd
->cdb_size
);
511 memcpy(srbcmd
->cdb
, user_srbcmd
->cdb
, sizeof(srbcmd
->cdb
));
513 switch (flags
& (SRB_DataIn
| SRB_DataOut
)) {
515 data_dir
= DMA_TO_DEVICE
;
517 case (SRB_DataIn
| SRB_DataOut
):
518 data_dir
= DMA_BIDIRECTIONAL
;
521 data_dir
= DMA_FROM_DEVICE
;
526 if (user_srbcmd
->sg
.count
> (sizeof(sg_list
)/sizeof(sg_list
[0]))) {
527 dprintk((KERN_DEBUG
"aacraid: too many sg entries %d\n",
528 le32_to_cpu(srbcmd
->sg
.count
)));
532 if (dev
->dac_support
== 1) {
533 struct user_sgmap64
* upsg
= (struct user_sgmap64
*)&user_srbcmd
->sg
;
534 struct sgmap64
* psg
= (struct sgmap64
*)&srbcmd
->sg
;
535 struct user_sgmap
* usg
;
539 * This should also catch if user used the 32 bit sgmap
541 actual_fibsize
= sizeof(struct aac_srb
) -
542 sizeof(struct sgentry
) +
543 ((upsg
->count
& 0xff) *
544 sizeof(struct sgentry
));
545 if(actual_fibsize
!= fibsize
){ // User made a mistake - should not continue
546 dprintk((KERN_DEBUG
"aacraid: Bad Size specified in Raw SRB command\n"));
550 usg
= kmalloc(actual_fibsize
- sizeof(struct aac_srb
)
551 + sizeof(struct sgmap
), GFP_KERNEL
);
553 dprintk((KERN_DEBUG
"aacraid: Allocation error in Raw SRB command\n"));
557 memcpy (usg
, upsg
, actual_fibsize
- sizeof(struct aac_srb
)
558 + sizeof(struct sgmap
));
559 actual_fibsize
= sizeof(struct aac_srb
) -
560 sizeof(struct sgentry
) + ((usg
->count
& 0xff) *
561 sizeof(struct sgentry64
));
562 if ((data_dir
== DMA_NONE
) && upsg
->count
) {
564 dprintk((KERN_DEBUG
"aacraid: SG with no direction specified in Raw SRB command\n"));
569 for (i
= 0; i
< usg
->count
; i
++) {
572 /* Does this really need to be GFP_DMA? */
573 p
= kmalloc(usg
->sg
[i
].count
,GFP_KERNEL
|__GFP_DMA
);
576 dprintk((KERN_DEBUG
"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
577 usg
->sg
[i
].count
,i
,usg
->count
));
581 sg_user
[i
] = (void __user
*)(long)usg
->sg
[i
].addr
;
582 sg_list
[i
] = p
; // save so we can clean up later
585 if( flags
& SRB_DataOut
){
586 if(copy_from_user(p
,sg_user
[i
],upsg
->sg
[i
].count
)){
588 dprintk((KERN_DEBUG
"aacraid: Could not copy sg data from user\n"));
593 addr
= pci_map_single(dev
->pdev
, p
, usg
->sg
[i
].count
, data_dir
);
595 psg
->sg
[i
].addr
[0] = cpu_to_le32(addr
& 0xffffffff);
596 psg
->sg
[i
].addr
[1] = cpu_to_le32(addr
>>32);
597 psg
->sg
[i
].count
= cpu_to_le32(usg
->sg
[i
].count
);
598 byte_count
+= usg
->sg
[i
].count
;
602 srbcmd
->count
= cpu_to_le32(byte_count
);
603 psg
->count
= cpu_to_le32(sg_indx
+1);
604 status
= aac_fib_send(ScsiPortCommand64
, srbfib
, actual_fibsize
, FsaNormal
, 1, 1,NULL
,NULL
);
606 struct user_sgmap
* upsg
= &user_srbcmd
->sg
;
607 struct sgmap
* psg
= &srbcmd
->sg
;
610 actual_fibsize
= sizeof (struct aac_srb
) + (((user_srbcmd
->sg
.count
& 0xff) - 1) * sizeof (struct sgentry
));
611 if(actual_fibsize
!= fibsize
){ // User made a mistake - should not continue
612 dprintk((KERN_DEBUG
"aacraid: Bad Size specified in Raw SRB command\n"));
616 if ((data_dir
== DMA_NONE
) && upsg
->count
) {
617 dprintk((KERN_DEBUG
"aacraid: SG with no direction specified in Raw SRB command\n"));
621 for (i
= 0; i
< upsg
->count
; i
++) {
624 p
= kmalloc(upsg
->sg
[i
].count
, GFP_KERNEL
);
626 dprintk((KERN_DEBUG
"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
627 upsg
->sg
[i
].count
, i
, upsg
->count
));
631 sg_user
[i
] = (void __user
*)(long)upsg
->sg
[i
].addr
;
632 sg_list
[i
] = p
; // save so we can clean up later
635 if( flags
& SRB_DataOut
){
636 if(copy_from_user(p
, sg_user
[i
],
637 upsg
->sg
[i
].count
)) {
638 dprintk((KERN_DEBUG
"aacraid: Could not copy sg data from user\n"));
643 addr
= pci_map_single(dev
->pdev
, p
,
644 upsg
->sg
[i
].count
, data_dir
);
646 psg
->sg
[i
].addr
= cpu_to_le32(addr
);
647 psg
->sg
[i
].count
= cpu_to_le32(upsg
->sg
[i
].count
);
648 byte_count
+= upsg
->sg
[i
].count
;
650 srbcmd
->count
= cpu_to_le32(byte_count
);
651 psg
->count
= cpu_to_le32(sg_indx
+1);
652 status
= aac_fib_send(ScsiPortCommand
, srbfib
, actual_fibsize
, FsaNormal
, 1, 1, NULL
, NULL
);
656 dprintk((KERN_DEBUG
"aacraid: Could not send raw srb fib to hba\n"));
661 if( flags
& SRB_DataIn
) {
662 for(i
= 0 ; i
<= sg_indx
; i
++){
663 byte_count
= le32_to_cpu((dev
->dac_support
== 1)
664 ? ((struct sgmap64
*)&srbcmd
->sg
)->sg
[i
].count
665 : srbcmd
->sg
.sg
[i
].count
);
666 if(copy_to_user(sg_user
[i
], sg_list
[i
], byte_count
)){
667 dprintk((KERN_DEBUG
"aacraid: Could not copy sg data to user\n"));
675 reply
= (struct aac_srb_reply
*) fib_data(srbfib
);
676 if(copy_to_user(user_reply
,reply
,sizeof(struct aac_srb_reply
))){
677 dprintk((KERN_DEBUG
"aacraid: Could not copy reply to user\n"));
684 for(i
=0; i
<= sg_indx
; i
++){
687 aac_fib_complete(srbfib
);
688 aac_fib_free(srbfib
);
693 struct aac_pci_info
{
699 static int aac_get_pci_info(struct aac_dev
* dev
, void __user
*arg
)
701 struct aac_pci_info pci_info
;
703 pci_info
.bus
= dev
->pdev
->bus
->number
;
704 pci_info
.slot
= PCI_SLOT(dev
->pdev
->devfn
);
706 if (copy_to_user(arg
, &pci_info
, sizeof(struct aac_pci_info
))) {
707 dprintk((KERN_DEBUG
"aacraid: Could not copy pci info\n"));
714 int aac_do_ioctl(struct aac_dev
* dev
, int cmd
, void __user
*arg
)
719 * HBA gets first crack
722 status
= aac_dev_ioctl(dev
, cmd
, arg
);
723 if(status
!= -ENOTTY
)
727 case FSACTL_MINIPORT_REV_CHECK
:
728 status
= check_revision(dev
, arg
);
730 case FSACTL_SEND_LARGE_FIB
:
732 status
= ioctl_send_fib(dev
, arg
);
734 case FSACTL_OPEN_GET_ADAPTER_FIB
:
735 status
= open_getadapter_fib(dev
, arg
);
737 case FSACTL_GET_NEXT_ADAPTER_FIB
:
738 status
= next_getadapter_fib(dev
, arg
);
740 case FSACTL_CLOSE_GET_ADAPTER_FIB
:
741 status
= close_getadapter_fib(dev
, arg
);
743 case FSACTL_SEND_RAW_SRB
:
744 status
= aac_send_raw_srb(dev
,arg
);
746 case FSACTL_GET_PCI_INFO
:
747 status
= aac_get_pci_info(dev
,arg
);