scsi_transport_srp.c 10 KB

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