scsi_transport_srp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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_get() - increment rport reference count
  154. */
  155. void srp_rport_get(struct srp_rport *rport)
  156. {
  157. get_device(&rport->dev);
  158. }
  159. EXPORT_SYMBOL(srp_rport_get);
  160. /**
  161. * srp_rport_put() - decrement rport reference count
  162. */
  163. void srp_rport_put(struct srp_rport *rport)
  164. {
  165. put_device(&rport->dev);
  166. }
  167. EXPORT_SYMBOL(srp_rport_put);
  168. /**
  169. * srp_rport_add - add a SRP remote port to the device hierarchy
  170. * @shost: scsi host the remote port is connected to.
  171. * @ids: The port id for the remote port.
  172. *
  173. * Publishes a port to the rest of the system.
  174. */
  175. struct srp_rport *srp_rport_add(struct Scsi_Host *shost,
  176. struct srp_rport_identifiers *ids)
  177. {
  178. struct srp_rport *rport;
  179. struct device *parent = &shost->shost_gendev;
  180. int id, ret;
  181. rport = kzalloc(sizeof(*rport), GFP_KERNEL);
  182. if (!rport)
  183. return ERR_PTR(-ENOMEM);
  184. device_initialize(&rport->dev);
  185. rport->dev.parent = get_device(parent);
  186. rport->dev.release = srp_rport_release;
  187. memcpy(rport->port_id, ids->port_id, sizeof(rport->port_id));
  188. rport->roles = ids->roles;
  189. id = atomic_inc_return(&to_srp_host_attrs(shost)->next_port_id);
  190. dev_set_name(&rport->dev, "port-%d:%d", shost->host_no, id);
  191. transport_setup_device(&rport->dev);
  192. ret = device_add(&rport->dev);
  193. if (ret) {
  194. transport_destroy_device(&rport->dev);
  195. put_device(&rport->dev);
  196. return ERR_PTR(ret);
  197. }
  198. if (shost->active_mode & MODE_TARGET &&
  199. ids->roles == SRP_RPORT_ROLE_INITIATOR) {
  200. ret = srp_tgt_it_nexus_create(shost, (unsigned long)rport,
  201. rport->port_id);
  202. if (ret) {
  203. device_del(&rport->dev);
  204. transport_destroy_device(&rport->dev);
  205. put_device(&rport->dev);
  206. return ERR_PTR(ret);
  207. }
  208. }
  209. transport_add_device(&rport->dev);
  210. transport_configure_device(&rport->dev);
  211. return rport;
  212. }
  213. EXPORT_SYMBOL_GPL(srp_rport_add);
  214. /**
  215. * srp_rport_del - remove a SRP remote port
  216. * @rport: SRP remote port to remove
  217. *
  218. * Removes the specified SRP remote port.
  219. */
  220. void srp_rport_del(struct srp_rport *rport)
  221. {
  222. struct device *dev = &rport->dev;
  223. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  224. if (shost->active_mode & MODE_TARGET &&
  225. rport->roles == SRP_RPORT_ROLE_INITIATOR)
  226. srp_tgt_it_nexus_destroy(shost, (unsigned long)rport);
  227. transport_remove_device(dev);
  228. device_del(dev);
  229. transport_destroy_device(dev);
  230. put_device(dev);
  231. }
  232. EXPORT_SYMBOL_GPL(srp_rport_del);
  233. static int do_srp_rport_del(struct device *dev, void *data)
  234. {
  235. if (scsi_is_srp_rport(dev))
  236. srp_rport_del(dev_to_rport(dev));
  237. return 0;
  238. }
  239. /**
  240. * srp_remove_host - tear down a Scsi_Host's SRP data structures
  241. * @shost: Scsi Host that is torn down
  242. *
  243. * Removes all SRP remote ports for a given Scsi_Host.
  244. * Must be called just before scsi_remove_host for SRP HBAs.
  245. */
  246. void srp_remove_host(struct Scsi_Host *shost)
  247. {
  248. device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
  249. }
  250. EXPORT_SYMBOL_GPL(srp_remove_host);
  251. static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
  252. int result)
  253. {
  254. struct srp_internal *i = to_srp_internal(shost->transportt);
  255. return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
  256. }
  257. static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
  258. {
  259. struct srp_internal *i = to_srp_internal(shost->transportt);
  260. return i->f->it_nexus_response(shost, nexus, result);
  261. }
  262. /**
  263. * srp_attach_transport - instantiate SRP transport template
  264. * @ft: SRP transport class function template
  265. */
  266. struct scsi_transport_template *
  267. srp_attach_transport(struct srp_function_template *ft)
  268. {
  269. int count;
  270. struct srp_internal *i;
  271. i = kzalloc(sizeof(*i), GFP_KERNEL);
  272. if (!i)
  273. return NULL;
  274. i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
  275. i->t.it_nexus_response = srp_it_nexus_response;
  276. i->t.host_size = sizeof(struct srp_host_attrs);
  277. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  278. i->t.host_attrs.ac.class = &srp_host_class.class;
  279. i->t.host_attrs.ac.match = srp_host_match;
  280. i->host_attrs[0] = NULL;
  281. transport_container_register(&i->t.host_attrs);
  282. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  283. i->rport_attr_cont.ac.class = &srp_rport_class.class;
  284. i->rport_attr_cont.ac.match = srp_rport_match;
  285. count = 0;
  286. i->rport_attrs[count++] = &dev_attr_port_id;
  287. i->rport_attrs[count++] = &dev_attr_roles;
  288. if (ft->rport_delete)
  289. i->rport_attrs[count++] = &dev_attr_delete;
  290. i->rport_attrs[count++] = NULL;
  291. BUG_ON(count > ARRAY_SIZE(i->rport_attrs));
  292. transport_container_register(&i->rport_attr_cont);
  293. i->f = ft;
  294. return &i->t;
  295. }
  296. EXPORT_SYMBOL_GPL(srp_attach_transport);
  297. /**
  298. * srp_release_transport - release SRP transport template instance
  299. * @t: transport template instance
  300. */
  301. void srp_release_transport(struct scsi_transport_template *t)
  302. {
  303. struct srp_internal *i = to_srp_internal(t);
  304. transport_container_unregister(&i->t.host_attrs);
  305. transport_container_unregister(&i->rport_attr_cont);
  306. kfree(i);
  307. }
  308. EXPORT_SYMBOL_GPL(srp_release_transport);
  309. static __init int srp_transport_init(void)
  310. {
  311. int ret;
  312. ret = transport_class_register(&srp_host_class);
  313. if (ret)
  314. return ret;
  315. ret = transport_class_register(&srp_rport_class);
  316. if (ret)
  317. goto unregister_host_class;
  318. return 0;
  319. unregister_host_class:
  320. transport_class_unregister(&srp_host_class);
  321. return ret;
  322. }
  323. static void __exit srp_transport_exit(void)
  324. {
  325. transport_class_unregister(&srp_host_class);
  326. transport_class_unregister(&srp_rport_class);
  327. }
  328. MODULE_AUTHOR("FUJITA Tomonori");
  329. MODULE_DESCRIPTION("SRP Transport Attributes");
  330. MODULE_LICENSE("GPL");
  331. module_init(srp_transport_init);
  332. module_exit(srp_transport_exit);