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