[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / block / aoe / aoechr.c
blob14aeca3e2e8c58ab178ba489b6e32f230ebec29d
1 /* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoechr.c
4 * AoE character device driver
5 */
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include "aoe.h"
11 enum {
12 //MINOR_STAT = 1, (moved to sysfs)
13 MINOR_ERR = 2,
14 MINOR_DISCOVER,
15 MINOR_INTERFACES,
16 MSGSZ = 2048,
17 NARGS = 10,
18 NMSG = 100, /* message backlog to retain */
21 struct aoe_chardev {
22 ulong minor;
23 char name[32];
26 enum { EMFL_VALID = 1 };
28 struct ErrMsg {
29 short flags;
30 short len;
31 char *msg;
34 static struct ErrMsg emsgs[NMSG];
35 static int emsgs_head_idx, emsgs_tail_idx;
36 static struct semaphore emsgs_sema;
37 static spinlock_t emsgs_lock;
38 static int nblocked_emsgs_readers;
39 static struct class_simple *aoe_class;
40 static struct aoe_chardev chardevs[] = {
41 { MINOR_ERR, "err" },
42 { MINOR_DISCOVER, "discover" },
43 { MINOR_INTERFACES, "interfaces" },
46 static int
47 discover(void)
49 aoecmd_cfg(0xffff, 0xff);
50 return 0;
53 static int
54 interfaces(const char __user *str, size_t size)
56 if (set_aoe_iflist(str, size)) {
57 printk(KERN_CRIT
58 "%s: could not set interface list: %s\n",
59 __FUNCTION__, "too many interfaces");
60 return -EINVAL;
62 return 0;
65 void
66 aoechr_error(char *msg)
68 struct ErrMsg *em;
69 char *mp;
70 ulong flags, n;
72 n = strlen(msg);
74 spin_lock_irqsave(&emsgs_lock, flags);
76 em = emsgs + emsgs_tail_idx;
77 if ((em->flags & EMFL_VALID)) {
78 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
79 return;
82 mp = kmalloc(n, GFP_ATOMIC);
83 if (mp == NULL) {
84 printk(KERN_CRIT "aoe: aoechr_error: allocation failure, len=%ld\n", n);
85 goto bail;
88 memcpy(mp, msg, n);
89 em->msg = mp;
90 em->flags |= EMFL_VALID;
91 em->len = n;
93 emsgs_tail_idx++;
94 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
96 spin_unlock_irqrestore(&emsgs_lock, flags);
98 if (nblocked_emsgs_readers)
99 up(&emsgs_sema);
102 static ssize_t
103 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
105 int ret = -EINVAL;
107 switch ((unsigned long) filp->private_data) {
108 default:
109 printk(KERN_INFO "aoe: aoechr_write: can't write to that file.\n");
110 break;
111 case MINOR_DISCOVER:
112 ret = discover();
113 break;
114 case MINOR_INTERFACES:
115 ret = interfaces(buf, cnt);
116 break;
118 if (ret == 0)
119 ret = cnt;
120 return ret;
123 static int
124 aoechr_open(struct inode *inode, struct file *filp)
126 int n, i;
128 n = MINOR(inode->i_rdev);
129 filp->private_data = (void *) (unsigned long) n;
131 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
132 if (chardevs[i].minor == n)
133 return 0;
134 return -EINVAL;
137 static int
138 aoechr_rel(struct inode *inode, struct file *filp)
140 return 0;
143 static ssize_t
144 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
146 unsigned long n;
147 char *mp;
148 struct ErrMsg *em;
149 ssize_t len;
150 ulong flags;
152 n = (unsigned long) filp->private_data;
153 switch (n) {
154 case MINOR_ERR:
155 spin_lock_irqsave(&emsgs_lock, flags);
156 loop:
157 em = emsgs + emsgs_head_idx;
158 if ((em->flags & EMFL_VALID) == 0) {
159 if (filp->f_flags & O_NDELAY) {
160 spin_unlock_irqrestore(&emsgs_lock, flags);
161 return -EAGAIN;
163 nblocked_emsgs_readers++;
165 spin_unlock_irqrestore(&emsgs_lock, flags);
167 n = down_interruptible(&emsgs_sema);
169 spin_lock_irqsave(&emsgs_lock, flags);
171 nblocked_emsgs_readers--;
173 if (n) {
174 spin_unlock_irqrestore(&emsgs_lock, flags);
175 return -ERESTARTSYS;
177 goto loop;
179 if (em->len > cnt) {
180 spin_unlock_irqrestore(&emsgs_lock, flags);
181 return -EAGAIN;
183 mp = em->msg;
184 len = em->len;
185 em->msg = NULL;
186 em->flags &= ~EMFL_VALID;
188 emsgs_head_idx++;
189 emsgs_head_idx %= ARRAY_SIZE(emsgs);
191 spin_unlock_irqrestore(&emsgs_lock, flags);
193 n = copy_to_user(buf, mp, len);
194 kfree(mp);
195 return n == 0 ? len : -EFAULT;
196 default:
197 return -EFAULT;
201 static struct file_operations aoe_fops = {
202 .write = aoechr_write,
203 .read = aoechr_read,
204 .open = aoechr_open,
205 .release = aoechr_rel,
206 .owner = THIS_MODULE,
209 int __init
210 aoechr_init(void)
212 int n, i;
214 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
215 if (n < 0) {
216 printk(KERN_ERR "aoe: aoechr_init: can't register char device\n");
217 return n;
219 sema_init(&emsgs_sema, 0);
220 spin_lock_init(&emsgs_lock);
221 aoe_class = class_simple_create(THIS_MODULE, "aoe");
222 if (IS_ERR(aoe_class)) {
223 unregister_chrdev(AOE_MAJOR, "aoechr");
224 return PTR_ERR(aoe_class);
226 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
227 class_simple_device_add(aoe_class,
228 MKDEV(AOE_MAJOR, chardevs[i].minor),
229 NULL, chardevs[i].name);
231 return 0;
234 void
235 aoechr_exit(void)
237 int i;
239 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
240 class_simple_device_remove(MKDEV(AOE_MAJOR, chardevs[i].minor));
241 class_simple_destroy(aoe_class);
242 unregister_chrdev(AOE_MAJOR, "aoechr");