commctrl.c 23 KB

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