| 1 | |
| 2 | |
| 3 | * transport_class.c - implementation of generic transport classes
|
| 4 | * using attribute_containers
|
| 5 | *
|
| 6 | * Copyright (c) 2005 - James Bottomley <[email protected]>
|
| 7 | *
|
| 8 | * The basic idea here is to allow any "device controller" (which
|
| 9 | * would most often be a Host Bus Adapter to use the services of one
|
| 10 | * or more tranport classes for performing transport specific
|
| 11 | * services. Transport specific services are things that the generic
|
| 12 | * command layer doesn't want to know about (speed settings, line
|
| 13 | * condidtioning, etc), but which the user might be interested in.
|
| 14 | * Thus, the HBA's use the routines exported by the transport classes
|
| 15 | * to perform these functions. The transport classes export certain
|
| 16 | * values to the user via sysfs using attribute containers.
|
| 17 | *
|
| 18 | * Note: because not every HBA will care about every transport
|
| 19 | * attribute, there's a many to one relationship that goes like this:
|
| 20 | *
|
| 21 | * transport class<-----attribute container<----class device
|
| 22 | *
|
| 23 | * Usually the attribute container is per-HBA, but the design doesn't
|
| 24 | * mandate that. Although most of the services will be specific to
|
| 25 | * the actual external storage connection used by the HBA, the generic
|
| 26 | * transport class is framed entirely in terms of generic devices to
|
| 27 | * allow it to be used by any physical HBA in the system.
|
| 28 |
|
| 29 | #include <linux/export.h>
|
| 30 | #include <linux/attribute_container.h>
|
| 31 | #include <linux/transport_class.h>
|
| 32 |
|
| 33 | static int transport_remove_classdev(struct attribute_container *cont,
|
| 34 | struct device *dev,
|
| 35 | struct device *classdev);
|
| 36 |
|
| 37 | |
| 38 | * transport_class_register - register an initial transport class
|
| 39 | *
|
| 40 | * @tclass: a pointer to the transport class structure to be initialised
|
| 41 | *
|
| 42 | * The transport class contains an embedded class which is used to
|
| 43 | * identify it. The caller should initialise this structure with
|
| 44 | * zeros and then generic class must have been initialised with the
|
| 45 | * actual transport class unique name. There's a macro
|
| 46 | * DECLARE_TRANSPORT_CLASS() to do this (declared classes still must
|
| 47 | * be registered).
|
| 48 | *
|
| 49 | * Returns 0 on success or error on failure.
|
| 50 |
|
| 51 | int transport_class_register(struct transport_class *tclass)
|
| 52 | {
|
| 53 | return class_register(&tclass->class);
|
| 54 | }
|
| 55 | EXPORT_SYMBOL_GPL(transport_class_register);
|
| 56 |
|
| 57 | |
| 58 | * transport_class_unregister - unregister a previously registered class
|
| 59 | *
|
| 60 | * @tclass: The transport class to unregister
|
| 61 | *
|
| 62 | * Must be called prior to deallocating the memory for the transport
|
| 63 | * class.
|
| 64 |
|
| 65 | void transport_class_unregister(struct transport_class *tclass)
|
| 66 | {
|
| 67 | class_unregister(&tclass->class);
|
| 68 | }
|
| 69 | EXPORT_SYMBOL_GPL(transport_class_unregister);
|
| 70 |
|
| 71 | static int anon_transport_dummy_function(struct transport_container *tc,
|
| 72 | struct device *dev,
|
| 73 | struct device *cdev)
|
| 74 | {
|
| 75 |
|
| 76 | return 0;
|
| 77 | }
|
| 78 |
|
| 79 | |
| 80 | * anon_transport_class_register - register an anonymous class
|
| 81 | *
|
| 82 | * @atc: The anon transport class to register
|
| 83 | *
|
| 84 | * The anonymous transport class contains both a transport class and a
|
| 85 | * container. The idea of an anonymous class is that it never
|
| 86 | * actually has any device attributes associated with it (and thus
|
| 87 | * saves on container storage). So it can only be used for triggering
|
| 88 | * events. Use prezero and then use DECLARE_ANON_TRANSPORT_CLASS() to
|
| 89 | * initialise the anon transport class storage.
|
| 90 |
|
| 91 | void anon_transport_class_register(struct anon_transport_class *atc)
|
| 92 | {
|
| 93 | atc->container.class = &atc->tclass.class;
|
| 94 | attribute_container_set_no_classdevs(&atc->container);
|
| 95 | attribute_container_register(&atc->container);
|
| 96 | atc->tclass.setup = anon_transport_dummy_function;
|
| 97 | atc->tclass.remove = anon_transport_dummy_function;
|
| 98 | }
|
| 99 | EXPORT_SYMBOL_GPL(anon_transport_class_register);
|
| 100 |
|
| 101 | |
| 102 | * anon_transport_class_unregister - unregister an anon class
|
| 103 | *
|
| 104 | * @atc: Pointer to the anon transport class to unregister
|
| 105 | *
|
| 106 | * Must be called prior to deallocating the memory for the anon
|
| 107 | * transport class.
|
| 108 |
|
| 109 | void anon_transport_class_unregister(struct anon_transport_class *atc)
|
| 110 | {
|
| 111 | if (unlikely(attribute_container_unregister(&atc->container)))
|
| 112 | BUG();
|
| 113 | }
|
| 114 | EXPORT_SYMBOL_GPL(anon_transport_class_unregister);
|
| 115 |
|
| 116 | static int transport_setup_classdev(struct attribute_container *cont,
|
| 117 | struct device *dev,
|
| 118 | struct device *classdev)
|
| 119 | {
|
| 120 | struct transport_class *tclass = class_to_transport_class(cont->class);
|
| 121 | struct transport_container *tcont = attribute_container_to_transport_container(cont);
|
| 122 |
|
| 123 | if (tclass->setup)
|
| 124 | tclass->setup(tcont, dev, classdev);
|
| 125 |
|
| 126 | return 0;
|
| 127 | }
|
| 128 |
|
| 129 | |
| 130 | * transport_setup_device - declare a new dev for transport class association but don't make it visible yet.
|
| 131 | * @dev: the generic device representing the entity being added
|
| 132 | *
|
| 133 | * Usually, dev represents some component in the HBA system (either
|
| 134 | * the HBA itself or a device remote across the HBA bus). This
|
| 135 | * routine is simply a trigger point to see if any set of transport
|
| 136 | * classes wishes to associate with the added device. This allocates
|
| 137 | * storage for the class device and initialises it, but does not yet
|
| 138 | * add it to the system or add attributes to it (you do this with
|
| 139 | * transport_add_device). If you have no need for a separate setup
|
| 140 | * and add operations, use transport_register_device (see
|
| 141 | * transport_class.h).
|
| 142 |
|
| 143 |
|
| 144 | void transport_setup_device(struct device *dev)
|
| 145 | {
|
| 146 | attribute_container_add_device(dev, transport_setup_classdev);
|
| 147 | }
|
| 148 | EXPORT_SYMBOL_GPL(transport_setup_device);
|
| 149 |
|
| 150 | static int transport_add_class_device(struct attribute_container *cont,
|
| 151 | struct device *dev,
|
| 152 | struct device *classdev)
|
| 153 | {
|
| 154 | struct transport_class *tclass = class_to_transport_class(cont->class);
|
| 155 | int error = attribute_container_add_class_device(classdev);
|
| 156 | struct transport_container *tcont =
|
| 157 | attribute_container_to_transport_container(cont);
|
| 158 |
|
| 159 | if (error)
|
| 160 | goto err_remove;
|
| 161 |
|
| 162 | if (tcont->statistics) {
|
| 163 | error = sysfs_create_group(&classdev->kobj, tcont->statistics);
|
| 164 | if (error)
|
| 165 | goto err_del;
|
| 166 | }
|
| 167 |
|
| 168 | if (tcont->encryption) {
|
| 169 | error = sysfs_create_group(&classdev->kobj, tcont->encryption);
|
| 170 | if (error)
|
| 171 | goto err_del_statistics;
|
| 172 | }
|
| 173 |
|
| 174 | return 0;
|
| 175 |
|
| 176 | err_del_statistics:
|
| 177 | if (tcont->statistics)
|
| 178 | sysfs_remove_group(&classdev->kobj, tcont->statistics);
|
| 179 | err_del:
|
| 180 | attribute_container_class_device_del(classdev);
|
| 181 | err_remove:
|
| 182 | if (tclass->remove)
|
| 183 | tclass->remove(tcont, dev, classdev);
|
| 184 |
|
| 185 | return error;
|
| 186 | }
|
| 187 |
|
| 188 |
|
| 189 | |
| 190 | * transport_add_device - declare a new dev for transport class association
|
| 191 | *
|
| 192 | * @dev: the generic device representing the entity being added
|
| 193 | *
|
| 194 | * Usually, dev represents some component in the HBA system (either
|
| 195 | * the HBA itself or a device remote across the HBA bus). This
|
| 196 | * routine is simply a trigger point used to add the device to the
|
| 197 | * system and register attributes for it.
|
| 198 |
|
| 199 | int transport_add_device(struct device *dev)
|
| 200 | {
|
| 201 | return attribute_container_device_trigger_safe(dev,
|
| 202 | transport_add_class_device,
|
| 203 | transport_remove_classdev);
|
| 204 | }
|
| 205 | EXPORT_SYMBOL_GPL(transport_add_device);
|
| 206 |
|
| 207 | static int transport_configure(struct attribute_container *cont,
|
| 208 | struct device *dev,
|
| 209 | struct device *cdev)
|
| 210 | {
|
| 211 | struct transport_class *tclass = class_to_transport_class(cont->class);
|
| 212 | struct transport_container *tcont = attribute_container_to_transport_container(cont);
|
| 213 |
|
| 214 | if (tclass->configure)
|
| 215 | tclass->configure(tcont, dev, cdev);
|
| 216 |
|
| 217 | return 0;
|
| 218 | }
|
| 219 |
|
| 220 | |
| 221 | * transport_configure_device - configure an already set up device
|
| 222 | *
|
| 223 | * @dev: generic device representing device to be configured
|
| 224 | *
|
| 225 | * The idea of configure is simply to provide a point within the setup
|
| 226 | * process to allow the transport class to extract information from a
|
| 227 | * device after it has been setup. This is used in SCSI because we
|
| 228 | * have to have a setup device to begin using the HBA, but after we
|
| 229 | * send the initial inquiry, we use configure to extract the device
|
| 230 | * parameters. The device need not have been added to be configured.
|
| 231 |
|
| 232 | void transport_configure_device(struct device *dev)
|
| 233 | {
|
| 234 | attribute_container_device_trigger(dev, transport_configure);
|
| 235 | }
|
| 236 | EXPORT_SYMBOL_GPL(transport_configure_device);
|
| 237 |
|
| 238 | static int transport_remove_classdev(struct attribute_container *cont,
|
| 239 | struct device *dev,
|
| 240 | struct device *classdev)
|
| 241 | {
|
| 242 | struct transport_container *tcont =
|
| 243 | attribute_container_to_transport_container(cont);
|
| 244 | struct transport_class *tclass = class_to_transport_class(cont->class);
|
| 245 |
|
| 246 | if (tclass->remove)
|
| 247 | tclass->remove(tcont, dev, classdev);
|
| 248 |
|
| 249 | if (tclass->remove != anon_transport_dummy_function) {
|
| 250 | if (tcont->statistics)
|
| 251 | sysfs_remove_group(&classdev->kobj, tcont->statistics);
|
| 252 | if (tcont->encryption)
|
| 253 | sysfs_remove_group(&classdev->kobj, tcont->encryption);
|
| 254 | attribute_container_class_device_del(classdev);
|
| 255 | }
|
| 256 |
|
| 257 | return 0;
|
| 258 | }
|
| 259 |
|
| 260 |
|
| 261 | |
| 262 | * transport_remove_device - remove the visibility of a device
|
| 263 | *
|
| 264 | * @dev: generic device to remove
|
| 265 | *
|
| 266 | * This call removes the visibility of the device (to the user from
|
| 267 | * sysfs), but does not destroy it. To eliminate a device entirely
|
| 268 | * you must also call transport_destroy_device. If you don't need to
|
| 269 | * do remove and destroy as separate operations, use
|
| 270 | * transport_unregister_device() (see transport_class.h) which will
|
| 271 | * perform both calls for you.
|
| 272 |
|
| 273 | void transport_remove_device(struct device *dev)
|
| 274 | {
|
| 275 | attribute_container_device_trigger(dev, transport_remove_classdev);
|
| 276 | }
|
| 277 | EXPORT_SYMBOL_GPL(transport_remove_device);
|
| 278 |
|
| 279 | static void transport_destroy_classdev(struct attribute_container *cont,
|
| 280 | struct device *dev,
|
| 281 | struct device *classdev)
|
| 282 | {
|
| 283 | struct transport_class *tclass = class_to_transport_class(cont->class);
|
| 284 |
|
| 285 | if (tclass->remove != anon_transport_dummy_function)
|
| 286 | put_device(classdev);
|
| 287 | }
|
| 288 |
|
| 289 |
|
| 290 | |
| 291 | * transport_destroy_device - destroy a removed device
|
| 292 | *
|
| 293 | * @dev: device to eliminate from the transport class.
|
| 294 | *
|
| 295 | * This call triggers the elimination of storage associated with the
|
| 296 | * transport classdev. Note: all it really does is relinquish a
|
| 297 | * reference to the classdev. The memory will not be freed until the
|
| 298 | * last reference goes to zero. Note also that the classdev retains a
|
| 299 | * reference count on dev, so dev too will remain for as long as the
|
| 300 | * transport class device remains around.
|
| 301 |
|
| 302 | void transport_destroy_device(struct device *dev)
|
| 303 | {
|
| 304 | attribute_container_remove_device(dev, transport_destroy_classdev);
|
| 305 | }
|
| 306 | EXPORT_SYMBOL_GPL(transport_destroy_device);
|