commctrl.c 24 KB

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