smc_ism.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Shared Memory Communications Direct over ISM devices (SMC-D)
  3. *
  4. * Functions for ISM device.
  5. *
  6. * Copyright IBM Corp. 2018
  7. */
  8. #include <linux/spinlock.h>
  9. #include <linux/slab.h>
  10. #include <asm/page.h>
  11. #include "smc.h"
  12. #include "smc_core.h"
  13. #include "smc_ism.h"
  14. #include "smc_pnet.h"
  15. struct smcd_dev_list smcd_dev_list = {
  16. .list = LIST_HEAD_INIT(smcd_dev_list.list),
  17. .lock = __SPIN_LOCK_UNLOCKED(smcd_dev_list.lock)
  18. };
  19. /* Test if an ISM communication is possible. */
  20. int smc_ism_cantalk(u64 peer_gid, unsigned short vlan_id, struct smcd_dev *smcd)
  21. {
  22. return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0,
  23. vlan_id);
  24. }
  25. int smc_ism_write(struct smcd_dev *smcd, const struct smc_ism_position *pos,
  26. void *data, size_t len)
  27. {
  28. int rc;
  29. rc = smcd->ops->move_data(smcd, pos->token, pos->index, pos->signal,
  30. pos->offset, data, len);
  31. return rc < 0 ? rc : 0;
  32. }
  33. /* Set a connection using this DMBE. */
  34. void smc_ism_set_conn(struct smc_connection *conn)
  35. {
  36. unsigned long flags;
  37. spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
  38. conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
  39. spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
  40. }
  41. /* Unset a connection using this DMBE. */
  42. void smc_ism_unset_conn(struct smc_connection *conn)
  43. {
  44. unsigned long flags;
  45. if (!conn->rmb_desc)
  46. return;
  47. spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
  48. conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
  49. spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
  50. }
  51. /* Register a VLAN identifier with the ISM device. Use a reference count
  52. * and add a VLAN identifier only when the first DMB using this VLAN is
  53. * registered.
  54. */
  55. int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
  56. {
  57. struct smc_ism_vlanid *new_vlan, *vlan;
  58. unsigned long flags;
  59. int rc = 0;
  60. if (!vlanid) /* No valid vlan id */
  61. return -EINVAL;
  62. /* create new vlan entry, in case we need it */
  63. new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
  64. if (!new_vlan)
  65. return -ENOMEM;
  66. new_vlan->vlanid = vlanid;
  67. refcount_set(&new_vlan->refcnt, 1);
  68. /* if there is an existing entry, increase count and return */
  69. spin_lock_irqsave(&smcd->lock, flags);
  70. list_for_each_entry(vlan, &smcd->vlan, list) {
  71. if (vlan->vlanid == vlanid) {
  72. refcount_inc(&vlan->refcnt);
  73. kfree(new_vlan);
  74. goto out;
  75. }
  76. }
  77. /* no existing entry found.
  78. * add new entry to device; might fail, e.g., if HW limit reached
  79. */
  80. if (smcd->ops->add_vlan_id(smcd, vlanid)) {
  81. kfree(new_vlan);
  82. rc = -EIO;
  83. goto out;
  84. }
  85. list_add_tail(&new_vlan->list, &smcd->vlan);
  86. out:
  87. spin_unlock_irqrestore(&smcd->lock, flags);
  88. return rc;
  89. }
  90. /* Unregister a VLAN identifier with the ISM device. Use a reference count
  91. * and remove a VLAN identifier only when the last DMB using this VLAN is
  92. * unregistered.
  93. */
  94. int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
  95. {
  96. struct smc_ism_vlanid *vlan;
  97. unsigned long flags;
  98. bool found = false;
  99. int rc = 0;
  100. if (!vlanid) /* No valid vlan id */
  101. return -EINVAL;
  102. spin_lock_irqsave(&smcd->lock, flags);
  103. list_for_each_entry(vlan, &smcd->vlan, list) {
  104. if (vlan->vlanid == vlanid) {
  105. if (!refcount_dec_and_test(&vlan->refcnt))
  106. goto out;
  107. found = true;
  108. break;
  109. }
  110. }
  111. if (!found) {
  112. rc = -ENOENT;
  113. goto out; /* VLAN id not in table */
  114. }
  115. /* Found and the last reference just gone */
  116. if (smcd->ops->del_vlan_id(smcd, vlanid))
  117. rc = -EIO;
  118. list_del(&vlan->list);
  119. kfree(vlan);
  120. out:
  121. spin_unlock_irqrestore(&smcd->lock, flags);
  122. return rc;
  123. }
  124. int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
  125. {
  126. struct smcd_dmb dmb;
  127. memset(&dmb, 0, sizeof(dmb));
  128. dmb.dmb_tok = dmb_desc->token;
  129. dmb.sba_idx = dmb_desc->sba_idx;
  130. dmb.cpu_addr = dmb_desc->cpu_addr;
  131. dmb.dma_addr = dmb_desc->dma_addr;
  132. dmb.dmb_len = dmb_desc->len;
  133. return smcd->ops->unregister_dmb(smcd, &dmb);
  134. }
  135. int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
  136. struct smc_buf_desc *dmb_desc)
  137. {
  138. struct smcd_dmb dmb;
  139. int rc;
  140. memset(&dmb, 0, sizeof(dmb));
  141. dmb.dmb_len = dmb_len;
  142. dmb.sba_idx = dmb_desc->sba_idx;
  143. dmb.vlan_id = lgr->vlan_id;
  144. dmb.rgid = lgr->peer_gid;
  145. rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb);
  146. if (!rc) {
  147. dmb_desc->sba_idx = dmb.sba_idx;
  148. dmb_desc->token = dmb.dmb_tok;
  149. dmb_desc->cpu_addr = dmb.cpu_addr;
  150. dmb_desc->dma_addr = dmb.dma_addr;
  151. dmb_desc->len = dmb.dmb_len;
  152. }
  153. return rc;
  154. }
  155. struct smc_ism_event_work {
  156. struct work_struct work;
  157. struct smcd_dev *smcd;
  158. struct smcd_event event;
  159. };
  160. #define ISM_EVENT_REQUEST 0x0001
  161. #define ISM_EVENT_RESPONSE 0x0002
  162. #define ISM_EVENT_REQUEST_IR 0x00000001
  163. #define ISM_EVENT_CODE_TESTLINK 0x83
  164. static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
  165. {
  166. union {
  167. u64 info;
  168. struct {
  169. u32 uid;
  170. unsigned short vlanid;
  171. u16 code;
  172. };
  173. } ev_info;
  174. switch (wrk->event.code) {
  175. case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
  176. ev_info.info = wrk->event.info;
  177. if (ev_info.code == ISM_EVENT_REQUEST) {
  178. ev_info.code = ISM_EVENT_RESPONSE;
  179. wrk->smcd->ops->signal_event(wrk->smcd,
  180. wrk->event.tok,
  181. ISM_EVENT_REQUEST_IR,
  182. ISM_EVENT_CODE_TESTLINK,
  183. ev_info.info);
  184. }
  185. break;
  186. }
  187. }
  188. /* worker for SMC-D events */
  189. static void smc_ism_event_work(struct work_struct *work)
  190. {
  191. struct smc_ism_event_work *wrk =
  192. container_of(work, struct smc_ism_event_work, work);
  193. switch (wrk->event.type) {
  194. case ISM_EVENT_GID: /* GID event, token is peer GID */
  195. smc_smcd_terminate(wrk->smcd, wrk->event.tok);
  196. break;
  197. case ISM_EVENT_DMB:
  198. break;
  199. case ISM_EVENT_SWR: /* Software defined event */
  200. smcd_handle_sw_event(wrk);
  201. break;
  202. }
  203. kfree(wrk);
  204. }
  205. static void smcd_release(struct device *dev)
  206. {
  207. struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev);
  208. kfree(smcd->conn);
  209. kfree(smcd);
  210. }
  211. struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
  212. const struct smcd_ops *ops, int max_dmbs)
  213. {
  214. struct smcd_dev *smcd;
  215. smcd = kzalloc(sizeof(*smcd), GFP_KERNEL);
  216. if (!smcd)
  217. return NULL;
  218. smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *),
  219. GFP_KERNEL);
  220. if (!smcd->conn) {
  221. kfree(smcd);
  222. return NULL;
  223. }
  224. smcd->dev.parent = parent;
  225. smcd->dev.release = smcd_release;
  226. device_initialize(&smcd->dev);
  227. dev_set_name(&smcd->dev, name);
  228. smcd->ops = ops;
  229. smc_pnetid_by_dev_port(parent, 0, smcd->pnetid);
  230. spin_lock_init(&smcd->lock);
  231. INIT_LIST_HEAD(&smcd->vlan);
  232. smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
  233. WQ_MEM_RECLAIM, name);
  234. return smcd;
  235. }
  236. EXPORT_SYMBOL_GPL(smcd_alloc_dev);
  237. int smcd_register_dev(struct smcd_dev *smcd)
  238. {
  239. spin_lock(&smcd_dev_list.lock);
  240. list_add_tail(&smcd->list, &smcd_dev_list.list);
  241. spin_unlock(&smcd_dev_list.lock);
  242. return device_add(&smcd->dev);
  243. }
  244. EXPORT_SYMBOL_GPL(smcd_register_dev);
  245. void smcd_unregister_dev(struct smcd_dev *smcd)
  246. {
  247. spin_lock(&smcd_dev_list.lock);
  248. list_del(&smcd->list);
  249. spin_unlock(&smcd_dev_list.lock);
  250. flush_workqueue(smcd->event_wq);
  251. destroy_workqueue(smcd->event_wq);
  252. smc_smcd_terminate(smcd, 0);
  253. device_del(&smcd->dev);
  254. }
  255. EXPORT_SYMBOL_GPL(smcd_unregister_dev);
  256. void smcd_free_dev(struct smcd_dev *smcd)
  257. {
  258. put_device(&smcd->dev);
  259. }
  260. EXPORT_SYMBOL_GPL(smcd_free_dev);
  261. /* SMCD Device event handler. Called from ISM device interrupt handler.
  262. * Parameters are smcd device pointer,
  263. * - event->type (0 --> DMB, 1 --> GID),
  264. * - event->code (event code),
  265. * - event->tok (either DMB token when event type 0, or GID when event type 1)
  266. * - event->time (time of day)
  267. * - event->info (debug info).
  268. *
  269. * Context:
  270. * - Function called in IRQ context from ISM device driver event handler.
  271. */
  272. void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event)
  273. {
  274. struct smc_ism_event_work *wrk;
  275. /* copy event to event work queue, and let it be handled there */
  276. wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
  277. if (!wrk)
  278. return;
  279. INIT_WORK(&wrk->work, smc_ism_event_work);
  280. wrk->smcd = smcd;
  281. wrk->event = *event;
  282. queue_work(smcd->event_wq, &wrk->work);
  283. }
  284. EXPORT_SYMBOL_GPL(smcd_handle_event);
  285. /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
  286. * Parameters are smcd device pointer and DMB number. Find the connection and
  287. * schedule the tasklet for this connection.
  288. *
  289. * Context:
  290. * - Function called in IRQ context from ISM device driver IRQ handler.
  291. */
  292. void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno)
  293. {
  294. struct smc_connection *conn = NULL;
  295. unsigned long flags;
  296. spin_lock_irqsave(&smcd->lock, flags);
  297. conn = smcd->conn[dmbno];
  298. if (conn)
  299. tasklet_schedule(&conn->rx_tsklet);
  300. spin_unlock_irqrestore(&smcd->lock, flags);
  301. }
  302. EXPORT_SYMBOL_GPL(smcd_handle_irq);