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 <linux/delay.h>
42 #include <linux/kthread.h>
43 #include <asm/semaphore.h>
44 #include <asm/uaccess.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;
68 fibptr = aac_fib_alloc(dev);
73 kfib = fibptr->hw_fib;
75 * First copy in the header so that we can check the size field.
77 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
82 * Since we copy based on the fib header size, make sure that we
83 * will not overrun the buffer when we copy the memory. Return
84 * an error if we would.
86 size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
87 if (size < le16_to_cpu(kfib->header.SenderSize))
88 size = le16_to_cpu(kfib->header.SenderSize);
89 if (size > dev->max_fib_size) {
94 /* Highjack the hw_fib */
95 hw_fib = fibptr->hw_fib;
96 hw_fib_pa = fibptr->hw_fib_pa;
97 fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
98 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
99 memcpy(kfib, hw_fib, dev->max_fib_size);
102 if (copy_from_user(kfib, arg, size)) {
107 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
108 aac_adapter_interrupt(dev);
110 * Since we didn't really send a fib, zero out the state to allow
111 * cleanup code not to assert.
113 kfib->header.XferState = 0;
115 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
116 le16_to_cpu(kfib->header.Size) , FsaNormal,
121 if (aac_fib_complete(fibptr) != 0) {
127 * Make sure that the size returned by the adapter (which includes
128 * the header) is less than or equal to the size of a fib, so we
129 * don't corrupt application data. Then copy that size to the user
130 * buffer. (Don't try to add the header information again, since it
131 * was already included by the adapter.)
135 if (copy_to_user(arg, (void *)kfib, size))
139 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
140 fibptr->hw_fib_pa = hw_fib_pa;
141 fibptr->hw_fib = hw_fib;
143 aac_fib_free(fibptr);
148 * open_getadapter_fib - Get the next fib
150 * This routine will get the next Fib, if available, from the AdapterFibContext
151 * passed in from the user.
154 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
156 struct aac_fib_context * fibctx;
159 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
160 if (fibctx == NULL) {
164 struct list_head * entry;
165 struct aac_fib_context * context;
167 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
168 fibctx->size = sizeof(struct aac_fib_context);
170 * Yes yes, I know this could be an index, but we have a
171 * better guarantee of uniqueness for the locked loop below.
172 * Without the aid of a persistent history, this also helps
173 * reduce the chance that the opaque context would be reused.
175 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
177 * Initialize the mutex used to wait for the next AIF.
179 init_MUTEX_LOCKED(&fibctx->wait_sem);
182 * Initialize the fibs and set the count of fibs on
186 INIT_LIST_HEAD(&fibctx->fib_list);
187 fibctx->jiffies = jiffies/HZ;
189 * Now add this context onto the adapter's
190 * AdapterFibContext list.
192 spin_lock_irqsave(&dev->fib_lock, flags);
193 /* Ensure that we have a unique identifier */
194 entry = dev->fib_list.next;
195 while (entry != &dev->fib_list) {
196 context = list_entry(entry, struct aac_fib_context, next);
197 if (context->unique == fibctx->unique) {
198 /* Not unique (32 bits) */
200 entry = dev->fib_list.next;
205 list_add_tail(&fibctx->next, &dev->fib_list);
206 spin_unlock_irqrestore(&dev->fib_lock, flags);
207 if (copy_to_user(arg, &fibctx->unique,
208 sizeof(fibctx->unique))) {
218 * next_getadapter_fib - get the next fib
219 * @dev: adapter to use
220 * @arg: ioctl argument
222 * This routine will get the next Fib, if available, from the AdapterFibContext
223 * passed in from the user.
226 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
230 struct aac_fib_context *fibctx;
232 struct list_head * entry;
235 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
238 * Verify that the HANDLE passed in was a valid AdapterFibContext
240 * Search the list of AdapterFibContext addresses on the adapter
241 * to be sure this is a valid address
243 entry = dev->fib_list.next;
246 while (entry != &dev->fib_list) {
247 fibctx = list_entry(entry, struct aac_fib_context, next);
249 * Extract the AdapterFibContext from the Input parameters.
251 if (fibctx->unique == f.fibctx) { /* We found a winner */
258 dprintk ((KERN_INFO "Fib Context not found\n"));
262 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
263 (fibctx->size != sizeof(struct aac_fib_context))) {
264 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
268 spin_lock_irqsave(&dev->fib_lock, flags);
270 * If there are no fibs to send back, then either wait or return
274 if (!list_empty(&fibctx->fib_list)) {
275 struct list_head * entry;
277 * Pull the next fib from the fibs
279 entry = fibctx->fib_list.next;
282 fib = list_entry(entry, struct fib, fiblink);
284 spin_unlock_irqrestore(&dev->fib_lock, flags);
285 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
291 * Free the space occupied by this copy of the fib.
297 spin_unlock_irqrestore(&dev->fib_lock, flags);
298 /* If someone killed the AIF aacraid thread, restart it */
299 status = !dev->aif_thread;
300 if (status && dev->queues && dev->fsa_dev) {
301 /* Be paranoid, be very paranoid! */
302 kthread_stop(dev->thread);
305 dev->thread = kthread_run(aac_command_thread, dev, dev->name);
309 if(down_interruptible(&fibctx->wait_sem) < 0) {
312 /* Lock again and retry */
313 spin_lock_irqsave(&dev->fib_lock, flags);
320 fibctx->jiffies = jiffies/HZ;
324 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
329 * First free any FIBs that have not been consumed.
331 while (!list_empty(&fibctx->fib_list)) {
332 struct list_head * entry;
334 * Pull the next fib from the fibs
336 entry = fibctx->fib_list.next;
338 fib = list_entry(entry, struct fib, fiblink);
341 * Free the space occupied by this copy of the fib.
347 * Remove the Context from the AdapterFibContext List
349 list_del(&fibctx->next);
355 * Free the space occupied by the Context
362 * close_getadapter_fib - close down user fib context
364 * @arg: ioctl arguments
366 * This routine will close down the fibctx passed in from the user.
369 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
371 struct aac_fib_context *fibctx;
374 struct list_head * entry;
377 * Verify that the HANDLE passed in was a valid AdapterFibContext
379 * Search the list of AdapterFibContext addresses on the adapter
380 * to be sure this is a valid address
383 entry = dev->fib_list.next;
386 while(entry != &dev->fib_list) {
387 fibctx = list_entry(entry, struct aac_fib_context, next);
389 * Extract the fibctx from the input parameters
391 if (fibctx->unique == (u32)(unsigned long)arg) {
392 /* We found a winner */
400 return 0; /* Already gone */
402 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
403 (fibctx->size != sizeof(struct aac_fib_context)))
405 spin_lock_irqsave(&dev->fib_lock, flags);
406 status = aac_close_fib_context(dev, fibctx);
407 spin_unlock_irqrestore(&dev->fib_lock, flags);
412 * check_revision - close down user fib context
414 * @arg: ioctl arguments
416 * This routine returns the driver version.
417 * Under Linux, there have been no version incompatibilities, so this is
421 static int check_revision(struct aac_dev *dev, void __user *arg)
423 struct revision response;
424 char *driver_version = aac_driver_version;
428 version = (simple_strtol(driver_version,
429 &driver_version, 10) << 24) | 0x00000400;
430 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
431 version += simple_strtol(driver_version + 1, NULL, 10);
432 response.version = cpu_to_le32(version);
433 # if (defined(AAC_DRIVER_BUILD))
434 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
436 response.build = cpu_to_le32(9999);
439 if (copy_to_user(arg, &response, sizeof(response)))
451 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
455 struct aac_srb *srbcmd = NULL;
456 struct user_aac_srb *user_srbcmd = NULL;
457 struct user_aac_srb __user *user_srb = arg;
458 struct aac_srb_reply __user *user_reply;
459 struct aac_srb_reply* reply;
464 void __user *sg_user[32];
468 u32 actual_fibsize = 0;
472 if (!capable(CAP_SYS_ADMIN)){
473 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
477 * Allocate and initialize a Fib then setup a BlockWrite command
479 if (!(srbfib = aac_fib_alloc(dev))) {
482 aac_fib_init(srbfib);
484 srbcmd = (struct aac_srb*) fib_data(srbfib);
486 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
487 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
488 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
493 if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
498 user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
500 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
504 if(copy_from_user(user_srbcmd, user_srb,fibsize)){
505 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
510 user_reply = arg+fibsize;
512 flags = user_srbcmd->flags; /* from user in cpu order */
513 // Fix up srb for endian and force some values
515 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
516 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
517 srbcmd->id = cpu_to_le32(user_srbcmd->id);
518 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
519 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
520 srbcmd->flags = cpu_to_le32(flags);
521 srbcmd->retry_limit = 0; // Obsolete parameter
522 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
523 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
525 switch (flags & (SRB_DataIn | SRB_DataOut)) {
527 data_dir = DMA_TO_DEVICE;
529 case (SRB_DataIn | SRB_DataOut):
530 data_dir = DMA_BIDIRECTIONAL;
533 data_dir = DMA_FROM_DEVICE;
538 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
539 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
540 le32_to_cpu(srbcmd->sg.count)));
544 if (dev->dac_support == 1) {
545 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
546 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
547 struct user_sgmap* usg;
551 * This should also catch if user used the 32 bit sgmap
553 actual_fibsize = sizeof(struct aac_srb) -
554 sizeof(struct sgentry) +
555 ((upsg->count & 0xff) *
556 sizeof(struct sgentry));
557 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
558 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
562 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
563 + sizeof(struct sgmap), GFP_KERNEL);
565 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
569 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
570 + sizeof(struct sgmap));
571 actual_fibsize = sizeof(struct aac_srb) -
572 sizeof(struct sgentry) + ((usg->count & 0xff) *
573 sizeof(struct sgentry64));
574 if ((data_dir == DMA_NONE) && upsg->count) {
576 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
581 for (i = 0; i < usg->count; i++) {
584 /* Does this really need to be GFP_DMA? */
585 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
588 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
589 usg->sg[i].count,i,usg->count));
593 sg_user[i] = (void __user *)(long)usg->sg[i].addr;
594 sg_list[i] = p; // save so we can clean up later
597 if( flags & SRB_DataOut ){
598 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
600 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
605 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
607 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
608 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
609 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
610 byte_count += usg->sg[i].count;
614 srbcmd->count = cpu_to_le32(byte_count);
615 psg->count = cpu_to_le32(sg_indx+1);
616 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
618 struct user_sgmap* upsg = &user_srbcmd->sg;
619 struct sgmap* psg = &srbcmd->sg;
622 actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
623 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
624 dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
628 if ((data_dir == DMA_NONE) && upsg->count) {
629 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
633 for (i = 0; i < upsg->count; i++) {
636 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
638 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
639 upsg->sg[i].count, i, upsg->count));
643 sg_user[i] = (void __user *)(long)upsg->sg[i].addr;
644 sg_list[i] = p; // save so we can clean up later
647 if( flags & SRB_DataOut ){
648 if(copy_from_user(p, sg_user[i],
649 upsg->sg[i].count)) {
650 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
655 addr = pci_map_single(dev->pdev, p,
656 upsg->sg[i].count, data_dir);
658 psg->sg[i].addr = cpu_to_le32(addr);
659 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
660 byte_count += upsg->sg[i].count;
662 srbcmd->count = cpu_to_le32(byte_count);
663 psg->count = cpu_to_le32(sg_indx+1);
664 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
668 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
673 if( flags & SRB_DataIn ) {
674 for(i = 0 ; i <= sg_indx; i++){
675 byte_count = le32_to_cpu((dev->dac_support == 1)
676 ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
677 : srbcmd->sg.sg[i].count);
678 if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
679 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
687 reply = (struct aac_srb_reply *) fib_data(srbfib);
688 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
689 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
696 for(i=0; i <= sg_indx; i++){
699 aac_fib_complete(srbfib);
700 aac_fib_free(srbfib);
705 struct aac_pci_info {
711 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
713 struct aac_pci_info pci_info;
715 pci_info.bus = dev->pdev->bus->number;
716 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
718 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
719 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
726 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
731 * HBA gets first crack
734 status = aac_dev_ioctl(dev, cmd, arg);
735 if(status != -ENOTTY)
739 case FSACTL_MINIPORT_REV_CHECK:
740 status = check_revision(dev, arg);
742 case FSACTL_SEND_LARGE_FIB:
744 status = ioctl_send_fib(dev, arg);
746 case FSACTL_OPEN_GET_ADAPTER_FIB:
747 status = open_getadapter_fib(dev, arg);
749 case FSACTL_GET_NEXT_ADAPTER_FIB:
750 status = next_getadapter_fib(dev, arg);
752 case FSACTL_CLOSE_GET_ADAPTER_FIB:
753 status = close_getadapter_fib(dev, arg);
755 case FSACTL_SEND_RAW_SRB:
756 status = aac_send_raw_srb(dev,arg);
758 case FSACTL_GET_PCI_INFO:
759 status = aac_get_pci_info(dev,arg);