2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
8 * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Abstract: Contains all routines for control of the AFA comm layer
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/sched.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
37 #include <linux/slab.h>
38 #include <linux/completion.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/blkdev.h>
41 #include <asm/semaphore.h>
42 #include <asm/uaccess.h>
47 * ioctl_send_fib - send a FIB from userspace
48 * @dev: adapter is being processed
49 * @arg: arguments to the ioctl call
51 * This routine sends a fib to the adapter on behalf of a user level
55 static int ioctl_send_fib(struct aac_dev * dev, void *arg)
60 fibptr = fib_alloc(dev);
64 kfib = fibptr->hw_fib;
66 * First copy in the header so that we can check the size field.
68 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
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.
77 if(le32_to_cpu(kfib->header.Size) > sizeof(struct hw_fib) - sizeof(struct aac_fibhdr)) {
82 if (copy_from_user((void *) kfib, arg, le32_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr))) {
87 if (kfib->header.Command == cpu_to_le32(TakeABreakPt)) {
88 aac_adapter_interrupt(dev);
90 * Since we didn't really send a fib, zero out the state to allow
91 * cleanup code not to assert.
93 kfib->header.XferState = 0;
95 if (fib_send(kfib->header.Command, fibptr, le32_to_cpu(kfib->header.Size) , FsaNormal,
96 1, 1, NULL, NULL) != 0)
101 if (fib_complete(fibptr) != 0) {
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.)
114 if (copy_to_user(arg, (void *)kfib, kfib->header.Size)) {
123 * open_getadapter_fib - Get the next fib
125 * This routine will get the next Fib, if available, from the AdapterFibContext
126 * passed in from the user.
129 static int open_getadapter_fib(struct aac_dev * dev, void *arg)
131 struct aac_fib_context * fibctx;
135 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
136 if (fibctx == NULL) {
139 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
140 fibctx->size = sizeof(struct aac_fib_context);
142 * Initialize the mutex used to wait for the next AIF.
144 init_MUTEX_LOCKED(&fibctx->wait_sem);
147 * Initialize the fibs and set the count of fibs on
151 AAC_INIT_LIST_HEAD(&fibctx->hw_fib_list);
152 fibctx->jiffies = jiffies/HZ;
154 * Now add this context onto the adapter's
155 * AdapterFibContext list.
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 *))) {
170 * next_getadapter_fib - get the next fib
171 * @dev: adapter to use
172 * @arg: ioctl argument
174 * This routine will get the next Fib, if available, from the AdapterFibContext
175 * passed in from the user.
178 static int next_getadapter_fib(struct aac_dev * dev, void *arg)
181 struct aac_fib_context *fibctx, *aifcp;
182 struct hw_fib * hw_fib;
184 struct list_head * entry;
188 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
191 * Extract the AdapterFibContext from the Input parameters.
193 fibctx = (struct aac_fib_context *) f.fibctx;
196 * Verify that the HANDLE passed in was a valid AdapterFibContext
198 * Search the list of AdapterFibContext addresses on the adapter
199 * to be sure this is a valid address
202 entry = dev->fib_list.next;
204 while(entry != &dev->fib_list) {
205 aifcp = list_entry(entry, struct aac_fib_context, next);
206 if(fibctx == aifcp) { /* We found a winner */
215 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
216 (fibctx->size != sizeof(struct aac_fib_context)))
219 spin_lock_irqsave(&dev->fib_lock, flags);
221 * If there are no fibs to send back, then either wait or return
225 if (!aac_list_empty(&fibctx->hw_fib_list)) {
226 struct aac_list_head * entry;
228 * Pull the next fib from the fibs
230 entry = (struct aac_list_head*)(ulong)fibctx->hw_fib_list.next;
233 hw_fib = aac_list_entry(entry, struct hw_fib, header.FibLinks);
235 spin_unlock_irqrestore(&dev->fib_lock, flags);
236 if (copy_to_user(f.fib, hw_fib, sizeof(struct hw_fib))) {
241 * Free the space occupied by this copy of the fib.
245 fibctx->jiffies = jiffies/HZ;
247 spin_unlock_irqrestore(&dev->fib_lock, flags);
249 if(down_interruptible(&fibctx->wait_sem) < 0) {
252 /* Lock again and retry */
253 spin_lock_irqsave(&dev->fib_lock, flags);
263 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
265 struct hw_fib *hw_fib;
268 * First free any FIBs that have not been consumed.
270 while (!aac_list_empty(&fibctx->hw_fib_list)) {
271 struct aac_list_head * entry;
273 * Pull the next fib from the fibs
275 entry = (struct aac_list_head*)(ulong)(fibctx->hw_fib_list.next);
277 hw_fib = aac_list_entry(entry, struct hw_fib, header.FibLinks);
280 * Free the space occupied by this copy of the fib.
285 * Remove the Context from the AdapterFibContext List
287 list_del(&fibctx->next);
293 * Free the space occupied by the Context
300 * close_getadapter_fib - close down user fib context
302 * @arg: ioctl arguments
304 * This routine will close down the fibctx passed in from the user.
307 static int close_getadapter_fib(struct aac_dev * dev, void *arg)
309 struct aac_fib_context *fibctx, *aifcp;
312 struct list_head * entry;
316 * Extract the fibctx from the input parameters
321 * Verify that the HANDLE passed in was a valid AdapterFibContext
323 * Search the list of AdapterFibContext addresses on the adapter
324 * to be sure this is a valid address
328 entry = dev->fib_list.next;
330 while(entry != &dev->fib_list) {
331 aifcp = list_entry(entry, struct aac_fib_context, next);
332 if(fibctx == aifcp) { /* We found a winner */
340 return 0; /* Already gone */
342 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
343 (fibctx->size != sizeof(struct aac_fib_context)))
345 spin_lock_irqsave(&dev->fib_lock, flags);
346 status = aac_close_fib_context(dev, fibctx);
347 spin_unlock_irqrestore(&dev->fib_lock, flags);
352 * check_revision - close down user fib context
354 * @arg: ioctl arguments
356 * This routine returns the firmware version.
357 * Under Linux, there have been no version incompatibilities, so this is simple!
360 static int check_revision(struct aac_dev *dev, void *arg)
362 struct revision response;
365 response.version = dev->adapter_info.kernelrev;
366 response.build = dev->adapter_info.kernelbuild;
368 if (copy_to_user(arg, &response, sizeof(response)))
379 int aac_send_raw_srb(struct aac_dev* dev, void* arg)
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;
395 u32 actual_fibsize = 0;
399 if (!capable(CAP_SYS_ADMIN)){
400 printk(KERN_DEBUG"aacraid: No permission to send raw srb\n");
404 * Allocate and initialize a Fib then setup a BlockWrite command
406 if (!(srbfib = fib_alloc(dev))) {
411 srbcmd = (struct aac_srb*) fib_data(srbfib);
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");
419 if(copy_from_user(srbcmd, user_srb,fibsize)){
420 printk(KERN_DEBUG"aacraid: Could not copy srb from user\n");
425 user_reply = arg+fibsize;
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);
438 switch(srbcmd->flags){
440 data_dir = DMA_TO_DEVICE;
442 case (SRB_DataIn | SRB_DataOut):
443 data_dir = DMA_BIDIRECTIONAL;
446 data_dir = DMA_FROM_DEVICE;
451 if( dev->pae_support ==1 ) {
452 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
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");
463 for (i = 0; i < psg->count; i++) {
467 p = kmalloc(psg->sg[i].count,GFP_KERNEL|__GFP_DMA);
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);
474 sg_user[i] = (ulong)psg->sg[i].addr;
475 sg_list[i] = (ulong)p; // save so we can clean up later
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");
485 addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
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;
494 srbcmd->count = cpu_to_le32(byte_count);
495 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,0,0);
497 struct sgmap* psg = &srbcmd->sg;
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");
506 for (i = 0; i < psg->count; i++) {
509 p = kmalloc(psg->sg[i].count,GFP_KERNEL);
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);
516 sg_user[i] = (ulong)(psg->sg[i].addr);
517 sg_list[i] = (ulong)p; // save so we can clean up later
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");
527 addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
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;
533 srbcmd->count = cpu_to_le32(byte_count);
534 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, 0, 0);
538 printk(KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n");
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");
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");
562 for(i=0; i <= sg_indx; i++){
563 kfree((void*)sg_list[i]);
565 fib_complete(srbfib);
572 struct aac_pci_info {
578 int aac_get_pci_info(struct aac_dev* dev, void* arg)
580 struct aac_pci_info pci_info;
582 pci_info.bus = dev->pdev->bus->number;
583 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
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");
593 int aac_do_ioctl(struct aac_dev * dev, int cmd, void *arg)
598 * HBA gets first crack
601 status = aac_dev_ioctl(dev, cmd, arg);
602 if(status != -ENOTTY)
606 case FSACTL_MINIPORT_REV_CHECK:
607 status = check_revision(dev, arg);
610 status = ioctl_send_fib(dev, arg);
612 case FSACTL_OPEN_GET_ADAPTER_FIB:
613 status = open_getadapter_fib(dev, arg);
615 case FSACTL_GET_NEXT_ADAPTER_FIB:
616 status = next_getadapter_fib(dev, arg);
618 case FSACTL_CLOSE_GET_ADAPTER_FIB:
619 status = close_getadapter_fib(dev, arg);
621 case FSACTL_SEND_RAW_SRB:
622 status = aac_send_raw_srb(dev,arg);
624 case FSACTL_GET_PCI_INFO:
625 status = aac_get_pci_info(dev,arg);