ccwgroup.c 13 KB

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