commctrl.c 23 KB

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