PageRenderTime 837ms CodeModel.GetById 20ms RepoModel.GetById 3ms app.codeStats 0ms

/drivers/scsi/aacraid/commctrl.c

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