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 /* Test if an ISM communication is possible - same CPC */
47 int smc_ism_cantalk(u64 peer_gid, unsigned short vlan_id, struct smcd_dev *smcd)
49 return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0,
53 void smc_ism_get_system_eid(u8 **eid)
55 if (!smc_ism_v2_capable)
58 *eid = smc_ism_v2_system_eid;
61 u16 smc_ism_get_chid(struct smcd_dev *smcd)
63 return smcd->ops->get_chid(smcd);
66 /* HW supports ISM V2 and thus System EID is defined */
67 bool smc_ism_is_v2_capable(void)
69 return smc_ism_v2_capable;
72 /* Set a connection using this DMBE. */
73 void smc_ism_set_conn(struct smc_connection *conn)
77 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
78 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
79 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
82 /* Unset a connection using this DMBE. */
83 void smc_ism_unset_conn(struct smc_connection *conn)
90 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
91 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
92 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
95 /* Register a VLAN identifier with the ISM device. Use a reference count
96 * and add a VLAN identifier only when the first DMB using this VLAN is
99 int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
101 struct smc_ism_vlanid *new_vlan, *vlan;
105 if (!vlanid) /* No valid vlan id */
108 /* create new vlan entry, in case we need it */
109 new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
112 new_vlan->vlanid = vlanid;
113 refcount_set(&new_vlan->refcnt, 1);
115 /* if there is an existing entry, increase count and return */
116 spin_lock_irqsave(&smcd->lock, flags);
117 list_for_each_entry(vlan, &smcd->vlan, list) {
118 if (vlan->vlanid == vlanid) {
119 refcount_inc(&vlan->refcnt);
125 /* no existing entry found.
126 * add new entry to device; might fail, e.g., if HW limit reached
128 if (smcd->ops->add_vlan_id(smcd, vlanid)) {
133 list_add_tail(&new_vlan->list, &smcd->vlan);
135 spin_unlock_irqrestore(&smcd->lock, flags);
139 /* Unregister a VLAN identifier with the ISM device. Use a reference count
140 * and remove a VLAN identifier only when the last DMB using this VLAN is
143 int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
145 struct smc_ism_vlanid *vlan;
150 if (!vlanid) /* No valid vlan id */
153 spin_lock_irqsave(&smcd->lock, flags);
154 list_for_each_entry(vlan, &smcd->vlan, list) {
155 if (vlan->vlanid == vlanid) {
156 if (!refcount_dec_and_test(&vlan->refcnt))
164 goto out; /* VLAN id not in table */
167 /* Found and the last reference just gone */
168 if (smcd->ops->del_vlan_id(smcd, vlanid))
170 list_del(&vlan->list);
173 spin_unlock_irqrestore(&smcd->lock, flags);
177 int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
182 if (!dmb_desc->dma_addr)
185 memset(&dmb, 0, sizeof(dmb));
186 dmb.dmb_tok = dmb_desc->token;
187 dmb.sba_idx = dmb_desc->sba_idx;
188 dmb.cpu_addr = dmb_desc->cpu_addr;
189 dmb.dma_addr = dmb_desc->dma_addr;
190 dmb.dmb_len = dmb_desc->len;
191 rc = smcd->ops->unregister_dmb(smcd, &dmb);
192 if (!rc || rc == ISM_ERROR) {
193 dmb_desc->cpu_addr = NULL;
194 dmb_desc->dma_addr = 0;
200 int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
201 struct smc_buf_desc *dmb_desc)
203 #if IS_ENABLED(CONFIG_ISM)
207 memset(&dmb, 0, sizeof(dmb));
208 dmb.dmb_len = dmb_len;
209 dmb.sba_idx = dmb_desc->sba_idx;
210 dmb.vlan_id = lgr->vlan_id;
211 dmb.rgid = lgr->peer_gid;
212 rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb, &smc_ism_client);
214 dmb_desc->sba_idx = dmb.sba_idx;
215 dmb_desc->token = dmb.dmb_tok;
216 dmb_desc->cpu_addr = dmb.cpu_addr;
217 dmb_desc->dma_addr = dmb.dma_addr;
218 dmb_desc->len = dmb.dmb_len;
226 static int smc_nl_handle_smcd_dev(struct smcd_dev *smcd,
228 struct netlink_callback *cb)
230 char smc_pnet[SMC_MAX_PNETID_LEN + 1];
231 struct smc_pci_dev smc_pci_dev;
232 struct nlattr *port_attrs;
233 struct nlattr *attrs;
239 nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
240 &smc_gen_nl_family, NLM_F_MULTI,
241 SMC_NETLINK_GET_DEV_SMCD);
244 attrs = nla_nest_start(skb, SMC_GEN_DEV_SMCD);
247 use_cnt = atomic_read(&smcd->lgr_cnt);
248 if (nla_put_u32(skb, SMC_NLA_DEV_USE_CNT, use_cnt))
250 if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRIT, use_cnt > 0))
252 memset(&smc_pci_dev, 0, sizeof(smc_pci_dev));
253 smc_set_pci_values(to_pci_dev(ism->dev.parent), &smc_pci_dev);
254 if (nla_put_u32(skb, SMC_NLA_DEV_PCI_FID, smc_pci_dev.pci_fid))
256 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_CHID, smc_pci_dev.pci_pchid))
258 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_VENDOR, smc_pci_dev.pci_vendor))
260 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_DEVICE, smc_pci_dev.pci_device))
262 if (nla_put_string(skb, SMC_NLA_DEV_PCI_ID, smc_pci_dev.pci_id))
265 port_attrs = nla_nest_start(skb, SMC_NLA_DEV_PORT);
268 if (nla_put_u8(skb, SMC_NLA_DEV_PORT_PNET_USR, smcd->pnetid_by_user))
270 memcpy(smc_pnet, smcd->pnetid, SMC_MAX_PNETID_LEN);
271 smc_pnet[SMC_MAX_PNETID_LEN] = 0;
272 if (nla_put_string(skb, SMC_NLA_DEV_PORT_PNETID, smc_pnet))
275 nla_nest_end(skb, port_attrs);
276 nla_nest_end(skb, attrs);
277 genlmsg_end(skb, nlh);
281 nla_nest_cancel(skb, port_attrs);
283 nla_nest_cancel(skb, attrs);
285 nlmsg_cancel(skb, nlh);
290 static void smc_nl_prep_smcd_dev(struct smcd_dev_list *dev_list,
292 struct netlink_callback *cb)
294 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
295 int snum = cb_ctx->pos[0];
296 struct smcd_dev *smcd;
299 mutex_lock(&dev_list->mutex);
300 list_for_each_entry(smcd, &dev_list->list, list) {
303 if (smc_nl_handle_smcd_dev(smcd, skb, cb))
309 mutex_unlock(&dev_list->mutex);
310 cb_ctx->pos[0] = num;
313 int smcd_nl_get_device(struct sk_buff *skb, struct netlink_callback *cb)
315 smc_nl_prep_smcd_dev(&smcd_dev_list, skb, cb);
319 #if IS_ENABLED(CONFIG_ISM)
320 struct smc_ism_event_work {
321 struct work_struct work;
322 struct smcd_dev *smcd;
323 struct ism_event event;
326 #define ISM_EVENT_REQUEST 0x0001
327 #define ISM_EVENT_RESPONSE 0x0002
328 #define ISM_EVENT_REQUEST_IR 0x00000001
329 #define ISM_EVENT_CODE_SHUTDOWN 0x80
330 #define ISM_EVENT_CODE_TESTLINK 0x83
332 union smcd_sw_event_info {
335 u8 uid[SMC_LGR_ID_SIZE];
336 unsigned short vlan_id;
341 static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
343 union smcd_sw_event_info ev_info;
345 ev_info.info = wrk->event.info;
346 switch (wrk->event.code) {
347 case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
348 smc_smcd_terminate(wrk->smcd, wrk->event.tok, ev_info.vlan_id);
350 case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
351 if (ev_info.code == ISM_EVENT_REQUEST) {
352 ev_info.code = ISM_EVENT_RESPONSE;
353 wrk->smcd->ops->signal_event(wrk->smcd,
355 ISM_EVENT_REQUEST_IR,
356 ISM_EVENT_CODE_TESTLINK,
363 /* worker for SMC-D events */
364 static void smc_ism_event_work(struct work_struct *work)
366 struct smc_ism_event_work *wrk =
367 container_of(work, struct smc_ism_event_work, work);
369 switch (wrk->event.type) {
370 case ISM_EVENT_GID: /* GID event, token is peer GID */
371 smc_smcd_terminate(wrk->smcd, wrk->event.tok, VLAN_VID_MASK);
375 case ISM_EVENT_SWR: /* Software defined event */
376 smcd_handle_sw_event(wrk);
382 static struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
383 const struct smcd_ops *ops, int max_dmbs)
385 struct smcd_dev *smcd;
387 smcd = devm_kzalloc(parent, sizeof(*smcd), GFP_KERNEL);
390 smcd->conn = devm_kcalloc(parent, max_dmbs,
391 sizeof(struct smc_connection *), GFP_KERNEL);
395 smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
396 WQ_MEM_RECLAIM, name);
402 spin_lock_init(&smcd->lock);
403 spin_lock_init(&smcd->lgr_lock);
404 INIT_LIST_HEAD(&smcd->vlan);
405 INIT_LIST_HEAD(&smcd->lgr_list);
406 init_waitqueue_head(&smcd->lgrs_deleted);
410 static void smcd_register_dev(struct ism_dev *ism)
412 const struct smcd_ops *ops = ism_get_smcd_ops();
413 struct smcd_dev *smcd;
418 smcd = smcd_alloc_dev(&ism->pdev->dev, dev_name(&ism->pdev->dev), ops,
423 ism_set_priv(ism, &smc_ism_client, smcd);
424 if (smc_pnetid_by_dev_port(&ism->pdev->dev, 0, smcd->pnetid))
425 smc_pnetid_by_table_smcd(smcd);
427 mutex_lock(&smcd_dev_list.mutex);
428 if (list_empty(&smcd_dev_list.list)) {
429 u8 *system_eid = NULL;
431 system_eid = smcd->ops->get_system_eid();
432 if (smcd->ops->supports_v2()) {
433 smc_ism_v2_capable = true;
434 memcpy(smc_ism_v2_system_eid, system_eid,
438 /* sort list: devices without pnetid before devices with pnetid */
440 list_add_tail(&smcd->list, &smcd_dev_list.list);
442 list_add(&smcd->list, &smcd_dev_list.list);
443 mutex_unlock(&smcd_dev_list.mutex);
445 pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n",
446 dev_name(&ism->dev), smcd->pnetid,
447 smcd->pnetid_by_user ? " (user defined)" : "");
452 static void smcd_unregister_dev(struct ism_dev *ism)
454 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
456 pr_warn_ratelimited("smc: removing smcd device %s\n",
457 dev_name(&ism->dev));
458 smcd->going_away = 1;
459 smc_smcd_terminate_all(smcd);
460 mutex_lock(&smcd_dev_list.mutex);
461 list_del_init(&smcd->list);
462 mutex_unlock(&smcd_dev_list.mutex);
463 destroy_workqueue(smcd->event_wq);
466 /* SMCD Device event handler. Called from ISM device interrupt handler.
467 * Parameters are ism device pointer,
468 * - event->type (0 --> DMB, 1 --> GID),
469 * - event->code (event code),
470 * - event->tok (either DMB token when event type 0, or GID when event type 1)
471 * - event->time (time of day)
472 * - event->info (debug info).
475 * - Function called in IRQ context from ISM device driver event handler.
477 static void smcd_handle_event(struct ism_dev *ism, struct ism_event *event)
479 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
480 struct smc_ism_event_work *wrk;
482 if (smcd->going_away)
484 /* copy event to event work queue, and let it be handled there */
485 wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
488 INIT_WORK(&wrk->work, smc_ism_event_work);
491 queue_work(smcd->event_wq, &wrk->work);
494 /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
495 * Parameters are the ism device pointer, DMB number, and the DMBE bitmask.
496 * Find the connection and schedule the tasklet for this connection.
499 * - Function called in IRQ context from ISM device driver IRQ handler.
501 static void smcd_handle_irq(struct ism_dev *ism, unsigned int dmbno,
504 struct smcd_dev *smcd = ism_get_priv(ism, &smc_ism_client);
505 struct smc_connection *conn = NULL;
508 spin_lock_irqsave(&smcd->lock, flags);
509 conn = smcd->conn[dmbno];
510 if (conn && !conn->killed)
511 tasklet_schedule(&conn->rx_tsklet);
512 spin_unlock_irqrestore(&smcd->lock, flags);
516 int smc_ism_signal_shutdown(struct smc_link_group *lgr)
519 #if IS_ENABLED(CONFIG_ISM)
520 union smcd_sw_event_info ev_info;
522 if (lgr->peer_shutdown)
525 memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
526 ev_info.vlan_id = lgr->vlan_id;
527 ev_info.code = ISM_EVENT_REQUEST;
528 rc = lgr->smcd->ops->signal_event(lgr->smcd, lgr->peer_gid,
529 ISM_EVENT_REQUEST_IR,
530 ISM_EVENT_CODE_SHUTDOWN,
536 int smc_ism_init(void)
540 #if IS_ENABLED(CONFIG_ISM)
541 smc_ism_v2_capable = false;
542 memset(smc_ism_v2_system_eid, 0, SMC_MAX_EID_LEN);
544 rc = ism_register_client(&smc_ism_client);
549 void smc_ism_exit(void)
551 #if IS_ENABLED(CONFIG_ISM)
552 ism_unregister_client(&smc_ism_client);