rio-sysfs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * RapidIO sysfs attributes and support
  3. *
  4. * Copyright 2005 MontaVista Software, Inc.
  5. * Matt Porter <mporter@kernel.crashing.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/rio.h>
  14. #include <linux/rio_drv.h>
  15. #include <linux/stat.h>
  16. #include <linux/capability.h>
  17. #include "rio.h"
  18. /* Sysfs support */
  19. #define rio_config_attr(field, format_string) \
  20. static ssize_t \
  21. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  22. { \
  23. struct rio_dev *rdev = to_rio_dev(dev); \
  24. \
  25. return sprintf(buf, format_string, rdev->field); \
  26. } \
  27. rio_config_attr(did, "0x%04x\n");
  28. rio_config_attr(vid, "0x%04x\n");
  29. rio_config_attr(device_rev, "0x%08x\n");
  30. rio_config_attr(asm_did, "0x%04x\n");
  31. rio_config_attr(asm_vid, "0x%04x\n");
  32. rio_config_attr(asm_rev, "0x%04x\n");
  33. rio_config_attr(destid, "0x%04x\n");
  34. rio_config_attr(hopcount, "0x%02x\n");
  35. static ssize_t routes_show(struct device *dev, struct device_attribute *attr, char *buf)
  36. {
  37. struct rio_dev *rdev = to_rio_dev(dev);
  38. char *str = buf;
  39. int i;
  40. for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
  41. i++) {
  42. if (rdev->rswitch->route_table[i] == RIO_INVALID_ROUTE)
  43. continue;
  44. str +=
  45. sprintf(str, "%04x %02x\n", i,
  46. rdev->rswitch->route_table[i]);
  47. }
  48. return (str - buf);
  49. }
  50. static ssize_t lprev_show(struct device *dev,
  51. struct device_attribute *attr, char *buf)
  52. {
  53. struct rio_dev *rdev = to_rio_dev(dev);
  54. return sprintf(buf, "%s\n",
  55. (rdev->prev) ? rio_name(rdev->prev) : "root");
  56. }
  57. static ssize_t lnext_show(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct rio_dev *rdev = to_rio_dev(dev);
  61. char *str = buf;
  62. int i;
  63. if (rdev->pef & RIO_PEF_SWITCH) {
  64. for (i = 0; i < RIO_GET_TOTAL_PORTS(rdev->swpinfo); i++) {
  65. if (rdev->rswitch->nextdev[i])
  66. str += sprintf(str, "%s\n",
  67. rio_name(rdev->rswitch->nextdev[i]));
  68. else
  69. str += sprintf(str, "null\n");
  70. }
  71. }
  72. return str - buf;
  73. }
  74. static ssize_t modalias_show(struct device *dev,
  75. struct device_attribute *attr, char *buf)
  76. {
  77. struct rio_dev *rdev = to_rio_dev(dev);
  78. return sprintf(buf, "rapidio:v%04Xd%04Xav%04Xad%04X\n",
  79. rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did);
  80. }
  81. struct device_attribute rio_dev_attrs[] = {
  82. __ATTR_RO(did),
  83. __ATTR_RO(vid),
  84. __ATTR_RO(device_rev),
  85. __ATTR_RO(asm_did),
  86. __ATTR_RO(asm_vid),
  87. __ATTR_RO(asm_rev),
  88. __ATTR_RO(lprev),
  89. __ATTR_RO(destid),
  90. __ATTR_RO(modalias),
  91. __ATTR_NULL,
  92. };
  93. static DEVICE_ATTR(routes, S_IRUGO, routes_show, NULL);
  94. static DEVICE_ATTR(lnext, S_IRUGO, lnext_show, NULL);
  95. static DEVICE_ATTR(hopcount, S_IRUGO, hopcount_show, NULL);
  96. static ssize_t
  97. rio_read_config(struct file *filp, struct kobject *kobj,
  98. struct bin_attribute *bin_attr,
  99. char *buf, loff_t off, size_t count)
  100. {
  101. struct rio_dev *dev =
  102. to_rio_dev(container_of(kobj, struct device, kobj));
  103. unsigned int size = 0x100;
  104. loff_t init_off = off;
  105. u8 *data = (u8 *) buf;
  106. /* Several chips lock up trying to read undefined config space */
  107. if (capable(CAP_SYS_ADMIN))
  108. size = RIO_MAINT_SPACE_SZ;
  109. if (off >= size)
  110. return 0;
  111. if (off + count > size) {
  112. size -= off;
  113. count = size;
  114. } else {
  115. size = count;
  116. }
  117. if ((off & 1) && size) {
  118. u8 val;
  119. rio_read_config_8(dev, off, &val);
  120. data[off - init_off] = val;
  121. off++;
  122. size--;
  123. }
  124. if ((off & 3) && size > 2) {
  125. u16 val;
  126. rio_read_config_16(dev, off, &val);
  127. data[off - init_off] = (val >> 8) & 0xff;
  128. data[off - init_off + 1] = val & 0xff;
  129. off += 2;
  130. size -= 2;
  131. }
  132. while (size > 3) {
  133. u32 val;
  134. rio_read_config_32(dev, off, &val);
  135. data[off - init_off] = (val >> 24) & 0xff;
  136. data[off - init_off + 1] = (val >> 16) & 0xff;
  137. data[off - init_off + 2] = (val >> 8) & 0xff;
  138. data[off - init_off + 3] = val & 0xff;
  139. off += 4;
  140. size -= 4;
  141. }
  142. if (size >= 2) {
  143. u16 val;
  144. rio_read_config_16(dev, off, &val);
  145. data[off - init_off] = (val >> 8) & 0xff;
  146. data[off - init_off + 1] = val & 0xff;
  147. off += 2;
  148. size -= 2;
  149. }
  150. if (size > 0) {
  151. u8 val;
  152. rio_read_config_8(dev, off, &val);
  153. data[off - init_off] = val;
  154. off++;
  155. --size;
  156. }
  157. return count;
  158. }
  159. static ssize_t
  160. rio_write_config(struct file *filp, struct kobject *kobj,
  161. struct bin_attribute *bin_attr,
  162. char *buf, loff_t off, size_t count)
  163. {
  164. struct rio_dev *dev =
  165. to_rio_dev(container_of(kobj, struct device, kobj));
  166. unsigned int size = count;
  167. loff_t init_off = off;
  168. u8 *data = (u8 *) buf;
  169. if (off >= RIO_MAINT_SPACE_SZ)
  170. return 0;
  171. if (off + count > RIO_MAINT_SPACE_SZ) {
  172. size = RIO_MAINT_SPACE_SZ - off;
  173. count = size;
  174. }
  175. if ((off & 1) && size) {
  176. rio_write_config_8(dev, off, data[off - init_off]);
  177. off++;
  178. size--;
  179. }
  180. if ((off & 3) && (size > 2)) {
  181. u16 val = data[off - init_off + 1];
  182. val |= (u16) data[off - init_off] << 8;
  183. rio_write_config_16(dev, off, val);
  184. off += 2;
  185. size -= 2;
  186. }
  187. while (size > 3) {
  188. u32 val = data[off - init_off + 3];
  189. val |= (u32) data[off - init_off + 2] << 8;
  190. val |= (u32) data[off - init_off + 1] << 16;
  191. val |= (u32) data[off - init_off] << 24;
  192. rio_write_config_32(dev, off, val);
  193. off += 4;
  194. size -= 4;
  195. }
  196. if (size >= 2) {
  197. u16 val = data[off - init_off + 1];
  198. val |= (u16) data[off - init_off] << 8;
  199. rio_write_config_16(dev, off, val);
  200. off += 2;
  201. size -= 2;
  202. }
  203. if (size) {
  204. rio_write_config_8(dev, off, data[off - init_off]);
  205. off++;
  206. --size;
  207. }
  208. return count;
  209. }
  210. static struct bin_attribute rio_config_attr = {
  211. .attr = {
  212. .name = "config",
  213. .mode = S_IRUGO | S_IWUSR,
  214. },
  215. .size = RIO_MAINT_SPACE_SZ,
  216. .read = rio_read_config,
  217. .write = rio_write_config,
  218. };
  219. /**
  220. * rio_create_sysfs_dev_files - create RIO specific sysfs files
  221. * @rdev: device whose entries should be created
  222. *
  223. * Create files when @rdev is added to sysfs.
  224. */
  225. int rio_create_sysfs_dev_files(struct rio_dev *rdev)
  226. {
  227. int err = 0;
  228. err = device_create_bin_file(&rdev->dev, &rio_config_attr);
  229. if (!err && (rdev->pef & RIO_PEF_SWITCH)) {
  230. err |= device_create_file(&rdev->dev, &dev_attr_routes);
  231. err |= device_create_file(&rdev->dev, &dev_attr_lnext);
  232. err |= device_create_file(&rdev->dev, &dev_attr_hopcount);
  233. }
  234. if (err)
  235. pr_warning("RIO: Failed to create attribute file(s) for %s\n",
  236. rio_name(rdev));
  237. return err;
  238. }
  239. /**
  240. * rio_remove_sysfs_dev_files - cleanup RIO specific sysfs files
  241. * @rdev: device whose entries we should free
  242. *
  243. * Cleanup when @rdev is removed from sysfs.
  244. */
  245. void rio_remove_sysfs_dev_files(struct rio_dev *rdev)
  246. {
  247. device_remove_bin_file(&rdev->dev, &rio_config_attr);
  248. if (rdev->pef & RIO_PEF_SWITCH) {
  249. device_remove_file(&rdev->dev, &dev_attr_routes);
  250. device_remove_file(&rdev->dev, &dev_attr_lnext);
  251. device_remove_file(&rdev->dev, &dev_attr_hopcount);
  252. }
  253. }
  254. static ssize_t bus_scan_store(struct bus_type *bus, const char *buf,
  255. size_t count)
  256. {
  257. long val;
  258. int rc;
  259. if (kstrtol(buf, 0, &val) < 0)
  260. return -EINVAL;
  261. if (val == RIO_MPORT_ANY) {
  262. rc = rio_init_mports();
  263. goto exit;
  264. }
  265. if (val < 0 || val >= RIO_MAX_MPORTS)
  266. return -EINVAL;
  267. rc = rio_mport_scan((int)val);
  268. exit:
  269. if (!rc)
  270. rc = count;
  271. return rc;
  272. }
  273. struct bus_attribute rio_bus_attrs[] = {
  274. __ATTR(scan, (S_IWUSR|S_IWGRP), NULL, bus_scan_store),
  275. __ATTR_NULL
  276. };