commctrl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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 <asm/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. unsigned long flags;
  312. spin_lock_irqsave(&dev->fib_lock, flags);
  313. /*
  314. * First free any FIBs that have not been consumed.
  315. */
  316. while (!list_empty(&fibctx->fib_list)) {
  317. struct list_head * entry;
  318. /*
  319. * Pull the next fib from the fibs
  320. */
  321. entry = fibctx->fib_list.next;
  322. list_del(entry);
  323. fib = list_entry(entry, struct fib, fiblink);
  324. fibctx->count--;
  325. /*
  326. * Free the space occupied by this copy of the fib.
  327. */
  328. kfree(fib->hw_fib_va);
  329. kfree(fib);
  330. }
  331. /*
  332. * Remove the Context from the AdapterFibContext List
  333. */
  334. list_del(&fibctx->next);
  335. spin_unlock_irqrestore(&dev->fib_lock, flags);
  336. /*
  337. * Invalidate context
  338. */
  339. fibctx->type = 0;
  340. /*
  341. * Free the space occupied by the Context
  342. */
  343. kfree(fibctx);
  344. return 0;
  345. }
  346. /**
  347. * close_getadapter_fib - close down user fib context
  348. * @dev: adapter
  349. * @arg: ioctl arguments
  350. *
  351. * This routine will close down the fibctx passed in from the user.
  352. */
  353. static int close_getadapter_fib(struct aac_dev * dev, void __user *arg)
  354. {
  355. struct aac_fib_context *fibctx;
  356. int status;
  357. unsigned long flags;
  358. struct list_head * entry;
  359. /*
  360. * Verify that the HANDLE passed in was a valid AdapterFibContext
  361. *
  362. * Search the list of AdapterFibContext addresses on the adapter
  363. * to be sure this is a valid address
  364. */
  365. entry = dev->fib_list.next;
  366. fibctx = NULL;
  367. while(entry != &dev->fib_list) {
  368. fibctx = list_entry(entry, struct aac_fib_context, next);
  369. /*
  370. * Extract the fibctx from the input parameters
  371. */
  372. if (fibctx->unique == (u32)(uintptr_t)arg) /* We found a winner */
  373. break;
  374. entry = entry->next;
  375. fibctx = NULL;
  376. }
  377. if (!fibctx)
  378. return 0; /* Already gone */
  379. if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
  380. (fibctx->size != sizeof(struct aac_fib_context)))
  381. return -EINVAL;
  382. spin_lock_irqsave(&dev->fib_lock, flags);
  383. status = aac_close_fib_context(dev, fibctx);
  384. spin_unlock_irqrestore(&dev->fib_lock, flags);
  385. return status;
  386. }
  387. /**
  388. * check_revision - close down user fib context
  389. * @dev: adapter
  390. * @arg: ioctl arguments
  391. *
  392. * This routine returns the driver version.
  393. * Under Linux, there have been no version incompatibilities, so this is
  394. * simple!
  395. */
  396. static int check_revision(struct aac_dev *dev, void __user *arg)
  397. {
  398. struct revision response;
  399. char *driver_version = aac_driver_version;
  400. u32 version;
  401. response.compat = 1;
  402. version = (simple_strtol(driver_version,
  403. &driver_version, 10) << 24) | 0x00000400;
  404. version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
  405. version += simple_strtol(driver_version + 1, NULL, 10);
  406. response.version = cpu_to_le32(version);
  407. # ifdef AAC_DRIVER_BUILD
  408. response.build = cpu_to_le32(AAC_DRIVER_BUILD);
  409. # else
  410. response.build = cpu_to_le32(9999);
  411. # endif
  412. if (copy_to_user(arg, &response, sizeof(response)))
  413. return -EFAULT;
  414. return 0;
  415. }
  416. /**
  417. *
  418. * aac_send_raw_scb
  419. *
  420. */
  421. static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
  422. {
  423. struct fib* srbfib;
  424. int status;
  425. struct aac_srb *srbcmd = NULL;
  426. struct user_aac_srb *user_srbcmd = NULL;
  427. struct user_aac_srb __user *user_srb = arg;
  428. struct aac_srb_reply __user *user_reply;
  429. struct aac_srb_reply* reply;
  430. u32 fibsize = 0;
  431. u32 flags = 0;
  432. s32 rcode = 0;
  433. u32 data_dir;
  434. void __user *sg_user[32];
  435. void *sg_list[32];
  436. u32 sg_indx = 0;
  437. u32 byte_count = 0;
  438. u32 actual_fibsize64, actual_fibsize = 0;
  439. int i;
  440. if (dev->in_reset) {
  441. dprintk((KERN_DEBUG"aacraid: send raw srb -EBUSY\n"));
  442. return -EBUSY;
  443. }
  444. if (!capable(CAP_SYS_ADMIN)){
  445. dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
  446. return -EPERM;
  447. }
  448. /*
  449. * Allocate and initialize a Fib then setup a SRB command
  450. */
  451. if (!(srbfib = aac_fib_alloc(dev))) {
  452. return -ENOMEM;
  453. }
  454. aac_fib_init(srbfib);
  455. srbcmd = (struct aac_srb*) fib_data(srbfib);
  456. memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
  457. if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
  458. dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
  459. rcode = -EFAULT;
  460. goto cleanup;
  461. }
  462. if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
  463. rcode = -EINVAL;
  464. goto cleanup;
  465. }
  466. user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
  467. if (!user_srbcmd) {
  468. dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
  469. rcode = -ENOMEM;
  470. goto cleanup;
  471. }
  472. if(copy_from_user(user_srbcmd, user_srb,fibsize)){
  473. dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
  474. rcode = -EFAULT;
  475. goto cleanup;
  476. }
  477. user_reply = arg+fibsize;
  478. flags = user_srbcmd->flags; /* from user in cpu order */
  479. // Fix up srb for endian and force some values
  480. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
  481. srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
  482. srbcmd->id = cpu_to_le32(user_srbcmd->id);
  483. srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
  484. srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
  485. srbcmd->flags = cpu_to_le32(flags);
  486. srbcmd->retry_limit = 0; // Obsolete parameter
  487. srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
  488. memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
  489. switch (flags & (SRB_DataIn | SRB_DataOut)) {
  490. case SRB_DataOut:
  491. data_dir = DMA_TO_DEVICE;
  492. break;
  493. case (SRB_DataIn | SRB_DataOut):
  494. data_dir = DMA_BIDIRECTIONAL;
  495. break;
  496. case SRB_DataIn:
  497. data_dir = DMA_FROM_DEVICE;
  498. break;
  499. default:
  500. data_dir = DMA_NONE;
  501. }
  502. if (user_srbcmd->sg.count > ARRAY_SIZE(sg_list)) {
  503. dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
  504. le32_to_cpu(srbcmd->sg.count)));
  505. rcode = -EINVAL;
  506. goto cleanup;
  507. }
  508. actual_fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
  509. ((user_srbcmd->sg.count & 0xff) * sizeof(struct sgentry));
  510. actual_fibsize64 = actual_fibsize + (user_srbcmd->sg.count & 0xff) *
  511. (sizeof(struct sgentry64) - sizeof(struct sgentry));
  512. /* User made a mistake - should not continue */
  513. if ((actual_fibsize != fibsize) && (actual_fibsize64 != fibsize)) {
  514. dprintk((KERN_DEBUG"aacraid: Bad Size specified in "
  515. "Raw SRB command calculated fibsize=%lu;%lu "
  516. "user_srbcmd->sg.count=%d aac_srb=%lu sgentry=%lu;%lu "
  517. "issued fibsize=%d\n",
  518. actual_fibsize, actual_fibsize64, user_srbcmd->sg.count,
  519. sizeof(struct aac_srb), sizeof(struct sgentry),
  520. sizeof(struct sgentry64), fibsize));
  521. rcode = -EINVAL;
  522. goto cleanup;
  523. }
  524. if ((data_dir == DMA_NONE) && user_srbcmd->sg.count) {
  525. dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
  526. rcode = -EINVAL;
  527. goto cleanup;
  528. }
  529. byte_count = 0;
  530. if (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64) {
  531. struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
  532. struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
  533. /*
  534. * This should also catch if user used the 32 bit sgmap
  535. */
  536. if (actual_fibsize64 == fibsize) {
  537. actual_fibsize = actual_fibsize64;
  538. for (i = 0; i < upsg->count; i++) {
  539. u64 addr;
  540. void* p;
  541. /* Does this really need to be GFP_DMA? */
  542. p = kmalloc(upsg->sg[i].count,GFP_KERNEL|__GFP_DMA);
  543. if(!p) {
  544. dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  545. upsg->sg[i].count,i,upsg->count));
  546. rcode = -ENOMEM;
  547. goto cleanup;
  548. }
  549. addr = (u64)upsg->sg[i].addr[0];
  550. addr += ((u64)upsg->sg[i].addr[1]) << 32;
  551. sg_user[i] = (void __user *)(uintptr_t)addr;
  552. sg_list[i] = p; // save so we can clean up later
  553. sg_indx = i;
  554. if (flags & SRB_DataOut) {
  555. if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
  556. dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
  557. rcode = -EFAULT;
  558. goto cleanup;
  559. }
  560. }
  561. addr = pci_map_single(dev->pdev, p, upsg->sg[i].count, data_dir);
  562. psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
  563. psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
  564. byte_count += upsg->sg[i].count;
  565. psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
  566. }
  567. } else {
  568. struct user_sgmap* usg;
  569. usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
  570. + sizeof(struct sgmap), GFP_KERNEL);
  571. if (!usg) {
  572. dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
  573. rcode = -ENOMEM;
  574. goto cleanup;
  575. }
  576. memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
  577. + sizeof(struct sgmap));
  578. actual_fibsize = actual_fibsize64;
  579. for (i = 0; i < usg->count; i++) {
  580. u64 addr;
  581. void* p;
  582. /* Does this really need to be GFP_DMA? */
  583. p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
  584. if(!p) {
  585. kfree (usg);
  586. dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  587. usg->sg[i].count,i,usg->count));
  588. rcode = -ENOMEM;
  589. goto cleanup;
  590. }
  591. sg_user[i] = (void __user *)(uintptr_t)usg->sg[i].addr;
  592. sg_list[i] = p; // save so we can clean up later
  593. sg_indx = i;
  594. if (flags & SRB_DataOut) {
  595. if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
  596. kfree (usg);
  597. dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
  598. rcode = -EFAULT;
  599. goto cleanup;
  600. }
  601. }
  602. addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
  603. psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
  604. psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
  605. byte_count += usg->sg[i].count;
  606. psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
  607. }
  608. kfree (usg);
  609. }
  610. srbcmd->count = cpu_to_le32(byte_count);
  611. psg->count = cpu_to_le32(sg_indx+1);
  612. status = aac_fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
  613. } else {
  614. struct user_sgmap* upsg = &user_srbcmd->sg;
  615. struct sgmap* psg = &srbcmd->sg;
  616. if (actual_fibsize64 == fibsize) {
  617. struct user_sgmap64* usg = (struct user_sgmap64 *)upsg;
  618. for (i = 0; i < upsg->count; i++) {
  619. uintptr_t addr;
  620. void* p;
  621. /* Does this really need to be GFP_DMA? */
  622. p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
  623. if(!p) {
  624. dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  625. usg->sg[i].count,i,usg->count));
  626. rcode = -ENOMEM;
  627. goto cleanup;
  628. }
  629. addr = (u64)usg->sg[i].addr[0];
  630. addr += ((u64)usg->sg[i].addr[1]) << 32;
  631. sg_user[i] = (void __user *)addr;
  632. sg_list[i] = p; // save so we can clean up later
  633. sg_indx = i;
  634. if (flags & SRB_DataOut) {
  635. if(copy_from_user(p,sg_user[i],usg->sg[i].count)){
  636. dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
  637. rcode = -EFAULT;
  638. goto cleanup;
  639. }
  640. }
  641. addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
  642. psg->sg[i].addr = cpu_to_le32(addr & 0xffffffff);
  643. byte_count += usg->sg[i].count;
  644. psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
  645. }
  646. } else {
  647. for (i = 0; i < upsg->count; i++) {
  648. dma_addr_t addr;
  649. void* p;
  650. p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
  651. if (!p) {
  652. dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  653. upsg->sg[i].count, i, upsg->count));
  654. rcode = -ENOMEM;
  655. goto cleanup;
  656. }
  657. sg_user[i] = (void __user *)(uintptr_t)upsg->sg[i].addr;
  658. sg_list[i] = p; // save so we can clean up later
  659. sg_indx = i;
  660. if (flags & SRB_DataOut) {
  661. if(copy_from_user(p, sg_user[i],
  662. upsg->sg[i].count)) {
  663. dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
  664. rcode = -EFAULT;
  665. goto cleanup;
  666. }
  667. }
  668. addr = pci_map_single(dev->pdev, p,
  669. upsg->sg[i].count, data_dir);
  670. psg->sg[i].addr = cpu_to_le32(addr);
  671. byte_count += upsg->sg[i].count;
  672. psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
  673. }
  674. }
  675. srbcmd->count = cpu_to_le32(byte_count);
  676. psg->count = cpu_to_le32(sg_indx+1);
  677. status = aac_fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
  678. }
  679. if (status == -EINTR) {
  680. rcode = -EINTR;
  681. goto cleanup;
  682. }
  683. if (status != 0){
  684. dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
  685. rcode = -ENXIO;
  686. goto cleanup;
  687. }
  688. if (flags & SRB_DataIn) {
  689. for(i = 0 ; i <= sg_indx; i++){
  690. byte_count = le32_to_cpu(
  691. (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)
  692. ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
  693. : srbcmd->sg.sg[i].count);
  694. if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
  695. dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
  696. rcode = -EFAULT;
  697. goto cleanup;
  698. }
  699. }
  700. }
  701. reply = (struct aac_srb_reply *) fib_data(srbfib);
  702. if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
  703. dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
  704. rcode = -EFAULT;
  705. goto cleanup;
  706. }
  707. cleanup:
  708. kfree(user_srbcmd);
  709. for(i=0; i <= sg_indx; i++){
  710. kfree(sg_list[i]);
  711. }
  712. if (rcode != -EINTR) {
  713. aac_fib_complete(srbfib);
  714. aac_fib_free(srbfib);
  715. }
  716. return rcode;
  717. }
  718. struct aac_pci_info {
  719. u32 bus;
  720. u32 slot;
  721. };
  722. static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
  723. {
  724. struct aac_pci_info pci_info;
  725. pci_info.bus = dev->pdev->bus->number;
  726. pci_info.slot = PCI_SLOT(dev->pdev->devfn);
  727. if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
  728. dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
  729. return -EFAULT;
  730. }
  731. return 0;
  732. }
  733. int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
  734. {
  735. int status;
  736. /*
  737. * HBA gets first crack
  738. */
  739. status = aac_dev_ioctl(dev, cmd, arg);
  740. if(status != -ENOTTY)
  741. return status;
  742. switch (cmd) {
  743. case FSACTL_MINIPORT_REV_CHECK:
  744. status = check_revision(dev, arg);
  745. break;
  746. case FSACTL_SEND_LARGE_FIB:
  747. case FSACTL_SENDFIB:
  748. status = ioctl_send_fib(dev, arg);
  749. break;
  750. case FSACTL_OPEN_GET_ADAPTER_FIB:
  751. status = open_getadapter_fib(dev, arg);
  752. break;
  753. case FSACTL_GET_NEXT_ADAPTER_FIB:
  754. status = next_getadapter_fib(dev, arg);
  755. break;
  756. case FSACTL_CLOSE_GET_ADAPTER_FIB:
  757. status = close_getadapter_fib(dev, arg);
  758. break;
  759. case FSACTL_SEND_RAW_SRB:
  760. status = aac_send_raw_srb(dev,arg);
  761. break;
  762. case FSACTL_GET_PCI_INFO:
  763. status = aac_get_pci_info(dev,arg);
  764. break;
  765. default:
  766. status = -ENOTTY;
  767. break;
  768. }
  769. return status;
  770. }