]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/block/aoe/aoechr.c
aoe: whitespace cleanup
[mirror_ubuntu-jammy-kernel.git] / drivers / block / aoe / aoechr.c
CommitLineData
fea05a26 1/* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
1da177e4
LT
2/*
3 * aoechr.c
4 * AoE character device driver
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
24879a8e 9#include <linux/completion.h>
68e0d42f 10#include <linux/delay.h>
5a0e3ad6 11#include <linux/slab.h>
2a48fc0a 12#include <linux/mutex.h>
e9bb8fb0 13#include <linux/skbuff.h>
d5decd3b 14#include <linux/export.h>
1da177e4
LT
15#include "aoe.h"
16
17enum {
18 //MINOR_STAT = 1, (moved to sysfs)
19 MINOR_ERR = 2,
20 MINOR_DISCOVER,
21 MINOR_INTERFACES,
3ae1c24e 22 MINOR_REVALIDATE,
262bf541 23 MINOR_FLUSH,
1da177e4 24 MSGSZ = 2048,
1da177e4
LT
25 NMSG = 100, /* message backlog to retain */
26};
27
28struct aoe_chardev {
29 ulong minor;
30 char name[32];
31};
32
33enum { EMFL_VALID = 1 };
34
35struct ErrMsg {
36 short flags;
37 short len;
38 char *msg;
39};
40
2a48fc0a 41static DEFINE_MUTEX(aoechr_mutex);
662a8896
EC
42
43/* A ring buffer of error messages, to be read through
44 * "/dev/etherd/err". When no messages are present,
45 * readers will block waiting for messages to appear.
46 */
1da177e4
LT
47static struct ErrMsg emsgs[NMSG];
48static int emsgs_head_idx, emsgs_tail_idx;
24879a8e 49static struct completion emsgs_comp;
1da177e4
LT
50static spinlock_t emsgs_lock;
51static int nblocked_emsgs_readers;
deb36970 52static struct class *aoe_class;
1da177e4
LT
53static struct aoe_chardev chardevs[] = {
54 { MINOR_ERR, "err" },
55 { MINOR_DISCOVER, "discover" },
56 { MINOR_INTERFACES, "interfaces" },
3ae1c24e 57 { MINOR_REVALIDATE, "revalidate" },
262bf541 58 { MINOR_FLUSH, "flush" },
1da177e4
LT
59};
60
61static int
62discover(void)
63{
64 aoecmd_cfg(0xffff, 0xff);
65 return 0;
66}
67
68static int
69interfaces(const char __user *str, size_t size)
70{
71 if (set_aoe_iflist(str, size)) {
a12c93f0
EC
72 printk(KERN_ERR
73 "aoe: could not set interface list: too many interfaces\n");
1da177e4
LT
74 return -EINVAL;
75 }
76 return 0;
77}
78
3ae1c24e
EC
79static int
80revalidate(const char __user *str, size_t size)
81{
82 int major, minor, n;
83 ulong flags;
84 struct aoedev *d;
68e0d42f 85 struct sk_buff *skb;
3ae1c24e
EC
86 char buf[16];
87
88 if (size >= sizeof buf)
89 return -EINVAL;
90 buf[sizeof buf - 1] = '\0';
91 if (copy_from_user(buf, str, size))
92 return -EFAULT;
93
3ae1c24e
EC
94 n = sscanf(buf, "e%d.%d", &major, &minor);
95 if (n != 2) {
896831f5 96 pr_err("aoe: invalid device specification %s\n", buf);
3ae1c24e
EC
97 return -EINVAL;
98 }
0c966214 99 d = aoedev_by_aoeaddr(major, minor, 0);
3ae1c24e
EC
100 if (!d)
101 return -EINVAL;
3ae1c24e 102 spin_lock_irqsave(&d->lock, flags);
68e0d42f 103 aoecmd_cleanslate(d);
25f4d75e 104 aoecmd_cfg(major, minor);
68e0d42f
EC
105loop:
106 skb = aoecmd_ata_id(d);
3ae1c24e 107 spin_unlock_irqrestore(&d->lock, flags);
68e0d42f
EC
108 /* try again if we are able to sleep a bit,
109 * otherwise give up this revalidation
110 */
25f4d75e 111 if (!skb && !msleep_interruptible(250)) {
68e0d42f
EC
112 spin_lock_irqsave(&d->lock, flags);
113 goto loop;
114 }
69cf2d85 115 aoedev_put(d);
e9bb8fb0
DM
116 if (skb) {
117 struct sk_buff_head queue;
118 __skb_queue_head_init(&queue);
119 __skb_queue_tail(&queue, skb);
120 aoenet_xmit(&queue);
121 }
3ae1c24e
EC
122 return 0;
123}
124
1da177e4
LT
125void
126aoechr_error(char *msg)
127{
128 struct ErrMsg *em;
129 char *mp;
130 ulong flags, n;
131
132 n = strlen(msg);
133
134 spin_lock_irqsave(&emsgs_lock, flags);
135
136 em = emsgs + emsgs_tail_idx;
137 if ((em->flags & EMFL_VALID)) {
138bail: spin_unlock_irqrestore(&emsgs_lock, flags);
139 return;
140 }
141
142 mp = kmalloc(n, GFP_ATOMIC);
143 if (mp == NULL) {
a12c93f0 144 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
1da177e4
LT
145 goto bail;
146 }
147
148 memcpy(mp, msg, n);
149 em->msg = mp;
150 em->flags |= EMFL_VALID;
151 em->len = n;
152
153 emsgs_tail_idx++;
154 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
155
156 spin_unlock_irqrestore(&emsgs_lock, flags);
157
158 if (nblocked_emsgs_readers)
24879a8e 159 complete(&emsgs_comp);
1da177e4
LT
160}
161
162static ssize_t
163aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
164{
165 int ret = -EINVAL;
166
167 switch ((unsigned long) filp->private_data) {
168 default:
a12c93f0 169 printk(KERN_INFO "aoe: can't write to that file.\n");
1da177e4
LT
170 break;
171 case MINOR_DISCOVER:
172 ret = discover();
173 break;
174 case MINOR_INTERFACES:
175 ret = interfaces(buf, cnt);
176 break;
3ae1c24e
EC
177 case MINOR_REVALIDATE:
178 ret = revalidate(buf, cnt);
262bf541
EC
179 break;
180 case MINOR_FLUSH:
181 ret = aoedev_flush(buf, cnt);
b21faa25 182 break;
1da177e4
LT
183 }
184 if (ret == 0)
185 ret = cnt;
186 return ret;
187}
188
189static int
190aoechr_open(struct inode *inode, struct file *filp)
191{
192 int n, i;
193
2a48fc0a 194 mutex_lock(&aoechr_mutex);
2017b376 195 n = iminor(inode);
1da177e4
LT
196 filp->private_data = (void *) (unsigned long) n;
197
198 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
579174a5 199 if (chardevs[i].minor == n) {
2a48fc0a 200 mutex_unlock(&aoechr_mutex);
1da177e4 201 return 0;
579174a5 202 }
2a48fc0a 203 mutex_unlock(&aoechr_mutex);
1da177e4
LT
204 return -EINVAL;
205}
206
207static int
208aoechr_rel(struct inode *inode, struct file *filp)
209{
210 return 0;
211}
212
213static ssize_t
214aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
215{
216 unsigned long n;
217 char *mp;
218 struct ErrMsg *em;
219 ssize_t len;
220 ulong flags;
221
222 n = (unsigned long) filp->private_data;
cf446f0d
EC
223 if (n != MINOR_ERR)
224 return -EFAULT;
225
226 spin_lock_irqsave(&emsgs_lock, flags);
1da177e4 227
cf446f0d
EC
228 for (;;) {
229 em = emsgs + emsgs_head_idx;
230 if ((em->flags & EMFL_VALID) != 0)
231 break;
232 if (filp->f_flags & O_NDELAY) {
1da177e4 233 spin_unlock_irqrestore(&emsgs_lock, flags);
cf446f0d
EC
234 return -EAGAIN;
235 }
236 nblocked_emsgs_readers++;
237
238 spin_unlock_irqrestore(&emsgs_lock, flags);
1da177e4 239
24879a8e 240 n = wait_for_completion_interruptible(&emsgs_comp);
1da177e4 241
cf446f0d 242 spin_lock_irqsave(&emsgs_lock, flags);
1da177e4 243
cf446f0d 244 nblocked_emsgs_readers--;
1da177e4 245
cf446f0d 246 if (n) {
1da177e4 247 spin_unlock_irqrestore(&emsgs_lock, flags);
cf446f0d 248 return -ERESTARTSYS;
1da177e4 249 }
cf446f0d
EC
250 }
251 if (em->len > cnt) {
252 spin_unlock_irqrestore(&emsgs_lock, flags);
253 return -EAGAIN;
254 }
255 mp = em->msg;
256 len = em->len;
257 em->msg = NULL;
258 em->flags &= ~EMFL_VALID;
1da177e4 259
cf446f0d
EC
260 emsgs_head_idx++;
261 emsgs_head_idx %= ARRAY_SIZE(emsgs);
1da177e4 262
cf446f0d 263 spin_unlock_irqrestore(&emsgs_lock, flags);
1da177e4 264
cf446f0d
EC
265 n = copy_to_user(buf, mp, len);
266 kfree(mp);
267 return n == 0 ? len : -EFAULT;
1da177e4
LT
268}
269
2b8693c0 270static const struct file_operations aoe_fops = {
1da177e4
LT
271 .write = aoechr_write,
272 .read = aoechr_read,
273 .open = aoechr_open,
274 .release = aoechr_rel,
275 .owner = THIS_MODULE,
6038f373 276 .llseek = noop_llseek,
1da177e4
LT
277};
278
2c9ede55 279static char *aoe_devnode(struct device *dev, umode_t *mode)
1ce8a0d3
KS
280{
281 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
282}
283
1da177e4
LT
284int __init
285aoechr_init(void)
286{
287 int n, i;
288
289 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
a04b41cd 290 if (n < 0) {
a12c93f0 291 printk(KERN_ERR "aoe: can't register char device\n");
1da177e4
LT
292 return n;
293 }
24879a8e 294 init_completion(&emsgs_comp);
1da177e4 295 spin_lock_init(&emsgs_lock);
deb36970 296 aoe_class = class_create(THIS_MODULE, "aoe");
1da177e4
LT
297 if (IS_ERR(aoe_class)) {
298 unregister_chrdev(AOE_MAJOR, "aoechr");
299 return PTR_ERR(aoe_class);
300 }
e454cea2 301 aoe_class->devnode = aoe_devnode;
1ce8a0d3 302
1da177e4 303 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
1ff9f542
GKH
304 device_create(aoe_class, NULL,
305 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
306 chardevs[i].name);
1da177e4
LT
307
308 return 0;
309}
310
311void
312aoechr_exit(void)
313{
314 int i;
315
316 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
7ea7ed01 317 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
deb36970 318 class_destroy(aoe_class);
1da177e4
LT
319 unregister_chrdev(AOE_MAJOR, "aoechr");
320}
321