/*1* transport_class.c - implementation of generic transport classes2* using attribute_containers3*4* Copyright (c) 2005 - James Bottomley <[email protected]>5*6* This file is licensed under GPLv27*8* The basic idea here is to allow any "device controller" (which9* would most often be a Host Bus Adapter to use the services of one10* or more tranport classes for performing transport specific11* services. Transport specific services are things that the generic12* command layer doesn't want to know about (speed settings, line13* condidtioning, etc), but which the user might be interested in.14* Thus, the HBA's use the routines exported by the transport classes15* to perform these functions. The transport classes export certain16* values to the user via sysfs using attribute containers.17*18* Note: because not every HBA will care about every transport19* attribute, there's a many to one relationship that goes like this:20*21* transport class<-----attribute container<----class device22*23* Usually the attribute container is per-HBA, but the design doesn't24* mandate that. Although most of the services will be specific to25* the actual external storage connection used by the HBA, the generic26* transport class is framed entirely in terms of generic devices to27* allow it to be used by any physical HBA in the system.28*/29#include <linux/attribute_container.h>30#include <linux/transport_class.h>3132/**33* transport_class_register - register an initial transport class34*35* @tclass: a pointer to the transport class structure to be initialised36*37* The transport class contains an embedded class which is used to38* identify it. The caller should initialise this structure with39* zeros and then generic class must have been initialised with the40* actual transport class unique name. There's a macro41* DECLARE_TRANSPORT_CLASS() to do this (declared classes still must42* be registered).43*44* Returns 0 on success or error on failure.45*/46int transport_class_register(struct transport_class *tclass)47{48return class_register(&tclass->class);49}50EXPORT_SYMBOL_GPL(transport_class_register);5152/**53* transport_class_unregister - unregister a previously registered class54*55* @tclass: The transport class to unregister56*57* Must be called prior to deallocating the memory for the transport58* class.59*/60void transport_class_unregister(struct transport_class *tclass)61{62class_unregister(&tclass->class);63}64EXPORT_SYMBOL_GPL(transport_class_unregister);6566static int anon_transport_dummy_function(struct transport_container *tc,67struct device *dev,68struct device *cdev)69{70/* do nothing */71return 0;72}7374/**75* anon_transport_class_register - register an anonymous class76*77* @atc: The anon transport class to register78*79* The anonymous transport class contains both a transport class and a80* container. The idea of an anonymous class is that it never81* actually has any device attributes associated with it (and thus82* saves on container storage). So it can only be used for triggering83* events. Use prezero and then use DECLARE_ANON_TRANSPORT_CLASS() to84* initialise the anon transport class storage.85*/86int anon_transport_class_register(struct anon_transport_class *atc)87{88int error;89atc->container.class = &atc->tclass.class;90attribute_container_set_no_classdevs(&atc->container);91error = attribute_container_register(&atc->container);92if (error)93return error;94atc->tclass.setup = anon_transport_dummy_function;95atc->tclass.remove = anon_transport_dummy_function;96return 0;97}98EXPORT_SYMBOL_GPL(anon_transport_class_register);99100/**101* anon_transport_class_unregister - unregister an anon class102*103* @atc: Pointer to the anon transport class to unregister104*105* Must be called prior to deallocating the memory for the anon106* transport class.107*/108void anon_transport_class_unregister(struct anon_transport_class *atc)109{110if (unlikely(attribute_container_unregister(&atc->container)))111BUG();112}113EXPORT_SYMBOL_GPL(anon_transport_class_unregister);114115static int transport_setup_classdev(struct attribute_container *cont,116struct device *dev,117struct device *classdev)118{119struct transport_class *tclass = class_to_transport_class(cont->class);120struct transport_container *tcont = attribute_container_to_transport_container(cont);121122if (tclass->setup)123tclass->setup(tcont, dev, classdev);124125return 0;126}127128/**129* transport_setup_device - declare a new dev for transport class association but don't make it visible yet.130* @dev: the generic device representing the entity being added131*132* Usually, dev represents some component in the HBA system (either133* the HBA itself or a device remote across the HBA bus). This134* routine is simply a trigger point to see if any set of transport135* classes wishes to associate with the added device. This allocates136* storage for the class device and initialises it, but does not yet137* add it to the system or add attributes to it (you do this with138* transport_add_device). If you have no need for a separate setup139* and add operations, use transport_register_device (see140* transport_class.h).141*/142143void transport_setup_device(struct device *dev)144{145attribute_container_add_device(dev, transport_setup_classdev);146}147EXPORT_SYMBOL_GPL(transport_setup_device);148149static int transport_add_class_device(struct attribute_container *cont,150struct device *dev,151struct device *classdev)152{153int error = attribute_container_add_class_device(classdev);154struct transport_container *tcont =155attribute_container_to_transport_container(cont);156157if (!error && tcont->statistics)158error = sysfs_create_group(&classdev->kobj, tcont->statistics);159160return error;161}162163164/**165* transport_add_device - declare a new dev for transport class association166*167* @dev: the generic device representing the entity being added168*169* Usually, dev represents some component in the HBA system (either170* the HBA itself or a device remote across the HBA bus). This171* routine is simply a trigger point used to add the device to the172* system and register attributes for it.173*/174175void transport_add_device(struct device *dev)176{177attribute_container_device_trigger(dev, transport_add_class_device);178}179EXPORT_SYMBOL_GPL(transport_add_device);180181static int transport_configure(struct attribute_container *cont,182struct device *dev,183struct device *cdev)184{185struct transport_class *tclass = class_to_transport_class(cont->class);186struct transport_container *tcont = attribute_container_to_transport_container(cont);187188if (tclass->configure)189tclass->configure(tcont, dev, cdev);190191return 0;192}193194/**195* transport_configure_device - configure an already set up device196*197* @dev: generic device representing device to be configured198*199* The idea of configure is simply to provide a point within the setup200* process to allow the transport class to extract information from a201* device after it has been setup. This is used in SCSI because we202* have to have a setup device to begin using the HBA, but after we203* send the initial inquiry, we use configure to extract the device204* parameters. The device need not have been added to be configured.205*/206void transport_configure_device(struct device *dev)207{208attribute_container_device_trigger(dev, transport_configure);209}210EXPORT_SYMBOL_GPL(transport_configure_device);211212static int transport_remove_classdev(struct attribute_container *cont,213struct device *dev,214struct device *classdev)215{216struct transport_container *tcont =217attribute_container_to_transport_container(cont);218struct transport_class *tclass = class_to_transport_class(cont->class);219220if (tclass->remove)221tclass->remove(tcont, dev, classdev);222223if (tclass->remove != anon_transport_dummy_function) {224if (tcont->statistics)225sysfs_remove_group(&classdev->kobj, tcont->statistics);226attribute_container_class_device_del(classdev);227}228229return 0;230}231232233/**234* transport_remove_device - remove the visibility of a device235*236* @dev: generic device to remove237*238* This call removes the visibility of the device (to the user from239* sysfs), but does not destroy it. To eliminate a device entirely240* you must also call transport_destroy_device. If you don't need to241* do remove and destroy as separate operations, use242* transport_unregister_device() (see transport_class.h) which will243* perform both calls for you.244*/245void transport_remove_device(struct device *dev)246{247attribute_container_device_trigger(dev, transport_remove_classdev);248}249EXPORT_SYMBOL_GPL(transport_remove_device);250251static void transport_destroy_classdev(struct attribute_container *cont,252struct device *dev,253struct device *classdev)254{255struct transport_class *tclass = class_to_transport_class(cont->class);256257if (tclass->remove != anon_transport_dummy_function)258put_device(classdev);259}260261262/**263* transport_destroy_device - destroy a removed device264*265* @dev: device to eliminate from the transport class.266*267* This call triggers the elimination of storage associated with the268* transport classdev. Note: all it really does is relinquish a269* reference to the classdev. The memory will not be freed until the270* last reference goes to zero. Note also that the classdev retains a271* reference count on dev, so dev too will remain for as long as the272* transport class device remains around.273*/274void transport_destroy_device(struct device *dev)275{276attribute_container_remove_device(dev, transport_destroy_classdev);277}278EXPORT_SYMBOL_GPL(transport_destroy_device);279280281