commctrl.c 20 KB

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