config-osm.c 14 KB

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