smc_ism.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_SHUTDOWN 0x80
  164. #define ISM_EVENT_CODE_TESTLINK 0x83
  165. union smcd_sw_event_info {
  166. u64 info;
  167. struct {
  168. u8 uid[SMC_LGR_ID_SIZE];
  169. unsigned short vlan_id;
  170. u16 code;
  171. };
  172. };
  173. static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
  174. {
  175. union smcd_sw_event_info ev_info;
  176. ev_info.info = wrk->event.info;
  177. switch (wrk->event.code) {
  178. case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
  179. smc_smcd_terminate(wrk->smcd, wrk->event.tok, ev_info.vlan_id);
  180. break;
  181. case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
  182. if (ev_info.code == ISM_EVENT_REQUEST) {
  183. ev_info.code = ISM_EVENT_RESPONSE;
  184. wrk->smcd->ops->signal_event(wrk->smcd,
  185. wrk->event.tok,
  186. ISM_EVENT_REQUEST_IR,
  187. ISM_EVENT_CODE_TESTLINK,
  188. ev_info.info);
  189. }
  190. break;
  191. }
  192. }
  193. int smc_ism_signal_shutdown(struct smc_link_group *lgr)
  194. {
  195. int rc;
  196. union smcd_sw_event_info ev_info;
  197. memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
  198. ev_info.vlan_id = lgr->vlan_id;
  199. ev_info.code = ISM_EVENT_REQUEST;
  200. rc = lgr->smcd->ops->signal_event(lgr->smcd, lgr->peer_gid,
  201. ISM_EVENT_REQUEST_IR,
  202. ISM_EVENT_CODE_SHUTDOWN,
  203. ev_info.info);
  204. return rc;
  205. }
  206. /* worker for SMC-D events */
  207. static void smc_ism_event_work(struct work_struct *work)
  208. {
  209. struct smc_ism_event_work *wrk =
  210. container_of(work, struct smc_ism_event_work, work);
  211. switch (wrk->event.type) {
  212. case ISM_EVENT_GID: /* GID event, token is peer GID */
  213. smc_smcd_terminate(wrk->smcd, wrk->event.tok, VLAN_VID_MASK);
  214. break;
  215. case ISM_EVENT_DMB:
  216. break;
  217. case ISM_EVENT_SWR: /* Software defined event */
  218. smcd_handle_sw_event(wrk);
  219. break;
  220. }
  221. kfree(wrk);
  222. }
  223. static void smcd_release(struct device *dev)
  224. {
  225. struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev);
  226. kfree(smcd->conn);
  227. kfree(smcd);
  228. }
  229. struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
  230. const struct smcd_ops *ops, int max_dmbs)
  231. {
  232. struct smcd_dev *smcd;
  233. smcd = kzalloc(sizeof(*smcd), GFP_KERNEL);
  234. if (!smcd)
  235. return NULL;
  236. smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *),
  237. GFP_KERNEL);
  238. if (!smcd->conn) {
  239. kfree(smcd);
  240. return NULL;
  241. }
  242. smcd->dev.parent = parent;
  243. smcd->dev.release = smcd_release;
  244. device_initialize(&smcd->dev);
  245. dev_set_name(&smcd->dev, name);
  246. smcd->ops = ops;
  247. smc_pnetid_by_dev_port(parent, 0, smcd->pnetid);
  248. spin_lock_init(&smcd->lock);
  249. INIT_LIST_HEAD(&smcd->vlan);
  250. smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
  251. WQ_MEM_RECLAIM, name);
  252. return smcd;
  253. }
  254. EXPORT_SYMBOL_GPL(smcd_alloc_dev);
  255. int smcd_register_dev(struct smcd_dev *smcd)
  256. {
  257. spin_lock(&smcd_dev_list.lock);
  258. list_add_tail(&smcd->list, &smcd_dev_list.list);
  259. spin_unlock(&smcd_dev_list.lock);
  260. return device_add(&smcd->dev);
  261. }
  262. EXPORT_SYMBOL_GPL(smcd_register_dev);
  263. void smcd_unregister_dev(struct smcd_dev *smcd)
  264. {
  265. spin_lock(&smcd_dev_list.lock);
  266. list_del(&smcd->list);
  267. spin_unlock(&smcd_dev_list.lock);
  268. flush_workqueue(smcd->event_wq);
  269. destroy_workqueue(smcd->event_wq);
  270. smc_smcd_terminate(smcd, 0, VLAN_VID_MASK);
  271. device_del(&smcd->dev);
  272. }
  273. EXPORT_SYMBOL_GPL(smcd_unregister_dev);
  274. void smcd_free_dev(struct smcd_dev *smcd)
  275. {
  276. put_device(&smcd->dev);
  277. }
  278. EXPORT_SYMBOL_GPL(smcd_free_dev);
  279. /* SMCD Device event handler. Called from ISM device interrupt handler.
  280. * Parameters are smcd device pointer,
  281. * - event->type (0 --> DMB, 1 --> GID),
  282. * - event->code (event code),
  283. * - event->tok (either DMB token when event type 0, or GID when event type 1)
  284. * - event->time (time of day)
  285. * - event->info (debug info).
  286. *
  287. * Context:
  288. * - Function called in IRQ context from ISM device driver event handler.
  289. */
  290. void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event)
  291. {
  292. struct smc_ism_event_work *wrk;
  293. /* copy event to event work queue, and let it be handled there */
  294. wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
  295. if (!wrk)
  296. return;
  297. INIT_WORK(&wrk->work, smc_ism_event_work);
  298. wrk->smcd = smcd;
  299. wrk->event = *event;
  300. queue_work(smcd->event_wq, &wrk->work);
  301. }
  302. EXPORT_SYMBOL_GPL(smcd_handle_event);
  303. /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
  304. * Parameters are smcd device pointer and DMB number. Find the connection and
  305. * schedule the tasklet for this connection.
  306. *
  307. * Context:
  308. * - Function called in IRQ context from ISM device driver IRQ handler.
  309. */
  310. void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno)
  311. {
  312. struct smc_connection *conn = NULL;
  313. unsigned long flags;
  314. spin_lock_irqsave(&smcd->lock, flags);
  315. conn = smcd->conn[dmbno];
  316. if (conn)
  317. tasklet_schedule(&conn->rx_tsklet);
  318. spin_unlock_irqrestore(&smcd->lock, flags);
  319. }
  320. EXPORT_SYMBOL_GPL(smcd_handle_irq);