commctrl.c 20 KB

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