smc_ism.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/mutex.h>
  10. #include <linux/slab.h>
  11. #include <asm/page.h>
  12. #include "smc.h"
  13. #include "smc_core.h"
  14. #include "smc_ism.h"
  15. #include "smc_pnet.h"
  16. struct smcd_dev_list smcd_dev_list = {
  17. .list = LIST_HEAD_INIT(smcd_dev_list.list),
  18. .mutex = __MUTEX_INITIALIZER(smcd_dev_list.mutex)
  19. };
  20. bool smc_ism_v2_capable;
  21. /* Test if an ISM communication is possible - same CPC */
  22. int smc_ism_cantalk(u64 peer_gid, unsigned short vlan_id, struct smcd_dev *smcd)
  23. {
  24. return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0,
  25. vlan_id);
  26. }
  27. int smc_ism_write(struct smcd_dev *smcd, const struct smc_ism_position *pos,
  28. void *data, size_t len)
  29. {
  30. int rc;
  31. rc = smcd->ops->move_data(smcd, pos->token, pos->index, pos->signal,
  32. pos->offset, data, len);
  33. return rc < 0 ? rc : 0;
  34. }
  35. void smc_ism_get_system_eid(struct smcd_dev *smcd, u8 **eid)
  36. {
  37. smcd->ops->get_system_eid(smcd, eid);
  38. }
  39. u16 smc_ism_get_chid(struct smcd_dev *smcd)
  40. {
  41. return smcd->ops->get_chid(smcd);
  42. }
  43. /* Set a connection using this DMBE. */
  44. void smc_ism_set_conn(struct smc_connection *conn)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
  48. conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
  49. spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
  50. }
  51. /* Unset a connection using this DMBE. */
  52. void smc_ism_unset_conn(struct smc_connection *conn)
  53. {
  54. unsigned long flags;
  55. if (!conn->rmb_desc)
  56. return;
  57. spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
  58. conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
  59. spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
  60. }
  61. /* Register a VLAN identifier with the ISM device. Use a reference count
  62. * and add a VLAN identifier only when the first DMB using this VLAN is
  63. * registered.
  64. */
  65. int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
  66. {
  67. struct smc_ism_vlanid *new_vlan, *vlan;
  68. unsigned long flags;
  69. int rc = 0;
  70. if (!vlanid) /* No valid vlan id */
  71. return -EINVAL;
  72. /* create new vlan entry, in case we need it */
  73. new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
  74. if (!new_vlan)
  75. return -ENOMEM;
  76. new_vlan->vlanid = vlanid;
  77. refcount_set(&new_vlan->refcnt, 1);
  78. /* if there is an existing entry, increase count and return */
  79. spin_lock_irqsave(&smcd->lock, flags);
  80. list_for_each_entry(vlan, &smcd->vlan, list) {
  81. if (vlan->vlanid == vlanid) {
  82. refcount_inc(&vlan->refcnt);
  83. kfree(new_vlan);
  84. goto out;
  85. }
  86. }
  87. /* no existing entry found.
  88. * add new entry to device; might fail, e.g., if HW limit reached
  89. */
  90. if (smcd->ops->add_vlan_id(smcd, vlanid)) {
  91. kfree(new_vlan);
  92. rc = -EIO;
  93. goto out;
  94. }
  95. list_add_tail(&new_vlan->list, &smcd->vlan);
  96. out:
  97. spin_unlock_irqrestore(&smcd->lock, flags);
  98. return rc;
  99. }
  100. /* Unregister a VLAN identifier with the ISM device. Use a reference count
  101. * and remove a VLAN identifier only when the last DMB using this VLAN is
  102. * unregistered.
  103. */
  104. int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
  105. {
  106. struct smc_ism_vlanid *vlan;
  107. unsigned long flags;
  108. bool found = false;
  109. int rc = 0;
  110. if (!vlanid) /* No valid vlan id */
  111. return -EINVAL;
  112. spin_lock_irqsave(&smcd->lock, flags);
  113. list_for_each_entry(vlan, &smcd->vlan, list) {
  114. if (vlan->vlanid == vlanid) {
  115. if (!refcount_dec_and_test(&vlan->refcnt))
  116. goto out;
  117. found = true;
  118. break;
  119. }
  120. }
  121. if (!found) {
  122. rc = -ENOENT;
  123. goto out; /* VLAN id not in table */
  124. }
  125. /* Found and the last reference just gone */
  126. if (smcd->ops->del_vlan_id(smcd, vlanid))
  127. rc = -EIO;
  128. list_del(&vlan->list);
  129. kfree(vlan);
  130. out:
  131. spin_unlock_irqrestore(&smcd->lock, flags);
  132. return rc;
  133. }
  134. int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
  135. {
  136. struct smcd_dmb dmb;
  137. int rc = 0;
  138. if (!dmb_desc->dma_addr)
  139. return rc;
  140. memset(&dmb, 0, sizeof(dmb));
  141. dmb.dmb_tok = dmb_desc->token;
  142. dmb.sba_idx = dmb_desc->sba_idx;
  143. dmb.cpu_addr = dmb_desc->cpu_addr;
  144. dmb.dma_addr = dmb_desc->dma_addr;
  145. dmb.dmb_len = dmb_desc->len;
  146. rc = smcd->ops->unregister_dmb(smcd, &dmb);
  147. if (!rc || rc == ISM_ERROR) {
  148. dmb_desc->cpu_addr = NULL;
  149. dmb_desc->dma_addr = 0;
  150. }
  151. return rc;
  152. }
  153. int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
  154. struct smc_buf_desc *dmb_desc)
  155. {
  156. struct smcd_dmb dmb;
  157. int rc;
  158. memset(&dmb, 0, sizeof(dmb));
  159. dmb.dmb_len = dmb_len;
  160. dmb.sba_idx = dmb_desc->sba_idx;
  161. dmb.vlan_id = lgr->vlan_id;
  162. dmb.rgid = lgr->peer_gid;
  163. rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb);
  164. if (!rc) {
  165. dmb_desc->sba_idx = dmb.sba_idx;
  166. dmb_desc->token = dmb.dmb_tok;
  167. dmb_desc->cpu_addr = dmb.cpu_addr;
  168. dmb_desc->dma_addr = dmb.dma_addr;
  169. dmb_desc->len = dmb.dmb_len;
  170. }
  171. return rc;
  172. }
  173. struct smc_ism_event_work {
  174. struct work_struct work;
  175. struct smcd_dev *smcd;
  176. struct smcd_event event;
  177. };
  178. #define ISM_EVENT_REQUEST 0x0001
  179. #define ISM_EVENT_RESPONSE 0x0002
  180. #define ISM_EVENT_REQUEST_IR 0x00000001
  181. #define ISM_EVENT_CODE_SHUTDOWN 0x80
  182. #define ISM_EVENT_CODE_TESTLINK 0x83
  183. union smcd_sw_event_info {
  184. u64 info;
  185. struct {
  186. u8 uid[SMC_LGR_ID_SIZE];
  187. unsigned short vlan_id;
  188. u16 code;
  189. };
  190. };
  191. static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
  192. {
  193. union smcd_sw_event_info ev_info;
  194. ev_info.info = wrk->event.info;
  195. switch (wrk->event.code) {
  196. case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
  197. smc_smcd_terminate(wrk->smcd, wrk->event.tok, ev_info.vlan_id);
  198. break;
  199. case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
  200. if (ev_info.code == ISM_EVENT_REQUEST) {
  201. ev_info.code = ISM_EVENT_RESPONSE;
  202. wrk->smcd->ops->signal_event(wrk->smcd,
  203. wrk->event.tok,
  204. ISM_EVENT_REQUEST_IR,
  205. ISM_EVENT_CODE_TESTLINK,
  206. ev_info.info);
  207. }
  208. break;
  209. }
  210. }
  211. int smc_ism_signal_shutdown(struct smc_link_group *lgr)
  212. {
  213. int rc;
  214. union smcd_sw_event_info ev_info;
  215. if (lgr->peer_shutdown)
  216. return 0;
  217. memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
  218. ev_info.vlan_id = lgr->vlan_id;
  219. ev_info.code = ISM_EVENT_REQUEST;
  220. rc = lgr->smcd->ops->signal_event(lgr->smcd, lgr->peer_gid,
  221. ISM_EVENT_REQUEST_IR,
  222. ISM_EVENT_CODE_SHUTDOWN,
  223. ev_info.info);
  224. return rc;
  225. }
  226. /* worker for SMC-D events */
  227. static void smc_ism_event_work(struct work_struct *work)
  228. {
  229. struct smc_ism_event_work *wrk =
  230. container_of(work, struct smc_ism_event_work, work);
  231. switch (wrk->event.type) {
  232. case ISM_EVENT_GID: /* GID event, token is peer GID */
  233. smc_smcd_terminate(wrk->smcd, wrk->event.tok, VLAN_VID_MASK);
  234. break;
  235. case ISM_EVENT_DMB:
  236. break;
  237. case ISM_EVENT_SWR: /* Software defined event */
  238. smcd_handle_sw_event(wrk);
  239. break;
  240. }
  241. kfree(wrk);
  242. }
  243. static void smcd_release(struct device *dev)
  244. {
  245. struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev);
  246. kfree(smcd->conn);
  247. kfree(smcd);
  248. }
  249. struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
  250. const struct smcd_ops *ops, int max_dmbs)
  251. {
  252. struct smcd_dev *smcd;
  253. smcd = kzalloc(sizeof(*smcd), GFP_KERNEL);
  254. if (!smcd)
  255. return NULL;
  256. smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *),
  257. GFP_KERNEL);
  258. if (!smcd->conn) {
  259. kfree(smcd);
  260. return NULL;
  261. }
  262. smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
  263. WQ_MEM_RECLAIM, name);
  264. if (!smcd->event_wq) {
  265. kfree(smcd->conn);
  266. kfree(smcd);
  267. return NULL;
  268. }
  269. smcd->dev.parent = parent;
  270. smcd->dev.release = smcd_release;
  271. device_initialize(&smcd->dev);
  272. dev_set_name(&smcd->dev, name);
  273. smcd->ops = ops;
  274. if (smc_pnetid_by_dev_port(parent, 0, smcd->pnetid))
  275. smc_pnetid_by_table_smcd(smcd);
  276. spin_lock_init(&smcd->lock);
  277. spin_lock_init(&smcd->lgr_lock);
  278. INIT_LIST_HEAD(&smcd->vlan);
  279. INIT_LIST_HEAD(&smcd->lgr_list);
  280. init_waitqueue_head(&smcd->lgrs_deleted);
  281. return smcd;
  282. }
  283. EXPORT_SYMBOL_GPL(smcd_alloc_dev);
  284. int smcd_register_dev(struct smcd_dev *smcd)
  285. {
  286. int rc;
  287. mutex_lock(&smcd_dev_list.mutex);
  288. if (list_empty(&smcd_dev_list.list)) {
  289. u8 *system_eid = NULL;
  290. smc_ism_get_system_eid(smcd, &system_eid);
  291. if (system_eid[24] != '0' || system_eid[28] != '0')
  292. smc_ism_v2_capable = true;
  293. }
  294. /* sort list: devices without pnetid before devices with pnetid */
  295. if (smcd->pnetid[0])
  296. list_add_tail(&smcd->list, &smcd_dev_list.list);
  297. else
  298. list_add(&smcd->list, &smcd_dev_list.list);
  299. mutex_unlock(&smcd_dev_list.mutex);
  300. pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n",
  301. dev_name(&smcd->dev), smcd->pnetid,
  302. smcd->pnetid_by_user ? " (user defined)" : "");
  303. rc = device_add(&smcd->dev);
  304. if (rc) {
  305. mutex_lock(&smcd_dev_list.mutex);
  306. list_del(&smcd->list);
  307. mutex_unlock(&smcd_dev_list.mutex);
  308. }
  309. return rc;
  310. }
  311. EXPORT_SYMBOL_GPL(smcd_register_dev);
  312. void smcd_unregister_dev(struct smcd_dev *smcd)
  313. {
  314. pr_warn_ratelimited("smc: removing smcd device %s\n",
  315. dev_name(&smcd->dev));
  316. mutex_lock(&smcd_dev_list.mutex);
  317. list_del_init(&smcd->list);
  318. mutex_unlock(&smcd_dev_list.mutex);
  319. smcd->going_away = 1;
  320. smc_smcd_terminate_all(smcd);
  321. flush_workqueue(smcd->event_wq);
  322. destroy_workqueue(smcd->event_wq);
  323. device_del(&smcd->dev);
  324. }
  325. EXPORT_SYMBOL_GPL(smcd_unregister_dev);
  326. void smcd_free_dev(struct smcd_dev *smcd)
  327. {
  328. put_device(&smcd->dev);
  329. }
  330. EXPORT_SYMBOL_GPL(smcd_free_dev);
  331. /* SMCD Device event handler. Called from ISM device interrupt handler.
  332. * Parameters are smcd device pointer,
  333. * - event->type (0 --> DMB, 1 --> GID),
  334. * - event->code (event code),
  335. * - event->tok (either DMB token when event type 0, or GID when event type 1)
  336. * - event->time (time of day)
  337. * - event->info (debug info).
  338. *
  339. * Context:
  340. * - Function called in IRQ context from ISM device driver event handler.
  341. */
  342. void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event)
  343. {
  344. struct smc_ism_event_work *wrk;
  345. if (smcd->going_away)
  346. return;
  347. /* copy event to event work queue, and let it be handled there */
  348. wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
  349. if (!wrk)
  350. return;
  351. INIT_WORK(&wrk->work, smc_ism_event_work);
  352. wrk->smcd = smcd;
  353. wrk->event = *event;
  354. queue_work(smcd->event_wq, &wrk->work);
  355. }
  356. EXPORT_SYMBOL_GPL(smcd_handle_event);
  357. /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
  358. * Parameters are smcd device pointer and DMB number. Find the connection and
  359. * schedule the tasklet for this connection.
  360. *
  361. * Context:
  362. * - Function called in IRQ context from ISM device driver IRQ handler.
  363. */
  364. void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno)
  365. {
  366. struct smc_connection *conn = NULL;
  367. unsigned long flags;
  368. spin_lock_irqsave(&smcd->lock, flags);
  369. conn = smcd->conn[dmbno];
  370. if (conn && !conn->killed)
  371. tasklet_schedule(&conn->rx_tsklet);
  372. spin_unlock_irqrestore(&smcd->lock, flags);
  373. }
  374. EXPORT_SYMBOL_GPL(smcd_handle_irq);
  375. void __init smc_ism_init(void)
  376. {
  377. smc_ism_v2_capable = false;
  378. }