smc_ism.c 13 KB

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