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-2007 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/pci.h>
35 #include <linux/spinlock.h>
36 #include <linux/slab.h>
37 #include <linux/completion.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/blkdev.h>
40 #include <linux/delay.h> /* ssleep prototype */
41 #include <linux/kthread.h>
42 #include <linux/semaphore.h>
43 #include <asm/uaccess.h>
44 #include <scsi/scsi_host.h>
49 * ioctl_send_fib - send a FIB from userspace
50 * @dev: adapter is being processed
51 * @arg: arguments to the ioctl call
53 * This routine sends a fib to the adapter on behalf of a user level
56 # define AAC_DEBUG_PREAMBLE KERN_INFO
57 # define AAC_DEBUG_POSTAMBLE
59 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
63 struct hw_fib * hw_fib = (struct hw_fib *)0;
64 dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
71 fibptr = aac_fib_alloc(dev);
76 kfib = fibptr->hw_fib_va;
78 * First copy in the header so that we can check the size field.
80 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
85 * Since we copy based on the fib header size, make sure that we
86 * will not overrun the buffer when we copy the memory. Return
87 * an error if we would.
89 size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
90 if (size < le16_to_cpu(kfib->header.SenderSize))
91 size = le16_to_cpu(kfib->header.SenderSize);
92 if (size > dev->max_fib_size) {
97 /* Highjack the hw_fib */
98 hw_fib = fibptr->hw_fib_va;
99 hw_fib_pa = fibptr->hw_fib_pa;
100 fibptr->hw_fib_va = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
101 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
102 memcpy(kfib, hw_fib, dev->max_fib_size);
105 if (copy_from_user(kfib, arg, size)) {
110 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
111 aac_adapter_interrupt(dev);
113 * Since we didn't really send a fib, zero out the state to allow
114 * cleanup code not to assert.
116 kfib->header.XferState = 0;
118 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
119 le16_to_cpu(kfib->header.Size) , FsaNormal,
124 if (aac_fib_complete(fibptr) != 0) {
130 * Make sure that the size returned by the adapter (which includes
131 * the header) is less than or equal to the size of a fib, so we
132 * don't corrupt application data. Then copy that size to the user
133 * buffer. (Don't try to add the header information again, since it
134 * was already included by the adapter.)
138 if (copy_to_user(arg, (void *)kfib, size))
142 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
143 fibptr->hw_fib_pa = hw_fib_pa;
144 fibptr->hw_fib_va = hw_fib;
146 if (retval != -EINTR)
147 aac_fib_free(fibptr);
152 * open_getadapter_fib - Get the next fib
154 * This routine will get the next Fib, if available, from the AdapterFibContext
155 * passed in from the user.
158 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
160 struct aac_fib_context * fibctx;
163 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
164 if (fibctx == NULL) {
168 struct list_head * entry;
169 struct aac_fib_context * context;
171 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
172 fibctx->size = sizeof(struct aac_fib_context);
174 * Yes yes, I know this could be an index, but we have a
175 * better guarantee of uniqueness for the locked loop below.
176 * Without the aid of a persistent history, this also helps
177 * reduce the chance that the opaque context would be reused.
179 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
181 * Initialize the mutex used to wait for the next AIF.
183 init_MUTEX_LOCKED(&fibctx->wait_sem);
186 * Initialize the fibs and set the count of fibs on
190 INIT_LIST_HEAD(&fibctx->fib_list);
191 fibctx->jiffies = jiffies/HZ;
193 * Now add this context onto the adapter's
194 * AdapterFibContext list.
196 spin_lock_irqsave(&dev->fib_lock, flags);
197 /* Ensure that we have a unique identifier */
198 entry = dev->fib_list.next;
199 while (entry != &dev->fib_list) {
200 context = list_entry(entry, struct aac_fib_context, next);
201 if (context->unique == fibctx->unique) {
202 /* Not unique (32 bits) */
204 entry = dev->fib_list.next;
209 list_add_tail(&fibctx->next, &dev->fib_list);
210 spin_unlock_irqrestore(&dev->fib_lock, flags);
211 if (copy_to_user(arg, &fibctx->unique,
212 sizeof(fibctx->unique))) {
222 * next_getadapter_fib - get the next fib
223 * @dev: adapter to use
224 * @arg: ioctl argument
226 * This routine will get the next Fib, if available, from the AdapterFibContext
227 * passed in from the user.
230 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
234 struct aac_fib_context *fibctx;
236 struct list_head * entry;
239 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
242 * Verify that the HANDLE passed in was a valid AdapterFibContext
244 * Search the list of AdapterFibContext addresses on the adapter
245 * to be sure this is a valid address
247 spin_lock_irqsave(&dev->fib_lock, flags);
248 entry = dev->fib_list.next;
251 while (entry != &dev->fib_list) {
252 fibctx = list_entry(entry, struct aac_fib_context, next);
254 * Extract the AdapterFibContext from the Input parameters.
256 if (fibctx->unique == f.fibctx) { /* We found a winner */
263 spin_unlock_irqrestore(&dev->fib_lock, flags);
264 dprintk ((KERN_INFO "Fib Context not found\n"));
268 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
269 (fibctx->size != sizeof(struct aac_fib_context))) {
270 spin_unlock_irqrestore(&dev->fib_lock, flags);
271 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
276 * If there are no fibs to send back, then either wait or return
280 if (!list_empty(&fibctx->fib_list)) {
282 * Pull the next fib from the fibs
284 entry = fibctx->fib_list.next;
287 fib = list_entry(entry, struct fib, fiblink);
289 spin_unlock_irqrestore(&dev->fib_lock, flags);
290 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
291 kfree(fib->hw_fib_va);
296 * Free the space occupied by this copy of the fib.
298 kfree(fib->hw_fib_va);
302 spin_unlock_irqrestore(&dev->fib_lock, flags);
303 /* If someone killed the AIF aacraid thread, restart it */
304 status = !dev->aif_thread;
305 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
306 /* Be paranoid, be very paranoid! */
307 kthread_stop(dev->thread);
310 dev->thread = kthread_run(aac_command_thread, dev, dev->name);
314 if(down_interruptible(&fibctx->wait_sem) < 0) {
317 /* Lock again and retry */
318 spin_lock_irqsave(&dev->fib_lock, flags);
325 fibctx->jiffies = jiffies/HZ;
329 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
334 * First free any FIBs that have not been consumed.
336 while (!list_empty(&fibctx->fib_list)) {
337 struct list_head * entry;
339 * Pull the next fib from the fibs
341 entry = fibctx->fib_list.next;
343 fib = list_entry(entry, struct fib, fiblink);
346 * Free the space occupied by this copy of the fib.
348 kfree(fib->hw_fib_va);
352 * Remove the Context from the AdapterFibContext List
354 list_del(&fibctx->next);
360 * Free the space occupied by the Context
367 * close_getadapter_fib - close down user fib context
369 * @arg: ioctl arguments
371 * This routine will close down the fibctx passed in from the user.
374 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
376 struct aac_fib_context *fibctx;
379 struct list_head * entry;
382 * Verify that the HANDLE passed in was a valid AdapterFibContext
384 * Search the list of AdapterFibContext addresses on the adapter
385 * to be sure this is a valid address
388 entry = dev->fib_list.next;
391 while(entry != &dev->fib_list) {
392 fibctx = list_entry(entry, struct aac_fib_context, next);
394 * Extract the fibctx from the input parameters
396 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
403 return 0; /* Already gone */
405 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
406 (fibctx->size != sizeof(struct aac_fib_context)))
408 spin_lock_irqsave(&dev->fib_lock, flags);
409 status = aac_close_fib_context(dev, fibctx);
410 spin_unlock_irqrestore(&dev->fib_lock, flags);
415 * check_revision - close down user fib context
417 * @arg: ioctl arguments
419 * This routine returns the driver version.
420 * Under Linux, there have been no version incompatibilities, so this is
424 static int check_revision(struct aac_dev *dev, void __user *arg)
426 struct revision response;
427 char *driver_version = aac_driver_version;
431 version = (simple_strtol(driver_version,
432 &driver_version, 10) << 24) | 0x00000400;
433 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
434 version += simple_strtol(driver_version + 1, NULL, 10);
435 response.version = cpu_to_le32(version);
436 # ifdef AAC_DRIVER_BUILD
437 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
439 response.build = cpu_to_le32(9999);
442 if (copy_to_user(arg, &response, sizeof(response)))
454 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
458 struct aac_srb *srbcmd = NULL;
459 struct user_aac_srb *user_srbcmd = NULL;
460 struct user_aac_srb __user *user_srb = arg;
461 struct aac_srb_reply __user *user_reply;
462 struct aac_srb_reply* reply;
467 void __user *sg_user[32];
471 u32 actual_fibsize64, actual_fibsize = 0;
476 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
479 if (!capable(CAP_SYS_ADMIN)){
480 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
484 * Allocate and initialize a Fib then setup a SRB command
486 if (!(srbfib = aac_fib_alloc(dev))) {
489 aac_fib_init(srbfib);
491 srbcmd = (struct aac_srb*) fib_data(srbfib);
493 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
494 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
495 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
500 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
505 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
507 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
511 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
512 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
517 user_reply = arg+fibsize;
519 flags = user_srbcmd->flags; /* from user in cpu order */
520 // Fix up srb for endian and force some values
522 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
523 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
524 srbcmd->id = cpu_to_le32(user_srbcmd->id);
525 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
526 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
527 srbcmd->flags = cpu_to_le32(flags);
528 srbcmd->retry_limit = 0; // Obsolete parameter
529 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
530 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
532 switch (flags & (SRB_DataIn | SRB_DataOut)) {
534 data_dir = DMA_TO_DEVICE;
536 case (SRB_DataIn | SRB_DataOut):
537 data_dir = DMA_BIDIRECTIONAL;
540 data_dir = DMA_FROM_DEVICE;
545 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
546 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
547 le32_to_cpu(srbcmd->sg.count)));
551 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
552 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
553 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
554 (sizeof(struct sgentry64) - sizeof(struct sgentry));
555 /* User made a mistake - should not continue */
556 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
557 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
558 "Raw SRB command calculated fibsize=%lu;%lu "
559 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
560 "issued fibsize=%d\n",
561 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
562 sizeof(struct aac_srb), sizeof(struct sgentry),
563 sizeof(struct sgentry64), fibsize));
567 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
568 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
573 if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
574 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
575 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
578 * This should also catch if user used the 32 bit sgmap
580 if (actual_fibsize64 == fibsize) {
581 actual_fibsize = actual_fibsize64;
582 for (i = 0; i < upsg->count; i++) {
585 if (upsg->sg[i].count >
586 (dev->adapter_info.options &
588 (dev->scsi_host_ptr->max_sectors << 9) :
593 /* Does this really need to be GFP_DMA? */
594 p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
596 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
597 upsg->sg[i].count,i,upsg->count));
601 addr = (u64)upsg->sg[i].addr[0];
602 addr += ((u64)upsg->sg[i].addr[1]) << 32;
603 sg_user[i] = (void __user *)(uintptr_t)addr;
604 sg_list[i] = p; // save so we can clean up later
607 if (flags & SRB_DataOut) {
608 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
609 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
614 addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
616 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
617 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
618 byte_count += upsg->sg[i].count;
619 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
622 struct user_sgmap* usg;
623 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
624 + sizeof(struct sgmap), GFP_KERNEL);
626 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
630 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
631 + sizeof(struct sgmap));
632 actual_fibsize = actual_fibsize64;
634 for (i = 0; i < usg->count; i++) {
637 if (usg->sg[i].count >
638 (dev->adapter_info.options &
640 (dev->scsi_host_ptr->max_sectors << 9) :
645 /* Does this really need to be GFP_DMA? */
646 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
649 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
650 usg->sg[i].count,i,usg->count));
654 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
655 sg_list[i] = p; // save so we can clean up later
658 if (flags & SRB_DataOut) {
659 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
661 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
666 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
668 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
669 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
670 byte_count += usg->sg[i].count;
671 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
675 srbcmd->count = cpu_to_le32(byte_count);
676 psg->count = cpu_to_le32(sg_indx+1);
677 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
679 struct user_sgmap* upsg = &user_srbcmd->sg;
680 struct sgmap* psg = &srbcmd->sg;
682 if (actual_fibsize64 == fibsize) {
683 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
684 for (i = 0; i < upsg->count; i++) {
687 if (usg->sg[i].count >
688 (dev->adapter_info.options &
690 (dev->scsi_host_ptr->max_sectors << 9) :
695 /* Does this really need to be GFP_DMA? */
696 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
698 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
699 usg->sg[i].count,i,usg->count));
703 addr = (u64)usg->sg[i].addr[0];
704 addr += ((u64)usg->sg[i].addr[1]) << 32;
705 sg_user[i] = (void __user *)addr;
706 sg_list[i] = p; // save so we can clean up later
709 if (flags & SRB_DataOut) {
710 if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
711 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
716 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
718 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
719 byte_count += usg->sg[i].count;
720 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
723 for (i = 0; i < upsg->count; i++) {
726 if (upsg->sg[i].count >
727 (dev->adapter_info.options &
729 (dev->scsi_host_ptr->max_sectors << 9) :
734 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
736 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
737 upsg->sg[i].count, i, upsg->count));
741 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
742 sg_list[i] = p; // save so we can clean up later
745 if (flags & SRB_DataOut) {
746 if(copy_from_user(p, sg_user[i],
747 upsg->sg[i].count)) {
748 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
753 addr = pci_map_single(dev->pdev, p,
754 upsg->sg[i].count, data_dir);
756 psg->sg[i].addr = cpu_to_le32(addr);
757 byte_count += upsg->sg[i].count;
758 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
761 srbcmd->count = cpu_to_le32(byte_count);
762 psg->count = cpu_to_le32(sg_indx+1);
763 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
765 if (status == -EINTR) {
771 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
776 if (flags & SRB_DataIn) {
777 for(i = 0 ; i <= sg_indx; i++){
778 byte_count = le32_to_cpu(
779 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
780 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
781 : srbcmd->sg.sg[i].count);
782 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
783 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
791 reply = (struct aac_srb_reply *) fib_data(srbfib);
792 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
793 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
800 for(i=0; i <= sg_indx; i++){
803 if (rcode != -EINTR) {
804 aac_fib_complete(srbfib);
805 aac_fib_free(srbfib);
811 struct aac_pci_info {
817 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
819 struct aac_pci_info pci_info;
821 pci_info.bus = dev->pdev->bus->number;
822 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
824 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
825 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
832 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
837 * HBA gets first crack
840 status = aac_dev_ioctl(dev, cmd, arg);
841 if(status != -ENOTTY)
845 case FSACTL_MINIPORT_REV_CHECK:
846 status = check_revision(dev, arg);
848 case FSACTL_SEND_LARGE_FIB:
850 status = ioctl_send_fib(dev, arg);
852 case FSACTL_OPEN_GET_ADAPTER_FIB:
853 status = open_getadapter_fib(dev, arg);
855 case FSACTL_GET_NEXT_ADAPTER_FIB:
856 status = next_getadapter_fib(dev, arg);
858 case FSACTL_CLOSE_GET_ADAPTER_FIB:
859 status = close_getadapter_fib(dev, arg);
861 case FSACTL_SEND_RAW_SRB:
862 status = aac_send_raw_srb(dev,arg);
864 case FSACTL_GET_PCI_INFO:
865 status = aac_get_pci_info(dev,arg);