ccwgroup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * bus driver for ccwgroup
  3. *
  4. * Copyright IBM Corp. 2002, 2012
  5. *
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. */
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/list.h>
  13. #include <linux/device.h>
  14. #include <linux/init.h>
  15. #include <linux/ctype.h>
  16. #include <linux/dcache.h>
  17. #include <asm/cio.h>
  18. #include <asm/ccwdev.h>
  19. #include <asm/ccwgroup.h>
  20. #include "device.h"
  21. #define CCW_BUS_ID_SIZE 10
  22. /* In Linux 2.4, we had a channel device layer called "chandev"
  23. * that did all sorts of obscure stuff for networking devices.
  24. * This is another driver that serves as a replacement for just
  25. * one of its functions, namely the translation of single subchannels
  26. * to devices that use multiple subchannels.
  27. */
  28. static struct bus_type ccwgroup_bus_type;
  29. static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
  30. {
  31. int i;
  32. char str[8];
  33. for (i = 0; i < gdev->count; i++) {
  34. sprintf(str, "cdev%d", i);
  35. sysfs_remove_link(&gdev->dev.kobj, str);
  36. sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
  37. }
  38. }
  39. /*
  40. * Remove references from ccw devices to ccw group device and from
  41. * ccw group device to ccw devices.
  42. */
  43. static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
  44. {
  45. struct ccw_device *cdev;
  46. int i;
  47. for (i = 0; i < gdev->count; i++) {
  48. cdev = gdev->cdev[i];
  49. if (!cdev)
  50. continue;
  51. spin_lock_irq(cdev->ccwlock);
  52. dev_set_drvdata(&cdev->dev, NULL);
  53. spin_unlock_irq(cdev->ccwlock);
  54. gdev->cdev[i] = NULL;
  55. put_device(&cdev->dev);
  56. }
  57. }
  58. /**
  59. * ccwgroup_set_online() - enable a ccwgroup device
  60. * @gdev: target ccwgroup device
  61. *
  62. * This function attempts to put the ccwgroup device into the online state.
  63. * Returns:
  64. * %0 on success and a negative error value on failure.
  65. */
  66. int ccwgroup_set_online(struct ccwgroup_device *gdev)
  67. {
  68. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
  69. int ret = -EINVAL;
  70. if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
  71. return -EAGAIN;
  72. if (gdev->state == CCWGROUP_ONLINE)
  73. goto out;
  74. if (gdrv->set_online)
  75. ret = gdrv->set_online(gdev);
  76. if (ret)
  77. goto out;
  78. gdev->state = CCWGROUP_ONLINE;
  79. out:
  80. atomic_set(&gdev->onoff, 0);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL(ccwgroup_set_online);
  84. /**
  85. * ccwgroup_set_offline() - disable a ccwgroup device
  86. * @gdev: target ccwgroup device
  87. *
  88. * This function attempts to put the ccwgroup device into the offline state.
  89. * Returns:
  90. * %0 on success and a negative error value on failure.
  91. */
  92. int ccwgroup_set_offline(struct ccwgroup_device *gdev)
  93. {
  94. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
  95. int ret = -EINVAL;
  96. if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
  97. return -EAGAIN;
  98. if (gdev->state == CCWGROUP_OFFLINE)
  99. goto out;
  100. if (gdrv->set_offline)
  101. ret = gdrv->set_offline(gdev);
  102. if (ret)
  103. goto out;
  104. gdev->state = CCWGROUP_OFFLINE;
  105. out:
  106. atomic_set(&gdev->onoff, 0);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL(ccwgroup_set_offline);
  110. static ssize_t ccwgroup_online_store(struct device *dev,
  111. struct device_attribute *attr,
  112. const char *buf, size_t count)
  113. {
  114. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  115. unsigned long value;
  116. int ret;
  117. device_lock(dev);
  118. if (!dev->driver) {
  119. ret = -EINVAL;
  120. goto out;
  121. }
  122. ret = kstrtoul(buf, 0, &value);
  123. if (ret)
  124. goto out;
  125. if (value == 1)
  126. ret = ccwgroup_set_online(gdev);
  127. else if (value == 0)
  128. ret = ccwgroup_set_offline(gdev);
  129. else
  130. ret = -EINVAL;
  131. out:
  132. device_unlock(dev);
  133. return (ret == 0) ? count : ret;
  134. }
  135. static ssize_t ccwgroup_online_show(struct device *dev,
  136. struct device_attribute *attr,
  137. char *buf)
  138. {
  139. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  140. int online;
  141. online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
  142. return scnprintf(buf, PAGE_SIZE, "%d\n", online);
  143. }
  144. /*
  145. * Provide an 'ungroup' attribute so the user can remove group devices no
  146. * longer needed or accidentially created. Saves memory :)
  147. */
  148. static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
  149. {
  150. mutex_lock(&gdev->reg_mutex);
  151. if (device_is_registered(&gdev->dev)) {
  152. __ccwgroup_remove_symlinks(gdev);
  153. device_unregister(&gdev->dev);
  154. __ccwgroup_remove_cdev_refs(gdev);
  155. }
  156. mutex_unlock(&gdev->reg_mutex);
  157. }
  158. static ssize_t ccwgroup_ungroup_store(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  163. int rc = 0;
  164. /* Prevent concurrent online/offline processing and ungrouping. */
  165. if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
  166. return -EAGAIN;
  167. if (gdev->state != CCWGROUP_OFFLINE) {
  168. rc = -EINVAL;
  169. goto out;
  170. }
  171. if (device_remove_file_self(dev, attr))
  172. ccwgroup_ungroup(gdev);
  173. else
  174. rc = -ENODEV;
  175. out:
  176. if (rc) {
  177. /* Release onoff "lock" when ungrouping failed. */
  178. atomic_set(&gdev->onoff, 0);
  179. return rc;
  180. }
  181. return count;
  182. }
  183. static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
  184. static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
  185. static struct attribute *ccwgroup_attrs[] = {
  186. &dev_attr_online.attr,
  187. &dev_attr_ungroup.attr,
  188. NULL,
  189. };
  190. static struct attribute_group ccwgroup_attr_group = {
  191. .attrs = ccwgroup_attrs,
  192. };
  193. static const struct attribute_group *ccwgroup_attr_groups[] = {
  194. &ccwgroup_attr_group,
  195. NULL,
  196. };
  197. static void ccwgroup_ungroup_workfn(struct work_struct *work)
  198. {
  199. struct ccwgroup_device *gdev =
  200. container_of(work, struct ccwgroup_device, ungroup_work);
  201. ccwgroup_ungroup(gdev);
  202. put_device(&gdev->dev);
  203. }
  204. static void ccwgroup_release(struct device *dev)
  205. {
  206. kfree(to_ccwgroupdev(dev));
  207. }
  208. static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
  209. {
  210. char str[8];
  211. int i, rc;
  212. for (i = 0; i < gdev->count; i++) {
  213. rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
  214. &gdev->dev.kobj, "group_device");
  215. if (rc) {
  216. for (--i; i >= 0; i--)
  217. sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
  218. "group_device");
  219. return rc;
  220. }
  221. }
  222. for (i = 0; i < gdev->count; i++) {
  223. sprintf(str, "cdev%d", i);
  224. rc = sysfs_create_link(&gdev->dev.kobj,
  225. &gdev->cdev[i]->dev.kobj, str);
  226. if (rc) {
  227. for (--i; i >= 0; i--) {
  228. sprintf(str, "cdev%d", i);
  229. sysfs_remove_link(&gdev->dev.kobj, str);
  230. }
  231. for (i = 0; i < gdev->count; i++)
  232. sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
  233. "group_device");
  234. return rc;
  235. }
  236. }
  237. return 0;
  238. }
  239. static int __get_next_id(const char **buf, struct ccw_dev_id *id)
  240. {
  241. unsigned int cssid, ssid, devno;
  242. int ret = 0, len;
  243. char *start, *end;
  244. start = (char *)*buf;
  245. end = strchr(start, ',');
  246. if (!end) {
  247. /* Last entry. Strip trailing newline, if applicable. */
  248. end = strchr(start, '\n');
  249. if (end)
  250. *end = '\0';
  251. len = strlen(start) + 1;
  252. } else {
  253. len = end - start + 1;
  254. end++;
  255. }
  256. if (len <= CCW_BUS_ID_SIZE) {
  257. if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
  258. ret = -EINVAL;
  259. } else
  260. ret = -EINVAL;
  261. if (!ret) {
  262. id->ssid = ssid;
  263. id->devno = devno;
  264. }
  265. *buf = end;
  266. return ret;
  267. }
  268. /**
  269. * ccwgroup_create_dev() - create and register a ccw group device
  270. * @parent: parent device for the new device
  271. * @gdrv: driver for the new group device
  272. * @num_devices: number of slave devices
  273. * @buf: buffer containing comma separated bus ids of slave devices
  274. *
  275. * Create and register a new ccw group device as a child of @parent. Slave
  276. * devices are obtained from the list of bus ids given in @buf.
  277. * Returns:
  278. * %0 on success and an error code on failure.
  279. * Context:
  280. * non-atomic
  281. */
  282. int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
  283. int num_devices, const char *buf)
  284. {
  285. struct ccwgroup_device *gdev;
  286. struct ccw_dev_id dev_id;
  287. int rc, i;
  288. gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
  289. GFP_KERNEL);
  290. if (!gdev)
  291. return -ENOMEM;
  292. atomic_set(&gdev->onoff, 0);
  293. mutex_init(&gdev->reg_mutex);
  294. mutex_lock(&gdev->reg_mutex);
  295. INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
  296. gdev->count = num_devices;
  297. gdev->dev.bus = &ccwgroup_bus_type;
  298. gdev->dev.parent = parent;
  299. gdev->dev.release = ccwgroup_release;
  300. device_initialize(&gdev->dev);
  301. for (i = 0; i < num_devices && buf; i++) {
  302. rc = __get_next_id(&buf, &dev_id);
  303. if (rc != 0)
  304. goto error;
  305. gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
  306. /*
  307. * All devices have to be of the same type in
  308. * order to be grouped.
  309. */
  310. if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
  311. gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
  312. gdev->cdev[i]->id.driver_info !=
  313. gdev->cdev[0]->id.driver_info) {
  314. rc = -EINVAL;
  315. goto error;
  316. }
  317. /* Don't allow a device to belong to more than one group. */
  318. spin_lock_irq(gdev->cdev[i]->ccwlock);
  319. if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
  320. spin_unlock_irq(gdev->cdev[i]->ccwlock);
  321. rc = -EINVAL;
  322. goto error;
  323. }
  324. dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
  325. spin_unlock_irq(gdev->cdev[i]->ccwlock);
  326. }
  327. /* Check for sufficient number of bus ids. */
  328. if (i < num_devices) {
  329. rc = -EINVAL;
  330. goto error;
  331. }
  332. /* Check for trailing stuff. */
  333. if (i == num_devices && strlen(buf) > 0) {
  334. rc = -EINVAL;
  335. goto error;
  336. }
  337. dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
  338. gdev->dev.groups = ccwgroup_attr_groups;
  339. if (gdrv) {
  340. gdev->dev.driver = &gdrv->driver;
  341. rc = gdrv->setup ? gdrv->setup(gdev) : 0;
  342. if (rc)
  343. goto error;
  344. }
  345. rc = device_add(&gdev->dev);
  346. if (rc)
  347. goto error;
  348. rc = __ccwgroup_create_symlinks(gdev);
  349. if (rc) {
  350. device_del(&gdev->dev);
  351. goto error;
  352. }
  353. mutex_unlock(&gdev->reg_mutex);
  354. return 0;
  355. error:
  356. for (i = 0; i < num_devices; i++)
  357. if (gdev->cdev[i]) {
  358. spin_lock_irq(gdev->cdev[i]->ccwlock);
  359. if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
  360. dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
  361. spin_unlock_irq(gdev->cdev[i]->ccwlock);
  362. put_device(&gdev->cdev[i]->dev);
  363. gdev->cdev[i] = NULL;
  364. }
  365. mutex_unlock(&gdev->reg_mutex);
  366. put_device(&gdev->dev);
  367. return rc;
  368. }
  369. EXPORT_SYMBOL(ccwgroup_create_dev);
  370. static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
  371. void *data)
  372. {
  373. struct ccwgroup_device *gdev = to_ccwgroupdev(data);
  374. if (action == BUS_NOTIFY_UNBIND_DRIVER) {
  375. get_device(&gdev->dev);
  376. schedule_work(&gdev->ungroup_work);
  377. }
  378. return NOTIFY_OK;
  379. }
  380. static struct notifier_block ccwgroup_nb = {
  381. .notifier_call = ccwgroup_notifier
  382. };
  383. static int __init init_ccwgroup(void)
  384. {
  385. int ret;
  386. ret = bus_register(&ccwgroup_bus_type);
  387. if (ret)
  388. return ret;
  389. ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
  390. if (ret)
  391. bus_unregister(&ccwgroup_bus_type);
  392. return ret;
  393. }
  394. static void __exit cleanup_ccwgroup(void)
  395. {
  396. bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
  397. bus_unregister(&ccwgroup_bus_type);
  398. }
  399. module_init(init_ccwgroup);
  400. module_exit(cleanup_ccwgroup);
  401. /************************** driver stuff ******************************/
  402. static int ccwgroup_remove(struct device *dev)
  403. {
  404. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  405. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
  406. if (!dev->driver)
  407. return 0;
  408. if (gdrv->remove)
  409. gdrv->remove(gdev);
  410. return 0;
  411. }
  412. static void ccwgroup_shutdown(struct device *dev)
  413. {
  414. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  415. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
  416. if (!dev->driver)
  417. return;
  418. if (gdrv->shutdown)
  419. gdrv->shutdown(gdev);
  420. }
  421. static int ccwgroup_pm_prepare(struct device *dev)
  422. {
  423. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  424. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
  425. /* Fail while device is being set online/offline. */
  426. if (atomic_read(&gdev->onoff))
  427. return -EAGAIN;
  428. if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
  429. return 0;
  430. return gdrv->prepare ? gdrv->prepare(gdev) : 0;
  431. }
  432. static void ccwgroup_pm_complete(struct device *dev)
  433. {
  434. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  435. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
  436. if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
  437. return;
  438. if (gdrv->complete)
  439. gdrv->complete(gdev);
  440. }
  441. static int ccwgroup_pm_freeze(struct device *dev)
  442. {
  443. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  444. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
  445. if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
  446. return 0;
  447. return gdrv->freeze ? gdrv->freeze(gdev) : 0;
  448. }
  449. static int ccwgroup_pm_thaw(struct device *dev)
  450. {
  451. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  452. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
  453. if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
  454. return 0;
  455. return gdrv->thaw ? gdrv->thaw(gdev) : 0;
  456. }
  457. static int ccwgroup_pm_restore(struct device *dev)
  458. {
  459. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  460. struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
  461. if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
  462. return 0;
  463. return gdrv->restore ? gdrv->restore(gdev) : 0;
  464. }
  465. static const struct dev_pm_ops ccwgroup_pm_ops = {
  466. .prepare = ccwgroup_pm_prepare,
  467. .complete = ccwgroup_pm_complete,
  468. .freeze = ccwgroup_pm_freeze,
  469. .thaw = ccwgroup_pm_thaw,
  470. .restore = ccwgroup_pm_restore,
  471. };
  472. static struct bus_type ccwgroup_bus_type = {
  473. .name = "ccwgroup",
  474. .remove = ccwgroup_remove,
  475. .shutdown = ccwgroup_shutdown,
  476. .pm = &ccwgroup_pm_ops,
  477. };
  478. /**
  479. * ccwgroup_driver_register() - register a ccw group driver
  480. * @cdriver: driver to be registered
  481. *
  482. * This function is mainly a wrapper around driver_register().
  483. */
  484. int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
  485. {
  486. /* register our new driver with the core */
  487. cdriver->driver.bus = &ccwgroup_bus_type;
  488. return driver_register(&cdriver->driver);
  489. }
  490. EXPORT_SYMBOL(ccwgroup_driver_register);
  491. static int __ccwgroup_match_all(struct device *dev, void *data)
  492. {
  493. return 1;
  494. }
  495. /**
  496. * ccwgroup_driver_unregister() - deregister a ccw group driver
  497. * @cdriver: driver to be deregistered
  498. *
  499. * This function is mainly a wrapper around driver_unregister().
  500. */
  501. void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
  502. {
  503. struct device *dev;
  504. /* We don't want ccwgroup devices to live longer than their driver. */
  505. while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
  506. __ccwgroup_match_all))) {
  507. struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
  508. ccwgroup_ungroup(gdev);
  509. put_device(dev);
  510. }
  511. driver_unregister(&cdriver->driver);
  512. }
  513. EXPORT_SYMBOL(ccwgroup_driver_unregister);
  514. /**
  515. * ccwgroup_probe_ccwdev() - probe function for slave devices
  516. * @cdev: ccw device to be probed
  517. *
  518. * This is a dummy probe function for ccw devices that are slave devices in
  519. * a ccw group device.
  520. * Returns:
  521. * always %0
  522. */
  523. int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
  524. {
  525. return 0;
  526. }
  527. EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
  528. /**
  529. * ccwgroup_remove_ccwdev() - remove function for slave devices
  530. * @cdev: ccw device to be removed
  531. *
  532. * This is a remove function for ccw devices that are slave devices in a ccw
  533. * group device. It sets the ccw device offline and also deregisters the
  534. * embedding ccw group device.
  535. */
  536. void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
  537. {
  538. struct ccwgroup_device *gdev;
  539. /* Ignore offlining errors, device is gone anyway. */
  540. ccw_device_set_offline(cdev);
  541. /* If one of its devices is gone, the whole group is done for. */
  542. spin_lock_irq(cdev->ccwlock);
  543. gdev = dev_get_drvdata(&cdev->dev);
  544. if (!gdev) {
  545. spin_unlock_irq(cdev->ccwlock);
  546. return;
  547. }
  548. /* Get ccwgroup device reference for local processing. */
  549. get_device(&gdev->dev);
  550. spin_unlock_irq(cdev->ccwlock);
  551. /* Unregister group device. */
  552. ccwgroup_ungroup(gdev);
  553. /* Release ccwgroup device reference for local processing. */
  554. put_device(&gdev->dev);
  555. }
  556. EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
  557. MODULE_LICENSE("GPL");