blob: 8ebc86ff100297101d8b9c5710ccb92d9bb24c2d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/miscdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Markus Lidel9e875452005-06-23 22:02:21 -070039#define SG_TABLESIZE 30
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041extern int i2o_parm_issue(struct i2o_device *, int, void *, int, void *, int);
42
f4c2c152005-04-10 22:29:42 -050043static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
44 unsigned long arg);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static spinlock_t i2o_config_lock;
47
48#define MODINC(x,y) ((x) = ((x) + 1) % (y))
49
50struct sg_simple_element {
51 u32 flag_count;
52 u32 addr_bus;
53};
54
55struct i2o_cfg_info {
56 struct file *fp;
57 struct fasync_struct *fasync;
58 struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
59 u16 q_in; // Queue head index
60 u16 q_out; // Queue tail index
61 u16 q_len; // Queue length
62 u16 q_lost; // Number of lost events
63 ulong q_id; // Event queue ID...used as tx_context
64 struct i2o_cfg_info *next;
65};
66static struct i2o_cfg_info *open_files = NULL;
67static ulong i2o_cfg_info_id = 0;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int i2o_cfg_getiops(unsigned long arg)
70{
71 struct i2o_controller *c;
72 u8 __user *user_iop_table = (void __user *)arg;
73 u8 tmp[MAX_I2O_CONTROLLERS];
74 int ret = 0;
75
76 memset(tmp, 0, MAX_I2O_CONTROLLERS);
77
78 list_for_each_entry(c, &i2o_controllers, list)
79 tmp[c->unit] = 1;
80
81 if (copy_to_user(user_iop_table, tmp, MAX_I2O_CONTROLLERS))
82 ret = -EFAULT;
83
84 return ret;
85};
86
87static int i2o_cfg_gethrt(unsigned long arg)
88{
89 struct i2o_controller *c;
90 struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
91 struct i2o_cmd_hrtlct kcmd;
92 i2o_hrt *hrt;
93 int len;
94 u32 reslen;
95 int ret = 0;
96
97 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
98 return -EFAULT;
99
100 if (get_user(reslen, kcmd.reslen) < 0)
101 return -EFAULT;
102
103 if (kcmd.resbuf == NULL)
104 return -EFAULT;
105
106 c = i2o_find_iop(kcmd.iop);
107 if (!c)
108 return -ENXIO;
109
110 hrt = (i2o_hrt *) c->hrt.virt;
111
112 len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
113
114 /* We did a get user...so assuming mem is ok...is this bad? */
115 put_user(len, kcmd.reslen);
116 if (len > reslen)
117 ret = -ENOBUFS;
118 if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
119 ret = -EFAULT;
120
121 return ret;
122};
123
124static int i2o_cfg_getlct(unsigned long arg)
125{
126 struct i2o_controller *c;
127 struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
128 struct i2o_cmd_hrtlct kcmd;
129 i2o_lct *lct;
130 int len;
131 int ret = 0;
132 u32 reslen;
133
134 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
135 return -EFAULT;
136
137 if (get_user(reslen, kcmd.reslen) < 0)
138 return -EFAULT;
139
140 if (kcmd.resbuf == NULL)
141 return -EFAULT;
142
143 c = i2o_find_iop(kcmd.iop);
144 if (!c)
145 return -ENXIO;
146
147 lct = (i2o_lct *) c->lct;
148
149 len = (unsigned int)lct->table_size << 2;
150 put_user(len, kcmd.reslen);
151 if (len > reslen)
152 ret = -ENOBUFS;
153 else if (copy_to_user(kcmd.resbuf, lct, len))
154 ret = -EFAULT;
155
156 return ret;
157};
158
159static int i2o_cfg_parms(unsigned long arg, unsigned int type)
160{
161 int ret = 0;
162 struct i2o_controller *c;
163 struct i2o_device *dev;
164 struct i2o_cmd_psetget __user *cmd =
165 (struct i2o_cmd_psetget __user *)arg;
166 struct i2o_cmd_psetget kcmd;
167 u32 reslen;
168 u8 *ops;
169 u8 *res;
170 int len = 0;
171
172 u32 i2o_cmd = (type == I2OPARMGET ?
173 I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
174
175 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
176 return -EFAULT;
177
178 if (get_user(reslen, kcmd.reslen))
179 return -EFAULT;
180
181 c = i2o_find_iop(kcmd.iop);
182 if (!c)
183 return -ENXIO;
184
185 dev = i2o_iop_find_device(c, kcmd.tid);
186 if (!dev)
187 return -ENXIO;
188
189 ops = (u8 *) kmalloc(kcmd.oplen, GFP_KERNEL);
190 if (!ops)
191 return -ENOMEM;
192
193 if (copy_from_user(ops, kcmd.opbuf, kcmd.oplen)) {
194 kfree(ops);
195 return -EFAULT;
196 }
197
198 /*
199 * It's possible to have a _very_ large table
200 * and that the user asks for all of it at once...
201 */
202 res = (u8 *) kmalloc(65536, GFP_KERNEL);
203 if (!res) {
204 kfree(ops);
205 return -ENOMEM;
206 }
207
208 len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
209 kfree(ops);
210
211 if (len < 0) {
212 kfree(res);
213 return -EAGAIN;
214 }
215
216 put_user(len, kcmd.reslen);
217 if (len > reslen)
218 ret = -ENOBUFS;
219 else if (copy_to_user(kcmd.resbuf, res, len))
220 ret = -EFAULT;
221
222 kfree(res);
223
224 return ret;
225};
226
227static int i2o_cfg_swdl(unsigned long arg)
228{
229 struct i2o_sw_xfer kxfer;
230 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
231 unsigned char maxfrag = 0, curfrag = 1;
232 struct i2o_dma buffer;
233 struct i2o_message __iomem *msg;
234 u32 m;
235 unsigned int status = 0, swlen = 0, fragsize = 8192;
236 struct i2o_controller *c;
237
238 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
239 return -EFAULT;
240
241 if (get_user(swlen, kxfer.swlen) < 0)
242 return -EFAULT;
243
244 if (get_user(maxfrag, kxfer.maxfrag) < 0)
245 return -EFAULT;
246
247 if (get_user(curfrag, kxfer.curfrag) < 0)
248 return -EFAULT;
249
250 if (curfrag == maxfrag)
251 fragsize = swlen - (maxfrag - 1) * 8192;
252
253 if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
254 return -EFAULT;
255
256 c = i2o_find_iop(kxfer.iop);
257 if (!c)
258 return -ENXIO;
259
260 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
261 if (m == I2O_QUEUE_EMPTY)
262 return -EBUSY;
263
264 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
265 i2o_msg_nop(c, m);
266 return -ENOMEM;
267 }
268
269 __copy_from_user(buffer.virt, kxfer.buf, fragsize);
270
271 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
272 writel(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 | ADAPTER_TID,
273 &msg->u.head[1]);
274 writel(i2o_config_driver.context, &msg->u.head[2]);
275 writel(0, &msg->u.head[3]);
276 writel((((u32) kxfer.flags) << 24) | (((u32) kxfer.sw_type) << 16) |
277 (((u32) maxfrag) << 8) | (((u32) curfrag)), &msg->body[0]);
278 writel(swlen, &msg->body[1]);
279 writel(kxfer.sw_id, &msg->body[2]);
280 writel(0xD0000000 | fragsize, &msg->body[3]);
281 writel(buffer.phys, &msg->body[4]);
282
283 osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
284 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
285
286 if (status != -ETIMEDOUT)
287 i2o_dma_free(&c->pdev->dev, &buffer);
288
289 if (status != I2O_POST_WAIT_OK) {
290 // it fails if you try and send frags out of order
291 // and for some yet unknown reasons too
292 osm_info("swdl failed, DetailedStatus = %d\n", status);
293 return status;
294 }
295
296 return 0;
297};
298
299static int i2o_cfg_swul(unsigned long arg)
300{
301 struct i2o_sw_xfer kxfer;
302 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
303 unsigned char maxfrag = 0, curfrag = 1;
304 struct i2o_dma buffer;
305 struct i2o_message __iomem *msg;
306 u32 m;
307 unsigned int status = 0, swlen = 0, fragsize = 8192;
308 struct i2o_controller *c;
309 int ret = 0;
310
311 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
312 goto return_fault;
313
314 if (get_user(swlen, kxfer.swlen) < 0)
315 goto return_fault;
316
317 if (get_user(maxfrag, kxfer.maxfrag) < 0)
318 goto return_fault;
319
320 if (get_user(curfrag, kxfer.curfrag) < 0)
321 goto return_fault;
322
323 if (curfrag == maxfrag)
324 fragsize = swlen - (maxfrag - 1) * 8192;
325
326 if (!kxfer.buf)
327 goto return_fault;
328
329 c = i2o_find_iop(kxfer.iop);
330 if (!c)
331 return -ENXIO;
332
333 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
334 if (m == I2O_QUEUE_EMPTY)
335 return -EBUSY;
336
337 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize, GFP_KERNEL)) {
338 i2o_msg_nop(c, m);
339 return -ENOMEM;
340 }
341
342 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
343 writel(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID,
344 &msg->u.head[1]);
345 writel(i2o_config_driver.context, &msg->u.head[2]);
346 writel(0, &msg->u.head[3]);
347 writel((u32) kxfer.flags << 24 | (u32) kxfer.
348 sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag,
349 &msg->body[0]);
350 writel(swlen, &msg->body[1]);
351 writel(kxfer.sw_id, &msg->body[2]);
352 writel(0xD0000000 | fragsize, &msg->body[3]);
353 writel(buffer.phys, &msg->body[4]);
354
355 osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
356 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
357
358 if (status != I2O_POST_WAIT_OK) {
359 if (status != -ETIMEDOUT)
360 i2o_dma_free(&c->pdev->dev, &buffer);
361
362 osm_info("swul failed, DetailedStatus = %d\n", status);
363 return status;
364 }
365
366 if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
367 ret = -EFAULT;
368
369 i2o_dma_free(&c->pdev->dev, &buffer);
370
Markus Lidelf33213e2005-06-23 22:02:23 -0700371 return_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return ret;
Markus Lidelf33213e2005-06-23 22:02:23 -0700373 return_fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ret = -EFAULT;
375 goto return_ret;
376};
377
378static int i2o_cfg_swdel(unsigned long arg)
379{
380 struct i2o_controller *c;
381 struct i2o_sw_xfer kxfer;
382 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
383 struct i2o_message __iomem *msg;
384 u32 m;
385 unsigned int swlen;
386 int token;
387
388 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
389 return -EFAULT;
390
391 if (get_user(swlen, kxfer.swlen) < 0)
392 return -EFAULT;
393
394 c = i2o_find_iop(kxfer.iop);
395 if (!c)
396 return -ENXIO;
397
398 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
399 if (m == I2O_QUEUE_EMPTY)
400 return -EBUSY;
401
402 writel(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
403 writel(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID,
404 &msg->u.head[1]);
405 writel(i2o_config_driver.context, &msg->u.head[2]);
406 writel(0, &msg->u.head[3]);
407 writel((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16,
408 &msg->body[0]);
409 writel(swlen, &msg->body[1]);
410 writel(kxfer.sw_id, &msg->body[2]);
411
412 token = i2o_msg_post_wait(c, m, 10);
413
414 if (token != I2O_POST_WAIT_OK) {
415 osm_info("swdel failed, DetailedStatus = %d\n", token);
416 return -ETIMEDOUT;
417 }
418
419 return 0;
420};
421
422static int i2o_cfg_validate(unsigned long arg)
423{
424 int token;
425 int iop = (int)arg;
426 struct i2o_message __iomem *msg;
427 u32 m;
428 struct i2o_controller *c;
429
430 c = i2o_find_iop(iop);
431 if (!c)
432 return -ENXIO;
433
434 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
435 if (m == I2O_QUEUE_EMPTY)
436 return -EBUSY;
437
438 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
439 writel(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop,
440 &msg->u.head[1]);
441 writel(i2o_config_driver.context, &msg->u.head[2]);
442 writel(0, &msg->u.head[3]);
443
444 token = i2o_msg_post_wait(c, m, 10);
445
446 if (token != I2O_POST_WAIT_OK) {
447 osm_info("Can't validate configuration, ErrorStatus = %d\n",
448 token);
449 return -ETIMEDOUT;
450 }
451
452 return 0;
453};
454
455static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
456{
457 struct i2o_message __iomem *msg;
458 u32 m;
459 struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
460 struct i2o_evt_id kdesc;
461 struct i2o_controller *c;
462 struct i2o_device *d;
463
464 if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
465 return -EFAULT;
466
467 /* IOP exists? */
468 c = i2o_find_iop(kdesc.iop);
469 if (!c)
470 return -ENXIO;
471
472 /* Device exists? */
473 d = i2o_iop_find_device(c, kdesc.tid);
474 if (!d)
475 return -ENODEV;
476
477 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
478 if (m == I2O_QUEUE_EMPTY)
479 return -EBUSY;
480
481 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
482 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | kdesc.tid,
483 &msg->u.head[1]);
484 writel(i2o_config_driver.context, &msg->u.head[2]);
485 writel(i2o_cntxt_list_add(c, fp->private_data), &msg->u.head[3]);
486 writel(kdesc.evt_mask, &msg->body[0]);
487
488 i2o_msg_post(c, m);
489
490 return 0;
491}
492
493static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
494{
495 struct i2o_cfg_info *p = NULL;
496 struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
497 struct i2o_evt_get kget;
498 unsigned long flags;
499
500 for (p = open_files; p; p = p->next)
501 if (p->q_id == (ulong) fp->private_data)
502 break;
503
504 if (!p->q_len)
505 return -ENOENT;
506
507 memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
508 MODINC(p->q_out, I2O_EVT_Q_LEN);
509 spin_lock_irqsave(&i2o_config_lock, flags);
510 p->q_len--;
511 kget.pending = p->q_len;
512 kget.lost = p->q_lost;
513 spin_unlock_irqrestore(&i2o_config_lock, flags);
514
515 if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
516 return -EFAULT;
517 return 0;
518}
519
Markus Lidelb2aaee32005-06-23 22:02:19 -0700520#ifdef CONFIG_I2O_EXT_ADAPTEC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#ifdef CONFIG_COMPAT
Markus Lidelf33213e2005-06-23 22:02:23 -0700522static int i2o_cfg_passthru32(struct file *file, unsigned cmnd,
523 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct i2o_cmd_passthru32 __user *cmd;
526 struct i2o_controller *c;
527 u32 __user *user_msg;
528 u32 *reply = NULL;
529 u32 __user *user_reply = NULL;
530 u32 size = 0;
531 u32 reply_size = 0;
532 u32 rcode = 0;
533 struct i2o_dma sg_list[SG_TABLESIZE];
534 u32 sg_offset = 0;
535 u32 sg_count = 0;
536 u32 i = 0;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700537 u32 sg_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 i2o_status_block *sb;
539 struct i2o_message *msg;
540 u32 m;
541 unsigned int iop;
542
543 cmd = (struct i2o_cmd_passthru32 __user *)arg;
544
545 if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
546 return -EFAULT;
547
548 user_msg = compat_ptr(i);
549
550 c = i2o_find_iop(iop);
551 if (!c) {
552 osm_debug("controller %d not found\n", iop);
553 return -ENXIO;
554 }
555
556 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
557
558 sb = c->status_block.virt;
559
560 if (get_user(size, &user_msg[0])) {
561 osm_warn("unable to get size!\n");
562 return -EFAULT;
563 }
564 size = size >> 16;
565
566 if (size > sb->inbound_frame_size) {
567 osm_warn("size of message > inbound_frame_size");
568 return -EFAULT;
569 }
570
571 user_reply = &user_msg[size];
572
573 size <<= 2; // Convert to bytes
574
575 /* Copy in the user's I2O command */
576 if (copy_from_user(msg, user_msg, size)) {
577 osm_warn("unable to copy user message\n");
578 return -EFAULT;
579 }
580 i2o_dump_message(msg);
581
582 if (get_user(reply_size, &user_reply[0]) < 0)
583 return -EFAULT;
584
585 reply_size >>= 16;
586 reply_size <<= 2;
587
588 reply = kmalloc(reply_size, GFP_KERNEL);
589 if (!reply) {
590 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
591 c->name);
592 return -ENOMEM;
593 }
594 memset(reply, 0, reply_size);
595
596 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
597
598 writel(i2o_config_driver.context, &msg->u.s.icntxt);
599 writel(i2o_cntxt_list_add(c, reply), &msg->u.s.tcntxt);
600
601 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
602 if (sg_offset) {
603 struct sg_simple_element *sg;
604
605 if (sg_offset * 4 >= size) {
606 rcode = -EFAULT;
607 goto cleanup;
608 }
609 // TODO 64bit fix
610 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
611 sg_offset);
612 sg_count =
613 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
614 if (sg_count > SG_TABLESIZE) {
615 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
616 c->name, sg_count);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700617 rcode = -EINVAL;
618 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621 for (i = 0; i < sg_count; i++) {
622 int sg_size;
623 struct i2o_dma *p;
624
625 if (!(sg[i].flag_count & 0x10000000
626 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
627 printk(KERN_DEBUG
628 "%s:Bad SG element %d - not simple (%x)\n",
629 c->name, i, sg[i].flag_count);
630 rcode = -EINVAL;
631 goto cleanup;
632 }
633 sg_size = sg[i].flag_count & 0xffffff;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700634 p = &(sg_list[sg_index++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Allocate memory for the transfer */
636 if (i2o_dma_alloc
637 (&c->pdev->dev, p, sg_size,
638 PCI_DMA_BIDIRECTIONAL)) {
639 printk(KERN_DEBUG
640 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
641 c->name, sg_size, i, sg_count);
642 rcode = -ENOMEM;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700643 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 /* Copy in the user's SG buffer if necessary */
646 if (sg[i].
647 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
648 // TODO 64bit fix
649 if (copy_from_user
Markus Lidelf33213e2005-06-23 22:02:23 -0700650 (p->virt,
651 (void __user *)(unsigned long)sg[i].
652 addr_bus, sg_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 printk(KERN_DEBUG
654 "%s: Could not copy SG buf %d FROM user\n",
655 c->name, i);
656 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700657 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 }
660 //TODO 64bit fix
661 sg[i].addr_bus = (u32) p->phys;
662 }
663 }
664
665 rcode = i2o_msg_post_wait(c, m, 60);
666 if (rcode)
Markus Lidel61fbfa82005-06-23 22:02:11 -0700667 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (sg_offset) {
Markus Lidel9e875452005-06-23 22:02:21 -0700670 u32 msg[I2O_OUTBOUND_MSG_FRAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Copy back the Scatter Gather buffers back to user space */
672 u32 j;
673 // TODO 64bit fix
674 struct sg_simple_element *sg;
675 int sg_size;
676
677 // re-acquire the original message to handle correctly the sg copy operation
Markus Lidel9e875452005-06-23 22:02:21 -0700678 memset(&msg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 // get user msg size in u32s
680 if (get_user(size, &user_msg[0])) {
681 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700682 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 size = size >> 16;
685 size *= 4;
686 /* Copy in the user's I2O command */
687 if (copy_from_user(msg, user_msg, size)) {
688 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700689 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 sg_count =
692 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
693
694 // TODO 64bit fix
695 sg = (struct sg_simple_element *)(msg + sg_offset);
696 for (j = 0; j < sg_count; j++) {
697 /* Copy out the SG list to user's buffer if necessary */
698 if (!
699 (sg[j].
700 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
701 sg_size = sg[j].flag_count & 0xffffff;
702 // TODO 64bit fix
703 if (copy_to_user
704 ((void __user *)(u64) sg[j].addr_bus,
705 sg_list[j].virt, sg_size)) {
706 printk(KERN_WARNING
707 "%s: Could not copy %p TO user %x\n",
708 c->name, sg_list[j].virt,
709 sg[j].addr_bus);
710 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700711 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 }
714 }
715 }
716
717 /* Copy back the reply to user space */
718 if (reply_size) {
719 // we wrote our own values for context - now restore the user supplied ones
720 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
721 printk(KERN_WARNING
722 "%s: Could not copy message context FROM user\n",
723 c->name);
724 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700725 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 if (copy_to_user(user_reply, reply, reply_size)) {
728 printk(KERN_WARNING
729 "%s: Could not copy reply TO user\n", c->name);
730 rcode = -EFAULT;
731 }
732 }
733
Markus Lidel61fbfa82005-06-23 22:02:11 -0700734 sg_list_cleanup:
735 for (i = 0; i < sg_index; i++)
736 i2o_dma_free(&c->pdev->dev, &sg_list[i]);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 cleanup:
739 kfree(reply);
740 return rcode;
741}
742
Markus Lidelf33213e2005-06-23 22:02:23 -0700743static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd,
744 unsigned long arg)
f4c2c152005-04-10 22:29:42 -0500745{
746 int ret;
Markus Lidelf33213e2005-06-23 22:02:23 -0700747 lock_kernel();
748 switch (cmd) {
f4c2c152005-04-10 22:29:42 -0500749 case I2OGETIOPS:
750 ret = i2o_cfg_ioctl(NULL, file, cmd, arg);
751 break;
752 case I2OPASSTHRU32:
753 ret = i2o_cfg_passthru32(file, cmd, arg);
754 break;
755 default:
756 ret = -ENOIOCTLCMD;
757 break;
758 }
759 unlock_kernel();
760 return ret;
761}
762
763#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765static int i2o_cfg_passthru(unsigned long arg)
766{
767 struct i2o_cmd_passthru __user *cmd =
768 (struct i2o_cmd_passthru __user *)arg;
769 struct i2o_controller *c;
770 u32 __user *user_msg;
771 u32 *reply = NULL;
772 u32 __user *user_reply = NULL;
773 u32 size = 0;
774 u32 reply_size = 0;
775 u32 rcode = 0;
776 void *sg_list[SG_TABLESIZE];
777 u32 sg_offset = 0;
778 u32 sg_count = 0;
779 int sg_index = 0;
780 u32 i = 0;
781 void *p = NULL;
782 i2o_status_block *sb;
783 struct i2o_message __iomem *msg;
784 u32 m;
785 unsigned int iop;
786
787 if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
788 return -EFAULT;
789
790 c = i2o_find_iop(iop);
791 if (!c) {
792 osm_warn("controller %d not found\n", iop);
793 return -ENXIO;
794 }
795
796 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
797
798 sb = c->status_block.virt;
799
800 if (get_user(size, &user_msg[0]))
801 return -EFAULT;
802 size = size >> 16;
803
804 if (size > sb->inbound_frame_size) {
805 osm_warn("size of message > inbound_frame_size");
806 return -EFAULT;
807 }
808
809 user_reply = &user_msg[size];
810
811 size <<= 2; // Convert to bytes
812
813 /* Copy in the user's I2O command */
814 if (copy_from_user(msg, user_msg, size))
815 return -EFAULT;
816
817 if (get_user(reply_size, &user_reply[0]) < 0)
818 return -EFAULT;
819
820 reply_size >>= 16;
821 reply_size <<= 2;
822
823 reply = kmalloc(reply_size, GFP_KERNEL);
824 if (!reply) {
825 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
826 c->name);
827 return -ENOMEM;
828 }
829 memset(reply, 0, reply_size);
830
831 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
832
833 writel(i2o_config_driver.context, &msg->u.s.icntxt);
834 writel(i2o_cntxt_list_add(c, reply), &msg->u.s.tcntxt);
835
836 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
837 if (sg_offset) {
838 struct sg_simple_element *sg;
839
840 if (sg_offset * 4 >= size) {
841 rcode = -EFAULT;
842 goto cleanup;
843 }
844 // TODO 64bit fix
845 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
846 sg_offset);
847 sg_count =
848 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
849 if (sg_count > SG_TABLESIZE) {
850 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
851 c->name, sg_count);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700852 rcode = -EINVAL;
853 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856 for (i = 0; i < sg_count; i++) {
857 int sg_size;
858
859 if (!(sg[i].flag_count & 0x10000000
860 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
861 printk(KERN_DEBUG
862 "%s:Bad SG element %d - not simple (%x)\n",
863 c->name, i, sg[i].flag_count);
864 rcode = -EINVAL;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700865 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 sg_size = sg[i].flag_count & 0xffffff;
868 /* Allocate memory for the transfer */
869 p = kmalloc(sg_size, GFP_KERNEL);
870 if (!p) {
871 printk(KERN_DEBUG
872 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
873 c->name, sg_size, i, sg_count);
874 rcode = -ENOMEM;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700875 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
878 /* Copy in the user's SG buffer if necessary */
879 if (sg[i].
880 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
881 // TODO 64bit fix
882 if (copy_from_user
883 (p, (void __user *)sg[i].addr_bus,
884 sg_size)) {
885 printk(KERN_DEBUG
886 "%s: Could not copy SG buf %d FROM user\n",
887 c->name, i);
888 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700889 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 }
892 //TODO 64bit fix
893 sg[i].addr_bus = virt_to_bus(p);
894 }
895 }
896
897 rcode = i2o_msg_post_wait(c, m, 60);
898 if (rcode)
Markus Lidel61fbfa82005-06-23 22:02:11 -0700899 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (sg_offset) {
902 u32 msg[128];
903 /* Copy back the Scatter Gather buffers back to user space */
904 u32 j;
905 // TODO 64bit fix
906 struct sg_simple_element *sg;
907 int sg_size;
908
909 // re-acquire the original message to handle correctly the sg copy operation
Markus Lidel9e875452005-06-23 22:02:21 -0700910 memset(&msg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 // get user msg size in u32s
912 if (get_user(size, &user_msg[0])) {
913 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700914 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 size = size >> 16;
917 size *= 4;
918 /* Copy in the user's I2O command */
919 if (copy_from_user(msg, user_msg, size)) {
920 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700921 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 sg_count =
924 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
925
926 // TODO 64bit fix
927 sg = (struct sg_simple_element *)(msg + sg_offset);
928 for (j = 0; j < sg_count; j++) {
929 /* Copy out the SG list to user's buffer if necessary */
930 if (!
931 (sg[j].
932 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
933 sg_size = sg[j].flag_count & 0xffffff;
934 // TODO 64bit fix
935 if (copy_to_user
936 ((void __user *)sg[j].addr_bus, sg_list[j],
937 sg_size)) {
938 printk(KERN_WARNING
939 "%s: Could not copy %p TO user %x\n",
940 c->name, sg_list[j],
941 sg[j].addr_bus);
942 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700943 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 }
946 }
947 }
948
949 /* Copy back the reply to user space */
950 if (reply_size) {
951 // we wrote our own values for context - now restore the user supplied ones
952 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
953 printk(KERN_WARNING
954 "%s: Could not copy message context FROM user\n",
955 c->name);
956 rcode = -EFAULT;
957 }
958 if (copy_to_user(user_reply, reply, reply_size)) {
959 printk(KERN_WARNING
960 "%s: Could not copy reply TO user\n", c->name);
961 rcode = -EFAULT;
962 }
963 }
964
Markus Lidel61fbfa82005-06-23 22:02:11 -0700965 sg_list_cleanup:
966 for (i = 0; i < sg_index; i++)
967 kfree(sg_list[i]);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 cleanup:
970 kfree(reply);
971 return rcode;
972}
Markus Lidelb2aaee32005-06-23 22:02:19 -0700973#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975/*
976 * IOCTL Handler
977 */
978static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
979 unsigned long arg)
980{
981 int ret;
982
983 switch (cmd) {
984 case I2OGETIOPS:
985 ret = i2o_cfg_getiops(arg);
986 break;
987
988 case I2OHRTGET:
989 ret = i2o_cfg_gethrt(arg);
990 break;
991
992 case I2OLCTGET:
993 ret = i2o_cfg_getlct(arg);
994 break;
995
996 case I2OPARMSET:
997 ret = i2o_cfg_parms(arg, I2OPARMSET);
998 break;
999
1000 case I2OPARMGET:
1001 ret = i2o_cfg_parms(arg, I2OPARMGET);
1002 break;
1003
1004 case I2OSWDL:
1005 ret = i2o_cfg_swdl(arg);
1006 break;
1007
1008 case I2OSWUL:
1009 ret = i2o_cfg_swul(arg);
1010 break;
1011
1012 case I2OSWDEL:
1013 ret = i2o_cfg_swdel(arg);
1014 break;
1015
1016 case I2OVALIDATE:
1017 ret = i2o_cfg_validate(arg);
1018 break;
1019
1020 case I2OEVTREG:
1021 ret = i2o_cfg_evt_reg(arg, fp);
1022 break;
1023
1024 case I2OEVTGET:
1025 ret = i2o_cfg_evt_get(arg, fp);
1026 break;
1027
Markus Lidelb2aaee32005-06-23 22:02:19 -07001028#ifdef CONFIG_I2O_EXT_ADAPTEC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 case I2OPASSTHRU:
1030 ret = i2o_cfg_passthru(arg);
1031 break;
Markus Lidelb2aaee32005-06-23 22:02:19 -07001032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 default:
1035 osm_debug("unknown ioctl called!\n");
1036 ret = -EINVAL;
1037 }
1038
1039 return ret;
1040}
1041
1042static int cfg_open(struct inode *inode, struct file *file)
1043{
1044 struct i2o_cfg_info *tmp =
1045 (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
1046 GFP_KERNEL);
1047 unsigned long flags;
1048
1049 if (!tmp)
1050 return -ENOMEM;
1051
1052 file->private_data = (void *)(i2o_cfg_info_id++);
1053 tmp->fp = file;
1054 tmp->fasync = NULL;
1055 tmp->q_id = (ulong) file->private_data;
1056 tmp->q_len = 0;
1057 tmp->q_in = 0;
1058 tmp->q_out = 0;
1059 tmp->q_lost = 0;
1060 tmp->next = open_files;
1061
1062 spin_lock_irqsave(&i2o_config_lock, flags);
1063 open_files = tmp;
1064 spin_unlock_irqrestore(&i2o_config_lock, flags);
1065
1066 return 0;
1067}
1068
1069static int cfg_fasync(int fd, struct file *fp, int on)
1070{
1071 ulong id = (ulong) fp->private_data;
1072 struct i2o_cfg_info *p;
1073
1074 for (p = open_files; p; p = p->next)
1075 if (p->q_id == id)
1076 break;
1077
1078 if (!p)
1079 return -EBADF;
1080
1081 return fasync_helper(fd, fp, on, &p->fasync);
1082}
1083
1084static int cfg_release(struct inode *inode, struct file *file)
1085{
1086 ulong id = (ulong) file->private_data;
1087 struct i2o_cfg_info *p1, *p2;
1088 unsigned long flags;
1089
1090 lock_kernel();
1091 p1 = p2 = NULL;
1092
1093 spin_lock_irqsave(&i2o_config_lock, flags);
1094 for (p1 = open_files; p1;) {
1095 if (p1->q_id == id) {
1096
1097 if (p1->fasync)
1098 cfg_fasync(-1, file, 0);
1099 if (p2)
1100 p2->next = p1->next;
1101 else
1102 open_files = p1->next;
1103
1104 kfree(p1);
1105 break;
1106 }
1107 p2 = p1;
1108 p1 = p1->next;
1109 }
1110 spin_unlock_irqrestore(&i2o_config_lock, flags);
1111 unlock_kernel();
1112
1113 return 0;
1114}
1115
1116static struct file_operations config_fops = {
1117 .owner = THIS_MODULE,
1118 .llseek = no_llseek,
1119 .ioctl = i2o_cfg_ioctl,
f4c2c152005-04-10 22:29:42 -05001120#ifdef CONFIG_COMPAT
1121 .compat_ioctl = i2o_cfg_compat_ioctl,
1122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 .open = cfg_open,
1124 .release = cfg_release,
1125 .fasync = cfg_fasync,
1126};
1127
1128static struct miscdevice i2o_miscdev = {
1129 I2O_MINOR,
1130 "i2octl",
1131 &config_fops
1132};
1133
Markus Lidelf10378f2005-06-23 22:02:16 -07001134static int __init i2o_config_old_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 spin_lock_init(&i2o_config_lock);
1137
1138 if (misc_register(&i2o_miscdev) < 0) {
1139 osm_err("can't register device.\n");
1140 return -EBUSY;
1141 }
Markus Lidelf33213e2005-06-23 22:02:23 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return 0;
1144}
1145
Markus Lidelf10378f2005-06-23 22:02:16 -07001146static void i2o_config_old_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 misc_deregister(&i2o_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151MODULE_AUTHOR("Red Hat Software");