Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Adaptec AAC series RAID controller driver | |
3 | * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com> | |
4 | * | |
5 | * based on the old aacraid driver that is.. | |
6 | * Adaptec aacraid device driver for Linux. | |
7 | * | |
8 | * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com) | |
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> | |
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> | |
43 | ||
44 | #include "aacraid.h" | |
45 | ||
46 | /** | |
47 | * ioctl_send_fib - send a FIB from userspace | |
48 | * @dev: adapter is being processed | |
49 | * @arg: arguments to the ioctl call | |
50 | * | |
51 | * This routine sends a fib to the adapter on behalf of a user level | |
52 | * program. | |
53 | */ | |
7c00ffa3 MH |
54 | # define AAC_DEBUG_PREAMBLE KERN_INFO |
55 | # define AAC_DEBUG_POSTAMBLE | |
1da177e4 LT |
56 | |
57 | static int ioctl_send_fib(struct aac_dev * dev, void __user *arg) | |
58 | { | |
59 | struct hw_fib * kfib; | |
60 | struct fib *fibptr; | |
7c00ffa3 MH |
61 | struct hw_fib * hw_fib = (struct hw_fib *)0; |
62 | dma_addr_t hw_fib_pa = (dma_addr_t)0LL; | |
63 | unsigned size; | |
64 | int retval; | |
1da177e4 LT |
65 | |
66 | fibptr = fib_alloc(dev); | |
7c00ffa3 | 67 | if(fibptr == NULL) { |
1da177e4 | 68 | return -ENOMEM; |
7c00ffa3 | 69 | } |
1da177e4 LT |
70 | |
71 | kfib = fibptr->hw_fib; | |
72 | /* | |
73 | * First copy in the header so that we can check the size field. | |
74 | */ | |
75 | if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) { | |
76 | fib_free(fibptr); | |
77 | return -EFAULT; | |
78 | } | |
79 | /* | |
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. | |
83 | */ | |
7c00ffa3 MH |
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) { | |
6e289a90 MH |
88 | if (size > 2048) { |
89 | retval = -EINVAL; | |
90 | goto cleanup; | |
91 | } | |
7c00ffa3 MH |
92 | /* Highjack the hw_fib */ |
93 | hw_fib = fibptr->hw_fib; | |
94 | hw_fib_pa = fibptr->hw_fib_pa; | |
95 | fibptr->hw_fib = kfib = pci_alloc_consistent(dev->pdev, size, &fibptr->hw_fib_pa); | |
96 | memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size); | |
97 | memcpy(kfib, hw_fib, dev->max_fib_size); | |
1da177e4 LT |
98 | } |
99 | ||
7c00ffa3 MH |
100 | if (copy_from_user(kfib, arg, size)) { |
101 | retval = -EFAULT; | |
102 | goto cleanup; | |
1da177e4 LT |
103 | } |
104 | ||
56b58712 | 105 | if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) { |
1da177e4 LT |
106 | aac_adapter_interrupt(dev); |
107 | /* | |
108 | * Since we didn't really send a fib, zero out the state to allow | |
109 | * cleanup code not to assert. | |
110 | */ | |
111 | kfib->header.XferState = 0; | |
112 | } else { | |
7c00ffa3 | 113 | retval = fib_send(le16_to_cpu(kfib->header.Command), fibptr, |
1da177e4 LT |
114 | le16_to_cpu(kfib->header.Size) , FsaNormal, |
115 | 1, 1, NULL, NULL); | |
116 | if (retval) { | |
7c00ffa3 | 117 | goto cleanup; |
1da177e4 LT |
118 | } |
119 | if (fib_complete(fibptr) != 0) { | |
7c00ffa3 MH |
120 | retval = -EINVAL; |
121 | goto cleanup; | |
1da177e4 LT |
122 | } |
123 | } | |
124 | /* | |
125 | * Make sure that the size returned by the adapter (which includes | |
126 | * the header) is less than or equal to the size of a fib, so we | |
127 | * don't corrupt application data. Then copy that size to the user | |
128 | * buffer. (Don't try to add the header information again, since it | |
129 | * was already included by the adapter.) | |
130 | */ | |
131 | ||
7c00ffa3 MH |
132 | retval = 0; |
133 | if (copy_to_user(arg, (void *)kfib, size)) | |
134 | retval = -EFAULT; | |
135 | cleanup: | |
136 | if (hw_fib) { | |
137 | pci_free_consistent(dev->pdev, size, kfib, fibptr->hw_fib_pa); | |
138 | fibptr->hw_fib_pa = hw_fib_pa; | |
139 | fibptr->hw_fib = hw_fib; | |
1da177e4 LT |
140 | } |
141 | fib_free(fibptr); | |
7c00ffa3 | 142 | return retval; |
1da177e4 LT |
143 | } |
144 | ||
145 | /** | |
146 | * open_getadapter_fib - Get the next fib | |
147 | * | |
148 | * This routine will get the next Fib, if available, from the AdapterFibContext | |
149 | * passed in from the user. | |
150 | */ | |
151 | ||
152 | static int open_getadapter_fib(struct aac_dev * dev, void __user *arg) | |
153 | { | |
154 | struct aac_fib_context * fibctx; | |
155 | int status; | |
156 | ||
157 | fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL); | |
158 | if (fibctx == NULL) { | |
159 | status = -ENOMEM; | |
160 | } else { | |
161 | unsigned long flags; | |
162 | struct list_head * entry; | |
163 | struct aac_fib_context * context; | |
164 | ||
165 | fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT; | |
166 | fibctx->size = sizeof(struct aac_fib_context); | |
167 | /* | |
168 | * Yes yes, I know this could be an index, but we have a | |
169 | * better guarantee of uniqueness for the locked loop below. | |
170 | * Without the aid of a persistent history, this also helps | |
171 | * reduce the chance that the opaque context would be reused. | |
172 | */ | |
173 | fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF); | |
174 | /* | |
175 | * Initialize the mutex used to wait for the next AIF. | |
176 | */ | |
177 | init_MUTEX_LOCKED(&fibctx->wait_sem); | |
178 | fibctx->wait = 0; | |
179 | /* | |
180 | * Initialize the fibs and set the count of fibs on | |
181 | * the list to 0. | |
182 | */ | |
183 | fibctx->count = 0; | |
184 | INIT_LIST_HEAD(&fibctx->fib_list); | |
185 | fibctx->jiffies = jiffies/HZ; | |
186 | /* | |
187 | * Now add this context onto the adapter's | |
188 | * AdapterFibContext list. | |
189 | */ | |
190 | spin_lock_irqsave(&dev->fib_lock, flags); | |
191 | /* Ensure that we have a unique identifier */ | |
192 | entry = dev->fib_list.next; | |
193 | while (entry != &dev->fib_list) { | |
194 | context = list_entry(entry, struct aac_fib_context, next); | |
195 | if (context->unique == fibctx->unique) { | |
196 | /* Not unique (32 bits) */ | |
197 | fibctx->unique++; | |
198 | entry = dev->fib_list.next; | |
199 | } else { | |
200 | entry = entry->next; | |
201 | } | |
202 | } | |
203 | list_add_tail(&fibctx->next, &dev->fib_list); | |
204 | spin_unlock_irqrestore(&dev->fib_lock, flags); | |
205 | if (copy_to_user(arg, &fibctx->unique, | |
206 | sizeof(fibctx->unique))) { | |
207 | status = -EFAULT; | |
208 | } else { | |
209 | status = 0; | |
210 | } | |
211 | } | |
212 | return status; | |
213 | } | |
214 | ||
215 | /** | |
216 | * next_getadapter_fib - get the next fib | |
217 | * @dev: adapter to use | |
218 | * @arg: ioctl argument | |
219 | * | |
220 | * This routine will get the next Fib, if available, from the AdapterFibContext | |
221 | * passed in from the user. | |
222 | */ | |
223 | ||
224 | static int next_getadapter_fib(struct aac_dev * dev, void __user *arg) | |
225 | { | |
226 | struct fib_ioctl f; | |
227 | struct fib *fib; | |
228 | struct aac_fib_context *fibctx; | |
229 | int status; | |
230 | struct list_head * entry; | |
231 | unsigned long flags; | |
232 | ||
233 | if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl))) | |
234 | return -EFAULT; | |
235 | /* | |
236 | * Verify that the HANDLE passed in was a valid AdapterFibContext | |
237 | * | |
238 | * Search the list of AdapterFibContext addresses on the adapter | |
239 | * to be sure this is a valid address | |
240 | */ | |
241 | entry = dev->fib_list.next; | |
242 | fibctx = NULL; | |
243 | ||
244 | while (entry != &dev->fib_list) { | |
245 | fibctx = list_entry(entry, struct aac_fib_context, next); | |
246 | /* | |
247 | * Extract the AdapterFibContext from the Input parameters. | |
248 | */ | |
249 | if (fibctx->unique == f.fibctx) { /* We found a winner */ | |
250 | break; | |
251 | } | |
252 | entry = entry->next; | |
253 | fibctx = NULL; | |
254 | } | |
255 | if (!fibctx) { | |
256 | dprintk ((KERN_INFO "Fib Context not found\n")); | |
257 | return -EINVAL; | |
258 | } | |
259 | ||
260 | if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) || | |
261 | (fibctx->size != sizeof(struct aac_fib_context))) { | |
262 | dprintk ((KERN_INFO "Fib Context corrupt?\n")); | |
263 | return -EINVAL; | |
264 | } | |
265 | status = 0; | |
266 | spin_lock_irqsave(&dev->fib_lock, flags); | |
267 | /* | |
268 | * If there are no fibs to send back, then either wait or return | |
269 | * -EAGAIN | |
270 | */ | |
271 | return_fib: | |
272 | if (!list_empty(&fibctx->fib_list)) { | |
273 | struct list_head * entry; | |
274 | /* | |
275 | * Pull the next fib from the fibs | |
276 | */ | |
277 | entry = fibctx->fib_list.next; | |
278 | list_del(entry); | |
279 | ||
280 | fib = list_entry(entry, struct fib, fiblink); | |
281 | fibctx->count--; | |
282 | spin_unlock_irqrestore(&dev->fib_lock, flags); | |
283 | if (copy_to_user(f.fib, fib->hw_fib, sizeof(struct hw_fib))) { | |
284 | kfree(fib->hw_fib); | |
285 | kfree(fib); | |
286 | return -EFAULT; | |
287 | } | |
288 | /* | |
289 | * Free the space occupied by this copy of the fib. | |
290 | */ | |
291 | kfree(fib->hw_fib); | |
292 | kfree(fib); | |
293 | status = 0; | |
1da177e4 LT |
294 | } else { |
295 | spin_unlock_irqrestore(&dev->fib_lock, flags); | |
296 | if (f.wait) { | |
297 | if(down_interruptible(&fibctx->wait_sem) < 0) { | |
298 | status = -EINTR; | |
299 | } else { | |
300 | /* Lock again and retry */ | |
301 | spin_lock_irqsave(&dev->fib_lock, flags); | |
302 | goto return_fib; | |
303 | } | |
304 | } else { | |
305 | status = -EAGAIN; | |
306 | } | |
307 | } | |
12a26d08 | 308 | fibctx->jiffies = jiffies/HZ; |
1da177e4 LT |
309 | return status; |
310 | } | |
311 | ||
312 | int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx) | |
313 | { | |
314 | struct fib *fib; | |
315 | ||
316 | /* | |
317 | * First free any FIBs that have not been consumed. | |
318 | */ | |
319 | while (!list_empty(&fibctx->fib_list)) { | |
320 | struct list_head * entry; | |
321 | /* | |
322 | * Pull the next fib from the fibs | |
323 | */ | |
324 | entry = fibctx->fib_list.next; | |
325 | list_del(entry); | |
326 | fib = list_entry(entry, struct fib, fiblink); | |
327 | fibctx->count--; | |
328 | /* | |
329 | * Free the space occupied by this copy of the fib. | |
330 | */ | |
331 | kfree(fib->hw_fib); | |
332 | kfree(fib); | |
333 | } | |
334 | /* | |
335 | * Remove the Context from the AdapterFibContext List | |
336 | */ | |
337 | list_del(&fibctx->next); | |
338 | /* | |
339 | * Invalidate context | |
340 | */ | |
341 | fibctx->type = 0; | |
342 | /* | |
343 | * Free the space occupied by the Context | |
344 | */ | |
345 | kfree(fibctx); | |
346 | return 0; | |
347 | } | |
348 | ||
349 | /** | |
350 | * close_getadapter_fib - close down user fib context | |
351 | * @dev: adapter | |
352 | * @arg: ioctl arguments | |
353 | * | |
354 | * This routine will close down the fibctx passed in from the user. | |
355 | */ | |
356 | ||
357 | static int close_getadapter_fib(struct aac_dev * dev, void __user *arg) | |
358 | { | |
359 | struct aac_fib_context *fibctx; | |
360 | int status; | |
361 | unsigned long flags; | |
362 | struct list_head * entry; | |
363 | ||
364 | /* | |
365 | * Verify that the HANDLE passed in was a valid AdapterFibContext | |
366 | * | |
367 | * Search the list of AdapterFibContext addresses on the adapter | |
368 | * to be sure this is a valid address | |
369 | */ | |
370 | ||
371 | entry = dev->fib_list.next; | |
372 | fibctx = NULL; | |
373 | ||
374 | while(entry != &dev->fib_list) { | |
375 | fibctx = list_entry(entry, struct aac_fib_context, next); | |
376 | /* | |
377 | * Extract the fibctx from the input parameters | |
378 | */ | |
379 | if (fibctx->unique == (u32)(unsigned long)arg) { | |
380 | /* We found a winner */ | |
381 | break; | |
382 | } | |
383 | entry = entry->next; | |
384 | fibctx = NULL; | |
385 | } | |
386 | ||
387 | if (!fibctx) | |
388 | return 0; /* Already gone */ | |
389 | ||
390 | if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) || | |
391 | (fibctx->size != sizeof(struct aac_fib_context))) | |
392 | return -EINVAL; | |
393 | spin_lock_irqsave(&dev->fib_lock, flags); | |
394 | status = aac_close_fib_context(dev, fibctx); | |
395 | spin_unlock_irqrestore(&dev->fib_lock, flags); | |
396 | return status; | |
397 | } | |
398 | ||
399 | /** | |
400 | * check_revision - close down user fib context | |
401 | * @dev: adapter | |
402 | * @arg: ioctl arguments | |
403 | * | |
404 | * This routine returns the driver version. | |
405 | * Under Linux, there have been no version incompatibilities, so this is | |
406 | * simple! | |
407 | */ | |
408 | ||
409 | static int check_revision(struct aac_dev *dev, void __user *arg) | |
410 | { | |
411 | struct revision response; | |
c7f47602 MH |
412 | char *driver_version = aac_driver_version; |
413 | u32 version; | |
414 | ||
9f30a323 | 415 | response.compat = 1; |
c7f47602 MH |
416 | version = (simple_strtol(driver_version, |
417 | &driver_version, 10) << 24) | 0x00000400; | |
418 | version += simple_strtol(driver_version + 1, &driver_version, 10) << 16; | |
419 | version += simple_strtol(driver_version + 1, NULL, 10); | |
420 | response.version = cpu_to_le32(version); | |
421 | # if (defined(AAC_DRIVER_BUILD)) | |
422 | response.build = cpu_to_le32(AAC_DRIVER_BUILD); | |
423 | # else | |
424 | response.build = cpu_to_le32(9999); | |
425 | # endif | |
1da177e4 LT |
426 | |
427 | if (copy_to_user(arg, &response, sizeof(response))) | |
428 | return -EFAULT; | |
429 | return 0; | |
430 | } | |
431 | ||
7c00ffa3 | 432 | |
1da177e4 LT |
433 | /** |
434 | * | |
435 | * aac_send_raw_scb | |
436 | * | |
437 | */ | |
438 | ||
4833869e | 439 | static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg) |
1da177e4 LT |
440 | { |
441 | struct fib* srbfib; | |
442 | int status; | |
56b58712 MH |
443 | struct aac_srb *srbcmd = NULL; |
444 | struct user_aac_srb *user_srbcmd = NULL; | |
445 | struct user_aac_srb __user *user_srb = arg; | |
1da177e4 LT |
446 | struct aac_srb_reply __user *user_reply; |
447 | struct aac_srb_reply* reply; | |
448 | u32 fibsize = 0; | |
449 | u32 flags = 0; | |
450 | s32 rcode = 0; | |
451 | u32 data_dir; | |
452 | void __user *sg_user[32]; | |
453 | void *sg_list[32]; | |
454 | u32 sg_indx = 0; | |
455 | u32 byte_count = 0; | |
456 | u32 actual_fibsize = 0; | |
457 | int i; | |
458 | ||
459 | ||
460 | if (!capable(CAP_SYS_ADMIN)){ | |
7c00ffa3 | 461 | dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n")); |
1da177e4 LT |
462 | return -EPERM; |
463 | } | |
464 | /* | |
465 | * Allocate and initialize a Fib then setup a BlockWrite command | |
466 | */ | |
467 | if (!(srbfib = fib_alloc(dev))) { | |
5d497cec | 468 | return -ENOMEM; |
1da177e4 LT |
469 | } |
470 | fib_init(srbfib); | |
471 | ||
472 | srbcmd = (struct aac_srb*) fib_data(srbfib); | |
473 | ||
7c00ffa3 | 474 | memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */ |
1da177e4 | 475 | if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){ |
7c00ffa3 | 476 | dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n")); |
1da177e4 LT |
477 | rcode = -EFAULT; |
478 | goto cleanup; | |
479 | } | |
480 | ||
7c00ffa3 | 481 | if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) { |
1da177e4 LT |
482 | rcode = -EINVAL; |
483 | goto cleanup; | |
484 | } | |
485 | ||
4645df10 | 486 | user_srbcmd = kmalloc(fibsize, GFP_KERNEL); |
7c00ffa3 MH |
487 | if (!user_srbcmd) { |
488 | dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n")); | |
489 | rcode = -ENOMEM; | |
490 | goto cleanup; | |
491 | } | |
56b58712 | 492 | if(copy_from_user(user_srbcmd, user_srb,fibsize)){ |
7c00ffa3 | 493 | dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n")); |
1da177e4 LT |
494 | rcode = -EFAULT; |
495 | goto cleanup; | |
496 | } | |
497 | ||
498 | user_reply = arg+fibsize; | |
499 | ||
56b58712 | 500 | flags = user_srbcmd->flags; /* from user in cpu order */ |
1da177e4 | 501 | // Fix up srb for endian and force some values |
56b58712 | 502 | |
1da177e4 | 503 | srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this |
7c00ffa3 MH |
504 | srbcmd->channel = cpu_to_le32(user_srbcmd->channel); |
505 | srbcmd->id = cpu_to_le32(user_srbcmd->id); | |
506 | srbcmd->lun = cpu_to_le32(user_srbcmd->lun); | |
7c00ffa3 | 507 | srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout); |
5d497cec MH |
508 | srbcmd->flags = cpu_to_le32(flags); |
509 | srbcmd->retry_limit = 0; // Obsolete parameter | |
56b58712 | 510 | srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size); |
5d497cec | 511 | memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb)); |
1da177e4 | 512 | |
56b58712 | 513 | switch (flags & (SRB_DataIn | SRB_DataOut)) { |
1da177e4 LT |
514 | case SRB_DataOut: |
515 | data_dir = DMA_TO_DEVICE; | |
516 | break; | |
517 | case (SRB_DataIn | SRB_DataOut): | |
518 | data_dir = DMA_BIDIRECTIONAL; | |
519 | break; | |
520 | case SRB_DataIn: | |
521 | data_dir = DMA_FROM_DEVICE; | |
522 | break; | |
523 | default: | |
524 | data_dir = DMA_NONE; | |
525 | } | |
5d497cec | 526 | if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) { |
7c00ffa3 MH |
527 | dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n", |
528 | le32_to_cpu(srbcmd->sg.count))); | |
529 | rcode = -EINVAL; | |
530 | goto cleanup; | |
531 | } | |
1da177e4 | 532 | if (dev->dac_support == 1) { |
56b58712 | 533 | struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg; |
84e29308 | 534 | struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg; |
7c00ffa3 | 535 | struct user_sgmap* usg; |
1da177e4 LT |
536 | byte_count = 0; |
537 | ||
538 | /* | |
539 | * This should also catch if user used the 32 bit sgmap | |
540 | */ | |
541 | actual_fibsize = sizeof(struct aac_srb) - | |
7c00ffa3 MH |
542 | sizeof(struct sgentry) + |
543 | ((upsg->count & 0xff) * | |
544 | sizeof(struct sgentry)); | |
1da177e4 | 545 | if(actual_fibsize != fibsize){ // User made a mistake - should not continue |
7c00ffa3 | 546 | dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n")); |
1da177e4 LT |
547 | rcode = -EINVAL; |
548 | goto cleanup; | |
549 | } | |
7c00ffa3 MH |
550 | usg = kmalloc(actual_fibsize - sizeof(struct aac_srb) |
551 | + sizeof(struct sgmap), GFP_KERNEL); | |
552 | if (!usg) { | |
553 | dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n")); | |
554 | rcode = -ENOMEM; | |
555 | goto cleanup; | |
556 | } | |
557 | memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb) | |
558 | + sizeof(struct sgmap)); | |
559 | actual_fibsize = sizeof(struct aac_srb) - | |
560 | sizeof(struct sgentry) + ((usg->count & 0xff) * | |
561 | sizeof(struct sgentry64)); | |
562 | if ((data_dir == DMA_NONE) && upsg->count) { | |
563 | kfree (usg); | |
564 | dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n")); | |
1da177e4 LT |
565 | rcode = -EINVAL; |
566 | goto cleanup; | |
567 | } | |
568 | ||
7c00ffa3 MH |
569 | for (i = 0; i < usg->count; i++) { |
570 | u64 addr; | |
1da177e4 | 571 | void* p; |
7c00ffa3 MH |
572 | /* Does this really need to be GFP_DMA? */ |
573 | p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA); | |
1da177e4 | 574 | if(p == 0) { |
7c00ffa3 MH |
575 | kfree (usg); |
576 | dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n", | |
577 | usg->sg[i].count,i,usg->count)); | |
1da177e4 LT |
578 | rcode = -ENOMEM; |
579 | goto cleanup; | |
580 | } | |
e75d5176 | 581 | sg_user[i] = (void __user *)(long)usg->sg[i].addr; |
1da177e4 LT |
582 | sg_list[i] = p; // save so we can clean up later |
583 | sg_indx = i; | |
584 | ||
585 | if( flags & SRB_DataOut ){ | |
56b58712 | 586 | if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){ |
7c00ffa3 MH |
587 | kfree (usg); |
588 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); | |
1da177e4 LT |
589 | rcode = -EFAULT; |
590 | goto cleanup; | |
591 | } | |
592 | } | |
7c00ffa3 | 593 | addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir); |
1da177e4 | 594 | |
56b58712 | 595 | psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff); |
7c00ffa3 MH |
596 | psg->sg[i].addr[1] = cpu_to_le32(addr>>32); |
597 | psg->sg[i].count = cpu_to_le32(usg->sg[i].count); | |
598 | byte_count += usg->sg[i].count; | |
1da177e4 | 599 | } |
7c00ffa3 | 600 | kfree (usg); |
1da177e4 LT |
601 | |
602 | srbcmd->count = cpu_to_le32(byte_count); | |
7c00ffa3 | 603 | psg->count = cpu_to_le32(sg_indx+1); |
1da177e4 LT |
604 | status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL); |
605 | } else { | |
56b58712 | 606 | struct user_sgmap* upsg = &user_srbcmd->sg; |
1da177e4 LT |
607 | struct sgmap* psg = &srbcmd->sg; |
608 | byte_count = 0; | |
609 | ||
5d497cec | 610 | actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry)); |
1da177e4 | 611 | if(actual_fibsize != fibsize){ // User made a mistake - should not continue |
7c00ffa3 | 612 | dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n")); |
1da177e4 LT |
613 | rcode = -EINVAL; |
614 | goto cleanup; | |
615 | } | |
56b58712 | 616 | if ((data_dir == DMA_NONE) && upsg->count) { |
7c00ffa3 | 617 | dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n")); |
1da177e4 LT |
618 | rcode = -EINVAL; |
619 | goto cleanup; | |
620 | } | |
56b58712 | 621 | for (i = 0; i < upsg->count; i++) { |
1da177e4 LT |
622 | dma_addr_t addr; |
623 | void* p; | |
56b58712 | 624 | p = kmalloc(upsg->sg[i].count, GFP_KERNEL); |
1da177e4 | 625 | if(p == 0) { |
7c00ffa3 MH |
626 | dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n", |
627 | upsg->sg[i].count, i, upsg->count)); | |
1da177e4 LT |
628 | rcode = -ENOMEM; |
629 | goto cleanup; | |
630 | } | |
e75d5176 | 631 | sg_user[i] = (void __user *)(long)upsg->sg[i].addr; |
1da177e4 LT |
632 | sg_list[i] = p; // save so we can clean up later |
633 | sg_indx = i; | |
634 | ||
635 | if( flags & SRB_DataOut ){ | |
7c00ffa3 MH |
636 | if(copy_from_user(p, sg_user[i], |
637 | upsg->sg[i].count)) { | |
638 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n")); | |
1da177e4 LT |
639 | rcode = -EFAULT; |
640 | goto cleanup; | |
641 | } | |
642 | } | |
7c00ffa3 MH |
643 | addr = pci_map_single(dev->pdev, p, |
644 | upsg->sg[i].count, data_dir); | |
1da177e4 LT |
645 | |
646 | psg->sg[i].addr = cpu_to_le32(addr); | |
56b58712 MH |
647 | psg->sg[i].count = cpu_to_le32(upsg->sg[i].count); |
648 | byte_count += upsg->sg[i].count; | |
1da177e4 LT |
649 | } |
650 | srbcmd->count = cpu_to_le32(byte_count); | |
7c00ffa3 | 651 | psg->count = cpu_to_le32(sg_indx+1); |
1da177e4 LT |
652 | status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL); |
653 | } | |
654 | ||
655 | if (status != 0){ | |
7c00ffa3 | 656 | dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n")); |
5d497cec | 657 | rcode = -ENXIO; |
1da177e4 LT |
658 | goto cleanup; |
659 | } | |
660 | ||
661 | if( flags & SRB_DataIn ) { | |
662 | for(i = 0 ; i <= sg_indx; i++){ | |
7c00ffa3 MH |
663 | byte_count = le32_to_cpu((dev->dac_support == 1) |
664 | ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count | |
665 | : srbcmd->sg.sg[i].count); | |
666 | if(copy_to_user(sg_user[i], sg_list[i], byte_count)){ | |
667 | dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n")); | |
1da177e4 LT |
668 | rcode = -EFAULT; |
669 | goto cleanup; | |
670 | ||
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | reply = (struct aac_srb_reply *) fib_data(srbfib); | |
676 | if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){ | |
7c00ffa3 | 677 | dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n")); |
1da177e4 LT |
678 | rcode = -EFAULT; |
679 | goto cleanup; | |
680 | } | |
681 | ||
682 | cleanup: | |
56b58712 | 683 | kfree(user_srbcmd); |
1da177e4 LT |
684 | for(i=0; i <= sg_indx; i++){ |
685 | kfree(sg_list[i]); | |
686 | } | |
687 | fib_complete(srbfib); | |
688 | fib_free(srbfib); | |
689 | ||
690 | return rcode; | |
691 | } | |
692 | ||
1da177e4 LT |
693 | struct aac_pci_info { |
694 | u32 bus; | |
695 | u32 slot; | |
696 | }; | |
697 | ||
698 | ||
4833869e | 699 | static int aac_get_pci_info(struct aac_dev* dev, void __user *arg) |
1da177e4 LT |
700 | { |
701 | struct aac_pci_info pci_info; | |
702 | ||
703 | pci_info.bus = dev->pdev->bus->number; | |
704 | pci_info.slot = PCI_SLOT(dev->pdev->devfn); | |
705 | ||
706 | if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) { | |
7c00ffa3 | 707 | dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n")); |
1da177e4 LT |
708 | return -EFAULT; |
709 | } | |
710 | return 0; | |
7c00ffa3 | 711 | } |
1da177e4 LT |
712 | |
713 | ||
714 | int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg) | |
715 | { | |
716 | int status; | |
717 | ||
718 | /* | |
719 | * HBA gets first crack | |
720 | */ | |
721 | ||
722 | status = aac_dev_ioctl(dev, cmd, arg); | |
723 | if(status != -ENOTTY) | |
724 | return status; | |
725 | ||
726 | switch (cmd) { | |
727 | case FSACTL_MINIPORT_REV_CHECK: | |
728 | status = check_revision(dev, arg); | |
729 | break; | |
7c00ffa3 | 730 | case FSACTL_SEND_LARGE_FIB: |
1da177e4 LT |
731 | case FSACTL_SENDFIB: |
732 | status = ioctl_send_fib(dev, arg); | |
733 | break; | |
734 | case FSACTL_OPEN_GET_ADAPTER_FIB: | |
735 | status = open_getadapter_fib(dev, arg); | |
736 | break; | |
737 | case FSACTL_GET_NEXT_ADAPTER_FIB: | |
738 | status = next_getadapter_fib(dev, arg); | |
739 | break; | |
740 | case FSACTL_CLOSE_GET_ADAPTER_FIB: | |
741 | status = close_getadapter_fib(dev, arg); | |
742 | break; | |
743 | case FSACTL_SEND_RAW_SRB: | |
744 | status = aac_send_raw_srb(dev,arg); | |
745 | break; | |
746 | case FSACTL_GET_PCI_INFO: | |
747 | status = aac_get_pci_info(dev,arg); | |
748 | break; | |
749 | default: | |
750 | status = -ENOTTY; | |
751 | break; | |
752 | } | |
753 | return status; | |
754 | } | |
755 |