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