scsi_transport_srp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #include <scsi/scsi.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport.h>
  31. #include <scsi/scsi_transport_srp.h>
  32. #include "scsi_transport_srp_internal.h"
  33. struct srp_host_attrs {
  34. atomic_t next_port_id;
  35. };
  36. #define to_srp_host_attrs(host) ((struct srp_host_attrs *)(host)->shost_data)
  37. #define SRP_HOST_ATTRS 0
  38. #define SRP_RPORT_ATTRS 2
  39. struct srp_internal {
  40. struct scsi_transport_template t;
  41. struct srp_function_template *f;
  42. struct device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
  43. struct device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
  44. struct device_attribute private_rport_attrs[SRP_RPORT_ATTRS];
  45. struct transport_container rport_attr_cont;
  46. };
  47. #define to_srp_internal(tmpl) container_of(tmpl, struct srp_internal, t)
  48. #define dev_to_rport(d) container_of(d, struct srp_rport, dev)
  49. #define transport_class_to_srp_rport(dev) dev_to_rport((dev)->parent)
  50. static int srp_host_setup(struct transport_container *tc, struct device *dev,
  51. struct device *cdev)
  52. {
  53. struct Scsi_Host *shost = dev_to_shost(dev);
  54. struct srp_host_attrs *srp_host = to_srp_host_attrs(shost);
  55. atomic_set(&srp_host->next_port_id, 0);
  56. return 0;
  57. }
  58. static DECLARE_TRANSPORT_CLASS(srp_host_class, "srp_host", srp_host_setup,
  59. NULL, NULL);
  60. static DECLARE_TRANSPORT_CLASS(srp_rport_class, "srp_remote_ports",
  61. NULL, NULL, NULL);
  62. #define SETUP_TEMPLATE(attrb, field, perm, test, ro_test, ro_perm) \
  63. i->private_##attrb[count] = dev_attr_##field; \
  64. i->private_##attrb[count].attr.mode = perm; \
  65. if (ro_test) { \
  66. i->private_##attrb[count].attr.mode = ro_perm; \
  67. i->private_##attrb[count].store = NULL; \
  68. } \
  69. i->attrb[count] = &i->private_##attrb[count]; \
  70. if (test) \
  71. count++
  72. #define SETUP_RPORT_ATTRIBUTE_RD(field) \
  73. SETUP_TEMPLATE(rport_attrs, field, S_IRUGO, 1, 0, 0)
  74. #define SETUP_RPORT_ATTRIBUTE_RW(field) \
  75. SETUP_TEMPLATE(rport_attrs, field, S_IRUGO | S_IWUSR, \
  76. 1, 1, S_IRUGO)
  77. #define SRP_PID(p) \
  78. (p)->port_id[0], (p)->port_id[1], (p)->port_id[2], (p)->port_id[3], \
  79. (p)->port_id[4], (p)->port_id[5], (p)->port_id[6], (p)->port_id[7], \
  80. (p)->port_id[8], (p)->port_id[9], (p)->port_id[10], (p)->port_id[11], \
  81. (p)->port_id[12], (p)->port_id[13], (p)->port_id[14], (p)->port_id[15]
  82. #define SRP_PID_FMT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
  83. "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
  84. static ssize_t
  85. show_srp_rport_id(struct device *dev, struct device_attribute *attr,
  86. char *buf)
  87. {
  88. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  89. return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
  90. }
  91. static DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
  92. static const struct {
  93. u32 value;
  94. char *name;
  95. } srp_rport_role_names[] = {
  96. {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
  97. {SRP_RPORT_ROLE_TARGET, "SRP Target"},
  98. };
  99. static ssize_t
  100. show_srp_rport_roles(struct device *dev, struct device_attribute *attr,
  101. char *buf)
  102. {
  103. struct srp_rport *rport = transport_class_to_srp_rport(dev);
  104. int i;
  105. char *name = NULL;
  106. for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
  107. if (srp_rport_role_names[i].value == rport->roles) {
  108. name = srp_rport_role_names[i].name;
  109. break;
  110. }
  111. return sprintf(buf, "%s\n", name ? : "unknown");
  112. }
  113. static DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
  114. static void srp_rport_release(struct device *dev)
  115. {
  116. struct srp_rport *rport = dev_to_rport(dev);
  117. put_device(dev->parent);
  118. kfree(rport);
  119. }
  120. static int scsi_is_srp_rport(const struct device *dev)
  121. {
  122. return dev->release == srp_rport_release;
  123. }
  124. static int srp_rport_match(struct attribute_container *cont,
  125. struct device *dev)
  126. {
  127. struct Scsi_Host *shost;
  128. struct srp_internal *i;
  129. if (!scsi_is_srp_rport(dev))
  130. return 0;
  131. shost = dev_to_shost(dev->parent);
  132. if (!shost->transportt)
  133. return 0;
  134. if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
  135. return 0;
  136. i = to_srp_internal(shost->transportt);
  137. return &i->rport_attr_cont.ac == cont;
  138. }
  139. static int srp_host_match(struct attribute_container *cont, struct device *dev)
  140. {
  141. struct Scsi_Host *shost;
  142. struct srp_internal *i;
  143. if (!scsi_is_host_device(dev))
  144. return 0;
  145. shost = dev_to_shost(dev);
  146. if (!shost->transportt)
  147. return 0;
  148. if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
  149. return 0;
  150. i = to_srp_internal(shost->transportt);
  151. return &i->t.host_attrs.ac == cont;
  152. }
  153. /**
  154. * srp_rport_add - add a SRP remote port to the device hierarchy
  155. * @shost: scsi host the remote port is connected to.
  156. * @ids: The port id for the remote port.
  157. *
  158. * Publishes a port to the rest of the system.
  159. */
  160. struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
  161. struct srp_rport_identifiers *ids)
  162. {
  163. struct srp_rport *rport;
  164. struct device *parent = &shost->shost_gendev;
  165. int id, ret;
  166. rport = kzalloc(sizeof(*rport), GFP_KERNEL);
  167. if (!rport)
  168. return ERR_PTR(-ENOMEM);
  169. device_initialize(&rport->dev);
  170. rport->dev.parent = get_device(parent);
  171. rport->dev.release = srp_rport_release;
  172. memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
  173. rport->roles = ids->roles;
  174. id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
  175. dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
  176. transport_setup_device(&rport->dev);
  177. ret = device_add(&rport->dev);
  178. if (ret) {
  179. transport_destroy_device(&rport->dev);
  180. put_device(&rport->dev);
  181. return ERR_PTR(ret);
  182. }
  183. if (shost->active_mode & MODE_TARGET &&
  184. ids->roles == SRP_RPORT_ROLE_INITIATOR) {
  185. ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
  186. rport->port_id);
  187. if (ret) {
  188. device_del(&rport->dev);
  189. transport_destroy_device(&rport->dev);
  190. put_device(&rport->dev);
  191. return ERR_PTR(ret);
  192. }
  193. }
  194. transport_add_device(&rport->dev);
  195. transport_configure_device(&rport->dev);
  196. return rport;
  197. }
  198. EXPORT_SYMBOL_GPL(srp_rport_add);
  199. /**
  200. * srp_rport_del - remove a SRP remote port
  201. * @rport: SRP remote port to remove
  202. *
  203. * Removes the specified SRP remote port.
  204. */
  205. void srp_rport_del(struct srp_rport *rport)
  206. {
  207. struct device *dev = &rport->dev;
  208. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  209. if (shost->active_mode & MODE_TARGET &&
  210. rport->roles == SRP_RPORT_ROLE_INITIATOR)
  211. srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
  212. transport_remove_device(dev);
  213. device_del(dev);
  214. transport_destroy_device(dev);
  215. put_device(dev);
  216. }
  217. EXPORT_SYMBOL_GPL(srp_rport_del);
  218. static int do_srp_rport_del(struct device *dev, void *data)
  219. {
  220. if (scsi_is_srp_rport(dev))
  221. srp_rport_del(dev_to_rport(dev));
  222. return 0;
  223. }
  224. /**
  225. * srp_remove_host - tear down a Scsi_Host's SRP data structures
  226. * @shost: Scsi Host that is torn down
  227. *
  228. * Removes all SRP remote ports for a given Scsi_Host.
  229. * Must be called just before scsi_remove_host for SRP HBAs.
  230. */
  231. void srp_remove_host(struct Scsi_Host *shost)
  232. {
  233. device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
  234. }
  235. EXPORT_SYMBOL_GPL(srp_remove_host);
  236. static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
  237. int result)
  238. {
  239. struct srp_internal *i = to_srp_internal(shost->transportt);
  240. return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
  241. }
  242. static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
  243. {
  244. struct srp_internal *i = to_srp_internal(shost->transportt);
  245. return i->f->it_nexus_response(shost, nexus, result);
  246. }
  247. /**
  248. * srp_attach_transport - instantiate SRP transport template
  249. * @ft: SRP transport class function template
  250. */
  251. struct scsi_transport_template *
  252. srp_attach_transport(struct srp_function_template *ft)
  253. {
  254. int count;
  255. struct srp_internal *i;
  256. i = kzalloc(sizeof(*i), GFP_KERNEL);
  257. if (!i)
  258. return NULL;
  259. i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
  260. i->t.it_nexus_response = srp_it_nexus_response;
  261. i->t.host_size = sizeof(struct srp_host_attrs);
  262. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  263. i->t.host_attrs.ac.class = &srp_host_class.class;
  264. i->t.host_attrs.ac.match = srp_host_match;
  265. i->host_attrs[0] = NULL;
  266. transport_container_register(&i->t.host_attrs);
  267. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  268. i->rport_attr_cont.ac.class = &srp_rport_class.class;
  269. i->rport_attr_cont.ac.match = srp_rport_match;
  270. transport_container_register(&i->rport_attr_cont);
  271. count = 0;
  272. SETUP_RPORT_ATTRIBUTE_RD(port_id);
  273. SETUP_RPORT_ATTRIBUTE_RD(roles);
  274. i->rport_attrs[count] = NULL;
  275. i->f = ft;
  276. return &i->t;
  277. }
  278. EXPORT_SYMBOL_GPL(srp_attach_transport);
  279. /**
  280. * srp_release_transport - release SRP transport template instance
  281. * @t: transport template instance
  282. */
  283. void srp_release_transport(struct scsi_transport_template *t)
  284. {
  285. struct srp_internal *i = to_srp_internal(t);
  286. transport_container_unregister(&i->t.host_attrs);
  287. transport_container_unregister(&i->rport_attr_cont);
  288. kfree(i);
  289. }
  290. EXPORT_SYMBOL_GPL(srp_release_transport);
  291. static __init int srp_transport_init(void)
  292. {
  293. int ret;
  294. ret = transport_class_register(&srp_host_class);
  295. if (ret)
  296. return ret;
  297. ret = transport_class_register(&srp_rport_class);
  298. if (ret)
  299. goto unregister_host_class;
  300. return 0;
  301. unregister_host_class:
  302. transport_class_unregister(&srp_host_class);
  303. return ret;
  304. }
  305. static void __exit srp_transport_exit(void)
  306. {
  307. transport_class_unregister(&srp_host_class);
  308. transport_class_unregister(&srp_rport_class);
  309. }
  310. MODULE_AUTHOR("FUJITA Tomonori");
  311. MODULE_DESCRIPTION("SRP Transport Attributes");
  312. MODULE_LICENSE("GPL");
  313. module_init(srp_transport_init);
  314. module_exit(srp_transport_exit);