Linux Audio

Check our new training course

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