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