GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / smc / smc_ism.c
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
18 struct 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. */
24 int 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
30 int 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. */
42 void 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. */
52 void 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  */
68 int 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);
103 out:
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  */
112 int 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);
141 out:
142         spin_unlock_irqrestore(&smcd->lock, flags);
143         return rc;
144 }
145
146 int 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
159 int 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
181 struct 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_TESTLINK         0x83
191
192 static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
193 {
194         union {
195                 u64     info;
196                 struct {
197                         u32             uid;
198                         unsigned short  vlanid;
199                         u16             code;
200                 };
201         } ev_info;
202
203         switch (wrk->event.code) {
204         case ISM_EVENT_CODE_TESTLINK:   /* Activity timer */
205                 ev_info.info = wrk->event.info;
206                 if (ev_info.code == ISM_EVENT_REQUEST) {
207                         ev_info.code = ISM_EVENT_RESPONSE;
208                         wrk->smcd->ops->signal_event(wrk->smcd,
209                                                      wrk->event.tok,
210                                                      ISM_EVENT_REQUEST_IR,
211                                                      ISM_EVENT_CODE_TESTLINK,
212                                                      ev_info.info);
213                         }
214                 break;
215         }
216 }
217
218 /* worker for SMC-D events */
219 static void smc_ism_event_work(struct work_struct *work)
220 {
221         struct smc_ism_event_work *wrk =
222                 container_of(work, struct smc_ism_event_work, work);
223
224         switch (wrk->event.type) {
225         case ISM_EVENT_GID:     /* GID event, token is peer GID */
226                 smc_smcd_terminate(wrk->smcd, wrk->event.tok);
227                 break;
228         case ISM_EVENT_DMB:
229                 break;
230         case ISM_EVENT_SWR:     /* Software defined event */
231                 smcd_handle_sw_event(wrk);
232                 break;
233         }
234         kfree(wrk);
235 }
236
237 static void smcd_release(struct device *dev)
238 {
239         struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev);
240
241         kfree(smcd->conn);
242         kfree(smcd);
243 }
244
245 struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
246                                 const struct smcd_ops *ops, int max_dmbs)
247 {
248         struct smcd_dev *smcd;
249
250         smcd = kzalloc(sizeof(*smcd), GFP_KERNEL);
251         if (!smcd)
252                 return NULL;
253         smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *),
254                              GFP_KERNEL);
255         if (!smcd->conn) {
256                 kfree(smcd);
257                 return NULL;
258         }
259
260         smcd->dev.parent = parent;
261         smcd->dev.release = smcd_release;
262         device_initialize(&smcd->dev);
263         dev_set_name(&smcd->dev, name);
264         smcd->ops = ops;
265         smc_pnetid_by_dev_port(parent, 0, smcd->pnetid);
266
267         spin_lock_init(&smcd->lock);
268         INIT_LIST_HEAD(&smcd->vlan);
269         smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
270                                                  WQ_MEM_RECLAIM, name);
271         return smcd;
272 }
273 EXPORT_SYMBOL_GPL(smcd_alloc_dev);
274
275 int smcd_register_dev(struct smcd_dev *smcd)
276 {
277         spin_lock(&smcd_dev_list.lock);
278         list_add_tail(&smcd->list, &smcd_dev_list.list);
279         spin_unlock(&smcd_dev_list.lock);
280
281         return device_add(&smcd->dev);
282 }
283 EXPORT_SYMBOL_GPL(smcd_register_dev);
284
285 void smcd_unregister_dev(struct smcd_dev *smcd)
286 {
287         spin_lock(&smcd_dev_list.lock);
288         list_del(&smcd->list);
289         spin_unlock(&smcd_dev_list.lock);
290         flush_workqueue(smcd->event_wq);
291         destroy_workqueue(smcd->event_wq);
292         smc_smcd_terminate(smcd, 0);
293
294         device_del(&smcd->dev);
295 }
296 EXPORT_SYMBOL_GPL(smcd_unregister_dev);
297
298 void smcd_free_dev(struct smcd_dev *smcd)
299 {
300         put_device(&smcd->dev);
301 }
302 EXPORT_SYMBOL_GPL(smcd_free_dev);
303
304 /* SMCD Device event handler. Called from ISM device interrupt handler.
305  * Parameters are smcd device pointer,
306  * - event->type (0 --> DMB, 1 --> GID),
307  * - event->code (event code),
308  * - event->tok (either DMB token when event type 0, or GID when event type 1)
309  * - event->time (time of day)
310  * - event->info (debug info).
311  *
312  * Context:
313  * - Function called in IRQ context from ISM device driver event handler.
314  */
315 void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event)
316 {
317         struct smc_ism_event_work *wrk;
318
319         /* copy event to event work queue, and let it be handled there */
320         wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
321         if (!wrk)
322                 return;
323         INIT_WORK(&wrk->work, smc_ism_event_work);
324         wrk->smcd = smcd;
325         wrk->event = *event;
326         queue_work(smcd->event_wq, &wrk->work);
327 }
328 EXPORT_SYMBOL_GPL(smcd_handle_event);
329
330 /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
331  * Parameters are smcd device pointer and DMB number. Find the connection and
332  * schedule the tasklet for this connection.
333  *
334  * Context:
335  * - Function called in IRQ context from ISM device driver IRQ handler.
336  */
337 void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno)
338 {
339         struct smc_connection *conn = NULL;
340         unsigned long flags;
341
342         spin_lock_irqsave(&smcd->lock, flags);
343         conn = smcd->conn[dmbno];
344         if (conn)
345                 tasklet_schedule(&conn->rx_tsklet);
346         spin_unlock_irqrestore(&smcd->lock, flags);
347 }
348 EXPORT_SYMBOL_GPL(smcd_handle_irq);