4 * Copyright (C) 2005 Markus Lidel <Markus.Lidel@shadowconnect.com>
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.
12 * Markus Lidel <Markus.Lidel@shadowconnect.com>
16 #include <linux/module.h>
17 #include <linux/i2o.h>
18 #include <linux/dcache.h>
19 #include <linux/namei.h>
22 #include <asm/uaccess.h>
24 #define OSM_NAME "config-osm"
25 #define OSM_VERSION "1.248"
26 #define OSM_DESCRIPTION "I2O Configuration OSM"
28 /* access mode user rw */
29 #define S_IWRSR (S_IRUSR | S_IWUSR)
31 static struct i2o_driver i2o_config_driver;
33 /* Special file operations for sysfs */
34 struct fops_attribute {
35 struct bin_attribute bin;
36 struct file_operations fops;
42 static ssize_t sysfs_read_dummy(struct kobject *kobj, char *buf, loff_t offset,
51 static ssize_t sysfs_write_dummy(struct kobject *kobj, char *buf, loff_t offset,
58 * sysfs_create_fops_file - Creates attribute with special file operations
59 * @kobj: kobject which should contains the attribute
60 * @attr: attributes which should be used to create file
62 * First creates attribute @attr in kobject @kobj. If it is the first time
63 * this function is called, merge old fops from sysfs with new one and
64 * write it back. Afterwords the new fops will be set for the created
67 * Returns 0 on success or negative error code on failure.
69 static int sysfs_create_fops_file(struct kobject *kobj,
70 struct fops_attribute *attr)
72 struct file_operations tmp, *fops;
80 attr->bin.read = sysfs_read_dummy;
83 attr->bin.write = sysfs_write_dummy;
85 if ((rc = sysfs_create_bin_file(kobj, &attr->bin)))
88 qstr.name = attr->bin.attr.name;
89 qstr.len = strlen(qstr.name);
90 qstr.hash = full_name_hash(qstr.name, qstr.len);
92 if ((d = lookup_hash(&qstr, kobj->dentry))) {
94 memcpy(&tmp, d->d_inode->i_fop, sizeof(tmp));
96 tmp.read = fops->read;
98 tmp.write = fops->write;
99 memcpy(fops, &tmp, sizeof(tmp));
102 d->d_inode->i_fop = fops;
104 sysfs_remove_bin_file(kobj, &attr->bin);
110 * sysfs_remove_fops_file - Remove attribute with special file operations
111 * @kobj: kobject which contains the attribute
112 * @attr: attributes which are used to create file
114 * Only wrapper arround sysfs_remove_bin_file()
116 * Returns 0 on success or negative error code on failure.
118 static inline int sysfs_remove_fops_file(struct kobject *kobj,
119 struct fops_attribute *attr)
121 return sysfs_remove_bin_file(kobj, &attr->bin);
125 * i2o_config_read_hrt - Returns the HRT of the controller
126 * @kob: kernel object handle
127 * @buf: buffer into which the HRT should be copied
129 * @count: number of bytes to read
131 * Put @count bytes starting at @off into @buf from the HRT of the I2O
132 * controller corresponding to @kobj.
134 * Returns number of bytes copied into buffer.
136 static ssize_t i2o_config_read_hrt(struct kobject *kobj, char *buf,
137 loff_t offset, size_t count)
139 struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
140 i2o_hrt *hrt = c->hrt.virt;
142 u32 size = (hrt->num_entries * hrt->entry_len + 2) * 4;
147 if (offset + count > size)
148 count = size - offset;
150 memcpy(buf, (u8 *) hrt + offset, count);
156 * i2o_config_read_lct - Returns the LCT of the controller
157 * @kob: kernel object handle
158 * @buf: buffer into which the LCT should be copied
160 * @count: number of bytes to read
162 * Put @count bytes starting at @off into @buf from the LCT of the I2O
163 * controller corresponding to @kobj.
165 * Returns number of bytes copied into buffer.
167 static ssize_t i2o_config_read_lct(struct kobject *kobj, char *buf,
168 loff_t offset, size_t count)
170 struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
171 u32 size = c->lct->table_size * 4;
176 if (offset + count > size)
177 count = size - offset;
179 memcpy(buf, (u8 *) c->lct + offset, count);
184 #define I2O_CONFIG_SW_ATTR(_name,_mode,_type,_swid) \
185 static ssize_t i2o_config_##_name##_read(struct file *file, char __user *buf, size_t count, loff_t * offset) { \
186 return i2o_config_sw_read(file, buf, count, offset, _type, _swid); \
189 static ssize_t i2o_config_##_name##_write(struct file *file, const char __user *buf, size_t count, loff_t * offset) { \
190 return i2o_config_sw_write(file, buf, count, offset, _type, _swid); \
193 static struct fops_attribute i2o_config_attr_##_name = { \
194 .bin = { .attr = { .name = __stringify(_name), .mode = _mode, \
195 .owner = THIS_MODULE }, \
197 .fops = { .write = i2o_config_##_name##_write, \
198 .read = i2o_config_##_name##_read} \
201 #ifdef CONFIG_I2O_EXT_ADAPTEC
204 * i2o_config_dpt_reagion - Converts type and id to flash region
205 * @swtype: type of software module reading
206 * @swid: id of software which should be read
208 * Converts type and id from I2O spec to the matching region for DPT /
209 * Adaptec controllers.
211 * Returns region which match type and id or -1 on error.
213 static u32 i2o_config_dpt_region(u8 swtype, u8 swid)
216 case I2O_SOFTWARE_MODULE_IRTOS:
218 * content: operation firmware
220 * 0xbc000 for 2554, 3754, 2564, 3757
229 case I2O_SOFTWARE_MODULE_IOP_PRIVATE:
231 * content: BIOS and SMOR
232 * BIOS size: first 0x8000 bytes
234 * 0x40000 for 2554, 3754, 2564, 3757
235 * 0x80000 for 2865, 3966
242 case I2O_SOFTWARE_MODULE_IOP_CONFIG:
246 * content: NVRAM defaults
247 * region size: 0x2000 bytes
252 * content: serial number
253 * region size: 0x2000 bytes
266 * i2o_config_sw_read - Read a software module from controller
267 * @file: file pointer
268 * @buf: buffer into which the data should be copied
269 * @count: number of bytes to read
271 * @swtype: type of software module reading
272 * @swid: id of software which should be read
274 * Transfers @count bytes at offset @offset from IOP into buffer using
275 * type @swtype and id @swid as described in I2O spec.
277 * Returns number of bytes copied into buffer or error code on failure.
279 static ssize_t i2o_config_sw_read(struct file *file, char __user * buf,
280 size_t count, loff_t * offset, u8 swtype,
283 struct sysfs_dirent *sd = file->f_dentry->d_parent->d_fsdata;
284 struct kobject *kobj = sd->s_element;
285 struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
286 u32 m, function = I2O_CMD_SW_UPLOAD;
287 struct i2o_dma buffer;
288 struct i2o_message __iomem *msg;
292 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
293 if (m == I2O_QUEUE_EMPTY)
296 mptr = &msg->body[3];
298 if ((rc = i2o_dma_alloc(&c->pdev->dev, &buffer, count, GFP_KERNEL))) {
302 #ifdef CONFIG_I2O_EXT_ADAPTEC
304 mptr = &msg->body[4];
305 function = I2O_CMD_PRIVATE;
307 writel(TEN_WORD_MSG_SIZE | SGL_OFFSET_8, &msg->u.head[0]);
309 writel(I2O_VENDOR_DPT << 16 | I2O_DPT_FLASH_READ,
311 writel(i2o_config_dpt_region(swtype, swid), &msg->body[1]);
312 writel(*offset, &msg->body[2]);
313 writel(count, &msg->body[3]);
316 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
318 writel(0xD0000000 | count, mptr++);
319 writel(buffer.phys, mptr);
321 writel(function << 24 | HOST_TID << 12 | ADAPTER_TID, &msg->u.head[1]);
322 writel(i2o_config_driver.context, &msg->u.head[2]);
323 writel(0, &msg->u.head[3]);
325 #ifdef CONFIG_I2O_EXT_ADAPTEC
329 writel((u32) swtype << 16 | (u32) 1 << 8, &msg->body[0]);
330 writel(0, &msg->body[1]);
331 writel(swid, &msg->body[2]);
334 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
336 if (status == I2O_POST_WAIT_OK) {
337 if (!(rc = copy_to_user(buf, buffer.virt, count))) {
344 if (status != -ETIMEDOUT)
345 i2o_dma_free(&c->pdev->dev, &buffer);
351 * i2o_config_sw_write - Write a software module to controller
352 * @file: file pointer
353 * @buf: buffer into which the data should be copied
354 * @count: number of bytes to read
356 * @swtype: type of software module writing
357 * @swid: id of software which should be written
359 * Transfers @count bytes at offset @offset from buffer to IOP using
360 * type @swtype and id @swid as described in I2O spec.
362 * Returns number of bytes copied from buffer or error code on failure.
364 static ssize_t i2o_config_sw_write(struct file *file, const char __user * buf,
365 size_t count, loff_t * offset, u8 swtype,
368 struct sysfs_dirent *sd = file->f_dentry->d_parent->d_fsdata;
369 struct kobject *kobj = sd->s_element;
370 struct i2o_controller *c = kobj_to_i2o_device(kobj)->iop;
371 u32 m, function = I2O_CMD_SW_DOWNLOAD;
372 struct i2o_dma buffer;
373 struct i2o_message __iomem *msg;
377 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
378 if (m == I2O_QUEUE_EMPTY)
381 mptr = &msg->body[3];
383 if ((rc = i2o_dma_alloc(&c->pdev->dev, &buffer, count, GFP_KERNEL)))
386 if ((rc = copy_from_user(buffer.virt, buf, count)))
389 #ifdef CONFIG_I2O_EXT_ADAPTEC
391 mptr = &msg->body[4];
392 function = I2O_CMD_PRIVATE;
394 writel(TEN_WORD_MSG_SIZE | SGL_OFFSET_8, &msg->u.head[0]);
396 writel(I2O_VENDOR_DPT << 16 | I2O_DPT_FLASH_WRITE,
398 writel(i2o_config_dpt_region(swtype, swid), &msg->body[1]);
399 writel(*offset, &msg->body[2]);
400 writel(count, &msg->body[3]);
403 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_7, &msg->u.head[0]);
405 writel(0xD4000000 | count, mptr++);
406 writel(buffer.phys, mptr);
408 writel(function << 24 | HOST_TID << 12 | ADAPTER_TID, &msg->u.head[1]);
409 writel(i2o_config_driver.context, &msg->u.head[2]);
410 writel(0, &msg->u.head[3]);
412 #ifdef CONFIG_I2O_EXT_ADAPTEC
416 writel((u32) swtype << 16 | (u32) 1 << 8, &msg->body[0]);
417 writel(0, &msg->body[1]);
418 writel(swid, &msg->body[2]);
421 status = i2o_msg_post_wait_mem(c, m, 60, &buffer);
423 if (status != -ETIMEDOUT)
424 i2o_dma_free(&c->pdev->dev, &buffer);
426 if (status != I2O_POST_WAIT_OK)
434 i2o_dma_free(&c->pdev->dev, &buffer);
442 /* attribute for HRT in sysfs */
443 static struct bin_attribute i2o_config_hrt_attr = {
447 .owner = THIS_MODULE},
449 .read = i2o_config_read_hrt
452 /* attribute for LCT in sysfs */
453 static struct bin_attribute i2o_config_lct_attr = {
457 .owner = THIS_MODULE},
459 .read = i2o_config_read_lct
462 /* IRTOS firmware access */
463 I2O_CONFIG_SW_ATTR(irtos, S_IWRSR, I2O_SOFTWARE_MODULE_IRTOS, 0);
465 #ifdef CONFIG_I2O_EXT_ADAPTEC
468 * attribute for BIOS / SMOR, nvram and serial number access on DPT / Adaptec
471 I2O_CONFIG_SW_ATTR(bios, S_IWRSR, I2O_SOFTWARE_MODULE_IOP_PRIVATE, 0);
472 I2O_CONFIG_SW_ATTR(nvram, S_IWRSR, I2O_SOFTWARE_MODULE_IOP_CONFIG, 0);
473 I2O_CONFIG_SW_ATTR(serial, S_IWRSR, I2O_SOFTWARE_MODULE_IOP_CONFIG, 1);
478 * i2o_config_notify_controller_add - Notify of added controller
479 * @c: the controller which was added
481 * If a I2O controller is added, we catch the notification to add sysfs
484 static void i2o_config_notify_controller_add(struct i2o_controller *c)
486 struct kobject *kobj = &c->exec->device.kobj;
488 sysfs_create_bin_file(kobj, &i2o_config_hrt_attr);
489 sysfs_create_bin_file(kobj, &i2o_config_lct_attr);
491 sysfs_create_fops_file(kobj, &i2o_config_attr_irtos);
492 #ifdef CONFIG_I2O_EXT_ADAPTEC
494 sysfs_create_fops_file(kobj, &i2o_config_attr_bios);
495 sysfs_create_fops_file(kobj, &i2o_config_attr_nvram);
496 sysfs_create_fops_file(kobj, &i2o_config_attr_serial);
502 * i2o_config_notify_controller_remove - Notify of removed controller
503 * @c: the controller which was removed
505 * If a I2O controller is removed, we catch the notification to remove the
508 static void i2o_config_notify_controller_remove(struct i2o_controller *c)
510 struct kobject *kobj = &c->exec->device.kobj;
512 #ifdef CONFIG_I2O_EXT_ADAPTEC
514 sysfs_remove_fops_file(kobj, &i2o_config_attr_serial);
515 sysfs_remove_fops_file(kobj, &i2o_config_attr_nvram);
516 sysfs_remove_fops_file(kobj, &i2o_config_attr_bios);
519 sysfs_remove_fops_file(kobj, &i2o_config_attr_irtos);
521 sysfs_remove_bin_file(kobj, &i2o_config_lct_attr);
522 sysfs_remove_bin_file(kobj, &i2o_config_hrt_attr);
525 /* Config OSM driver struct */
526 static struct i2o_driver i2o_config_driver = {
528 .notify_controller_add = i2o_config_notify_controller_add,
529 .notify_controller_remove = i2o_config_notify_controller_remove
532 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
533 #include "i2o_config.c"
537 * i2o_config_init - Configuration OSM initialization function
539 * Registers Configuration OSM in the I2O core and if old ioctl's are
540 * compiled in initialize them.
542 * Returns 0 on success or negative error code on failure.
544 static int __init i2o_config_init(void)
546 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
548 if (i2o_driver_register(&i2o_config_driver)) {
549 osm_err("handler register failed.\n");
552 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
553 if (i2o_config_old_init())
554 i2o_driver_unregister(&i2o_config_driver);
561 * i2o_config_exit - Configuration OSM exit function
563 * If old ioctl's are compiled in exit remove them and unregisters
564 * Configuration OSM from I2O core.
566 static void i2o_config_exit(void)
568 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
569 i2o_config_old_exit();
572 i2o_driver_unregister(&i2o_config_driver);
575 MODULE_AUTHOR("Markus Lidel <Markus.Lidel@shadowconnect.com>");
576 MODULE_LICENSE("GPL");
577 MODULE_DESCRIPTION(OSM_DESCRIPTION);
578 MODULE_VERSION(OSM_VERSION);
580 module_init(i2o_config_init);
581 module_exit(i2o_config_exit);