1 // SPDX-License-Identifier: GPL-2.0
2 /* Shared Memory Communications Direct over ISM devices (SMC-D)
4 * Functions for ISM device.
6 * Copyright IBM Corp. 2018
9 #include <linux/spinlock.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
19 struct smcd_dev_list smcd_dev_list = {
20 .list = LIST_HEAD_INIT(smcd_dev_list.list),
21 .mutex = __MUTEX_INITIALIZER(smcd_dev_list.mutex)
24 /* Test if an ISM communication is possible. */
25 int smc_ism_cantalk(u64 peer_gid, unsigned short vlan_id, struct smcd_dev *smcd)
27 return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0,
31 int smc_ism_write(struct smcd_dev *smcd, const struct smc_ism_position *pos,
32 void *data, size_t len)
36 rc = smcd->ops->move_data(smcd, pos->token, pos->index, pos->signal,
37 pos->offset, data, len);
39 return rc < 0 ? rc : 0;
42 /* Set a connection using this DMBE. */
43 void smc_ism_set_conn(struct smc_connection *conn)
47 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
48 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn;
49 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
52 /* Unset a connection using this DMBE. */
53 void smc_ism_unset_conn(struct smc_connection *conn)
60 spin_lock_irqsave(&conn->lgr->smcd->lock, flags);
61 conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL;
62 spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags);
65 /* Register a VLAN identifier with the ISM device. Use a reference count
66 * and add a VLAN identifier only when the first DMB using this VLAN is
69 int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid)
71 struct smc_ism_vlanid *new_vlan, *vlan;
75 if (!vlanid) /* No valid vlan id */
78 /* create new vlan entry, in case we need it */
79 new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL);
82 new_vlan->vlanid = vlanid;
83 refcount_set(&new_vlan->refcnt, 1);
85 /* if there is an existing entry, increase count and return */
86 spin_lock_irqsave(&smcd->lock, flags);
87 list_for_each_entry(vlan, &smcd->vlan, list) {
88 if (vlan->vlanid == vlanid) {
89 refcount_inc(&vlan->refcnt);
95 /* no existing entry found.
96 * add new entry to device; might fail, e.g., if HW limit reached
98 if (smcd->ops->add_vlan_id(smcd, vlanid)) {
103 list_add_tail(&new_vlan->list, &smcd->vlan);
105 spin_unlock_irqrestore(&smcd->lock, flags);
109 /* Unregister a VLAN identifier with the ISM device. Use a reference count
110 * and remove a VLAN identifier only when the last DMB using this VLAN is
113 int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid)
115 struct smc_ism_vlanid *vlan;
120 if (!vlanid) /* No valid vlan id */
123 spin_lock_irqsave(&smcd->lock, flags);
124 list_for_each_entry(vlan, &smcd->vlan, list) {
125 if (vlan->vlanid == vlanid) {
126 if (!refcount_dec_and_test(&vlan->refcnt))
134 goto out; /* VLAN id not in table */
137 /* Found and the last reference just gone */
138 if (smcd->ops->del_vlan_id(smcd, vlanid))
140 list_del(&vlan->list);
143 spin_unlock_irqrestore(&smcd->lock, flags);
147 int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc)
152 if (!dmb_desc->dma_addr)
155 memset(&dmb, 0, sizeof(dmb));
156 dmb.dmb_tok = dmb_desc->token;
157 dmb.sba_idx = dmb_desc->sba_idx;
158 dmb.cpu_addr = dmb_desc->cpu_addr;
159 dmb.dma_addr = dmb_desc->dma_addr;
160 dmb.dmb_len = dmb_desc->len;
161 rc = smcd->ops->unregister_dmb(smcd, &dmb);
162 if (!rc || rc == ISM_ERROR) {
163 dmb_desc->cpu_addr = NULL;
164 dmb_desc->dma_addr = 0;
170 int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len,
171 struct smc_buf_desc *dmb_desc)
176 memset(&dmb, 0, sizeof(dmb));
177 dmb.dmb_len = dmb_len;
178 dmb.sba_idx = dmb_desc->sba_idx;
179 dmb.vlan_id = lgr->vlan_id;
180 dmb.rgid = lgr->peer_gid;
181 rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb);
183 dmb_desc->sba_idx = dmb.sba_idx;
184 dmb_desc->token = dmb.dmb_tok;
185 dmb_desc->cpu_addr = dmb.cpu_addr;
186 dmb_desc->dma_addr = dmb.dma_addr;
187 dmb_desc->len = dmb.dmb_len;
192 struct smc_ism_event_work {
193 struct work_struct work;
194 struct smcd_dev *smcd;
195 struct smcd_event event;
198 #define ISM_EVENT_REQUEST 0x0001
199 #define ISM_EVENT_RESPONSE 0x0002
200 #define ISM_EVENT_REQUEST_IR 0x00000001
201 #define ISM_EVENT_CODE_SHUTDOWN 0x80
202 #define ISM_EVENT_CODE_TESTLINK 0x83
204 union smcd_sw_event_info {
207 u8 uid[SMC_LGR_ID_SIZE];
208 unsigned short vlan_id;
213 static void smcd_handle_sw_event(struct smc_ism_event_work *wrk)
215 union smcd_sw_event_info ev_info;
217 ev_info.info = wrk->event.info;
218 switch (wrk->event.code) {
219 case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */
220 smc_smcd_terminate(wrk->smcd, wrk->event.tok, ev_info.vlan_id);
222 case ISM_EVENT_CODE_TESTLINK: /* Activity timer */
223 if (ev_info.code == ISM_EVENT_REQUEST) {
224 ev_info.code = ISM_EVENT_RESPONSE;
225 wrk->smcd->ops->signal_event(wrk->smcd,
227 ISM_EVENT_REQUEST_IR,
228 ISM_EVENT_CODE_TESTLINK,
235 int smc_ism_signal_shutdown(struct smc_link_group *lgr)
238 union smcd_sw_event_info ev_info;
240 if (lgr->peer_shutdown)
243 memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE);
244 ev_info.vlan_id = lgr->vlan_id;
245 ev_info.code = ISM_EVENT_REQUEST;
246 rc = lgr->smcd->ops->signal_event(lgr->smcd, lgr->peer_gid,
247 ISM_EVENT_REQUEST_IR,
248 ISM_EVENT_CODE_SHUTDOWN,
253 /* worker for SMC-D events */
254 static void smc_ism_event_work(struct work_struct *work)
256 struct smc_ism_event_work *wrk =
257 container_of(work, struct smc_ism_event_work, work);
259 switch (wrk->event.type) {
260 case ISM_EVENT_GID: /* GID event, token is peer GID */
261 smc_smcd_terminate(wrk->smcd, wrk->event.tok, VLAN_VID_MASK);
265 case ISM_EVENT_SWR: /* Software defined event */
266 smcd_handle_sw_event(wrk);
272 static void smcd_release(struct device *dev)
274 struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev);
280 struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name,
281 const struct smcd_ops *ops, int max_dmbs)
283 struct smcd_dev *smcd;
285 smcd = kzalloc(sizeof(*smcd), GFP_KERNEL);
288 smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *),
295 smcd->dev.parent = parent;
296 smcd->dev.release = smcd_release;
297 device_initialize(&smcd->dev);
298 dev_set_name(&smcd->dev, name);
300 if (smc_pnetid_by_dev_port(parent, 0, smcd->pnetid))
301 smc_pnetid_by_table_smcd(smcd);
303 spin_lock_init(&smcd->lock);
304 spin_lock_init(&smcd->lgr_lock);
305 INIT_LIST_HEAD(&smcd->vlan);
306 INIT_LIST_HEAD(&smcd->lgr_list);
307 init_waitqueue_head(&smcd->lgrs_deleted);
308 smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)",
309 WQ_MEM_RECLAIM, name);
310 if (!smcd->event_wq) {
317 EXPORT_SYMBOL_GPL(smcd_alloc_dev);
319 int smcd_register_dev(struct smcd_dev *smcd)
321 mutex_lock(&smcd_dev_list.mutex);
322 list_add_tail(&smcd->list, &smcd_dev_list.list);
323 mutex_unlock(&smcd_dev_list.mutex);
325 pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n",
326 dev_name(&smcd->dev), smcd->pnetid,
327 smcd->pnetid_by_user ? " (user defined)" : "");
329 return device_add(&smcd->dev);
331 EXPORT_SYMBOL_GPL(smcd_register_dev);
333 void smcd_unregister_dev(struct smcd_dev *smcd)
335 pr_warn_ratelimited("smc: removing smcd device %s\n",
336 dev_name(&smcd->dev));
337 mutex_lock(&smcd_dev_list.mutex);
338 list_del_init(&smcd->list);
339 mutex_unlock(&smcd_dev_list.mutex);
340 smcd->going_away = 1;
341 smc_smcd_terminate_all(smcd);
342 flush_workqueue(smcd->event_wq);
343 destroy_workqueue(smcd->event_wq);
345 device_del(&smcd->dev);
347 EXPORT_SYMBOL_GPL(smcd_unregister_dev);
349 void smcd_free_dev(struct smcd_dev *smcd)
351 put_device(&smcd->dev);
353 EXPORT_SYMBOL_GPL(smcd_free_dev);
355 /* SMCD Device event handler. Called from ISM device interrupt handler.
356 * Parameters are smcd device pointer,
357 * - event->type (0 --> DMB, 1 --> GID),
358 * - event->code (event code),
359 * - event->tok (either DMB token when event type 0, or GID when event type 1)
360 * - event->time (time of day)
361 * - event->info (debug info).
364 * - Function called in IRQ context from ISM device driver event handler.
366 void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event)
368 struct smc_ism_event_work *wrk;
370 if (smcd->going_away)
372 /* copy event to event work queue, and let it be handled there */
373 wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC);
376 INIT_WORK(&wrk->work, smc_ism_event_work);
379 queue_work(smcd->event_wq, &wrk->work);
381 EXPORT_SYMBOL_GPL(smcd_handle_event);
383 /* SMCD Device interrupt handler. Called from ISM device interrupt handler.
384 * Parameters are smcd device pointer and DMB number. Find the connection and
385 * schedule the tasklet for this connection.
388 * - Function called in IRQ context from ISM device driver IRQ handler.
390 void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno)
392 struct smc_connection *conn = NULL;
395 spin_lock_irqsave(&smcd->lock, flags);
396 conn = smcd->conn[dmbno];
397 if (conn && !conn->killed)
398 tasklet_schedule(&conn->rx_tsklet);
399 spin_unlock_irqrestore(&smcd->lock, flags);
401 EXPORT_SYMBOL_GPL(smcd_handle_irq);