chp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * drivers/s390/cio/chp.c
  3. *
  4. * Copyright IBM Corp. 1999,2007
  5. * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
  6. * Arnd Bergmann (arndb@de.ibm.com)
  7. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  8. */
  9. #include <linux/bug.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/spinlock.h>
  12. #include <asm/errno.h>
  13. #include "chpid.h"
  14. #include "cio.h"
  15. #include "css.h"
  16. #include "ioasm.h"
  17. #include "cio_debug.h"
  18. #include "chp.h"
  19. #define to_channelpath(device) container_of(device, struct channel_path, dev)
  20. /* Return channel_path struct for given chpid. */
  21. static inline struct channel_path *chpid_to_chp(struct chp_id chpid)
  22. {
  23. return css[chpid.cssid]->chps[chpid.id];
  24. }
  25. /* Set vary state for given chpid. */
  26. static void set_chp_logically_online(struct chp_id chpid, int onoff)
  27. {
  28. chpid_to_chp(chpid)->state = onoff;
  29. }
  30. /* On succes return 0 if channel-path is varied offline, 1 if it is varied
  31. * online. Return -ENODEV if channel-path is not registered. */
  32. int chp_get_status(struct chp_id chpid)
  33. {
  34. return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
  35. }
  36. /**
  37. * chp_get_sch_opm - return opm for subchannel
  38. * @sch: subchannel
  39. *
  40. * Calculate and return the operational path mask (opm) based on the chpids
  41. * used by the subchannel and the status of the associated channel-paths.
  42. */
  43. u8 chp_get_sch_opm(struct subchannel *sch)
  44. {
  45. struct chp_id chpid;
  46. int opm;
  47. int i;
  48. opm = 0;
  49. chp_id_init(&chpid);
  50. for (i=0; i < 8; i++) {
  51. opm <<= 1;
  52. chpid.id = sch->schib.pmcw.chpid[i];
  53. if (chp_get_status(chpid) != 0)
  54. opm |= 1;
  55. }
  56. return opm;
  57. }
  58. /**
  59. * chp_is_registered - check if a channel-path is registered
  60. * @chpid: channel-path ID
  61. *
  62. * Return non-zero if a channel-path with the given chpid is registered,
  63. * zero otherwise.
  64. */
  65. int chp_is_registered(struct chp_id chpid)
  66. {
  67. return chpid_to_chp(chpid) != NULL;
  68. }
  69. /*
  70. * Function: s390_vary_chpid
  71. * Varies the specified chpid online or offline
  72. */
  73. static int s390_vary_chpid(struct chp_id chpid, int on)
  74. {
  75. char dbf_text[15];
  76. int status;
  77. sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
  78. chpid.id);
  79. CIO_TRACE_EVENT( 2, dbf_text);
  80. status = chp_get_status(chpid);
  81. if (status < 0) {
  82. printk(KERN_ERR "Can't vary unknown chpid %x.%02x\n",
  83. chpid.cssid, chpid.id);
  84. return -EINVAL;
  85. }
  86. if (!on && !status) {
  87. printk(KERN_ERR "chpid %x.%02x is already offline\n",
  88. chpid.cssid, chpid.id);
  89. return -EINVAL;
  90. }
  91. set_chp_logically_online(chpid, on);
  92. chsc_chp_vary(chpid, on);
  93. return 0;
  94. }
  95. /*
  96. * Channel measurement related functions
  97. */
  98. static ssize_t chp_measurement_chars_read(struct kobject *kobj, char *buf,
  99. loff_t off, size_t count)
  100. {
  101. struct channel_path *chp;
  102. unsigned int size;
  103. chp = to_channelpath(container_of(kobj, struct device, kobj));
  104. if (!chp->cmg_chars)
  105. return 0;
  106. size = sizeof(struct cmg_chars);
  107. if (off > size)
  108. return 0;
  109. if (off + count > size)
  110. count = size - off;
  111. memcpy(buf, chp->cmg_chars + off, count);
  112. return count;
  113. }
  114. static struct bin_attribute chp_measurement_chars_attr = {
  115. .attr = {
  116. .name = "measurement_chars",
  117. .mode = S_IRUSR,
  118. .owner = THIS_MODULE,
  119. },
  120. .size = sizeof(struct cmg_chars),
  121. .read = chp_measurement_chars_read,
  122. };
  123. static void chp_measurement_copy_block(struct cmg_entry *buf,
  124. struct channel_subsystem *css,
  125. struct chp_id chpid)
  126. {
  127. void *area;
  128. struct cmg_entry *entry, reference_buf;
  129. int idx;
  130. if (chpid.id < 128) {
  131. area = css->cub_addr1;
  132. idx = chpid.id;
  133. } else {
  134. area = css->cub_addr2;
  135. idx = chpid.id - 128;
  136. }
  137. entry = area + (idx * sizeof(struct cmg_entry));
  138. do {
  139. memcpy(buf, entry, sizeof(*entry));
  140. memcpy(&reference_buf, entry, sizeof(*entry));
  141. } while (reference_buf.values[0] != buf->values[0]);
  142. }
  143. static ssize_t chp_measurement_read(struct kobject *kobj, char *buf,
  144. loff_t off, size_t count)
  145. {
  146. struct channel_path *chp;
  147. struct channel_subsystem *css;
  148. unsigned int size;
  149. chp = to_channelpath(container_of(kobj, struct device, kobj));
  150. css = to_css(chp->dev.parent);
  151. size = sizeof(struct cmg_entry);
  152. /* Only allow single reads. */
  153. if (off || count < size)
  154. return 0;
  155. chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
  156. count = size;
  157. return count;
  158. }
  159. static struct bin_attribute chp_measurement_attr = {
  160. .attr = {
  161. .name = "measurement",
  162. .mode = S_IRUSR,
  163. .owner = THIS_MODULE,
  164. },
  165. .size = sizeof(struct cmg_entry),
  166. .read = chp_measurement_read,
  167. };
  168. void chp_remove_cmg_attr(struct channel_path *chp)
  169. {
  170. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  171. device_remove_bin_file(&chp->dev, &chp_measurement_attr);
  172. }
  173. int chp_add_cmg_attr(struct channel_path *chp)
  174. {
  175. int ret;
  176. ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
  177. if (ret)
  178. return ret;
  179. ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
  180. if (ret)
  181. device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
  182. return ret;
  183. }
  184. /*
  185. * Files for the channel path entries.
  186. */
  187. static ssize_t chp_status_show(struct device *dev,
  188. struct device_attribute *attr, char *buf)
  189. {
  190. struct channel_path *chp = container_of(dev, struct channel_path, dev);
  191. if (!chp)
  192. return 0;
  193. return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
  194. sprintf(buf, "offline\n"));
  195. }
  196. static ssize_t chp_status_write(struct device *dev,
  197. struct device_attribute *attr,
  198. const char *buf, size_t count)
  199. {
  200. struct channel_path *cp = container_of(dev, struct channel_path, dev);
  201. char cmd[10];
  202. int num_args;
  203. int error;
  204. num_args = sscanf(buf, "%5s", cmd);
  205. if (!num_args)
  206. return count;
  207. if (!strnicmp(cmd, "on", 2) || !strcmp(cmd, "1"))
  208. error = s390_vary_chpid(cp->chpid, 1);
  209. else if (!strnicmp(cmd, "off", 3) || !strcmp(cmd, "0"))
  210. error = s390_vary_chpid(cp->chpid, 0);
  211. else
  212. error = -EINVAL;
  213. return error < 0 ? error : count;
  214. }
  215. static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
  216. static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
  217. char *buf)
  218. {
  219. struct channel_path *chp = container_of(dev, struct channel_path, dev);
  220. if (!chp)
  221. return 0;
  222. return sprintf(buf, "%x\n", chp->desc.desc);
  223. }
  224. static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
  225. static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
  226. char *buf)
  227. {
  228. struct channel_path *chp = to_channelpath(dev);
  229. if (!chp)
  230. return 0;
  231. if (chp->cmg == -1) /* channel measurements not available */
  232. return sprintf(buf, "unknown\n");
  233. return sprintf(buf, "%x\n", chp->cmg);
  234. }
  235. static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
  236. static ssize_t chp_shared_show(struct device *dev,
  237. struct device_attribute *attr, char *buf)
  238. {
  239. struct channel_path *chp = to_channelpath(dev);
  240. if (!chp)
  241. return 0;
  242. if (chp->shared == -1) /* channel measurements not available */
  243. return sprintf(buf, "unknown\n");
  244. return sprintf(buf, "%x\n", chp->shared);
  245. }
  246. static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
  247. static struct attribute * chp_attrs[] = {
  248. &dev_attr_status.attr,
  249. &dev_attr_type.attr,
  250. &dev_attr_cmg.attr,
  251. &dev_attr_shared.attr,
  252. NULL,
  253. };
  254. static struct attribute_group chp_attr_group = {
  255. .attrs = chp_attrs,
  256. };
  257. static void chp_release(struct device *dev)
  258. {
  259. struct channel_path *cp;
  260. cp = container_of(dev, struct channel_path, dev);
  261. kfree(cp);
  262. }
  263. /**
  264. * chp_new - register a new channel-path
  265. * @chpid - channel-path ID
  266. *
  267. * Create and register data structure representing new channel-path. Return
  268. * zero on success, non-zero otherwise.
  269. */
  270. int chp_new(struct chp_id chpid)
  271. {
  272. struct channel_path *chp;
  273. int ret;
  274. chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
  275. if (!chp)
  276. return -ENOMEM;
  277. /* fill in status, etc. */
  278. chp->chpid = chpid;
  279. chp->state = 1;
  280. chp->dev.parent = &css[chpid.cssid]->device;
  281. chp->dev.release = chp_release;
  282. snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp%x.%02x", chpid.cssid,
  283. chpid.id);
  284. /* Obtain channel path description and fill it in. */
  285. ret = chsc_determine_channel_path_description(chpid, &chp->desc);
  286. if (ret)
  287. goto out_free;
  288. /* Get channel-measurement characteristics. */
  289. if (css_characteristics_avail && css_chsc_characteristics.scmc
  290. && css_chsc_characteristics.secm) {
  291. ret = chsc_get_channel_measurement_chars(chp);
  292. if (ret)
  293. goto out_free;
  294. } else {
  295. static int msg_done;
  296. if (!msg_done) {
  297. printk(KERN_WARNING "cio: Channel measurements not "
  298. "available, continuing.\n");
  299. msg_done = 1;
  300. }
  301. chp->cmg = -1;
  302. }
  303. /* make it known to the system */
  304. ret = device_register(&chp->dev);
  305. if (ret) {
  306. printk(KERN_WARNING "%s: could not register %x.%02x\n",
  307. __func__, chpid.cssid, chpid.id);
  308. goto out_free;
  309. }
  310. ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
  311. if (ret) {
  312. device_unregister(&chp->dev);
  313. goto out_free;
  314. }
  315. mutex_lock(&css[chpid.cssid]->mutex);
  316. if (css[chpid.cssid]->cm_enabled) {
  317. ret = chp_add_cmg_attr(chp);
  318. if (ret) {
  319. sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
  320. device_unregister(&chp->dev);
  321. mutex_unlock(&css[chpid.cssid]->mutex);
  322. goto out_free;
  323. }
  324. }
  325. css[chpid.cssid]->chps[chpid.id] = chp;
  326. mutex_unlock(&css[chpid.cssid]->mutex);
  327. return ret;
  328. out_free:
  329. kfree(chp);
  330. return ret;
  331. }
  332. /**
  333. * chp_get_chp_desc - return newly allocated channel-path description
  334. * @chpid: channel-path ID
  335. *
  336. * On success return a newly allocated copy of the channel-path description
  337. * data associated with the given channel-path ID. Return %NULL on error.
  338. */
  339. void *chp_get_chp_desc(struct chp_id chpid)
  340. {
  341. struct channel_path *chp;
  342. struct channel_path_desc *desc;
  343. chp = chpid_to_chp(chpid);
  344. if (!chp)
  345. return NULL;
  346. desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
  347. if (!desc)
  348. return NULL;
  349. memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
  350. return desc;
  351. }
  352. /**
  353. * chp_process_crw - process channel-path status change
  354. * @id: channel-path ID number
  355. * @status: non-zero if channel-path has become available, zero otherwise
  356. *
  357. * Handle channel-report-words indicating that the status of a channel-path
  358. * has changed.
  359. */
  360. int chp_process_crw(int id, int status)
  361. {
  362. struct chp_id chpid;
  363. chp_id_init(&chpid);
  364. chpid.id = id;
  365. if (status) {
  366. if (!chp_is_registered(chpid))
  367. chp_new(chpid);
  368. return chsc_chp_online(chpid);
  369. } else {
  370. chsc_chp_offline(chpid);
  371. return 0;
  372. }
  373. }