rio-sysfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 = to_rio_dev(kobj_to_dev(kobj));
  111. unsigned int size = 0x100;
  112. loff_t init_off = off;
  113. u8 *data = (u8 *) buf;
  114. /* Several chips lock up trying to read undefined config space */
  115. if (capable(CAP_SYS_ADMIN))
  116. size = RIO_MAINT_SPACE_SZ;
  117. if (off >= size)
  118. return 0;
  119. if (off + count > size) {
  120. size -= off;
  121. count = size;
  122. } else {
  123. size = count;
  124. }
  125. if ((off & 1) && size) {
  126. u8 val;
  127. rio_read_config_8(dev, off, &val);
  128. data[off - init_off] = val;
  129. off++;
  130. size--;
  131. }
  132. if ((off & 3) && size > 2) {
  133. u16 val;
  134. rio_read_config_16(dev, off, &val);
  135. data[off - init_off] = (val >> 8) & 0xff;
  136. data[off - init_off + 1] = val & 0xff;
  137. off += 2;
  138. size -= 2;
  139. }
  140. while (size > 3) {
  141. u32 val;
  142. rio_read_config_32(dev, off, &val);
  143. data[off - init_off] = (val >> 24) & 0xff;
  144. data[off - init_off + 1] = (val >> 16) & 0xff;
  145. data[off - init_off + 2] = (val >> 8) & 0xff;
  146. data[off - init_off + 3] = val & 0xff;
  147. off += 4;
  148. size -= 4;
  149. }
  150. if (size >= 2) {
  151. u16 val;
  152. rio_read_config_16(dev, off, &val);
  153. data[off - init_off] = (val >> 8) & 0xff;
  154. data[off - init_off + 1] = val & 0xff;
  155. off += 2;
  156. size -= 2;
  157. }
  158. if (size > 0) {
  159. u8 val;
  160. rio_read_config_8(dev, off, &val);
  161. data[off - init_off] = val;
  162. off++;
  163. --size;
  164. }
  165. return count;
  166. }
  167. static ssize_t
  168. rio_write_config(struct file *filp, struct kobject *kobj,
  169. struct bin_attribute *bin_attr,
  170. char *buf, loff_t off, size_t count)
  171. {
  172. struct rio_dev *dev = to_rio_dev(kobj_to_dev(kobj));
  173. unsigned int size = count;
  174. loff_t init_off = off;
  175. u8 *data = (u8 *) buf;
  176. if (off >= RIO_MAINT_SPACE_SZ)
  177. return 0;
  178. if (off + count > RIO_MAINT_SPACE_SZ) {
  179. size = RIO_MAINT_SPACE_SZ - off;
  180. count = size;
  181. }
  182. if ((off & 1) && size) {
  183. rio_write_config_8(dev, off, data[off - init_off]);
  184. off++;
  185. size--;
  186. }
  187. if ((off & 3) && (size > 2)) {
  188. u16 val = data[off - init_off + 1];
  189. val |= (u16) data[off - init_off] << 8;
  190. rio_write_config_16(dev, off, val);
  191. off += 2;
  192. size -= 2;
  193. }
  194. while (size > 3) {
  195. u32 val = data[off - init_off + 3];
  196. val |= (u32) data[off - init_off + 2] << 8;
  197. val |= (u32) data[off - init_off + 1] << 16;
  198. val |= (u32) data[off - init_off] << 24;
  199. rio_write_config_32(dev, off, val);
  200. off += 4;
  201. size -= 4;
  202. }
  203. if (size >= 2) {
  204. u16 val = data[off - init_off + 1];
  205. val |= (u16) data[off - init_off] << 8;
  206. rio_write_config_16(dev, off, val);
  207. off += 2;
  208. size -= 2;
  209. }
  210. if (size) {
  211. rio_write_config_8(dev, off, data[off - init_off]);
  212. off++;
  213. --size;
  214. }
  215. return count;
  216. }
  217. static struct bin_attribute rio_config_attr = {
  218. .attr = {
  219. .name = "config",
  220. .mode = S_IRUGO | S_IWUSR,
  221. },
  222. .size = RIO_MAINT_SPACE_SZ,
  223. .read = rio_read_config,
  224. .write = rio_write_config,
  225. };
  226. /**
  227. * rio_create_sysfs_dev_files - create RIO specific sysfs files
  228. * @rdev: device whose entries should be created
  229. *
  230. * Create files when @rdev is added to sysfs.
  231. */
  232. int rio_create_sysfs_dev_files(struct rio_dev *rdev)
  233. {
  234. int err = 0;
  235. err = device_create_bin_file(&rdev->dev, &rio_config_attr);
  236. if (!err && (rdev->pef & RIO_PEF_SWITCH)) {
  237. err |= device_create_file(&rdev->dev, &dev_attr_routes);
  238. err |= device_create_file(&rdev->dev, &dev_attr_lnext);
  239. err |= device_create_file(&rdev->dev, &dev_attr_hopcount);
  240. }
  241. if (err)
  242. pr_warning("RIO: Failed to create attribute file(s) for %s\n",
  243. rio_name(rdev));
  244. return err;
  245. }
  246. /**
  247. * rio_remove_sysfs_dev_files - cleanup RIO specific sysfs files
  248. * @rdev: device whose entries we should free
  249. *
  250. * Cleanup when @rdev is removed from sysfs.
  251. */
  252. void rio_remove_sysfs_dev_files(struct rio_dev *rdev)
  253. {
  254. device_remove_bin_file(&rdev->dev, &rio_config_attr);
  255. if (rdev->pef & RIO_PEF_SWITCH) {
  256. device_remove_file(&rdev->dev, &dev_attr_routes);
  257. device_remove_file(&rdev->dev, &dev_attr_lnext);
  258. device_remove_file(&rdev->dev, &dev_attr_hopcount);
  259. }
  260. }
  261. static ssize_t bus_scan_store(struct bus_type *bus, const char *buf,
  262. size_t count)
  263. {
  264. long val;
  265. int rc;
  266. if (kstrtol(buf, 0, &val) < 0)
  267. return -EINVAL;
  268. if (val == RIO_MPORT_ANY) {
  269. rc = rio_init_mports();
  270. goto exit;
  271. }
  272. if (val < 0 || val >= RIO_MAX_MPORTS)
  273. return -EINVAL;
  274. rc = rio_mport_scan((int)val);
  275. exit:
  276. if (!rc)
  277. rc = count;
  278. return rc;
  279. }
  280. static BUS_ATTR(scan, (S_IWUSR|S_IWGRP), NULL, bus_scan_store);
  281. static struct attribute *rio_bus_attrs[] = {
  282. &bus_attr_scan.attr,
  283. NULL,
  284. };
  285. static const struct attribute_group rio_bus_group = {
  286. .attrs = rio_bus_attrs,
  287. };
  288. const struct attribute_group *rio_bus_groups[] = {
  289. &rio_bus_group,
  290. NULL,
  291. };
  292. static ssize_t
  293. port_destid_show(struct device *dev, struct device_attribute *attr,
  294. char *buf)
  295. {
  296. struct rio_mport *mport = to_rio_mport(dev);
  297. if (mport)
  298. return sprintf(buf, "0x%04x\n", mport->host_deviceid);
  299. else
  300. return -ENODEV;
  301. }
  302. static DEVICE_ATTR_RO(port_destid);
  303. static ssize_t sys_size_show(struct device *dev, struct device_attribute *attr,
  304. char *buf)
  305. {
  306. struct rio_mport *mport = to_rio_mport(dev);
  307. if (mport)
  308. return sprintf(buf, "%u\n", mport->sys_size);
  309. else
  310. return -ENODEV;
  311. }
  312. static DEVICE_ATTR_RO(sys_size);
  313. static struct attribute *rio_mport_attrs[] = {
  314. &dev_attr_port_destid.attr,
  315. &dev_attr_sys_size.attr,
  316. NULL,
  317. };
  318. static const struct attribute_group rio_mport_group = {
  319. .attrs = rio_mport_attrs,
  320. };
  321. const struct attribute_group *rio_mport_groups[] = {
  322. &rio_mport_group,
  323. NULL,
  324. };