2 * SCSI RDMA (SRP) transport class
4 * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org>
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
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.
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
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>
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>
34 struct srp_host_attrs {
35 atomic_t next_port_id;
37 #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
39 #define SRP_HOST_ATTRS 0
40 #define SRP_RPORT_ATTRS 3
43 struct scsi_transport_template t;
44 struct srp_function_template *f;
46 struct class_device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
48 struct class_device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
49 struct class_device_attribute private_rport_attrs[SRP_RPORT_ATTRS];
50 struct transport_container rport_attr_cont;
53 #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
55 #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
56 #define transport_class_to_srp_rport(cdev) dev_to_rport((cdev)->dev)
58 static int srp_host_setup(struct transport_container *tc, struct device *dev,
59 struct class_device *cdev)
61 struct Scsi_Host *shost = dev_to_shost(dev);
62 struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
64 atomic_set(&srp_host->next_port_id, 0);
68 static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
71 static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
74 #define SETUP_TEMPLATE(attrb, field, perm, test, ro_test, ro_perm) \
75 i->private_##attrb[count] = class_device_attr_##field; \
76 i->private_##attrb[count].attr.mode = perm; \
78 i->private_##attrb[count].attr.mode = ro_perm; \
79 i->private_##attrb[count].store = NULL; \
81 i->attrb[count] = &i->private_##attrb[count]; \
85 #define SETUP_RPORT_ATTRIBUTE_RD(field) \
86 SETUP_TEMPLATE(rport_attrs, field, S_IRUGO, 1, 0, 0)
88 #define SETUP_RPORT_ATTRIBUTE_RW(field) \
89 SETUP_TEMPLATE(rport_attrs, field, S_IRUGO | S_IWUSR, \
93 (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
94 (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
95 (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
96 (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
98 #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
99 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
102 show_srp_rport_id(struct class_device *cdev, char *buf)
104 struct srp_rport *rport = transport_class_to_srp_rport(cdev);
105 return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
108 static CLASS_DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
110 static void srp_rport_release(struct device *dev)
112 struct srp_rport *rport = dev_to_rport(dev);
114 put_device(dev->parent);
118 static int scsi_is_srp_rport(const struct device *dev)
120 return dev->release == srp_rport_release;
123 static int srp_rport_match(struct attribute_container *cont,
126 struct Scsi_Host *shost;
127 struct srp_internal *i;
129 if (!scsi_is_srp_rport(dev))
132 shost = dev_to_shost(dev->parent);
133 if (!shost->transportt)
135 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
138 i = to_srp_internal(shost->transportt);
139 return &i->rport_attr_cont.ac == cont;
142 static int srp_host_match(struct attribute_container *cont, struct device *dev)
144 struct Scsi_Host *shost;
145 struct srp_internal *i;
147 if (!scsi_is_host_device(dev))
150 shost = dev_to_shost(dev);
151 if (!shost->transportt)
153 if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
156 i = to_srp_internal(shost->transportt);
157 return &i->t.host_attrs.ac == cont;
161 * srp_rport_add - add a SRP remote port to the device hierarchy
163 * @shost: scsi host the remote port is connected to.
164 * @ids: The port id for the remote port.
166 * publishes a port to the rest of the system
168 struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
169 struct srp_rport_identifiers *ids)
171 struct srp_rport *rport;
172 struct device *parent = &shost->shost_gendev;
175 rport = kzalloc(sizeof(*rport), GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
179 device_initialize(&rport->dev);
181 rport->dev.parent = get_device(parent);
182 rport->dev.release = srp_rport_release;
184 memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
186 id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
187 sprintf(rport->dev.bus_id, "port-%d:%d", shost->host_no, id);
189 transport_setup_device(&rport->dev);
191 ret = device_add(&rport->dev);
193 transport_destroy_device(&rport->dev);
194 put_device(&rport->dev);
198 transport_add_device(&rport->dev);
199 transport_configure_device(&rport->dev);
203 EXPORT_SYMBOL_GPL(srp_rport_add);
206 * srp_rport_del -- remove a SRP remote port
207 * @port: SRP remote port to remove
209 * Removes the specified SRP remote port.
211 void srp_rport_del(struct srp_rport *rport)
213 struct device *dev = &rport->dev;
215 transport_remove_device(dev);
217 transport_destroy_device(dev);
220 EXPORT_SYMBOL_GPL(srp_rport_del);
222 static int do_srp_rport_del(struct device *dev, void *data)
224 srp_rport_del(dev_to_rport(dev));
229 * srp_remove_host -- tear down a Scsi_Host's SRP data structures
230 * @shost: Scsi Host that is torn down
232 * Removes all SRP remote ports for a given Scsi_Host.
233 * Must be called just before scsi_remove_host for SRP HBAs.
235 void srp_remove_host(struct Scsi_Host *shost)
237 device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
239 EXPORT_SYMBOL_GPL(srp_remove_host);
242 * srp_attach_transport -- instantiate SRP transport template
243 * @ft: SRP transport class function template
245 struct scsi_transport_template *
246 srp_attach_transport(struct srp_function_template *ft)
249 struct srp_internal *i;
251 i = kzalloc(sizeof(*i), GFP_KERNEL);
255 i->t.host_size = sizeof(struct srp_host_attrs);
256 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
257 i->t.host_attrs.ac.class = &srp_host_class.class;
258 i->t.host_attrs.ac.match = srp_host_match;
259 i->host_attrs[0] = NULL;
260 transport_container_register(&i->t.host_attrs);
262 i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
263 i->rport_attr_cont.ac.class = &srp_rport_class.class;
264 i->rport_attr_cont.ac.match = srp_rport_match;
265 transport_container_register(&i->rport_attr_cont);
268 SETUP_RPORT_ATTRIBUTE_RD(port_id);
269 i->rport_attrs[count] = NULL;
275 EXPORT_SYMBOL_GPL(srp_attach_transport);
278 * srp_release_transport -- release SRP transport template instance
279 * @t: transport template instance
281 void srp_release_transport(struct scsi_transport_template *t)
283 struct srp_internal *i = to_srp_internal(t);
285 transport_container_unregister(&i->t.host_attrs);
286 transport_container_unregister(&i->rport_attr_cont);
290 EXPORT_SYMBOL_GPL(srp_release_transport);
292 static __init int srp_transport_init(void)
296 ret = transport_class_register(&srp_host_class);
299 ret = transport_class_register(&srp_rport_class);
301 goto unregister_host_class;
304 unregister_host_class:
305 transport_class_unregister(&srp_host_class);
309 static void __exit srp_transport_exit(void)
311 transport_class_unregister(&srp_host_class);
312 transport_class_unregister(&srp_rport_class);
315 MODULE_AUTHOR("FUJITA Tomonori");
316 MODULE_DESCRIPTION("SRP Transport Attributes");
317 MODULE_LICENSE("GPL");
319 module_init(srp_transport_init);
320 module_exit(srp_transport_exit);