blob: 46d373287a30be71e599808b58dedca09d6a36e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * I2O Configuration Interface Driver
3 *
4 * (C) Copyright 1999-2002 Red Hat
5 *
6 * Written by Alan Cox, Building Number Three Ltd
7 *
8 * Fixes/additions:
9 * Deepak Saxena (04/20/1999):
10 * Added basic ioctl() support
11 * Deepak Saxena (06/07/1999):
12 * Added software download ioctl (still testing)
13 * Auvo Häkkinen (09/10/1999):
14 * Changes to i2o_cfg_reply(), ioctl_parms()
15 * Added ioct_validate()
16 * Taneli Vähäkangas (09/30/1999):
17 * Fixed ioctl_swdl()
18 * Taneli Vähäkangas (10/04/1999):
19 * Changed ioctl_swdl(), implemented ioctl_swul() and ioctl_swdel()
20 * Deepak Saxena (11/18/1999):
21 * Added event managmenet support
22 * Alan Cox <alan@redhat.com>:
23 * 2.4 rewrite ported to 2.5
24 * Markus Lidel <Markus.Lidel@shadowconnect.com>:
25 * Added pass-thru support for Adaptec's raidutils
26 *
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation; either version
30 * 2 of the License, or (at your option) any later version.
31 */
32
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/pci.h>
36#include <linux/i2o.h>
37#include <linux/errno.h>
38#include <linux/init.h>
39#include <linux/slab.h>
40#include <linux/miscdevice.h>
41#include <linux/mm.h>
42#include <linux/spinlock.h>
43#include <linux/smp_lock.h>
44#include <linux/ioctl32.h>
45#include <linux/compat.h>
46#include <linux/syscalls.h>
47
48#include <asm/uaccess.h>
49#include <asm/io.h>
50
51#define OSM_NAME "config-osm"
52#define OSM_VERSION "$Rev$"
53#define OSM_DESCRIPTION "I2O Configuration OSM"
54
55extern int i2o_parm_issue(struct i2o_device *, int, void *, int, void *, int);
56
f4c2c152005-04-10 22:29:42 -050057static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
58 unsigned long arg);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static spinlock_t i2o_config_lock;
61
62#define MODINC(x,y) ((x) = ((x) + 1) % (y))
63
64struct sg_simple_element {
65 u32 flag_count;
66 u32 addr_bus;
67};
68
69struct i2o_cfg_info {
70 struct file *fp;
71 struct fasync_struct *fasync;
72 struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
73 u16 q_in; // Queue head index
74 u16 q_out; // Queue tail index
75 u16 q_len; // Queue length
76 u16 q_lost; // Number of lost events
77 ulong q_id; // Event queue ID...used as tx_context
78 struct i2o_cfg_info *next;
79};
80static struct i2o_cfg_info *open_files = NULL;
81static ulong i2o_cfg_info_id = 0;
82
83/*
84 * Each of these describes an i2o message handler. They are
85 * multiplexed by the i2o_core code
86 */
87
88static struct i2o_driver i2o_config_driver = {
89 .name = OSM_NAME
90};
91
92static int i2o_cfg_getiops(unsigned long arg)
93{
94 struct i2o_controller *c;
95 u8 __user *user_iop_table = (void __user *)arg;
96 u8 tmp[MAX_I2O_CONTROLLERS];
97 int ret = 0;
98
99 memset(tmp, 0, MAX_I2O_CONTROLLERS);
100
101 list_for_each_entry(c, &i2o_controllers, list)
102 tmp[c->unit] = 1;
103
104 if (copy_to_user(user_iop_table, tmp, MAX_I2O_CONTROLLERS))
105 ret = -EFAULT;
106
107 return ret;
108};
109
110static int i2o_cfg_gethrt(unsigned long arg)
111{
112 struct i2o_controller *c;
113 struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
114 struct i2o_cmd_hrtlct kcmd;
115 i2o_hrt *hrt;
116 int len;
117 u32 reslen;
118 int ret = 0;
119
120 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
121 return -EFAULT;
122
123 if (get_user(reslen, kcmd.reslen) < 0)
124 return -EFAULT;
125
126 if (kcmd.resbuf == NULL)
127 return -EFAULT;
128
129 c = i2o_find_iop(kcmd.iop);
130 if (!c)
131 return -ENXIO;
132
133 hrt = (i2o_hrt *) c->hrt.virt;
134
135 len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
136
137 /* We did a get user...so assuming mem is ok...is this bad? */
138 put_user(len, kcmd.reslen);
139 if (len > reslen)
140 ret = -ENOBUFS;
141 if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
142 ret = -EFAULT;
143
144 return ret;
145};
146
147static int i2o_cfg_getlct(unsigned long arg)
148{
149 struct i2o_controller *c;
150 struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
151 struct i2o_cmd_hrtlct kcmd;
152 i2o_lct *lct;
153 int len;
154 int ret = 0;
155 u32 reslen;
156
157 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
158 return -EFAULT;
159
160 if (get_user(reslen, kcmd.reslen) < 0)
161 return -EFAULT;
162
163 if (kcmd.resbuf == NULL)
164 return -EFAULT;
165
166 c = i2o_find_iop(kcmd.iop);
167 if (!c)
168 return -ENXIO;
169
170 lct = (i2o_lct *) c->lct;
171
172 len = (unsigned int)lct->table_size << 2;
173 put_user(len, kcmd.reslen);
174 if (len > reslen)
175 ret = -ENOBUFS;
176 else if (copy_to_user(kcmd.resbuf, lct, len))
177 ret = -EFAULT;
178
179 return ret;
180};
181
182static int i2o_cfg_parms(unsigned long arg, unsigned int type)
183{
184 int ret = 0;
185 struct i2o_controller *c;
186 struct i2o_device *dev;
187 struct i2o_cmd_psetget __user *cmd =
188 (struct i2o_cmd_psetget __user *)arg;
189 struct i2o_cmd_psetget kcmd;
190 u32 reslen;
191 u8 *ops;
192 u8 *res;
193 int len = 0;
194
195 u32 i2o_cmd = (type == I2OPARMGET ?
196 I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
197
198 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
199 return -EFAULT;
200
201 if (get_user(reslen, kcmd.reslen))
202 return -EFAULT;
203
204 c = i2o_find_iop(kcmd.iop);
205 if (!c)
206 return -ENXIO;
207
208 dev = i2o_iop_find_device(c, kcmd.tid);
209 if (!dev)
210 return -ENXIO;
211
212 ops = (u8 *) kmalloc(kcmd.oplen, GFP_KERNEL);
213 if (!ops)
214 return -ENOMEM;
215
216 if (copy_from_user(ops, kcmd.opbuf, kcmd.oplen)) {
217 kfree(ops);
218 return -EFAULT;
219 }
220
221 /*
222 * It's possible to have a _very_ large table
223 * and that the user asks for all of it at once...
224 */
225 res = (u8 *) kmalloc(65536, GFP_KERNEL);
226 if (!res) {
227 kfree(ops);
228 return -ENOMEM;
229 }
230
231 len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
232 kfree(ops);
233
234 if (len < 0) {
235 kfree(res);
236 return -EAGAIN;
237 }
238
239 put_user(len, kcmd.reslen);
240 if (len > reslen)
241 ret = -ENOBUFS;
242 else if (copy_to_user(kcmd.resbuf, res, len))
243 ret = -EFAULT;
244
245 kfree(res);
246
247 return ret;
248};
249
250static int i2o_cfg_swdl(unsigned long arg)
251{
252 struct i2o_sw_xfer kxfer;
253 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
254 unsigned char maxfrag = 0, curfrag = 1;
255 struct i2o_dma buffer;
256 struct i2o_message __iomem *msg;
257 u32 m;
258 unsigned int status = 0, swlen = 0, fragsize = 8192;
259 struct i2o_controller *c;
260
261 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
262 return -EFAULT;
263
264 if (get_user(swlen, kxfer.swlen) < 0)
265 return -EFAULT;
266
267 if (get_user(maxfrag, kxfer.maxfrag) < 0)
268 return -EFAULT;
269
270 if (get_user(curfrag, kxfer.curfrag) < 0)
271 return -EFAULT;
272
273 if (curfrag == maxfrag)
274 fragsize = swlen - (maxfrag - 1) * 8192;
275
276 if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
277 return -EFAULT;
278
279 c = i2o_find_iop(kxfer.iop);
280 if (!c)
281 return -ENXIO;
282
283 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
284 if (m == I2O_QUEUE_EMPTY)
285 return -EBUSY;
286
287 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
288 i2o_msg_nop(c, m);
289 return -ENOMEM;
290 }
291
292 __copy_from_user(buffer.virt, kxfer.buf, fragsize);
293
294 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
295 writel(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 | ADAPTER_TID,
296 &msg->u.head[1]);
297 writel(i2o_config_driver.context, &msg->u.head[2]);
298 writel(0, &msg->u.head[3]);
299 writel((((u32) kxfer.flags) << 24) | (((u32) kxfer.sw_type) << 16) |
300 (((u32) maxfrag) << 8) | (((u32) curfrag)), &msg->body[0]);
301 writel(swlen, &msg->body[1]);
302 writel(kxfer.sw_id, &msg->body[2]);
303 writel(0xD0000000 | fragsize, &msg->body[3]);
304 writel(buffer.phys, &msg->body[4]);
305
306 osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
307 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
308
309 if (status != -ETIMEDOUT)
310 i2o_dma_free(&c->pdev->dev, &buffer);
311
312 if (status != I2O_POST_WAIT_OK) {
313 // it fails if you try and send frags out of order
314 // and for some yet unknown reasons too
315 osm_info("swdl failed, DetailedStatus = %d\n", status);
316 return status;
317 }
318
319 return 0;
320};
321
322static int i2o_cfg_swul(unsigned long arg)
323{
324 struct i2o_sw_xfer kxfer;
325 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
326 unsigned char maxfrag = 0, curfrag = 1;
327 struct i2o_dma buffer;
328 struct i2o_message __iomem *msg;
329 u32 m;
330 unsigned int status = 0, swlen = 0, fragsize = 8192;
331 struct i2o_controller *c;
332 int ret = 0;
333
334 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
335 goto return_fault;
336
337 if (get_user(swlen, kxfer.swlen) < 0)
338 goto return_fault;
339
340 if (get_user(maxfrag, kxfer.maxfrag) < 0)
341 goto return_fault;
342
343 if (get_user(curfrag, kxfer.curfrag) < 0)
344 goto return_fault;
345
346 if (curfrag == maxfrag)
347 fragsize = swlen - (maxfrag - 1) * 8192;
348
349 if (!kxfer.buf)
350 goto return_fault;
351
352 c = i2o_find_iop(kxfer.iop);
353 if (!c)
354 return -ENXIO;
355
356 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
357 if (m == I2O_QUEUE_EMPTY)
358 return -EBUSY;
359
360 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
361 i2o_msg_nop(c, m);
362 return -ENOMEM;
363 }
364
365 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
366 writel(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID,
367 &msg->u.head[1]);
368 writel(i2o_config_driver.context, &msg->u.head[2]);
369 writel(0, &msg->u.head[3]);
370 writel((u32) kxfer.flags << 24 | (u32) kxfer.
371 sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag,
372 &msg->body[0]);
373 writel(swlen, &msg->body[1]);
374 writel(kxfer.sw_id, &msg->body[2]);
375 writel(0xD0000000 | fragsize, &msg->body[3]);
376 writel(buffer.phys, &msg->body[4]);
377
378 osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
379 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
380
381 if (status != I2O_POST_WAIT_OK) {
382 if (status != -ETIMEDOUT)
383 i2o_dma_free(&c->pdev->dev, &buffer);
384
385 osm_info("swul failed, DetailedStatus = %d\n", status);
386 return status;
387 }
388
389 if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
390 ret = -EFAULT;
391
392 i2o_dma_free(&c->pdev->dev, &buffer);
393
394return_ret:
395 return ret;
396return_fault:
397 ret = -EFAULT;
398 goto return_ret;
399};
400
401static int i2o_cfg_swdel(unsigned long arg)
402{
403 struct i2o_controller *c;
404 struct i2o_sw_xfer kxfer;
405 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
406 struct i2o_message __iomem *msg;
407 u32 m;
408 unsigned int swlen;
409 int token;
410
411 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
412 return -EFAULT;
413
414 if (get_user(swlen, kxfer.swlen) < 0)
415 return -EFAULT;
416
417 c = i2o_find_iop(kxfer.iop);
418 if (!c)
419 return -ENXIO;
420
421 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
422 if (m == I2O_QUEUE_EMPTY)
423 return -EBUSY;
424
425 writel(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
426 writel(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID,
427 &msg->u.head[1]);
428 writel(i2o_config_driver.context, &msg->u.head[2]);
429 writel(0, &msg->u.head[3]);
430 writel((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16,
431 &msg->body[0]);
432 writel(swlen, &msg->body[1]);
433 writel(kxfer.sw_id, &msg->body[2]);
434
435 token = i2o_msg_post_wait(c, m, 10);
436
437 if (token != I2O_POST_WAIT_OK) {
438 osm_info("swdel failed, DetailedStatus = %d\n", token);
439 return -ETIMEDOUT;
440 }
441
442 return 0;
443};
444
445static int i2o_cfg_validate(unsigned long arg)
446{
447 int token;
448 int iop = (int)arg;
449 struct i2o_message __iomem *msg;
450 u32 m;
451 struct i2o_controller *c;
452
453 c = i2o_find_iop(iop);
454 if (!c)
455 return -ENXIO;
456
457 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
458 if (m == I2O_QUEUE_EMPTY)
459 return -EBUSY;
460
461 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
462 writel(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop,
463 &msg->u.head[1]);
464 writel(i2o_config_driver.context, &msg->u.head[2]);
465 writel(0, &msg->u.head[3]);
466
467 token = i2o_msg_post_wait(c, m, 10);
468
469 if (token != I2O_POST_WAIT_OK) {
470 osm_info("Can't validate configuration, ErrorStatus = %d\n",
471 token);
472 return -ETIMEDOUT;
473 }
474
475 return 0;
476};
477
478static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
479{
480 struct i2o_message __iomem *msg;
481 u32 m;
482 struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
483 struct i2o_evt_id kdesc;
484 struct i2o_controller *c;
485 struct i2o_device *d;
486
487 if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
488 return -EFAULT;
489
490 /* IOP exists? */
491 c = i2o_find_iop(kdesc.iop);
492 if (!c)
493 return -ENXIO;
494
495 /* Device exists? */
496 d = i2o_iop_find_device(c, kdesc.tid);
497 if (!d)
498 return -ENODEV;
499
500 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
501 if (m == I2O_QUEUE_EMPTY)
502 return -EBUSY;
503
504 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
505 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | kdesc.tid,
506 &msg->u.head[1]);
507 writel(i2o_config_driver.context, &msg->u.head[2]);
508 writel(i2o_cntxt_list_add(c, fp->private_data), &msg->u.head[3]);
509 writel(kdesc.evt_mask, &msg->body[0]);
510
511 i2o_msg_post(c, m);
512
513 return 0;
514}
515
516static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
517{
518 struct i2o_cfg_info *p = NULL;
519 struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
520 struct i2o_evt_get kget;
521 unsigned long flags;
522
523 for (p = open_files; p; p = p->next)
524 if (p->q_id == (ulong) fp->private_data)
525 break;
526
527 if (!p->q_len)
528 return -ENOENT;
529
530 memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
531 MODINC(p->q_out, I2O_EVT_Q_LEN);
532 spin_lock_irqsave(&i2o_config_lock, flags);
533 p->q_len--;
534 kget.pending = p->q_len;
535 kget.lost = p->q_lost;
536 spin_unlock_irqrestore(&i2o_config_lock, flags);
537
538 if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
539 return -EFAULT;
540 return 0;
541}
542
543#ifdef CONFIG_COMPAT
f4c2c152005-04-10 22:29:42 -0500544static int i2o_cfg_passthru32(struct file *file, unsigned cmnd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct i2o_cmd_passthru32 __user *cmd;
547 struct i2o_controller *c;
548 u32 __user *user_msg;
549 u32 *reply = NULL;
550 u32 __user *user_reply = NULL;
551 u32 size = 0;
552 u32 reply_size = 0;
553 u32 rcode = 0;
554 struct i2o_dma sg_list[SG_TABLESIZE];
555 u32 sg_offset = 0;
556 u32 sg_count = 0;
557 u32 i = 0;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700558 u32 sg_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 i2o_status_block *sb;
560 struct i2o_message *msg;
561 u32 m;
562 unsigned int iop;
563
564 cmd = (struct i2o_cmd_passthru32 __user *)arg;
565
566 if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
567 return -EFAULT;
568
569 user_msg = compat_ptr(i);
570
571 c = i2o_find_iop(iop);
572 if (!c) {
573 osm_debug("controller %d not found\n", iop);
574 return -ENXIO;
575 }
576
577 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
578
579 sb = c->status_block.virt;
580
581 if (get_user(size, &user_msg[0])) {
582 osm_warn("unable to get size!\n");
583 return -EFAULT;
584 }
585 size = size >> 16;
586
587 if (size > sb->inbound_frame_size) {
588 osm_warn("size of message > inbound_frame_size");
589 return -EFAULT;
590 }
591
592 user_reply = &user_msg[size];
593
594 size <<= 2; // Convert to bytes
595
596 /* Copy in the user's I2O command */
597 if (copy_from_user(msg, user_msg, size)) {
598 osm_warn("unable to copy user message\n");
599 return -EFAULT;
600 }
601 i2o_dump_message(msg);
602
603 if (get_user(reply_size, &user_reply[0]) < 0)
604 return -EFAULT;
605
606 reply_size >>= 16;
607 reply_size <<= 2;
608
609 reply = kmalloc(reply_size, GFP_KERNEL);
610 if (!reply) {
611 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
612 c->name);
613 return -ENOMEM;
614 }
615 memset(reply, 0, reply_size);
616
617 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
618
619 writel(i2o_config_driver.context, &msg->u.s.icntxt);
620 writel(i2o_cntxt_list_add(c, reply), &msg->u.s.tcntxt);
621
622 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
623 if (sg_offset) {
624 struct sg_simple_element *sg;
625
626 if (sg_offset * 4 >= size) {
627 rcode = -EFAULT;
628 goto cleanup;
629 }
630 // TODO 64bit fix
631 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
632 sg_offset);
633 sg_count =
634 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
635 if (sg_count > SG_TABLESIZE) {
636 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
637 c->name, sg_count);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700638 rcode = -EINVAL;
639 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 for (i = 0; i < sg_count; i++) {
643 int sg_size;
644 struct i2o_dma *p;
645
646 if (!(sg[i].flag_count & 0x10000000
647 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
648 printk(KERN_DEBUG
649 "%s:Bad SG element %d - not simple (%x)\n",
650 c->name, i, sg[i].flag_count);
651 rcode = -EINVAL;
652 goto cleanup;
653 }
654 sg_size = sg[i].flag_count & 0xffffff;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700655 p = &(sg_list[sg_index++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Allocate memory for the transfer */
657 if (i2o_dma_alloc
658 (&c->pdev->dev, p, sg_size,
659 PCI_DMA_BIDIRECTIONAL)) {
660 printk(KERN_DEBUG
661 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
662 c->name, sg_size, i, sg_count);
663 rcode = -ENOMEM;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700664 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 /* Copy in the user's SG buffer if necessary */
667 if (sg[i].
668 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
669 // TODO 64bit fix
670 if (copy_from_user
671 (p->virt, (void __user *)(unsigned long)sg[i].addr_bus,
672 sg_size)) {
673 printk(KERN_DEBUG
674 "%s: Could not copy SG buf %d FROM user\n",
675 c->name, i);
676 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700677 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 }
680 //TODO 64bit fix
681 sg[i].addr_bus = (u32) p->phys;
682 }
683 }
684
685 rcode = i2o_msg_post_wait(c, m, 60);
686 if (rcode)
Markus Lidel61fbfa82005-06-23 22:02:11 -0700687 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (sg_offset) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700690 u32 msg[MSG_FRAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* Copy back the Scatter Gather buffers back to user space */
692 u32 j;
693 // TODO 64bit fix
694 struct sg_simple_element *sg;
695 int sg_size;
696
697 // re-acquire the original message to handle correctly the sg copy operation
698 memset(&msg, 0, MSG_FRAME_SIZE * 4);
699 // get user msg size in u32s
700 if (get_user(size, &user_msg[0])) {
701 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700702 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 size = size >> 16;
705 size *= 4;
706 /* Copy in the user's I2O command */
707 if (copy_from_user(msg, user_msg, size)) {
708 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700709 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 sg_count =
712 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
713
714 // TODO 64bit fix
715 sg = (struct sg_simple_element *)(msg + sg_offset);
716 for (j = 0; j < sg_count; j++) {
717 /* Copy out the SG list to user's buffer if necessary */
718 if (!
719 (sg[j].
720 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
721 sg_size = sg[j].flag_count & 0xffffff;
722 // TODO 64bit fix
723 if (copy_to_user
724 ((void __user *)(u64) sg[j].addr_bus,
725 sg_list[j].virt, sg_size)) {
726 printk(KERN_WARNING
727 "%s: Could not copy %p TO user %x\n",
728 c->name, sg_list[j].virt,
729 sg[j].addr_bus);
730 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700731 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
734 }
735 }
736
737 /* Copy back the reply to user space */
738 if (reply_size) {
739 // we wrote our own values for context - now restore the user supplied ones
740 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
741 printk(KERN_WARNING
742 "%s: Could not copy message context FROM user\n",
743 c->name);
744 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700745 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 if (copy_to_user(user_reply, reply, reply_size)) {
748 printk(KERN_WARNING
749 "%s: Could not copy reply TO user\n", c->name);
750 rcode = -EFAULT;
751 }
752 }
753
Markus Lidel61fbfa82005-06-23 22:02:11 -0700754 sg_list_cleanup:
755 for (i = 0; i < sg_index; i++)
756 i2o_dma_free(&c->pdev->dev, &sg_list[i]);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 cleanup:
759 kfree(reply);
760 return rcode;
761}
762
f4c2c152005-04-10 22:29:42 -0500763static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
764{
765 int ret;
766 lock_kernel();
767 switch (cmd) {
768 case I2OGETIOPS:
769 ret = i2o_cfg_ioctl(NULL, file, cmd, arg);
770 break;
771 case I2OPASSTHRU32:
772 ret = i2o_cfg_passthru32(file, cmd, arg);
773 break;
774 default:
775 ret = -ENOIOCTLCMD;
776 break;
777 }
778 unlock_kernel();
779 return ret;
780}
781
782#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784static int i2o_cfg_passthru(unsigned long arg)
785{
786 struct i2o_cmd_passthru __user *cmd =
787 (struct i2o_cmd_passthru __user *)arg;
788 struct i2o_controller *c;
789 u32 __user *user_msg;
790 u32 *reply = NULL;
791 u32 __user *user_reply = NULL;
792 u32 size = 0;
793 u32 reply_size = 0;
794 u32 rcode = 0;
795 void *sg_list[SG_TABLESIZE];
796 u32 sg_offset = 0;
797 u32 sg_count = 0;
798 int sg_index = 0;
799 u32 i = 0;
800 void *p = NULL;
801 i2o_status_block *sb;
802 struct i2o_message __iomem *msg;
803 u32 m;
804 unsigned int iop;
805
806 if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
807 return -EFAULT;
808
809 c = i2o_find_iop(iop);
810 if (!c) {
811 osm_warn("controller %d not found\n", iop);
812 return -ENXIO;
813 }
814
815 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
816
817 sb = c->status_block.virt;
818
819 if (get_user(size, &user_msg[0]))
820 return -EFAULT;
821 size = size >> 16;
822
823 if (size > sb->inbound_frame_size) {
824 osm_warn("size of message > inbound_frame_size");
825 return -EFAULT;
826 }
827
828 user_reply = &user_msg[size];
829
830 size <<= 2; // Convert to bytes
831
832 /* Copy in the user's I2O command */
833 if (copy_from_user(msg, user_msg, size))
834 return -EFAULT;
835
836 if (get_user(reply_size, &user_reply[0]) < 0)
837 return -EFAULT;
838
839 reply_size >>= 16;
840 reply_size <<= 2;
841
842 reply = kmalloc(reply_size, GFP_KERNEL);
843 if (!reply) {
844 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
845 c->name);
846 return -ENOMEM;
847 }
848 memset(reply, 0, reply_size);
849
850 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
851
852 writel(i2o_config_driver.context, &msg->u.s.icntxt);
853 writel(i2o_cntxt_list_add(c, reply), &msg->u.s.tcntxt);
854
855 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
856 if (sg_offset) {
857 struct sg_simple_element *sg;
858
859 if (sg_offset * 4 >= size) {
860 rcode = -EFAULT;
861 goto cleanup;
862 }
863 // TODO 64bit fix
864 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
865 sg_offset);
866 sg_count =
867 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
868 if (sg_count > SG_TABLESIZE) {
869 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
870 c->name, sg_count);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700871 rcode = -EINVAL;
872 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 for (i = 0; i < sg_count; i++) {
876 int sg_size;
877
878 if (!(sg[i].flag_count & 0x10000000
879 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
880 printk(KERN_DEBUG
881 "%s:Bad SG element %d - not simple (%x)\n",
882 c->name, i, sg[i].flag_count);
883 rcode = -EINVAL;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700884 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 sg_size = sg[i].flag_count & 0xffffff;
887 /* Allocate memory for the transfer */
888 p = kmalloc(sg_size, GFP_KERNEL);
889 if (!p) {
890 printk(KERN_DEBUG
891 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
892 c->name, sg_size, i, sg_count);
893 rcode = -ENOMEM;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700894 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
897 /* Copy in the user's SG buffer if necessary */
898 if (sg[i].
899 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
900 // TODO 64bit fix
901 if (copy_from_user
902 (p, (void __user *)sg[i].addr_bus,
903 sg_size)) {
904 printk(KERN_DEBUG
905 "%s: Could not copy SG buf %d FROM user\n",
906 c->name, i);
907 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700908 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 }
911 //TODO 64bit fix
912 sg[i].addr_bus = virt_to_bus(p);
913 }
914 }
915
916 rcode = i2o_msg_post_wait(c, m, 60);
917 if (rcode)
Markus Lidel61fbfa82005-06-23 22:02:11 -0700918 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (sg_offset) {
921 u32 msg[128];
922 /* Copy back the Scatter Gather buffers back to user space */
923 u32 j;
924 // TODO 64bit fix
925 struct sg_simple_element *sg;
926 int sg_size;
927
928 // re-acquire the original message to handle correctly the sg copy operation
929 memset(&msg, 0, MSG_FRAME_SIZE * 4);
930 // get user msg size in u32s
931 if (get_user(size, &user_msg[0])) {
932 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700933 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935 size = size >> 16;
936 size *= 4;
937 /* Copy in the user's I2O command */
938 if (copy_from_user(msg, user_msg, size)) {
939 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700940 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942 sg_count =
943 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
944
945 // TODO 64bit fix
946 sg = (struct sg_simple_element *)(msg + sg_offset);
947 for (j = 0; j < sg_count; j++) {
948 /* Copy out the SG list to user's buffer if necessary */
949 if (!
950 (sg[j].
951 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
952 sg_size = sg[j].flag_count & 0xffffff;
953 // TODO 64bit fix
954 if (copy_to_user
955 ((void __user *)sg[j].addr_bus, sg_list[j],
956 sg_size)) {
957 printk(KERN_WARNING
958 "%s: Could not copy %p TO user %x\n",
959 c->name, sg_list[j],
960 sg[j].addr_bus);
961 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700962 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 }
965 }
966 }
967
968 /* Copy back the reply to user space */
969 if (reply_size) {
970 // we wrote our own values for context - now restore the user supplied ones
971 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
972 printk(KERN_WARNING
973 "%s: Could not copy message context FROM user\n",
974 c->name);
975 rcode = -EFAULT;
976 }
977 if (copy_to_user(user_reply, reply, reply_size)) {
978 printk(KERN_WARNING
979 "%s: Could not copy reply TO user\n", c->name);
980 rcode = -EFAULT;
981 }
982 }
983
Markus Lidel61fbfa82005-06-23 22:02:11 -0700984 sg_list_cleanup:
985 for (i = 0; i < sg_index; i++)
986 kfree(sg_list[i]);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 cleanup:
989 kfree(reply);
990 return rcode;
991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993/*
994 * IOCTL Handler
995 */
996static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
997 unsigned long arg)
998{
999 int ret;
1000
1001 switch (cmd) {
1002 case I2OGETIOPS:
1003 ret = i2o_cfg_getiops(arg);
1004 break;
1005
1006 case I2OHRTGET:
1007 ret = i2o_cfg_gethrt(arg);
1008 break;
1009
1010 case I2OLCTGET:
1011 ret = i2o_cfg_getlct(arg);
1012 break;
1013
1014 case I2OPARMSET:
1015 ret = i2o_cfg_parms(arg, I2OPARMSET);
1016 break;
1017
1018 case I2OPARMGET:
1019 ret = i2o_cfg_parms(arg, I2OPARMGET);
1020 break;
1021
1022 case I2OSWDL:
1023 ret = i2o_cfg_swdl(arg);
1024 break;
1025
1026 case I2OSWUL:
1027 ret = i2o_cfg_swul(arg);
1028 break;
1029
1030 case I2OSWDEL:
1031 ret = i2o_cfg_swdel(arg);
1032 break;
1033
1034 case I2OVALIDATE:
1035 ret = i2o_cfg_validate(arg);
1036 break;
1037
1038 case I2OEVTREG:
1039 ret = i2o_cfg_evt_reg(arg, fp);
1040 break;
1041
1042 case I2OEVTGET:
1043 ret = i2o_cfg_evt_get(arg, fp);
1044 break;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 case I2OPASSTHRU:
1047 ret = i2o_cfg_passthru(arg);
1048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 default:
1051 osm_debug("unknown ioctl called!\n");
1052 ret = -EINVAL;
1053 }
1054
1055 return ret;
1056}
1057
1058static int cfg_open(struct inode *inode, struct file *file)
1059{
1060 struct i2o_cfg_info *tmp =
1061 (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
1062 GFP_KERNEL);
1063 unsigned long flags;
1064
1065 if (!tmp)
1066 return -ENOMEM;
1067
1068 file->private_data = (void *)(i2o_cfg_info_id++);
1069 tmp->fp = file;
1070 tmp->fasync = NULL;
1071 tmp->q_id = (ulong) file->private_data;
1072 tmp->q_len = 0;
1073 tmp->q_in = 0;
1074 tmp->q_out = 0;
1075 tmp->q_lost = 0;
1076 tmp->next = open_files;
1077
1078 spin_lock_irqsave(&i2o_config_lock, flags);
1079 open_files = tmp;
1080 spin_unlock_irqrestore(&i2o_config_lock, flags);
1081
1082 return 0;
1083}
1084
1085static int cfg_fasync(int fd, struct file *fp, int on)
1086{
1087 ulong id = (ulong) fp->private_data;
1088 struct i2o_cfg_info *p;
1089
1090 for (p = open_files; p; p = p->next)
1091 if (p->q_id == id)
1092 break;
1093
1094 if (!p)
1095 return -EBADF;
1096
1097 return fasync_helper(fd, fp, on, &p->fasync);
1098}
1099
1100static int cfg_release(struct inode *inode, struct file *file)
1101{
1102 ulong id = (ulong) file->private_data;
1103 struct i2o_cfg_info *p1, *p2;
1104 unsigned long flags;
1105
1106 lock_kernel();
1107 p1 = p2 = NULL;
1108
1109 spin_lock_irqsave(&i2o_config_lock, flags);
1110 for (p1 = open_files; p1;) {
1111 if (p1->q_id == id) {
1112
1113 if (p1->fasync)
1114 cfg_fasync(-1, file, 0);
1115 if (p2)
1116 p2->next = p1->next;
1117 else
1118 open_files = p1->next;
1119
1120 kfree(p1);
1121 break;
1122 }
1123 p2 = p1;
1124 p1 = p1->next;
1125 }
1126 spin_unlock_irqrestore(&i2o_config_lock, flags);
1127 unlock_kernel();
1128
1129 return 0;
1130}
1131
1132static struct file_operations config_fops = {
1133 .owner = THIS_MODULE,
1134 .llseek = no_llseek,
1135 .ioctl = i2o_cfg_ioctl,
f4c2c152005-04-10 22:29:42 -05001136#ifdef CONFIG_COMPAT
1137 .compat_ioctl = i2o_cfg_compat_ioctl,
1138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 .open = cfg_open,
1140 .release = cfg_release,
1141 .fasync = cfg_fasync,
1142};
1143
1144static struct miscdevice i2o_miscdev = {
1145 I2O_MINOR,
1146 "i2octl",
1147 &config_fops
1148};
1149
1150static int __init i2o_config_init(void)
1151{
1152 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1153
1154 spin_lock_init(&i2o_config_lock);
1155
1156 if (misc_register(&i2o_miscdev) < 0) {
1157 osm_err("can't register device.\n");
1158 return -EBUSY;
1159 }
1160 /*
1161 * Install our handler
1162 */
1163 if (i2o_driver_register(&i2o_config_driver)) {
1164 osm_err("handler register failed.\n");
1165 misc_deregister(&i2o_miscdev);
1166 return -EBUSY;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 0;
1169}
1170
1171static void i2o_config_exit(void)
1172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 misc_deregister(&i2o_miscdev);
1174 i2o_driver_unregister(&i2o_config_driver);
1175}
1176
1177MODULE_AUTHOR("Red Hat Software");
1178MODULE_LICENSE("GPL");
1179MODULE_DESCRIPTION(OSM_DESCRIPTION);
1180MODULE_VERSION(OSM_VERSION);
1181
1182module_init(i2o_config_init);
1183module_exit(i2o_config_exit);