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 __user *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 int retval = fib_send(kfib->header.Command, fibptr,
96 le32_to_cpu(kfib->header.Size) , FsaNormal,
102 if (fib_complete(fibptr) != 0) {
108 * Make sure that the size returned by the adapter (which includes
109 * the header) is less than or equal to the size of a fib, so we
110 * don't corrupt application data. Then copy that size to the user
111 * buffer. (Don't try to add the header information again, since it
112 * was already included by the adapter.)
115 if (copy_to_user(arg, (void *)kfib, kfib->header.Size)) {
124 * open_getadapter_fib - Get the next fib
126 * This routine will get the next Fib, if available, from the AdapterFibContext
127 * passed in from the user.
130 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
132 struct aac_fib_context * fibctx;
135 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
136 if (fibctx == NULL) {
140 struct list_head * entry;
141 struct aac_fib_context * context;
143 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
144 fibctx->size = sizeof(struct aac_fib_context);
146 * Yes yes, I know this could be an index, but we have a
147 * better guarantee of uniqueness for the locked loop below.
148 * Without the aid of a persistent history, this also helps
149 * reduce the chance that the opaque context would be reused.
151 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
153 * Initialize the mutex used to wait for the next AIF.
155 init_MUTEX_LOCKED(&fibctx->wait_sem);
158 * Initialize the fibs and set the count of fibs on
162 INIT_LIST_HEAD(&fibctx->fib_list);
163 fibctx->jiffies = jiffies/HZ;
165 * Now add this context onto the adapter's
166 * AdapterFibContext list.
168 spin_lock_irqsave(&dev->fib_lock, flags);
169 /* Ensure that we have a unique identifier */
170 entry = dev->fib_list.next;
171 while (entry != &dev->fib_list) {
172 context = list_entry(entry, struct aac_fib_context, next);
173 if (context->unique == fibctx->unique) {
174 /* Not unique (32 bits) */
176 entry = dev->fib_list.next;
181 list_add_tail(&fibctx->next, &dev->fib_list);
182 spin_unlock_irqrestore(&dev->fib_lock, flags);
183 if (copy_to_user(arg, &fibctx->unique,
184 sizeof(fibctx->unique))) {
194 * next_getadapter_fib - get the next fib
195 * @dev: adapter to use
196 * @arg: ioctl argument
198 * This routine will get the next Fib, if available, from the AdapterFibContext
199 * passed in from the user.
202 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
206 struct aac_fib_context *fibctx;
208 struct list_head * entry;
211 if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
214 * Verify that the HANDLE passed in was a valid AdapterFibContext
216 * Search the list of AdapterFibContext addresses on the adapter
217 * to be sure this is a valid address
219 entry = dev->fib_list.next;
222 while (entry != &dev->fib_list) {
223 fibctx = list_entry(entry, struct aac_fib_context, next);
225 * Extract the AdapterFibContext from the Input parameters.
227 if (fibctx->unique == f.fibctx) { /* We found a winner */
234 dprintk ((KERN_INFO "Fib Context not found\n"));
238 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
239 (fibctx->size != sizeof(struct aac_fib_context))) {
240 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
244 spin_lock_irqsave(&dev->fib_lock, flags);
246 * If there are no fibs to send back, then either wait or return
250 if (!list_empty(&fibctx->fib_list)) {
251 struct list_head * entry;
253 * Pull the next fib from the fibs
255 entry = fibctx->fib_list.next;
258 fib = list_entry(entry, struct fib, fiblink);
260 spin_unlock_irqrestore(&dev->fib_lock, flags);
261 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
267 * Free the space occupied by this copy of the fib.
272 fibctx->jiffies = jiffies/HZ;
274 spin_unlock_irqrestore(&dev->fib_lock, flags);
276 if(down_interruptible(&fibctx->wait_sem) < 0) {
279 /* Lock again and retry */
280 spin_lock_irqsave(&dev->fib_lock, flags);
290 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
295 * First free any FIBs that have not been consumed.
297 while (!list_empty(&fibctx->fib_list)) {
298 struct list_head * entry;
300 * Pull the next fib from the fibs
302 entry = fibctx->fib_list.next;
304 fib = list_entry(entry, struct fib, fiblink);
307 * Free the space occupied by this copy of the fib.
313 * Remove the Context from the AdapterFibContext List
315 list_del(&fibctx->next);
321 * Free the space occupied by the Context
328 * close_getadapter_fib - close down user fib context
330 * @arg: ioctl arguments
332 * This routine will close down the fibctx passed in from the user.
335 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
337 struct aac_fib_context *fibctx;
340 struct list_head * entry;
343 * Verify that the HANDLE passed in was a valid AdapterFibContext
345 * Search the list of AdapterFibContext addresses on the adapter
346 * to be sure this is a valid address
349 entry = dev->fib_list.next;
352 while(entry != &dev->fib_list) {
353 fibctx = list_entry(entry, struct aac_fib_context, next);
355 * Extract the fibctx from the input parameters
357 if (fibctx->unique == (u32)(unsigned long)arg) {
358 /* We found a winner */
366 return 0; /* Already gone */
368 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
369 (fibctx->size != sizeof(struct aac_fib_context)))
371 spin_lock_irqsave(&dev->fib_lock, flags);
372 status = aac_close_fib_context(dev, fibctx);
373 spin_unlock_irqrestore(&dev->fib_lock, flags);
378 * check_revision - close down user fib context
380 * @arg: ioctl arguments
382 * This routine returns the driver version.
383 * Under Linux, there have been no version incompatibilities, so this is
387 static int check_revision(struct aac_dev *dev, void __user *arg)
389 struct revision response;
392 response.version = dev->adapter_info.kernelrev;
393 response.build = dev->adapter_info.kernelbuild;
395 if (copy_to_user(arg, &response, sizeof(response)))
406 int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
410 struct aac_srb *srbcmd;
411 struct aac_srb __user *user_srb = arg;
412 struct aac_srb_reply __user *user_reply;
413 struct aac_srb_reply* reply;
418 void __user *sg_user[32];
422 u32 actual_fibsize = 0;
426 if (!capable(CAP_SYS_ADMIN)){
427 printk(KERN_DEBUG"aacraid: No permission to send raw srb\n");
431 * Allocate and initialize a Fib then setup a BlockWrite command
433 if (!(srbfib = fib_alloc(dev))) {
438 srbcmd = (struct aac_srb*) fib_data(srbfib);
440 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
441 printk(KERN_DEBUG"aacraid: Could not copy data size from user\n");
446 if (fibsize > FIB_DATA_SIZE_IN_BYTES) {
451 if(copy_from_user(srbcmd, user_srb,fibsize)){
452 printk(KERN_DEBUG"aacraid: Could not copy srb from user\n");
457 user_reply = arg+fibsize;
459 flags = srbcmd->flags;
460 // Fix up srb for endian and force some values
461 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
462 srbcmd->channel = cpu_to_le32(srbcmd->channel);
463 srbcmd->id = cpu_to_le32(srbcmd->id);
464 srbcmd->lun = cpu_to_le32(srbcmd->lun);
465 srbcmd->flags = cpu_to_le32(srbcmd->flags);
466 srbcmd->timeout = cpu_to_le32(srbcmd->timeout);
467 srbcmd->retry_limit =cpu_to_le32(0); // Obsolete parameter
468 srbcmd->cdb_size = cpu_to_le32(srbcmd->cdb_size);
470 switch (srbcmd->flags & (SRB_DataIn | SRB_DataOut)) {
472 data_dir = DMA_TO_DEVICE;
474 case (SRB_DataIn | SRB_DataOut):
475 data_dir = DMA_BIDIRECTIONAL;
478 data_dir = DMA_FROM_DEVICE;
483 if (dev->dac_support == 1) {
484 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
488 * This should also catch if user used the 32 bit sgmap
490 actual_fibsize = sizeof(struct aac_srb) -
491 sizeof(struct sgentry) + ((srbcmd->sg.count & 0xff) *
492 sizeof(struct sgentry64));
493 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
494 printk(KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n");
498 if ((data_dir == DMA_NONE) && psg->count) {
499 printk(KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n");
504 for (i = 0; i < psg->count; i++) {
508 p = kmalloc(psg->sg[i].count,GFP_KERNEL|__GFP_DMA);
510 printk(KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
511 psg->sg[i].count,i,psg->count);
515 sg_user[i] = (void __user *)psg->sg[i].addr;
516 sg_list[i] = p; // save so we can clean up later
519 if( flags & SRB_DataOut ){
520 if(copy_from_user(p,sg_user[i],psg->sg[i].count)){
521 printk(KERN_DEBUG"aacraid: Could not copy sg data from user\n");
526 addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
528 le_addr = cpu_to_le64(addr);
529 psg->sg[i].addr[1] = (u32)(le_addr>>32);
530 psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
531 psg->sg[i].count = cpu_to_le32(psg->sg[i].count);
532 byte_count += psg->sg[i].count;
535 srbcmd->count = cpu_to_le32(byte_count);
536 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
538 struct sgmap* psg = &srbcmd->sg;
541 actual_fibsize = sizeof (struct aac_srb) + (((srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
542 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
543 printk(KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n");
547 if ((data_dir == DMA_NONE) && psg->count) {
548 printk(KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n");
552 for (i = 0; i < psg->count; i++) {
555 p = kmalloc(psg->sg[i].count,GFP_KERNEL);
557 printk(KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
558 psg->sg[i].count,i,psg->count);
562 sg_user[i] = (void __user *)(psg->sg[i].addr);
563 sg_list[i] = p; // save so we can clean up later
566 if( flags & SRB_DataOut ){
567 if(copy_from_user(p,sg_user[i],psg->sg[i].count)){
568 printk(KERN_DEBUG"aacraid: Could not copy sg data from user\n");
573 addr = pci_map_single(dev->pdev, p, psg->sg[i].count, data_dir);
575 psg->sg[i].addr = cpu_to_le32(addr);
576 psg->sg[i].count = cpu_to_le32(psg->sg[i].count);
577 byte_count += psg->sg[i].count;
579 srbcmd->count = cpu_to_le32(byte_count);
580 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
584 printk(KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n");
589 if( flags & SRB_DataIn ) {
590 for(i = 0 ; i <= sg_indx; i++){
591 if(copy_to_user(sg_user[i],sg_list[i],le32_to_cpu(srbcmd->sg.sg[i].count))){
592 printk(KERN_DEBUG"aacraid: Could not copy sg data to user\n");
600 reply = (struct aac_srb_reply *) fib_data(srbfib);
601 if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
602 printk(KERN_DEBUG"aacraid: Could not copy reply to user\n");
608 for(i=0; i <= sg_indx; i++){
611 fib_complete(srbfib);
618 struct aac_pci_info {
624 int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
626 struct aac_pci_info pci_info;
628 pci_info.bus = dev->pdev->bus->number;
629 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
631 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
632 printk(KERN_DEBUG "aacraid: Could not copy pci info\n");
639 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
644 * HBA gets first crack
647 status = aac_dev_ioctl(dev, cmd, arg);
648 if(status != -ENOTTY)
652 case FSACTL_MINIPORT_REV_CHECK:
653 status = check_revision(dev, arg);
656 status = ioctl_send_fib(dev, arg);
658 case FSACTL_OPEN_GET_ADAPTER_FIB:
659 status = open_getadapter_fib(dev, arg);
661 case FSACTL_GET_NEXT_ADAPTER_FIB:
662 status = next_getadapter_fib(dev, arg);
664 case FSACTL_CLOSE_GET_ADAPTER_FIB:
665 status = close_getadapter_fib(dev, arg);
667 case FSACTL_SEND_RAW_SRB:
668 status = aac_send_raw_srb(dev,arg);
670 case FSACTL_GET_PCI_INFO:
671 status = aac_get_pci_info(dev,arg);