aoechr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoechr.c
  4. * AoE character device driver
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/completion.h>
  9. #include <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/mutex.h>
  12. #include <linux/skbuff.h>
  13. #include "aoe.h"
  14. enum {
  15. //MINOR_STAT = 1, (moved to sysfs)
  16. MINOR_ERR = 2,
  17. MINOR_DISCOVER,
  18. MINOR_INTERFACES,
  19. MINOR_REVALIDATE,
  20. MINOR_FLUSH,
  21. MSGSZ = 2048,
  22. NMSG = 100, /* message backlog to retain */
  23. };
  24. struct aoe_chardev {
  25. ulong minor;
  26. char name[32];
  27. };
  28. enum { EMFL_VALID = 1 };
  29. struct ErrMsg {
  30. short flags;
  31. short len;
  32. char *msg;
  33. };
  34. static DEFINE_MUTEX(aoechr_mutex);
  35. static struct ErrMsg emsgs[NMSG];
  36. static int emsgs_head_idx, emsgs_tail_idx;
  37. static struct completion emsgs_comp;
  38. static spinlock_t emsgs_lock;
  39. static int nblocked_emsgs_readers;
  40. static struct class *aoe_class;
  41. static struct aoe_chardev chardevs[] = {
  42. { MINOR_ERR, "err" },
  43. { MINOR_DISCOVER, "discover" },
  44. { MINOR_INTERFACES, "interfaces" },
  45. { MINOR_REVALIDATE, "revalidate" },
  46. { MINOR_FLUSH, "flush" },
  47. };
  48. static int
  49. discover(void)
  50. {
  51. aoecmd_cfg(0xffff, 0xff);
  52. return 0;
  53. }
  54. static int
  55. interfaces(const char __user *str, size_t size)
  56. {
  57. if (set_aoe_iflist(str, size)) {
  58. printk(KERN_ERR
  59. "aoe: could not set interface list: too many interfaces\n");
  60. return -EINVAL;
  61. }
  62. return 0;
  63. }
  64. static int
  65. revalidate(const char __user *str, size_t size)
  66. {
  67. int major, minor, n;
  68. ulong flags;
  69. struct aoedev *d;
  70. struct sk_buff *skb;
  71. char buf[16];
  72. if (size >= sizeof buf)
  73. return -EINVAL;
  74. buf[sizeof buf - 1] = '\0';
  75. if (copy_from_user(buf, str, size))
  76. return -EFAULT;
  77. /* should be e%d.%d format */
  78. n = sscanf(buf, "e%d.%d", &major, &minor);
  79. if (n != 2) {
  80. printk(KERN_ERR "aoe: invalid device specification\n");
  81. return -EINVAL;
  82. }
  83. d = aoedev_by_aoeaddr(major, minor);
  84. if (!d)
  85. return -EINVAL;
  86. spin_lock_irqsave(&d->lock, flags);
  87. aoecmd_cleanslate(d);
  88. loop:
  89. skb = aoecmd_ata_id(d);
  90. spin_unlock_irqrestore(&d->lock, flags);
  91. /* try again if we are able to sleep a bit,
  92. * otherwise give up this revalidation
  93. */
  94. if (!skb && !msleep_interruptible(200)) {
  95. spin_lock_irqsave(&d->lock, flags);
  96. goto loop;
  97. }
  98. if (skb) {
  99. struct sk_buff_head queue;
  100. __skb_queue_head_init(&queue);
  101. __skb_queue_tail(&queue, skb);
  102. aoenet_xmit(&queue);
  103. }
  104. aoecmd_cfg(major, minor);
  105. return 0;
  106. }
  107. void
  108. aoechr_error(char *msg)
  109. {
  110. struct ErrMsg *em;
  111. char *mp;
  112. ulong flags, n;
  113. n = strlen(msg);
  114. spin_lock_irqsave(&emsgs_lock, flags);
  115. em = emsgs + emsgs_tail_idx;
  116. if ((em->flags & EMFL_VALID)) {
  117. bail: spin_unlock_irqrestore(&emsgs_lock, flags);
  118. return;
  119. }
  120. mp = kmalloc(n, GFP_ATOMIC);
  121. if (mp == NULL) {
  122. printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
  123. goto bail;
  124. }
  125. memcpy(mp, msg, n);
  126. em->msg = mp;
  127. em->flags |= EMFL_VALID;
  128. em->len = n;
  129. emsgs_tail_idx++;
  130. emsgs_tail_idx %= ARRAY_SIZE(emsgs);
  131. spin_unlock_irqrestore(&emsgs_lock, flags);
  132. if (nblocked_emsgs_readers)
  133. complete(&emsgs_comp);
  134. }
  135. static ssize_t
  136. aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
  137. {
  138. int ret = -EINVAL;
  139. switch ((unsigned long) filp->private_data) {
  140. default:
  141. printk(KERN_INFO "aoe: can't write to that file.\n");
  142. break;
  143. case MINOR_DISCOVER:
  144. ret = discover();
  145. break;
  146. case MINOR_INTERFACES:
  147. ret = interfaces(buf, cnt);
  148. break;
  149. case MINOR_REVALIDATE:
  150. ret = revalidate(buf, cnt);
  151. break;
  152. case MINOR_FLUSH:
  153. ret = aoedev_flush(buf, cnt);
  154. }
  155. if (ret == 0)
  156. ret = cnt;
  157. return ret;
  158. }
  159. static int
  160. aoechr_open(struct inode *inode, struct file *filp)
  161. {
  162. int n, i;
  163. mutex_lock(&aoechr_mutex);
  164. n = iminor(inode);
  165. filp->private_data = (void *) (unsigned long) n;
  166. for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
  167. if (chardevs[i].minor == n) {
  168. mutex_unlock(&aoechr_mutex);
  169. return 0;
  170. }
  171. mutex_unlock(&aoechr_mutex);
  172. return -EINVAL;
  173. }
  174. static int
  175. aoechr_rel(struct inode *inode, struct file *filp)
  176. {
  177. return 0;
  178. }
  179. static ssize_t
  180. aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
  181. {
  182. unsigned long n;
  183. char *mp;
  184. struct ErrMsg *em;
  185. ssize_t len;
  186. ulong flags;
  187. n = (unsigned long) filp->private_data;
  188. if (n != MINOR_ERR)
  189. return -EFAULT;
  190. spin_lock_irqsave(&emsgs_lock, flags);
  191. for (;;) {
  192. em = emsgs + emsgs_head_idx;
  193. if ((em->flags & EMFL_VALID) != 0)
  194. break;
  195. if (filp->f_flags & O_NDELAY) {
  196. spin_unlock_irqrestore(&emsgs_lock, flags);
  197. return -EAGAIN;
  198. }
  199. nblocked_emsgs_readers++;
  200. spin_unlock_irqrestore(&emsgs_lock, flags);
  201. n = wait_for_completion_interruptible(&emsgs_comp);
  202. spin_lock_irqsave(&emsgs_lock, flags);
  203. nblocked_emsgs_readers--;
  204. if (n) {
  205. spin_unlock_irqrestore(&emsgs_lock, flags);
  206. return -ERESTARTSYS;
  207. }
  208. }
  209. if (em->len > cnt) {
  210. spin_unlock_irqrestore(&emsgs_lock, flags);
  211. return -EAGAIN;
  212. }
  213. mp = em->msg;
  214. len = em->len;
  215. em->msg = NULL;
  216. em->flags &= ~EMFL_VALID;
  217. emsgs_head_idx++;
  218. emsgs_head_idx %= ARRAY_SIZE(emsgs);
  219. spin_unlock_irqrestore(&emsgs_lock, flags);
  220. n = copy_to_user(buf, mp, len);
  221. kfree(mp);
  222. return n == 0 ? len : -EFAULT;
  223. }
  224. static const struct file_operations aoe_fops = {
  225. .write = aoechr_write,
  226. .read = aoechr_read,
  227. .open = aoechr_open,
  228. .release = aoechr_rel,
  229. .owner = THIS_MODULE,
  230. .llseek = noop_llseek,
  231. };
  232. static char *aoe_devnode(struct device *dev, mode_t *mode)
  233. {
  234. return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
  235. }
  236. int __init
  237. aoechr_init(void)
  238. {
  239. int n, i;
  240. n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
  241. if (n < 0) {
  242. printk(KERN_ERR "aoe: can't register char device\n");
  243. return n;
  244. }
  245. init_completion(&emsgs_comp);
  246. spin_lock_init(&emsgs_lock);
  247. aoe_class = class_create(THIS_MODULE, "aoe");
  248. if (IS_ERR(aoe_class)) {
  249. unregister_chrdev(AOE_MAJOR, "aoechr");
  250. return PTR_ERR(aoe_class);
  251. }
  252. aoe_class->devnode = aoe_devnode;
  253. for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
  254. device_create(aoe_class, NULL,
  255. MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
  256. chardevs[i].name);
  257. return 0;
  258. }
  259. void
  260. aoechr_exit(void)
  261. {
  262. int i;
  263. for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
  264. device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
  265. class_destroy(aoe_class);
  266. unregister_chrdev(AOE_MAJOR, "aoechr");
  267. }