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 /* Set a connection using this DMBE. */
95 void smc_ism_set_conn(struct smc_connection *conn)
99 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
100 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
101 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
104 /* Unset a connection using this DMBE. */
105 void smc_ism_unset_conn(struct smc_connection *conn)
112 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
113 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
114 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
117 /* Register a VLAN identifier with the ISM device. Use a reference count
118 * and add a VLAN identifier only when the first DMB using this VLAN is
121 int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
123 struct smc_ism_vlanid *new_vlan, *vlan;
127 if (!vlanid) /* No valid vlan id */
130 /* create new vlan entry, in case we need it */
131 new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
134 new_vlan->vlanid = vlanid;
135 refcount_set(&new_vlan->refcnt, 1);
137 /* if there is an existing entry, increase count and return */
138 spin_lock_irqsave(&smcd->lock, flags);
139 list_for_each_entry(vlan, &smcd->vlan, list) {
140 if (vlan->vlanid == vlanid) {
141 refcount_inc(&vlan->refcnt);
147 /* no existing entry found.
148 * add new entry to device; might fail, e.g., if HW limit reached
150 if (smcd->ops->add_vlan_id(smcd, vlanid)) {
155 list_add_tail(&new_vlan->list, &smcd->vlan);
157 spin_unlock_irqrestore(&smcd->lock, flags);
161 /* Unregister a VLAN identifier with the ISM device. Use a reference count
162 * and remove a VLAN identifier only when the last DMB using this VLAN is
165 int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
167 struct smc_ism_vlanid *vlan;
172 if (!vlanid) /* No valid vlan id */
175 spin_lock_irqsave(&smcd->lock, flags);
176 list_for_each_entry(vlan, &smcd->vlan, list) {
177 if (vlan->vlanid == vlanid) {
178 if (!refcount_dec_and_test(&vlan->refcnt))
186 goto out; /* VLAN id not in table */
189 /* Found and the last reference just gone */
190 if (smcd->ops->del_vlan_id(smcd, vlanid))
192 list_del(&vlan->list);
195 spin_unlock_irqrestore(&smcd->lock, flags);
199 int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
204 if (!dmb_desc->dma_addr)
207 memset(&dmb, 0, sizeof(dmb));
208 dmb.dmb_tok = dmb_desc->token;
209 dmb.sba_idx = dmb_desc->sba_idx;
210 dmb.cpu_addr = dmb_desc->cpu_addr;
211 dmb.dma_addr = dmb_desc->dma_addr;
212 dmb.dmb_len = dmb_desc->len;
213 rc = smcd->ops->unregister_dmb(smcd, &dmb);
214 if (!rc || rc == ISM_ERROR) {
215 dmb_desc->cpu_addr = NULL;
216 dmb_desc->dma_addr = 0;
222 int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
223 struct smc_buf_desc *dmb_desc)
225 #if IS_ENABLED(CONFIG_ISM)
229 memset(&dmb, 0, sizeof(dmb));
230 dmb.dmb_len = dmb_len;
231 dmb.sba_idx = dmb_desc->sba_idx;
232 dmb.vlan_id = lgr->vlan_id;
233 dmb.rgid = lgr->peer_gid.gid;
234 rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb, &smc_ism_client);
236 dmb_desc->sba_idx = dmb.sba_idx;
237 dmb_desc->token = dmb.dmb_tok;
238 dmb_desc->cpu_addr = dmb.cpu_addr;
239 dmb_desc->dma_addr = dmb.dma_addr;
240 dmb_desc->len = dmb.dmb_len;
248 static int smc_nl_handle_smcd_dev(struct smcd_dev *smcd,
250 struct netlink_callback *cb)
252 char smc_pnet[SMC_MAX_PNETID_LEN + 1];
253 struct smc_pci_dev smc_pci_dev;
254 struct nlattr *port_attrs;
255 struct nlattr *attrs;
261 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
262 &smc_gen_nl_family, NLM_F_MULTI,
263 SMC_NETLINK_GET_DEV_SMCD);
266 attrs = nla_nest_start(skb, SMC_GEN_DEV_SMCD);
269 use_cnt = atomic_read(&smcd->lgr_cnt);
270 if (nla_put_u32(skb, SMC_NLA_DEV_USE_CNT, use_cnt))
272 if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRIT, use_cnt > 0))
274 memset(&smc_pci_dev, 0, sizeof(smc_pci_dev));
275 smc_set_pci_values(to_pci_dev(ism->dev.parent), &smc_pci_dev);
276 if (nla_put_u32(skb, SMC_NLA_DEV_PCI_FID, smc_pci_dev.pci_fid))
278 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_CHID, smc_pci_dev.pci_pchid))
280 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_VENDOR, smc_pci_dev.pci_vendor))
282 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_DEVICE, smc_pci_dev.pci_device))
284 if (nla_put_string(skb, SMC_NLA_DEV_PCI_ID, smc_pci_dev.pci_id))
287 port_attrs = nla_nest_start(skb, SMC_NLA_DEV_PORT);
290 if (nla_put_u8(skb, SMC_NLA_DEV_PORT_PNET_USR, smcd->pnetid_by_user))
292 memcpy(smc_pnet, smcd->pnetid, SMC_MAX_PNETID_LEN);
293 smc_pnet[SMC_MAX_PNETID_LEN] = 0;
294 if (nla_put_string(skb, SMC_NLA_DEV_PORT_PNETID, smc_pnet))
297 nla_nest_end(skb, port_attrs);
298 nla_nest_end(skb, attrs);
299 genlmsg_end(skb, nlh);
303 nla_nest_cancel(skb, port_attrs);
305 nla_nest_cancel(skb, attrs);
307 nlmsg_cancel(skb, nlh);
312 static void smc_nl_prep_smcd_dev(struct smcd_dev_list *dev_list,
314 struct netlink_callback *cb)
316 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
317 int snum = cb_ctx->pos[0];
318 struct smcd_dev *smcd;
321 mutex_lock(&dev_list->mutex);
322 list_for_each_entry(smcd, &dev_list->list, list) {
325 if (smc_nl_handle_smcd_dev(smcd, skb, cb))
331 mutex_unlock(&dev_list->mutex);
332 cb_ctx->pos[0] = num;
335 int smcd_nl_get_device(struct sk_buff *skb, struct netlink_callback *cb)
337 smc_nl_prep_smcd_dev(&smcd_dev_list, skb, cb);
341 #if IS_ENABLED(CONFIG_ISM)
342 struct smc_ism_event_work {
343 struct work_struct work;
344 struct smcd_dev *smcd;
345 struct ism_event event;
348 #define ISM_EVENT_REQUEST 0x0001
349 #define ISM_EVENT_RESPONSE 0x0002
350 #define ISM_EVENT_REQUEST_IR 0x00000001
351 #define ISM_EVENT_CODE_SHUTDOWN 0x80
352 #define ISM_EVENT_CODE_TESTLINK 0x83
354 union smcd_sw_event_info {
357 u8 uid[SMC_LGR_ID_SIZE];
358 unsigned short vlan_id;
363 static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
365 struct smcd_gid peer_gid = { .gid = wrk->event.tok,
367 union smcd_sw_event_info ev_info;
369 ev_info.info = wrk->event.info;
370 switch (wrk->event.code) {
371 case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
372 smc_smcd_terminate(wrk->smcd, &peer_gid, ev_info.vlan_id);
374 case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
375 if (ev_info.code == ISM_EVENT_REQUEST) {
376 ev_info.code = ISM_EVENT_RESPONSE;
377 wrk->smcd->ops->signal_event(wrk->smcd,
379 ISM_EVENT_REQUEST_IR,
380 ISM_EVENT_CODE_TESTLINK,
387 /* worker for SMC-D events */
388 static void smc_ism_event_work(struct work_struct *work)
390 struct smc_ism_event_work *wrk =
391 container_of(work, struct smc_ism_event_work, work);
392 struct smcd_gid smcd_gid = { .gid = wrk->event.tok,
395 switch (wrk->event.type) {
396 case ISM_EVENT_GID: /* GID event, token is peer GID */
397 smc_smcd_terminate(wrk->smcd, &smcd_gid, VLAN_VID_MASK);
401 case ISM_EVENT_SWR: /* Software defined event */
402 smcd_handle_sw_event(wrk);
408 static struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
409 const struct smcd_ops *ops, int max_dmbs)
411 struct smcd_dev *smcd;
413 smcd = devm_kzalloc(parent, sizeof(*smcd), GFP_KERNEL);
416 smcd->conn = devm_kcalloc(parent, max_dmbs,
417 sizeof(struct smc_connection *), GFP_KERNEL);
421 smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
422 WQ_MEM_RECLAIM, name);
428 spin_lock_init(&smcd->lock);
429 spin_lock_init(&smcd->lgr_lock);
430 INIT_LIST_HEAD(&smcd->vlan);
431 INIT_LIST_HEAD(&smcd->lgr_list);
432 init_waitqueue_head(&smcd->lgrs_deleted);
436 static void smcd_register_dev(struct ism_dev *ism)
438 const struct smcd_ops *ops = ism_get_smcd_ops();
439 struct smcd_dev *smcd;
444 smcd = smcd_alloc_dev(&ism->pdev->dev, dev_name(&ism->pdev->dev), ops,
449 ism_set_priv(ism, &smc_ism_client, smcd);
450 if (smc_pnetid_by_dev_port(&ism->pdev->dev, 0, smcd->pnetid))
451 smc_pnetid_by_table_smcd(smcd);
453 mutex_lock(&smcd_dev_list.mutex);
454 if (list_empty(&smcd_dev_list.list)) {
455 if (smcd->ops->supports_v2())
456 smc_ism_v2_capable = true;
458 /* sort list: devices without pnetid before devices with pnetid */
460 list_add_tail(&smcd->list, &smcd_dev_list.list);
462 list_add(&smcd->list, &smcd_dev_list.list);
463 mutex_unlock(&smcd_dev_list.mutex);
465 pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n",
466 dev_name(&ism->dev), smcd->pnetid,
467 smcd->pnetid_by_user ? " (user defined)" : "");
472 static void smcd_unregister_dev(struct ism_dev *ism)
474 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
476 pr_warn_ratelimited("smc: removing smcd device %s\n",
477 dev_name(&ism->dev));
478 smcd->going_away = 1;
479 smc_smcd_terminate_all(smcd);
480 mutex_lock(&smcd_dev_list.mutex);
481 list_del_init(&smcd->list);
482 mutex_unlock(&smcd_dev_list.mutex);
483 destroy_workqueue(smcd->event_wq);
486 /* SMCD Device event handler. Called from ISM device interrupt handler.
487 * Parameters are ism device pointer,
488 * - event->type (0 --> DMB, 1 --> GID),
489 * - event->code (event code),
490 * - event->tok (either DMB token when event type 0, or GID when event type 1)
491 * - event->time (time of day)
492 * - event->info (debug info).
495 * - Function called in IRQ context from ISM device driver event handler.
497 static void smcd_handle_event(struct ism_dev *ism, struct ism_event *event)
499 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
500 struct smc_ism_event_work *wrk;
502 if (smcd->going_away)
504 /* copy event to event work queue, and let it be handled there */
505 wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
508 INIT_WORK(&wrk->work, smc_ism_event_work);
511 queue_work(smcd->event_wq, &wrk->work);
514 /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
515 * Parameters are the ism device pointer, DMB number, and the DMBE bitmask.
516 * Find the connection and schedule the tasklet for this connection.
519 * - Function called in IRQ context from ISM device driver IRQ handler.
521 static void smcd_handle_irq(struct ism_dev *ism, unsigned int dmbno,
524 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
525 struct smc_connection *conn = NULL;
528 spin_lock_irqsave(&smcd->lock, flags);
529 conn = smcd->conn[dmbno];
530 if (conn && !conn->killed)
531 tasklet_schedule(&conn->rx_tsklet);
532 spin_unlock_irqrestore(&smcd->lock, flags);
536 int smc_ism_signal_shutdown(struct smc_link_group *lgr)
539 #if IS_ENABLED(CONFIG_ISM)
540 union smcd_sw_event_info ev_info;
542 if (lgr->peer_shutdown)
545 memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
546 ev_info.vlan_id = lgr->vlan_id;
547 ev_info.code = ISM_EVENT_REQUEST;
548 rc = lgr->smcd->ops->signal_event(lgr->smcd, &lgr->peer_gid,
549 ISM_EVENT_REQUEST_IR,
550 ISM_EVENT_CODE_SHUTDOWN,
556 int smc_ism_init(void)
560 smc_ism_v2_capable = false;
561 smc_ism_create_system_eid();
563 #if IS_ENABLED(CONFIG_ISM)
564 rc = ism_register_client(&smc_ism_client);
569 void smc_ism_exit(void)
571 #if IS_ENABLED(CONFIG_ISM)
572 ism_unregister_client(&smc_ism_client);