rio-sysfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. static DEVICE_ATTR_RO(field);
  28. rio_config_attr(did, "0x%04x\n");
  29. rio_config_attr(vid, "0x%04x\n");
  30. rio_config_attr(device_rev, "0x%08x\n");
  31. rio_config_attr(asm_did, "0x%04x\n");
  32. rio_config_attr(asm_vid, "0x%04x\n");
  33. rio_config_attr(asm_rev, "0x%04x\n");
  34. rio_config_attr(destid, "0x%04x\n");
  35. rio_config_attr(hopcount, "0x%02x\n");
  36. static ssize_t routes_show(struct device *dev, struct device_attribute *attr, char *buf)
  37. {
  38. struct rio_dev *rdev = to_rio_dev(dev);
  39. char *str = buf;
  40. int i;
  41. for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rdev->net->hport->sys_size);
  42. i++) {
  43. if (rdev->rswitch->route_table[i] == RIO_INVALID_ROUTE)
  44. continue;
  45. str +=
  46. sprintf(str, "%04x %02x\n", i,
  47. rdev->rswitch->route_table[i]);
  48. }
  49. return (str - buf);
  50. }
  51. static DEVICE_ATTR_RO(routes);
  52. static ssize_t lprev_show(struct device *dev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. struct rio_dev *rdev = to_rio_dev(dev);
  56. return sprintf(buf, "%s\n",
  57. (rdev->prev) ? rio_name(rdev->prev) : "root");
  58. }
  59. static DEVICE_ATTR_RO(lprev);
  60. static ssize_t lnext_show(struct device *dev,
  61. struct device_attribute *attr, char *buf)
  62. {
  63. struct rio_dev *rdev = to_rio_dev(dev);
  64. char *str = buf;
  65. int i;
  66. if (rdev->pef & RIO_PEF_SWITCH) {
  67. for (i = 0; i < RIO_GET_TOTAL_PORTS(rdev->swpinfo); i++) {
  68. if (rdev->rswitch->nextdev[i])
  69. str += sprintf(str, "%s\n",
  70. rio_name(rdev->rswitch->nextdev[i]));
  71. else
  72. str += sprintf(str, "null\n");
  73. }
  74. }
  75. return str - buf;
  76. }
  77. static DEVICE_ATTR_RO(lnext);
  78. static ssize_t modalias_show(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. struct rio_dev *rdev = to_rio_dev(dev);
  82. return sprintf(buf, "rapidio:v%04Xd%04Xav%04Xad%04X\n",
  83. rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did);
  84. }
  85. static DEVICE_ATTR_RO(modalias);
  86. static struct attribute *rio_dev_attrs[] = {
  87. &dev_attr_did.attr,
  88. &dev_attr_vid.attr,
  89. &dev_attr_device_rev.attr,
  90. &dev_attr_asm_did.attr,
  91. &dev_attr_asm_vid.attr,
  92. &dev_attr_asm_rev.attr,
  93. &dev_attr_lprev.attr,
  94. &dev_attr_destid.attr,
  95. &dev_attr_modalias.attr,
  96. NULL,
  97. };
  98. static const struct attribute_group rio_dev_group = {
  99. .attrs = rio_dev_attrs,
  100. };
  101. const struct attribute_group *rio_dev_groups[] = {
  102. &rio_dev_group,
  103. NULL,
  104. };
  105. static ssize_t
  106. rio_read_config(struct file *filp, struct kobject *kobj,
  107. struct bin_attribute *bin_attr,
  108. char *buf, loff_t off, size_t count)
  109. {
  110. struct rio_dev *dev =
  111. to_rio_dev(container_of(kobj, struct device, kobj));
  112. unsigned int size = 0x100;
  113. loff_t init_off = off;
  114. u8 *data = (u8 *) buf;
  115. /* Several chips lock up trying to read undefined config space */
  116. if (capable(CAP_SYS_ADMIN))
  117. size = RIO_MAINT_SPACE_SZ;
  118. if (off >= size)
  119. return 0;
  120. if (off + count > size) {
  121. size -= off;
  122. count = size;
  123. } else {
  124. size = count;
  125. }
  126. if ((off & 1) && size) {
  127. u8 val;
  128. rio_read_config_8(dev, off, &val);
  129. data[off - init_off] = val;
  130. off++;
  131. size--;
  132. }
  133. if ((off & 3) && size > 2) {
  134. u16 val;
  135. rio_read_config_16(dev, off, &val);
  136. data[off - init_off] = (val >> 8) & 0xff;
  137. data[off - init_off + 1] = val & 0xff;
  138. off += 2;
  139. size -= 2;
  140. }
  141. while (size > 3) {
  142. u32 val;
  143. rio_read_config_32(dev, off, &val);
  144. data[off - init_off] = (val >> 24) & 0xff;
  145. data[off - init_off + 1] = (val >> 16) & 0xff;
  146. data[off - init_off + 2] = (val >> 8) & 0xff;
  147. data[off - init_off + 3] = val & 0xff;
  148. off += 4;
  149. size -= 4;
  150. }
  151. if (size >= 2) {
  152. u16 val;
  153. rio_read_config_16(dev, off, &val);
  154. data[off - init_off] = (val >> 8) & 0xff;
  155. data[off - init_off + 1] = val & 0xff;
  156. off += 2;
  157. size -= 2;
  158. }
  159. if (size > 0) {
  160. u8 val;
  161. rio_read_config_8(dev, off, &val);
  162. data[off - init_off] = val;
  163. off++;
  164. --size;
  165. }
  166. return count;
  167. }
  168. static ssize_t
  169. rio_write_config(struct file *filp, struct kobject *kobj,
  170. struct bin_attribute *bin_attr,
  171. char *buf, loff_t off, size_t count)
  172. {
  173. struct rio_dev *dev =
  174. to_rio_dev(container_of(kobj, struct device, kobj));
  175. unsigned int size = count;
  176. loff_t init_off = off;
  177. u8 *data = (u8 *) buf;
  178. if (off >= RIO_MAINT_SPACE_SZ)
  179. return 0;
  180. if (off + count > RIO_MAINT_SPACE_SZ) {
  181. size = RIO_MAINT_SPACE_SZ - off;
  182. count = size;
  183. }
  184. if ((off & 1) && size) {
  185. rio_write_config_8(dev, off, data[off - init_off]);
  186. off++;
  187. size--;
  188. }
  189. if ((off & 3) && (size > 2)) {
  190. u16 val = data[off - init_off + 1];
  191. val |= (u16) data[off - init_off] << 8;
  192. rio_write_config_16(dev, off, val);
  193. off += 2;
  194. size -= 2;
  195. }
  196. while (size > 3) {
  197. u32 val = data[off - init_off + 3];
  198. val |= (u32) data[off - init_off + 2] << 8;
  199. val |= (u32) data[off - init_off + 1] << 16;
  200. val |= (u32) data[off - init_off] << 24;
  201. rio_write_config_32(dev, off, val);
  202. off += 4;
  203. size -= 4;
  204. }
  205. if (size >= 2) {
  206. u16 val = data[off - init_off + 1];
  207. val |= (u16) data[off - init_off] << 8;
  208. rio_write_config_16(dev, off, val);
  209. off += 2;
  210. size -= 2;
  211. }
  212. if (size) {
  213. rio_write_config_8(dev, off, data[off - init_off]);
  214. off++;
  215. --size;
  216. }
  217. return count;
  218. }
  219. static struct bin_attribute rio_config_attr = {
  220. .attr = {
  221. .name = "config",
  222. .mode = S_IRUGO | S_IWUSR,
  223. },
  224. .size = RIO_MAINT_SPACE_SZ,
  225. .read = rio_read_config,
  226. .write = rio_write_config,
  227. };
  228. /**
  229. * rio_create_sysfs_dev_files - create RIO specific sysfs files
  230. * @rdev: device whose entries should be created
  231. *
  232. * Create files when @rdev is added to sysfs.
  233. */
  234. int rio_create_sysfs_dev_files(struct rio_dev *rdev)
  235. {
  236. int err = 0;
  237. err = device_create_bin_file(&rdev->dev, &rio_config_attr);
  238. if (!err && (rdev->pef & RIO_PEF_SWITCH)) {
  239. err |= device_create_file(&rdev->dev, &dev_attr_routes);
  240. err |= device_create_file(&rdev->dev, &dev_attr_lnext);
  241. err |= device_create_file(&rdev->dev, &dev_attr_hopcount);
  242. }
  243. if (err)
  244. pr_warning("RIO: Failed to create attribute file(s) for %s\n",
  245. rio_name(rdev));
  246. return err;
  247. }
  248. /**
  249. * rio_remove_sysfs_dev_files - cleanup RIO specific sysfs files
  250. * @rdev: device whose entries we should free
  251. *
  252. * Cleanup when @rdev is removed from sysfs.
  253. */
  254. void rio_remove_sysfs_dev_files(struct rio_dev *rdev)
  255. {
  256. device_remove_bin_file(&rdev->dev, &rio_config_attr);
  257. if (rdev->pef & RIO_PEF_SWITCH) {
  258. device_remove_file(&rdev->dev, &dev_attr_routes);
  259. device_remove_file(&rdev->dev, &dev_attr_lnext);
  260. device_remove_file(&rdev->dev, &dev_attr_hopcount);
  261. }
  262. }
  263. static ssize_t bus_scan_store(struct bus_type *bus, const char *buf,
  264. size_t count)
  265. {
  266. long val;
  267. int rc;
  268. if (kstrtol(buf, 0, &val) < 0)
  269. return -EINVAL;
  270. if (val == RIO_MPORT_ANY) {
  271. rc = rio_init_mports();
  272. goto exit;
  273. }
  274. if (val < 0 || val >= RIO_MAX_MPORTS)
  275. return -EINVAL;
  276. rc = rio_mport_scan((int)val);
  277. exit:
  278. if (!rc)
  279. rc = count;
  280. return rc;
  281. }
  282. static BUS_ATTR(scan, (S_IWUSR|S_IWGRP), NULL, bus_scan_store);
  283. static struct attribute *rio_bus_attrs[] = {
  284. &bus_attr_scan.attr,
  285. NULL,
  286. };
  287. static const struct attribute_group rio_bus_group = {
  288. .attrs = rio_bus_attrs,
  289. };
  290. const struct attribute_group *rio_bus_groups[] = {
  291. &rio_bus_group,
  292. NULL,
  293. };
  294. static ssize_t
  295. port_destid_show(struct device *dev, struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct rio_mport *mport = to_rio_mport(dev);
  299. if (mport)
  300. return sprintf(buf, "0x%04x\n", mport->host_deviceid);
  301. else
  302. return -ENODEV;
  303. }
  304. static DEVICE_ATTR_RO(port_destid);
  305. static ssize_t sys_size_show(struct device *dev, struct device_attribute *attr,
  306. char *buf)
  307. {
  308. struct rio_mport *mport = to_rio_mport(dev);
  309. if (mport)
  310. return sprintf(buf, "%u\n", mport->sys_size);
  311. else
  312. return -ENODEV;
  313. }
  314. static DEVICE_ATTR_RO(sys_size);
  315. static struct attribute *rio_mport_attrs[] = {
  316. &dev_attr_port_destid.attr,
  317. &dev_attr_sys_size.attr,
  318. NULL,
  319. };
  320. static const struct attribute_group rio_mport_group = {
  321. .attrs = rio_mport_attrs,
  322. };
  323. const struct attribute_group *rio_mport_groups[] = {
  324. &rio_mport_group,
  325. NULL,
  326. };