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