blob: 63c3dd5578bf0d24cdae1309271c26c55da7e6bb [file] [log] [blame]
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001// 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>
Ursula Braun82087c02020-07-08 17:05:14 +020010#include <linux/mutex.h>
Hans Wippelc6ba7c92018-06-28 19:05:07 +020011#include <linux/slab.h>
12#include <asm/page.h>
13
14#include "smc.h"
15#include "smc_core.h"
16#include "smc_ism.h"
Hans Wippel1619f772018-06-28 19:05:08 +020017#include "smc_pnet.h"
Hans Wippelc6ba7c92018-06-28 19:05:07 +020018
19struct smcd_dev_list smcd_dev_list = {
20 .list = LIST_HEAD_INIT(smcd_dev_list.list),
Ursula Braun82087c02020-07-08 17:05:14 +020021 .mutex = __MUTEX_INITIALIZER(smcd_dev_list.mutex)
Hans Wippelc6ba7c92018-06-28 19:05:07 +020022};
23
Ursula Braun201091e2020-09-26 12:44:24 +020024bool smc_ism_v2_capable;
25
26/* Test if an ISM communication is possible - same CPC */
Hans Wippelc6ba7c92018-06-28 19:05:07 +020027int smc_ism_cantalk(u64 peer_gid, unsigned short vlan_id, struct smcd_dev *smcd)
28{
29 return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0,
30 vlan_id);
31}
32
33int smc_ism_write(struct smcd_dev *smcd, const struct smc_ism_position *pos,
34 void *data, size_t len)
35{
36 int rc;
37
38 rc = smcd->ops->move_data(smcd, pos->token, pos->index, pos->signal,
39 pos->offset, data, len);
40
41 return rc < 0 ? rc : 0;
42}
43
Ursula Braun201091e2020-09-26 12:44:24 +020044void smc_ism_get_system_eid(struct smcd_dev *smcd, u8 **eid)
45{
46 smcd->ops->get_system_eid(smcd, eid);
47}
48
Hans Wippelc6ba7c92018-06-28 19:05:07 +020049/* Set a connection using this DMBE. */
50void smc_ism_set_conn(struct smc_connection *conn)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
55 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
56 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
57}
58
59/* Unset a connection using this DMBE. */
60void smc_ism_unset_conn(struct smc_connection *conn)
61{
62 unsigned long flags;
63
64 if (!conn->rmb_desc)
65 return;
66
67 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
68 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
69 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
70}
71
72/* Register a VLAN identifier with the ISM device. Use a reference count
73 * and add a VLAN identifier only when the first DMB using this VLAN is
74 * registered.
75 */
76int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
77{
78 struct smc_ism_vlanid *new_vlan, *vlan;
79 unsigned long flags;
80 int rc = 0;
81
82 if (!vlanid) /* No valid vlan id */
83 return -EINVAL;
84
85 /* create new vlan entry, in case we need it */
86 new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
87 if (!new_vlan)
88 return -ENOMEM;
89 new_vlan->vlanid = vlanid;
90 refcount_set(&new_vlan->refcnt, 1);
91
92 /* if there is an existing entry, increase count and return */
93 spin_lock_irqsave(&smcd->lock, flags);
94 list_for_each_entry(vlan, &smcd->vlan, list) {
95 if (vlan->vlanid == vlanid) {
96 refcount_inc(&vlan->refcnt);
97 kfree(new_vlan);
98 goto out;
99 }
100 }
101
102 /* no existing entry found.
103 * add new entry to device; might fail, e.g., if HW limit reached
104 */
105 if (smcd->ops->add_vlan_id(smcd, vlanid)) {
106 kfree(new_vlan);
107 rc = -EIO;
108 goto out;
109 }
110 list_add_tail(&new_vlan->list, &smcd->vlan);
111out:
112 spin_unlock_irqrestore(&smcd->lock, flags);
113 return rc;
114}
115
116/* Unregister a VLAN identifier with the ISM device. Use a reference count
117 * and remove a VLAN identifier only when the last DMB using this VLAN is
118 * unregistered.
119 */
120int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
121{
122 struct smc_ism_vlanid *vlan;
123 unsigned long flags;
124 bool found = false;
125 int rc = 0;
126
127 if (!vlanid) /* No valid vlan id */
128 return -EINVAL;
129
130 spin_lock_irqsave(&smcd->lock, flags);
131 list_for_each_entry(vlan, &smcd->vlan, list) {
132 if (vlan->vlanid == vlanid) {
133 if (!refcount_dec_and_test(&vlan->refcnt))
134 goto out;
135 found = true;
136 break;
137 }
138 }
139 if (!found) {
140 rc = -ENOENT;
141 goto out; /* VLAN id not in table */
142 }
143
144 /* Found and the last reference just gone */
145 if (smcd->ops->del_vlan_id(smcd, vlanid))
146 rc = -EIO;
147 list_del(&vlan->list);
148 kfree(vlan);
149out:
150 spin_unlock_irqrestore(&smcd->lock, flags);
151 return rc;
152}
153
154int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
155{
156 struct smcd_dmb dmb;
Ursula Braun42bfba92019-11-14 13:02:41 +0100157 int rc = 0;
158
159 if (!dmb_desc->dma_addr)
160 return rc;
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200161
162 memset(&dmb, 0, sizeof(dmb));
163 dmb.dmb_tok = dmb_desc->token;
164 dmb.sba_idx = dmb_desc->sba_idx;
165 dmb.cpu_addr = dmb_desc->cpu_addr;
166 dmb.dma_addr = dmb_desc->dma_addr;
167 dmb.dmb_len = dmb_desc->len;
Ursula Braun42bfba92019-11-14 13:02:41 +0100168 rc = smcd->ops->unregister_dmb(smcd, &dmb);
169 if (!rc || rc == ISM_ERROR) {
170 dmb_desc->cpu_addr = NULL;
171 dmb_desc->dma_addr = 0;
172 }
173
174 return rc;
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200175}
176
177int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
178 struct smc_buf_desc *dmb_desc)
179{
180 struct smcd_dmb dmb;
181 int rc;
182
183 memset(&dmb, 0, sizeof(dmb));
184 dmb.dmb_len = dmb_len;
185 dmb.sba_idx = dmb_desc->sba_idx;
186 dmb.vlan_id = lgr->vlan_id;
187 dmb.rgid = lgr->peer_gid;
188 rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb);
189 if (!rc) {
190 dmb_desc->sba_idx = dmb.sba_idx;
191 dmb_desc->token = dmb.dmb_tok;
192 dmb_desc->cpu_addr = dmb.cpu_addr;
193 dmb_desc->dma_addr = dmb.dma_addr;
194 dmb_desc->len = dmb.dmb_len;
195 }
196 return rc;
197}
198
199struct smc_ism_event_work {
200 struct work_struct work;
201 struct smcd_dev *smcd;
202 struct smcd_event event;
203};
204
Ursula Braun0d86caf2018-08-10 17:45:11 +0200205#define ISM_EVENT_REQUEST 0x0001
206#define ISM_EVENT_RESPONSE 0x0002
207#define ISM_EVENT_REQUEST_IR 0x00000001
Hans Wippel0512f692018-11-20 16:46:41 +0100208#define ISM_EVENT_CODE_SHUTDOWN 0x80
Ursula Braun0d86caf2018-08-10 17:45:11 +0200209#define ISM_EVENT_CODE_TESTLINK 0x83
210
Hans Wippel0512f692018-11-20 16:46:41 +0100211union smcd_sw_event_info {
212 u64 info;
213 struct {
214 u8 uid[SMC_LGR_ID_SIZE];
215 unsigned short vlan_id;
216 u16 code;
217 };
218};
219
Ursula Braun0d86caf2018-08-10 17:45:11 +0200220static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
221{
Hans Wippel0512f692018-11-20 16:46:41 +0100222 union smcd_sw_event_info ev_info;
Ursula Braun0d86caf2018-08-10 17:45:11 +0200223
Hans Wippel0512f692018-11-20 16:46:41 +0100224 ev_info.info = wrk->event.info;
Ursula Braun0d86caf2018-08-10 17:45:11 +0200225 switch (wrk->event.code) {
Hans Wippel0512f692018-11-20 16:46:41 +0100226 case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
227 smc_smcd_terminate(wrk->smcd, wrk->event.tok, ev_info.vlan_id);
228 break;
Ursula Braun0d86caf2018-08-10 17:45:11 +0200229 case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
Ursula Braun0d86caf2018-08-10 17:45:11 +0200230 if (ev_info.code == ISM_EVENT_REQUEST) {
231 ev_info.code = ISM_EVENT_RESPONSE;
232 wrk->smcd->ops->signal_event(wrk->smcd,
233 wrk->event.tok,
234 ISM_EVENT_REQUEST_IR,
235 ISM_EVENT_CODE_TESTLINK,
236 ev_info.info);
237 }
238 break;
239 }
240}
241
Hans Wippel0512f692018-11-20 16:46:41 +0100242int smc_ism_signal_shutdown(struct smc_link_group *lgr)
243{
244 int rc;
245 union smcd_sw_event_info ev_info;
246
Ursula Braun50c6b202019-11-14 13:02:40 +0100247 if (lgr->peer_shutdown)
248 return 0;
249
Hans Wippel0512f692018-11-20 16:46:41 +0100250 memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
251 ev_info.vlan_id = lgr->vlan_id;
252 ev_info.code = ISM_EVENT_REQUEST;
253 rc = lgr->smcd->ops->signal_event(lgr->smcd, lgr->peer_gid,
254 ISM_EVENT_REQUEST_IR,
255 ISM_EVENT_CODE_SHUTDOWN,
256 ev_info.info);
257 return rc;
258}
259
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200260/* worker for SMC-D events */
261static void smc_ism_event_work(struct work_struct *work)
262{
263 struct smc_ism_event_work *wrk =
264 container_of(work, struct smc_ism_event_work, work);
265
266 switch (wrk->event.type) {
267 case ISM_EVENT_GID: /* GID event, token is peer GID */
Hans Wippel0512f692018-11-20 16:46:41 +0100268 smc_smcd_terminate(wrk->smcd, wrk->event.tok, VLAN_VID_MASK);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200269 break;
270 case ISM_EVENT_DMB:
271 break;
Ursula Braun0d86caf2018-08-10 17:45:11 +0200272 case ISM_EVENT_SWR: /* Software defined event */
273 smcd_handle_sw_event(wrk);
274 break;
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200275 }
276 kfree(wrk);
277}
278
279static void smcd_release(struct device *dev)
280{
281 struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev);
282
283 kfree(smcd->conn);
284 kfree(smcd);
285}
286
287struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
288 const struct smcd_ops *ops, int max_dmbs)
289{
290 struct smcd_dev *smcd;
291
292 smcd = kzalloc(sizeof(*smcd), GFP_KERNEL);
293 if (!smcd)
294 return NULL;
295 smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *),
296 GFP_KERNEL);
297 if (!smcd->conn) {
298 kfree(smcd);
299 return NULL;
300 }
301
302 smcd->dev.parent = parent;
303 smcd->dev.release = smcd_release;
304 device_initialize(&smcd->dev);
305 dev_set_name(&smcd->dev, name);
306 smcd->ops = ops;
Karsten Graulfdff7042020-04-29 17:10:37 +0200307 if (smc_pnetid_by_dev_port(parent, 0, smcd->pnetid))
308 smc_pnetid_by_table_smcd(smcd);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200309
310 spin_lock_init(&smcd->lock);
Ursula Brauna0a62ee2019-10-09 10:07:44 +0200311 spin_lock_init(&smcd->lgr_lock);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200312 INIT_LIST_HEAD(&smcd->vlan);
Ursula Brauna2351c52019-10-09 10:07:43 +0200313 INIT_LIST_HEAD(&smcd->lgr_list);
Ursula Braun5edd6b92019-11-14 13:02:43 +0100314 init_waitqueue_head(&smcd->lgrs_deleted);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200315 smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
316 WQ_MEM_RECLAIM, name);
Kangjie Lue183d4e2019-04-11 11:17:31 +0200317 if (!smcd->event_wq) {
318 kfree(smcd->conn);
319 kfree(smcd);
320 return NULL;
321 }
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200322 return smcd;
323}
324EXPORT_SYMBOL_GPL(smcd_alloc_dev);
325
326int smcd_register_dev(struct smcd_dev *smcd)
327{
Ursula Braun82087c02020-07-08 17:05:14 +0200328 mutex_lock(&smcd_dev_list.mutex);
Ursula Braun201091e2020-09-26 12:44:24 +0200329 if (list_empty(&smcd_dev_list.list)) {
330 u8 *system_eid = NULL;
331
332 smc_ism_get_system_eid(smcd, &system_eid);
333 if ((*system_eid) + 24 != '0' || (*system_eid) + 28 != '0')
334 smc_ism_v2_capable = true;
335 }
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200336 list_add_tail(&smcd->list, &smcd_dev_list.list);
Ursula Braun82087c02020-07-08 17:05:14 +0200337 mutex_unlock(&smcd_dev_list.mutex);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200338
Karsten Graul0a99be42020-05-05 15:01:20 +0200339 pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n",
340 dev_name(&smcd->dev), smcd->pnetid,
341 smcd->pnetid_by_user ? " (user defined)" : "");
342
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200343 return device_add(&smcd->dev);
344}
345EXPORT_SYMBOL_GPL(smcd_register_dev);
346
347void smcd_unregister_dev(struct smcd_dev *smcd)
348{
Karsten Graul0a99be42020-05-05 15:01:20 +0200349 pr_warn_ratelimited("smc: removing smcd device %s\n",
350 dev_name(&smcd->dev));
Ursula Braun82087c02020-07-08 17:05:14 +0200351 mutex_lock(&smcd_dev_list.mutex);
Ursula Braun50c6b202019-11-14 13:02:40 +0100352 list_del_init(&smcd->list);
Ursula Braun82087c02020-07-08 17:05:14 +0200353 mutex_unlock(&smcd_dev_list.mutex);
Ursula Braunc3d94942019-10-09 10:07:46 +0200354 smcd->going_away = 1;
Ursula Braun5421ec22019-11-14 13:02:42 +0100355 smc_smcd_terminate_all(smcd);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200356 flush_workqueue(smcd->event_wq);
357 destroy_workqueue(smcd->event_wq);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200358
359 device_del(&smcd->dev);
360}
361EXPORT_SYMBOL_GPL(smcd_unregister_dev);
362
363void smcd_free_dev(struct smcd_dev *smcd)
364{
365 put_device(&smcd->dev);
366}
367EXPORT_SYMBOL_GPL(smcd_free_dev);
368
369/* SMCD Device event handler. Called from ISM device interrupt handler.
370 * Parameters are smcd device pointer,
371 * - event->type (0 --> DMB, 1 --> GID),
372 * - event->code (event code),
373 * - event->tok (either DMB token when event type 0, or GID when event type 1)
374 * - event->time (time of day)
375 * - event->info (debug info).
376 *
377 * Context:
378 * - Function called in IRQ context from ISM device driver event handler.
379 */
380void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event)
381{
382 struct smc_ism_event_work *wrk;
383
Ursula Braunc3d94942019-10-09 10:07:46 +0200384 if (smcd->going_away)
385 return;
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200386 /* copy event to event work queue, and let it be handled there */
387 wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
388 if (!wrk)
389 return;
390 INIT_WORK(&wrk->work, smc_ism_event_work);
391 wrk->smcd = smcd;
392 wrk->event = *event;
393 queue_work(smcd->event_wq, &wrk->work);
394}
395EXPORT_SYMBOL_GPL(smcd_handle_event);
396
397/* SMCD Device interrupt handler. Called from ISM device interrupt handler.
398 * Parameters are smcd device pointer and DMB number. Find the connection and
399 * schedule the tasklet for this connection.
400 *
401 * Context:
402 * - Function called in IRQ context from ISM device driver IRQ handler.
403 */
404void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno)
405{
Hans Wippelbe244f22018-06-28 19:05:10 +0200406 struct smc_connection *conn = NULL;
407 unsigned long flags;
408
409 spin_lock_irqsave(&smcd->lock, flags);
410 conn = smcd->conn[dmbno];
Ursula Braun42bfba92019-11-14 13:02:41 +0100411 if (conn && !conn->killed)
Hans Wippelbe244f22018-06-28 19:05:10 +0200412 tasklet_schedule(&conn->rx_tsklet);
413 spin_unlock_irqrestore(&smcd->lock, flags);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200414}
415EXPORT_SYMBOL_GPL(smcd_handle_irq);
Ursula Braun201091e2020-09-26 12:44:24 +0200416
417void __init smc_ism_init(void)
418{
419 smc_ism_v2_capable = false;
420}