// SPDX-License-Identifier: GPL-2.01/*2* transport_class.c - implementation of generic transport classes3* using attribute_containers4*5* Copyright (c) 2005 - James Bottomley <[email protected]>6*7* The basic idea here is to allow any "device controller" (which8* would most often be a Host Bus Adapter to use the services of one9* or more tranport classes for performing transport specific10* services. Transport specific services are things that the generic11* command layer doesn't want to know about (speed settings, line12* condidtioning, etc), but which the user might be interested in.13* Thus, the HBA's use the routines exported by the transport classes14* to perform these functions. The transport classes export certain15* values to the user via sysfs using attribute containers.16*17* Note: because not every HBA will care about every transport18* attribute, there's a many to one relationship that goes like this:19*20* transport class<-----attribute container<----class device21*22* Usually the attribute container is per-HBA, but the design doesn't23* mandate that. Although most of the services will be specific to24* the actual external storage connection used by the HBA, the generic25* transport class is framed entirely in terms of generic devices to26* allow it to be used by any physical HBA in the system.27*/28#include <linux/export.h>29#include <linux/attribute_container.h>30#include <linux/transport_class.h>3132static int transport_remove_classdev(struct attribute_container *cont,33struct device *dev,34struct device *classdev);3536/**37* transport_class_register - register an initial transport class38*39* @tclass: a pointer to the transport class structure to be initialised40*41* The transport class contains an embedded class which is used to42* identify it. The caller should initialise this structure with43* zeros and then generic class must have been initialised with the44* actual transport class unique name. There's a macro45* DECLARE_TRANSPORT_CLASS() to do this (declared classes still must46* be registered).47*48* Returns 0 on success or error on failure.49*/50int transport_class_register(struct transport_class *tclass)51{52return class_register(&tclass->class);53}54EXPORT_SYMBOL_GPL(transport_class_register);5556/**57* transport_class_unregister - unregister a previously registered class58*59* @tclass: The transport class to unregister60*61* Must be called prior to deallocating the memory for the transport62* class.63*/64void transport_class_unregister(struct transport_class *tclass)65{66class_unregister(&tclass->class);67}68EXPORT_SYMBOL_GPL(transport_class_unregister);6970static int anon_transport_dummy_function(struct transport_container *tc,71struct device *dev,72struct device *cdev)73{74/* do nothing */75return 0;76}7778/**79* anon_transport_class_register - register an anonymous class80*81* @atc: The anon transport class to register82*83* The anonymous transport class contains both a transport class and a84* container. The idea of an anonymous class is that it never85* actually has any device attributes associated with it (and thus86* saves on container storage). So it can only be used for triggering87* events. Use prezero and then use DECLARE_ANON_TRANSPORT_CLASS() to88* initialise the anon transport class storage.89*/90int anon_transport_class_register(struct anon_transport_class *atc)91{92int error;93atc->container.class = &atc->tclass.class;94attribute_container_set_no_classdevs(&atc->container);95error = attribute_container_register(&atc->container);96if (error)97return error;98atc->tclass.setup = anon_transport_dummy_function;99atc->tclass.remove = anon_transport_dummy_function;100return 0;101}102EXPORT_SYMBOL_GPL(anon_transport_class_register);103104/**105* anon_transport_class_unregister - unregister an anon class106*107* @atc: Pointer to the anon transport class to unregister108*109* Must be called prior to deallocating the memory for the anon110* transport class.111*/112void anon_transport_class_unregister(struct anon_transport_class *atc)113{114if (unlikely(attribute_container_unregister(&atc->container)))115BUG();116}117EXPORT_SYMBOL_GPL(anon_transport_class_unregister);118119static int transport_setup_classdev(struct attribute_container *cont,120struct device *dev,121struct device *classdev)122{123struct transport_class *tclass = class_to_transport_class(cont->class);124struct transport_container *tcont = attribute_container_to_transport_container(cont);125126if (tclass->setup)127tclass->setup(tcont, dev, classdev);128129return 0;130}131132/**133* transport_setup_device - declare a new dev for transport class association but don't make it visible yet.134* @dev: the generic device representing the entity being added135*136* Usually, dev represents some component in the HBA system (either137* the HBA itself or a device remote across the HBA bus). This138* routine is simply a trigger point to see if any set of transport139* classes wishes to associate with the added device. This allocates140* storage for the class device and initialises it, but does not yet141* add it to the system or add attributes to it (you do this with142* transport_add_device). If you have no need for a separate setup143* and add operations, use transport_register_device (see144* transport_class.h).145*/146147void transport_setup_device(struct device *dev)148{149attribute_container_add_device(dev, transport_setup_classdev);150}151EXPORT_SYMBOL_GPL(transport_setup_device);152153static int transport_add_class_device(struct attribute_container *cont,154struct device *dev,155struct device *classdev)156{157struct transport_class *tclass = class_to_transport_class(cont->class);158int error = attribute_container_add_class_device(classdev);159struct transport_container *tcont =160attribute_container_to_transport_container(cont);161162if (error)163goto err_remove;164165if (tcont->statistics) {166error = sysfs_create_group(&classdev->kobj, tcont->statistics);167if (error)168goto err_del;169}170171return 0;172173err_del:174attribute_container_class_device_del(classdev);175err_remove:176if (tclass->remove)177tclass->remove(tcont, dev, classdev);178179return error;180}181182183/**184* transport_add_device - declare a new dev for transport class association185*186* @dev: the generic device representing the entity being added187*188* Usually, dev represents some component in the HBA system (either189* the HBA itself or a device remote across the HBA bus). This190* routine is simply a trigger point used to add the device to the191* system and register attributes for it.192*/193int transport_add_device(struct device *dev)194{195return attribute_container_device_trigger_safe(dev,196transport_add_class_device,197transport_remove_classdev);198}199EXPORT_SYMBOL_GPL(transport_add_device);200201static int transport_configure(struct attribute_container *cont,202struct device *dev,203struct device *cdev)204{205struct transport_class *tclass = class_to_transport_class(cont->class);206struct transport_container *tcont = attribute_container_to_transport_container(cont);207208if (tclass->configure)209tclass->configure(tcont, dev, cdev);210211return 0;212}213214/**215* transport_configure_device - configure an already set up device216*217* @dev: generic device representing device to be configured218*219* The idea of configure is simply to provide a point within the setup220* process to allow the transport class to extract information from a221* device after it has been setup. This is used in SCSI because we222* have to have a setup device to begin using the HBA, but after we223* send the initial inquiry, we use configure to extract the device224* parameters. The device need not have been added to be configured.225*/226void transport_configure_device(struct device *dev)227{228attribute_container_device_trigger(dev, transport_configure);229}230EXPORT_SYMBOL_GPL(transport_configure_device);231232static int transport_remove_classdev(struct attribute_container *cont,233struct device *dev,234struct device *classdev)235{236struct transport_container *tcont =237attribute_container_to_transport_container(cont);238struct transport_class *tclass = class_to_transport_class(cont->class);239240if (tclass->remove)241tclass->remove(tcont, dev, classdev);242243if (tclass->remove != anon_transport_dummy_function) {244if (tcont->statistics)245sysfs_remove_group(&classdev->kobj, tcont->statistics);246attribute_container_class_device_del(classdev);247}248249return 0;250}251252253/**254* transport_remove_device - remove the visibility of a device255*256* @dev: generic device to remove257*258* This call removes the visibility of the device (to the user from259* sysfs), but does not destroy it. To eliminate a device entirely260* you must also call transport_destroy_device. If you don't need to261* do remove and destroy as separate operations, use262* transport_unregister_device() (see transport_class.h) which will263* perform both calls for you.264*/265void transport_remove_device(struct device *dev)266{267attribute_container_device_trigger(dev, transport_remove_classdev);268}269EXPORT_SYMBOL_GPL(transport_remove_device);270271static void transport_destroy_classdev(struct attribute_container *cont,272struct device *dev,273struct device *classdev)274{275struct transport_class *tclass = class_to_transport_class(cont->class);276277if (tclass->remove != anon_transport_dummy_function)278put_device(classdev);279}280281282/**283* transport_destroy_device - destroy a removed device284*285* @dev: device to eliminate from the transport class.286*287* This call triggers the elimination of storage associated with the288* transport classdev. Note: all it really does is relinquish a289* reference to the classdev. The memory will not be freed until the290* last reference goes to zero. Note also that the classdev retains a291* reference count on dev, so dev too will remain for as long as the292* transport class device remains around.293*/294void transport_destroy_device(struct device *dev)295{296attribute_container_remove_device(dev, transport_destroy_classdev);297}298EXPORT_SYMBOL_GPL(transport_destroy_device);299300301