patch-2_6_7-vs1_9_1_12
[linux-2.6.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 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/sched.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/blkdev.h>
41 #include <asm/semaphore.h>
42 #include <asm/uaccess.h>
43
44 #include "aacraid.h"
45
46 /**
47  *      ioctl_send_fib  -       send a FIB from userspace
48  *      @dev:   adapter is being processed
49  *      @arg:   arguments to the ioctl call
50  *      
51  *      This routine sends a fib to the adapter on behalf of a user level
52  *      program.
53  */
54  
55 static int ioctl_send_fib(struct aac_dev * dev, void *arg)
56 {
57         struct hw_fib * kfib;
58         struct fib *fibptr;
59
60         fibptr = fib_alloc(dev);
61         if(fibptr == NULL)
62                 return -ENOMEM;
63                 
64         kfib = fibptr->hw_fib;
65         /*
66          *      First copy in the header so that we can check the size field.
67          */
68         if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
69                 fib_free(fibptr);
70                 return -EFAULT;
71         }
72         /*
73          *      Since we copy based on the fib header size, make sure that we
74          *      will not overrun the buffer when we copy the memory. Return
75          *      an error if we would.
76          */
77         if(le32_to_cpu(kfib->header.Size) > sizeof(struct hw_fib) - sizeof(struct aac_fibhdr)) {
78                 fib_free(fibptr);
79                 return -EINVAL;
80         }
81
82         if (copy_from_user((void *) kfib, arg, le32_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr))) {
83                 fib_free(fibptr);
84                 return -EFAULT;
85         }
86
87         if (kfib->header.Command == cpu_to_le32(TakeABreakPt)) {
88                 aac_adapter_interrupt(dev);
89                 /*
90                  * Since we didn't really send a fib, zero out the state to allow 
91                  * cleanup code not to assert.
92                  */
93                 kfib->header.XferState = 0;
94         } else {
95                 if (fib_send(kfib->header.Command, fibptr, le32_to_cpu(kfib->header.Size) , FsaNormal,
96                         1, 1, NULL, NULL) != 0) 
97                 {
98                         fib_free(fibptr);
99                         return -EINVAL;
100                 }
101                 if (fib_complete(fibptr) != 0) {
102                         fib_free(fibptr);
103                         return -EINVAL;
104                 }
105         }
106         /*
107          *      Make sure that the size returned by the adapter (which includes
108          *      the header) is less than or equal to the size of a fib, so we
109          *      don't corrupt application data. Then copy that size to the user
110          *      buffer. (Don't try to add the header information again, since it
111          *      was already included by the adapter.)
112          */
113
114         if (copy_to_user(arg, (void *)kfib, kfib->header.Size)) {
115                 fib_free(fibptr);
116                 return -EFAULT;
117         }
118         fib_free(fibptr);
119         return 0;
120 }
121
122 /**
123  *      open_getadapter_fib     -       Get the next fib
124  *
125  *      This routine will get the next Fib, if available, from the AdapterFibContext
126  *      passed in from the user.
127  */
128
129 static int open_getadapter_fib(struct aac_dev * dev, void *arg)
130 {
131         struct aac_fib_context * fibctx;
132         int status;
133         unsigned long flags;
134
135         fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
136         if (fibctx == NULL) {
137                 status = -ENOMEM;
138         } else {
139                 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
140                 fibctx->size = sizeof(struct aac_fib_context);
141                 /*
142                  *      Initialize the mutex used to wait for the next AIF.
143                  */
144                 init_MUTEX_LOCKED(&fibctx->wait_sem);
145                 fibctx->wait = 0;
146                 /*
147                  *      Initialize the fibs and set the count of fibs on
148                  *      the list to 0.
149                  */
150                 fibctx->count = 0;
151                 INIT_LIST_HEAD(&fibctx->fib_list);
152                 fibctx->jiffies = jiffies/HZ;
153                 /*
154                  *      Now add this context onto the adapter's 
155                  *      AdapterFibContext list.
156                  */
157                 spin_lock_irqsave(&dev->fib_lock, flags);
158                 list_add_tail(&fibctx->next, &dev->fib_list);
159                 spin_unlock_irqrestore(&dev->fib_lock, flags);
160                 if (copy_to_user(arg,  &fibctx, sizeof(struct aac_fib_context *))) {
161                         status = -EFAULT;
162                 } else {
163                         status = 0;
164                 }       
165         }
166         return status;
167 }
168
169 /**
170  *      next_getadapter_fib     -       get the next fib
171  *      @dev: adapter to use
172  *      @arg: ioctl argument
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 next_getadapter_fib(struct aac_dev * dev, void *arg)
179 {
180         struct fib_ioctl f;
181         struct aac_fib_context *fibctx, *aifcp;
182         struct fib *fib;
183         int status;
184         struct list_head * entry;
185         int found;
186         unsigned long flags;
187         
188         if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
189                 return -EFAULT;
190         /*
191          *      Extract the AdapterFibContext from the Input parameters.
192          */
193         fibctx = (struct aac_fib_context *) f.fibctx;
194
195         /*
196          *      Verify that the HANDLE passed in was a valid AdapterFibContext
197          *
198          *      Search the list of AdapterFibContext addresses on the adapter
199          *      to be sure this is a valid address
200          */
201         found = 0;
202         entry = dev->fib_list.next;
203
204         while(entry != &dev->fib_list) {
205                 aifcp = list_entry(entry, struct aac_fib_context, next);
206                 if(fibctx == aifcp) {   /* We found a winner */
207                         found = 1;
208                         break;
209                 }
210                 entry = entry->next;
211         }
212         if (found == 0)
213                 return -EINVAL;
214
215         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
216                  (fibctx->size != sizeof(struct aac_fib_context)))
217                 return -EINVAL;
218         status = 0;
219         spin_lock_irqsave(&dev->fib_lock, flags);
220         /*
221          *      If there are no fibs to send back, then either wait or return
222          *      -EAGAIN
223          */
224 return_fib:
225         if (!list_empty(&fibctx->fib_list)) {
226                 struct list_head * entry;
227                 /*
228                  *      Pull the next fib from the fibs
229                  */
230                 entry = fibctx->fib_list.next;
231                 list_del(entry);
232                 
233                 fib = list_entry(entry, struct fib, fiblink);
234                 fibctx->count--;
235                 spin_unlock_irqrestore(&dev->fib_lock, flags);
236                 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
237                         kfree(fib->hw_fib);
238                         kfree(fib);
239                         return -EFAULT;
240                 }       
241                 /*
242                  *      Free the space occupied by this copy of the fib.
243                  */
244                 kfree(fib->hw_fib);
245                 kfree(fib);
246                 status = 0;
247                 fibctx->jiffies = jiffies/HZ;
248         } else {
249                 spin_unlock_irqrestore(&dev->fib_lock, flags);
250                 if (f.wait) {
251                         if(down_interruptible(&fibctx->wait_sem) < 0) {
252                                 status = -EINTR;
253                         } else {
254                                 /* Lock again and retry */
255                                 spin_lock_irqsave(&dev->fib_lock, flags);
256                                 goto return_fib;
257                         }
258                 } else {
259                         status = -EAGAIN;
260                 }       
261         }
262         return status;
263 }
264
265 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
266 {
267         struct fib *fib;
268
269         /*
270          *      First free any FIBs that have not been consumed.
271          */
272         while (!list_empty(&fibctx->fib_list)) {
273                 struct list_head * entry;
274                 /*
275                  *      Pull the next fib from the fibs
276                  */
277                 entry = fibctx->fib_list.next;
278                 list_del(entry);
279                 fib = list_entry(entry, struct fib, fiblink);
280                 fibctx->count--;
281                 /*
282                  *      Free the space occupied by this copy of the fib.
283                  */
284                 kfree(fib->hw_fib);
285                 kfree(fib);
286         }
287         /*
288          *      Remove the Context from the AdapterFibContext List
289          */
290         list_del(&fibctx->next);
291         /*
292          *      Invalidate context
293          */
294         fibctx->type = 0;
295         /*
296          *      Free the space occupied by the Context
297          */
298         kfree(fibctx);
299         return 0;
300 }
301
302 /**
303  *      close_getadapter_fib    -       close down user fib context
304  *      @dev: adapter
305  *      @arg: ioctl arguments
306  *
307  *      This routine will close down the fibctx passed in from the user.
308  */
309  
310 static int close_getadapter_fib(struct aac_dev * dev, void *arg)
311 {
312         struct aac_fib_context *fibctx, *aifcp;
313         int status;
314         unsigned long flags;
315         struct list_head * entry;
316         int found;
317
318         /*
319          *      Extract the fibctx from the input parameters
320          */
321         fibctx = arg;
322
323         /*
324          *      Verify that the HANDLE passed in was a valid AdapterFibContext
325          *
326          *      Search the list of AdapterFibContext addresses on the adapter
327          *      to be sure this is a valid address
328          */
329
330         found = 0;
331         entry = dev->fib_list.next;
332
333         while(entry != &dev->fib_list) {
334                 aifcp = list_entry(entry, struct aac_fib_context, next);
335                 if(fibctx == aifcp) {   /* We found a winner */
336                         found = 1;
337                         break;
338                 }
339                 entry = entry->next;
340         }
341
342         if(found == 0)
343                 return 0; /* Already gone */
344
345         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
346                  (fibctx->size != sizeof(struct aac_fib_context)))
347                 return -EINVAL;
348         spin_lock_irqsave(&dev->fib_lock, flags);
349         status = aac_close_fib_context(dev, fibctx);
350         spin_unlock_irqrestore(&dev->fib_lock, flags);
351         return status;
352 }
353
354 /**
355  *      check_revision  -       close down user fib context
356  *      @dev: adapter
357  *      @arg: ioctl arguments
358  *
359  *      This routine returns the firmware version.
360  *      Under Linux, there have been no version incompatibilities, so this is simple!
361  */
362
363 static int check_revision(struct aac_dev *dev, void *arg)
364 {
365         struct revision response;
366
367         response.compat = 1;
368         response.version = dev->adapter_info.kernelrev;
369         response.build = dev->adapter_info.kernelbuild;
370
371         if (copy_to_user(arg, &response, sizeof(response)))
372                 return -EFAULT;
373         return 0;
374 }
375
376 /**
377  *
378  * aac_send_raw_scb
379  *
380  */
381
382 int aac_send_raw_srb(struct aac_dev* dev, void* arg)
383 {
384         struct fib* srbfib;
385         int status;
386         struct aac_srb *srbcmd;
387         struct aac_srb *user_srb = arg;
388         struct aac_srb_reply* user_reply;
389         struct aac_srb_reply* reply;
390         u32 fibsize = 0;
391         u32 flags = 0;
392         s32 rcode = 0;
393         u32 data_dir;
394         ulong sg_user[32];
395         ulong sg_list[32];
396         u32   sg_indx = 0;
397         u32 byte_count = 0;
398         u32 actual_fibsize = 0;
399         int i;
400
401
402         if (!capable(CAP_SYS_ADMIN)){
403                 printk(KERN_DEBUG"aacraid: No permission to send raw srb\n"); 
404                 return -EPERM;
405         }
406         /*
407          *      Allocate and initialize a Fib then setup a BlockWrite command
408          */
409         if (!(srbfib = fib_alloc(dev))) {
410                 return -1;
411         }
412         fib_init(srbfib);
413
414         srbcmd = (struct aac_srb*) fib_data(srbfib);
415
416         if(copy_from_user((void*)&fibsize, (void*)&user_srb->count,sizeof(u32))){
417                 printk(KERN_DEBUG"aacraid: Could not copy data size from user\n"); 
418                 rcode = -EFAULT;
419                 goto cleanup;
420         }
421
422         if(copy_from_user(srbcmd, user_srb,fibsize)){
423                 printk(KERN_DEBUG"aacraid: Could not copy srb from user\n"); 
424                 rcode = -EFAULT;
425                 goto cleanup;
426         }
427
428         user_reply = arg+fibsize;
429
430         flags = srbcmd->flags;
431         // Fix up srb for endian and force some values
432         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);       // Force this
433         srbcmd->channel  = cpu_to_le32(srbcmd->channel);
434         srbcmd->target   = cpu_to_le32(srbcmd->target);
435         srbcmd->lun      = cpu_to_le32(srbcmd->lun);
436         srbcmd->flags    = cpu_to_le32(srbcmd->flags);
437         srbcmd->timeout  = cpu_to_le32(srbcmd->timeout);
438         srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
439         srbcmd->cdb_size = cpu_to_le32(srbcmd->cdb_size);
440         
441         switch(srbcmd->flags){
442         case SRB_DataOut:
443                 data_dir = DMA_TO_DEVICE;
444                 break;
445         case (SRB_DataIn | SRB_DataOut):
446                 data_dir = DMA_BIDIRECTIONAL;
447                 break;
448         case SRB_DataIn:
449                 data_dir = DMA_FROM_DEVICE;
450                 break;
451         default:
452                 data_dir = DMA_NONE;
453         }
454         if( dev->pae_support ==1 ) {
455                 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
456                 byte_count = 0;
457
458                 // This should also catch if user used the 32 bit sgmap
459                 actual_fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry64));
460                 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
461                         printk(KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n");
462                         rcode = -EINVAL;
463                         goto cleanup;
464                 }
465
466                 for (i = 0; i < psg->count; i++) {
467                         dma_addr_t addr; 
468                         u64 le_addr;
469                         void* p;
470                         p = kmalloc(psg->sg[i].count,GFP_KERNEL|__GFP_DMA);
471                         if(p == 0) {
472                                 printk(KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
473                                 psg->sg[i].count,i,psg->count);
474                                 rcode = -ENOMEM;
475                                 goto cleanup;
476                         }
477                         sg_user[i] = (ulong)psg->sg[i].addr;
478                         sg_list[i] = (ulong)p; // save so we can clean up later
479                         sg_indx = i;
480
481                         if( flags & SRB_DataOut ){
482                                 if(copy_from_user(p,psg->sg[i].addr,psg->sg[i].count)){
483                                         printk(KERN_DEBUG"aacraid: Could not copy sg data from user\n"); 
484                                         rcode = -EFAULT;
485                                         goto cleanup;
486                                 }
487                         }
488                         addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
489
490                         le_addr = cpu_to_le64(addr);
491                         psg->sg[i].addr[1] = (u32)(le_addr>>32);
492                         psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
493                         psg->sg[i].count = cpu_to_le32(psg->sg[i].count);  
494                         byte_count += psg->sg[i].count;
495                 }
496
497                 srbcmd->count = cpu_to_le32(byte_count);
498                 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,0,0);
499         } else {
500                 struct sgmap* psg = &srbcmd->sg;
501                 byte_count = 0;
502
503                 actual_fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
504                 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
505                         printk(KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n");
506                         rcode = -EINVAL;
507                         goto cleanup;
508                 }
509                 for (i = 0; i < psg->count; i++) {
510                         dma_addr_t addr; 
511                         void* p;
512                         p = kmalloc(psg->sg[i].count,GFP_KERNEL);
513                         if(p == 0) {
514                                 printk(KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
515                                 psg->sg[i].count,i,psg->count);
516                                 rcode = -ENOMEM;
517                                 goto cleanup;
518                         }
519                         sg_user[i] = (ulong)(psg->sg[i].addr);
520                         sg_list[i] = (ulong)p; // save so we can clean up later
521                         sg_indx = i;
522
523                         if( flags & SRB_DataOut ){
524                                 if(copy_from_user((void*)p,(void*)(ulong)(psg->sg[i].addr),psg->sg[i].count)){
525                                         printk(KERN_DEBUG"aacraid: Could not copy sg data from user\n"); 
526                                         rcode = -EFAULT;
527                                         goto cleanup;
528                                 }
529                         }
530                         addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
531
532                         psg->sg[i].addr = cpu_to_le32(addr);
533                         psg->sg[i].count = cpu_to_le32(psg->sg[i].count);  
534                         byte_count += psg->sg[i].count;
535                 }
536                 srbcmd->count = cpu_to_le32(byte_count);
537                 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, 0, 0);
538         }
539
540         if (status != 0){
541                 printk(KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"); 
542                 rcode = -1;
543                 goto cleanup;
544         }
545
546         if( flags & SRB_DataIn ) {
547                 for(i = 0 ; i <= sg_indx; i++){
548                         if(copy_to_user((void*)(sg_user[i]),(void*)(sg_list[i]),le32_to_cpu(srbcmd->sg.sg[i].count))){
549                                 printk(KERN_DEBUG"aacraid: Could not copy sg data to user\n"); 
550                                 rcode = -EFAULT;
551                                 goto cleanup;
552
553                         }
554                 }
555         }
556
557         reply = (struct aac_srb_reply *) fib_data(srbfib);
558         if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
559                 printk(KERN_DEBUG"aacraid: Could not copy reply to user\n"); 
560                 rcode = -EFAULT;
561                 goto cleanup;
562         }
563
564 cleanup:
565         for(i=0; i <= sg_indx; i++){
566                 kfree((void*)sg_list[i]);
567         }
568         fib_complete(srbfib);
569         fib_free(srbfib);
570
571         return rcode;
572 }
573
574
575 struct aac_pci_info {
576         u32 bus;
577         u32 slot;
578 };
579
580
581 int aac_get_pci_info(struct aac_dev* dev, void* arg)
582 {
583         struct aac_pci_info pci_info;
584
585         pci_info.bus = dev->pdev->bus->number;
586         pci_info.slot = PCI_SLOT(dev->pdev->devfn);
587
588        if(copy_to_user( arg, (void*)&pci_info, sizeof(struct aac_pci_info))){
589                 printk(KERN_DEBUG "aacraid: Could not copy pci info\n");
590                return -EFAULT;
591         }
592         return 0;
593  }
594  
595
596 int aac_do_ioctl(struct aac_dev * dev, int cmd, void *arg)
597 {
598         int status;
599         
600         /*
601          *      HBA gets first crack
602          */
603          
604         status = aac_dev_ioctl(dev, cmd, arg);
605         if(status != -ENOTTY)
606                 return status;
607
608         switch (cmd) {
609         case FSACTL_MINIPORT_REV_CHECK:
610                 status = check_revision(dev, arg);
611                 break;
612         case FSACTL_SENDFIB:
613                 status = ioctl_send_fib(dev, arg);
614                 break;
615         case FSACTL_OPEN_GET_ADAPTER_FIB:
616                 status = open_getadapter_fib(dev, arg);
617                 break;
618         case FSACTL_GET_NEXT_ADAPTER_FIB:
619                 status = next_getadapter_fib(dev, arg);
620                 break;
621         case FSACTL_CLOSE_GET_ADAPTER_FIB:
622                 status = close_getadapter_fib(dev, arg);
623                 break;
624         case FSACTL_SEND_RAW_SRB:
625                 status = aac_send_raw_srb(dev,arg);
626                 break;
627         case FSACTL_GET_PCI_INFO:
628                 status = aac_get_pci_info(dev,arg);
629                 break;
630         default:
631                 status = -ENOTTY;
632                 break;  
633         }
634         return status;
635 }
636