blob: 0d85f797141f926769268b4edb1c3f14ba9c594a [file] [log] [blame]
FUJITA Tomonori09345f62007-06-27 16:32:39 +09001/*
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>
FUJITA Tomonori0012fdf2007-08-02 00:20:34 +090033#include "scsi_transport_srp_internal.h"
FUJITA Tomonori09345f62007-06-27 16:32:39 +090034
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
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +090041#define SRP_RPORT_ATTRS 2
FUJITA Tomonori09345f62007-06-27 16:32:39 +090042
43struct srp_internal {
44 struct scsi_transport_template t;
45 struct srp_function_template *f;
46
Tony Jonesee959b02008-02-22 00:13:36 +010047 struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
FUJITA Tomonori09345f62007-06-27 16:32:39 +090048
Tony Jonesee959b02008-02-22 00:13:36 +010049 struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
FUJITA Tomonori09345f62007-06-27 16:32:39 +090050 struct transport_container rport_attr_cont;
51};
52
53#define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
54
55#define dev_to_rport(d) container_of(d, struct srp_rport, dev)
Tony Jonesee959b02008-02-22 00:13:36 +010056#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
FUJITA Tomonori09345f62007-06-27 16:32:39 +090057
58static int srp_host_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +010059 struct device *cdev)
FUJITA Tomonori09345f62007-06-27 16:32:39 +090060{
61 struct Scsi_Host *shost = dev_to_shost(dev);
62 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
63
64 atomic_set(&srp_host->next_port_id, 0);
65 return 0;
66}
67
68static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
69 NULL, NULL);
70
71static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
72 NULL, NULL, NULL);
73
FUJITA Tomonori09345f62007-06-27 16:32:39 +090074#define SRP_PID(p) \
75 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
76 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
77 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
78 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
79
80#define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
81 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
82
83static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +010084show_srp_rport_id(struct device *dev, struct device_attribute *attr,
85 char *buf)
FUJITA Tomonori09345f62007-06-27 16:32:39 +090086{
Tony Jonesee959b02008-02-22 00:13:36 +010087 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonori09345f62007-06-27 16:32:39 +090088 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
89}
90
Tony Jonesee959b02008-02-22 00:13:36 +010091static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
FUJITA Tomonori09345f62007-06-27 16:32:39 +090092
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +090093static const struct {
94 u32 value;
95 char *name;
96} srp_rport_role_names[] = {
97 {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
98 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
99};
100
101static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100102show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
103 char *buf)
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900104{
Tony Jonesee959b02008-02-22 00:13:36 +0100105 struct srp_rport *rport = transport_class_to_srp_rport(dev);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900106 int i;
107 char *name = NULL;
108
109 for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
110 if (srp_rport_role_names[i].value == rport->roles) {
111 name = srp_rport_role_names[i].name;
112 break;
113 }
114 return sprintf(buf, "%s\n", name ? : "unknown");
115}
116
Tony Jonesee959b02008-02-22 00:13:36 +0100117static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900118
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900119static void srp_rport_release(struct device *dev)
120{
121 struct srp_rport *rport = dev_to_rport(dev);
122
123 put_device(dev->parent);
124 kfree(rport);
125}
126
127static int scsi_is_srp_rport(const struct device *dev)
128{
129 return dev->release == srp_rport_release;
130}
131
132static int srp_rport_match(struct attribute_container *cont,
133 struct device *dev)
134{
135 struct Scsi_Host *shost;
136 struct srp_internal *i;
137
138 if (!scsi_is_srp_rport(dev))
139 return 0;
140
141 shost = dev_to_shost(dev->parent);
142 if (!shost->transportt)
143 return 0;
144 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
145 return 0;
146
147 i = to_srp_internal(shost->transportt);
148 return &i->rport_attr_cont.ac == cont;
149}
150
151static int srp_host_match(struct attribute_container *cont, struct device *dev)
152{
153 struct Scsi_Host *shost;
154 struct srp_internal *i;
155
156 if (!scsi_is_host_device(dev))
157 return 0;
158
159 shost = dev_to_shost(dev);
160 if (!shost->transportt)
161 return 0;
162 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
163 return 0;
164
165 i = to_srp_internal(shost->transportt);
166 return &i->t.host_attrs.ac == cont;
167}
168
169/**
170 * srp_rport_add - add a SRP remote port to the device hierarchy
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900171 * @shost: scsi host the remote port is connected to.
172 * @ids: The port id for the remote port.
173 *
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800174 * Publishes a port to the rest of the system.
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900175 */
176struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
177 struct srp_rport_identifiers *ids)
178{
179 struct srp_rport *rport;
180 struct device *parent = &shost->shost_gendev;
181 int id, ret;
182
183 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
184 if (!rport)
185 return ERR_PTR(-ENOMEM);
186
187 device_initialize(&rport->dev);
188
189 rport->dev.parent = get_device(parent);
190 rport->dev.release = srp_rport_release;
191
192 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
FUJITA Tomonoriaebd5e42007-07-11 15:08:15 +0900193 rport->roles = ids->roles;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900194
195 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
Kay Sievers71610f52008-12-03 22:41:36 +0100196 dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900197
198 transport_setup_device(&rport->dev);
199
200 ret = device_add(&rport->dev);
201 if (ret) {
202 transport_destroy_device(&rport->dev);
203 put_device(&rport->dev);
204 return ERR_PTR(ret);
205 }
206
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900207 if (shost->active_mode & MODE_TARGET &&
208 ids->roles == SRP_RPORT_ROLE_INITIATOR) {
FUJITA Tomonori0012fdf2007-08-02 00:20:34 +0900209 ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
210 rport->port_id);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900211 if (ret) {
212 device_del(&rport->dev);
213 transport_destroy_device(&rport->dev);
214 put_device(&rport->dev);
215 return ERR_PTR(ret);
216 }
217 }
218
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900219 transport_add_device(&rport->dev);
220 transport_configure_device(&rport->dev);
221
222 return rport;
223}
224EXPORT_SYMBOL_GPL(srp_rport_add);
225
226/**
Rob Landleyeb448202007-11-03 13:30:39 -0500227 * srp_rport_del - remove a SRP remote port
228 * @rport: SRP remote port to remove
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900229 *
230 * Removes the specified SRP remote port.
231 */
232void srp_rport_del(struct srp_rport *rport)
233{
234 struct device *dev = &rport->dev;
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900235 struct Scsi_Host *shost = dev_to_shost(dev->parent);
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900236
FUJITA Tomonori72e39ea2007-09-01 02:03:39 +0900237 if (shost->active_mode & MODE_TARGET &&
238 rport->roles == SRP_RPORT_ROLE_INITIATOR)
239 srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900240
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900241 transport_remove_device(dev);
242 device_del(dev);
243 transport_destroy_device(dev);
244 put_device(dev);
245}
246EXPORT_SYMBOL_GPL(srp_rport_del);
247
248static int do_srp_rport_del(struct device *dev, void *data)
249{
Dave Dillow91183342008-01-03 21:34:49 -0500250 if (scsi_is_srp_rport(dev))
251 srp_rport_del(dev_to_rport(dev));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900252 return 0;
253}
254
255/**
Rob Landleyeb448202007-11-03 13:30:39 -0500256 * srp_remove_host - tear down a Scsi_Host's SRP data structures
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900257 * @shost: Scsi Host that is torn down
258 *
259 * Removes all SRP remote ports for a given Scsi_Host.
260 * Must be called just before scsi_remove_host for SRP HBAs.
261 */
262void srp_remove_host(struct Scsi_Host *shost)
263{
264 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
265}
266EXPORT_SYMBOL_GPL(srp_remove_host);
267
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900268static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
269 int result)
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900270{
271 struct srp_internal *i = to_srp_internal(shost->transportt);
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900272 return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
273}
274
275static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
276{
277 struct srp_internal *i = to_srp_internal(shost->transportt);
278 return i->f->it_nexus_response(shost, nexus, result);
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900279}
280
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900281/**
Rob Landleyeb448202007-11-03 13:30:39 -0500282 * srp_attach_transport - instantiate SRP transport template
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900283 * @ft: SRP transport class function template
284 */
285struct scsi_transport_template *
286srp_attach_transport(struct srp_function_template *ft)
287{
288 int count;
289 struct srp_internal *i;
290
291 i = kzalloc(sizeof(*i), GFP_KERNEL);
292 if (!i)
293 return NULL;
294
FUJITA Tomonoribfb74372007-07-11 15:08:22 +0900295 i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
FUJITA Tomonori62fe8822007-07-11 15:08:19 +0900296 i->t.it_nexus_response = srp_it_nexus_response;
297
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900298 i->t.host_size = sizeof(struct srp_host_attrs);
299 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
300 i->t.host_attrs.ac.class = &srp_host_class.class;
301 i->t.host_attrs.ac.match = srp_host_match;
302 i->host_attrs[0] = NULL;
303 transport_container_register(&i->t.host_attrs);
304
305 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
306 i->rport_attr_cont.ac.class = &srp_rport_class.class;
307 i->rport_attr_cont.ac.match = srp_rport_match;
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900308
309 count = 0;
Bart Van Assche9134a852011-09-10 16:31:57 +0200310 i->rport_attrs[count++] = &dev_attr_port_id;
311 i->rport_attrs[count++] = &dev_attr_roles;
312 i->rport_attrs[count++] = NULL;
313 BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900314
Bart Van Asscheac9be302011-10-21 19:31:07 +0200315 transport_container_register(&i->rport_attr_cont);
316
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900317 i->f = ft;
318
319 return &i->t;
320}
321EXPORT_SYMBOL_GPL(srp_attach_transport);
322
323/**
Rob Landleyeb448202007-11-03 13:30:39 -0500324 * srp_release_transport - release SRP transport template instance
FUJITA Tomonori09345f62007-06-27 16:32:39 +0900325 * @t: transport template instance
326 */
327void srp_release_transport(struct scsi_transport_template *t)
328{
329 struct srp_internal *i = to_srp_internal(t);
330
331 transport_container_unregister(&i->t.host_attrs);
332 transport_container_unregister(&i->rport_attr_cont);
333
334 kfree(i);
335}
336EXPORT_SYMBOL_GPL(srp_release_transport);
337
338static __init int srp_transport_init(void)
339{
340 int ret;
341
342 ret = transport_class_register(&srp_host_class);
343 if (ret)
344 return ret;
345 ret = transport_class_register(&srp_rport_class);
346 if (ret)
347 goto unregister_host_class;
348
349 return 0;
350unregister_host_class:
351 transport_class_unregister(&srp_host_class);
352 return ret;
353}
354
355static void __exit srp_transport_exit(void)
356{
357 transport_class_unregister(&srp_host_class);
358 transport_class_unregister(&srp_rport_class);
359}
360
361MODULE_AUTHOR("FUJITA Tomonori");
362MODULE_DESCRIPTION("SRP Transport Attributes");
363MODULE_LICENSE("GPL");
364
365module_init(srp_transport_init);
366module_exit(srp_transport_exit);