Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  bus driver for ccwgroup
0004  *
0005  *  Copyright IBM Corp. 2002, 2012
0006  *
0007  *  Author(s): Arnd Bergmann (arndb@de.ibm.com)
0008  *         Cornelia Huck (cornelia.huck@de.ibm.com)
0009  */
0010 #include <linux/module.h>
0011 #include <linux/errno.h>
0012 #include <linux/slab.h>
0013 #include <linux/list.h>
0014 #include <linux/device.h>
0015 #include <linux/init.h>
0016 #include <linux/ctype.h>
0017 #include <linux/dcache.h>
0018 
0019 #include <asm/cio.h>
0020 #include <asm/ccwdev.h>
0021 #include <asm/ccwgroup.h>
0022 
0023 #include "device.h"
0024 
0025 #define CCW_BUS_ID_SIZE     10
0026 
0027 /* In Linux 2.4, we had a channel device layer called "chandev"
0028  * that did all sorts of obscure stuff for networking devices.
0029  * This is another driver that serves as a replacement for just
0030  * one of its functions, namely the translation of single subchannels
0031  * to devices that use multiple subchannels.
0032  */
0033 
0034 static struct bus_type ccwgroup_bus_type;
0035 
0036 static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
0037 {
0038     int i;
0039     char str[16];
0040 
0041     for (i = 0; i < gdev->count; i++) {
0042         sprintf(str, "cdev%d", i);
0043         sysfs_remove_link(&gdev->dev.kobj, str);
0044         sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
0045     }
0046 }
0047 
0048 /**
0049  * ccwgroup_set_online() - enable a ccwgroup device
0050  * @gdev: target ccwgroup device
0051  *
0052  * This function attempts to put the ccwgroup device into the online state.
0053  * Returns:
0054  *  %0 on success and a negative error value on failure.
0055  */
0056 int ccwgroup_set_online(struct ccwgroup_device *gdev)
0057 {
0058     struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
0059     int ret = -EINVAL;
0060 
0061     if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
0062         return -EAGAIN;
0063     if (gdev->state == CCWGROUP_ONLINE)
0064         goto out;
0065     if (gdrv->set_online)
0066         ret = gdrv->set_online(gdev);
0067     if (ret)
0068         goto out;
0069 
0070     gdev->state = CCWGROUP_ONLINE;
0071 out:
0072     atomic_set(&gdev->onoff, 0);
0073     return ret;
0074 }
0075 EXPORT_SYMBOL(ccwgroup_set_online);
0076 
0077 /**
0078  * ccwgroup_set_offline() - disable a ccwgroup device
0079  * @gdev: target ccwgroup device
0080  * @call_gdrv: Call the registered gdrv set_offline function
0081  *
0082  * This function attempts to put the ccwgroup device into the offline state.
0083  * Returns:
0084  *  %0 on success and a negative error value on failure.
0085  */
0086 int ccwgroup_set_offline(struct ccwgroup_device *gdev, bool call_gdrv)
0087 {
0088     struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
0089     int ret = -EINVAL;
0090 
0091     if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
0092         return -EAGAIN;
0093     if (gdev->state == CCWGROUP_OFFLINE)
0094         goto out;
0095     if (!call_gdrv) {
0096         ret = 0;
0097         goto offline;
0098     }
0099     if (gdrv->set_offline)
0100         ret = gdrv->set_offline(gdev);
0101     if (ret)
0102         goto out;
0103 
0104 offline:
0105     gdev->state = CCWGROUP_OFFLINE;
0106 out:
0107     atomic_set(&gdev->onoff, 0);
0108     return ret;
0109 }
0110 EXPORT_SYMBOL(ccwgroup_set_offline);
0111 
0112 static ssize_t ccwgroup_online_store(struct device *dev,
0113                      struct device_attribute *attr,
0114                      const char *buf, size_t count)
0115 {
0116     struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
0117     unsigned long value;
0118     int ret;
0119 
0120     device_lock(dev);
0121     if (!dev->driver) {
0122         ret = -EINVAL;
0123         goto out;
0124     }
0125 
0126     ret = kstrtoul(buf, 0, &value);
0127     if (ret)
0128         goto out;
0129 
0130     if (value == 1)
0131         ret = ccwgroup_set_online(gdev);
0132     else if (value == 0)
0133         ret = ccwgroup_set_offline(gdev, true);
0134     else
0135         ret = -EINVAL;
0136 out:
0137     device_unlock(dev);
0138     return (ret == 0) ? count : ret;
0139 }
0140 
0141 static ssize_t ccwgroup_online_show(struct device *dev,
0142                     struct device_attribute *attr,
0143                     char *buf)
0144 {
0145     struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
0146     int online;
0147 
0148     online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
0149 
0150     return scnprintf(buf, PAGE_SIZE, "%d\n", online);
0151 }
0152 
0153 /*
0154  * Provide an 'ungroup' attribute so the user can remove group devices no
0155  * longer needed or accidentially created. Saves memory :)
0156  */
0157 static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
0158 {
0159     mutex_lock(&gdev->reg_mutex);
0160     if (device_is_registered(&gdev->dev)) {
0161         __ccwgroup_remove_symlinks(gdev);
0162         device_unregister(&gdev->dev);
0163     }
0164     mutex_unlock(&gdev->reg_mutex);
0165 }
0166 
0167 static ssize_t ccwgroup_ungroup_store(struct device *dev,
0168                       struct device_attribute *attr,
0169                       const char *buf, size_t count)
0170 {
0171     struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
0172     int rc = 0;
0173 
0174     /* Prevent concurrent online/offline processing and ungrouping. */
0175     if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
0176         return -EAGAIN;
0177     if (gdev->state != CCWGROUP_OFFLINE) {
0178         rc = -EINVAL;
0179         goto out;
0180     }
0181 
0182     if (device_remove_file_self(dev, attr))
0183         ccwgroup_ungroup(gdev);
0184     else
0185         rc = -ENODEV;
0186 out:
0187     if (rc) {
0188         /* Release onoff "lock" when ungrouping failed. */
0189         atomic_set(&gdev->onoff, 0);
0190         return rc;
0191     }
0192     return count;
0193 }
0194 static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
0195 static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
0196 
0197 static struct attribute *ccwgroup_dev_attrs[] = {
0198     &dev_attr_online.attr,
0199     &dev_attr_ungroup.attr,
0200     NULL,
0201 };
0202 ATTRIBUTE_GROUPS(ccwgroup_dev);
0203 
0204 static void ccwgroup_ungroup_workfn(struct work_struct *work)
0205 {
0206     struct ccwgroup_device *gdev =
0207         container_of(work, struct ccwgroup_device, ungroup_work);
0208 
0209     ccwgroup_ungroup(gdev);
0210     put_device(&gdev->dev);
0211 }
0212 
0213 static void ccwgroup_release(struct device *dev)
0214 {
0215     struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
0216     unsigned int i;
0217 
0218     for (i = 0; i < gdev->count; i++) {
0219         struct ccw_device *cdev = gdev->cdev[i];
0220         unsigned long flags;
0221 
0222         if (cdev) {
0223             spin_lock_irqsave(cdev->ccwlock, flags);
0224             if (dev_get_drvdata(&cdev->dev) == gdev)
0225                 dev_set_drvdata(&cdev->dev, NULL);
0226             spin_unlock_irqrestore(cdev->ccwlock, flags);
0227             put_device(&cdev->dev);
0228         }
0229     }
0230 
0231     kfree(gdev);
0232 }
0233 
0234 static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
0235 {
0236     char str[16];
0237     int i, rc;
0238 
0239     for (i = 0; i < gdev->count; i++) {
0240         rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
0241                        &gdev->dev.kobj, "group_device");
0242         if (rc) {
0243             for (--i; i >= 0; i--)
0244                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
0245                           "group_device");
0246             return rc;
0247         }
0248     }
0249     for (i = 0; i < gdev->count; i++) {
0250         sprintf(str, "cdev%d", i);
0251         rc = sysfs_create_link(&gdev->dev.kobj,
0252                        &gdev->cdev[i]->dev.kobj, str);
0253         if (rc) {
0254             for (--i; i >= 0; i--) {
0255                 sprintf(str, "cdev%d", i);
0256                 sysfs_remove_link(&gdev->dev.kobj, str);
0257             }
0258             for (i = 0; i < gdev->count; i++)
0259                 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
0260                           "group_device");
0261             return rc;
0262         }
0263     }
0264     return 0;
0265 }
0266 
0267 static int __get_next_id(const char **buf, struct ccw_dev_id *id)
0268 {
0269     unsigned int cssid, ssid, devno;
0270     int ret = 0, len;
0271     char *start, *end;
0272 
0273     start = (char *)*buf;
0274     end = strchr(start, ',');
0275     if (!end) {
0276         /* Last entry. Strip trailing newline, if applicable. */
0277         end = strchr(start, '\n');
0278         if (end)
0279             *end = '\0';
0280         len = strlen(start) + 1;
0281     } else {
0282         len = end - start + 1;
0283         end++;
0284     }
0285     if (len <= CCW_BUS_ID_SIZE) {
0286         if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
0287             ret = -EINVAL;
0288     } else
0289         ret = -EINVAL;
0290 
0291     if (!ret) {
0292         id->ssid = ssid;
0293         id->devno = devno;
0294     }
0295     *buf = end;
0296     return ret;
0297 }
0298 
0299 /**
0300  * ccwgroup_create_dev() - create and register a ccw group device
0301  * @parent: parent device for the new device
0302  * @gdrv: driver for the new group device
0303  * @num_devices: number of slave devices
0304  * @buf: buffer containing comma separated bus ids of slave devices
0305  *
0306  * Create and register a new ccw group device as a child of @parent. Slave
0307  * devices are obtained from the list of bus ids given in @buf.
0308  * Returns:
0309  *  %0 on success and an error code on failure.
0310  * Context:
0311  *  non-atomic
0312  */
0313 int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
0314             int num_devices, const char *buf)
0315 {
0316     struct ccwgroup_device *gdev;
0317     struct ccw_dev_id dev_id;
0318     int rc, i;
0319 
0320     if (num_devices < 1)
0321         return -EINVAL;
0322 
0323     gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL);
0324     if (!gdev)
0325         return -ENOMEM;
0326 
0327     atomic_set(&gdev->onoff, 0);
0328     mutex_init(&gdev->reg_mutex);
0329     mutex_lock(&gdev->reg_mutex);
0330     INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
0331     gdev->count = num_devices;
0332     gdev->dev.bus = &ccwgroup_bus_type;
0333     gdev->dev.parent = parent;
0334     gdev->dev.release = ccwgroup_release;
0335     device_initialize(&gdev->dev);
0336 
0337     for (i = 0; i < num_devices && buf; i++) {
0338         rc = __get_next_id(&buf, &dev_id);
0339         if (rc != 0)
0340             goto error;
0341         gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
0342         /*
0343          * All devices have to be of the same type in
0344          * order to be grouped.
0345          */
0346         if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
0347             gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
0348             gdev->cdev[i]->id.driver_info !=
0349             gdev->cdev[0]->id.driver_info) {
0350             rc = -EINVAL;
0351             goto error;
0352         }
0353         /* Don't allow a device to belong to more than one group. */
0354         spin_lock_irq(gdev->cdev[i]->ccwlock);
0355         if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
0356             spin_unlock_irq(gdev->cdev[i]->ccwlock);
0357             rc = -EINVAL;
0358             goto error;
0359         }
0360         dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
0361         spin_unlock_irq(gdev->cdev[i]->ccwlock);
0362     }
0363     /* Check for sufficient number of bus ids. */
0364     if (i < num_devices) {
0365         rc = -EINVAL;
0366         goto error;
0367     }
0368     /* Check for trailing stuff. */
0369     if (i == num_devices && buf && strlen(buf) > 0) {
0370         rc = -EINVAL;
0371         goto error;
0372     }
0373     /* Check if the devices are bound to the required ccw driver. */
0374     if (gdrv && gdrv->ccw_driver &&
0375         gdev->cdev[0]->drv != gdrv->ccw_driver) {
0376         rc = -EINVAL;
0377         goto error;
0378     }
0379 
0380     dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
0381 
0382     if (gdrv) {
0383         gdev->dev.driver = &gdrv->driver;
0384         rc = gdrv->setup ? gdrv->setup(gdev) : 0;
0385         if (rc)
0386             goto error;
0387     }
0388     rc = device_add(&gdev->dev);
0389     if (rc)
0390         goto error;
0391     rc = __ccwgroup_create_symlinks(gdev);
0392     if (rc) {
0393         device_del(&gdev->dev);
0394         goto error;
0395     }
0396     mutex_unlock(&gdev->reg_mutex);
0397     return 0;
0398 error:
0399     mutex_unlock(&gdev->reg_mutex);
0400     put_device(&gdev->dev);
0401     return rc;
0402 }
0403 EXPORT_SYMBOL(ccwgroup_create_dev);
0404 
0405 static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
0406                  void *data)
0407 {
0408     struct ccwgroup_device *gdev = to_ccwgroupdev(data);
0409 
0410     if (action == BUS_NOTIFY_UNBOUND_DRIVER) {
0411         get_device(&gdev->dev);
0412         schedule_work(&gdev->ungroup_work);
0413     }
0414 
0415     return NOTIFY_OK;
0416 }
0417 
0418 static struct notifier_block ccwgroup_nb = {
0419     .notifier_call = ccwgroup_notifier
0420 };
0421 
0422 static int __init init_ccwgroup(void)
0423 {
0424     int ret;
0425 
0426     ret = bus_register(&ccwgroup_bus_type);
0427     if (ret)
0428         return ret;
0429 
0430     ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
0431     if (ret)
0432         bus_unregister(&ccwgroup_bus_type);
0433 
0434     return ret;
0435 }
0436 
0437 static void __exit cleanup_ccwgroup(void)
0438 {
0439     bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
0440     bus_unregister(&ccwgroup_bus_type);
0441 }
0442 
0443 module_init(init_ccwgroup);
0444 module_exit(cleanup_ccwgroup);
0445 
0446 /************************** driver stuff ******************************/
0447 
0448 static void ccwgroup_remove(struct device *dev)
0449 {
0450     struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
0451     struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
0452 
0453     if (gdrv->remove)
0454         gdrv->remove(gdev);
0455 }
0456 
0457 static void ccwgroup_shutdown(struct device *dev)
0458 {
0459     struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
0460     struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
0461 
0462     if (!dev->driver)
0463         return;
0464     if (gdrv->shutdown)
0465         gdrv->shutdown(gdev);
0466 }
0467 
0468 static struct bus_type ccwgroup_bus_type = {
0469     .name   = "ccwgroup",
0470     .dev_groups = ccwgroup_dev_groups,
0471     .remove = ccwgroup_remove,
0472     .shutdown = ccwgroup_shutdown,
0473 };
0474 
0475 bool dev_is_ccwgroup(struct device *dev)
0476 {
0477     return dev->bus == &ccwgroup_bus_type;
0478 }
0479 EXPORT_SYMBOL(dev_is_ccwgroup);
0480 
0481 /**
0482  * ccwgroup_driver_register() - register a ccw group driver
0483  * @cdriver: driver to be registered
0484  *
0485  * This function is mainly a wrapper around driver_register().
0486  */
0487 int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
0488 {
0489     /* register our new driver with the core */
0490     cdriver->driver.bus = &ccwgroup_bus_type;
0491 
0492     return driver_register(&cdriver->driver);
0493 }
0494 EXPORT_SYMBOL(ccwgroup_driver_register);
0495 
0496 /**
0497  * ccwgroup_driver_unregister() - deregister a ccw group driver
0498  * @cdriver: driver to be deregistered
0499  *
0500  * This function is mainly a wrapper around driver_unregister().
0501  */
0502 void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
0503 {
0504     driver_unregister(&cdriver->driver);
0505 }
0506 EXPORT_SYMBOL(ccwgroup_driver_unregister);
0507 
0508 /**
0509  * ccwgroup_probe_ccwdev() - probe function for slave devices
0510  * @cdev: ccw device to be probed
0511  *
0512  * This is a dummy probe function for ccw devices that are slave devices in
0513  * a ccw group device.
0514  * Returns:
0515  *  always %0
0516  */
0517 int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
0518 {
0519     return 0;
0520 }
0521 EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
0522 
0523 /**
0524  * ccwgroup_remove_ccwdev() - remove function for slave devices
0525  * @cdev: ccw device to be removed
0526  *
0527  * This is a remove function for ccw devices that are slave devices in a ccw
0528  * group device. It sets the ccw device offline and also deregisters the
0529  * embedding ccw group device.
0530  */
0531 void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
0532 {
0533     struct ccwgroup_device *gdev;
0534 
0535     /* Ignore offlining errors, device is gone anyway. */
0536     ccw_device_set_offline(cdev);
0537     /* If one of its devices is gone, the whole group is done for. */
0538     spin_lock_irq(cdev->ccwlock);
0539     gdev = dev_get_drvdata(&cdev->dev);
0540     if (!gdev) {
0541         spin_unlock_irq(cdev->ccwlock);
0542         return;
0543     }
0544     /* Get ccwgroup device reference for local processing. */
0545     get_device(&gdev->dev);
0546     spin_unlock_irq(cdev->ccwlock);
0547     /* Unregister group device. */
0548     ccwgroup_ungroup(gdev);
0549     /* Release ccwgroup device reference for local processing. */
0550     put_device(&gdev->dev);
0551 }
0552 EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
0553 MODULE_LICENSE("GPL");