Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Adaptec AAC series RAID controller driver |
Alan Cox | fa195af | 2008-10-27 15:16:36 +0000 | [diff] [blame] | 3 | * (c) Copyright 2001 Red Hat Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * based on the old aacraid driver that is.. |
| 6 | * Adaptec aacraid device driver for Linux. |
| 7 | * |
Mahesh Rajashekhara | e8b12f0 | 2011-03-17 02:10:32 -0700 | [diff] [blame] | 8 | * Copyright (c) 2000-2010 Adaptec, Inc. |
| 9 | * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; see the file COPYING. If not, write to |
| 23 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | * |
| 25 | * Module Name: |
| 26 | * commctrl.c |
| 27 | * |
| 28 | * Abstract: Contains all routines for control of the AFA comm layer |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 41 | #include <linux/delay.h> /* ssleep prototype */ |
Mark Haverkamp | dc4adbf | 2006-03-27 09:44:26 -0800 | [diff] [blame] | 42 | #include <linux/kthread.h> |
Matthew Wilcox | 6188e10 | 2008-04-18 22:21:05 -0400 | [diff] [blame] | 43 | #include <linux/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/uaccess.h> |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 45 | #include <scsi/scsi_host.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include "aacraid.h" |
| 48 | |
| 49 | /** |
| 50 | * ioctl_send_fib - send a FIB from userspace |
| 51 | * @dev: adapter is being processed |
| 52 | * @arg: arguments to the ioctl call |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 53 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * This routine sends a fib to the adapter on behalf of a user level |
| 55 | * program. |
| 56 | */ |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 57 | # define AAC_DEBUG_PREAMBLE KERN_INFO |
| 58 | # define AAC_DEBUG_POSTAMBLE |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static int ioctl_send_fib(struct aac_dev * dev, void __user *arg) |
| 61 | { |
| 62 | struct hw_fib * kfib; |
| 63 | struct fib *fibptr; |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 64 | struct hw_fib * hw_fib = (struct hw_fib *)0; |
| 65 | dma_addr_t hw_fib_pa = (dma_addr_t)0LL; |
| 66 | unsigned size; |
| 67 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Mark Haverkamp | 33bb3b2 | 2007-03-15 10:27:21 -0700 | [diff] [blame] | 69 | if (dev->in_reset) { |
| 70 | return -EBUSY; |
| 71 | } |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 72 | fibptr = aac_fib_alloc(dev); |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 73 | if(fibptr == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | return -ENOMEM; |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 75 | } |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 76 | |
Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 77 | kfib = fibptr->hw_fib_va; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
| 79 | * First copy in the header so that we can check the size field. |
| 80 | */ |
| 81 | if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) { |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 82 | aac_fib_free(fibptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return -EFAULT; |
| 84 | } |
| 85 | /* |
| 86 | * Since we copy based on the fib header size, make sure that we |
| 87 | * will not overrun the buffer when we copy the memory. Return |
| 88 | * an error if we would. |
| 89 | */ |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 90 | size = le16_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr); |
| 91 | if (size < le16_to_cpu(kfib->header.SenderSize)) |
| 92 | size = le16_to_cpu(kfib->header.SenderSize); |
| 93 | if (size > dev->max_fib_size) { |
FUJITA Tomonori | e989911 | 2008-10-23 17:36:08 +0900 | [diff] [blame] | 94 | dma_addr_t daddr; |
| 95 | |
Mark Haverkamp | 6e289a9 | 2006-01-11 09:28:07 -0800 | [diff] [blame] | 96 | if (size > 2048) { |
| 97 | retval = -EINVAL; |
| 98 | goto cleanup; |
| 99 | } |
FUJITA Tomonori | e989911 | 2008-10-23 17:36:08 +0900 | [diff] [blame] | 100 | |
| 101 | kfib = pci_alloc_consistent(dev->pdev, size, &daddr); |
| 102 | if (!kfib) { |
| 103 | retval = -ENOMEM; |
| 104 | goto cleanup; |
| 105 | } |
| 106 | |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 107 | /* Highjack the hw_fib */ |
Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 108 | hw_fib = fibptr->hw_fib_va; |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 109 | hw_fib_pa = fibptr->hw_fib_pa; |
FUJITA Tomonori | e989911 | 2008-10-23 17:36:08 +0900 | [diff] [blame] | 110 | fibptr->hw_fib_va = kfib; |
| 111 | fibptr->hw_fib_pa = daddr; |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 112 | memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size); |
| 113 | memcpy(kfib, hw_fib, dev->max_fib_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 116 | if (copy_from_user(kfib, arg, size)) { |
| 117 | retval = -EFAULT; |
| 118 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 121 | if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | aac_adapter_interrupt(dev); |
| 123 | /* |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 124 | * Since we didn't really send a fib, zero out the state to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * cleanup code not to assert. |
| 126 | */ |
| 127 | kfib->header.XferState = 0; |
| 128 | } else { |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 129 | retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | le16_to_cpu(kfib->header.Size) , FsaNormal, |
| 131 | 1, 1, NULL, NULL); |
| 132 | if (retval) { |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 133 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 135 | if (aac_fib_complete(fibptr) != 0) { |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 136 | retval = -EINVAL; |
| 137 | goto cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | /* |
| 141 | * Make sure that the size returned by the adapter (which includes |
| 142 | * the header) is less than or equal to the size of a fib, so we |
| 143 | * don't corrupt application data. Then copy that size to the user |
| 144 | * buffer. (Don't try to add the header information again, since it |
| 145 | * was already included by the adapter.) |
| 146 | */ |
| 147 | |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 148 | retval = 0; |
| 149 | if (copy_to_user(arg, (void *)kfib, size)) |
| 150 | retval = -EFAULT; |
| 151 | cleanup: |
| 152 | if (hw_fib) { |
| 153 | pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa); |
| 154 | fibptr->hw_fib_pa = hw_fib_pa; |
Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 155 | fibptr->hw_fib_va = hw_fib; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 157 | if (retval != -ERESTARTSYS) |
Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 158 | aac_fib_free(fibptr); |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 159 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * open_getadapter_fib - Get the next fib |
| 164 | * |
| 165 | * This routine will get the next Fib, if available, from the AdapterFibContext |
| 166 | * passed in from the user. |
| 167 | */ |
| 168 | |
| 169 | static int open_getadapter_fib(struct aac_dev * dev, void __user *arg) |
| 170 | { |
| 171 | struct aac_fib_context * fibctx; |
| 172 | int status; |
| 173 | |
| 174 | fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL); |
| 175 | if (fibctx == NULL) { |
| 176 | status = -ENOMEM; |
| 177 | } else { |
| 178 | unsigned long flags; |
| 179 | struct list_head * entry; |
| 180 | struct aac_fib_context * context; |
| 181 | |
| 182 | fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT; |
| 183 | fibctx->size = sizeof(struct aac_fib_context); |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 184 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * Yes yes, I know this could be an index, but we have a |
| 186 | * better guarantee of uniqueness for the locked loop below. |
| 187 | * Without the aid of a persistent history, this also helps |
| 188 | * reduce the chance that the opaque context would be reused. |
| 189 | */ |
| 190 | fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF); |
| 191 | /* |
| 192 | * Initialize the mutex used to wait for the next AIF. |
| 193 | */ |
Thomas Gleixner | 6de76cf | 2010-09-07 14:32:47 +0000 | [diff] [blame] | 194 | sema_init(&fibctx->wait_sem, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | fibctx->wait = 0; |
| 196 | /* |
| 197 | * Initialize the fibs and set the count of fibs on |
| 198 | * the list to 0. |
| 199 | */ |
| 200 | fibctx->count = 0; |
| 201 | INIT_LIST_HEAD(&fibctx->fib_list); |
| 202 | fibctx->jiffies = jiffies/HZ; |
| 203 | /* |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 204 | * Now add this context onto the adapter's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * AdapterFibContext list. |
| 206 | */ |
| 207 | spin_lock_irqsave(&dev->fib_lock, flags); |
| 208 | /* Ensure that we have a unique identifier */ |
| 209 | entry = dev->fib_list.next; |
| 210 | while (entry != &dev->fib_list) { |
| 211 | context = list_entry(entry, struct aac_fib_context, next); |
| 212 | if (context->unique == fibctx->unique) { |
| 213 | /* Not unique (32 bits) */ |
| 214 | fibctx->unique++; |
| 215 | entry = dev->fib_list.next; |
| 216 | } else { |
| 217 | entry = entry->next; |
| 218 | } |
| 219 | } |
| 220 | list_add_tail(&fibctx->next, &dev->fib_list); |
| 221 | spin_unlock_irqrestore(&dev->fib_lock, flags); |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 222 | if (copy_to_user(arg, &fibctx->unique, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | sizeof(fibctx->unique))) { |
| 224 | status = -EFAULT; |
| 225 | } else { |
| 226 | status = 0; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | return status; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * next_getadapter_fib - get the next fib |
| 234 | * @dev: adapter to use |
| 235 | * @arg: ioctl argument |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 236 | * |
| 237 | * This routine will get the next Fib, if available, from the AdapterFibContext |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | * passed in from the user. |
| 239 | */ |
| 240 | |
| 241 | static int next_getadapter_fib(struct aac_dev * dev, void __user *arg) |
| 242 | { |
| 243 | struct fib_ioctl f; |
| 244 | struct fib *fib; |
| 245 | struct aac_fib_context *fibctx; |
| 246 | int status; |
| 247 | struct list_head * entry; |
| 248 | unsigned long flags; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl))) |
| 251 | return -EFAULT; |
| 252 | /* |
| 253 | * Verify that the HANDLE passed in was a valid AdapterFibContext |
| 254 | * |
| 255 | * Search the list of AdapterFibContext addresses on the adapter |
| 256 | * to be sure this is a valid address |
| 257 | */ |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 258 | spin_lock_irqsave(&dev->fib_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | entry = dev->fib_list.next; |
| 260 | fibctx = NULL; |
| 261 | |
| 262 | while (entry != &dev->fib_list) { |
| 263 | fibctx = list_entry(entry, struct aac_fib_context, next); |
| 264 | /* |
| 265 | * Extract the AdapterFibContext from the Input parameters. |
| 266 | */ |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 267 | if (fibctx->unique == f.fibctx) { /* We found a winner */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | entry = entry->next; |
| 271 | fibctx = NULL; |
| 272 | } |
| 273 | if (!fibctx) { |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 274 | spin_unlock_irqrestore(&dev->fib_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | dprintk ((KERN_INFO "Fib Context not found\n")); |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
| 279 | if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) || |
| 280 | (fibctx->size != sizeof(struct aac_fib_context))) { |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 281 | spin_unlock_irqrestore(&dev->fib_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | dprintk ((KERN_INFO "Fib Context corrupt?\n")); |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* |
| 287 | * If there are no fibs to send back, then either wait or return |
| 288 | * -EAGAIN |
| 289 | */ |
| 290 | return_fib: |
| 291 | if (!list_empty(&fibctx->fib_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | /* |
| 293 | * Pull the next fib from the fibs |
| 294 | */ |
| 295 | entry = fibctx->fib_list.next; |
| 296 | list_del(entry); |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | fib = list_entry(entry, struct fib, fiblink); |
| 299 | fibctx->count--; |
| 300 | spin_unlock_irqrestore(&dev->fib_lock, flags); |
Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 301 | if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) { |
| 302 | kfree(fib->hw_fib_va); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | kfree(fib); |
| 304 | return -EFAULT; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | /* |
| 307 | * Free the space occupied by this copy of the fib. |
| 308 | */ |
Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 309 | kfree(fib->hw_fib_va); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | kfree(fib); |
| 311 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } else { |
| 313 | spin_unlock_irqrestore(&dev->fib_lock, flags); |
Mark Haverkamp | dc4adbf | 2006-03-27 09:44:26 -0800 | [diff] [blame] | 314 | /* If someone killed the AIF aacraid thread, restart it */ |
| 315 | status = !dev->aif_thread; |
Mark Haverkamp | 8c867b2 | 2006-08-03 08:03:30 -0700 | [diff] [blame] | 316 | if (status && !dev->in_reset && dev->queues && dev->fsa_dev) { |
Mark Haverkamp | dc4adbf | 2006-03-27 09:44:26 -0800 | [diff] [blame] | 317 | /* Be paranoid, be very paranoid! */ |
| 318 | kthread_stop(dev->thread); |
| 319 | ssleep(1); |
| 320 | dev->aif_thread = 0; |
| 321 | dev->thread = kthread_run(aac_command_thread, dev, dev->name); |
| 322 | ssleep(1); |
| 323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | if (f.wait) { |
| 325 | if(down_interruptible(&fibctx->wait_sem) < 0) { |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 326 | status = -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } else { |
| 328 | /* Lock again and retry */ |
| 329 | spin_lock_irqsave(&dev->fib_lock, flags); |
| 330 | goto return_fib; |
| 331 | } |
| 332 | } else { |
| 333 | status = -EAGAIN; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Mark Haverkamp | 12a26d0 | 2005-08-03 15:39:25 -0700 | [diff] [blame] | 336 | fibctx->jiffies = jiffies/HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return status; |
| 338 | } |
| 339 | |
| 340 | int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx) |
| 341 | { |
| 342 | struct fib *fib; |
| 343 | |
| 344 | /* |
| 345 | * First free any FIBs that have not been consumed. |
| 346 | */ |
| 347 | while (!list_empty(&fibctx->fib_list)) { |
| 348 | struct list_head * entry; |
| 349 | /* |
| 350 | * Pull the next fib from the fibs |
| 351 | */ |
| 352 | entry = fibctx->fib_list.next; |
| 353 | list_del(entry); |
| 354 | fib = list_entry(entry, struct fib, fiblink); |
| 355 | fibctx->count--; |
| 356 | /* |
| 357 | * Free the space occupied by this copy of the fib. |
| 358 | */ |
Mark Haverkamp | a8166a5 | 2007-03-15 10:26:22 -0700 | [diff] [blame] | 359 | kfree(fib->hw_fib_va); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | kfree(fib); |
| 361 | } |
| 362 | /* |
| 363 | * Remove the Context from the AdapterFibContext List |
| 364 | */ |
| 365 | list_del(&fibctx->next); |
| 366 | /* |
| 367 | * Invalidate context |
| 368 | */ |
| 369 | fibctx->type = 0; |
| 370 | /* |
| 371 | * Free the space occupied by the Context |
| 372 | */ |
| 373 | kfree(fibctx); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * close_getadapter_fib - close down user fib context |
| 379 | * @dev: adapter |
| 380 | * @arg: ioctl arguments |
| 381 | * |
| 382 | * This routine will close down the fibctx passed in from the user. |
| 383 | */ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | static int close_getadapter_fib(struct aac_dev * dev, void __user *arg) |
| 386 | { |
| 387 | struct aac_fib_context *fibctx; |
| 388 | int status; |
| 389 | unsigned long flags; |
| 390 | struct list_head * entry; |
| 391 | |
| 392 | /* |
| 393 | * Verify that the HANDLE passed in was a valid AdapterFibContext |
| 394 | * |
| 395 | * Search the list of AdapterFibContext addresses on the adapter |
| 396 | * to be sure this is a valid address |
| 397 | */ |
| 398 | |
| 399 | entry = dev->fib_list.next; |
| 400 | fibctx = NULL; |
| 401 | |
| 402 | while(entry != &dev->fib_list) { |
| 403 | fibctx = list_entry(entry, struct aac_fib_context, next); |
| 404 | /* |
| 405 | * Extract the fibctx from the input parameters |
| 406 | */ |
Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 407 | if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | entry = entry->next; |
| 410 | fibctx = NULL; |
| 411 | } |
| 412 | |
| 413 | if (!fibctx) |
| 414 | return 0; /* Already gone */ |
| 415 | |
| 416 | if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) || |
| 417 | (fibctx->size != sizeof(struct aac_fib_context))) |
| 418 | return -EINVAL; |
| 419 | spin_lock_irqsave(&dev->fib_lock, flags); |
| 420 | status = aac_close_fib_context(dev, fibctx); |
| 421 | spin_unlock_irqrestore(&dev->fib_lock, flags); |
| 422 | return status; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * check_revision - close down user fib context |
| 427 | * @dev: adapter |
| 428 | * @arg: ioctl arguments |
| 429 | * |
| 430 | * This routine returns the driver version. |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 431 | * Under Linux, there have been no version incompatibilities, so this is |
| 432 | * simple! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | */ |
| 434 | |
| 435 | static int check_revision(struct aac_dev *dev, void __user *arg) |
| 436 | { |
| 437 | struct revision response; |
Mark Haverkamp | c7f4760 | 2005-08-03 15:38:55 -0700 | [diff] [blame] | 438 | char *driver_version = aac_driver_version; |
| 439 | u32 version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Mark Haverkamp | 9f30a32 | 2005-10-24 10:52:02 -0700 | [diff] [blame] | 441 | response.compat = 1; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 442 | version = (simple_strtol(driver_version, |
Mark Haverkamp | c7f4760 | 2005-08-03 15:38:55 -0700 | [diff] [blame] | 443 | &driver_version, 10) << 24) | 0x00000400; |
| 444 | version += simple_strtol(driver_version + 1, &driver_version, 10) << 16; |
| 445 | version += simple_strtol(driver_version + 1, NULL, 10); |
| 446 | response.version = cpu_to_le32(version); |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 447 | # ifdef AAC_DRIVER_BUILD |
Mark Haverkamp | c7f4760 | 2005-08-03 15:38:55 -0700 | [diff] [blame] | 448 | response.build = cpu_to_le32(AAC_DRIVER_BUILD); |
| 449 | # else |
| 450 | response.build = cpu_to_le32(9999); |
| 451 | # endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | if (copy_to_user(arg, &response, sizeof(response))) |
| 454 | return -EFAULT; |
| 455 | return 0; |
| 456 | } |
| 457 | |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /** |
| 460 | * |
| 461 | * aac_send_raw_scb |
| 462 | * |
| 463 | */ |
| 464 | |
Adrian Bunk | 4833869 | 2005-04-25 19:45:58 -0700 | [diff] [blame] | 465 | static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
| 467 | struct fib* srbfib; |
| 468 | int status; |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 469 | struct aac_srb *srbcmd = NULL; |
| 470 | struct user_aac_srb *user_srbcmd = NULL; |
| 471 | struct user_aac_srb __user *user_srb = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | struct aac_srb_reply __user *user_reply; |
| 473 | struct aac_srb_reply* reply; |
| 474 | u32 fibsize = 0; |
| 475 | u32 flags = 0; |
| 476 | s32 rcode = 0; |
| 477 | u32 data_dir; |
| 478 | void __user *sg_user[32]; |
| 479 | void *sg_list[32]; |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 480 | u32 sg_indx = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | u32 byte_count = 0; |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 482 | u32 actual_fibsize64, actual_fibsize = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | int i; |
| 484 | |
| 485 | |
Mark Haverkamp | 33bb3b2 | 2007-03-15 10:27:21 -0700 | [diff] [blame] | 486 | if (dev->in_reset) { |
| 487 | dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n")); |
| 488 | return -EBUSY; |
| 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (!capable(CAP_SYS_ADMIN)){ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 491 | dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | return -EPERM; |
| 493 | } |
| 494 | /* |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 495 | * Allocate and initialize a Fib then setup a SRB command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | */ |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 497 | if (!(srbfib = aac_fib_alloc(dev))) { |
Mark Haverkamp | 5d497ce | 2005-06-17 13:38:04 -0700 | [diff] [blame] | 498 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 500 | aac_fib_init(srbfib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | srbcmd = (struct aac_srb*) fib_data(srbfib); |
| 503 | |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 504 | memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 506 | dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | rcode = -EFAULT; |
| 508 | goto cleanup; |
| 509 | } |
| 510 | |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 511 | if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | rcode = -EINVAL; |
| 513 | goto cleanup; |
| 514 | } |
| 515 | |
Dave Jones | 4645df1 | 2005-07-12 13:58:08 -0700 | [diff] [blame] | 516 | user_srbcmd = kmalloc(fibsize, GFP_KERNEL); |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 517 | if (!user_srbcmd) { |
| 518 | dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n")); |
| 519 | rcode = -ENOMEM; |
| 520 | goto cleanup; |
| 521 | } |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 522 | if(copy_from_user(user_srbcmd, user_srb,fibsize)){ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 523 | dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | rcode = -EFAULT; |
| 525 | goto cleanup; |
| 526 | } |
| 527 | |
| 528 | user_reply = arg+fibsize; |
| 529 | |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 530 | flags = user_srbcmd->flags; /* from user in cpu order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | // Fix up srb for endian and force some values |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 534 | srbcmd->channel = cpu_to_le32(user_srbcmd->channel); |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 535 | srbcmd->id = cpu_to_le32(user_srbcmd->id); |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 536 | srbcmd->lun = cpu_to_le32(user_srbcmd->lun); |
| 537 | srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout); |
| 538 | srbcmd->flags = cpu_to_le32(flags); |
Mark Haverkamp | 5d497ce | 2005-06-17 13:38:04 -0700 | [diff] [blame] | 539 | srbcmd->retry_limit = 0; // Obsolete parameter |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 540 | srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size); |
Mark Haverkamp | 5d497ce | 2005-06-17 13:38:04 -0700 | [diff] [blame] | 541 | memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb)); |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 542 | |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 543 | switch (flags & (SRB_DataIn | SRB_DataOut)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | case SRB_DataOut: |
| 545 | data_dir = DMA_TO_DEVICE; |
| 546 | break; |
| 547 | case (SRB_DataIn | SRB_DataOut): |
| 548 | data_dir = DMA_BIDIRECTIONAL; |
| 549 | break; |
| 550 | case SRB_DataIn: |
| 551 | data_dir = DMA_FROM_DEVICE; |
| 552 | break; |
| 553 | default: |
| 554 | data_dir = DMA_NONE; |
| 555 | } |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 556 | if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) { |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 557 | dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n", |
| 558 | le32_to_cpu(srbcmd->sg.count))); |
| 559 | rcode = -EINVAL; |
| 560 | goto cleanup; |
| 561 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 562 | actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) + |
| 563 | ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry)); |
| 564 | actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) * |
| 565 | (sizeof(struct sgentry64) - sizeof(struct sgentry)); |
| 566 | /* User made a mistake - should not continue */ |
| 567 | if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) { |
| 568 | dprintk((KERN_DEBUG"aacraid: Bad Size specified in " |
| 569 | "Raw SRB command calculated fibsize=%lu;%lu " |
| 570 | "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu " |
| 571 | "issued fibsize=%d\n", |
| 572 | actual_fibsize, actual_fibsize64, user_srbcmd->sg.count, |
| 573 | sizeof(struct aac_srb), sizeof(struct sgentry), |
| 574 | sizeof(struct sgentry64), fibsize)); |
| 575 | rcode = -EINVAL; |
| 576 | goto cleanup; |
| 577 | } |
| 578 | if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) { |
| 579 | dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n")); |
| 580 | rcode = -EINVAL; |
| 581 | goto cleanup; |
| 582 | } |
| 583 | byte_count = 0; |
| 584 | if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) { |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 585 | struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg; |
Mark Haverkamp | 84e2930 | 2005-07-07 13:40:00 -0700 | [diff] [blame] | 586 | struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * This should also catch if user used the 32 bit sgmap |
| 590 | */ |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 591 | if (actual_fibsize64 == fibsize) { |
| 592 | actual_fibsize = actual_fibsize64; |
| 593 | for (i = 0; i < upsg->count; i++) { |
| 594 | u64 addr; |
| 595 | void* p; |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 596 | if (upsg->sg[i].count > |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 597 | ((dev->adapter_info.options & |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 598 | AAC_OPT_NEW_COMM) ? |
| 599 | (dev->scsi_host_ptr->max_sectors << 9) : |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 600 | 65536)) { |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 601 | rcode = -EINVAL; |
| 602 | goto cleanup; |
| 603 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 604 | /* Does this really need to be GFP_DMA? */ |
| 605 | p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA); |
Salyzyn, Mark | 6dcd4a7 | 2008-01-08 13:08:04 -0800 | [diff] [blame] | 606 | if(!p) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 607 | dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n", |
| 608 | upsg->sg[i].count,i,upsg->count)); |
| 609 | rcode = -ENOMEM; |
| 610 | goto cleanup; |
| 611 | } |
| 612 | addr = (u64)upsg->sg[i].addr[0]; |
| 613 | addr += ((u64)upsg->sg[i].addr[1]) << 32; |
Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 614 | sg_user[i] = (void __user *)(uintptr_t)addr; |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 615 | sg_list[i] = p; // save so we can clean up later |
| 616 | sg_indx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 618 | if (flags & SRB_DataOut) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 619 | if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){ |
| 620 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); |
| 621 | rcode = -EFAULT; |
| 622 | goto cleanup; |
| 623 | } |
| 624 | } |
| 625 | addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir); |
| 626 | |
| 627 | psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff); |
| 628 | psg->sg[i].addr[1] = cpu_to_le32(addr>>32); |
| 629 | byte_count += upsg->sg[i].count; |
| 630 | psg->sg[i].count = cpu_to_le32(upsg->sg[i].count); |
| 631 | } |
| 632 | } else { |
| 633 | struct user_sgmap* usg; |
| 634 | usg = kmalloc(actual_fibsize - sizeof(struct aac_srb) |
| 635 | + sizeof(struct sgmap), GFP_KERNEL); |
| 636 | if (!usg) { |
| 637 | dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | rcode = -ENOMEM; |
| 639 | goto cleanup; |
| 640 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 641 | memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb) |
| 642 | + sizeof(struct sgmap)); |
| 643 | actual_fibsize = actual_fibsize64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 645 | for (i = 0; i < usg->count; i++) { |
| 646 | u64 addr; |
| 647 | void* p; |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 648 | if (usg->sg[i].count > |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 649 | ((dev->adapter_info.options & |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 650 | AAC_OPT_NEW_COMM) ? |
| 651 | (dev->scsi_host_ptr->max_sectors << 9) : |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 652 | 65536)) { |
Jesper Juhl | 7dd72f5 | 2012-01-08 22:44:19 +0100 | [diff] [blame] | 653 | kfree(usg); |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 654 | rcode = -EINVAL; |
| 655 | goto cleanup; |
| 656 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 657 | /* Does this really need to be GFP_DMA? */ |
| 658 | p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA); |
Salyzyn, Mark | 6dcd4a7 | 2008-01-08 13:08:04 -0800 | [diff] [blame] | 659 | if(!p) { |
Julia Lawall | 8a52da6 | 2010-05-15 11:46:12 +0200 | [diff] [blame] | 660 | dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n", |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 661 | usg->sg[i].count,i,usg->count)); |
Julia Lawall | 8a52da6 | 2010-05-15 11:46:12 +0200 | [diff] [blame] | 662 | kfree(usg); |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 663 | rcode = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | goto cleanup; |
| 665 | } |
Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 666 | sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr; |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 667 | sg_list[i] = p; // save so we can clean up later |
| 668 | sg_indx = i; |
| 669 | |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 670 | if (flags & SRB_DataOut) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 671 | if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){ |
| 672 | kfree (usg); |
| 673 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); |
| 674 | rcode = -EFAULT; |
| 675 | goto cleanup; |
| 676 | } |
| 677 | } |
| 678 | addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir); |
| 679 | |
| 680 | psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff); |
| 681 | psg->sg[i].addr[1] = cpu_to_le32(addr>>32); |
| 682 | byte_count += usg->sg[i].count; |
| 683 | psg->sg[i].count = cpu_to_le32(usg->sg[i].count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 685 | kfree (usg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | srbcmd->count = cpu_to_le32(byte_count); |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 688 | psg->count = cpu_to_le32(sg_indx+1); |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 689 | status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } else { |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 691 | struct user_sgmap* upsg = &user_srbcmd->sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | struct sgmap* psg = &srbcmd->sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 694 | if (actual_fibsize64 == fibsize) { |
| 695 | struct user_sgmap64* usg = (struct user_sgmap64 *)upsg; |
| 696 | for (i = 0; i < upsg->count; i++) { |
Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 697 | uintptr_t addr; |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 698 | void* p; |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 699 | if (usg->sg[i].count > |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 700 | ((dev->adapter_info.options & |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 701 | AAC_OPT_NEW_COMM) ? |
| 702 | (dev->scsi_host_ptr->max_sectors << 9) : |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 703 | 65536)) { |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 704 | rcode = -EINVAL; |
| 705 | goto cleanup; |
| 706 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 707 | /* Does this really need to be GFP_DMA? */ |
| 708 | p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA); |
Salyzyn, Mark | 6dcd4a7 | 2008-01-08 13:08:04 -0800 | [diff] [blame] | 709 | if(!p) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 710 | dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n", |
| 711 | usg->sg[i].count,i,usg->count)); |
| 712 | rcode = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | goto cleanup; |
| 714 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 715 | addr = (u64)usg->sg[i].addr[0]; |
| 716 | addr += ((u64)usg->sg[i].addr[1]) << 32; |
Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 717 | sg_user[i] = (void __user *)addr; |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 718 | sg_list[i] = p; // save so we can clean up later |
| 719 | sg_indx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 721 | if (flags & SRB_DataOut) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 722 | if(copy_from_user(p,sg_user[i],usg->sg[i].count)){ |
| 723 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); |
| 724 | rcode = -EFAULT; |
| 725 | goto cleanup; |
| 726 | } |
| 727 | } |
| 728 | addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir); |
| 729 | |
| 730 | psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff); |
| 731 | byte_count += usg->sg[i].count; |
| 732 | psg->sg[i].count = cpu_to_le32(usg->sg[i].count); |
| 733 | } |
| 734 | } else { |
| 735 | for (i = 0; i < upsg->count; i++) { |
| 736 | dma_addr_t addr; |
| 737 | void* p; |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 738 | if (upsg->sg[i].count > |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 739 | ((dev->adapter_info.options & |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 740 | AAC_OPT_NEW_COMM) ? |
| 741 | (dev->scsi_host_ptr->max_sectors << 9) : |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 742 | 65536)) { |
Mark Salyzyn | 0905071 | 2008-05-28 15:32:55 -0400 | [diff] [blame] | 743 | rcode = -EINVAL; |
| 744 | goto cleanup; |
| 745 | } |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 746 | p = kmalloc(upsg->sg[i].count, GFP_KERNEL); |
Salyzyn, Mark | 6dcd4a7 | 2008-01-08 13:08:04 -0800 | [diff] [blame] | 747 | if (!p) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 748 | dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n", |
| 749 | upsg->sg[i].count, i, upsg->count)); |
| 750 | rcode = -ENOMEM; |
| 751 | goto cleanup; |
| 752 | } |
Al Viro | 142956a | 2007-10-29 05:11:28 +0000 | [diff] [blame] | 753 | sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr; |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 754 | sg_list[i] = p; // save so we can clean up later |
| 755 | sg_indx = i; |
| 756 | |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 757 | if (flags & SRB_DataOut) { |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 758 | if(copy_from_user(p, sg_user[i], |
| 759 | upsg->sg[i].count)) { |
| 760 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); |
| 761 | rcode = -EFAULT; |
| 762 | goto cleanup; |
| 763 | } |
| 764 | } |
| 765 | addr = pci_map_single(dev->pdev, p, |
| 766 | upsg->sg[i].count, data_dir); |
| 767 | |
| 768 | psg->sg[i].addr = cpu_to_le32(addr); |
| 769 | byte_count += upsg->sg[i].count; |
| 770 | psg->sg[i].count = cpu_to_le32(upsg->sg[i].count); |
| 771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | srbcmd->count = cpu_to_le32(byte_count); |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 774 | psg->count = cpu_to_le32(sg_indx+1); |
Mark Haverkamp | bfb35aa8 | 2006-02-01 09:30:55 -0800 | [diff] [blame] | 775 | status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 777 | if (status == -ERESTARTSYS) { |
| 778 | rcode = -ERESTARTSYS; |
Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 779 | goto cleanup; |
| 780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
| 782 | if (status != 0){ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 783 | dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n")); |
Mark Haverkamp | 5d497ce | 2005-06-17 13:38:04 -0700 | [diff] [blame] | 784 | rcode = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | goto cleanup; |
| 786 | } |
| 787 | |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 788 | if (flags & SRB_DataIn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | for(i = 0 ; i <= sg_indx; i++){ |
Mark Haverkamp | f2b1a06 | 2007-03-15 10:27:32 -0700 | [diff] [blame] | 790 | byte_count = le32_to_cpu( |
| 791 | (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 792 | ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count |
| 793 | : srbcmd->sg.sg[i].count); |
| 794 | if(copy_to_user(sg_user[i], sg_list[i], byte_count)){ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 795 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | rcode = -EFAULT; |
| 797 | goto cleanup; |
| 798 | |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | reply = (struct aac_srb_reply *) fib_data(srbfib); |
| 804 | if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 805 | dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | rcode = -EFAULT; |
| 807 | goto cleanup; |
| 808 | } |
| 809 | |
| 810 | cleanup: |
Mark Haverkamp | 56b5871 | 2005-04-27 06:05:51 -0700 | [diff] [blame] | 811 | kfree(user_srbcmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | for(i=0; i <= sg_indx; i++){ |
| 813 | kfree(sg_list[i]); |
| 814 | } |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 815 | if (rcode != -ERESTARTSYS) { |
Mark Haverkamp | c8f7b07 | 2006-08-03 08:02:24 -0700 | [diff] [blame] | 816 | aac_fib_complete(srbfib); |
| 817 | aac_fib_free(srbfib); |
| 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
| 820 | return rcode; |
| 821 | } |
| 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | struct aac_pci_info { |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 824 | u32 bus; |
| 825 | u32 slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | }; |
| 827 | |
| 828 | |
Adrian Bunk | 4833869 | 2005-04-25 19:45:58 -0700 | [diff] [blame] | 829 | static int aac_get_pci_info(struct aac_dev* dev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 831 | struct aac_pci_info pci_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | pci_info.bus = dev->pdev->bus->number; |
| 834 | pci_info.slot = PCI_SLOT(dev->pdev->devfn); |
| 835 | |
Salyzyn, Mark | 5234e25 | 2008-01-28 12:16:52 -0800 | [diff] [blame] | 836 | if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) { |
| 837 | dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n")); |
| 838 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 840 | return 0; |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 841 | } |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
| 844 | int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg) |
| 845 | { |
| 846 | int status; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | /* |
| 849 | * HBA gets first crack |
| 850 | */ |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | status = aac_dev_ioctl(dev, cmd, arg); |
Penchala Narasimha Reddy Chilakala, ERS-HCLTech | cacb6dc | 2009-12-21 18:39:27 +0530 | [diff] [blame] | 853 | if (status != -ENOTTY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return status; |
| 855 | |
| 856 | switch (cmd) { |
| 857 | case FSACTL_MINIPORT_REV_CHECK: |
| 858 | status = check_revision(dev, arg); |
| 859 | break; |
Mark Haverkamp | 7c00ffa | 2005-05-16 18:28:42 -0700 | [diff] [blame] | 860 | case FSACTL_SEND_LARGE_FIB: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | case FSACTL_SENDFIB: |
| 862 | status = ioctl_send_fib(dev, arg); |
| 863 | break; |
| 864 | case FSACTL_OPEN_GET_ADAPTER_FIB: |
| 865 | status = open_getadapter_fib(dev, arg); |
| 866 | break; |
| 867 | case FSACTL_GET_NEXT_ADAPTER_FIB: |
| 868 | status = next_getadapter_fib(dev, arg); |
| 869 | break; |
| 870 | case FSACTL_CLOSE_GET_ADAPTER_FIB: |
| 871 | status = close_getadapter_fib(dev, arg); |
| 872 | break; |
| 873 | case FSACTL_SEND_RAW_SRB: |
| 874 | status = aac_send_raw_srb(dev,arg); |
| 875 | break; |
| 876 | case FSACTL_GET_PCI_INFO: |
| 877 | status = aac_get_pci_info(dev,arg); |
| 878 | break; |
| 879 | default: |
| 880 | status = -ENOTTY; |
Salyzyn, Mark | 8ce3eca | 2008-01-16 07:39:06 -0800 | [diff] [blame] | 881 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
| 883 | return status; |
| 884 | } |
| 885 | |