aoechr.c 6.1 KB

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