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