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