scsi_transport_srp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 class_device_attribute *host_attrs[SRP_HOST_ATTRS + 1];
  43. struct class_device_attribute *rport_attrs[SRP_RPORT_ATTRS + 1];
  44. struct class_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(cdev) dev_to_rport((cdev)->dev)
  50. static int srp_host_setup(struct transport_container *tc, struct device *dev,
  51. struct class_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] = class_device_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 class_device *cdev, char *buf)
  86. {
  87. struct srp_rport *rport = transport_class_to_srp_rport(cdev);
  88. return sprintf(buf, SRP_PID_FMT "\n", SRP_PID(rport));
  89. }
  90. static CLASS_DEVICE_ATTR(port_id, S_IRUGO, show_srp_rport_id, NULL);
  91. static const struct {
  92. u32 value;
  93. char *name;
  94. } srp_rport_role_names[] = {
  95. {SRP_RPORT_ROLE_INITIATOR, "SRP Initiator"},
  96. {SRP_RPORT_ROLE_TARGET, "SRP Target"},
  97. };
  98. static ssize_t
  99. show_srp_rport_roles(struct class_device *cdev, char *buf)
  100. {
  101. struct srp_rport *rport = transport_class_to_srp_rport(cdev);
  102. int i;
  103. char *name = NULL;
  104. for (i = 0; i < ARRAY_SIZE(srp_rport_role_names); i++)
  105. if (srp_rport_role_names[i].value == rport->roles) {
  106. name = srp_rport_role_names[i].name;
  107. break;
  108. }
  109. return sprintf(buf, "%s\n", name ? : "unknown");
  110. }
  111. static CLASS_DEVICE_ATTR(roles, S_IRUGO, show_srp_rport_roles, NULL);
  112. static void srp_rport_release(struct device *dev)
  113. {
  114. struct srp_rport *rport = dev_to_rport(dev);
  115. put_device(dev->parent);
  116. kfree(rport);
  117. }
  118. static int scsi_is_srp_rport(const struct device *dev)
  119. {
  120. return dev->release == srp_rport_release;
  121. }
  122. static int srp_rport_match(struct attribute_container *cont,
  123. struct device *dev)
  124. {
  125. struct Scsi_Host *shost;
  126. struct srp_internal *i;
  127. if (!scsi_is_srp_rport(dev))
  128. return 0;
  129. shost = dev_to_shost(dev->parent);
  130. if (!shost->transportt)
  131. return 0;
  132. if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
  133. return 0;
  134. i = to_srp_internal(shost->transportt);
  135. return &i->rport_attr_cont.ac == cont;
  136. }
  137. static int srp_host_match(struct attribute_container *cont, struct device *dev)
  138. {
  139. struct Scsi_Host *shost;
  140. struct srp_internal *i;
  141. if (!scsi_is_host_device(dev))
  142. return 0;
  143. shost = dev_to_shost(dev);
  144. if (!shost->transportt)
  145. return 0;
  146. if (shost->transportt->host_attrs.ac.class != &srp_host_class.class)
  147. return 0;
  148. i = to_srp_internal(shost->transportt);
  149. return &i->t.host_attrs.ac == cont;
  150. }
  151. /**
  152. * srp_rport_add - add a SRP remote port to the device hierarchy
  153. *
  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. sprintf(rport->dev.bus_id, "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. * @port: 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. srp_rport_del(dev_to_rport(dev));
  220. return 0;
  221. }
  222. /**
  223. * srp_remove_host -- tear down a Scsi_Host's SRP data structures
  224. * @shost: Scsi Host that is torn down
  225. *
  226. * Removes all SRP remote ports for a given Scsi_Host.
  227. * Must be called just before scsi_remove_host for SRP HBAs.
  228. */
  229. void srp_remove_host(struct Scsi_Host *shost)
  230. {
  231. device_for_each_child(&shost->shost_gendev, NULL, do_srp_rport_del);
  232. }
  233. EXPORT_SYMBOL_GPL(srp_remove_host);
  234. static int srp_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
  235. int result)
  236. {
  237. struct srp_internal *i = to_srp_internal(shost->transportt);
  238. return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
  239. }
  240. static int srp_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
  241. {
  242. struct srp_internal *i = to_srp_internal(shost->transportt);
  243. return i->f->it_nexus_response(shost, nexus, result);
  244. }
  245. /**
  246. * srp_attach_transport -- instantiate SRP transport template
  247. * @ft: SRP transport class function template
  248. */
  249. struct scsi_transport_template *
  250. srp_attach_transport(struct srp_function_template *ft)
  251. {
  252. int count;
  253. struct srp_internal *i;
  254. i = kzalloc(sizeof(*i), GFP_KERNEL);
  255. if (!i)
  256. return NULL;
  257. i->t.tsk_mgmt_response = srp_tsk_mgmt_response;
  258. i->t.it_nexus_response = srp_it_nexus_response;
  259. i->t.host_size = sizeof(struct srp_host_attrs);
  260. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  261. i->t.host_attrs.ac.class = &srp_host_class.class;
  262. i->t.host_attrs.ac.match = srp_host_match;
  263. i->host_attrs[0] = NULL;
  264. transport_container_register(&i->t.host_attrs);
  265. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  266. i->rport_attr_cont.ac.class = &srp_rport_class.class;
  267. i->rport_attr_cont.ac.match = srp_rport_match;
  268. transport_container_register(&i->rport_attr_cont);
  269. count = 0;
  270. SETUP_RPORT_ATTRIBUTE_RD(port_id);
  271. SETUP_RPORT_ATTRIBUTE_RD(roles);
  272. i->rport_attrs[count] = NULL;
  273. i->f = ft;
  274. return &i->t;
  275. }
  276. EXPORT_SYMBOL_GPL(srp_attach_transport);
  277. /**
  278. * srp_release_transport -- release SRP transport template instance
  279. * @t: transport template instance
  280. */
  281. void srp_release_transport(struct scsi_transport_template *t)
  282. {
  283. struct srp_internal *i = to_srp_internal(t);
  284. transport_container_unregister(&i->t.host_attrs);
  285. transport_container_unregister(&i->rport_attr_cont);
  286. kfree(i);
  287. }
  288. EXPORT_SYMBOL_GPL(srp_release_transport);
  289. static __init int srp_transport_init(void)
  290. {
  291. int ret;
  292. ret = transport_class_register(&srp_host_class);
  293. if (ret)
  294. return ret;
  295. ret = transport_class_register(&srp_rport_class);
  296. if (ret)
  297. goto unregister_host_class;
  298. return 0;
  299. unregister_host_class:
  300. transport_class_unregister(&srp_host_class);
  301. return ret;
  302. }
  303. static void __exit srp_transport_exit(void)
  304. {
  305. transport_class_unregister(&srp_host_class);
  306. transport_class_unregister(&srp_rport_class);
  307. }
  308. MODULE_AUTHOR("FUJITA Tomonori");
  309. MODULE_DESCRIPTION("SRP Transport Attributes");
  310. MODULE_LICENSE("GPL");
  311. module_init(srp_transport_init);
  312. module_exit(srp_transport_exit);