blob: 068ba0785bb4e1eaa3db77fcc0fd84bd7cbd6f67 [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)
Jan Engelhardt96de0e22007-10-19 23:21:04 +020013 * Auvo Häkkinen (09/10/1999):
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Changes to i2o_cfg_reply(), ioctl_parms()
15 * Added ioct_validate()
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016 * Taneli Vähäkangas (09/30/1999):
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Fixed ioctl_swdl()
Jan Engelhardt96de0e22007-10-19 23:21:04 +020018 * Taneli Vähäkangas (10/04/1999):
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Changed ioctl_swdl(), implemented ioctl_swul() and ioctl_swdel()
20 * Deepak Saxena (11/18/1999):
21 * Added event managmenet support
Alan Coxf1b11e52009-01-05 14:04:40 +000022 * Alan Cox <alan@lxorguk.ukuu.org.uk>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Adrian Bunke62c23c2006-07-10 04:45:40 -070040#include "core.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Adrian Bunke62c23c2006-07-10 04:45:40 -070042#define SG_TABLESIZE 30
Markus Lideldcceafe2006-01-06 00:19:32 -080043
Alan Cox5d9d6e42010-01-04 16:18:27 +000044static long i2o_cfg_ioctl(struct file *, unsigned int, unsigned long);
f4c2c152005-04-10 22:29:42 -050045
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
Kulikov Vasiliy89596f22010-08-10 18:02:04 -0700114 if (put_user(len, kcmd.reslen))
115 ret = -EFAULT;
116 else if (len > reslen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ret = -ENOBUFS;
Kulikov Vasiliyd929dc22010-08-10 18:02:03 -0700118 else if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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;
Kulikov Vasiliy89596f22010-08-10 18:02:04 -0700150 if (put_user(len, kcmd.reslen))
151 ret = -EFAULT;
152 else if (len > reslen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ret = -ENOBUFS;
154 else if (copy_to_user(kcmd.resbuf, lct, len))
155 ret = -EFAULT;
156
157 return ret;
158};
159
160static int i2o_cfg_parms(unsigned long arg, unsigned int type)
161{
162 int ret = 0;
163 struct i2o_controller *c;
164 struct i2o_device *dev;
165 struct i2o_cmd_psetget __user *cmd =
166 (struct i2o_cmd_psetget __user *)arg;
167 struct i2o_cmd_psetget kcmd;
168 u32 reslen;
169 u8 *ops;
170 u8 *res;
171 int len = 0;
172
173 u32 i2o_cmd = (type == I2OPARMGET ?
174 I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
175
176 if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
177 return -EFAULT;
178
179 if (get_user(reslen, kcmd.reslen))
180 return -EFAULT;
181
182 c = i2o_find_iop(kcmd.iop);
183 if (!c)
184 return -ENXIO;
185
186 dev = i2o_iop_find_device(c, kcmd.tid);
187 if (!dev)
188 return -ENXIO;
189
Julia Lawallb81d67a2010-05-26 14:42:12 -0700190 ops = memdup_user(kcmd.opbuf, kcmd.oplen);
191 if (IS_ERR(ops))
192 return PTR_ERR(ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
195 * It's possible to have a _very_ large table
196 * and that the user asks for all of it at once...
197 */
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800198 res = kmalloc(65536, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!res) {
200 kfree(ops);
201 return -ENOMEM;
202 }
203
204 len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
205 kfree(ops);
206
207 if (len < 0) {
208 kfree(res);
209 return -EAGAIN;
210 }
211
Kulikov Vasiliy89596f22010-08-10 18:02:04 -0700212 if (put_user(len, kcmd.reslen))
213 ret = -EFAULT;
214 else if (len > reslen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ret = -ENOBUFS;
216 else if (copy_to_user(kcmd.resbuf, res, len))
217 ret = -EFAULT;
218
219 kfree(res);
220
221 return ret;
222};
223
224static int i2o_cfg_swdl(unsigned long arg)
225{
226 struct i2o_sw_xfer kxfer;
227 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
228 unsigned char maxfrag = 0, curfrag = 1;
229 struct i2o_dma buffer;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800230 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned int status = 0, swlen = 0, fragsize = 8192;
232 struct i2o_controller *c;
233
234 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
235 return -EFAULT;
236
237 if (get_user(swlen, kxfer.swlen) < 0)
238 return -EFAULT;
239
240 if (get_user(maxfrag, kxfer.maxfrag) < 0)
241 return -EFAULT;
242
243 if (get_user(curfrag, kxfer.curfrag) < 0)
244 return -EFAULT;
245
246 if (curfrag == maxfrag)
247 fragsize = swlen - (maxfrag - 1) * 8192;
248
249 if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
250 return -EFAULT;
251
252 c = i2o_find_iop(kxfer.iop);
253 if (!c)
254 return -ENXIO;
255
Markus Lidela1a5ea72006-01-06 00:19:29 -0800256 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
257 if (IS_ERR(msg))
258 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Alan Cox9d793b02008-10-15 22:02:47 -0700260 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
Markus Lidela1a5ea72006-01-06 00:19:29 -0800261 i2o_msg_nop(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -ENOMEM;
263 }
264
Randy Dunlap9d69b7d2006-12-06 20:38:23 -0800265 if (__copy_from_user(buffer.virt, kxfer.buf, fragsize)) {
266 i2o_msg_nop(c, msg);
267 i2o_dma_free(&c->pdev->dev, &buffer);
268 return -EFAULT;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Markus Lidela1a5ea72006-01-06 00:19:29 -0800271 msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
272 msg->u.head[1] =
273 cpu_to_le32(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 |
274 ADAPTER_TID);
275 msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
276 msg->u.head[3] = cpu_to_le32(0);
277 msg->body[0] =
278 cpu_to_le32((((u32) kxfer.flags) << 24) | (((u32) kxfer.
279 sw_type) << 16) |
280 (((u32) maxfrag) << 8) | (((u32) curfrag)));
281 msg->body[1] = cpu_to_le32(swlen);
282 msg->body[2] = cpu_to_le32(kxfer.sw_id);
283 msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
284 msg->body[4] = cpu_to_le32(buffer.phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
Markus Lidela1a5ea72006-01-06 00:19:29 -0800287 status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (status != -ETIMEDOUT)
290 i2o_dma_free(&c->pdev->dev, &buffer);
291
292 if (status != I2O_POST_WAIT_OK) {
293 // it fails if you try and send frags out of order
294 // and for some yet unknown reasons too
295 osm_info("swdl failed, DetailedStatus = %d\n", status);
296 return status;
297 }
298
299 return 0;
300};
301
302static int i2o_cfg_swul(unsigned long arg)
303{
304 struct i2o_sw_xfer kxfer;
305 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
306 unsigned char maxfrag = 0, curfrag = 1;
307 struct i2o_dma buffer;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800308 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unsigned int status = 0, swlen = 0, fragsize = 8192;
310 struct i2o_controller *c;
311 int ret = 0;
312
313 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
Dan Carpenterb1ffdc82010-04-23 22:19:13 +0200314 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (get_user(swlen, kxfer.swlen) < 0)
Dan Carpenterb1ffdc82010-04-23 22:19:13 +0200317 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (get_user(maxfrag, kxfer.maxfrag) < 0)
Dan Carpenterb1ffdc82010-04-23 22:19:13 +0200320 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (get_user(curfrag, kxfer.curfrag) < 0)
Dan Carpenterb1ffdc82010-04-23 22:19:13 +0200323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (curfrag == maxfrag)
326 fragsize = swlen - (maxfrag - 1) * 8192;
327
328 if (!kxfer.buf)
Dan Carpenterb1ffdc82010-04-23 22:19:13 +0200329 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 c = i2o_find_iop(kxfer.iop);
332 if (!c)
333 return -ENXIO;
334
Markus Lidela1a5ea72006-01-06 00:19:29 -0800335 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
336 if (IS_ERR(msg))
337 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Alan Cox9d793b02008-10-15 22:02:47 -0700339 if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
Markus Lidela1a5ea72006-01-06 00:19:29 -0800340 i2o_msg_nop(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENOMEM;
342 }
343
Markus Lidela1a5ea72006-01-06 00:19:29 -0800344 msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
345 msg->u.head[1] =
346 cpu_to_le32(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID);
347 msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
348 msg->u.head[3] = cpu_to_le32(0);
349 msg->body[0] =
350 cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.
351 sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag);
352 msg->body[1] = cpu_to_le32(swlen);
353 msg->body[2] = cpu_to_le32(kxfer.sw_id);
354 msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
355 msg->body[4] = cpu_to_le32(buffer.phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
Markus Lidela1a5ea72006-01-06 00:19:29 -0800358 status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (status != I2O_POST_WAIT_OK) {
361 if (status != -ETIMEDOUT)
362 i2o_dma_free(&c->pdev->dev, &buffer);
363
364 osm_info("swul failed, DetailedStatus = %d\n", status);
365 return status;
366 }
367
368 if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
369 ret = -EFAULT;
370
371 i2o_dma_free(&c->pdev->dev, &buffer);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return ret;
Dan Carpenterb1ffdc82010-04-23 22:19:13 +0200374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376static int i2o_cfg_swdel(unsigned long arg)
377{
378 struct i2o_controller *c;
379 struct i2o_sw_xfer kxfer;
380 struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800381 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unsigned int swlen;
383 int token;
384
385 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
386 return -EFAULT;
387
388 if (get_user(swlen, kxfer.swlen) < 0)
389 return -EFAULT;
390
391 c = i2o_find_iop(kxfer.iop);
392 if (!c)
393 return -ENXIO;
394
Markus Lidela1a5ea72006-01-06 00:19:29 -0800395 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
396 if (IS_ERR(msg))
397 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Markus Lidela1a5ea72006-01-06 00:19:29 -0800399 msg->u.head[0] = cpu_to_le32(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0);
400 msg->u.head[1] =
401 cpu_to_le32(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID);
402 msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
403 msg->u.head[3] = cpu_to_le32(0);
404 msg->body[0] =
405 cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16);
406 msg->body[1] = cpu_to_le32(swlen);
407 msg->body[2] = cpu_to_le32(kxfer.sw_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Markus Lidela1a5ea72006-01-06 00:19:29 -0800409 token = i2o_msg_post_wait(c, msg, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (token != I2O_POST_WAIT_OK) {
412 osm_info("swdel failed, DetailedStatus = %d\n", token);
413 return -ETIMEDOUT;
414 }
415
416 return 0;
417};
418
419static int i2o_cfg_validate(unsigned long arg)
420{
421 int token;
422 int iop = (int)arg;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800423 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct i2o_controller *c;
425
426 c = i2o_find_iop(iop);
427 if (!c)
428 return -ENXIO;
429
Markus Lidela1a5ea72006-01-06 00:19:29 -0800430 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
431 if (IS_ERR(msg))
432 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Markus Lidela1a5ea72006-01-06 00:19:29 -0800434 msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
435 msg->u.head[1] =
436 cpu_to_le32(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop);
437 msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
438 msg->u.head[3] = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Markus Lidela1a5ea72006-01-06 00:19:29 -0800440 token = i2o_msg_post_wait(c, msg, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (token != I2O_POST_WAIT_OK) {
443 osm_info("Can't validate configuration, ErrorStatus = %d\n",
444 token);
445 return -ETIMEDOUT;
446 }
447
448 return 0;
449};
450
451static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
452{
Markus Lidela1a5ea72006-01-06 00:19:29 -0800453 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
455 struct i2o_evt_id kdesc;
456 struct i2o_controller *c;
457 struct i2o_device *d;
458
459 if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
460 return -EFAULT;
461
462 /* IOP exists? */
463 c = i2o_find_iop(kdesc.iop);
464 if (!c)
465 return -ENXIO;
466
467 /* Device exists? */
468 d = i2o_iop_find_device(c, kdesc.tid);
469 if (!d)
470 return -ENODEV;
471
Markus Lidela1a5ea72006-01-06 00:19:29 -0800472 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
473 if (IS_ERR(msg))
474 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Markus Lidela1a5ea72006-01-06 00:19:29 -0800476 msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
477 msg->u.head[1] =
478 cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 |
479 kdesc.tid);
480 msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
481 msg->u.head[3] = cpu_to_le32(i2o_cntxt_list_add(c, fp->private_data));
482 msg->body[0] = cpu_to_le32(kdesc.evt_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Markus Lidela1a5ea72006-01-06 00:19:29 -0800484 i2o_msg_post(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 return 0;
487}
488
489static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
490{
491 struct i2o_cfg_info *p = NULL;
492 struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
493 struct i2o_evt_get kget;
494 unsigned long flags;
495
496 for (p = open_files; p; p = p->next)
497 if (p->q_id == (ulong) fp->private_data)
498 break;
499
500 if (!p->q_len)
501 return -ENOENT;
502
503 memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
504 MODINC(p->q_out, I2O_EVT_Q_LEN);
505 spin_lock_irqsave(&i2o_config_lock, flags);
506 p->q_len--;
507 kget.pending = p->q_len;
508 kget.lost = p->q_lost;
509 spin_unlock_irqrestore(&i2o_config_lock, flags);
510
511 if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
512 return -EFAULT;
513 return 0;
514}
515
516#ifdef CONFIG_COMPAT
Markus Lidelf33213e2005-06-23 22:02:23 -0700517static int i2o_cfg_passthru32(struct file *file, unsigned cmnd,
518 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct i2o_cmd_passthru32 __user *cmd;
521 struct i2o_controller *c;
522 u32 __user *user_msg;
523 u32 *reply = NULL;
524 u32 __user *user_reply = NULL;
525 u32 size = 0;
526 u32 reply_size = 0;
527 u32 rcode = 0;
528 struct i2o_dma sg_list[SG_TABLESIZE];
529 u32 sg_offset = 0;
530 u32 sg_count = 0;
531 u32 i = 0;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700532 u32 sg_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 i2o_status_block *sb;
534 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned int iop;
536
537 cmd = (struct i2o_cmd_passthru32 __user *)arg;
538
539 if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
540 return -EFAULT;
541
542 user_msg = compat_ptr(i);
543
544 c = i2o_find_iop(iop);
545 if (!c) {
546 osm_debug("controller %d not found\n", iop);
547 return -ENXIO;
548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 sb = c->status_block.virt;
551
552 if (get_user(size, &user_msg[0])) {
553 osm_warn("unable to get size!\n");
554 return -EFAULT;
555 }
556 size = size >> 16;
557
558 if (size > sb->inbound_frame_size) {
559 osm_warn("size of message > inbound_frame_size");
560 return -EFAULT;
561 }
562
563 user_reply = &user_msg[size];
564
565 size <<= 2; // Convert to bytes
566
Vasily Averin1725d712007-07-17 04:04:24 -0700567 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
568 if (IS_ERR(msg))
569 return PTR_ERR(msg);
570
571 rcode = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Copy in the user's I2O command */
573 if (copy_from_user(msg, user_msg, size)) {
574 osm_warn("unable to copy user message\n");
Vasily Averin1725d712007-07-17 04:04:24 -0700575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 i2o_dump_message(msg);
578
579 if (get_user(reply_size, &user_reply[0]) < 0)
Vasily Averin1725d712007-07-17 04:04:24 -0700580 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 reply_size >>= 16;
583 reply_size <<= 2;
584
Vasily Averin1725d712007-07-17 04:04:24 -0700585 rcode = -ENOMEM;
Markus Lidelf6ed39a2006-01-06 00:19:33 -0800586 reply = kzalloc(reply_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!reply) {
588 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
589 c->name);
Vasily Averin1725d712007-07-17 04:04:24 -0700590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
596 if (sg_offset) {
597 struct sg_simple_element *sg;
598
599 if (sg_offset * 4 >= size) {
600 rcode = -EFAULT;
601 goto cleanup;
602 }
603 // TODO 64bit fix
604 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
605 sg_offset);
606 sg_count =
607 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
608 if (sg_count > SG_TABLESIZE) {
609 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
610 c->name, sg_count);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700611 rcode = -EINVAL;
612 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 for (i = 0; i < sg_count; i++) {
616 int sg_size;
617 struct i2o_dma *p;
618
619 if (!(sg[i].flag_count & 0x10000000
620 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
621 printk(KERN_DEBUG
622 "%s:Bad SG element %d - not simple (%x)\n",
623 c->name, i, sg[i].flag_count);
624 rcode = -EINVAL;
625 goto cleanup;
626 }
627 sg_size = sg[i].flag_count & 0xffffff;
Markus Lideldcceafe2006-01-06 00:19:32 -0800628 p = &(sg_list[sg_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Allocate memory for the transfer */
Alan Cox9d793b02008-10-15 22:02:47 -0700630 if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 printk(KERN_DEBUG
632 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
633 c->name, sg_size, i, sg_count);
634 rcode = -ENOMEM;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700635 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Markus Lideldcceafe2006-01-06 00:19:32 -0800637 sg_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Copy in the user's SG buffer if necessary */
639 if (sg[i].
640 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
641 // TODO 64bit fix
642 if (copy_from_user
Markus Lidelf33213e2005-06-23 22:02:23 -0700643 (p->virt,
644 (void __user *)(unsigned long)sg[i].
645 addr_bus, sg_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 printk(KERN_DEBUG
647 "%s: Could not copy SG buf %d FROM user\n",
648 c->name, i);
649 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700650 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
653 //TODO 64bit fix
654 sg[i].addr_bus = (u32) p->phys;
655 }
656 }
657
Markus Lidela1a5ea72006-01-06 00:19:29 -0800658 rcode = i2o_msg_post_wait(c, msg, 60);
Vasily Averin1725d712007-07-17 04:04:24 -0700659 msg = NULL;
Markus Lideldcceafe2006-01-06 00:19:32 -0800660 if (rcode) {
661 reply[4] = ((u32) rcode) << 24;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700662 goto sg_list_cleanup;
Markus Lideldcceafe2006-01-06 00:19:32 -0800663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (sg_offset) {
Vasily Averin1725d712007-07-17 04:04:24 -0700666 u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Copy back the Scatter Gather buffers back to user space */
668 u32 j;
669 // TODO 64bit fix
670 struct sg_simple_element *sg;
671 int sg_size;
672
673 // re-acquire the original message to handle correctly the sg copy operation
Vasily Averin1725d712007-07-17 04:04:24 -0700674 memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 // get user msg size in u32s
676 if (get_user(size, &user_msg[0])) {
677 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700678 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 size = size >> 16;
681 size *= 4;
682 /* Copy in the user's I2O command */
Vasily Averin1725d712007-07-17 04:04:24 -0700683 if (copy_from_user(rmsg, user_msg, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700685 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 sg_count =
688 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
689
690 // TODO 64bit fix
Vasily Averin1725d712007-07-17 04:04:24 -0700691 sg = (struct sg_simple_element *)(rmsg + sg_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 for (j = 0; j < sg_count; j++) {
693 /* Copy out the SG list to user's buffer if necessary */
694 if (!
695 (sg[j].
696 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
697 sg_size = sg[j].flag_count & 0xffffff;
698 // TODO 64bit fix
699 if (copy_to_user
700 ((void __user *)(u64) sg[j].addr_bus,
701 sg_list[j].virt, sg_size)) {
702 printk(KERN_WARNING
703 "%s: Could not copy %p TO user %x\n",
704 c->name, sg_list[j].virt,
705 sg[j].addr_bus);
706 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700707 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 }
710 }
711 }
712
Vasily Averin1725d712007-07-17 04:04:24 -0700713sg_list_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Copy back the reply to user space */
715 if (reply_size) {
716 // we wrote our own values for context - now restore the user supplied ones
717 if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
718 printk(KERN_WARNING
719 "%s: Could not copy message context FROM user\n",
720 c->name);
721 rcode = -EFAULT;
722 }
723 if (copy_to_user(user_reply, reply, reply_size)) {
724 printk(KERN_WARNING
725 "%s: Could not copy reply TO user\n", c->name);
726 rcode = -EFAULT;
727 }
728 }
Markus Lidel61fbfa82005-06-23 22:02:11 -0700729 for (i = 0; i < sg_index; i++)
730 i2o_dma_free(&c->pdev->dev, &sg_list[i]);
731
Vasily Averin1725d712007-07-17 04:04:24 -0700732cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 kfree(reply);
Vasily Averin1725d712007-07-17 04:04:24 -0700734out:
735 if (msg)
736 i2o_msg_nop(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return rcode;
738}
739
Markus Lidelf33213e2005-06-23 22:02:23 -0700740static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd,
741 unsigned long arg)
f4c2c152005-04-10 22:29:42 -0500742{
743 int ret;
Markus Lidelf33213e2005-06-23 22:02:23 -0700744 lock_kernel();
745 switch (cmd) {
f4c2c152005-04-10 22:29:42 -0500746 case I2OGETIOPS:
Alan Cox5d9d6e42010-01-04 16:18:27 +0000747 ret = i2o_cfg_ioctl(file, cmd, arg);
f4c2c152005-04-10 22:29:42 -0500748 break;
749 case I2OPASSTHRU32:
750 ret = i2o_cfg_passthru32(file, cmd, arg);
751 break;
752 default:
753 ret = -ENOIOCTLCMD;
754 break;
755 }
756 unlock_kernel();
757 return ret;
758}
759
760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Randy Dunlapf13a6032006-12-06 20:38:24 -0800762#ifdef CONFIG_I2O_EXT_ADAPTEC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static int i2o_cfg_passthru(unsigned long arg)
764{
765 struct i2o_cmd_passthru __user *cmd =
766 (struct i2o_cmd_passthru __user *)arg;
767 struct i2o_controller *c;
768 u32 __user *user_msg;
769 u32 *reply = NULL;
770 u32 __user *user_reply = NULL;
771 u32 size = 0;
772 u32 reply_size = 0;
773 u32 rcode = 0;
Alan Cox9d793b02008-10-15 22:02:47 -0700774 struct i2o_dma sg_list[SG_TABLESIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 u32 sg_offset = 0;
776 u32 sg_count = 0;
777 int sg_index = 0;
778 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 i2o_status_block *sb;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800780 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 unsigned int iop;
782
783 if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
784 return -EFAULT;
785
786 c = i2o_find_iop(iop);
787 if (!c) {
788 osm_warn("controller %d not found\n", iop);
789 return -ENXIO;
790 }
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 sb = c->status_block.virt;
793
794 if (get_user(size, &user_msg[0]))
795 return -EFAULT;
796 size = size >> 16;
797
798 if (size > sb->inbound_frame_size) {
799 osm_warn("size of message > inbound_frame_size");
800 return -EFAULT;
801 }
802
803 user_reply = &user_msg[size];
804
805 size <<= 2; // Convert to bytes
806
Vasily Averin1725d712007-07-17 04:04:24 -0700807 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
808 if (IS_ERR(msg))
809 return PTR_ERR(msg);
810
811 rcode = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* Copy in the user's I2O command */
813 if (copy_from_user(msg, user_msg, size))
Vasily Averin1725d712007-07-17 04:04:24 -0700814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (get_user(reply_size, &user_reply[0]) < 0)
Vasily Averin1725d712007-07-17 04:04:24 -0700817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 reply_size >>= 16;
820 reply_size <<= 2;
821
Markus Lidelf6ed39a2006-01-06 00:19:33 -0800822 reply = kzalloc(reply_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!reply) {
824 printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
825 c->name);
Vasily Averin1725d712007-07-17 04:04:24 -0700826 rcode = -ENOMEM;
827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 sg_offset = (msg->u.head[0] >> 4) & 0x0f;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
833 if (sg_offset) {
834 struct sg_simple_element *sg;
Alan Cox9d793b02008-10-15 22:02:47 -0700835 struct i2o_dma *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (sg_offset * 4 >= size) {
838 rcode = -EFAULT;
839 goto cleanup;
840 }
841 // TODO 64bit fix
842 sg = (struct sg_simple_element *)((&msg->u.head[0]) +
843 sg_offset);
844 sg_count =
845 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
846 if (sg_count > SG_TABLESIZE) {
847 printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
848 c->name, sg_count);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700849 rcode = -EINVAL;
850 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
853 for (i = 0; i < sg_count; i++) {
854 int sg_size;
855
856 if (!(sg[i].flag_count & 0x10000000
857 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
858 printk(KERN_DEBUG
859 "%s:Bad SG element %d - not simple (%x)\n",
860 c->name, i, sg[i].flag_count);
861 rcode = -EINVAL;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700862 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 sg_size = sg[i].flag_count & 0xffffff;
Alan Cox9d793b02008-10-15 22:02:47 -0700865 p = &(sg_list[sg_index]);
866 if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* Allocate memory for the transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 printk(KERN_DEBUG
869 "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
870 c->name, sg_size, i, sg_count);
871 rcode = -ENOMEM;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700872 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Alan Cox9d793b02008-10-15 22:02:47 -0700874 sg_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* Copy in the user's SG buffer if necessary */
876 if (sg[i].
877 flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
878 // TODO 64bit fix
879 if (copy_from_user
Alan Cox9d793b02008-10-15 22:02:47 -0700880 (p->virt, (void __user *)sg[i].addr_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 sg_size)) {
882 printk(KERN_DEBUG
883 "%s: Could not copy SG buf %d FROM user\n",
884 c->name, i);
885 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700886 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 }
Alan Cox9d793b02008-10-15 22:02:47 -0700889 sg[i].addr_bus = p->phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 }
892
Markus Lidela1a5ea72006-01-06 00:19:29 -0800893 rcode = i2o_msg_post_wait(c, msg, 60);
Vasily Averin1725d712007-07-17 04:04:24 -0700894 msg = NULL;
Markus Lideldcceafe2006-01-06 00:19:32 -0800895 if (rcode) {
896 reply[4] = ((u32) rcode) << 24;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700897 goto sg_list_cleanup;
Markus Lideldcceafe2006-01-06 00:19:32 -0800898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (sg_offset) {
Alan Cox9d793b02008-10-15 22:02:47 -0700901 u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* Copy back the Scatter Gather buffers back to user space */
903 u32 j;
904 // TODO 64bit fix
905 struct sg_simple_element *sg;
906 int sg_size;
907
908 // re-acquire the original message to handle correctly the sg copy operation
Vasily Averin1725d712007-07-17 04:04:24 -0700909 memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 // get user msg size in u32s
911 if (get_user(size, &user_msg[0])) {
912 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700913 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 size = size >> 16;
916 size *= 4;
917 /* Copy in the user's I2O command */
Vasily Averin1725d712007-07-17 04:04:24 -0700918 if (copy_from_user(rmsg, user_msg, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700920 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 sg_count =
923 (size - sg_offset * 4) / sizeof(struct sg_simple_element);
924
925 // TODO 64bit fix
Vasily Averin1725d712007-07-17 04:04:24 -0700926 sg = (struct sg_simple_element *)(rmsg + sg_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 for (j = 0; j < sg_count; j++) {
928 /* Copy out the SG list to user's buffer if necessary */
929 if (!
930 (sg[j].
931 flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
932 sg_size = sg[j].flag_count & 0xffffff;
933 // TODO 64bit fix
934 if (copy_to_user
Alan Cox9d793b02008-10-15 22:02:47 -0700935 ((void __user *)sg[j].addr_bus, sg_list[j].virt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 sg_size)) {
937 printk(KERN_WARNING
938 "%s: Could not copy %p TO user %x\n",
Alan Cox9d793b02008-10-15 22:02:47 -0700939 c->name, sg_list[j].virt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sg[j].addr_bus);
941 rcode = -EFAULT;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700942 goto sg_list_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 }
945 }
946 }
947
Vasily Averin1725d712007-07-17 04:04:24 -0700948sg_list_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* 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 for (i = 0; i < sg_index; i++)
Alan Cox9d793b02008-10-15 22:02:47 -0700966 i2o_dma_free(&c->pdev->dev, &sg_list[i]);
Markus Lidel61fbfa82005-06-23 22:02:11 -0700967
Vasily Averin1725d712007-07-17 04:04:24 -0700968cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 kfree(reply);
Vasily Averin1725d712007-07-17 04:04:24 -0700970out:
971 if (msg)
972 i2o_msg_nop(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return rcode;
974}
Markus Lidelb2aaee32005-06-23 22:02:19 -0700975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977/*
978 * IOCTL Handler
979 */
Alan Cox5d9d6e42010-01-04 16:18:27 +0000980static long i2o_cfg_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 int ret;
983
Alan Cox5d9d6e42010-01-04 16:18:27 +0000984 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 switch (cmd) {
986 case I2OGETIOPS:
987 ret = i2o_cfg_getiops(arg);
988 break;
989
990 case I2OHRTGET:
991 ret = i2o_cfg_gethrt(arg);
992 break;
993
994 case I2OLCTGET:
995 ret = i2o_cfg_getlct(arg);
996 break;
997
998 case I2OPARMSET:
999 ret = i2o_cfg_parms(arg, I2OPARMSET);
1000 break;
1001
1002 case I2OPARMGET:
1003 ret = i2o_cfg_parms(arg, I2OPARMGET);
1004 break;
1005
1006 case I2OSWDL:
1007 ret = i2o_cfg_swdl(arg);
1008 break;
1009
1010 case I2OSWUL:
1011 ret = i2o_cfg_swul(arg);
1012 break;
1013
1014 case I2OSWDEL:
1015 ret = i2o_cfg_swdel(arg);
1016 break;
1017
1018 case I2OVALIDATE:
1019 ret = i2o_cfg_validate(arg);
1020 break;
1021
1022 case I2OEVTREG:
1023 ret = i2o_cfg_evt_reg(arg, fp);
1024 break;
1025
1026 case I2OEVTGET:
1027 ret = i2o_cfg_evt_get(arg, fp);
1028 break;
1029
Markus Lidelb2aaee32005-06-23 22:02:19 -07001030#ifdef CONFIG_I2O_EXT_ADAPTEC
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 case I2OPASSTHRU:
1032 ret = i2o_cfg_passthru(arg);
1033 break;
Markus Lidelb2aaee32005-06-23 22:02:19 -07001034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 default:
1037 osm_debug("unknown ioctl called!\n");
1038 ret = -EINVAL;
1039 }
Alan Cox5d9d6e42010-01-04 16:18:27 +00001040 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return ret;
1042}
1043
1044static int cfg_open(struct inode *inode, struct file *file)
1045{
1046 struct i2o_cfg_info *tmp =
1047 (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
1048 GFP_KERNEL);
1049 unsigned long flags;
1050
1051 if (!tmp)
1052 return -ENOMEM;
1053
Arnd Bergmannb78032a2008-05-20 19:16:14 +02001054 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 file->private_data = (void *)(i2o_cfg_info_id++);
1056 tmp->fp = file;
1057 tmp->fasync = NULL;
1058 tmp->q_id = (ulong) file->private_data;
1059 tmp->q_len = 0;
1060 tmp->q_in = 0;
1061 tmp->q_out = 0;
1062 tmp->q_lost = 0;
1063 tmp->next = open_files;
1064
1065 spin_lock_irqsave(&i2o_config_lock, flags);
1066 open_files = tmp;
1067 spin_unlock_irqrestore(&i2o_config_lock, flags);
Arnd Bergmannb78032a2008-05-20 19:16:14 +02001068 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 return 0;
1071}
1072
1073static int cfg_fasync(int fd, struct file *fp, int on)
1074{
1075 ulong id = (ulong) fp->private_data;
1076 struct i2o_cfg_info *p;
Jonathan Corbet743115e2008-06-19 15:44:57 -06001077 int ret = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Jonathan Corbet743115e2008-06-19 15:44:57 -06001079 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 for (p = open_files; p; p = p->next)
1081 if (p->q_id == id)
1082 break;
1083
Jonathan Corbet743115e2008-06-19 15:44:57 -06001084 if (p)
1085 ret = fasync_helper(fd, fp, on, &p->fasync);
1086 unlock_kernel();
1087 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090static int cfg_release(struct inode *inode, struct file *file)
1091{
1092 ulong id = (ulong) file->private_data;
Al Viro233e70f2008-10-31 23:28:30 +00001093 struct i2o_cfg_info *p, **q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 unsigned long flags;
1095
1096 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_lock_irqsave(&i2o_config_lock, flags);
Al Viro233e70f2008-10-31 23:28:30 +00001098 for (q = &open_files; (p = *q) != NULL; q = &p->next) {
1099 if (p->q_id == id) {
1100 *q = p->next;
1101 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 spin_unlock_irqrestore(&i2o_config_lock, flags);
1106 unlock_kernel();
1107
1108 return 0;
1109}
1110
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001111static const struct file_operations config_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 .owner = THIS_MODULE,
1113 .llseek = no_llseek,
Alan Cox5d9d6e42010-01-04 16:18:27 +00001114 .unlocked_ioctl = i2o_cfg_ioctl,
f4c2c152005-04-10 22:29:42 -05001115#ifdef CONFIG_COMPAT
1116 .compat_ioctl = i2o_cfg_compat_ioctl,
1117#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .open = cfg_open,
1119 .release = cfg_release,
1120 .fasync = cfg_fasync,
1121};
1122
1123static struct miscdevice i2o_miscdev = {
1124 I2O_MINOR,
1125 "i2octl",
1126 &config_fops
1127};
1128
Markus Lidelf10378f2005-06-23 22:02:16 -07001129static int __init i2o_config_old_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 spin_lock_init(&i2o_config_lock);
1132
1133 if (misc_register(&i2o_miscdev) < 0) {
1134 osm_err("can't register device.\n");
1135 return -EBUSY;
1136 }
Markus Lidelf33213e2005-06-23 22:02:23 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return 0;
1139}
1140
Markus Lidelf10378f2005-06-23 22:02:16 -07001141static void i2o_config_old_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 misc_deregister(&i2o_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146MODULE_AUTHOR("Red Hat Software");