commctrl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. } else {
  272. spin_unlock_irqrestore(&dev->fib_lock, flags);
  273. if (f.wait) {
  274. if(down_interruptible(&fibctx->wait_sem) < 0) {
  275. status = -EINTR;
  276. } else {
  277. /* Lock again and retry */
  278. spin_lock_irqsave(&dev->fib_lock, flags);
  279. goto return_fib;
  280. }
  281. } else {
  282. status = -EAGAIN;
  283. }
  284. }
  285. fibctx->jiffies = jiffies/HZ;
  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. char *driver_version = aac_driver_version;
  379. u32 version;
  380. response.compat = cpu_to_le32(1);
  381. version = (simple_strtol(driver_version,
  382. &driver_version, 10) << 24) | 0x00000400;
  383. version += simple_strtol(driver_version + 1, &driver_version, 10) << 16;
  384. version += simple_strtol(driver_version + 1, NULL, 10);
  385. response.version = cpu_to_le32(version);
  386. # if (defined(AAC_DRIVER_BUILD))
  387. response.build = cpu_to_le32(AAC_DRIVER_BUILD);
  388. # else
  389. response.build = cpu_to_le32(9999);
  390. # endif
  391. if (copy_to_user(arg, &response, sizeof(response)))
  392. return -EFAULT;
  393. return 0;
  394. }
  395. /**
  396. *
  397. * aac_send_raw_scb
  398. *
  399. */
  400. static int aac_send_raw_srb(struct aac_dev* dev, void __user * arg)
  401. {
  402. struct fib* srbfib;
  403. int status;
  404. struct aac_srb *srbcmd = NULL;
  405. struct user_aac_srb *user_srbcmd = NULL;
  406. struct user_aac_srb __user *user_srb = arg;
  407. struct aac_srb_reply __user *user_reply;
  408. struct aac_srb_reply* reply;
  409. u32 fibsize = 0;
  410. u32 flags = 0;
  411. s32 rcode = 0;
  412. u32 data_dir;
  413. void __user *sg_user[32];
  414. void *sg_list[32];
  415. u32 sg_indx = 0;
  416. u32 byte_count = 0;
  417. u32 actual_fibsize = 0;
  418. int i;
  419. if (!capable(CAP_SYS_ADMIN)){
  420. dprintk((KERN_DEBUG"aacraid: No permission to send raw srb\n"));
  421. return -EPERM;
  422. }
  423. /*
  424. * Allocate and initialize a Fib then setup a BlockWrite command
  425. */
  426. if (!(srbfib = fib_alloc(dev))) {
  427. return -ENOMEM;
  428. }
  429. fib_init(srbfib);
  430. srbcmd = (struct aac_srb*) fib_data(srbfib);
  431. memset(sg_list, 0, sizeof(sg_list)); /* cleanup may take issue */
  432. if(copy_from_user(&fibsize, &user_srb->count,sizeof(u32))){
  433. dprintk((KERN_DEBUG"aacraid: Could not copy data size from user\n"));
  434. rcode = -EFAULT;
  435. goto cleanup;
  436. }
  437. if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr))) {
  438. rcode = -EINVAL;
  439. goto cleanup;
  440. }
  441. user_srbcmd = kmalloc(fibsize, GFP_KERNEL);
  442. if (!user_srbcmd) {
  443. dprintk((KERN_DEBUG"aacraid: Could not make a copy of the srb\n"));
  444. rcode = -ENOMEM;
  445. goto cleanup;
  446. }
  447. if(copy_from_user(user_srbcmd, user_srb,fibsize)){
  448. dprintk((KERN_DEBUG"aacraid: Could not copy srb from user\n"));
  449. rcode = -EFAULT;
  450. goto cleanup;
  451. }
  452. user_reply = arg+fibsize;
  453. flags = user_srbcmd->flags; /* from user in cpu order */
  454. // Fix up srb for endian and force some values
  455. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); // Force this
  456. srbcmd->channel = cpu_to_le32(user_srbcmd->channel);
  457. srbcmd->id = cpu_to_le32(user_srbcmd->id);
  458. srbcmd->lun = cpu_to_le32(user_srbcmd->lun);
  459. srbcmd->timeout = cpu_to_le32(user_srbcmd->timeout);
  460. srbcmd->flags = cpu_to_le32(flags);
  461. srbcmd->retry_limit = 0; // Obsolete parameter
  462. srbcmd->cdb_size = cpu_to_le32(user_srbcmd->cdb_size);
  463. memcpy(srbcmd->cdb, user_srbcmd->cdb, sizeof(srbcmd->cdb));
  464. switch (flags & (SRB_DataIn | SRB_DataOut)) {
  465. case SRB_DataOut:
  466. data_dir = DMA_TO_DEVICE;
  467. break;
  468. case (SRB_DataIn | SRB_DataOut):
  469. data_dir = DMA_BIDIRECTIONAL;
  470. break;
  471. case SRB_DataIn:
  472. data_dir = DMA_FROM_DEVICE;
  473. break;
  474. default:
  475. data_dir = DMA_NONE;
  476. }
  477. if (user_srbcmd->sg.count > (sizeof(sg_list)/sizeof(sg_list[0]))) {
  478. dprintk((KERN_DEBUG"aacraid: too many sg entries %d\n",
  479. le32_to_cpu(srbcmd->sg.count)));
  480. rcode = -EINVAL;
  481. goto cleanup;
  482. }
  483. if (dev->dac_support == 1) {
  484. struct user_sgmap64* upsg = (struct user_sgmap64*)&user_srbcmd->sg;
  485. struct sgmap64* psg = (struct sgmap64*)&srbcmd->sg;
  486. struct user_sgmap* usg;
  487. byte_count = 0;
  488. /*
  489. * This should also catch if user used the 32 bit sgmap
  490. */
  491. actual_fibsize = sizeof(struct aac_srb) -
  492. sizeof(struct sgentry) +
  493. ((upsg->count & 0xff) *
  494. sizeof(struct sgentry));
  495. if(actual_fibsize != fibsize){ // User made a mistake - should not continue
  496. dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
  497. rcode = -EINVAL;
  498. goto cleanup;
  499. }
  500. usg = kmalloc(actual_fibsize - sizeof(struct aac_srb)
  501. + sizeof(struct sgmap), GFP_KERNEL);
  502. if (!usg) {
  503. dprintk((KERN_DEBUG"aacraid: Allocation error in Raw SRB command\n"));
  504. rcode = -ENOMEM;
  505. goto cleanup;
  506. }
  507. memcpy (usg, upsg, actual_fibsize - sizeof(struct aac_srb)
  508. + sizeof(struct sgmap));
  509. actual_fibsize = sizeof(struct aac_srb) -
  510. sizeof(struct sgentry) + ((usg->count & 0xff) *
  511. sizeof(struct sgentry64));
  512. if ((data_dir == DMA_NONE) && upsg->count) {
  513. kfree (usg);
  514. dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
  515. rcode = -EINVAL;
  516. goto cleanup;
  517. }
  518. for (i = 0; i < usg->count; i++) {
  519. u64 addr;
  520. void* p;
  521. /* Does this really need to be GFP_DMA? */
  522. p = kmalloc(usg->sg[i].count,GFP_KERNEL|__GFP_DMA);
  523. if(p == 0) {
  524. kfree (usg);
  525. dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  526. usg->sg[i].count,i,usg->count));
  527. rcode = -ENOMEM;
  528. goto cleanup;
  529. }
  530. sg_user[i] = (void __user *)usg->sg[i].addr;
  531. sg_list[i] = p; // save so we can clean up later
  532. sg_indx = i;
  533. if( flags & SRB_DataOut ){
  534. if(copy_from_user(p,sg_user[i],upsg->sg[i].count)){
  535. kfree (usg);
  536. dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
  537. rcode = -EFAULT;
  538. goto cleanup;
  539. }
  540. }
  541. addr = pci_map_single(dev->pdev, p, usg->sg[i].count, data_dir);
  542. psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
  543. psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
  544. psg->sg[i].count = cpu_to_le32(usg->sg[i].count);
  545. byte_count += usg->sg[i].count;
  546. }
  547. kfree (usg);
  548. srbcmd->count = cpu_to_le32(byte_count);
  549. psg->count = cpu_to_le32(sg_indx+1);
  550. status = fib_send(ScsiPortCommand64, srbfib, actual_fibsize, FsaNormal, 1, 1,NULL,NULL);
  551. } else {
  552. struct user_sgmap* upsg = &user_srbcmd->sg;
  553. struct sgmap* psg = &srbcmd->sg;
  554. byte_count = 0;
  555. actual_fibsize = sizeof (struct aac_srb) + (((user_srbcmd->sg.count & 0xff) - 1) * sizeof (struct sgentry));
  556. if(actual_fibsize != fibsize){ // User made a mistake - should not continue
  557. dprintk((KERN_DEBUG"aacraid: Bad Size specified in Raw SRB command\n"));
  558. rcode = -EINVAL;
  559. goto cleanup;
  560. }
  561. if ((data_dir == DMA_NONE) && upsg->count) {
  562. dprintk((KERN_DEBUG"aacraid: SG with no direction specified in Raw SRB command\n"));
  563. rcode = -EINVAL;
  564. goto cleanup;
  565. }
  566. for (i = 0; i < upsg->count; i++) {
  567. dma_addr_t addr;
  568. void* p;
  569. p = kmalloc(upsg->sg[i].count, GFP_KERNEL);
  570. if(p == 0) {
  571. dprintk((KERN_DEBUG"aacraid: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  572. upsg->sg[i].count, i, upsg->count));
  573. rcode = -ENOMEM;
  574. goto cleanup;
  575. }
  576. sg_user[i] = (void __user *)upsg->sg[i].addr;
  577. sg_list[i] = p; // save so we can clean up later
  578. sg_indx = i;
  579. if( flags & SRB_DataOut ){
  580. if(copy_from_user(p, sg_user[i],
  581. upsg->sg[i].count)) {
  582. dprintk((KERN_DEBUG"aacraid: Could not copy sg data from user\n"));
  583. rcode = -EFAULT;
  584. goto cleanup;
  585. }
  586. }
  587. addr = pci_map_single(dev->pdev, p,
  588. upsg->sg[i].count, data_dir);
  589. psg->sg[i].addr = cpu_to_le32(addr);
  590. psg->sg[i].count = cpu_to_le32(upsg->sg[i].count);
  591. byte_count += upsg->sg[i].count;
  592. }
  593. srbcmd->count = cpu_to_le32(byte_count);
  594. psg->count = cpu_to_le32(sg_indx+1);
  595. status = fib_send(ScsiPortCommand, srbfib, actual_fibsize, FsaNormal, 1, 1, NULL, NULL);
  596. }
  597. if (status != 0){
  598. dprintk((KERN_DEBUG"aacraid: Could not send raw srb fib to hba\n"));
  599. rcode = -ENXIO;
  600. goto cleanup;
  601. }
  602. if( flags & SRB_DataIn ) {
  603. for(i = 0 ; i <= sg_indx; i++){
  604. byte_count = le32_to_cpu((dev->dac_support == 1)
  605. ? ((struct sgmap64*)&srbcmd->sg)->sg[i].count
  606. : srbcmd->sg.sg[i].count);
  607. if(copy_to_user(sg_user[i], sg_list[i], byte_count)){
  608. dprintk((KERN_DEBUG"aacraid: Could not copy sg data to user\n"));
  609. rcode = -EFAULT;
  610. goto cleanup;
  611. }
  612. }
  613. }
  614. reply = (struct aac_srb_reply *) fib_data(srbfib);
  615. if(copy_to_user(user_reply,reply,sizeof(struct aac_srb_reply))){
  616. dprintk((KERN_DEBUG"aacraid: Could not copy reply to user\n"));
  617. rcode = -EFAULT;
  618. goto cleanup;
  619. }
  620. cleanup:
  621. kfree(user_srbcmd);
  622. for(i=0; i <= sg_indx; i++){
  623. kfree(sg_list[i]);
  624. }
  625. fib_complete(srbfib);
  626. fib_free(srbfib);
  627. return rcode;
  628. }
  629. struct aac_pci_info {
  630. u32 bus;
  631. u32 slot;
  632. };
  633. static int aac_get_pci_info(struct aac_dev* dev, void __user *arg)
  634. {
  635. struct aac_pci_info pci_info;
  636. pci_info.bus = dev->pdev->bus->number;
  637. pci_info.slot = PCI_SLOT(dev->pdev->devfn);
  638. if (copy_to_user(arg, &pci_info, sizeof(struct aac_pci_info))) {
  639. dprintk((KERN_DEBUG "aacraid: Could not copy pci info\n"));
  640. return -EFAULT;
  641. }
  642. return 0;
  643. }
  644. int aac_do_ioctl(struct aac_dev * dev, int cmd, void __user *arg)
  645. {
  646. int status;
  647. /*
  648. * HBA gets first crack
  649. */
  650. status = aac_dev_ioctl(dev, cmd, arg);
  651. if(status != -ENOTTY)
  652. return status;
  653. switch (cmd) {
  654. case FSACTL_MINIPORT_REV_CHECK:
  655. status = check_revision(dev, arg);
  656. break;
  657. case FSACTL_SEND_LARGE_FIB:
  658. case FSACTL_SENDFIB:
  659. status = ioctl_send_fib(dev, arg);
  660. break;
  661. case FSACTL_OPEN_GET_ADAPTER_FIB:
  662. status = open_getadapter_fib(dev, arg);
  663. break;
  664. case FSACTL_GET_NEXT_ADAPTER_FIB:
  665. status = next_getadapter_fib(dev, arg);
  666. break;
  667. case FSACTL_CLOSE_GET_ADAPTER_FIB:
  668. status = close_getadapter_fib(dev, arg);
  669. break;
  670. case FSACTL_SEND_RAW_SRB:
  671. status = aac_send_raw_srb(dev,arg);
  672. break;
  673. case FSACTL_GET_PCI_INFO:
  674. status = aac_get_pci_info(dev,arg);
  675. break;
  676. default:
  677. status = -ENOTTY;
  678. break;
  679. }
  680. return status;
  681. }