Blame view
drivers/scsi/scsi_transport_srp.c
10.2 KB
09345f650 [SCSI] add srp tr... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* * SCSI RDMA (SRP) transport class * * Copyright (C) 2007 FUJITA Tomonori <tomof@acm.org> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, version 2 of the * License. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA */ #include <linux/init.h> #include <linux/module.h> #include <linux/jiffies.h> #include <linux/err.h> #include <linux/slab.h> #include <linux/string.h> #include <scsi/scsi.h> #include <scsi/scsi_device.h> #include <scsi/scsi_host.h> #include <scsi/scsi_transport.h> #include <scsi/scsi_transport_srp.h> |
0012fdf98 [SCSI] scsi_trans... |
33 |
#include "scsi_transport_srp_internal.h" |
09345f650 [SCSI] add srp tr... |
34 35 36 37 38 39 40 |
struct srp_host_attrs { atomic_t next_port_id; }; #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data) #define SRP_HOST_ATTRS 0 |
aebd5e476 [SCSI] transport_... |
41 |
#define SRP_RPORT_ATTRS 2 |
09345f650 [SCSI] add srp tr... |
42 43 44 45 |
struct srp_internal { struct scsi_transport_template t; struct srp_function_template *f; |
ee959b00c SCSI: convert str... |
46 |
struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1]; |
09345f650 [SCSI] add srp tr... |
47 |
|
ee959b00c SCSI: convert str... |
48 49 |
struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1]; struct device_attribute private_rport_attrs[SRP_RPORT_ATTRS]; |
09345f650 [SCSI] add srp tr... |
50 51 52 53 54 55 |
struct transport_container rport_attr_cont; }; #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t) #define dev_to_rport(d) container_of(d, struct srp_rport, dev) |
ee959b00c SCSI: convert str... |
56 |
#define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent) |
09345f650 [SCSI] add srp tr... |
57 58 |
static int srp_host_setup(struct transport_container *tc, struct device *dev, |
ee959b00c SCSI: convert str... |
59 |
struct device *cdev) |
09345f650 [SCSI] add srp tr... |
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
{ struct Scsi_Host *shost = dev_to_shost(dev); struct srp_host_attrs *srp_host = to_srp_host_attrs(shost); atomic_set(&srp_host->next_port_id, 0); return 0; } static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup, NULL, NULL); static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports", NULL, NULL, NULL); #define SETUP_TEMPLATE(attrb, field, perm, test, ro_test, ro_perm) \ |
ee959b00c SCSI: convert str... |
75 |
i->private_##attrb[count] = dev_attr_##field; \ |
09345f650 [SCSI] add srp tr... |
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
i->private_##attrb[count].attr.mode = perm; \ if (ro_test) { \ i->private_##attrb[count].attr.mode = ro_perm; \ i->private_##attrb[count].store = NULL; \ } \ i->attrb[count] = &i->private_##attrb[count]; \ if (test) \ count++ #define SETUP_RPORT_ATTRIBUTE_RD(field) \ SETUP_TEMPLATE(rport_attrs, field, S_IRUGO, 1, 0, 0) #define SETUP_RPORT_ATTRIBUTE_RW(field) \ SETUP_TEMPLATE(rport_attrs, field, S_IRUGO | S_IWUSR, \ 1, 1, S_IRUGO) #define SRP_PID(p) \ (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \ (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \ (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \ (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15] #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \ "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x" static ssize_t |
ee959b00c SCSI: convert str... |
102 103 |
show_srp_rport_id(struct device *dev, struct device_attribute *attr, char *buf) |
09345f650 [SCSI] add srp tr... |
104 |
{ |
ee959b00c SCSI: convert str... |
105 |
struct srp_rport *rport = transport_class_to_srp_rport(dev); |
09345f650 [SCSI] add srp tr... |
106 107 108 |
return sprintf(buf, SRP_PID_FMT " ", SRP_PID(rport)); } |
ee959b00c SCSI: convert str... |
109 |
static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL); |
09345f650 [SCSI] add srp tr... |
110 |
|
aebd5e476 [SCSI] transport_... |
111 112 113 114 115 116 117 118 119 |
static const struct { u32 value; char *name; } srp_rport_role_names[] = { {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"}, {SRP_RPORT_ROLE_TARGET, "SRP Target"}, }; static ssize_t |
ee959b00c SCSI: convert str... |
120 121 |
show_srp_rport_roles(struct device *dev, struct device_attribute *attr, char *buf) |
aebd5e476 [SCSI] transport_... |
122 |
{ |
ee959b00c SCSI: convert str... |
123 |
struct srp_rport *rport = transport_class_to_srp_rport(dev); |
aebd5e476 [SCSI] transport_... |
124 125 126 127 128 129 130 131 132 133 134 |
int i; char *name = NULL; for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++) if (srp_rport_role_names[i].value == rport->roles) { name = srp_rport_role_names[i].name; break; } return sprintf(buf, "%s ", name ? : "unknown"); } |
ee959b00c SCSI: convert str... |
135 |
static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL); |
aebd5e476 [SCSI] transport_... |
136 |
|
09345f650 [SCSI] add srp tr... |
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
static void srp_rport_release(struct device *dev) { struct srp_rport *rport = dev_to_rport(dev); put_device(dev->parent); kfree(rport); } static int scsi_is_srp_rport(const struct device *dev) { return dev->release == srp_rport_release; } static int srp_rport_match(struct attribute_container *cont, struct device *dev) { struct Scsi_Host *shost; struct srp_internal *i; if (!scsi_is_srp_rport(dev)) return 0; shost = dev_to_shost(dev->parent); if (!shost->transportt) return 0; if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) return 0; i = to_srp_internal(shost->transportt); return &i->rport_attr_cont.ac == cont; } static int srp_host_match(struct attribute_container *cont, struct device *dev) { struct Scsi_Host *shost; struct srp_internal *i; if (!scsi_is_host_device(dev)) return 0; shost = dev_to_shost(dev); if (!shost->transportt) return 0; if (shost->transportt->host_attrs.ac.class != &srp_host_class.class) return 0; i = to_srp_internal(shost->transportt); return &i->t.host_attrs.ac == cont; } /** * srp_rport_add - add a SRP remote port to the device hierarchy |
09345f650 [SCSI] add srp tr... |
189 190 191 |
* @shost: scsi host the remote port is connected to. * @ids: The port id for the remote port. * |
dc8875e10 [SCSI] docbook an... |
192 |
* Publishes a port to the rest of the system. |
09345f650 [SCSI] add srp tr... |
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
*/ struct srp_rport *srp_rport_add(struct Scsi_Host *shost, struct srp_rport_identifiers *ids) { struct srp_rport *rport; struct device *parent = &shost->shost_gendev; int id, ret; rport = kzalloc(sizeof(*rport), GFP_KERNEL); if (!rport) return ERR_PTR(-ENOMEM); device_initialize(&rport->dev); rport->dev.parent = get_device(parent); rport->dev.release = srp_rport_release; memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id)); |
aebd5e476 [SCSI] transport_... |
211 |
rport->roles = ids->roles; |
09345f650 [SCSI] add srp tr... |
212 213 |
id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id); |
71610f55f [SCSI] struct dev... |
214 |
dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id); |
09345f650 [SCSI] add srp tr... |
215 216 217 218 219 220 221 222 223 |
transport_setup_device(&rport->dev); ret = device_add(&rport->dev); if (ret) { transport_destroy_device(&rport->dev); put_device(&rport->dev); return ERR_PTR(ret); } |
72e39ea7e [SCSI] srp_transp... |
224 225 |
if (shost->active_mode & MODE_TARGET && ids->roles == SRP_RPORT_ROLE_INITIATOR) { |
0012fdf98 [SCSI] scsi_trans... |
226 227 |
ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport, rport->port_id); |
62fe88261 [SCSI] srp_transp... |
228 229 230 231 232 233 234 |
if (ret) { device_del(&rport->dev); transport_destroy_device(&rport->dev); put_device(&rport->dev); return ERR_PTR(ret); } } |
09345f650 [SCSI] add srp tr... |
235 236 237 238 239 240 241 242 |
transport_add_device(&rport->dev); transport_configure_device(&rport->dev); return rport; } EXPORT_SYMBOL_GPL(srp_rport_add); /** |
eb44820c2 [SCSI] Add Docume... |
243 244 |
* srp_rport_del - remove a SRP remote port * @rport: SRP remote port to remove |
09345f650 [SCSI] add srp tr... |
245 246 247 248 249 250 |
* * Removes the specified SRP remote port. */ void srp_rport_del(struct srp_rport *rport) { struct device *dev = &rport->dev; |
72e39ea7e [SCSI] srp_transp... |
251 |
struct Scsi_Host *shost = dev_to_shost(dev->parent); |
09345f650 [SCSI] add srp tr... |
252 |
|
72e39ea7e [SCSI] srp_transp... |
253 254 255 |
if (shost->active_mode & MODE_TARGET && rport->roles == SRP_RPORT_ROLE_INITIATOR) srp_tgt_it_nexus_destroy(shost, (unsigned long)rport); |
62fe88261 [SCSI] srp_transp... |
256 |
|
09345f650 [SCSI] add srp tr... |
257 258 259 260 261 262 263 264 265 |
transport_remove_device(dev); device_del(dev); transport_destroy_device(dev); put_device(dev); } EXPORT_SYMBOL_GPL(srp_rport_del); static int do_srp_rport_del(struct device *dev, void *data) { |
911833440 [SCSI] SRP transp... |
266 267 |
if (scsi_is_srp_rport(dev)) srp_rport_del(dev_to_rport(dev)); |
09345f650 [SCSI] add srp tr... |
268 269 270 271 |
return 0; } /** |
eb44820c2 [SCSI] Add Docume... |
272 |
* srp_remove_host - tear down a Scsi_Host's SRP data structures |
09345f650 [SCSI] add srp tr... |
273 274 275 276 277 278 279 280 281 282 |
* @shost: Scsi Host that is torn down * * Removes all SRP remote ports for a given Scsi_Host. * Must be called just before scsi_remove_host for SRP HBAs. */ void srp_remove_host(struct Scsi_Host *shost) { device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del); } EXPORT_SYMBOL_GPL(srp_remove_host); |
bfb743740 [SCSI] tgt: move ... |
283 284 |
static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id, int result) |
62fe88261 [SCSI] srp_transp... |
285 286 |
{ struct srp_internal *i = to_srp_internal(shost->transportt); |
bfb743740 [SCSI] tgt: move ... |
287 288 289 290 291 292 293 |
return i->f->tsk_mgmt_response(shost, nexus, tm_id, result); } static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result) { struct srp_internal *i = to_srp_internal(shost->transportt); return i->f->it_nexus_response(shost, nexus, result); |
62fe88261 [SCSI] srp_transp... |
294 |
} |
09345f650 [SCSI] add srp tr... |
295 |
/** |
eb44820c2 [SCSI] Add Docume... |
296 |
* srp_attach_transport - instantiate SRP transport template |
09345f650 [SCSI] add srp tr... |
297 298 299 300 301 302 303 304 305 306 307 |
* @ft: SRP transport class function template */ struct scsi_transport_template * srp_attach_transport(struct srp_function_template *ft) { int count; struct srp_internal *i; i = kzalloc(sizeof(*i), GFP_KERNEL); if (!i) return NULL; |
bfb743740 [SCSI] tgt: move ... |
308 |
i->t.tsk_mgmt_response = srp_tsk_mgmt_response; |
62fe88261 [SCSI] srp_transp... |
309 |
i->t.it_nexus_response = srp_it_nexus_response; |
09345f650 [SCSI] add srp tr... |
310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
i->t.host_size = sizeof(struct srp_host_attrs); i->t.host_attrs.ac.attrs = &i->host_attrs[0]; i->t.host_attrs.ac.class = &srp_host_class.class; i->t.host_attrs.ac.match = srp_host_match; i->host_attrs[0] = NULL; transport_container_register(&i->t.host_attrs); i->rport_attr_cont.ac.attrs = &i->rport_attrs[0]; i->rport_attr_cont.ac.class = &srp_rport_class.class; i->rport_attr_cont.ac.match = srp_rport_match; transport_container_register(&i->rport_attr_cont); count = 0; SETUP_RPORT_ATTRIBUTE_RD(port_id); |
aebd5e476 [SCSI] transport_... |
324 |
SETUP_RPORT_ATTRIBUTE_RD(roles); |
09345f650 [SCSI] add srp tr... |
325 326 327 328 329 330 331 332 333 |
i->rport_attrs[count] = NULL; i->f = ft; return &i->t; } EXPORT_SYMBOL_GPL(srp_attach_transport); /** |
eb44820c2 [SCSI] Add Docume... |
334 |
* srp_release_transport - release SRP transport template instance |
09345f650 [SCSI] add srp tr... |
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
* @t: transport template instance */ void srp_release_transport(struct scsi_transport_template *t) { struct srp_internal *i = to_srp_internal(t); transport_container_unregister(&i->t.host_attrs); transport_container_unregister(&i->rport_attr_cont); kfree(i); } EXPORT_SYMBOL_GPL(srp_release_transport); static __init int srp_transport_init(void) { int ret; ret = transport_class_register(&srp_host_class); if (ret) return ret; ret = transport_class_register(&srp_rport_class); if (ret) goto unregister_host_class; return 0; unregister_host_class: transport_class_unregister(&srp_host_class); return ret; } static void __exit srp_transport_exit(void) { transport_class_unregister(&srp_host_class); transport_class_unregister(&srp_rport_class); } MODULE_AUTHOR("FUJITA Tomonori"); MODULE_DESCRIPTION("SRP Transport Attributes"); MODULE_LICENSE("GPL"); module_init(srp_transport_init); module_exit(srp_transport_exit); |