commctrl.c 22 KB

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