commctrl.c 24 KB

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