config-osm.c 14 KB

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