commctrl.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/config.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/sched.h>
  35. #include <linux/pci.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/slab.h>
  38. #include <linux/completion.h>
  39. #include <linux/blk.h>
  40. #include <asm/semaphore.h>
  41. #include <asm/uaccess.h>
  42. #include "scsi.h"
  43. #include "hosts.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.  
  54. static int ioctl_send_fib(struct aac_dev * dev, void *arg)
  55. {
  56. struct hw_fib * kfib;
  57. struct fib *fibptr;
  58. fibptr = fib_alloc(dev);
  59. if(fibptr == NULL)
  60. return -ENOMEM;
  61. kfib = fibptr->fib;
  62. /*
  63.  * First copy in the header so that we can check the size field.
  64.  */
  65. if (copy_from_user((void *)kfib, arg, sizeof(struct aac_fibhdr))) {
  66. fib_free(fibptr);
  67. return -EFAULT;
  68. }
  69. /*
  70.  * Since we copy based on the fib header size, make sure that we
  71.  * will not overrun the buffer when we copy the memory. Return
  72.  * an error if we would.
  73.  */
  74. if(le32_to_cpu(kfib->header.Size) > sizeof(struct hw_fib) - sizeof(struct aac_fibhdr)) {
  75. fib_free(fibptr);
  76. return -EINVAL;
  77. }
  78. if (copy_from_user((void *) kfib, arg, le32_to_cpu(kfib->header.Size) + sizeof(struct aac_fibhdr))) {
  79. fib_free(fibptr);
  80. return -EFAULT;
  81. }
  82. if (kfib->header.Command == cpu_to_le32(TakeABreakPt)) {
  83. aac_adapter_interrupt(dev);
  84. /*
  85.  * Since we didn't really send a fib, zero out the state to allow 
  86.  * cleanup code not to assert.
  87.  */
  88. kfib->header.XferState = 0;
  89. } else {
  90. if (fib_send(kfib->header.Command, fibptr, le32_to_cpu(kfib->header.Size) , FsaNormal,
  91. 1, 1, NULL, NULL) != 0) 
  92. {
  93. fib_free(fibptr);
  94. return -EINVAL;
  95. }
  96. if (fib_complete(fibptr) != 0) {
  97. fib_free(fibptr);
  98. return -EINVAL;
  99. }
  100. }
  101. /*
  102.  * Make sure that the size returned by the adapter (which includes
  103.  * the header) is less than or equal to the size of a fib, so we
  104.  * don't corrupt application data. Then copy that size to the user
  105.  * buffer. (Don't try to add the header information again, since it
  106.  * was already included by the adapter.)
  107.  */
  108. if (copy_to_user(arg, (void *)kfib, kfib->header.Size)) {
  109. fib_free(fibptr);
  110. return -EFAULT;
  111. }
  112. fib_free(fibptr);
  113. return 0;
  114. }
  115. /**
  116.  * open_getadapter_fib - Get the next fib
  117.  *
  118.  * This routine will get the next Fib, if available, from the AdapterFibContext
  119.  * passed in from the user.
  120.  */
  121. static int open_getadapter_fib(struct aac_dev * dev, void *arg)
  122. {
  123. struct aac_fib_context * fibctx;
  124. int status;
  125. unsigned long flags;
  126. fibctx = kmalloc(sizeof(struct aac_fib_context), GFP_KERNEL);
  127. if (fibctx == NULL) {
  128. status = -ENOMEM;
  129. } else {
  130. fibctx->type = FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT;
  131. fibctx->size = sizeof(struct aac_fib_context);
  132. /*
  133.  * Initialize the mutex used to wait for the next AIF.
  134.  */
  135. init_MUTEX_LOCKED(&fibctx->wait_sem);
  136. fibctx->wait = 0;
  137. /*
  138.  * Initialize the fibs and set the count of fibs on
  139.  * the list to 0.
  140.  */
  141. fibctx->count = 0;
  142. INIT_LIST_HEAD(&fibctx->fibs);
  143. fibctx->jiffies = jiffies/HZ;
  144. /*
  145.  * Now add this context onto the adapter's 
  146.  * AdapterFibContext list.
  147.  */
  148. spin_lock_irqsave(&dev->fib_lock, flags);
  149. list_add_tail(&fibctx->next, &dev->fib_list);
  150. spin_unlock_irqrestore(&dev->fib_lock, flags);
  151. if (copy_to_user(arg,  &fibctx, sizeof(struct aac_fib_context *))) {
  152. status = -EFAULT;
  153. } else {
  154. status = 0;
  155. }
  156. }
  157. return status;
  158. }
  159. /**
  160.  * next_getadapter_fib - get the next fib
  161.  * @dev: adapter to use
  162.  * @arg: ioctl argument
  163.  *
  164.  *  This routine will get the next Fib, if available, from the AdapterFibContext
  165.  * passed in from the user.
  166.  */
  167. static int next_getadapter_fib(struct aac_dev * dev, void *arg)
  168. {
  169. struct fib_ioctl f;
  170. struct aac_fib_context *fibctx, *aifcp;
  171. struct hw_fib * fib;
  172. int status;
  173. struct list_head * entry;
  174. int found;
  175. unsigned long flags;
  176. if(copy_from_user((void *)&f, arg, sizeof(struct fib_ioctl)))
  177. return -EFAULT;
  178. /*
  179.  * Extract the AdapterFibContext from the Input parameters.
  180.  */
  181. fibctx = (struct aac_fib_context *) f.fibctx;
  182. /*
  183.  * Verify that the HANDLE passed in was a valid AdapterFibContext
  184.  *
  185.  * Search the list of AdapterFibContext addresses on the adapter
  186.  * to be sure this is a valid address
  187.  */
  188. found = 0;
  189. entry = dev->fib_list.next;
  190. while(entry != &dev->fib_list) {
  191. aifcp = list_entry(entry, struct aac_fib_context, next);
  192. if(fibctx == aifcp) {   /* We found a winner */
  193. found = 1;
  194. break;
  195. }
  196. entry = entry->next;
  197. }
  198. if (found == 0)
  199. return -EINVAL;
  200. if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
  201.  (fibctx->size != sizeof(struct aac_fib_context)))
  202. return -EINVAL;
  203. status = 0;
  204. spin_lock_irqsave(&dev->fib_lock, flags);
  205. /*
  206.  * If there are no fibs to send back, then either wait or return
  207.  * -EAGAIN
  208.  */
  209. return_fib:
  210. if (!list_empty(&fibctx->fibs)) {
  211. struct list_head * entry;
  212. /*
  213.  * Pull the next fib from the fibs
  214.  */
  215. entry = fibctx->fibs.next;
  216. list_del(entry);
  217. fib = list_entry(entry, struct hw_fib, header.FibLinks);
  218. fibctx->count--;
  219. spin_unlock_irqrestore(&dev->fib_lock, flags);
  220. if (copy_to_user(f.fib, fib, sizeof(struct hw_fib))) {
  221. kfree(fib);
  222. return -EFAULT;
  223. }
  224. /*
  225.  * Free the space occupied by this copy of the fib.
  226.  */
  227. kfree(fib);
  228. status = 0;
  229. fibctx->jiffies = jiffies/HZ;
  230. } else {
  231. spin_unlock_irqrestore(&dev->fib_lock, flags);
  232. if (f.wait) {
  233. if(down_interruptible(&fibctx->wait_sem) < 0) {
  234. status = -EINTR;
  235. } else {
  236. /* Lock again and retry */
  237. spin_lock_irqsave(&dev->fib_lock, flags);
  238. goto return_fib;
  239. }
  240. } else {
  241. status = -EAGAIN;
  242. }
  243. }
  244. return status;
  245. }
  246. int aac_close_fib_context(struct aac_dev * dev, struct aac_fib_context * fibctx)
  247. {
  248. struct hw_fib *fib;
  249. /*
  250.  * First free any FIBs that have not been consumed.
  251.  */
  252. while (!list_empty(&fibctx->fibs)) {
  253. struct list_head * entry;
  254. /*
  255.  * Pull the next fib from the fibs
  256.  */
  257. entry = fibctx->fibs.next;
  258. list_del(entry);
  259. fib = list_entry(entry, struct hw_fib, header.FibLinks);
  260. fibctx->count--;
  261. /*
  262.  * Free the space occupied by this copy of the fib.
  263.  */
  264. kfree(fib);
  265. }
  266. /*
  267.  * Remove the Context from the AdapterFibContext List
  268.  */
  269. list_del(&fibctx->next);
  270. /*
  271.  * Invalidate context
  272.  */
  273. fibctx->type = 0;
  274. /*
  275.  * Free the space occupied by the Context
  276.  */
  277. kfree(fibctx);
  278. return 0;
  279. }
  280. /**
  281.  * close_getadapter_fib - close down user fib context
  282.  * @dev: adapter
  283.  * @arg: ioctl arguments
  284.  *
  285.  * This routine will close down the fibctx passed in from the user.
  286.  */
  287.  
  288. static int close_getadapter_fib(struct aac_dev * dev, void *arg)
  289. {
  290. struct aac_fib_context *fibctx, *aifcp;
  291. int status;
  292. unsigned long flags;
  293. struct list_head * entry;
  294. int found;
  295. /*
  296.  * Extract the fibctx from the input parameters
  297.  */
  298. fibctx = arg;
  299. /*
  300.  * Verify that the HANDLE passed in was a valid AdapterFibContext
  301.  *
  302.  * Search the list of AdapterFibContext addresses on the adapter
  303.  * to be sure this is a valid address
  304.  */
  305. found = 0;
  306. entry = dev->fib_list.next;
  307. while(entry != &dev->fib_list) {
  308. aifcp = list_entry(entry, struct aac_fib_context, next);
  309. if(fibctx == aifcp) {   /* We found a winner */
  310. found = 1;
  311. break;
  312. }
  313. entry = entry->next;
  314. }
  315. if(found == 0)
  316. return 0; /* Already gone */
  317. if((fibctx->type != FSAFS_NTC_GET_ADAPTER_FIB_CONTEXT) ||
  318.  (fibctx->size != sizeof(struct aac_fib_context)))
  319. return -EINVAL;
  320. spin_lock_irqsave(&dev->fib_lock, flags);
  321. status = aac_close_fib_context(dev, fibctx);
  322. spin_unlock_irqrestore(&dev->fib_lock, flags);
  323. return status;
  324. }
  325. /**
  326.  * check_revision - close down user fib context
  327.  * @dev: adapter
  328.  * @arg: ioctl arguments
  329.  *
  330.  * This routine returns the firmware version.
  331.  *      Under Linux, there have been no version incompatibilities, so this is simple!
  332.  */
  333. static int check_revision(struct aac_dev *dev, void *arg)
  334. {
  335. struct revision response;
  336. response.compat = 1;
  337. response.version = dev->adapter_info.kernelrev;
  338. response.build = dev->adapter_info.kernelbuild;
  339. if (copy_to_user(arg, &response, sizeof(response)))
  340. return -EFAULT;
  341. return 0;
  342. }
  343. struct aac_pci_info {
  344.         u32 bus;
  345.         u32 slot;
  346. };
  347. int aac_get_pci_info(struct aac_dev* dev, void* arg)
  348. {
  349.         struct aac_pci_info pci_info;
  350. pci_info.bus = dev->pdev->bus->number;
  351. pci_info.slot = PCI_SLOT(dev->pdev->devfn);
  352.        if(copy_to_user( arg, (void*)&pci_info, sizeof(struct aac_pci_info)))
  353.                return -EFAULT;
  354.         return 0;
  355.  }
  356.  
  357. int aac_do_ioctl(struct aac_dev * dev, int cmd, void *arg)
  358. {
  359. int status;
  360. /*
  361.  * HBA gets first crack
  362.  */
  363.  
  364. status = aac_dev_ioctl(dev, cmd, arg);
  365. if(status != -ENOTTY)
  366. return status;
  367. switch (cmd) {
  368. case FSACTL_MINIPORT_REV_CHECK:
  369. status = check_revision(dev, arg);
  370. break;
  371. case FSACTL_SENDFIB:
  372. status = ioctl_send_fib(dev, arg);
  373. break;
  374. case FSACTL_OPEN_GET_ADAPTER_FIB:
  375. status = open_getadapter_fib(dev, arg);
  376. break;
  377. case FSACTL_GET_NEXT_ADAPTER_FIB:
  378. status = next_getadapter_fib(dev, arg);
  379. break;
  380. case FSACTL_CLOSE_GET_ADAPTER_FIB:
  381. status = close_getadapter_fib(dev, arg);
  382. break;
  383. case FSACTL_GET_PCI_INFO:
  384. status = aac_get_pci_info(dev,arg);
  385. break;
  386. default:
  387. status = -ENOTTY;
  388.    break;
  389. }
  390. return status;
  391. }