commctrl.c 20 KB

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