]> err.no Git - linux-2.6/blob - drivers/message/i2o/config-osm.c
Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / message / i2o / config-osm.c
1 /*
2  *      Configuration OSM
3  *
4  *      Copyright (C) 2005      Markus Lidel <Markus.Lidel@shadowconnect.com>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation; either version 2 of the License, or (at your
9  *      option) any later version.
10  *
11  *      Fixes/additions:
12  *              Markus Lidel <Markus.Lidel@shadowconnect.com>
13  *                      initial version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/i2o.h>
18 #include <linux/namei.h>
19
20 #include <asm/uaccess.h>
21
22 #define OSM_NAME        "config-osm"
23 #define OSM_VERSION     "1.248"
24 #define OSM_DESCRIPTION "I2O Configuration OSM"
25
26 /* access mode user rw */
27 #define S_IWRSR (S_IRUSR | S_IWUSR)
28
29 static struct i2o_driver i2o_config_driver;
30
31 /* Special file operations for sysfs */
32 struct fops_attribute {
33         struct bin_attribute bin;
34         struct file_operations fops;
35 };
36
37 /**
38  *      sysfs_read_dummy
39  */
40 static ssize_t sysfs_read_dummy(struct kobject *kobj, char *buf, loff_t offset,
41                                 size_t count)
42 {
43         return 0;
44 };
45
46 /**
47  *      sysfs_write_dummy
48  */
49 static ssize_t sysfs_write_dummy(struct kobject *kobj, char *buf, loff_t offset,
50                                  size_t count)
51 {
52         return 0;
53 };
54
55 /**
56  *      sysfs_create_fops_file - Creates attribute with special file operations
57  *      @kobj: kobject which should contains the attribute
58  *      @attr: attributes which should be used to create file
59  *
60  *      First creates attribute @attr in kobject @kobj. If it is the first time
61  *      this function is called, merge old fops from sysfs with new one and
62  *      write it back. Afterwords the new fops will be set for the created
63  *      attribute.
64  *
65  *      Returns 0 on success or negative error code on failure.
66  */
67 static int sysfs_create_fops_file(struct kobject *kobj,
68                                   struct fops_attribute *attr)
69 {
70         struct file_operations tmp, *fops;
71         struct dentry *d;
72         struct qstr qstr;
73         int rc;
74
75         fops = &attr->fops;
76
77         if (fops->read)
78                 attr->bin.read = sysfs_read_dummy;
79
80         if (fops->write)
81                 attr->bin.write = sysfs_write_dummy;
82
83         if ((rc = sysfs_create_bin_file(kobj, &attr->bin)))
84                 return rc;
85
86         qstr.name = attr->bin.attr.name;
87         qstr.len = strlen(qstr.name);
88         qstr.hash = full_name_hash(qstr.name, qstr.len);
89
90         if ((d = lookup_hash(&qstr, kobj->dentry))) {
91                 if (!fops->owner) {
92                         memcpy(&tmp, d->d_inode->i_fop, sizeof(tmp));
93                         if (fops->read)
94                                 tmp.read = fops->read;
95                         if (fops->write)
96                                 tmp.write = fops->write;
97                         memcpy(fops, &tmp, sizeof(tmp));
98                 }
99
100                 d->d_inode->i_fop = fops;
101         } else
102                 sysfs_remove_bin_file(kobj, &attr->bin);
103
104         return -ENOENT;
105 };
106
107 /**
108  *      sysfs_remove_fops_file - Remove attribute with special file operations
109  *      @kobj: kobject which contains the attribute
110  *      @attr: attributes which are used to create file
111  *
112  *      Only wrapper arround sysfs_remove_bin_file()
113  *
114  *      Returns 0 on success or negative error code on failure.
115  */
116 static inline int sysfs_remove_fops_file(struct kobject *kobj,
117                                          struct fops_attribute *attr)
118 {
119         return sysfs_remove_bin_file(kobj, &attr->bin);
120 };
121
122 /**
123  *      i2o_config_read_hrt - Returns the HRT of the controller
124  *      @kob: kernel object handle
125  *      @buf: buffer into which the HRT should be copied
126  *      @off: file offset
127  *      @count: number of bytes to read
128  *
129  *      Put @count bytes starting at @off into @buf from the HRT of the I2O
130  *      controller corresponding to @kobj.
131  *
132  *      Returns number of bytes copied into buffer.
133  */
134 static ssize_t i2o_config_read_hrt(struct kobject *kobj, char *buf,
135                                    loff_t offset, size_t count)
136 {
137         struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
138         i2o_hrt *hrt = c->hrt.virt;
139
140         u32 size = (hrt->num_entries * hrt->entry_len + 2) * 4;
141
142         if (offset > size)
143                 return 0;
144
145         if (offset + count > size)
146                 count = size - offset;
147
148         memcpy(buf, (u8 *) hrt + offset, count);
149
150         return count;
151 };
152
153 /**
154  *      i2o_config_read_lct - Returns the LCT of the controller
155  *      @kob: kernel object handle
156  *      @buf: buffer into which the LCT should be copied
157  *      @off: file offset
158  *      @count: number of bytes to read
159  *
160  *      Put @count bytes starting at @off into @buf from the LCT of the I2O
161  *      controller corresponding to @kobj.
162  *
163  *      Returns number of bytes copied into buffer.
164  */
165 static ssize_t i2o_config_read_lct(struct kobject *kobj, char *buf,
166                                    loff_t offset, size_t count)
167 {
168         struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
169         u32 size = c->lct->table_size * 4;
170
171         if (offset > size)
172                 return 0;
173
174         if (offset + count > size)
175                 count = size - offset;
176
177         memcpy(buf, (u8 *) c->lct + offset, count);
178
179         return count;
180 };
181
182 #define I2O_CONFIG_SW_ATTR(_name,_mode,_type,_swid) \
183 static ssize_t i2o_config_##_name##_read(struct file *file, char __user *buf, size_t count, loff_t * offset) { \
184         return i2o_config_sw_read(file, buf, count, offset, _type, _swid); \
185 };\
186 \
187 static ssize_t i2o_config_##_name##_write(struct file *file, const char __user *buf, size_t count, loff_t * offset) { \
188         return i2o_config_sw_write(file, buf, count, offset, _type, _swid); \
189 }; \
190 \
191 static struct fops_attribute i2o_config_attr_##_name = { \
192         .bin = { .attr = { .name = __stringify(_name), .mode = _mode, \
193                            .owner = THIS_MODULE }, \
194                  .size = 0, }, \
195         .fops = { .write = i2o_config_##_name##_write, \
196                   .read = i2o_config_##_name##_read} \
197 };
198
199 #ifdef CONFIG_I2O_EXT_ADAPTEC
200
201 /**
202  *      i2o_config_dpt_reagion - Converts type and id to flash region
203  *      @swtype: type of software module reading
204  *      @swid: id of software which should be read
205  *
206  *      Converts type and id from I2O spec to the matching region for DPT /
207  *      Adaptec controllers.
208  *
209  *      Returns region which match type and id or -1 on error.
210  */
211 static u32 i2o_config_dpt_region(u8 swtype, u8 swid)
212 {
213         switch (swtype) {
214         case I2O_SOFTWARE_MODULE_IRTOS:
215                 /*
216                  * content: operation firmware
217                  * region size:
218                  *      0xbc000 for 2554, 3754, 2564, 3757
219                  *      0x170000 for 2865
220                  *      0x17c000 for 3966
221                  */
222                 if (!swid)
223                         return 0;
224
225                 break;
226
227         case I2O_SOFTWARE_MODULE_IOP_PRIVATE:
228                 /*
229                  * content: BIOS and SMOR
230                  * BIOS size: first 0x8000 bytes
231                  * region size:
232                  *      0x40000 for 2554, 3754, 2564, 3757
233                  *      0x80000 for 2865, 3966
234                  */
235                 if (!swid)
236                         return 1;
237
238                 break;
239
240         case I2O_SOFTWARE_MODULE_IOP_CONFIG:
241                 switch (swid) {
242                 case 0:
243                         /*
244                          * content: NVRAM defaults
245                          * region size: 0x2000 bytes
246                          */
247                         return 2;
248                 case 1:
249                         /*
250                          * content: serial number
251                          * region size: 0x2000 bytes
252                          */
253                         return 3;
254                 }
255                 break;
256         }
257
258         return -1;
259 };
260
261 #endif
262
263 /**
264  *      i2o_config_sw_read - Read a software module from controller
265  *      @file: file pointer
266  *      @buf: buffer into which the data should be copied
267  *      @count: number of bytes to read
268  *      @off: file offset
269  *      @swtype: type of software module reading
270  *      @swid: id of software which should be read
271  *
272  *      Transfers @count bytes at offset @offset from IOP into buffer using
273  *      type @swtype and id @swid as described in I2O spec.
274  *
275  *      Returns number of bytes copied into buffer or error code on failure.
276  */
277 static ssize_t i2o_config_sw_read(struct file *file, char __user * buf,
278                                   size_t count, loff_t * offset, u8 swtype,
279                                   u32 swid)
280 {
281         struct sysfs_dirent *sd = file->f_dentry->d_parent->d_fsdata;
282         struct kobject *kobj = sd->s_element;
283         struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
284         u32 m, function = I2O_CMD_SW_UPLOAD;
285         struct i2o_dma buffer;
286         struct i2o_message __iomem *msg;
287         u32 __iomem *mptr;
288         int rc, status;
289
290         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
291         if (m == I2O_QUEUE_EMPTY)
292                 return -EBUSY;
293
294         mptr = &msg->body[3];
295
296         if ((rc = i2o_dma_alloc(&c->pdev->dev, &buffer, count, GFP_KERNEL))) {
297                 i2o_msg_nop(c, m);
298                 return rc;
299         }
300 #ifdef CONFIG_I2O_EXT_ADAPTEC
301         if (c->adaptec) {
302                 mptr = &msg->body[4];
303                 function = I2O_CMD_PRIVATE;
304
305                 writel(TEN_WORD_MSG_SIZE | SGL_OFFSET_8, &msg->u.head[0]);
306
307                 writel(I2O_VENDOR_DPT << 16 | I2O_DPT_FLASH_READ,
308                        &msg->body[0]);
309                 writel(i2o_config_dpt_region(swtype, swid), &msg->body[1]);
310                 writel(*offset, &msg->body[2]);
311                 writel(count, &msg->body[3]);
312         } else
313 #endif
314                 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
315
316         writel(0xD0000000 | count, mptr++);
317         writel(buffer.phys, mptr);
318
319         writel(function << 24 | HOST_TID << 12 | ADAPTER_TID, &msg->u.head[1]);
320         writel(i2o_config_driver.context, &msg->u.head[2]);
321         writel(0, &msg->u.head[3]);
322
323 #ifdef CONFIG_I2O_EXT_ADAPTEC
324         if (!c->adaptec)
325 #endif
326         {
327                 writel((u32) swtype << 16 | (u32) 1 << 8, &msg->body[0]);
328                 writel(0, &msg->body[1]);
329                 writel(swid, &msg->body[2]);
330         }
331
332         status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
333
334         if (status == I2O_POST_WAIT_OK) {
335                 if (!(rc = copy_to_user(buf, buffer.virt, count))) {
336                         rc = count;
337                         *offset += count;
338                 }
339         } else
340                 rc = -EIO;
341
342         if (status != -ETIMEDOUT)
343                 i2o_dma_free(&c->pdev->dev, &buffer);
344
345         return rc;
346 };
347
348 /**
349  *      i2o_config_sw_write - Write a software module to controller
350  *      @file: file pointer
351  *      @buf: buffer into which the data should be copied
352  *      @count: number of bytes to read
353  *      @off: file offset
354  *      @swtype: type of software module writing
355  *      @swid: id of software which should be written
356  *
357  *      Transfers @count bytes at offset @offset from buffer to IOP using
358  *      type @swtype and id @swid as described in I2O spec.
359  *
360  *      Returns number of bytes copied from buffer or error code on failure.
361  */
362 static ssize_t i2o_config_sw_write(struct file *file, const char __user * buf,
363                                    size_t count, loff_t * offset, u8 swtype,
364                                    u32 swid)
365 {
366         struct sysfs_dirent *sd = file->f_dentry->d_parent->d_fsdata;
367         struct kobject *kobj = sd->s_element;
368         struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
369         u32 m, function = I2O_CMD_SW_DOWNLOAD;
370         struct i2o_dma buffer;
371         struct i2o_message __iomem *msg;
372         u32 __iomem *mptr;
373         int rc, status;
374
375         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
376         if (m == I2O_QUEUE_EMPTY)
377                 return -EBUSY;
378
379         mptr = &msg->body[3];
380
381         if ((rc = i2o_dma_alloc(&c->pdev->dev, &buffer, count, GFP_KERNEL)))
382                 goto nop_msg;
383
384         if ((rc = copy_from_user(buffer.virt, buf, count)))
385                 goto free_buffer;
386
387 #ifdef CONFIG_I2O_EXT_ADAPTEC
388         if (c->adaptec) {
389                 mptr = &msg->body[4];
390                 function = I2O_CMD_PRIVATE;
391
392                 writel(TEN_WORD_MSG_SIZE | SGL_OFFSET_8, &msg->u.head[0]);
393
394                 writel(I2O_VENDOR_DPT << 16 | I2O_DPT_FLASH_WRITE,
395                        &msg->body[0]);
396                 writel(i2o_config_dpt_region(swtype, swid), &msg->body[1]);
397                 writel(*offset, &msg->body[2]);
398                 writel(count, &msg->body[3]);
399         } else
400 #endif
401                 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
402
403         writel(0xD4000000 | count, mptr++);
404         writel(buffer.phys, mptr);
405
406         writel(function << 24 | HOST_TID << 12 | ADAPTER_TID, &msg->u.head[1]);
407         writel(i2o_config_driver.context, &msg->u.head[2]);
408         writel(0, &msg->u.head[3]);
409
410 #ifdef CONFIG_I2O_EXT_ADAPTEC
411         if (!c->adaptec)
412 #endif
413         {
414                 writel((u32) swtype << 16 | (u32) 1 << 8, &msg->body[0]);
415                 writel(0, &msg->body[1]);
416                 writel(swid, &msg->body[2]);
417         }
418
419         status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
420
421         if (status != -ETIMEDOUT)
422                 i2o_dma_free(&c->pdev->dev, &buffer);
423
424         if (status != I2O_POST_WAIT_OK)
425                 return -EIO;
426
427         *offset += count;
428
429         return count;
430
431       free_buffer:
432         i2o_dma_free(&c->pdev->dev, &buffer);
433
434       nop_msg:
435         i2o_msg_nop(c, m);
436
437         return rc;
438 };
439
440 /* attribute for HRT in sysfs */
441 static struct bin_attribute i2o_config_hrt_attr = {
442         .attr = {
443                  .name = "hrt",
444                  .mode = S_IRUGO,
445                  .owner = THIS_MODULE},
446         .size = 0,
447         .read = i2o_config_read_hrt
448 };
449
450 /* attribute for LCT in sysfs */
451 static struct bin_attribute i2o_config_lct_attr = {
452         .attr = {
453                  .name = "lct",
454                  .mode = S_IRUGO,
455                  .owner = THIS_MODULE},
456         .size = 0,
457         .read = i2o_config_read_lct
458 };
459
460 /* IRTOS firmware access */
461 I2O_CONFIG_SW_ATTR(irtos, S_IWRSR, I2O_SOFTWARE_MODULE_IRTOS, 0);
462
463 #ifdef CONFIG_I2O_EXT_ADAPTEC
464
465 /*
466  * attribute for BIOS / SMOR, nvram and serial number access on DPT / Adaptec
467  * controllers
468  */
469 I2O_CONFIG_SW_ATTR(bios, S_IWRSR, I2O_SOFTWARE_MODULE_IOP_PRIVATE, 0);
470 I2O_CONFIG_SW_ATTR(nvram, S_IWRSR, I2O_SOFTWARE_MODULE_IOP_CONFIG, 0);
471 I2O_CONFIG_SW_ATTR(serial, S_IWRSR, I2O_SOFTWARE_MODULE_IOP_CONFIG, 1);
472
473 #endif
474
475 /**
476  *      i2o_config_notify_controller_add - Notify of added controller
477  *      @c: the controller which was added
478  *
479  *      If a I2O controller is added, we catch the notification to add sysfs
480  *      entries.
481  */
482 static void i2o_config_notify_controller_add(struct i2o_controller *c)
483 {
484         struct kobject *kobj = &c->exec->device.kobj;
485
486         sysfs_create_bin_file(kobj, &i2o_config_hrt_attr);
487         sysfs_create_bin_file(kobj, &i2o_config_lct_attr);
488
489         sysfs_create_fops_file(kobj, &i2o_config_attr_irtos);
490 #ifdef CONFIG_I2O_EXT_ADAPTEC
491         if (c->adaptec) {
492                 sysfs_create_fops_file(kobj, &i2o_config_attr_bios);
493                 sysfs_create_fops_file(kobj, &i2o_config_attr_nvram);
494                 sysfs_create_fops_file(kobj, &i2o_config_attr_serial);
495         }
496 #endif
497 };
498
499 /**
500  *      i2o_config_notify_controller_remove - Notify of removed controller
501  *      @c: the controller which was removed
502  *
503  *      If a I2O controller is removed, we catch the notification to remove the
504  *      sysfs entries.
505  */
506 static void i2o_config_notify_controller_remove(struct i2o_controller *c)
507 {
508         struct kobject *kobj = &c->exec->device.kobj;
509
510 #ifdef CONFIG_I2O_EXT_ADAPTEC
511         if (c->adaptec) {
512                 sysfs_remove_fops_file(kobj, &i2o_config_attr_serial);
513                 sysfs_remove_fops_file(kobj, &i2o_config_attr_nvram);
514                 sysfs_remove_fops_file(kobj, &i2o_config_attr_bios);
515         }
516 #endif
517         sysfs_remove_fops_file(kobj, &i2o_config_attr_irtos);
518
519         sysfs_remove_bin_file(kobj, &i2o_config_lct_attr);
520         sysfs_remove_bin_file(kobj, &i2o_config_hrt_attr);
521 };
522
523 /* Config OSM driver struct */
524 static struct i2o_driver i2o_config_driver = {
525         .name = OSM_NAME,
526         .notify_controller_add = i2o_config_notify_controller_add,
527         .notify_controller_remove = i2o_config_notify_controller_remove
528 };
529
530 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
531 #include "i2o_config.c"
532 #endif
533
534 /**
535  *      i2o_config_init - Configuration OSM initialization function
536  *
537  *      Registers Configuration OSM in the I2O core and if old ioctl's are
538  *      compiled in initialize them.
539  *
540  *      Returns 0 on success or negative error code on failure.
541  */
542 static int __init i2o_config_init(void)
543 {
544         printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
545
546         if (i2o_driver_register(&i2o_config_driver)) {
547                 osm_err("handler register failed.\n");
548                 return -EBUSY;
549         }
550 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
551         if (i2o_config_old_init())
552                 i2o_driver_unregister(&i2o_config_driver);
553 #endif
554
555         return 0;
556 }
557
558 /**
559  *      i2o_config_exit - Configuration OSM exit function
560  *
561  *      If old ioctl's are compiled in exit remove them and unregisters
562  *      Configuration OSM from I2O core.
563  */
564 static void i2o_config_exit(void)
565 {
566 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
567         i2o_config_old_exit();
568 #endif
569
570         i2o_driver_unregister(&i2o_config_driver);
571 }
572
573 MODULE_AUTHOR("Markus Lidel <Markus.Lidel@shadowconnect.com>");
574 MODULE_LICENSE("GPL");
575 MODULE_DESCRIPTION(OSM_DESCRIPTION);
576 MODULE_VERSION(OSM_VERSION);
577
578 module_init(i2o_config_init);
579 module_exit(i2o_config_exit);