1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Adaptec AAC series RAID controller driver
4 * (c) Copyright 2001 Red Hat Inc.
6 * based on the old aacraid driver that is..
7 * Adaptec aacraid device driver for Linux.
9 * Copyright (c) 2000-2010 Adaptec, Inc.
16 * Abstract: Contains all routines for control of the AFA comm layer
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/completion.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/blkdev.h>
28 #include <linux/compat.h>
29 #include <linux/delay.h> /* ssleep prototype */
30 #include <linux/kthread.h>
31 #include <linux/uaccess.h>
32 #include <scsi/scsi_host.h>
36 # define AAC_DEBUG_PREAMBLE KERN_INFO
37 # define AAC_DEBUG_POSTAMBLE
39 * ioctl_send_fib - send a FIB from userspace
40 * @dev: adapter is being processed
41 * @arg: arguments to the ioctl call
43 * This routine sends a fib to the adapter on behalf of a user level
46 static int ioctl_send_fib(struct aac_dev * dev, void __user *arg)
50 struct hw_fib * hw_fib = (struct hw_fib *)0;
51 dma_addr_t hw_fib_pa = (dma_addr_t)0LL;
52 unsigned int size, osize;
58 fibptr = aac_fib_alloc(dev);
63 kfib = fibptr->hw_fib_va;
65 * First copy in the header so that we can check the size field.
67 if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
72 * Since we copy based on the fib header size, make sure that we
73 * will not overrun the buffer when we copy the memory. Return
74 * an error if we would.
76 osize = size = le16_to_cpu(kfib->header.Size) +
77 sizeof(struct aac_fibhdr);
78 if (size < le16_to_cpu(kfib->header.SenderSize))
79 size = le16_to_cpu(kfib->header.SenderSize);
80 if (size > dev->max_fib_size) {
88 kfib = dma_alloc_coherent(&dev->pdev->dev, size, &daddr,
95 /* Highjack the hw_fib */
96 hw_fib = fibptr->hw_fib_va;
97 hw_fib_pa = fibptr->hw_fib_pa;
98 fibptr->hw_fib_va = kfib;
99 fibptr->hw_fib_pa = daddr;
100 memset(((char *)kfib) + dev->max_fib_size, 0, size - dev->max_fib_size);
101 memcpy(kfib, hw_fib, dev->max_fib_size);
104 if (copy_from_user(kfib, arg, size)) {
109 /* Sanity check the second copy */
110 if ((osize != le16_to_cpu(kfib->header.Size) +
111 sizeof(struct aac_fibhdr))
112 || (size < le16_to_cpu(kfib->header.SenderSize))) {
117 if (kfib->header.Command == cpu_to_le16(TakeABreakPt)) {
118 aac_adapter_interrupt(dev);
120 * Since we didn't really send a fib, zero out the state to allow
121 * cleanup code not to assert.
123 kfib->header.XferState = 0;
125 retval = aac_fib_send(le16_to_cpu(kfib->header.Command), fibptr,
126 le16_to_cpu(kfib->header.Size) , FsaNormal,
131 if (aac_fib_complete(fibptr) != 0) {
137 * Make sure that the size returned by the adapter (which includes
138 * the header) is less than or equal to the size of a fib, so we
139 * don't corrupt application data. Then copy that size to the user
140 * buffer. (Don't try to add the header information again, since it
141 * was already included by the adapter.)
145 if (copy_to_user(arg, (void *)kfib, size))
149 dma_free_coherent(&dev->pdev->dev, size, kfib,
151 fibptr->hw_fib_pa = hw_fib_pa;
152 fibptr->hw_fib_va = hw_fib;
154 if (retval != -ERESTARTSYS)
155 aac_fib_free(fibptr);
160 * open_getadapter_fib - Get the next fib
161 * @dev: adapter is being processed
162 * @arg: arguments to the open call
164 * This routine will get the next Fib, if available, from the AdapterFibContext
165 * passed in from the user.
167 static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)
169 struct aac_fib_context * fibctx;
172 fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
173 if (fibctx == NULL) {
177 struct list_head * entry;
178 struct aac_fib_context * context;
180 fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
181 fibctx->size = sizeof(struct aac_fib_context);
183 * Yes yes, I know this could be an index, but we have a
184 * better guarantee of uniqueness for the locked loop below.
185 * Without the aid of a persistent history, this also helps
186 * reduce the chance that the opaque context would be reused.
188 fibctx->unique = (u32)((ulong)fibctx & 0xFFFFFFFF);
190 * Initialize the mutex used to wait for the next AIF.
192 init_completion(&fibctx->completion);
195 * Initialize the fibs and set the count of fibs on
199 INIT_LIST_HEAD(&fibctx->fib_list);
200 fibctx->jiffies = jiffies/HZ;
202 * Now add this context onto the adapter's
203 * AdapterFibContext list.
205 spin_lock_irqsave(&dev->fib_lock, flags);
206 /* Ensure that we have a unique identifier */
207 entry = dev->fib_list.next;
208 while (entry != &dev->fib_list) {
209 context = list_entry(entry, struct aac_fib_context, next);
210 if (context->unique == fibctx->unique) {
211 /* Not unique (32 bits) */
213 entry = dev->fib_list.next;
218 list_add_tail(&fibctx->next, &dev->fib_list);
219 spin_unlock_irqrestore(&dev->fib_lock, flags);
220 if (copy_to_user(arg, &fibctx->unique,
221 sizeof(fibctx->unique))) {
230 struct compat_fib_ioctl {
237 * next_getadapter_fib - get the next fib
238 * @dev: adapter to use
239 * @arg: ioctl argument
241 * This routine will get the next Fib, if available, from the AdapterFibContext
242 * passed in from the user.
244 static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)
248 struct aac_fib_context *fibctx;
250 struct list_head * entry;
253 if (in_compat_syscall()) {
254 struct compat_fib_ioctl cf;
256 if (copy_from_user(&cf, arg, sizeof(struct compat_fib_ioctl)))
259 f.fibctx = cf.fibctx;
261 f.fib = compat_ptr(cf.fib);
263 if (copy_from_user(&f, arg, sizeof(struct fib_ioctl)))
267 * Verify that the HANDLE passed in was a valid AdapterFibContext
269 * Search the list of AdapterFibContext addresses on the adapter
270 * to be sure this is a valid address
272 spin_lock_irqsave(&dev->fib_lock, flags);
273 entry = dev->fib_list.next;
276 while (entry != &dev->fib_list) {
277 fibctx = list_entry(entry, struct aac_fib_context, next);
279 * Extract the AdapterFibContext from the Input parameters.
281 if (fibctx->unique == f.fibctx) { /* We found a winner */
288 spin_unlock_irqrestore(&dev->fib_lock, flags);
289 dprintk ((KERN_INFO "Fib Context not found\n"));
293 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
294 (fibctx->size != sizeof(struct aac_fib_context))) {
295 spin_unlock_irqrestore(&dev->fib_lock, flags);
296 dprintk ((KERN_INFO "Fib Context corrupt?\n"));
301 * If there are no fibs to send back, then either wait or return
305 if (!list_empty(&fibctx->fib_list)) {
307 * Pull the next fib from the fibs
309 entry = fibctx->fib_list.next;
312 fib = list_entry(entry, struct fib, fiblink);
314 spin_unlock_irqrestore(&dev->fib_lock, flags);
315 if (copy_to_user(f.fib, fib->hw_fib_va, sizeof(struct hw_fib))) {
316 kfree(fib->hw_fib_va);
321 * Free the space occupied by this copy of the fib.
323 kfree(fib->hw_fib_va);
327 spin_unlock_irqrestore(&dev->fib_lock, flags);
328 /* If someone killed the AIF aacraid thread, restart it */
329 status = !dev->aif_thread;
330 if (status && !dev->in_reset && dev->queues && dev->fsa_dev) {
331 /* Be paranoid, be very paranoid! */
332 kthread_stop(dev->thread);
335 dev->thread = kthread_run(aac_command_thread, dev,
340 if (wait_for_completion_interruptible(&fibctx->completion) < 0) {
341 status = -ERESTARTSYS;
343 /* Lock again and retry */
344 spin_lock_irqsave(&dev->fib_lock, flags);
351 fibctx->jiffies = jiffies/HZ;
355 int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
360 * First free any FIBs that have not been consumed.
362 while (!list_empty(&fibctx->fib_list)) {
363 struct list_head * entry;
365 * Pull the next fib from the fibs
367 entry = fibctx->fib_list.next;
369 fib = list_entry(entry, struct fib, fiblink);
372 * Free the space occupied by this copy of the fib.
374 kfree(fib->hw_fib_va);
378 * Remove the Context from the AdapterFibContext List
380 list_del(&fibctx->next);
386 * Free the space occupied by the Context
393 * close_getadapter_fib - close down user fib context
395 * @arg: ioctl arguments
397 * This routine will close down the fibctx passed in from the user.
400 static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
402 struct aac_fib_context *fibctx;
405 struct list_head * entry;
408 * Verify that the HANDLE passed in was a valid AdapterFibContext
410 * Search the list of AdapterFibContext addresses on the adapter
411 * to be sure this is a valid address
414 entry = dev->fib_list.next;
417 while(entry != &dev->fib_list) {
418 fibctx = list_entry(entry, struct aac_fib_context, next);
420 * Extract the fibctx from the input parameters
422 if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
429 return 0; /* Already gone */
431 if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
432 (fibctx->size != sizeof(struct aac_fib_context)))
434 spin_lock_irqsave(&dev->fib_lock, flags);
435 status = aac_close_fib_context(dev, fibctx);
436 spin_unlock_irqrestore(&dev->fib_lock, flags);
441 * check_revision - close down user fib context
443 * @arg: ioctl arguments
445 * This routine returns the driver version.
446 * Under Linux, there have been no version incompatibilities, so this is
450 static int check_revision(struct aac_dev *dev, void __user *arg)
452 struct revision response;
453 char *driver_version = aac_driver_version;
457 version = (simple_strtol(driver_version,
458 &driver_version, 10) << 24) | 0x00000400;
459 version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
460 version += simple_strtol(driver_version + 1, NULL, 10);
461 response.version = cpu_to_le32(version);
462 # ifdef AAC_DRIVER_BUILD
463 response.build = cpu_to_le32(AAC_DRIVER_BUILD);
465 response.build = cpu_to_le32(9999);
468 if (copy_to_user(arg, &response, sizeof(response)))
476 * @dev: adapter is being processed
477 * @arg: arguments to the send call
479 static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
483 struct aac_srb *srbcmd = NULL;
484 struct aac_hba_cmd_req *hbacmd = NULL;
485 struct user_aac_srb *user_srbcmd = NULL;
486 struct user_aac_srb __user *user_srb = arg;
487 struct aac_srb_reply __user *user_reply;
493 void __user *sg_user[HBA_MAX_SG_EMBEDDED];
494 void *sg_list[HBA_MAX_SG_EMBEDDED];
495 u32 sg_count[HBA_MAX_SG_EMBEDDED];
498 u32 actual_fibsize64, actual_fibsize = 0;
500 int is_native_device;
505 dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
508 if (!capable(CAP_SYS_ADMIN)){
509 dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
513 * Allocate and initialize a Fib then setup a SRB command
515 if (!(srbfib = aac_fib_alloc(dev))) {
519 memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
520 if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
521 dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
526 if ((fibsize < (sizeof(struct user_aac_srb) - sizeof(struct user_sgentry))) ||
527 (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))) {
532 user_srbcmd = memdup_user(user_srb, fibsize);
533 if (IS_ERR(user_srbcmd)) {
534 rcode = PTR_ERR(user_srbcmd);
539 flags = user_srbcmd->flags; /* from user in cpu order */
540 switch (flags & (SRB_DataIn | SRB_DataOut)) {
542 data_dir = DMA_TO_DEVICE;
544 case (SRB_DataIn | SRB_DataOut):
545 data_dir = DMA_BIDIRECTIONAL;
548 data_dir = DMA_FROM_DEVICE;
553 if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
554 dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
555 user_srbcmd->sg.count));
559 if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
560 dprintk((KERN_DEBUG"aacraid:SG with no direction specified\n"));
564 actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
565 ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
566 actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
567 (sizeof(struct sgentry64) - sizeof(struct sgentry));
568 /* User made a mistake - should not continue */
569 if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
570 dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
571 "Raw SRB command calculated fibsize=%lu;%lu "
572 "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
573 "issued fibsize=%d\n",
574 actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
575 sizeof(struct aac_srb), sizeof(struct sgentry),
576 sizeof(struct sgentry64), fibsize));
581 chn = user_srbcmd->channel;
582 if (chn < AAC_MAX_BUSES && user_srbcmd->id < AAC_MAX_TARGETS &&
583 dev->hba_map[chn][user_srbcmd->id].devtype ==
584 AAC_DEVTYPE_NATIVE_RAW) {
585 is_native_device = 1;
586 hbacmd = (struct aac_hba_cmd_req *)srbfib->hw_fib_va;
587 memset(hbacmd, 0, 96); /* sizeof(*hbacmd) is not necessary */
589 /* iu_type is a parameter of aac_hba_send */
594 case DMA_FROM_DEVICE:
595 case DMA_BIDIRECTIONAL:
602 hbacmd->lun[1] = cpu_to_le32(user_srbcmd->lun);
603 hbacmd->it_nexus = dev->hba_map[chn][user_srbcmd->id].rmw_nexus;
606 * we fill in reply_qid later in aac_src_deliver_message
607 * we fill in iu_type, request_id later in aac_hba_send
608 * we fill in emb_data_desc_count, data_length later
612 memcpy(hbacmd->cdb, user_srbcmd->cdb, sizeof(hbacmd->cdb));
614 address = (u64)srbfib->hw_error_pa;
615 hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
616 hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
617 hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
618 hbacmd->emb_data_desc_count =
619 cpu_to_le32(user_srbcmd->sg.count);
620 srbfib->hbacmd_size = 64 +
621 user_srbcmd->sg.count * sizeof(struct aac_hba_sgl);
624 is_native_device = 0;
625 aac_fib_init(srbfib);
627 /* raw_srb FIB is not FastResponseCapable */
628 srbfib->hw_fib_va->header.XferState &=
629 ~cpu_to_le32(FastResponseCapable);
631 srbcmd = (struct aac_srb *) fib_data(srbfib);
633 // Fix up srb for endian and force some values
635 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
636 srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
637 srbcmd->id = cpu_to_le32(user_srbcmd->id);
638 srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
639 srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
640 srbcmd->flags = cpu_to_le32(flags);
641 srbcmd->retry_limit = 0; // Obsolete parameter
642 srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
643 memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
647 if (is_native_device) {
648 struct user_sgmap *usg32 = &user_srbcmd->sg;
649 struct user_sgmap64 *usg64 =
650 (struct user_sgmap64 *)&user_srbcmd->sg;
652 for (i = 0; i < usg32->count; i++) {
656 sg_count[i] = (actual_fibsize64 == fibsize) ?
657 usg64->sg[i].count : usg32->sg[i].count;
659 (dev->scsi_host_ptr->max_sectors << 9)) {
660 pr_err("aacraid: upsg->sg[%d].count=%u>%u\n",
662 dev->scsi_host_ptr->max_sectors << 9);
667 p = kmalloc(sg_count[i], GFP_KERNEL);
673 if (actual_fibsize64 == fibsize) {
674 addr = (u64)usg64->sg[i].addr[0];
675 addr += ((u64)usg64->sg[i].addr[1]) << 32;
677 addr = (u64)usg32->sg[i].addr;
680 sg_user[i] = (void __user *)(uintptr_t)addr;
681 sg_list[i] = p; // save so we can clean up later
684 if (flags & SRB_DataOut) {
685 if (copy_from_user(p, sg_user[i],
691 addr = dma_map_single(&dev->pdev->dev, p, sg_count[i],
693 hbacmd->sge[i].addr_hi = cpu_to_le32((u32)(addr>>32));
694 hbacmd->sge[i].addr_lo = cpu_to_le32(
695 (u32)(addr & 0xffffffff));
696 hbacmd->sge[i].len = cpu_to_le32(sg_count[i]);
697 hbacmd->sge[i].flags = 0;
698 byte_count += sg_count[i];
701 if (usg32->count > 0) /* embedded sglist */
702 hbacmd->sge[usg32->count-1].flags =
703 cpu_to_le32(0x40000000);
704 hbacmd->data_length = cpu_to_le32(byte_count);
706 status = aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, srbfib,
709 } else if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
710 struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
711 struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
714 * This should also catch if user used the 32 bit sgmap
716 if (actual_fibsize64 == fibsize) {
717 actual_fibsize = actual_fibsize64;
718 for (i = 0; i < upsg->count; i++) {
722 sg_count[i] = upsg->sg[i].count;
724 ((dev->adapter_info.options &
726 (dev->scsi_host_ptr->max_sectors << 9) :
732 p = kmalloc(sg_count[i], GFP_KERNEL);
734 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
735 sg_count[i], i, upsg->count));
739 addr = (u64)upsg->sg[i].addr[0];
740 addr += ((u64)upsg->sg[i].addr[1]) << 32;
741 sg_user[i] = (void __user *)(uintptr_t)addr;
742 sg_list[i] = p; // save so we can clean up later
745 if (flags & SRB_DataOut) {
746 if (copy_from_user(p, sg_user[i],
748 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
753 addr = dma_map_single(&dev->pdev->dev, p,
754 sg_count[i], data_dir);
756 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
757 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
758 byte_count += sg_count[i];
759 psg->sg[i].count = cpu_to_le32(sg_count[i]);
762 struct user_sgmap* usg;
764 actual_fibsize - sizeof(struct aac_srb)
765 + sizeof(struct sgmap), GFP_KERNEL);
767 dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
771 actual_fibsize = actual_fibsize64;
773 for (i = 0; i < usg->count; i++) {
777 sg_count[i] = usg->sg[i].count;
779 ((dev->adapter_info.options &
781 (dev->scsi_host_ptr->max_sectors << 9) :
788 p = kmalloc(sg_count[i], GFP_KERNEL);
790 dprintk((KERN_DEBUG "aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
791 sg_count[i], i, usg->count));
796 sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
797 sg_list[i] = p; // save so we can clean up later
800 if (flags & SRB_DataOut) {
801 if (copy_from_user(p, sg_user[i],
804 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
809 addr = dma_map_single(&dev->pdev->dev, p,
810 sg_count[i], data_dir);
812 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
813 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
814 byte_count += sg_count[i];
815 psg->sg[i].count = cpu_to_le32(sg_count[i]);
819 srbcmd->count = cpu_to_le32(byte_count);
820 if (user_srbcmd->sg.count)
821 psg->count = cpu_to_le32(sg_indx+1);
824 status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
826 struct user_sgmap* upsg = &user_srbcmd->sg;
827 struct sgmap* psg = &srbcmd->sg;
829 if (actual_fibsize64 == fibsize) {
830 struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
831 for (i = 0; i < upsg->count; i++) {
835 sg_count[i] = usg->sg[i].count;
837 ((dev->adapter_info.options &
839 (dev->scsi_host_ptr->max_sectors << 9) :
844 p = kmalloc(sg_count[i], GFP_KERNEL);
846 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
847 sg_count[i], i, usg->count));
851 addr = (u64)usg->sg[i].addr[0];
852 addr += ((u64)usg->sg[i].addr[1]) << 32;
853 sg_user[i] = (void __user *)addr;
854 sg_list[i] = p; // save so we can clean up later
857 if (flags & SRB_DataOut) {
858 if (copy_from_user(p, sg_user[i],
860 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
865 addr = dma_map_single(&dev->pdev->dev, p,
869 psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
870 byte_count += usg->sg[i].count;
871 psg->sg[i].count = cpu_to_le32(sg_count[i]);
874 for (i = 0; i < upsg->count; i++) {
878 sg_count[i] = upsg->sg[i].count;
880 ((dev->adapter_info.options &
882 (dev->scsi_host_ptr->max_sectors << 9) :
887 p = kmalloc(sg_count[i], GFP_KERNEL);
889 dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
890 sg_count[i], i, upsg->count));
894 sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
895 sg_list[i] = p; // save so we can clean up later
898 if (flags & SRB_DataOut) {
899 if (copy_from_user(p, sg_user[i],
901 dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
906 addr = dma_map_single(&dev->pdev->dev, p,
907 sg_count[i], data_dir);
909 psg->sg[i].addr = cpu_to_le32(addr);
910 byte_count += sg_count[i];
911 psg->sg[i].count = cpu_to_le32(sg_count[i]);
914 srbcmd->count = cpu_to_le32(byte_count);
915 if (user_srbcmd->sg.count)
916 psg->count = cpu_to_le32(sg_indx+1);
919 status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
922 if (status == -ERESTARTSYS) {
923 rcode = -ERESTARTSYS;
928 dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
933 if (flags & SRB_DataIn) {
934 for(i = 0 ; i <= sg_indx; i++){
935 if (copy_to_user(sg_user[i], sg_list[i], sg_count[i])) {
936 dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
944 user_reply = arg + fibsize;
945 if (is_native_device) {
946 struct aac_hba_resp *err =
947 &((struct aac_native_hba *)srbfib->hw_fib_va)->resp.err;
948 struct aac_srb_reply reply;
950 memset(&reply, 0, sizeof(reply));
951 reply.status = ST_OK;
952 if (srbfib->flags & FIB_CONTEXT_FLAG_FASTRESP) {
954 reply.srb_status = SRB_STATUS_SUCCESS;
955 reply.scsi_status = 0;
956 reply.data_xfer_length = byte_count;
957 reply.sense_data_size = 0;
958 memset(reply.sense_data, 0, AAC_SENSE_BUFFERSIZE);
960 reply.srb_status = err->service_response;
961 reply.scsi_status = err->status;
962 reply.data_xfer_length = byte_count -
963 le32_to_cpu(err->residual_count);
964 reply.sense_data_size = err->sense_response_data_len;
965 memcpy(reply.sense_data, err->sense_response_buf,
966 AAC_SENSE_BUFFERSIZE);
968 if (copy_to_user(user_reply, &reply,
969 sizeof(struct aac_srb_reply))) {
970 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n"));
975 struct aac_srb_reply *reply;
977 reply = (struct aac_srb_reply *) fib_data(srbfib);
978 if (copy_to_user(user_reply, reply,
979 sizeof(struct aac_srb_reply))) {
980 dprintk((KERN_DEBUG"aacraid: Copy to user failed\n"));
988 if (rcode != -ERESTARTSYS) {
989 for (i = 0; i <= sg_indx; i++)
991 aac_fib_complete(srbfib);
992 aac_fib_free(srbfib);
998 struct aac_pci_info {
1004 static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
1006 struct aac_pci_info pci_info;
1008 pci_info.bus = dev->pdev->bus->number;
1009 pci_info.slot = PCI_SLOT(dev->pdev->devfn);
1011 if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
1012 dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
1018 static int aac_get_hba_info(struct aac_dev *dev, void __user *arg)
1020 struct aac_hba_info hbainfo;
1022 memset(&hbainfo, 0, sizeof(hbainfo));
1023 hbainfo.adapter_number = (u8) dev->id;
1024 hbainfo.system_io_bus_number = dev->pdev->bus->number;
1025 hbainfo.device_number = (dev->pdev->devfn >> 3);
1026 hbainfo.function_number = (dev->pdev->devfn & 0x0007);
1028 hbainfo.vendor_id = dev->pdev->vendor;
1029 hbainfo.device_id = dev->pdev->device;
1030 hbainfo.sub_vendor_id = dev->pdev->subsystem_vendor;
1031 hbainfo.sub_system_id = dev->pdev->subsystem_device;
1033 if (copy_to_user(arg, &hbainfo, sizeof(struct aac_hba_info))) {
1034 dprintk((KERN_DEBUG "aacraid: Could not copy hba info\n"));
1041 struct aac_reset_iop {
1045 static int aac_send_reset_adapter(struct aac_dev *dev, void __user *arg)
1047 struct aac_reset_iop reset;
1050 if (copy_from_user((void *)&reset, arg, sizeof(struct aac_reset_iop)))
1053 dev->adapter_shutdown = 1;
1055 mutex_unlock(&dev->ioctl_mutex);
1056 retval = aac_reset_adapter(dev, 0, reset.reset_type);
1057 mutex_lock(&dev->ioctl_mutex);
1062 int aac_do_ioctl(struct aac_dev *dev, unsigned int cmd, void __user *arg)
1066 mutex_lock(&dev->ioctl_mutex);
1068 if (dev->adapter_shutdown) {
1074 * HBA gets first crack
1077 status = aac_dev_ioctl(dev, cmd, arg);
1078 if (status != -ENOTTY)
1082 case FSACTL_MINIPORT_REV_CHECK:
1083 status = check_revision(dev, arg);
1085 case FSACTL_SEND_LARGE_FIB:
1086 case FSACTL_SENDFIB:
1087 status = ioctl_send_fib(dev, arg);
1089 case FSACTL_OPEN_GET_ADAPTER_FIB:
1090 status = open_getadapter_fib(dev, arg);
1092 case FSACTL_GET_NEXT_ADAPTER_FIB:
1093 status = next_getadapter_fib(dev, arg);
1095 case FSACTL_CLOSE_GET_ADAPTER_FIB:
1096 status = close_getadapter_fib(dev, arg);
1098 case FSACTL_SEND_RAW_SRB:
1099 status = aac_send_raw_srb(dev,arg);
1101 case FSACTL_GET_PCI_INFO:
1102 status = aac_get_pci_info(dev,arg);
1104 case FSACTL_GET_HBA_INFO:
1105 status = aac_get_hba_info(dev, arg);
1107 case FSACTL_RESET_IOP:
1108 status = aac_send_reset_adapter(dev, arg);
1117 mutex_unlock(&dev->ioctl_mutex);