| Linux kernel & device driver programming |
| [ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] |
1 /* 1
2 * SCSI RDMA (SRP) transport class
3 *
4 * Copyright (C) 2007 FUJITA Tomonori <tomof@a
5 *
6 * This program is free software; you can redi
7 * modify it under the terms of the GNU Genera
8 * published by the Free Software Foundation,
9 * License.
10 *
11 * This program is distributed in the hope tha
12 * WITHOUT ANY WARRANTY; without even the impl
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU
17 * along with this program; if not, write to t
18 * Foundation, Inc., 51 Franklin St, Fifth Flo
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
35 struct srp_host_attrs {
36 atomic_t next_port_id;
37 };
38 #define to_srp_host_attrs(host) ((struct srp_h
39
40 #define SRP_HOST_ATTRS 0
41 #define SRP_RPORT_ATTRS 2
42
43 struct srp_internal {
44 struct scsi_transport_template t;
45 struct srp_function_template *f;
46
47 struct device_attribute *host_attrs[SR
48
49 struct device_attribute *rport_attrs[S
50 struct device_attribute private_rport_
51 struct transport_container rport_attr_
52 };
53
54 #define to_srp_internal(tmpl) container_of(tmp
55
56 #define dev_to_rport(d) container_of(d, struct
57 #define transport_class_to_srp_rport(dev) dev_
58
59 static int srp_host_setup(struct transport_con
60 struct device *cdev)
61 {
62 struct Scsi_Host *shost = dev_to_shost
63 struct srp_host_attrs *srp_host = to_s
64
65 atomic_set(&srp_host->next_port_id, 0)
66 return 0;
67 }
68
69 static DECLARE_TRANSPORT_CLASS(srp_host_class,
70 NULL, NULL);
71
72 static DECLARE_TRANSPORT_CLASS(srp_rport_class
73 NULL, NULL, NUL
74
75 #define SETUP_TEMPLATE(attrb, field, perm, tes
76 i->private_##attrb[count] = dev_attr_#
77 i->private_##attrb[count].attr.mode =
78 if (ro_test) {
79 i->private_##attrb[count].attr
80 i->private_##attrb[count].stor
81 }
82 i->attrb[count] = &i->private_##attrb[
83 if (test)
84 count++
85
86 #define SETUP_RPORT_ATTRIBUTE_RD(field)
87 SETUP_TEMPLATE(rport_attrs, field, S_I
88
89 #define SETUP_RPORT_ATTRIBUTE_RW(field)
90 SETUP_TEMPLATE(rport_attrs, field, S_I
91 1, 1, S_IRUGO)
92
93 #define SRP_PID(p) \
94 (p)->port_id[0], (p)->port_id[1], (p)-
95 (p)->port_id[4], (p)->port_id[5], (p)-
96 (p)->port_id[8], (p)->port_id[9], (p)-
97 (p)->port_id[12], (p)->port_id[13], (p
98
99 #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:
100 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%0
101
102 static ssize_t
103 show_srp_rport_id(struct device *dev, struct d
104 char *buf)
105 {
106 struct srp_rport *rport = transport_cl
107 return sprintf(buf, SRP_PID_FMT "\n",
108 }
109
110 static DEVICE_ATTR(port_id, S_IRUGO, show_srp_
111
112 static const struct {
113 u32 value;
114 char *name;
115 } srp_rport_role_names[] = {
116 {SRP_RPORT_ROLE_INITIATOR, "SRP Initia
117 {SRP_RPORT_ROLE_TARGET, "SRP Target"},
118 };
119
120 static ssize_t
121 show_srp_rport_roles(struct device *dev, struc
122 char *buf)
123 {
124 struct srp_rport *rport = transport_cl
125 int i;
126 char *name = NULL;
127
128 for (i = 0; i < ARRAY_SIZE(srp_rport_r
129 if (srp_rport_role_names[i].va
130 name = srp_rport_role_
131 break;
132 }
133 return sprintf(buf, "%s\n", name ? : "
134 }
135
136 static DEVICE_ATTR(roles, S_IRUGO, show_srp_rp
137
138 static void srp_rport_release(struct device *d
139 {
140 struct srp_rport *rport = dev_to_rport
141
142 put_device(dev->parent);
143 kfree(rport);
144 }
145
146 static int scsi_is_srp_rport(const struct devi
147 {
148 return dev->release == srp_rport_relea
149 }
150
151 static int srp_rport_match(struct attribute_co
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.c
164 return 0;
165
166 i = to_srp_internal(shost->transportt)
167 return &i->rport_attr_cont.ac == cont;
168 }
169
170 static int srp_host_match(struct attribute_con
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.c
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 th
190 * @shost: scsi host the remote port is c
191 * @ids: The port id for the remote por
192 *
193 * Publishes a port to the rest of the system.
194 */
195 struct srp_rport *srp_rport_add(struct Scsi_Ho
196 struct srp_rpo
197 {
198 struct srp_rport *rport;
199 struct device *parent = &shost->shost_
200 int id, ret;
201
202 rport = kzalloc(sizeof(*rport), GFP_KE
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, s
212 rport->roles = ids->roles;
213
214 id = atomic_inc_return(&to_srp_host_at
215 dev_set_name(&rport->dev, "port-%d:%d"
216
217 transport_setup_device(&rport->dev);
218
219 ret = device_add(&rport->dev);
220 if (ret) {
221 transport_destroy_device(&rpor
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_INITI
228 ret = srp_tgt_it_nexus_create(
229
230 if (ret) {
231 device_del(&rport->dev
232 transport_destroy_devi
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 }
243 EXPORT_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 */
251 void srp_rport_del(struct srp_rport *rport)
252 {
253 struct device *dev = &rport->dev;
254 struct Scsi_Host *shost = dev_to_shost
255
256 if (shost->active_mode & MODE_TARGET &
257 rport->roles == SRP_RPORT_ROLE_INI
258 srp_tgt_it_nexus_destroy(shost
259
260 transport_remove_device(dev);
261 device_del(dev);
262 transport_destroy_device(dev);
263 put_device(dev);
264 }
265 EXPORT_SYMBOL_GPL(srp_rport_del);
266
267 static int do_srp_rport_del(struct device *dev
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
276 * @shost: Scsi Host that is torn down
277 *
278 * Removes all SRP remote ports for a given Sc
279 * Must be called just before scsi_remove_host
280 */
281 void srp_remove_host(struct Scsi_Host *shost)
282 {
283 device_for_each_child(&shost->shost_ge
284 }
285 EXPORT_SYMBOL_GPL(srp_remove_host);
286
287 static int srp_tsk_mgmt_response(struct Scsi_H
288 int result)
289 {
290 struct srp_internal *i = to_srp_intern
291 return i->f->tsk_mgmt_response(shost,
292 }
293
294 static int srp_it_nexus_response(struct Scsi_H
295 {
296 struct srp_internal *i = to_srp_intern
297 return i->f->it_nexus_response(shost,
298 }
299
300 /**
301 * srp_attach_transport - instantiate SRP tr
302 * @ft: SRP transport class function t
303 */
304 struct scsi_transport_template *
305 srp_attach_transport(struct srp_function_templ
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_
315 i->t.it_nexus_response = srp_it_nexus_
316
317 i->t.host_size = sizeof(struct srp_hos
318 i->t.host_attrs.ac.attrs = &i->host_at
319 i->t.host_attrs.ac.class = &srp_host_c
320 i->t.host_attrs.ac.match = srp_host_ma
321 i->host_attrs[0] = NULL;
322 transport_container_register(&i->t.hos
323
324 i->rport_attr_cont.ac.attrs = &i->rpor
325 i->rport_attr_cont.ac.class = &srp_rpo
326 i->rport_attr_cont.ac.match = srp_rpor
327 transport_container_register(&i->rport
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 }
338 EXPORT_SYMBOL_GPL(srp_attach_transport);
339
340 /**
341 * srp_release_transport - release SRP trans
342 * @t: transport template instance
343 */
344 void srp_release_transport(struct scsi_transpo
345 {
346 struct srp_internal *i = to_srp_intern
347
348 transport_container_unregister(&i->t.h
349 transport_container_unregister(&i->rpo
350
351 kfree(i);
352 }
353 EXPORT_SYMBOL_GPL(srp_release_transport);
354
355 static __init int srp_transport_init(void)
356 {
357 int ret;
358
359 ret = transport_class_register(&srp_ho
360 if (ret)
361 return ret;
362 ret = transport_class_register(&srp_rp
363 if (ret)
364 goto unregister_host_class;
365
366 return 0;
367 unregister_host_class:
368 transport_class_unregister(&srp_host_c
369 return ret;
370 }
371
372 static void __exit srp_transport_exit(void)
373 {
374 transport_class_unregister(&srp_host_c
375 transport_class_unregister(&srp_rport_
376 }
377
378 MODULE_AUTHOR("FUJITA Tomonori");
379 MODULE_DESCRIPTION("SRP Transport Attributes")
380 MODULE_LICENSE("GPL");
381
382 module_init(srp_transport_init);
383 module_exit(srp_transport_exit);
384
| This page was automatically generated by the LXR engine. |