]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/aacraid/commctrl.c
xtensa process.c must #include <linux/fs.h>
[linux-2.6-omap-h63xx.git] / drivers / scsi / aacraid / commctrl.c
1 /*
2  *      Adaptec AAC series RAID controller driver
3  *      (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
4  *
5  * based on the old aacraid driver that is..
6  * Adaptec aacraid device driver for Linux.
7  *
8  * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
9  *
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)
13  * any later version.
14  *
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.
19  *
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.
23  *
24  * Module Name:
25  *  commctrl.c
26  *
27  * Abstract: Contains all routines for control of the AFA comm layer
28  *
29  */
30
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 <asm/semaphore.h>
43 #include <asm/uaccess.h>
44
45 #include "aacraid.h"
46
47 /**
48  *      ioctl_send_fib  -       send a FIB from userspace
49  *      @dev:   adapter is being processed
50  *      @arg:   arguments to the ioctl call
51  *      
52  *      This routine sends a fib to the adapter on behalf of a user level
53  *      program.
54  */
55 # define AAC_DEBUG_PREAMBLE     KERN_INFO
56 # define AAC_DEBUG_POSTAMBLE
57  
58 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
59 {
60         struct hw_fib * kfib;
61         struct fib *fibptr;
62         struct hw_fib * hw_fib = (struct hw_fib *)0;
63         dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
64         unsigned size;
65         int retval;
66
67         if (dev->in_reset) {
68                 return -EBUSY;
69         }
70         fibptr = aac_fib_alloc(dev);
71         if(fibptr == NULL) {
72                 return -ENOMEM;
73         }
74                 
75         kfib = fibptr->hw_fib_va;
76         /*
77          *      First copy in the header so that we can check the size field.
78          */
79         if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
80                 aac_fib_free(fibptr);
81                 return -EFAULT;
82         }
83         /*
84          *      Since we copy based on the fib header size, make sure that we
85          *      will not overrun the buffer when we copy the memory. Return
86          *      an error if we would.
87          */
88         size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
89         if (size < le16_to_cpu(kfib->header.SenderSize))
90                 size = le16_to_cpu(kfib->header.SenderSize);
91         if (size > dev->max_fib_size) {
92                 if (size > 2048) {
93                         retval = -EINVAL;
94                         goto cleanup;
95                 }
96                 /* Highjack the hw_fib */
97                 hw_fib = fibptr->hw_fib_va;
98                 hw_fib_pa = fibptr->hw_fib_pa;
99                 fibptr->hw_fib_va = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
100                 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
101                 memcpy(kfib, hw_fib, dev->max_fib_size);
102         }
103
104         if (copy_from_user(kfib, arg, size)) {
105                 retval = -EFAULT;
106                 goto cleanup;
107         }
108
109         if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
110                 aac_adapter_interrupt(dev);
111                 /*
112                  * Since we didn't really send a fib, zero out the state to allow 
113                  * cleanup code not to assert.
114                  */
115                 kfib->header.XferState = 0;
116         } else {
117                 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
118                                 le16_to_cpu(kfib->header.Size) , FsaNormal,
119                                 1, 1, NULL, NULL);
120                 if (retval) {
121                         goto cleanup;
122                 }
123                 if (aac_fib_complete(fibptr) != 0) {
124                         retval = -EINVAL;
125                         goto cleanup;
126                 }
127         }
128         /*
129          *      Make sure that the size returned by the adapter (which includes
130          *      the header) is less than or equal to the size of a fib, so we
131          *      don't corrupt application data. Then copy that size to the user
132          *      buffer. (Don't try to add the header information again, since it
133          *      was already included by the adapter.)
134          */
135
136         retval = 0;
137         if (copy_to_user(arg, (void *)kfib, size))
138                 retval = -EFAULT;
139 cleanup:
140         if (hw_fib) {
141                 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
142                 fibptr->hw_fib_pa = hw_fib_pa;
143                 fibptr->hw_fib_va = hw_fib;
144         }
145         if (retval != -EINTR)
146                 aac_fib_free(fibptr);
147         return retval;
148 }
149
150 /**
151  *      open_getadapter_fib     -       Get the next fib
152  *
153  *      This routine will get the next Fib, if available, from the AdapterFibContext
154  *      passed in from the user.
155  */
156
157 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
158 {
159         struct aac_fib_context * fibctx;
160         int status;
161
162         fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
163         if (fibctx == NULL) {
164                 status = -ENOMEM;
165         } else {
166                 unsigned long flags;
167                 struct list_head * entry;
168                 struct aac_fib_context * context;
169
170                 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
171                 fibctx->size = sizeof(struct aac_fib_context);
172                 /*
173                  *      Yes yes, I know this could be an index, but we have a
174                  * better guarantee of uniqueness for the locked loop below.
175                  * Without the aid of a persistent history, this also helps
176                  * reduce the chance that the opaque context would be reused.
177                  */
178                 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
179                 /*
180                  *      Initialize the mutex used to wait for the next AIF.
181                  */
182                 init_MUTEX_LOCKED(&fibctx->wait_sem);
183                 fibctx->wait = 0;
184                 /*
185                  *      Initialize the fibs and set the count of fibs on
186                  *      the list to 0.
187                  */
188                 fibctx->count = 0;
189                 INIT_LIST_HEAD(&fibctx->fib_list);
190                 fibctx->jiffies = jiffies/HZ;
191                 /*
192                  *      Now add this context onto the adapter's 
193                  *      AdapterFibContext list.
194                  */
195                 spin_lock_irqsave(&dev->fib_lock, flags);
196                 /* Ensure that we have a unique identifier */
197                 entry = dev->fib_list.next;
198                 while (entry != &dev->fib_list) {
199                         context = list_entry(entry, struct aac_fib_context, next);
200                         if (context->unique == fibctx->unique) {
201                                 /* Not unique (32 bits) */
202                                 fibctx->unique++;
203                                 entry = dev->fib_list.next;
204                         } else {
205                                 entry = entry->next;
206                         }
207                 }
208                 list_add_tail(&fibctx->next, &dev->fib_list);
209                 spin_unlock_irqrestore(&dev->fib_lock, flags);
210                 if (copy_to_user(arg,  &fibctx->unique, 
211                                                 sizeof(fibctx->unique))) {
212                         status = -EFAULT;
213                 } else {
214                         status = 0;
215                 }       
216         }
217         return status;
218 }
219
220 /**
221  *      next_getadapter_fib     -       get the next fib
222  *      @dev: adapter to use
223  *      @arg: ioctl argument
224  *      
225  *      This routine will get the next Fib, if available, from the AdapterFibContext
226  *      passed in from the user.
227  */
228
229 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
230 {
231         struct fib_ioctl f;
232         struct fib *fib;
233         struct aac_fib_context *fibctx;
234         int status;
235         struct list_head * entry;
236         unsigned long flags;
237         
238         if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
239                 return -EFAULT;
240         /*
241          *      Verify that the HANDLE passed in was a valid AdapterFibContext
242          *
243          *      Search the list of AdapterFibContext addresses on the adapter
244          *      to be sure this is a valid address
245          */
246         entry = dev->fib_list.next;
247         fibctx = NULL;
248
249         while (entry != &dev->fib_list) {
250                 fibctx = list_entry(entry, struct aac_fib_context, next);
251                 /*
252                  *      Extract the AdapterFibContext from the Input parameters.
253                  */
254                 if (fibctx->unique == f.fibctx) {   /* We found a winner */
255                         break;
256                 }
257                 entry = entry->next;
258                 fibctx = NULL;
259         }
260         if (!fibctx) {
261                 dprintk ((KERN_INFO "Fib Context not found\n"));
262                 return -EINVAL;
263         }
264
265         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
266                  (fibctx->size != sizeof(struct aac_fib_context))) {
267                 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
268                 return -EINVAL;
269         }
270         status = 0;
271         spin_lock_irqsave(&dev->fib_lock, flags);
272         /*
273          *      If there are no fibs to send back, then either wait or return
274          *      -EAGAIN
275          */
276 return_fib:
277         if (!list_empty(&fibctx->fib_list)) {
278                 struct list_head * entry;
279                 /*
280                  *      Pull the next fib from the fibs
281                  */
282                 entry = fibctx->fib_list.next;
283                 list_del(entry);
284                 
285                 fib = list_entry(entry, struct fib, fiblink);
286                 fibctx->count--;
287                 spin_unlock_irqrestore(&dev->fib_lock, flags);
288                 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
289                         kfree(fib->hw_fib_va);
290                         kfree(fib);
291                         return -EFAULT;
292                 }       
293                 /*
294                  *      Free the space occupied by this copy of the fib.
295                  */
296                 kfree(fib->hw_fib_va);
297                 kfree(fib);
298                 status = 0;
299         } else {
300                 spin_unlock_irqrestore(&dev->fib_lock, flags);
301                 /* If someone killed the AIF aacraid thread, restart it */
302                 status = !dev->aif_thread;
303                 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
304                         /* Be paranoid, be very paranoid! */
305                         kthread_stop(dev->thread);
306                         ssleep(1);
307                         dev->aif_thread = 0;
308                         dev->thread = kthread_run(aac_command_thread, dev, dev->name);
309                         ssleep(1);
310                 }
311                 if (f.wait) {
312                         if(down_interruptible(&fibctx->wait_sem) < 0) {
313                                 status = -EINTR;
314                         } else {
315                                 /* Lock again and retry */
316                                 spin_lock_irqsave(&dev->fib_lock, flags);
317                                 goto return_fib;
318                         }
319                 } else {
320                         status = -EAGAIN;
321                 }       
322         }
323         fibctx->jiffies = jiffies/HZ;
324         return status;
325 }
326
327 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
328 {
329         struct fib *fib;
330
331         /*
332          *      First free any FIBs that have not been consumed.
333          */
334         while (!list_empty(&fibctx->fib_list)) {
335                 struct list_head * entry;
336                 /*
337                  *      Pull the next fib from the fibs
338                  */
339                 entry = fibctx->fib_list.next;
340                 list_del(entry);
341                 fib = list_entry(entry, struct fib, fiblink);
342                 fibctx->count--;
343                 /*
344                  *      Free the space occupied by this copy of the fib.
345                  */
346                 kfree(fib->hw_fib_va);
347                 kfree(fib);
348         }
349         /*
350          *      Remove the Context from the AdapterFibContext List
351          */
352         list_del(&fibctx->next);
353         /*
354          *      Invalidate context
355          */
356         fibctx->type = 0;
357         /*
358          *      Free the space occupied by the Context
359          */
360         kfree(fibctx);
361         return 0;
362 }
363
364 /**
365  *      close_getadapter_fib    -       close down user fib context
366  *      @dev: adapter
367  *      @arg: ioctl arguments
368  *
369  *      This routine will close down the fibctx passed in from the user.
370  */
371  
372 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
373 {
374         struct aac_fib_context *fibctx;
375         int status;
376         unsigned long flags;
377         struct list_head * entry;
378
379         /*
380          *      Verify that the HANDLE passed in was a valid AdapterFibContext
381          *
382          *      Search the list of AdapterFibContext addresses on the adapter
383          *      to be sure this is a valid address
384          */
385
386         entry = dev->fib_list.next;
387         fibctx = NULL;
388
389         while(entry != &dev->fib_list) {
390                 fibctx = list_entry(entry, struct aac_fib_context, next);
391                 /*
392                  *      Extract the fibctx from the input parameters
393                  */
394                 if (fibctx->unique == (u32)(ptrdiff_t)arg) /* We found a winner */
395                         break;
396                 entry = entry->next;
397                 fibctx = NULL;
398         }
399
400         if (!fibctx)
401                 return 0; /* Already gone */
402
403         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
404                  (fibctx->size != sizeof(struct aac_fib_context)))
405                 return -EINVAL;
406         spin_lock_irqsave(&dev->fib_lock, flags);
407         status = aac_close_fib_context(dev, fibctx);
408         spin_unlock_irqrestore(&dev->fib_lock, flags);
409         return status;
410 }
411
412 /**
413  *      check_revision  -       close down user fib context
414  *      @dev: adapter
415  *      @arg: ioctl arguments
416  *
417  *      This routine returns the driver version.
418  *      Under Linux, there have been no version incompatibilities, so this is 
419  *      simple!
420  */
421
422 static int check_revision(struct aac_dev *dev, void __user *arg)
423 {
424         struct revision response;
425         char *driver_version = aac_driver_version;
426         u32 version;
427
428         response.compat = 1;
429         version = (simple_strtol(driver_version, 
430                                 &driver_version, 10) << 24) | 0x00000400;
431         version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
432         version += simple_strtol(driver_version + 1, NULL, 10);
433         response.version = cpu_to_le32(version);
434 #       if (defined(AAC_DRIVER_BUILD))
435                 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
436 #       else
437                 response.build = cpu_to_le32(9999);
438 #       endif
439
440         if (copy_to_user(arg, &response, sizeof(response)))
441                 return -EFAULT;
442         return 0;
443 }
444
445
446 /**
447  *
448  * aac_send_raw_scb
449  *
450  */
451
452 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
453 {
454         struct fib* srbfib;
455         int status;
456         struct aac_srb *srbcmd = NULL;
457         struct user_aac_srb *user_srbcmd = NULL;
458         struct user_aac_srb __user *user_srb = arg;
459         struct aac_srb_reply __user *user_reply;
460         struct aac_srb_reply* reply;
461         u32 fibsize = 0;
462         u32 flags = 0;
463         s32 rcode = 0;
464         u32 data_dir;
465         void __user *sg_user[32];
466         void *sg_list[32];
467         u32   sg_indx = 0;
468         u32 byte_count = 0;
469         u32 actual_fibsize64, actual_fibsize = 0;
470         int i;
471
472
473         if (dev->in_reset) {
474                 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
475                 return -EBUSY;
476         }
477         if (!capable(CAP_SYS_ADMIN)){
478                 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n")); 
479                 return -EPERM;
480         }
481         /*
482          *      Allocate and initialize a Fib then setup a SRB command
483          */
484         if (!(srbfib = aac_fib_alloc(dev))) {
485                 return -ENOMEM;
486         }
487         aac_fib_init(srbfib);
488
489         srbcmd = (struct aac_srb*) fib_data(srbfib);
490
491         memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
492         if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
493                 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n")); 
494                 rcode = -EFAULT;
495                 goto cleanup;
496         }
497
498         if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
499                 rcode = -EINVAL;
500                 goto cleanup;
501         }
502
503         user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
504         if (!user_srbcmd) {
505                 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
506                 rcode = -ENOMEM;
507                 goto cleanup;
508         }
509         if(copy_from_user(user_srbcmd, user_srb,fibsize)){
510                 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n")); 
511                 rcode = -EFAULT;
512                 goto cleanup;
513         }
514
515         user_reply = arg+fibsize;
516
517         flags = user_srbcmd->flags; /* from user in cpu order */
518         // Fix up srb for endian and force some values
519
520         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);       // Force this
521         srbcmd->channel  = cpu_to_le32(user_srbcmd->channel);
522         srbcmd->id       = cpu_to_le32(user_srbcmd->id);
523         srbcmd->lun      = cpu_to_le32(user_srbcmd->lun);
524         srbcmd->timeout  = cpu_to_le32(user_srbcmd->timeout);
525         srbcmd->flags    = cpu_to_le32(flags);
526         srbcmd->retry_limit = 0; // Obsolete parameter
527         srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
528         memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
529         
530         switch (flags & (SRB_DataIn | SRB_DataOut)) {
531         case SRB_DataOut:
532                 data_dir = DMA_TO_DEVICE;
533                 break;
534         case (SRB_DataIn | SRB_DataOut):
535                 data_dir = DMA_BIDIRECTIONAL;
536                 break;
537         case SRB_DataIn:
538                 data_dir = DMA_FROM_DEVICE;
539                 break;
540         default:
541                 data_dir = DMA_NONE;
542         }
543         if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
544                 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
545                   le32_to_cpu(srbcmd->sg.count)));
546                 rcode = -EINVAL;
547                 goto cleanup;
548         }
549         actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
550                 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
551         actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
552           (sizeof(struct sgentry64) - sizeof(struct sgentry));
553         /* User made a mistake - should not continue */
554         if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
555                 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
556                   "Raw SRB command calculated fibsize=%lu;%lu "
557                   "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
558                   "issued fibsize=%d\n",
559                   actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
560                   sizeof(struct aac_srb), sizeof(struct sgentry),
561                   sizeof(struct sgentry64), fibsize));
562                 rcode = -EINVAL;
563                 goto cleanup;
564         }
565         if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
566                 dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
567                 rcode = -EINVAL;
568                 goto cleanup;
569         }
570         byte_count = 0;
571         if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
572                 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
573                 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
574
575                 /*
576                  * This should also catch if user used the 32 bit sgmap
577                  */
578                 if (actual_fibsize64 == fibsize) {
579                         actual_fibsize = actual_fibsize64;
580                         for (i = 0; i < upsg->count; i++) {
581                                 u64 addr;
582                                 void* p;
583                                 /* Does this really need to be GFP_DMA? */
584                                 p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
585                                 if(p == 0) {
586                                         dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
587                                           upsg->sg[i].count,i,upsg->count));
588                                         rcode = -ENOMEM;
589                                         goto cleanup;
590                                 }
591                                 addr = (u64)upsg->sg[i].addr[0];
592                                 addr += ((u64)upsg->sg[i].addr[1]) << 32;
593                                 sg_user[i] = (void __user *)(ptrdiff_t)addr;
594                                 sg_list[i] = p; // save so we can clean up later
595                                 sg_indx = i;
596
597                                 if( flags & SRB_DataOut ){
598                                         if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
599                                                 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
600                                                 rcode = -EFAULT;
601                                                 goto cleanup;
602                                         }
603                                 }
604                                 addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
605
606                                 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
607                                 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
608                                 byte_count += upsg->sg[i].count;
609                                 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
610                         }
611                 } else {
612                         struct user_sgmap* usg;
613                         usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
614                           + sizeof(struct sgmap), GFP_KERNEL);
615                         if (!usg) {
616                                 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
617                                 rcode = -ENOMEM;
618                                 goto cleanup;
619                         }
620                         memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
621                           + sizeof(struct sgmap));
622                         actual_fibsize = actual_fibsize64;
623
624                         for (i = 0; i < usg->count; i++) {
625                                 u64 addr;
626                                 void* p;
627                                 /* Does this really need to be GFP_DMA? */
628                                 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
629                                 if(p == 0) {
630                                         kfree (usg);
631                                         dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
632                                           usg->sg[i].count,i,usg->count));
633                                         rcode = -ENOMEM;
634                                         goto cleanup;
635                                 }
636                                 sg_user[i] = (void __user *)(ptrdiff_t)usg->sg[i].addr;
637                                 sg_list[i] = p; // save so we can clean up later
638                                 sg_indx = i;
639
640                                 if( flags & SRB_DataOut ){
641                                         if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
642                                                 kfree (usg);
643                                                 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
644                                                 rcode = -EFAULT;
645                                                 goto cleanup;
646                                         }
647                                 }
648                                 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
649
650                                 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
651                                 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
652                                 byte_count += usg->sg[i].count;
653                                 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
654                         }
655                         kfree (usg);
656                 }
657                 srbcmd->count = cpu_to_le32(byte_count);
658                 psg->count = cpu_to_le32(sg_indx+1);
659                 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
660         } else {
661                 struct user_sgmap* upsg = &user_srbcmd->sg;
662                 struct sgmap* psg = &srbcmd->sg;
663
664                 if (actual_fibsize64 == fibsize) {
665                         struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
666                         for (i = 0; i < upsg->count; i++) {
667                                 u64 addr;
668                                 void* p;
669                                 /* Does this really need to be GFP_DMA? */
670                                 p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
671                                 if(p == 0) {
672                                         dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
673                                           usg->sg[i].count,i,usg->count));
674                                         rcode = -ENOMEM;
675                                         goto cleanup;
676                                 }
677                                 addr = (u64)usg->sg[i].addr[0];
678                                 addr += ((u64)usg->sg[i].addr[1]) << 32;
679                                 sg_user[i] = (void __user *)(ptrdiff_t)addr;
680                                 sg_list[i] = p; // save so we can clean up later
681                                 sg_indx = i;
682
683                                 if( flags & SRB_DataOut ){
684                                         if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
685                                                 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
686                                                 rcode = -EFAULT;
687                                                 goto cleanup;
688                                         }
689                                 }
690                                 addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
691
692                                 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
693                                 byte_count += usg->sg[i].count;
694                                 psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
695                         }
696                 } else {
697                         for (i = 0; i < upsg->count; i++) {
698                                 dma_addr_t addr;
699                                 void* p;
700                                 p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
701                                 if(p == 0) {
702                                         dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
703                                           upsg->sg[i].count, i, upsg->count));
704                                         rcode = -ENOMEM;
705                                         goto cleanup;
706                                 }
707                                 sg_user[i] = (void __user *)(ptrdiff_t)upsg->sg[i].addr;
708                                 sg_list[i] = p; // save so we can clean up later
709                                 sg_indx = i;
710
711                                 if( flags & SRB_DataOut ){
712                                         if(copy_from_user(p, sg_user[i],
713                                                         upsg->sg[i].count)) {
714                                                 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
715                                                 rcode = -EFAULT;
716                                                 goto cleanup;
717                                         }
718                                 }
719                                 addr = pci_map_single(dev->pdev, p,
720                                         upsg->sg[i].count, data_dir);
721
722                                 psg->sg[i].addr = cpu_to_le32(addr);
723                                 byte_count += upsg->sg[i].count;
724                                 psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
725                         }
726                 }
727                 srbcmd->count = cpu_to_le32(byte_count);
728                 psg->count = cpu_to_le32(sg_indx+1);
729                 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
730         }
731         if (status == -EINTR) {
732                 rcode = -EINTR;
733                 goto cleanup;
734         }
735
736         if (status != 0){
737                 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n")); 
738                 rcode = -ENXIO;
739                 goto cleanup;
740         }
741
742         if( flags & SRB_DataIn ) {
743                 for(i = 0 ; i <= sg_indx; i++){
744                         byte_count = le32_to_cpu(
745                           (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
746                               ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
747                               : srbcmd->sg.sg[i].count);
748                         if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
749                                 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n")); 
750                                 rcode = -EFAULT;
751                                 goto cleanup;
752
753                         }
754                 }
755         }
756
757         reply = (struct aac_srb_reply *) fib_data(srbfib);
758         if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
759                 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n")); 
760                 rcode = -EFAULT;
761                 goto cleanup;
762         }
763
764 cleanup:
765         kfree(user_srbcmd);
766         for(i=0; i <= sg_indx; i++){
767                 kfree(sg_list[i]);
768         }
769         if (rcode != -EINTR) {
770                 aac_fib_complete(srbfib);
771                 aac_fib_free(srbfib);
772         }
773
774         return rcode;
775 }
776
777 struct aac_pci_info {
778         u32 bus;
779         u32 slot;
780 };
781
782
783 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
784 {
785         struct aac_pci_info pci_info;
786
787         pci_info.bus = dev->pdev->bus->number;
788         pci_info.slot = PCI_SLOT(dev->pdev->devfn);
789
790        if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
791                dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
792                return -EFAULT;
793         }
794         return 0;
795 }
796  
797
798 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
799 {
800         int status;
801         
802         /*
803          *      HBA gets first crack
804          */
805          
806         status = aac_dev_ioctl(dev, cmd, arg);
807         if(status != -ENOTTY)
808                 return status;
809
810         switch (cmd) {
811         case FSACTL_MINIPORT_REV_CHECK:
812                 status = check_revision(dev, arg);
813                 break;
814         case FSACTL_SEND_LARGE_FIB:
815         case FSACTL_SENDFIB:
816                 status = ioctl_send_fib(dev, arg);
817                 break;
818         case FSACTL_OPEN_GET_ADAPTER_FIB:
819                 status = open_getadapter_fib(dev, arg);
820                 break;
821         case FSACTL_GET_NEXT_ADAPTER_FIB:
822                 status = next_getadapter_fib(dev, arg);
823                 break;
824         case FSACTL_CLOSE_GET_ADAPTER_FIB:
825                 status = close_getadapter_fib(dev, arg);
826                 break;
827         case FSACTL_SEND_RAW_SRB:
828                 status = aac_send_raw_srb(dev,arg);
829                 break;
830         case FSACTL_GET_PCI_INFO:
831                 status = aac_get_pci_info(dev,arg);
832                 break;
833         default:
834                 status = -ENOTTY;
835                 break;  
836         }
837         return status;
838 }
839