commctrl.c 19 KB

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