ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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                 AAC_INIT_LIST_HEAD(&fibctx->hw_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 hw_fib * hw_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 (!aac_list_empty(&fibctx->hw_fib_list)) {
226                 struct aac_list_head * entry;
227                 /*
228                  *      Pull the next fib from the fibs
229                  */
230                 entry = (struct aac_list_head*)(ulong)fibctx->hw_fib_list.next;
231                 aac_list_del(entry);
232                 
233                 hw_fib = aac_list_entry(entry, struct hw_fib, header.FibLinks);
234                 fibctx->count--;
235                 spin_unlock_irqrestore(&dev->fib_lock, flags);
236                 if (copy_to_user(f.fib, hw_fib, sizeof(struct hw_fib))) {
237                         kfree(hw_fib);
238                         return -EFAULT;
239                 }       
240                 /*
241                  *      Free the space occupied by this copy of the fib.
242                  */
243                 kfree(hw_fib);
244                 status = 0;
245                 fibctx->jiffies = jiffies/HZ;
246         } else {
247                 spin_unlock_irqrestore(&dev->fib_lock, flags);
248                 if (f.wait) {
249                         if(down_interruptible(&fibctx->wait_sem) < 0) {
250                                 status = -EINTR;
251                         } else {
252                                 /* Lock again and retry */
253                                 spin_lock_irqsave(&dev->fib_lock, flags);
254                                 goto return_fib;
255                         }
256                 } else {
257                         status = -EAGAIN;
258                 }       
259         }
260         return status;
261 }
262
263 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
264 {
265         struct hw_fib *hw_fib;
266
267         /*
268          *      First free any FIBs that have not been consumed.
269          */
270         while (!aac_list_empty(&fibctx->hw_fib_list)) {
271                 struct aac_list_head * entry;
272                 /*
273                  *      Pull the next fib from the fibs
274                  */
275                 entry = (struct aac_list_head*)(ulong)(fibctx->hw_fib_list.next);
276                 aac_list_del(entry);
277                 hw_fib = aac_list_entry(entry, struct hw_fib, header.FibLinks);
278                 fibctx->count--;
279                 /*
280                  *      Free the space occupied by this copy of the fib.
281                  */
282                 kfree(hw_fib);
283         }
284         /*
285          *      Remove the Context from the AdapterFibContext List
286          */
287         list_del(&fibctx->next);
288         /*
289          *      Invalidate context
290          */
291         fibctx->type = 0;
292         /*
293          *      Free the space occupied by the Context
294          */
295         kfree(fibctx);
296         return 0;
297 }
298
299 /**
300  *      close_getadapter_fib    -       close down user fib context
301  *      @dev: adapter
302  *      @arg: ioctl arguments
303  *
304  *      This routine will close down the fibctx passed in from the user.
305  */
306  
307 static int close_getadapter_fib(struct aac_dev * dev, void *arg)
308 {
309         struct aac_fib_context *fibctx, *aifcp;
310         int status;
311         unsigned long flags;
312         struct list_head * entry;
313         int found;
314
315         /*
316          *      Extract the fibctx from the input parameters
317          */
318         fibctx = arg;
319
320         /*
321          *      Verify that the HANDLE passed in was a valid AdapterFibContext
322          *
323          *      Search the list of AdapterFibContext addresses on the adapter
324          *      to be sure this is a valid address
325          */
326
327         found = 0;
328         entry = dev->fib_list.next;
329
330         while(entry != &dev->fib_list) {
331                 aifcp = list_entry(entry, struct aac_fib_context, next);
332                 if(fibctx == aifcp) {   /* We found a winner */
333                         found = 1;
334                         break;
335                 }
336                 entry = entry->next;
337         }
338
339         if(found == 0)
340                 return 0; /* Already gone */
341
342         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
343                  (fibctx->size != sizeof(struct aac_fib_context)))
344                 return -EINVAL;
345         spin_lock_irqsave(&dev->fib_lock, flags);
346         status = aac_close_fib_context(dev, fibctx);
347         spin_unlock_irqrestore(&dev->fib_lock, flags);
348         return status;
349 }
350
351 /**
352  *      check_revision  -       close down user fib context
353  *      @dev: adapter
354  *      @arg: ioctl arguments
355  *
356  *      This routine returns the firmware version.
357  *      Under Linux, there have been no version incompatibilities, so this is simple!
358  */
359
360 static int check_revision(struct aac_dev *dev, void *arg)
361 {
362         struct revision response;
363
364         response.compat = 1;
365         response.version = dev->adapter_info.kernelrev;
366         response.build = dev->adapter_info.kernelbuild;
367
368         if (copy_to_user(arg, &response, sizeof(response)))
369                 return -EFAULT;
370         return 0;
371 }
372
373 /**
374  *
375  * aac_send_raw_scb
376  *
377  */
378
379 int aac_send_raw_srb(struct aac_dev* dev, void* arg)
380 {
381         struct fib* srbfib;
382         int status;
383         struct aac_srb *srbcmd;
384         struct aac_srb *user_srb = arg;
385         struct aac_srb_reply* user_reply;
386         struct aac_srb_reply* reply;
387         u32 fibsize = 0;
388         u32 flags = 0;
389         s32 rcode = 0;
390         u32 data_dir;
391         ulong sg_user[32];
392         ulong sg_list[32];
393         u32   sg_indx = 0;
394         u32 byte_count = 0;
395         u32 actual_fibsize = 0;
396         int i;
397
398
399         if (!capable(CAP_SYS_ADMIN)){
400                 printk(KERN_DEBUG"aacraid: No permission to send raw srb\n"); 
401                 return -EPERM;
402         }
403         /*
404          *      Allocate and initialize a Fib then setup a BlockWrite command
405          */
406         if (!(srbfib = fib_alloc(dev))) {
407                 return -1;
408         }
409         fib_init(srbfib);
410
411         srbcmd = (struct aac_srb*) fib_data(srbfib);
412
413         if(copy_from_user((void*)&fibsize, (void*)&user_srb->count,sizeof(u32))){
414                 printk(KERN_DEBUG"aacraid: Could not copy data size from user\n"); 
415                 rcode = -EFAULT;
416                 goto cleanup;
417         }
418
419         if(copy_from_user(srbcmd, user_srb,fibsize)){
420                 printk(KERN_DEBUG"aacraid: Could not copy srb from user\n"); 
421                 rcode = -EFAULT;
422                 goto cleanup;
423         }
424
425         user_reply = arg+fibsize;
426
427         flags = srbcmd->flags;
428         // Fix up srb for endian and force some values
429         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);       // Force this
430         srbcmd->channel  = cpu_to_le32(srbcmd->channel);
431         srbcmd->target   = cpu_to_le32(srbcmd->target);
432         srbcmd->lun      = cpu_to_le32(srbcmd->lun);
433         srbcmd->flags    = cpu_to_le32(srbcmd->flags);
434         srbcmd->timeout  = cpu_to_le32(srbcmd->timeout);
435         srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
436         srbcmd->cdb_size = cpu_to_le32(srbcmd->cdb_size);
437         
438         switch(srbcmd->flags){
439         case SRB_DataOut:
440                 data_dir = DMA_TO_DEVICE;
441                 break;
442         case (SRB_DataIn | SRB_DataOut):
443                 data_dir = DMA_BIDIRECTIONAL;
444                 break;
445         case SRB_DataIn:
446                 data_dir = DMA_FROM_DEVICE;
447                 break;
448         default:
449                 data_dir = DMA_NONE;
450         }
451         if( dev->pae_support ==1 ) {
452                 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
453                 byte_count = 0;
454
455                 // This should also catch if user used the 32 bit sgmap
456                 actual_fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry64));
457                 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
458                         printk(KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n");
459                         rcode = -EINVAL;
460                         goto cleanup;
461                 }
462
463                 for (i = 0; i < psg->count; i++) {
464                         dma_addr_t addr; 
465                         u64 le_addr;
466                         void* p;
467                         p = kmalloc(psg->sg[i].count,GFP_KERNEL|__GFP_DMA);
468                         if(p == 0) {
469                                 printk(KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
470                                 psg->sg[i].count,i,psg->count);
471                                 rcode = -ENOMEM;
472                                 goto cleanup;
473                         }
474                         sg_user[i] = (ulong)psg->sg[i].addr;
475                         sg_list[i] = (ulong)p; // save so we can clean up later
476                         sg_indx = i;
477
478                         if( flags & SRB_DataOut ){
479                                 if(copy_from_user(p,psg->sg[i].addr,psg->sg[i].count)){
480                                         printk(KERN_DEBUG"aacraid: Could not copy sg data from user\n"); 
481                                         rcode = -EFAULT;
482                                         goto cleanup;
483                                 }
484                         }
485                         addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
486
487                         le_addr = cpu_to_le64(addr);
488                         psg->sg[i].addr[1] = (u32)(le_addr>>32);
489                         psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
490                         psg->sg[i].count = cpu_to_le32(psg->sg[i].count);  
491                         byte_count += psg->sg[i].count;
492                 }
493
494                 srbcmd->count = cpu_to_le32(byte_count);
495                 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,0,0);
496         } else {
497                 struct sgmap* psg = &srbcmd->sg;
498                 byte_count = 0;
499
500                 actual_fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
501                 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
502                         printk(KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n");
503                         rcode = -EINVAL;
504                         goto cleanup;
505                 }
506                 for (i = 0; i < psg->count; i++) {
507                         dma_addr_t addr; 
508                         void* p;
509                         p = kmalloc(psg->sg[i].count,GFP_KERNEL);
510                         if(p == 0) {
511                                 printk(KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
512                                 psg->sg[i].count,i,psg->count);
513                                 rcode = -ENOMEM;
514                                 goto cleanup;
515                         }
516                         sg_user[i] = (ulong)(psg->sg[i].addr);
517                         sg_list[i] = (ulong)p; // save so we can clean up later
518                         sg_indx = i;
519
520                         if( flags & SRB_DataOut ){
521                                 if(copy_from_user((void*)p,(void*)(ulong)(psg->sg[i].addr),psg->sg[i].count)){
522                                         printk(KERN_DEBUG"aacraid: Could not copy sg data from user\n"); 
523                                         rcode = -EFAULT;
524                                         goto cleanup;
525                                 }
526                         }
527                         addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
528
529                         psg->sg[i].addr = cpu_to_le32(addr);
530                         psg->sg[i].count = cpu_to_le32(psg->sg[i].count);  
531                         byte_count += psg->sg[i].count;
532                 }
533                 srbcmd->count = cpu_to_le32(byte_count);
534                 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, 0, 0);
535         }
536
537         if (status != 0){
538                 printk(KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"); 
539                 rcode = -1;
540                 goto cleanup;
541         }
542
543         if( flags & SRB_DataIn ) {
544                 for(i = 0 ; i <= sg_indx; i++){
545                         if(copy_to_user((void*)(sg_user[i]),(void*)(sg_list[i]),le32_to_cpu(srbcmd->sg.sg[i].count))){
546                                 printk(KERN_DEBUG"aacraid: Could not copy sg data to user\n"); 
547                                 rcode = -EFAULT;
548                                 goto cleanup;
549
550                         }
551                 }
552         }
553
554         reply = (struct aac_srb_reply *) fib_data(srbfib);
555         if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
556                 printk(KERN_DEBUG"aacraid: Could not copy reply to user\n"); 
557                 rcode = -EFAULT;
558                 goto cleanup;
559         }
560
561 cleanup:
562         for(i=0; i <= sg_indx; i++){
563                 kfree((void*)sg_list[i]);
564         }
565         fib_complete(srbfib);
566         fib_free(srbfib);
567
568         return rcode;
569 }
570
571
572 struct aac_pci_info {
573         u32 bus;
574         u32 slot;
575 };
576
577
578 int aac_get_pci_info(struct aac_dev* dev, void* arg)
579 {
580         struct aac_pci_info pci_info;
581
582         pci_info.bus = dev->pdev->bus->number;
583         pci_info.slot = PCI_SLOT(dev->pdev->devfn);
584
585        if(copy_to_user( arg, (void*)&pci_info, sizeof(struct aac_pci_info))){
586                 printk(KERN_DEBUG "aacraid: Could not copy pci info\n");
587                return -EFAULT;
588         }
589         return 0;
590  }
591  
592
593 int aac_do_ioctl(struct aac_dev * dev, int cmd, void *arg)
594 {
595         int status;
596         
597         /*
598          *      HBA gets first crack
599          */
600          
601         status = aac_dev_ioctl(dev, cmd, arg);
602         if(status != -ENOTTY)
603                 return status;
604
605         switch (cmd) {
606         case FSACTL_MINIPORT_REV_CHECK:
607                 status = check_revision(dev, arg);
608                 break;
609         case FSACTL_SENDFIB:
610                 status = ioctl_send_fib(dev, arg);
611                 break;
612         case FSACTL_OPEN_GET_ADAPTER_FIB:
613                 status = open_getadapter_fib(dev, arg);
614                 break;
615         case FSACTL_GET_NEXT_ADAPTER_FIB:
616                 status = next_getadapter_fib(dev, arg);
617                 break;
618         case FSACTL_CLOSE_GET_ADAPTER_FIB:
619                 status = close_getadapter_fib(dev, arg);
620                 break;
621         case FSACTL_SEND_RAW_SRB:
622                 status = aac_send_raw_srb(dev,arg);
623                 break;
624         case FSACTL_GET_PCI_INFO:
625                 status = aac_get_pci_info(dev,arg);
626                 break;
627         default:
628                 status = -ENOTTY;
629                 break;  
630         }
631         return status;
632 }
633