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
 
  54 # define AAC_DEBUG_PREAMBLE     KERN_INFO
 
  55 # define AAC_DEBUG_POSTAMBLE
 
  57 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
 
  61         struct hw_fib * hw_fib = (struct hw_fib *)0;
 
  62         dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
 
  66         fibptr = fib_alloc(dev);
 
  71         kfib = fibptr->hw_fib;
 
  73          *      First copy in the header so that we can check the size field.
 
  75         if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
 
  80          *      Since we copy based on the fib header size, make sure that we
 
  81          *      will not overrun the buffer when we copy the memory. Return
 
  82          *      an error if we would.
 
  84         size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr);
 
  85         if (size < le16_to_cpu(kfib->header.SenderSize))
 
  86                 size = le16_to_cpu(kfib->header.SenderSize);
 
  87         if (size > dev->max_fib_size) {
 
  88                 /* Highjack the hw_fib */
 
  89                 hw_fib = fibptr->hw_fib;
 
  90                 hw_fib_pa = fibptr->hw_fib_pa;
 
  91                 fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa);
 
  92                 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
 
  93                 memcpy(kfib, hw_fib, dev->max_fib_size);
 
  96         if (copy_from_user(kfib, arg, size)) {
 
 101         if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
 
 102                 aac_adapter_interrupt(dev);
 
 104                  * Since we didn't really send a fib, zero out the state to allow 
 
 105                  * cleanup code not to assert.
 
 107                 kfib->header.XferState = 0;
 
 109                 retval = fib_send(le16_to_cpu(kfib->header.Command), fibptr,
 
 110                                 le16_to_cpu(kfib->header.Size) , FsaNormal,
 
 115                 if (fib_complete(fibptr) != 0) {
 
 121          *      Make sure that the size returned by the adapter (which includes
 
 122          *      the header) is less than or equal to the size of a fib, so we
 
 123          *      don't corrupt application data. Then copy that size to the user
 
 124          *      buffer. (Don't try to add the header information again, since it
 
 125          *      was already included by the adapter.)
 
 129         if (copy_to_user(arg, (void *)kfib, size))
 
 133                 pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa);
 
 134                 fibptr->hw_fib_pa = hw_fib_pa;
 
 135                 fibptr->hw_fib = hw_fib;
 
 142  *      open_getadapter_fib     -       Get the next fib
 
 144  *      This routine will get the next Fib, if available, from the AdapterFibContext
 
 145  *      passed in from the user.
 
 148 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
 
 150         struct aac_fib_context * fibctx;
 
 153         fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
 
 154         if (fibctx == NULL) {
 
 158                 struct list_head * entry;
 
 159                 struct aac_fib_context * context;
 
 161                 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
 
 162                 fibctx->size = sizeof(struct aac_fib_context);
 
 164                  *      Yes yes, I know this could be an index, but we have a
 
 165                  * better guarantee of uniqueness for the locked loop below.
 
 166                  * Without the aid of a persistent history, this also helps
 
 167                  * reduce the chance that the opaque context would be reused.
 
 169                 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
 
 171                  *      Initialize the mutex used to wait for the next AIF.
 
 173                 init_MUTEX_LOCKED(&fibctx->wait_sem);
 
 176                  *      Initialize the fibs and set the count of fibs on
 
 180                 INIT_LIST_HEAD(&fibctx->fib_list);
 
 181                 fibctx->jiffies = jiffies/HZ;
 
 183                  *      Now add this context onto the adapter's 
 
 184                  *      AdapterFibContext list.
 
 186                 spin_lock_irqsave(&dev->fib_lock, flags);
 
 187                 /* Ensure that we have a unique identifier */
 
 188                 entry = dev->fib_list.next;
 
 189                 while (entry != &dev->fib_list) {
 
 190                         context = list_entry(entry, struct aac_fib_context, next);
 
 191                         if (context->unique == fibctx->unique) {
 
 192                                 /* Not unique (32 bits) */
 
 194                                 entry = dev->fib_list.next;
 
 199                 list_add_tail(&fibctx->next, &dev->fib_list);
 
 200                 spin_unlock_irqrestore(&dev->fib_lock, flags);
 
 201                 if (copy_to_user(arg,  &fibctx->unique, 
 
 202                                                 sizeof(fibctx->unique))) {
 
 212  *      next_getadapter_fib     -       get the next fib
 
 213  *      @dev: adapter to use
 
 214  *      @arg: ioctl argument
 
 216  *      This routine will get the next Fib, if available, from the AdapterFibContext
 
 217  *      passed in from the user.
 
 220 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
 
 224         struct aac_fib_context *fibctx;
 
 226         struct list_head * entry;
 
 229         if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
 
 232          *      Verify that the HANDLE passed in was a valid AdapterFibContext
 
 234          *      Search the list of AdapterFibContext addresses on the adapter
 
 235          *      to be sure this is a valid address
 
 237         entry = dev->fib_list.next;
 
 240         while (entry != &dev->fib_list) {
 
 241                 fibctx = list_entry(entry, struct aac_fib_context, next);
 
 243                  *      Extract the AdapterFibContext from the Input parameters.
 
 245                 if (fibctx->unique == f.fibctx) {   /* We found a winner */
 
 252                 dprintk ((KERN_INFO "Fib Context not found\n"));
 
 256         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
 
 257                  (fibctx->size != sizeof(struct aac_fib_context))) {
 
 258                 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
 
 262         spin_lock_irqsave(&dev->fib_lock, flags);
 
 264          *      If there are no fibs to send back, then either wait or return
 
 268         if (!list_empty(&fibctx->fib_list)) {
 
 269                 struct list_head * entry;
 
 271                  *      Pull the next fib from the fibs
 
 273                 entry = fibctx->fib_list.next;
 
 276                 fib = list_entry(entry, struct fib, fiblink);
 
 278                 spin_unlock_irqrestore(&dev->fib_lock, flags);
 
 279                 if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) {
 
 285                  *      Free the space occupied by this copy of the fib.
 
 291                 spin_unlock_irqrestore(&dev->fib_lock, flags);
 
 293                         if(down_interruptible(&fibctx->wait_sem) < 0) {
 
 296                                 /* Lock again and retry */
 
 297                                 spin_lock_irqsave(&dev->fib_lock, flags);
 
 304         fibctx->jiffies = jiffies/HZ;
 
 308 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
 
 313          *      First free any FIBs that have not been consumed.
 
 315         while (!list_empty(&fibctx->fib_list)) {
 
 316                 struct list_head * entry;
 
 318                  *      Pull the next fib from the fibs
 
 320                 entry = fibctx->fib_list.next;
 
 322                 fib = list_entry(entry, struct fib, fiblink);
 
 325                  *      Free the space occupied by this copy of the fib.
 
 331          *      Remove the Context from the AdapterFibContext List
 
 333         list_del(&fibctx->next);
 
 339          *      Free the space occupied by the Context
 
 346  *      close_getadapter_fib    -       close down user fib context
 
 348  *      @arg: ioctl arguments
 
 350  *      This routine will close down the fibctx passed in from the user.
 
 353 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
 
 355         struct aac_fib_context *fibctx;
 
 358         struct list_head * entry;
 
 361          *      Verify that the HANDLE passed in was a valid AdapterFibContext
 
 363          *      Search the list of AdapterFibContext addresses on the adapter
 
 364          *      to be sure this is a valid address
 
 367         entry = dev->fib_list.next;
 
 370         while(entry != &dev->fib_list) {
 
 371                 fibctx = list_entry(entry, struct aac_fib_context, next);
 
 373                  *      Extract the fibctx from the input parameters
 
 375                 if (fibctx->unique == (u32)(unsigned long)arg) {   
 
 376                         /* We found a winner */
 
 384                 return 0; /* Already gone */
 
 386         if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
 
 387                  (fibctx->size != sizeof(struct aac_fib_context)))
 
 389         spin_lock_irqsave(&dev->fib_lock, flags);
 
 390         status = aac_close_fib_context(dev, fibctx);
 
 391         spin_unlock_irqrestore(&dev->fib_lock, flags);
 
 396  *      check_revision  -       close down user fib context
 
 398  *      @arg: ioctl arguments
 
 400  *      This routine returns the driver version.
 
 401  *      Under Linux, there have been no version incompatibilities, so this is 
 
 405 static int check_revision(struct aac_dev *dev, void __user *arg)
 
 407         struct revision response;
 
 408         char *driver_version = aac_driver_version;
 
 412         version = (simple_strtol(driver_version, 
 
 413                                 &driver_version, 10) << 24) | 0x00000400;
 
 414         version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
 
 415         version += simple_strtol(driver_version + 1, NULL, 10);
 
 416         response.version = cpu_to_le32(version);
 
 417 #       if (defined(AAC_DRIVER_BUILD))
 
 418                 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
 
 420                 response.build = cpu_to_le32(9999);
 
 423         if (copy_to_user(arg, &response, sizeof(response)))
 
 435 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
 
 439         struct aac_srb *srbcmd = NULL;
 
 440         struct user_aac_srb *user_srbcmd = NULL;
 
 441         struct user_aac_srb __user *user_srb = arg;
 
 442         struct aac_srb_reply __user *user_reply;
 
 443         struct aac_srb_reply* reply;
 
 448         void __user *sg_user[32];
 
 452         u32 actual_fibsize = 0;
 
 456         if (!capable(CAP_SYS_ADMIN)){
 
 457                 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n")); 
 
 461          *      Allocate and initialize a Fib then setup a BlockWrite command
 
 463         if (!(srbfib = fib_alloc(dev))) {
 
 468         srbcmd = (struct aac_srb*) fib_data(srbfib);
 
 470         memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
 
 471         if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
 
 472                 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n")); 
 
 477         if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
 
 482         user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
 
 484                 dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
 
 488         if(copy_from_user(user_srbcmd, user_srb,fibsize)){
 
 489                 dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n")); 
 
 494         user_reply = arg+fibsize;
 
 496         flags = user_srbcmd->flags; /* from user in cpu order */
 
 497         // Fix up srb for endian and force some values
 
 499         srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);       // Force this
 
 500         srbcmd->channel  = cpu_to_le32(user_srbcmd->channel);
 
 501         srbcmd->id       = cpu_to_le32(user_srbcmd->id);
 
 502         srbcmd->lun      = cpu_to_le32(user_srbcmd->lun);
 
 503         srbcmd->timeout  = cpu_to_le32(user_srbcmd->timeout);
 
 504         srbcmd->flags    = cpu_to_le32(flags);
 
 505         srbcmd->retry_limit = 0; // Obsolete parameter
 
 506         srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
 
 507         memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
 
 509         switch (flags & (SRB_DataIn | SRB_DataOut)) {
 
 511                 data_dir = DMA_TO_DEVICE;
 
 513         case (SRB_DataIn | SRB_DataOut):
 
 514                 data_dir = DMA_BIDIRECTIONAL;
 
 517                 data_dir = DMA_FROM_DEVICE;
 
 522         if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) {
 
 523                 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
 
 524                   le32_to_cpu(srbcmd->sg.count)));
 
 528         if (dev->dac_support == 1) {
 
 529                 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
 
 530                 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
 
 531                 struct user_sgmap* usg;
 
 535                  * This should also catch if user used the 32 bit sgmap
 
 537                 actual_fibsize = sizeof(struct aac_srb) - 
 
 538                         sizeof(struct sgentry) +
 
 539                         ((upsg->count & 0xff) * 
 
 540                         sizeof(struct sgentry));
 
 541                 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
 
 542                         dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
 
 546                 usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
 
 547                   + sizeof(struct sgmap), GFP_KERNEL);
 
 549                         dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
 
 553                 memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
 
 554                   + sizeof(struct sgmap));
 
 555                 actual_fibsize = sizeof(struct aac_srb) - 
 
 556                         sizeof(struct sgentry) + ((usg->count & 0xff) * 
 
 557                                 sizeof(struct sgentry64));
 
 558                 if ((data_dir == DMA_NONE) && upsg->count) {
 
 560                         dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
 
 565                 for (i = 0; i < usg->count; i++) {
 
 568                         /* Does this really need to be GFP_DMA? */
 
 569                         p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
 
 572                                 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
 
 573                                   usg->sg[i].count,i,usg->count));
 
 577                         sg_user[i] = (void __user *)(long)usg->sg[i].addr;
 
 578                         sg_list[i] = p; // save so we can clean up later
 
 581                         if( flags & SRB_DataOut ){
 
 582                                 if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
 
 584                                         dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); 
 
 589                         addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
 
 591                         psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
 
 592                         psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
 
 593                         psg->sg[i].count = cpu_to_le32(usg->sg[i].count);  
 
 594                         byte_count += usg->sg[i].count;
 
 598                 srbcmd->count = cpu_to_le32(byte_count);
 
 599                 psg->count = cpu_to_le32(sg_indx+1);
 
 600                 status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
 
 602                 struct user_sgmap* upsg = &user_srbcmd->sg;
 
 603                 struct sgmap* psg = &srbcmd->sg;
 
 606                 actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
 
 607                 if(actual_fibsize != fibsize){ // User made a mistake - should not continue
 
 608                         dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
 
 612                 if ((data_dir == DMA_NONE) && upsg->count) {
 
 613                         dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
 
 617                 for (i = 0; i < upsg->count; i++) {
 
 620                         p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
 
 622                                 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
 
 623                                   upsg->sg[i].count, i, upsg->count));
 
 627                         sg_user[i] = (void __user *)(long)upsg->sg[i].addr;
 
 628                         sg_list[i] = p; // save so we can clean up later
 
 631                         if( flags & SRB_DataOut ){
 
 632                                 if(copy_from_user(p, sg_user[i],
 
 633                                                 upsg->sg[i].count)) {
 
 634                                         dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); 
 
 639                         addr = pci_map_single(dev->pdev, p,
 
 640                                 upsg->sg[i].count, data_dir);
 
 642                         psg->sg[i].addr = cpu_to_le32(addr);
 
 643                         psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);  
 
 644                         byte_count += upsg->sg[i].count;
 
 646                 srbcmd->count = cpu_to_le32(byte_count);
 
 647                 psg->count = cpu_to_le32(sg_indx+1);
 
 648                 status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
 
 652                 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n")); 
 
 657         if( flags & SRB_DataIn ) {
 
 658                 for(i = 0 ; i <= sg_indx; i++){
 
 659                         byte_count = le32_to_cpu((dev->dac_support == 1)
 
 660                               ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
 
 661                               : srbcmd->sg.sg[i].count);
 
 662                         if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
 
 663                                 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n")); 
 
 671         reply = (struct aac_srb_reply *) fib_data(srbfib);
 
 672         if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
 
 673                 dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n")); 
 
 680         for(i=0; i <= sg_indx; i++){
 
 683         fib_complete(srbfib);
 
 689 struct aac_pci_info {
 
 695 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
 
 697         struct aac_pci_info pci_info;
 
 699         pci_info.bus = dev->pdev->bus->number;
 
 700         pci_info.slot = PCI_SLOT(dev->pdev->devfn);
 
 702        if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
 
 703                dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
 
 710 int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
 
 715          *      HBA gets first crack
 
 718         status = aac_dev_ioctl(dev, cmd, arg);
 
 719         if(status != -ENOTTY)
 
 723         case FSACTL_MINIPORT_REV_CHECK:
 
 724                 status = check_revision(dev, arg);
 
 726         case FSACTL_SEND_LARGE_FIB:
 
 728                 status = ioctl_send_fib(dev, arg);
 
 730         case FSACTL_OPEN_GET_ADAPTER_FIB:
 
 731                 status = open_getadapter_fib(dev, arg);
 
 733         case FSACTL_GET_NEXT_ADAPTER_FIB:
 
 734                 status = next_getadapter_fib(dev, arg);
 
 736         case FSACTL_CLOSE_GET_ADAPTER_FIB:
 
 737                 status = close_getadapter_fib(dev, arg);
 
 739         case FSACTL_SEND_RAW_SRB:
 
 740                 status = aac_send_raw_srb(dev,arg);
 
 742         case FSACTL_GET_PCI_INFO:
 
 743                 status = aac_get_pci_info(dev,arg);