Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * SCSI RDMA (SRP) transport class
  3 *
  4 * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License as
  8 * published by the Free Software Foundation, version 2 of the
  9 * License.
 10 *
 11 * This program is distributed in the hope that it will be useful, but
 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14 * General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 19 * 02110-1301 USA
 20 */
 21#include <linux/init.h>
 22#include <linux/module.h>
 23#include <linux/jiffies.h>
 24#include <linux/err.h>
 25#include <linux/slab.h>
 26#include <linux/string.h>
 27
 28#include <scsi/scsi.h>
 
 29#include <scsi/scsi_device.h>
 30#include <scsi/scsi_host.h>
 31#include <scsi/scsi_transport.h>
 32#include <scsi/scsi_transport_srp.h>
 33#include "scsi_transport_srp_internal.h"
 34
 35struct srp_host_attrs {
 36	atomic_t next_port_id;
 37};
 38#define to_srp_host_attrs(host)	((struct srp_host_attrs *)(host)->shost_data)
 39
 40#define SRP_HOST_ATTRS 0
 41#define SRP_RPORT_ATTRS 2
 42
 43struct srp_internal {
 44	struct scsi_transport_template t;
 45	struct srp_function_template *f;
 46
 47	struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
 48
 49	struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
 50	struct device_attribute private_rport_attrs[SRP_RPORT_ATTRS];
 51	struct transport_container rport_attr_cont;
 52};
 53
 54#define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
 55
 56#define	dev_to_rport(d)	container_of(d, struct srp_rport, dev)
 57#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 58
 59static int srp_host_setup(struct transport_container *tc, struct device *dev,
 60			  struct device *cdev)
 61{
 62	struct Scsi_Host *shost = dev_to_shost(dev);
 63	struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
 64
 65	atomic_set(&srp_host->next_port_id, 0);
 66	return 0;
 67}
 68
 69static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
 70			       NULL, NULL);
 71
 72static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
 73			       NULL, NULL, NULL);
 74
 75#define SETUP_TEMPLATE(attrb, field, perm, test, ro_test, ro_perm)	\
 76	i->private_##attrb[count] = dev_attr_##field;		\
 77	i->private_##attrb[count].attr.mode = perm;			\
 78	if (ro_test) {							\
 79		i->private_##attrb[count].attr.mode = ro_perm;		\
 80		i->private_##attrb[count].store = NULL;			\
 81	}								\
 82	i->attrb[count] = &i->private_##attrb[count];			\
 83	if (test)							\
 84		count++
 85
 86#define SETUP_RPORT_ATTRIBUTE_RD(field)					\
 87	SETUP_TEMPLATE(rport_attrs, field, S_IRUGO, 1, 0, 0)
 88
 89#define SETUP_RPORT_ATTRIBUTE_RW(field)					\
 90	SETUP_TEMPLATE(rport_attrs, field, S_IRUGO | S_IWUSR,		\
 91		       1, 1, S_IRUGO)
 92
 93#define SRP_PID(p) \
 94	(p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
 95	(p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
 96	(p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
 97	(p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
 98
 99#define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
100	"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
101
102static ssize_t
103show_srp_rport_id(struct device *dev, struct device_attribute *attr,
104		  char *buf)
105{
106	struct srp_rport *rport = transport_class_to_srp_rport(dev);
107	return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
108}
109
110static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
111
112static const struct {
113	u32 value;
114	char *name;
115} srp_rport_role_names[] = {
116	{SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
117	{SRP_RPORT_ROLE_TARGET, "SRP Target"},
118};
119
120static ssize_t
121show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
122		     char *buf)
123{
124	struct srp_rport *rport = transport_class_to_srp_rport(dev);
125	int i;
126	char *name = NULL;
127
128	for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
129		if (srp_rport_role_names[i].value == rport->roles) {
130			name = srp_rport_role_names[i].name;
131			break;
132		}
133	return sprintf(buf, "%s\n", name ? : "unknown");
134}
135
136static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138static void srp_rport_release(struct device *dev)
139{
140	struct srp_rport *rport = dev_to_rport(dev);
141
142	put_device(dev->parent);
143	kfree(rport);
144}
145
146static int scsi_is_srp_rport(const struct device *dev)
147{
148	return dev->release == srp_rport_release;
149}
150
151static int srp_rport_match(struct attribute_container *cont,
152			   struct device *dev)
153{
154	struct Scsi_Host *shost;
155	struct srp_internal *i;
156
157	if (!scsi_is_srp_rport(dev))
158		return 0;
159
160	shost = dev_to_shost(dev->parent);
161	if (!shost->transportt)
162		return 0;
163	if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
164		return 0;
165
166	i = to_srp_internal(shost->transportt);
167	return &i->rport_attr_cont.ac == cont;
168}
169
170static int srp_host_match(struct attribute_container *cont, struct device *dev)
171{
172	struct Scsi_Host *shost;
173	struct srp_internal *i;
174
175	if (!scsi_is_host_device(dev))
176		return 0;
177
178	shost = dev_to_shost(dev);
179	if (!shost->transportt)
180		return 0;
181	if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
182		return 0;
183
184	i = to_srp_internal(shost->transportt);
185	return &i->t.host_attrs.ac == cont;
186}
187
188/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189 * srp_rport_add - add a SRP remote port to the device hierarchy
190 * @shost:	scsi host the remote port is connected to.
191 * @ids:	The port id for the remote port.
192 *
193 * Publishes a port to the rest of the system.
194 */
195struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
196				struct srp_rport_identifiers *ids)
197{
198	struct srp_rport *rport;
199	struct device *parent = &shost->shost_gendev;
 
200	int id, ret;
201
202	rport = kzalloc(sizeof(*rport), GFP_KERNEL);
203	if (!rport)
204		return ERR_PTR(-ENOMEM);
205
 
 
206	device_initialize(&rport->dev);
207
208	rport->dev.parent = get_device(parent);
209	rport->dev.release = srp_rport_release;
210
211	memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
212	rport->roles = ids->roles;
213
 
 
 
 
 
 
 
 
 
 
 
214	id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
215	dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
216
217	transport_setup_device(&rport->dev);
218
219	ret = device_add(&rport->dev);
220	if (ret) {
221		transport_destroy_device(&rport->dev);
222		put_device(&rport->dev);
223		return ERR_PTR(ret);
224	}
225
226	if (shost->active_mode & MODE_TARGET &&
227	    ids->roles == SRP_RPORT_ROLE_INITIATOR) {
228		ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
229					      rport->port_id);
230		if (ret) {
231			device_del(&rport->dev);
232			transport_destroy_device(&rport->dev);
233			put_device(&rport->dev);
234			return ERR_PTR(ret);
235		}
236	}
237
238	transport_add_device(&rport->dev);
239	transport_configure_device(&rport->dev);
240
241	return rport;
242}
243EXPORT_SYMBOL_GPL(srp_rport_add);
244
245/**
246 * srp_rport_del  -  remove a SRP remote port
247 * @rport:	SRP remote port to remove
248 *
249 * Removes the specified SRP remote port.
250 */
251void srp_rport_del(struct srp_rport *rport)
252{
253	struct device *dev = &rport->dev;
254	struct Scsi_Host *shost = dev_to_shost(dev->parent);
255
256	if (shost->active_mode & MODE_TARGET &&
257	    rport->roles == SRP_RPORT_ROLE_INITIATOR)
258		srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
259
260	transport_remove_device(dev);
261	device_del(dev);
262	transport_destroy_device(dev);
 
263	put_device(dev);
264}
265EXPORT_SYMBOL_GPL(srp_rport_del);
266
267static int do_srp_rport_del(struct device *dev, void *data)
268{
269	if (scsi_is_srp_rport(dev))
270		srp_rport_del(dev_to_rport(dev));
271	return 0;
272}
273
274/**
275 * srp_remove_host  -  tear down a Scsi_Host's SRP data structures
276 * @shost:	Scsi Host that is torn down
277 *
278 * Removes all SRP remote ports for a given Scsi_Host.
279 * Must be called just before scsi_remove_host for SRP HBAs.
280 */
281void srp_remove_host(struct Scsi_Host *shost)
282{
283	device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
284}
285EXPORT_SYMBOL_GPL(srp_remove_host);
286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
288				 int result)
289{
290	struct srp_internal *i = to_srp_internal(shost->transportt);
291	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
292}
293
294static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
295{
296	struct srp_internal *i = to_srp_internal(shost->transportt);
297	return i->f->it_nexus_response(shost, nexus, result);
298}
299
300/**
301 * srp_attach_transport  -  instantiate SRP transport template
302 * @ft:		SRP transport class function template
303 */
304struct scsi_transport_template *
305srp_attach_transport(struct srp_function_template *ft)
306{
307	int count;
308	struct srp_internal *i;
309
310	i = kzalloc(sizeof(*i), GFP_KERNEL);
311	if (!i)
312		return NULL;
313
 
 
314	i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
315	i->t.it_nexus_response = srp_it_nexus_response;
316
317	i->t.host_size = sizeof(struct srp_host_attrs);
318	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
319	i->t.host_attrs.ac.class = &srp_host_class.class;
320	i->t.host_attrs.ac.match = srp_host_match;
321	i->host_attrs[0] = NULL;
322	transport_container_register(&i->t.host_attrs);
323
324	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
325	i->rport_attr_cont.ac.class = &srp_rport_class.class;
326	i->rport_attr_cont.ac.match = srp_rport_match;
327	transport_container_register(&i->rport_attr_cont);
328
329	count = 0;
330	SETUP_RPORT_ATTRIBUTE_RD(port_id);
331	SETUP_RPORT_ATTRIBUTE_RD(roles);
332	i->rport_attrs[count] = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334	i->f = ft;
335
336	return &i->t;
337}
338EXPORT_SYMBOL_GPL(srp_attach_transport);
339
340/**
341 * srp_release_transport  -  release SRP transport template instance
342 * @t:		transport template instance
343 */
344void srp_release_transport(struct scsi_transport_template *t)
345{
346	struct srp_internal *i = to_srp_internal(t);
347
348	transport_container_unregister(&i->t.host_attrs);
349	transport_container_unregister(&i->rport_attr_cont);
350
351	kfree(i);
352}
353EXPORT_SYMBOL_GPL(srp_release_transport);
354
355static __init int srp_transport_init(void)
356{
357	int ret;
358
359	ret = transport_class_register(&srp_host_class);
360	if (ret)
361		return ret;
362	ret = transport_class_register(&srp_rport_class);
363	if (ret)
364		goto unregister_host_class;
365
366	return 0;
367unregister_host_class:
368	transport_class_unregister(&srp_host_class);
369	return ret;
370}
371
372static void __exit srp_transport_exit(void)
373{
374	transport_class_unregister(&srp_host_class);
375	transport_class_unregister(&srp_rport_class);
376}
377
378MODULE_AUTHOR("FUJITA Tomonori");
379MODULE_DESCRIPTION("SRP Transport Attributes");
380MODULE_LICENSE("GPL");
381
382module_init(srp_transport_init);
383module_exit(srp_transport_exit);
v4.10.11
  1/*
  2 * SCSI RDMA (SRP) transport class
  3 *
  4 * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License as
  8 * published by the Free Software Foundation, version 2 of the
  9 * License.
 10 *
 11 * This program is distributed in the hope that it will be useful, but
 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14 * General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 19 * 02110-1301 USA
 20 */
 21#include <linux/init.h>
 22#include <linux/module.h>
 23#include <linux/jiffies.h>
 24#include <linux/err.h>
 25#include <linux/slab.h>
 26#include <linux/string.h>
 27
 28#include <scsi/scsi.h>
 29#include <scsi/scsi_cmnd.h>
 30#include <scsi/scsi_device.h>
 31#include <scsi/scsi_host.h>
 32#include <scsi/scsi_transport.h>
 33#include <scsi/scsi_transport_srp.h>
 34#include "scsi_priv.h"
 35
 36struct srp_host_attrs {
 37	atomic_t next_port_id;
 38};
 39#define to_srp_host_attrs(host)	((struct srp_host_attrs *)(host)->shost_data)
 40
 41#define SRP_HOST_ATTRS 0
 42#define SRP_RPORT_ATTRS 8
 43
 44struct srp_internal {
 45	struct scsi_transport_template t;
 46	struct srp_function_template *f;
 47
 48	struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
 49
 50	struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
 
 51	struct transport_container rport_attr_cont;
 52};
 53
 54#define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
 55
 56#define	dev_to_rport(d)	container_of(d, struct srp_rport, dev)
 57#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
 58static inline struct Scsi_Host *rport_to_shost(struct srp_rport *r)
 59{
 60	return dev_to_shost(r->dev.parent);
 61}
 62
 63static inline struct srp_rport *shost_to_rport(struct Scsi_Host *shost)
 64{
 65	return transport_class_to_srp_rport(&shost->shost_gendev);
 66}
 67
 68/**
 69 * srp_tmo_valid() - check timeout combination validity
 70 * @reconnect_delay: Reconnect delay in seconds.
 71 * @fast_io_fail_tmo: Fast I/O fail timeout in seconds.
 72 * @dev_loss_tmo: Device loss timeout in seconds.
 73 *
 74 * The combination of the timeout parameters must be such that SCSI commands
 75 * are finished in a reasonable time. Hence do not allow the fast I/O fail
 76 * timeout to exceed SCSI_DEVICE_BLOCK_MAX_TIMEOUT nor allow dev_loss_tmo to
 77 * exceed that limit if failing I/O fast has been disabled. Furthermore, these
 78 * parameters must be such that multipath can detect failed paths timely.
 79 * Hence do not allow all three parameters to be disabled simultaneously.
 80 */
 81int srp_tmo_valid(int reconnect_delay, int fast_io_fail_tmo, int dev_loss_tmo)
 82{
 83	if (reconnect_delay < 0 && fast_io_fail_tmo < 0 && dev_loss_tmo < 0)
 84		return -EINVAL;
 85	if (reconnect_delay == 0)
 86		return -EINVAL;
 87	if (fast_io_fail_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
 88		return -EINVAL;
 89	if (fast_io_fail_tmo < 0 &&
 90	    dev_loss_tmo > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
 91		return -EINVAL;
 92	if (dev_loss_tmo >= LONG_MAX / HZ)
 93		return -EINVAL;
 94	if (fast_io_fail_tmo >= 0 && dev_loss_tmo >= 0 &&
 95	    fast_io_fail_tmo >= dev_loss_tmo)
 96		return -EINVAL;
 97	return 0;
 98}
 99EXPORT_SYMBOL_GPL(srp_tmo_valid);
100
101static int srp_host_setup(struct transport_container *tc, struct device *dev,
102			  struct device *cdev)
103{
104	struct Scsi_Host *shost = dev_to_shost(dev);
105	struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
106
107	atomic_set(&srp_host->next_port_id, 0);
108	return 0;
109}
110
111static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
112			       NULL, NULL);
113
114static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
115			       NULL, NULL, NULL);
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117static ssize_t
118show_srp_rport_id(struct device *dev, struct device_attribute *attr,
119		  char *buf)
120{
121	struct srp_rport *rport = transport_class_to_srp_rport(dev);
122	return sprintf(buf, "%16phC\n", rport->port_id);
123}
124
125static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
126
127static const struct {
128	u32 value;
129	char *name;
130} srp_rport_role_names[] = {
131	{SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
132	{SRP_RPORT_ROLE_TARGET, "SRP Target"},
133};
134
135static ssize_t
136show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
137		     char *buf)
138{
139	struct srp_rport *rport = transport_class_to_srp_rport(dev);
140	int i;
141	char *name = NULL;
142
143	for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
144		if (srp_rport_role_names[i].value == rport->roles) {
145			name = srp_rport_role_names[i].name;
146			break;
147		}
148	return sprintf(buf, "%s\n", name ? : "unknown");
149}
150
151static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
152
153static ssize_t store_srp_rport_delete(struct device *dev,
154				      struct device_attribute *attr,
155				      const char *buf, size_t count)
156{
157	struct srp_rport *rport = transport_class_to_srp_rport(dev);
158	struct Scsi_Host *shost = dev_to_shost(dev);
159	struct srp_internal *i = to_srp_internal(shost->transportt);
160
161	if (i->f->rport_delete) {
162		i->f->rport_delete(rport);
163		return count;
164	} else {
165		return -ENOSYS;
166	}
167}
168
169static DEVICE_ATTR(delete, S_IWUSR, NULL, store_srp_rport_delete);
170
171static ssize_t show_srp_rport_state(struct device *dev,
172				    struct device_attribute *attr,
173				    char *buf)
174{
175	static const char *const state_name[] = {
176		[SRP_RPORT_RUNNING]	= "running",
177		[SRP_RPORT_BLOCKED]	= "blocked",
178		[SRP_RPORT_FAIL_FAST]	= "fail-fast",
179		[SRP_RPORT_LOST]	= "lost",
180	};
181	struct srp_rport *rport = transport_class_to_srp_rport(dev);
182	enum srp_rport_state state = rport->state;
183
184	return sprintf(buf, "%s\n",
185		       (unsigned)state < ARRAY_SIZE(state_name) ?
186		       state_name[state] : "???");
187}
188
189static DEVICE_ATTR(state, S_IRUGO, show_srp_rport_state, NULL);
190
191static ssize_t srp_show_tmo(char *buf, int tmo)
192{
193	return tmo >= 0 ? sprintf(buf, "%d\n", tmo) : sprintf(buf, "off\n");
194}
195
196int srp_parse_tmo(int *tmo, const char *buf)
197{
198	int res = 0;
199
200	if (strncmp(buf, "off", 3) != 0)
201		res = kstrtoint(buf, 0, tmo);
202	else
203		*tmo = -1;
204
205	return res;
206}
207EXPORT_SYMBOL(srp_parse_tmo);
208
209static ssize_t show_reconnect_delay(struct device *dev,
210				    struct device_attribute *attr, char *buf)
211{
212	struct srp_rport *rport = transport_class_to_srp_rport(dev);
213
214	return srp_show_tmo(buf, rport->reconnect_delay);
215}
216
217static ssize_t store_reconnect_delay(struct device *dev,
218				     struct device_attribute *attr,
219				     const char *buf, const size_t count)
220{
221	struct srp_rport *rport = transport_class_to_srp_rport(dev);
222	int res, delay;
223
224	res = srp_parse_tmo(&delay, buf);
225	if (res)
226		goto out;
227	res = srp_tmo_valid(delay, rport->fast_io_fail_tmo,
228			    rport->dev_loss_tmo);
229	if (res)
230		goto out;
231
232	if (rport->reconnect_delay <= 0 && delay > 0 &&
233	    rport->state != SRP_RPORT_RUNNING) {
234		queue_delayed_work(system_long_wq, &rport->reconnect_work,
235				   delay * HZ);
236	} else if (delay <= 0) {
237		cancel_delayed_work(&rport->reconnect_work);
238	}
239	rport->reconnect_delay = delay;
240	res = count;
241
242out:
243	return res;
244}
245
246static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR, show_reconnect_delay,
247		   store_reconnect_delay);
248
249static ssize_t show_failed_reconnects(struct device *dev,
250				      struct device_attribute *attr, char *buf)
251{
252	struct srp_rport *rport = transport_class_to_srp_rport(dev);
253
254	return sprintf(buf, "%d\n", rport->failed_reconnects);
255}
256
257static DEVICE_ATTR(failed_reconnects, S_IRUGO, show_failed_reconnects, NULL);
258
259static ssize_t show_srp_rport_fast_io_fail_tmo(struct device *dev,
260					       struct device_attribute *attr,
261					       char *buf)
262{
263	struct srp_rport *rport = transport_class_to_srp_rport(dev);
264
265	return srp_show_tmo(buf, rport->fast_io_fail_tmo);
266}
267
268static ssize_t store_srp_rport_fast_io_fail_tmo(struct device *dev,
269						struct device_attribute *attr,
270						const char *buf, size_t count)
271{
272	struct srp_rport *rport = transport_class_to_srp_rport(dev);
273	int res;
274	int fast_io_fail_tmo;
275
276	res = srp_parse_tmo(&fast_io_fail_tmo, buf);
277	if (res)
278		goto out;
279	res = srp_tmo_valid(rport->reconnect_delay, fast_io_fail_tmo,
280			    rport->dev_loss_tmo);
281	if (res)
282		goto out;
283	rport->fast_io_fail_tmo = fast_io_fail_tmo;
284	res = count;
285
286out:
287	return res;
288}
289
290static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
291		   show_srp_rport_fast_io_fail_tmo,
292		   store_srp_rport_fast_io_fail_tmo);
293
294static ssize_t show_srp_rport_dev_loss_tmo(struct device *dev,
295					   struct device_attribute *attr,
296					   char *buf)
297{
298	struct srp_rport *rport = transport_class_to_srp_rport(dev);
299
300	return srp_show_tmo(buf, rport->dev_loss_tmo);
301}
302
303static ssize_t store_srp_rport_dev_loss_tmo(struct device *dev,
304					    struct device_attribute *attr,
305					    const char *buf, size_t count)
306{
307	struct srp_rport *rport = transport_class_to_srp_rport(dev);
308	int res;
309	int dev_loss_tmo;
310
311	res = srp_parse_tmo(&dev_loss_tmo, buf);
312	if (res)
313		goto out;
314	res = srp_tmo_valid(rport->reconnect_delay, rport->fast_io_fail_tmo,
315			    dev_loss_tmo);
316	if (res)
317		goto out;
318	rport->dev_loss_tmo = dev_loss_tmo;
319	res = count;
320
321out:
322	return res;
323}
324
325static DEVICE_ATTR(dev_loss_tmo, S_IRUGO | S_IWUSR,
326		   show_srp_rport_dev_loss_tmo,
327		   store_srp_rport_dev_loss_tmo);
328
329static int srp_rport_set_state(struct srp_rport *rport,
330			       enum srp_rport_state new_state)
331{
332	enum srp_rport_state old_state = rport->state;
333
334	lockdep_assert_held(&rport->mutex);
335
336	switch (new_state) {
337	case SRP_RPORT_RUNNING:
338		switch (old_state) {
339		case SRP_RPORT_LOST:
340			goto invalid;
341		default:
342			break;
343		}
344		break;
345	case SRP_RPORT_BLOCKED:
346		switch (old_state) {
347		case SRP_RPORT_RUNNING:
348			break;
349		default:
350			goto invalid;
351		}
352		break;
353	case SRP_RPORT_FAIL_FAST:
354		switch (old_state) {
355		case SRP_RPORT_LOST:
356			goto invalid;
357		default:
358			break;
359		}
360		break;
361	case SRP_RPORT_LOST:
362		break;
363	}
364	rport->state = new_state;
365	return 0;
366
367invalid:
368	return -EINVAL;
369}
370
371/**
372 * srp_reconnect_work() - reconnect and schedule a new attempt if necessary
373 * @work: Work structure used for scheduling this operation.
374 */
375static void srp_reconnect_work(struct work_struct *work)
376{
377	struct srp_rport *rport = container_of(to_delayed_work(work),
378					struct srp_rport, reconnect_work);
379	struct Scsi_Host *shost = rport_to_shost(rport);
380	int delay, res;
381
382	res = srp_reconnect_rport(rport);
383	if (res != 0) {
384		shost_printk(KERN_ERR, shost,
385			     "reconnect attempt %d failed (%d)\n",
386			     ++rport->failed_reconnects, res);
387		delay = rport->reconnect_delay *
388			min(100, max(1, rport->failed_reconnects - 10));
389		if (delay > 0)
390			queue_delayed_work(system_long_wq,
391					   &rport->reconnect_work, delay * HZ);
392	}
393}
394
395static void __rport_fail_io_fast(struct srp_rport *rport)
396{
397	struct Scsi_Host *shost = rport_to_shost(rport);
398	struct srp_internal *i;
399
400	lockdep_assert_held(&rport->mutex);
401
402	if (srp_rport_set_state(rport, SRP_RPORT_FAIL_FAST))
403		return;
404	/*
405	 * Call scsi_target_block() to wait for ongoing shost->queuecommand()
406	 * calls before invoking i->f->terminate_rport_io().
407	 */
408	scsi_target_block(rport->dev.parent);
409	scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
410
411	/* Involve the LLD if possible to terminate all I/O on the rport. */
412	i = to_srp_internal(shost->transportt);
413	if (i->f->terminate_rport_io)
414		i->f->terminate_rport_io(rport);
415}
416
417/**
418 * rport_fast_io_fail_timedout() - fast I/O failure timeout handler
419 * @work: Work structure used for scheduling this operation.
420 */
421static void rport_fast_io_fail_timedout(struct work_struct *work)
422{
423	struct srp_rport *rport = container_of(to_delayed_work(work),
424					struct srp_rport, fast_io_fail_work);
425	struct Scsi_Host *shost = rport_to_shost(rport);
426
427	pr_info("fast_io_fail_tmo expired for SRP %s / %s.\n",
428		dev_name(&rport->dev), dev_name(&shost->shost_gendev));
429
430	mutex_lock(&rport->mutex);
431	if (rport->state == SRP_RPORT_BLOCKED)
432		__rport_fail_io_fast(rport);
433	mutex_unlock(&rport->mutex);
434}
435
436/**
437 * rport_dev_loss_timedout() - device loss timeout handler
438 * @work: Work structure used for scheduling this operation.
439 */
440static void rport_dev_loss_timedout(struct work_struct *work)
441{
442	struct srp_rport *rport = container_of(to_delayed_work(work),
443					struct srp_rport, dev_loss_work);
444	struct Scsi_Host *shost = rport_to_shost(rport);
445	struct srp_internal *i = to_srp_internal(shost->transportt);
446
447	pr_info("dev_loss_tmo expired for SRP %s / %s.\n",
448		dev_name(&rport->dev), dev_name(&shost->shost_gendev));
449
450	mutex_lock(&rport->mutex);
451	WARN_ON(srp_rport_set_state(rport, SRP_RPORT_LOST) != 0);
452	scsi_target_unblock(rport->dev.parent, SDEV_TRANSPORT_OFFLINE);
453	mutex_unlock(&rport->mutex);
454
455	i->f->rport_delete(rport);
456}
457
458static void __srp_start_tl_fail_timers(struct srp_rport *rport)
459{
460	struct Scsi_Host *shost = rport_to_shost(rport);
461	int delay, fast_io_fail_tmo, dev_loss_tmo;
462
463	lockdep_assert_held(&rport->mutex);
464
465	delay = rport->reconnect_delay;
466	fast_io_fail_tmo = rport->fast_io_fail_tmo;
467	dev_loss_tmo = rport->dev_loss_tmo;
468	pr_debug("%s current state: %d\n", dev_name(&shost->shost_gendev),
469		 rport->state);
470
471	if (rport->state == SRP_RPORT_LOST)
472		return;
473	if (delay > 0)
474		queue_delayed_work(system_long_wq, &rport->reconnect_work,
475				   1UL * delay * HZ);
476	if ((fast_io_fail_tmo >= 0 || dev_loss_tmo >= 0) &&
477	    srp_rport_set_state(rport, SRP_RPORT_BLOCKED) == 0) {
478		pr_debug("%s new state: %d\n", dev_name(&shost->shost_gendev),
479			 rport->state);
480		scsi_target_block(&shost->shost_gendev);
481		if (fast_io_fail_tmo >= 0)
482			queue_delayed_work(system_long_wq,
483					   &rport->fast_io_fail_work,
484					   1UL * fast_io_fail_tmo * HZ);
485		if (dev_loss_tmo >= 0)
486			queue_delayed_work(system_long_wq,
487					   &rport->dev_loss_work,
488					   1UL * dev_loss_tmo * HZ);
489	}
490}
491
492/**
493 * srp_start_tl_fail_timers() - start the transport layer failure timers
494 * @rport: SRP target port.
495 *
496 * Start the transport layer fast I/O failure and device loss timers. Do not
497 * modify a timer that was already started.
498 */
499void srp_start_tl_fail_timers(struct srp_rport *rport)
500{
501	mutex_lock(&rport->mutex);
502	__srp_start_tl_fail_timers(rport);
503	mutex_unlock(&rport->mutex);
504}
505EXPORT_SYMBOL(srp_start_tl_fail_timers);
506
507/**
508 * srp_reconnect_rport() - reconnect to an SRP target port
509 * @rport: SRP target port.
510 *
511 * Blocks SCSI command queueing before invoking reconnect() such that
512 * queuecommand() won't be invoked concurrently with reconnect() from outside
513 * the SCSI EH. This is important since a reconnect() implementation may
514 * reallocate resources needed by queuecommand().
515 *
516 * Notes:
517 * - This function neither waits until outstanding requests have finished nor
518 *   tries to abort these. It is the responsibility of the reconnect()
519 *   function to finish outstanding commands before reconnecting to the target
520 *   port.
521 * - It is the responsibility of the caller to ensure that the resources
522 *   reallocated by the reconnect() function won't be used while this function
523 *   is in progress. One possible strategy is to invoke this function from
524 *   the context of the SCSI EH thread only. Another possible strategy is to
525 *   lock the rport mutex inside each SCSI LLD callback that can be invoked by
526 *   the SCSI EH (the scsi_host_template.eh_*() functions and also the
527 *   scsi_host_template.queuecommand() function).
528 */
529int srp_reconnect_rport(struct srp_rport *rport)
530{
531	struct Scsi_Host *shost = rport_to_shost(rport);
532	struct srp_internal *i = to_srp_internal(shost->transportt);
533	struct scsi_device *sdev;
534	int res;
535
536	pr_debug("SCSI host %s\n", dev_name(&shost->shost_gendev));
537
538	res = mutex_lock_interruptible(&rport->mutex);
539	if (res)
540		goto out;
541	scsi_target_block(&shost->shost_gendev);
542	res = rport->state != SRP_RPORT_LOST ? i->f->reconnect(rport) : -ENODEV;
543	pr_debug("%s (state %d): transport.reconnect() returned %d\n",
544		 dev_name(&shost->shost_gendev), rport->state, res);
545	if (res == 0) {
546		cancel_delayed_work(&rport->fast_io_fail_work);
547		cancel_delayed_work(&rport->dev_loss_work);
548
549		rport->failed_reconnects = 0;
550		srp_rport_set_state(rport, SRP_RPORT_RUNNING);
551		scsi_target_unblock(&shost->shost_gendev, SDEV_RUNNING);
552		/*
553		 * If the SCSI error handler has offlined one or more devices,
554		 * invoking scsi_target_unblock() won't change the state of
555		 * these devices into running so do that explicitly.
556		 */
557		spin_lock_irq(shost->host_lock);
558		__shost_for_each_device(sdev, shost)
559			if (sdev->sdev_state == SDEV_OFFLINE)
560				sdev->sdev_state = SDEV_RUNNING;
561		spin_unlock_irq(shost->host_lock);
562	} else if (rport->state == SRP_RPORT_RUNNING) {
563		/*
564		 * srp_reconnect_rport() has been invoked with fast_io_fail
565		 * and dev_loss off. Mark the port as failed and start the TL
566		 * failure timers if these had not yet been started.
567		 */
568		__rport_fail_io_fast(rport);
569		scsi_target_unblock(&shost->shost_gendev,
570				    SDEV_TRANSPORT_OFFLINE);
571		__srp_start_tl_fail_timers(rport);
572	} else if (rport->state != SRP_RPORT_BLOCKED) {
573		scsi_target_unblock(&shost->shost_gendev,
574				    SDEV_TRANSPORT_OFFLINE);
575	}
576	mutex_unlock(&rport->mutex);
577
578out:
579	return res;
580}
581EXPORT_SYMBOL(srp_reconnect_rport);
582
583/**
584 * srp_timed_out() - SRP transport intercept of the SCSI timeout EH
585 * @scmd: SCSI command.
586 *
587 * If a timeout occurs while an rport is in the blocked state, ask the SCSI
588 * EH to continue waiting (BLK_EH_RESET_TIMER). Otherwise let the SCSI core
589 * handle the timeout (BLK_EH_NOT_HANDLED).
590 *
591 * Note: This function is called from soft-IRQ context and with the request
592 * queue lock held.
593 */
594static enum blk_eh_timer_return srp_timed_out(struct scsi_cmnd *scmd)
595{
596	struct scsi_device *sdev = scmd->device;
597	struct Scsi_Host *shost = sdev->host;
598	struct srp_internal *i = to_srp_internal(shost->transportt);
599	struct srp_rport *rport = shost_to_rport(shost);
600
601	pr_debug("timeout for sdev %s\n", dev_name(&sdev->sdev_gendev));
602	return rport->fast_io_fail_tmo < 0 && rport->dev_loss_tmo < 0 &&
603		i->f->reset_timer_if_blocked && scsi_device_blocked(sdev) ?
604		BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
605}
606
607static void srp_rport_release(struct device *dev)
608{
609	struct srp_rport *rport = dev_to_rport(dev);
610
611	put_device(dev->parent);
612	kfree(rport);
613}
614
615static int scsi_is_srp_rport(const struct device *dev)
616{
617	return dev->release == srp_rport_release;
618}
619
620static int srp_rport_match(struct attribute_container *cont,
621			   struct device *dev)
622{
623	struct Scsi_Host *shost;
624	struct srp_internal *i;
625
626	if (!scsi_is_srp_rport(dev))
627		return 0;
628
629	shost = dev_to_shost(dev->parent);
630	if (!shost->transportt)
631		return 0;
632	if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
633		return 0;
634
635	i = to_srp_internal(shost->transportt);
636	return &i->rport_attr_cont.ac == cont;
637}
638
639static int srp_host_match(struct attribute_container *cont, struct device *dev)
640{
641	struct Scsi_Host *shost;
642	struct srp_internal *i;
643
644	if (!scsi_is_host_device(dev))
645		return 0;
646
647	shost = dev_to_shost(dev);
648	if (!shost->transportt)
649		return 0;
650	if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
651		return 0;
652
653	i = to_srp_internal(shost->transportt);
654	return &i->t.host_attrs.ac == cont;
655}
656
657/**
658 * srp_rport_get() - increment rport reference count
659 * @rport: SRP target port.
660 */
661void srp_rport_get(struct srp_rport *rport)
662{
663	get_device(&rport->dev);
664}
665EXPORT_SYMBOL(srp_rport_get);
666
667/**
668 * srp_rport_put() - decrement rport reference count
669 * @rport: SRP target port.
670 */
671void srp_rport_put(struct srp_rport *rport)
672{
673	put_device(&rport->dev);
674}
675EXPORT_SYMBOL(srp_rport_put);
676
677/**
678 * srp_rport_add - add a SRP remote port to the device hierarchy
679 * @shost:	scsi host the remote port is connected to.
680 * @ids:	The port id for the remote port.
681 *
682 * Publishes a port to the rest of the system.
683 */
684struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
685				struct srp_rport_identifiers *ids)
686{
687	struct srp_rport *rport;
688	struct device *parent = &shost->shost_gendev;
689	struct srp_internal *i = to_srp_internal(shost->transportt);
690	int id, ret;
691
692	rport = kzalloc(sizeof(*rport), GFP_KERNEL);
693	if (!rport)
694		return ERR_PTR(-ENOMEM);
695
696	mutex_init(&rport->mutex);
697
698	device_initialize(&rport->dev);
699
700	rport->dev.parent = get_device(parent);
701	rport->dev.release = srp_rport_release;
702
703	memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
704	rport->roles = ids->roles;
705
706	if (i->f->reconnect)
707		rport->reconnect_delay = i->f->reconnect_delay ?
708			*i->f->reconnect_delay : 10;
709	INIT_DELAYED_WORK(&rport->reconnect_work, srp_reconnect_work);
710	rport->fast_io_fail_tmo = i->f->fast_io_fail_tmo ?
711		*i->f->fast_io_fail_tmo : 15;
712	rport->dev_loss_tmo = i->f->dev_loss_tmo ? *i->f->dev_loss_tmo : 60;
713	INIT_DELAYED_WORK(&rport->fast_io_fail_work,
714			  rport_fast_io_fail_timedout);
715	INIT_DELAYED_WORK(&rport->dev_loss_work, rport_dev_loss_timedout);
716
717	id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
718	dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
719
720	transport_setup_device(&rport->dev);
721
722	ret = device_add(&rport->dev);
723	if (ret) {
724		transport_destroy_device(&rport->dev);
725		put_device(&rport->dev);
726		return ERR_PTR(ret);
727	}
728
 
 
 
 
 
 
 
 
 
 
 
 
729	transport_add_device(&rport->dev);
730	transport_configure_device(&rport->dev);
731
732	return rport;
733}
734EXPORT_SYMBOL_GPL(srp_rport_add);
735
736/**
737 * srp_rport_del  -  remove a SRP remote port
738 * @rport:	SRP remote port to remove
739 *
740 * Removes the specified SRP remote port.
741 */
742void srp_rport_del(struct srp_rport *rport)
743{
744	struct device *dev = &rport->dev;
 
 
 
 
 
745
746	transport_remove_device(dev);
747	device_del(dev);
748	transport_destroy_device(dev);
749
750	put_device(dev);
751}
752EXPORT_SYMBOL_GPL(srp_rport_del);
753
754static int do_srp_rport_del(struct device *dev, void *data)
755{
756	if (scsi_is_srp_rport(dev))
757		srp_rport_del(dev_to_rport(dev));
758	return 0;
759}
760
761/**
762 * srp_remove_host  -  tear down a Scsi_Host's SRP data structures
763 * @shost:	Scsi Host that is torn down
764 *
765 * Removes all SRP remote ports for a given Scsi_Host.
766 * Must be called just before scsi_remove_host for SRP HBAs.
767 */
768void srp_remove_host(struct Scsi_Host *shost)
769{
770	device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
771}
772EXPORT_SYMBOL_GPL(srp_remove_host);
773
774/**
775 * srp_stop_rport_timers - stop the transport layer recovery timers
776 * @rport: SRP remote port for which to stop the timers.
777 *
778 * Must be called after srp_remove_host() and scsi_remove_host(). The caller
779 * must hold a reference on the rport (rport->dev) and on the SCSI host
780 * (rport->dev.parent).
781 */
782void srp_stop_rport_timers(struct srp_rport *rport)
783{
784	mutex_lock(&rport->mutex);
785	if (rport->state == SRP_RPORT_BLOCKED)
786		__rport_fail_io_fast(rport);
787	srp_rport_set_state(rport, SRP_RPORT_LOST);
788	mutex_unlock(&rport->mutex);
789
790	cancel_delayed_work_sync(&rport->reconnect_work);
791	cancel_delayed_work_sync(&rport->fast_io_fail_work);
792	cancel_delayed_work_sync(&rport->dev_loss_work);
793}
794EXPORT_SYMBOL_GPL(srp_stop_rport_timers);
795
796static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
797				 int result)
798{
799	struct srp_internal *i = to_srp_internal(shost->transportt);
800	return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
801}
802
803static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
804{
805	struct srp_internal *i = to_srp_internal(shost->transportt);
806	return i->f->it_nexus_response(shost, nexus, result);
807}
808
809/**
810 * srp_attach_transport  -  instantiate SRP transport template
811 * @ft:		SRP transport class function template
812 */
813struct scsi_transport_template *
814srp_attach_transport(struct srp_function_template *ft)
815{
816	int count;
817	struct srp_internal *i;
818
819	i = kzalloc(sizeof(*i), GFP_KERNEL);
820	if (!i)
821		return NULL;
822
823	i->t.eh_timed_out = srp_timed_out;
824
825	i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
826	i->t.it_nexus_response = srp_it_nexus_response;
827
828	i->t.host_size = sizeof(struct srp_host_attrs);
829	i->t.host_attrs.ac.attrs = &i->host_attrs[0];
830	i->t.host_attrs.ac.class = &srp_host_class.class;
831	i->t.host_attrs.ac.match = srp_host_match;
832	i->host_attrs[0] = NULL;
833	transport_container_register(&i->t.host_attrs);
834
835	i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
836	i->rport_attr_cont.ac.class = &srp_rport_class.class;
837	i->rport_attr_cont.ac.match = srp_rport_match;
 
838
839	count = 0;
840	i->rport_attrs[count++] = &dev_attr_port_id;
841	i->rport_attrs[count++] = &dev_attr_roles;
842	if (ft->has_rport_state) {
843		i->rport_attrs[count++] = &dev_attr_state;
844		i->rport_attrs[count++] = &dev_attr_fast_io_fail_tmo;
845		i->rport_attrs[count++] = &dev_attr_dev_loss_tmo;
846	}
847	if (ft->reconnect) {
848		i->rport_attrs[count++] = &dev_attr_reconnect_delay;
849		i->rport_attrs[count++] = &dev_attr_failed_reconnects;
850	}
851	if (ft->rport_delete)
852		i->rport_attrs[count++] = &dev_attr_delete;
853	i->rport_attrs[count++] = NULL;
854	BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
855
856	transport_container_register(&i->rport_attr_cont);
857
858	i->f = ft;
859
860	return &i->t;
861}
862EXPORT_SYMBOL_GPL(srp_attach_transport);
863
864/**
865 * srp_release_transport  -  release SRP transport template instance
866 * @t:		transport template instance
867 */
868void srp_release_transport(struct scsi_transport_template *t)
869{
870	struct srp_internal *i = to_srp_internal(t);
871
872	transport_container_unregister(&i->t.host_attrs);
873	transport_container_unregister(&i->rport_attr_cont);
874
875	kfree(i);
876}
877EXPORT_SYMBOL_GPL(srp_release_transport);
878
879static __init int srp_transport_init(void)
880{
881	int ret;
882
883	ret = transport_class_register(&srp_host_class);
884	if (ret)
885		return ret;
886	ret = transport_class_register(&srp_rport_class);
887	if (ret)
888		goto unregister_host_class;
889
890	return 0;
891unregister_host_class:
892	transport_class_unregister(&srp_host_class);
893	return ret;
894}
895
896static void __exit srp_transport_exit(void)
897{
898	transport_class_unregister(&srp_host_class);
899	transport_class_unregister(&srp_rport_class);
900}
901
902MODULE_AUTHOR("FUJITA Tomonori");
903MODULE_DESCRIPTION("SRP Transport Attributes");
904MODULE_LICENSE("GPL");
905
906module_init(srp_transport_init);
907module_exit(srp_transport_exit);