1 // SPDX-License-Identifier: GPL-2.0
2 /* Shared Memory Communications Direct over ISM devices (SMC-D)
4 * Functions for ISM device.
6 * Copyright IBM Corp. 2018
9 #include <linux/if_vlan.h>
10 #include <linux/spinlock.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
19 #include "smc_netlink.h"
20 #include "linux/ism.h"
22 struct smcd_dev_list smcd_dev_list = {
23 .list = LIST_HEAD_INIT(smcd_dev_list.list),
24 .mutex = __MUTEX_INITIALIZER(smcd_dev_list.mutex)
27 static bool smc_ism_v2_capable;
28 static u8 smc_ism_v2_system_eid[SMC_MAX_EID_LEN];
30 #if IS_ENABLED(CONFIG_ISM)
31 static void smcd_register_dev(struct ism_dev *ism);
32 static void smcd_unregister_dev(struct ism_dev *ism);
33 static void smcd_handle_event(struct ism_dev *ism, struct ism_event *event);
34 static void smcd_handle_irq(struct ism_dev *ism, unsigned int dmbno,
37 static struct ism_client smc_ism_client = {
39 .add = smcd_register_dev,
40 .remove = smcd_unregister_dev,
41 .handle_event = smcd_handle_event,
42 .handle_irq = smcd_handle_irq,
46 static void smc_ism_create_system_eid(void)
48 struct smc_ism_seid *seid =
49 (struct smc_ism_seid *)smc_ism_v2_system_eid;
50 #if IS_ENABLED(CONFIG_S390)
55 memcpy(seid->seid_string, "IBM-SYSZ-ISMSEID00000000", 24);
57 ident_tail = (u16)(id.ident & SMC_ISM_IDENT_MASK);
58 snprintf(tmp, 5, "%04X", ident_tail);
59 memcpy(seid->serial_number, tmp, 4);
60 snprintf(tmp, 5, "%04X", id.machine);
61 memcpy(seid->type, tmp, 4);
63 memset(seid, 0, SMC_MAX_EID_LEN);
67 /* Test if an ISM communication is possible - same CPC */
68 int smc_ism_cantalk(struct smcd_gid *peer_gid, unsigned short vlan_id,
69 struct smcd_dev *smcd)
71 return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0,
75 void smc_ism_get_system_eid(u8 **eid)
77 if (!smc_ism_v2_capable)
80 *eid = smc_ism_v2_system_eid;
83 u16 smc_ism_get_chid(struct smcd_dev *smcd)
85 return smcd->ops->get_chid(smcd);
88 /* HW supports ISM V2 and thus System EID is defined */
89 bool smc_ism_is_v2_capable(void)
91 return smc_ism_v2_capable;
94 void smc_ism_set_v2_capable(void)
96 smc_ism_v2_capable = true;
99 /* Set a connection using this DMBE. */
100 void smc_ism_set_conn(struct smc_connection *conn)
104 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
105 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
106 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
109 /* Unset a connection using this DMBE. */
110 void smc_ism_unset_conn(struct smc_connection *conn)
117 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
118 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
119 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
122 /* Register a VLAN identifier with the ISM device. Use a reference count
123 * and add a VLAN identifier only when the first DMB using this VLAN is
126 int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
128 struct smc_ism_vlanid *new_vlan, *vlan;
132 if (!vlanid) /* No valid vlan id */
134 if (!smcd->ops->add_vlan_id)
137 /* create new vlan entry, in case we need it */
138 new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
141 new_vlan->vlanid = vlanid;
142 refcount_set(&new_vlan->refcnt, 1);
144 /* if there is an existing entry, increase count and return */
145 spin_lock_irqsave(&smcd->lock, flags);
146 list_for_each_entry(vlan, &smcd->vlan, list) {
147 if (vlan->vlanid == vlanid) {
148 refcount_inc(&vlan->refcnt);
154 /* no existing entry found.
155 * add new entry to device; might fail, e.g., if HW limit reached
157 if (smcd->ops->add_vlan_id(smcd, vlanid)) {
162 list_add_tail(&new_vlan->list, &smcd->vlan);
164 spin_unlock_irqrestore(&smcd->lock, flags);
168 /* Unregister a VLAN identifier with the ISM device. Use a reference count
169 * and remove a VLAN identifier only when the last DMB using this VLAN is
172 int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
174 struct smc_ism_vlanid *vlan;
179 if (!vlanid) /* No valid vlan id */
181 if (!smcd->ops->del_vlan_id)
184 spin_lock_irqsave(&smcd->lock, flags);
185 list_for_each_entry(vlan, &smcd->vlan, list) {
186 if (vlan->vlanid == vlanid) {
187 if (!refcount_dec_and_test(&vlan->refcnt))
195 goto out; /* VLAN id not in table */
198 /* Found and the last reference just gone */
199 if (smcd->ops->del_vlan_id(smcd, vlanid))
201 list_del(&vlan->list);
204 spin_unlock_irqrestore(&smcd->lock, flags);
208 int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
213 if (!dmb_desc->dma_addr)
216 memset(&dmb, 0, sizeof(dmb));
217 dmb.dmb_tok = dmb_desc->token;
218 dmb.sba_idx = dmb_desc->sba_idx;
219 dmb.cpu_addr = dmb_desc->cpu_addr;
220 dmb.dma_addr = dmb_desc->dma_addr;
221 dmb.dmb_len = dmb_desc->len;
222 rc = smcd->ops->unregister_dmb(smcd, &dmb);
223 if (!rc || rc == ISM_ERROR) {
224 dmb_desc->cpu_addr = NULL;
225 dmb_desc->dma_addr = 0;
231 int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
232 struct smc_buf_desc *dmb_desc)
237 memset(&dmb, 0, sizeof(dmb));
238 dmb.dmb_len = dmb_len;
239 dmb.sba_idx = dmb_desc->sba_idx;
240 dmb.vlan_id = lgr->vlan_id;
241 dmb.rgid = lgr->peer_gid.gid;
242 rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb, lgr->smcd->client);
244 dmb_desc->sba_idx = dmb.sba_idx;
245 dmb_desc->token = dmb.dmb_tok;
246 dmb_desc->cpu_addr = dmb.cpu_addr;
247 dmb_desc->dma_addr = dmb.dma_addr;
248 dmb_desc->len = dmb.dmb_len;
253 bool smc_ism_support_dmb_nocopy(struct smcd_dev *smcd)
255 /* for now only loopback-ism supports
256 * merging sndbuf with peer DMB to avoid
257 * data copies between them.
259 return (smcd->ops->support_dmb_nocopy &&
260 smcd->ops->support_dmb_nocopy(smcd));
263 int smc_ism_attach_dmb(struct smcd_dev *dev, u64 token,
264 struct smc_buf_desc *dmb_desc)
269 if (!dev->ops->attach_dmb)
272 memset(&dmb, 0, sizeof(dmb));
274 rc = dev->ops->attach_dmb(dev, &dmb);
276 dmb_desc->sba_idx = dmb.sba_idx;
277 dmb_desc->token = dmb.dmb_tok;
278 dmb_desc->cpu_addr = dmb.cpu_addr;
279 dmb_desc->dma_addr = dmb.dma_addr;
280 dmb_desc->len = dmb.dmb_len;
285 int smc_ism_detach_dmb(struct smcd_dev *dev, u64 token)
287 if (!dev->ops->detach_dmb)
290 return dev->ops->detach_dmb(dev, token);
293 static int smc_nl_handle_smcd_dev(struct smcd_dev *smcd,
295 struct netlink_callback *cb)
297 char smc_pnet[SMC_MAX_PNETID_LEN + 1];
298 struct smc_pci_dev smc_pci_dev;
299 struct nlattr *port_attrs;
300 struct nlattr *attrs;
306 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
307 &smc_gen_nl_family, NLM_F_MULTI,
308 SMC_NETLINK_GET_DEV_SMCD);
311 attrs = nla_nest_start(skb, SMC_GEN_DEV_SMCD);
314 use_cnt = atomic_read(&smcd->lgr_cnt);
315 if (nla_put_u32(skb, SMC_NLA_DEV_USE_CNT, use_cnt))
317 if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRIT, use_cnt > 0))
319 memset(&smc_pci_dev, 0, sizeof(smc_pci_dev));
320 smc_set_pci_values(to_pci_dev(ism->dev.parent), &smc_pci_dev);
321 if (nla_put_u32(skb, SMC_NLA_DEV_PCI_FID, smc_pci_dev.pci_fid))
323 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_CHID, smc_pci_dev.pci_pchid))
325 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_VENDOR, smc_pci_dev.pci_vendor))
327 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_DEVICE, smc_pci_dev.pci_device))
329 if (nla_put_string(skb, SMC_NLA_DEV_PCI_ID, smc_pci_dev.pci_id))
332 port_attrs = nla_nest_start(skb, SMC_NLA_DEV_PORT);
335 if (nla_put_u8(skb, SMC_NLA_DEV_PORT_PNET_USR, smcd->pnetid_by_user))
337 memcpy(smc_pnet, smcd->pnetid, SMC_MAX_PNETID_LEN);
338 smc_pnet[SMC_MAX_PNETID_LEN] = 0;
339 if (nla_put_string(skb, SMC_NLA_DEV_PORT_PNETID, smc_pnet))
342 nla_nest_end(skb, port_attrs);
343 nla_nest_end(skb, attrs);
344 genlmsg_end(skb, nlh);
348 nla_nest_cancel(skb, port_attrs);
350 nla_nest_cancel(skb, attrs);
352 nlmsg_cancel(skb, nlh);
357 static void smc_nl_prep_smcd_dev(struct smcd_dev_list *dev_list,
359 struct netlink_callback *cb)
361 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
362 int snum = cb_ctx->pos[0];
363 struct smcd_dev *smcd;
366 mutex_lock(&dev_list->mutex);
367 list_for_each_entry(smcd, &dev_list->list, list) {
370 if (smc_ism_is_loopback(smcd))
372 if (smc_nl_handle_smcd_dev(smcd, skb, cb))
378 mutex_unlock(&dev_list->mutex);
379 cb_ctx->pos[0] = num;
382 int smcd_nl_get_device(struct sk_buff *skb, struct netlink_callback *cb)
384 smc_nl_prep_smcd_dev(&smcd_dev_list, skb, cb);
388 #if IS_ENABLED(CONFIG_ISM)
389 struct smc_ism_event_work {
390 struct work_struct work;
391 struct smcd_dev *smcd;
392 struct ism_event event;
395 #define ISM_EVENT_REQUEST 0x0001
396 #define ISM_EVENT_RESPONSE 0x0002
397 #define ISM_EVENT_REQUEST_IR 0x00000001
398 #define ISM_EVENT_CODE_SHUTDOWN 0x80
399 #define ISM_EVENT_CODE_TESTLINK 0x83
401 union smcd_sw_event_info {
404 u8 uid[SMC_LGR_ID_SIZE];
405 unsigned short vlan_id;
410 static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
412 struct smcd_gid peer_gid = { .gid = wrk->event.tok,
414 union smcd_sw_event_info ev_info;
416 ev_info.info = wrk->event.info;
417 switch (wrk->event.code) {
418 case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
419 smc_smcd_terminate(wrk->smcd, &peer_gid, ev_info.vlan_id);
421 case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
422 if (ev_info.code == ISM_EVENT_REQUEST &&
423 wrk->smcd->ops->signal_event) {
424 ev_info.code = ISM_EVENT_RESPONSE;
425 wrk->smcd->ops->signal_event(wrk->smcd,
427 ISM_EVENT_REQUEST_IR,
428 ISM_EVENT_CODE_TESTLINK,
435 /* worker for SMC-D events */
436 static void smc_ism_event_work(struct work_struct *work)
438 struct smc_ism_event_work *wrk =
439 container_of(work, struct smc_ism_event_work, work);
440 struct smcd_gid smcd_gid = { .gid = wrk->event.tok,
443 switch (wrk->event.type) {
444 case ISM_EVENT_GID: /* GID event, token is peer GID */
445 smc_smcd_terminate(wrk->smcd, &smcd_gid, VLAN_VID_MASK);
449 case ISM_EVENT_SWR: /* Software defined event */
450 smcd_handle_sw_event(wrk);
456 static struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
457 const struct smcd_ops *ops, int max_dmbs)
459 struct smcd_dev *smcd;
461 smcd = devm_kzalloc(parent, sizeof(*smcd), GFP_KERNEL);
464 smcd->conn = devm_kcalloc(parent, max_dmbs,
465 sizeof(struct smc_connection *), GFP_KERNEL);
469 smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
470 WQ_MEM_RECLAIM, name);
476 spin_lock_init(&smcd->lock);
477 spin_lock_init(&smcd->lgr_lock);
478 INIT_LIST_HEAD(&smcd->vlan);
479 INIT_LIST_HEAD(&smcd->lgr_list);
480 init_waitqueue_head(&smcd->lgrs_deleted);
484 static void smcd_register_dev(struct ism_dev *ism)
486 const struct smcd_ops *ops = ism_get_smcd_ops();
487 struct smcd_dev *smcd, *fentry;
492 smcd = smcd_alloc_dev(&ism->pdev->dev, dev_name(&ism->pdev->dev), ops,
497 smcd->client = &smc_ism_client;
498 ism_set_priv(ism, &smc_ism_client, smcd);
499 if (smc_pnetid_by_dev_port(&ism->pdev->dev, 0, smcd->pnetid))
500 smc_pnetid_by_table_smcd(smcd);
502 if (smcd->ops->supports_v2())
503 smc_ism_set_v2_capable();
504 mutex_lock(&smcd_dev_list.mutex);
506 * - devices without pnetid before devices with pnetid;
507 * - loopback-ism always at the very beginning;
509 if (!smcd->pnetid[0]) {
510 fentry = list_first_entry_or_null(&smcd_dev_list.list,
511 struct smcd_dev, list);
512 if (fentry && smc_ism_is_loopback(fentry))
513 list_add(&smcd->list, &fentry->list);
515 list_add(&smcd->list, &smcd_dev_list.list);
517 list_add_tail(&smcd->list, &smcd_dev_list.list);
519 mutex_unlock(&smcd_dev_list.mutex);
521 pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n",
522 dev_name(&ism->dev), smcd->pnetid,
523 smcd->pnetid_by_user ? " (user defined)" : "");
528 static void smcd_unregister_dev(struct ism_dev *ism)
530 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
532 pr_warn_ratelimited("smc: removing smcd device %s\n",
533 dev_name(&ism->dev));
534 smcd->going_away = 1;
535 smc_smcd_terminate_all(smcd);
536 mutex_lock(&smcd_dev_list.mutex);
537 list_del_init(&smcd->list);
538 mutex_unlock(&smcd_dev_list.mutex);
539 destroy_workqueue(smcd->event_wq);
542 /* SMCD Device event handler. Called from ISM device interrupt handler.
543 * Parameters are ism device pointer,
544 * - event->type (0 --> DMB, 1 --> GID),
545 * - event->code (event code),
546 * - event->tok (either DMB token when event type 0, or GID when event type 1)
547 * - event->time (time of day)
548 * - event->info (debug info).
551 * - Function called in IRQ context from ISM device driver event handler.
553 static void smcd_handle_event(struct ism_dev *ism, struct ism_event *event)
555 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
556 struct smc_ism_event_work *wrk;
558 if (smcd->going_away)
560 /* copy event to event work queue, and let it be handled there */
561 wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
564 INIT_WORK(&wrk->work, smc_ism_event_work);
567 queue_work(smcd->event_wq, &wrk->work);
570 /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
571 * Parameters are the ism device pointer, DMB number, and the DMBE bitmask.
572 * Find the connection and schedule the tasklet for this connection.
575 * - Function called in IRQ context from ISM device driver IRQ handler.
577 static void smcd_handle_irq(struct ism_dev *ism, unsigned int dmbno,
580 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
581 struct smc_connection *conn = NULL;
584 spin_lock_irqsave(&smcd->lock, flags);
585 conn = smcd->conn[dmbno];
586 if (conn && !conn->killed)
587 tasklet_schedule(&conn->rx_tsklet);
588 spin_unlock_irqrestore(&smcd->lock, flags);
592 int smc_ism_signal_shutdown(struct smc_link_group *lgr)
595 #if IS_ENABLED(CONFIG_ISM)
596 union smcd_sw_event_info ev_info;
598 if (lgr->peer_shutdown)
600 if (!lgr->smcd->ops->signal_event)
603 memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
604 ev_info.vlan_id = lgr->vlan_id;
605 ev_info.code = ISM_EVENT_REQUEST;
606 rc = lgr->smcd->ops->signal_event(lgr->smcd, &lgr->peer_gid,
607 ISM_EVENT_REQUEST_IR,
608 ISM_EVENT_CODE_SHUTDOWN,
614 int smc_ism_init(void)
618 smc_ism_v2_capable = false;
619 smc_ism_create_system_eid();
621 #if IS_ENABLED(CONFIG_ISM)
622 rc = ism_register_client(&smc_ism_client);
627 void smc_ism_exit(void)
629 #if IS_ENABLED(CONFIG_ISM)
630 ism_unregister_client(&smc_ism_client);