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 = 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) {
88 /* Highjack the hw_fib */
89 hw_fib = fibptr->hw_fib;
90 hw_fib_pa = fibptr->hw_fib_pa;
91 fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
92 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
93 memcpy(kfib, hw_fib, dev->max_fib_size);
96 if (copy_from_user(kfib, arg, size)) {
101 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
102 aac_adapter_interrupt(dev);
104 * Since we didn't really send a fib, zero out the state to allow
105 * cleanup code not to assert.
107 kfib->header.XferState = 0;
109 retval = fib_send(le16_to_cpu(kfib->header.Command), fibptr,
110 le16_to_cpu(kfib->header.Size) , FsaNormal,
115 if (fib_complete(fibptr) != 0) {
121 * Make sure that the size returned by the adapter (which includes
122 * the header) is less than or equal to the size of a fib, so we
123 * don't corrupt application data. Then copy that size to the user
124 * buffer. (Don't try to add the header information again, since it
125 * was already included by the adapter.)
129 if (copy_to_user(arg, (void *)kfib, size))
133 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
134 fibptr->hw_fib_pa = hw_fib_pa;
135 fibptr->hw_fib = hw_fib;
142 * open_getadapter_fib - Get the next fib
144 * This routine will get the next Fib, if available, from the AdapterFibContext
145 * passed in from the user.
148 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
150 struct aac_fib_context * fibctx;
153 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
154 if (fibctx == NULL) {
158 struct list_head * entry;
159 struct aac_fib_context * context;
161 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
162 fibctx->size = sizeof(struct aac_fib_context);
164 * Yes yes, I know this could be an index, but we have a
165 * better guarantee of uniqueness for the locked loop below.
166 * Without the aid of a persistent history, this also helps
167 * reduce the chance that the opaque context would be reused.
169 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
171 * Initialize the mutex used to wait for the next AIF.
173 init_MUTEX_LOCKED(&fibctx->wait_sem);
176 * Initialize the fibs and set the count of fibs on
180 INIT_LIST_HEAD(&fibctx->fib_list);
181 fibctx->jiffies = jiffies/HZ;
183 * Now add this context onto the adapter's
184 * AdapterFibContext list.
186 spin_lock_irqsave(&dev->fib_lock, flags);
187 /* Ensure that we have a unique identifier */
188 entry = dev->fib_list.next;
189 while (entry != &dev->fib_list) {
190 context = list_entry(entry, struct aac_fib_context, next);
191 if (context->unique == fibctx->unique) {
192 /* Not unique (32 bits) */
194 entry = dev->fib_list.next;
199 list_add_tail(&fibctx->next, &dev->fib_list);
200 spin_unlock_irqrestore(&dev->fib_lock, flags);
201 if (copy_to_user(arg, &fibctx->unique,
202 sizeof(fibctx->unique))) {
212 * next_getadapter_fib - get the next fib
213 * @dev: adapter to use
214 * @arg: ioctl argument
216 * This routine will get the next Fib, if available, from the AdapterFibContext
217 * passed in from the user.
220 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
224 struct aac_fib_context *fibctx;
226 struct list_head * entry;
229 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
232 * Verify that the HANDLE passed in was a valid AdapterFibContext
234 * Search the list of AdapterFibContext addresses on the adapter
235 * to be sure this is a valid address
237 entry = dev->fib_list.next;
240 while (entry != &dev->fib_list) {
241 fibctx = list_entry(entry, struct aac_fib_context, next);
243 * Extract the AdapterFibContext from the Input parameters.
245 if (fibctx->unique == f.fibctx) { /* We found a winner */
252 dprintk ((KERN_INFO "Fib Context not found\n"));
256 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
257 (fibctx->size != sizeof(struct aac_fib_context))) {
258 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
262 spin_lock_irqsave(&dev->fib_lock, flags);
264 * If there are no fibs to send back, then either wait or return
268 if (!list_empty(&fibctx->fib_list)) {
269 struct list_head * entry;
271 * Pull the next fib from the fibs
273 entry = fibctx->fib_list.next;
276 fib = list_entry(entry, struct fib, fiblink);
278 spin_unlock_irqrestore(&dev->fib_lock, flags);
279 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
285 * Free the space occupied by this copy of the fib.
290 fibctx->jiffies = jiffies/HZ;
292 spin_unlock_irqrestore(&dev->fib_lock, flags);
294 if(down_interruptible(&fibctx->wait_sem) < 0) {
297 /* Lock again and retry */
298 spin_lock_irqsave(&dev->fib_lock, flags);
308 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
313 * First free any FIBs that have not been consumed.
315 while (!list_empty(&fibctx->fib_list)) {
316 struct list_head * entry;
318 * Pull the next fib from the fibs
320 entry = fibctx->fib_list.next;
322 fib = list_entry(entry, struct fib, fiblink);
325 * Free the space occupied by this copy of the fib.
331 * Remove the Context from the AdapterFibContext List
333 list_del(&fibctx->next);
339 * Free the space occupied by the Context
346 * close_getadapter_fib - close down user fib context
348 * @arg: ioctl arguments
350 * This routine will close down the fibctx passed in from the user.
353 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
355 struct aac_fib_context *fibctx;
358 struct list_head * entry;
361 * Verify that the HANDLE passed in was a valid AdapterFibContext
363 * Search the list of AdapterFibContext addresses on the adapter
364 * to be sure this is a valid address
367 entry = dev->fib_list.next;
370 while(entry != &dev->fib_list) {
371 fibctx = list_entry(entry, struct aac_fib_context, next);
373 * Extract the fibctx from the input parameters
375 if (fibctx->unique == (u32)(unsigned long)arg) {
376 /* We found a winner */
384 return 0; /* Already gone */
386 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
387 (fibctx->size != sizeof(struct aac_fib_context)))
389 spin_lock_irqsave(&dev->fib_lock, flags);
390 status = aac_close_fib_context(dev, fibctx);
391 spin_unlock_irqrestore(&dev->fib_lock, flags);
396 * check_revision - close down user fib context
398 * @arg: ioctl arguments
400 * This routine returns the driver version.
401 * Under Linux, there have been no version incompatibilities, so this is
405 static int check_revision(struct aac_dev *dev, void __user *arg)
407 struct revision response;
410 response.version = le32_to_cpu(dev->adapter_info.kernelrev);
411 response.build = le32_to_cpu(dev->adapter_info.kernelbuild);
413 if (copy_to_user(arg, &response, sizeof(response)))
425 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
429 struct aac_srb *srbcmd = NULL;
430 struct user_aac_srb *user_srbcmd = NULL;
431 struct user_aac_srb __user *user_srb = arg;
432 struct aac_srb_reply __user *user_reply;
433 struct aac_srb_reply* reply;
438 void __user *sg_user[32];
442 u32 actual_fibsize = 0;
446 if (!capable(CAP_SYS_ADMIN)){
447 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
451 * Allocate and initialize a Fib then setup a BlockWrite command
453 if (!(srbfib = fib_alloc(dev))) {
458 srbcmd = (struct aac_srb*) fib_data(srbfib);
460 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
461 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
462 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
467 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
472 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
474 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
478 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
479 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
484 user_reply = arg+fibsize;
486 flags = user_srbcmd->flags; /* from user in cpu order */
487 // Fix up srb for endian and force some values
489 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
490 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
491 srbcmd->id = cpu_to_le32(user_srbcmd->id);
492 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
493 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
494 srbcmd->flags = cpu_to_le32(flags);
495 srbcmd->retry_limit = 0; // Obsolete parameter
496 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
497 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
499 switch (flags & (SRB_DataIn | SRB_DataOut)) {
501 data_dir = DMA_TO_DEVICE;
503 case (SRB_DataIn | SRB_DataOut):
504 data_dir = DMA_BIDIRECTIONAL;
507 data_dir = DMA_FROM_DEVICE;
512 if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) {
513 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
514 le32_to_cpu(srbcmd->sg.count)));
518 if (dev->dac_support == 1) {
519 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
520 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
521 struct user_sgmap* usg;
525 * This should also catch if user used the 32 bit sgmap
527 actual_fibsize = sizeof(struct aac_srb) -
528 sizeof(struct sgentry) +
529 ((upsg->count & 0xff) *
530 sizeof(struct sgentry));
531 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
532 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
536 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
537 + sizeof(struct sgmap), GFP_KERNEL);
539 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
543 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
544 + sizeof(struct sgmap));
545 actual_fibsize = sizeof(struct aac_srb) -
546 sizeof(struct sgentry) + ((usg->count & 0xff) *
547 sizeof(struct sgentry64));
548 if ((data_dir == DMA_NONE) && upsg->count) {
550 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
555 for (i = 0; i < usg->count; i++) {
558 /* Does this really need to be GFP_DMA? */
559 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
562 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
563 usg->sg[i].count,i,usg->count));
567 sg_user[i] = (void __user *)usg->sg[i].addr;
568 sg_list[i] = p; // save so we can clean up later
571 if( flags & SRB_DataOut ){
572 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
574 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
579 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
581 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
582 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
583 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
584 byte_count += usg->sg[i].count;
588 srbcmd->count = cpu_to_le32(byte_count);
589 psg->count = cpu_to_le32(sg_indx+1);
590 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
592 struct user_sgmap* upsg = &user_srbcmd->sg;
593 struct sgmap* psg = &srbcmd->sg;
596 actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
597 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
598 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
602 if ((data_dir == DMA_NONE) && upsg->count) {
603 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
607 for (i = 0; i < upsg->count; i++) {
610 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
612 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
613 upsg->sg[i].count, i, upsg->count));
617 sg_user[i] = (void __user *)upsg->sg[i].addr;
618 sg_list[i] = p; // save so we can clean up later
621 if( flags & SRB_DataOut ){
622 if(copy_from_user(p, sg_user[i],
623 upsg->sg[i].count)) {
624 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
629 addr = pci_map_single(dev->pdev, p,
630 upsg->sg[i].count, data_dir);
632 psg->sg[i].addr = cpu_to_le32(addr);
633 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
634 byte_count += upsg->sg[i].count;
636 srbcmd->count = cpu_to_le32(byte_count);
637 psg->count = cpu_to_le32(sg_indx+1);
638 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
642 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
647 if( flags & SRB_DataIn ) {
648 for(i = 0 ; i <= sg_indx; i++){
649 byte_count = le32_to_cpu((dev->dac_support == 1)
650 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
651 : srbcmd->sg.sg[i].count);
652 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
653 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
661 reply = (struct aac_srb_reply *) fib_data(srbfib);
662 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
663 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
670 for(i=0; i <= sg_indx; i++){
673 fib_complete(srbfib);
679 struct aac_pci_info {
685 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
687 struct aac_pci_info pci_info;
689 pci_info.bus = dev->pdev->bus->number;
690 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
692 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
693 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
700 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
705 * HBA gets first crack
708 status = aac_dev_ioctl(dev, cmd, arg);
709 if(status != -ENOTTY)
713 case FSACTL_MINIPORT_REV_CHECK:
714 status = check_revision(dev, arg);
716 case FSACTL_SEND_LARGE_FIB:
718 status = ioctl_send_fib(dev, arg);
720 case FSACTL_OPEN_GET_ADAPTER_FIB:
721 status = open_getadapter_fib(dev, arg);
723 case FSACTL_GET_NEXT_ADAPTER_FIB:
724 status = next_getadapter_fib(dev, arg);
726 case FSACTL_CLOSE_GET_ADAPTER_FIB:
727 status = close_getadapter_fib(dev, arg);
729 case FSACTL_SEND_RAW_SRB:
730 status = aac_send_raw_srb(dev,arg);
732 case FSACTL_GET_PCI_INFO:
733 status = aac_get_pci_info(dev,arg);