commctrl.c 23 KB

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