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/namei.h>
20 #include <asm/uaccess.h>
22 #define OSM_NAME "config-osm"
23 #define OSM_VERSION "1.248"
24 #define OSM_DESCRIPTION "I2O Configuration OSM"
26 /* access mode user rw */
27 #define S_IWRSR (S_IRUSR | S_IWUSR)
29 static struct i2o_driver i2o_config_driver
;
31 /* Special file operations for sysfs */
32 struct fops_attribute
{
33 struct bin_attribute bin
;
34 struct file_operations fops
;
40 static ssize_t
sysfs_read_dummy(struct kobject
*kobj
, char *buf
, loff_t offset
,
49 static ssize_t
sysfs_write_dummy(struct kobject
*kobj
, char *buf
, loff_t offset
,
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
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
65 * Returns 0 on success or negative error code on failure.
67 static int sysfs_create_fops_file(struct kobject
*kobj
,
68 struct fops_attribute
*attr
)
70 struct file_operations tmp
, *fops
;
78 attr
->bin
.read
= sysfs_read_dummy
;
81 attr
->bin
.write
= sysfs_write_dummy
;
83 if ((rc
= sysfs_create_bin_file(kobj
, &attr
->bin
)))
86 qstr
.name
= attr
->bin
.attr
.name
;
87 qstr
.len
= strlen(qstr
.name
);
88 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
90 if ((d
= lookup_hash(&qstr
, kobj
->dentry
))) {
92 memcpy(&tmp
, d
->d_inode
->i_fop
, sizeof(tmp
));
94 tmp
.read
= fops
->read
;
96 tmp
.write
= fops
->write
;
97 memcpy(fops
, &tmp
, sizeof(tmp
));
100 d
->d_inode
->i_fop
= fops
;
102 sysfs_remove_bin_file(kobj
, &attr
->bin
);
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
112 * Only wrapper arround sysfs_remove_bin_file()
114 * Returns 0 on success or negative error code on failure.
116 static inline int sysfs_remove_fops_file(struct kobject
*kobj
,
117 struct fops_attribute
*attr
)
119 return sysfs_remove_bin_file(kobj
, &attr
->bin
);
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
127 * @count: number of bytes to read
129 * Put @count bytes starting at @off into @buf from the HRT of the I2O
130 * controller corresponding to @kobj.
132 * Returns number of bytes copied into buffer.
134 static ssize_t
i2o_config_read_hrt(struct kobject
*kobj
, char *buf
,
135 loff_t offset
, size_t count
)
137 struct i2o_controller
*c
= kobj_to_i2o_device(kobj
)->iop
;
138 i2o_hrt
*hrt
= c
->hrt
.virt
;
140 u32 size
= (hrt
->num_entries
* hrt
->entry_len
+ 2) * 4;
145 if (offset
+ count
> size
)
146 count
= size
- offset
;
148 memcpy(buf
, (u8
*) hrt
+ offset
, count
);
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
158 * @count: number of bytes to read
160 * Put @count bytes starting at @off into @buf from the LCT of the I2O
161 * controller corresponding to @kobj.
163 * Returns number of bytes copied into buffer.
165 static ssize_t
i2o_config_read_lct(struct kobject
*kobj
, char *buf
,
166 loff_t offset
, size_t count
)
168 struct i2o_controller
*c
= kobj_to_i2o_device(kobj
)->iop
;
169 u32 size
= c
->lct
->table_size
* 4;
174 if (offset
+ count
> size
)
175 count
= size
- offset
;
177 memcpy(buf
, (u8
*) c
->lct
+ offset
, count
);
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); \
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); \
191 static struct fops_attribute i2o_config_attr_##_name = { \
192 .bin = { .attr = { .name = __stringify(_name), .mode = _mode, \
193 .owner = THIS_MODULE }, \
195 .fops = { .write = i2o_config_##_name##_write, \
196 .read = i2o_config_##_name##_read} \
199 #ifdef CONFIG_I2O_EXT_ADAPTEC
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
206 * Converts type and id from I2O spec to the matching region for DPT /
207 * Adaptec controllers.
209 * Returns region which match type and id or -1 on error.
211 static u32
i2o_config_dpt_region(u8 swtype
, u8 swid
)
214 case I2O_SOFTWARE_MODULE_IRTOS
:
216 * content: operation firmware
218 * 0xbc000 for 2554, 3754, 2564, 3757
227 case I2O_SOFTWARE_MODULE_IOP_PRIVATE
:
229 * content: BIOS and SMOR
230 * BIOS size: first 0x8000 bytes
232 * 0x40000 for 2554, 3754, 2564, 3757
233 * 0x80000 for 2865, 3966
240 case I2O_SOFTWARE_MODULE_IOP_CONFIG
:
244 * content: NVRAM defaults
245 * region size: 0x2000 bytes
250 * content: serial number
251 * region size: 0x2000 bytes
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
269 * @swtype: type of software module reading
270 * @swid: id of software which should be read
272 * Transfers @count bytes at offset @offset from IOP into buffer using
273 * type @swtype and id @swid as described in I2O spec.
275 * Returns number of bytes copied into buffer or error code on failure.
277 static ssize_t
i2o_config_sw_read(struct file
*file
, char __user
* buf
,
278 size_t count
, loff_t
* offset
, u8 swtype
,
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
;
290 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
291 if (m
== I2O_QUEUE_EMPTY
)
294 mptr
= &msg
->body
[3];
296 if ((rc
= i2o_dma_alloc(&c
->pdev
->dev
, &buffer
, count
, GFP_KERNEL
))) {
300 #ifdef CONFIG_I2O_EXT_ADAPTEC
302 mptr
= &msg
->body
[4];
303 function
= I2O_CMD_PRIVATE
;
305 writel(TEN_WORD_MSG_SIZE
| SGL_OFFSET_8
, &msg
->u
.head
[0]);
307 writel(I2O_VENDOR_DPT
<< 16 | I2O_DPT_FLASH_READ
,
309 writel(i2o_config_dpt_region(swtype
, swid
), &msg
->body
[1]);
310 writel(*offset
, &msg
->body
[2]);
311 writel(count
, &msg
->body
[3]);
314 writel(NINE_WORD_MSG_SIZE
| SGL_OFFSET_7
, &msg
->u
.head
[0]);
316 writel(0xD0000000 | count
, mptr
++);
317 writel(buffer
.phys
, mptr
);
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]);
323 #ifdef CONFIG_I2O_EXT_ADAPTEC
327 writel((u32
) swtype
<< 16 | (u32
) 1 << 8, &msg
->body
[0]);
328 writel(0, &msg
->body
[1]);
329 writel(swid
, &msg
->body
[2]);
332 status
= i2o_msg_post_wait_mem(c
, m
, 60, &buffer
);
334 if (status
== I2O_POST_WAIT_OK
) {
335 if (!(rc
= copy_to_user(buf
, buffer
.virt
, count
))) {
342 if (status
!= -ETIMEDOUT
)
343 i2o_dma_free(&c
->pdev
->dev
, &buffer
);
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
354 * @swtype: type of software module writing
355 * @swid: id of software which should be written
357 * Transfers @count bytes at offset @offset from buffer to IOP using
358 * type @swtype and id @swid as described in I2O spec.
360 * Returns number of bytes copied from buffer or error code on failure.
362 static ssize_t
i2o_config_sw_write(struct file
*file
, const char __user
* buf
,
363 size_t count
, loff_t
* offset
, u8 swtype
,
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
;
375 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
376 if (m
== I2O_QUEUE_EMPTY
)
379 mptr
= &msg
->body
[3];
381 if ((rc
= i2o_dma_alloc(&c
->pdev
->dev
, &buffer
, count
, GFP_KERNEL
)))
384 if ((rc
= copy_from_user(buffer
.virt
, buf
, count
)))
387 #ifdef CONFIG_I2O_EXT_ADAPTEC
389 mptr
= &msg
->body
[4];
390 function
= I2O_CMD_PRIVATE
;
392 writel(TEN_WORD_MSG_SIZE
| SGL_OFFSET_8
, &msg
->u
.head
[0]);
394 writel(I2O_VENDOR_DPT
<< 16 | I2O_DPT_FLASH_WRITE
,
396 writel(i2o_config_dpt_region(swtype
, swid
), &msg
->body
[1]);
397 writel(*offset
, &msg
->body
[2]);
398 writel(count
, &msg
->body
[3]);
401 writel(NINE_WORD_MSG_SIZE
| SGL_OFFSET_7
, &msg
->u
.head
[0]);
403 writel(0xD4000000 | count
, mptr
++);
404 writel(buffer
.phys
, mptr
);
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]);
410 #ifdef CONFIG_I2O_EXT_ADAPTEC
414 writel((u32
) swtype
<< 16 | (u32
) 1 << 8, &msg
->body
[0]);
415 writel(0, &msg
->body
[1]);
416 writel(swid
, &msg
->body
[2]);
419 status
= i2o_msg_post_wait_mem(c
, m
, 60, &buffer
);
421 if (status
!= -ETIMEDOUT
)
422 i2o_dma_free(&c
->pdev
->dev
, &buffer
);
424 if (status
!= I2O_POST_WAIT_OK
)
432 i2o_dma_free(&c
->pdev
->dev
, &buffer
);
440 /* attribute for HRT in sysfs */
441 static struct bin_attribute i2o_config_hrt_attr
= {
445 .owner
= THIS_MODULE
},
447 .read
= i2o_config_read_hrt
450 /* attribute for LCT in sysfs */
451 static struct bin_attribute i2o_config_lct_attr
= {
455 .owner
= THIS_MODULE
},
457 .read
= i2o_config_read_lct
460 /* IRTOS firmware access */
461 I2O_CONFIG_SW_ATTR(irtos
, S_IWRSR
, I2O_SOFTWARE_MODULE_IRTOS
, 0);
463 #ifdef CONFIG_I2O_EXT_ADAPTEC
466 * attribute for BIOS / SMOR, nvram and serial number access on DPT / Adaptec
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);
476 * i2o_config_notify_controller_add - Notify of added controller
477 * @c: the controller which was added
479 * If a I2O controller is added, we catch the notification to add sysfs
482 static void i2o_config_notify_controller_add(struct i2o_controller
*c
)
484 struct kobject
*kobj
= &c
->exec
->device
.kobj
;
486 sysfs_create_bin_file(kobj
, &i2o_config_hrt_attr
);
487 sysfs_create_bin_file(kobj
, &i2o_config_lct_attr
);
489 sysfs_create_fops_file(kobj
, &i2o_config_attr_irtos
);
490 #ifdef CONFIG_I2O_EXT_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
);
500 * i2o_config_notify_controller_remove - Notify of removed controller
501 * @c: the controller which was removed
503 * If a I2O controller is removed, we catch the notification to remove the
506 static void i2o_config_notify_controller_remove(struct i2o_controller
*c
)
508 struct kobject
*kobj
= &c
->exec
->device
.kobj
;
510 #ifdef CONFIG_I2O_EXT_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
);
517 sysfs_remove_fops_file(kobj
, &i2o_config_attr_irtos
);
519 sysfs_remove_bin_file(kobj
, &i2o_config_lct_attr
);
520 sysfs_remove_bin_file(kobj
, &i2o_config_hrt_attr
);
523 /* Config OSM driver struct */
524 static struct i2o_driver i2o_config_driver
= {
526 .notify_controller_add
= i2o_config_notify_controller_add
,
527 .notify_controller_remove
= i2o_config_notify_controller_remove
530 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
531 #include "i2o_config.c"
535 * i2o_config_init - Configuration OSM initialization function
537 * Registers Configuration OSM in the I2O core and if old ioctl's are
538 * compiled in initialize them.
540 * Returns 0 on success or negative error code on failure.
542 static int __init
i2o_config_init(void)
544 printk(KERN_INFO OSM_DESCRIPTION
" v" OSM_VERSION
"\n");
546 if (i2o_driver_register(&i2o_config_driver
)) {
547 osm_err("handler register failed.\n");
550 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
551 if (i2o_config_old_init())
552 i2o_driver_unregister(&i2o_config_driver
);
559 * i2o_config_exit - Configuration OSM exit function
561 * If old ioctl's are compiled in exit remove them and unregisters
562 * Configuration OSM from I2O core.
564 static void i2o_config_exit(void)
566 #ifdef CONFIG_I2O_CONFIG_OLD_IOCTL
567 i2o_config_old_exit();
570 i2o_driver_unregister(&i2o_config_driver
);
573 MODULE_AUTHOR("Markus Lidel <Markus.Lidel@shadowconnect.com>");
574 MODULE_LICENSE("GPL");
575 MODULE_DESCRIPTION(OSM_DESCRIPTION
);
576 MODULE_VERSION(OSM_VERSION
);
578 module_init(i2o_config_init
);
579 module_exit(i2o_config_exit
);