commctrl.c 22 KB

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