Hans Wippel | c6ba7c9 | 2018-06-28 19:05:07 +0200 | [diff] [blame^] | 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 | |
| 17 | struct smcd_dev_list smcd_dev_list = { |
| 18 | .list = LIST_HEAD_INIT(smcd_dev_list.list), |
| 19 | .lock = __SPIN_LOCK_UNLOCKED(smcd_dev_list.lock) |
| 20 | }; |
| 21 | |
| 22 | /* Test if an ISM communication is possible. */ |
| 23 | int smc_ism_cantalk(u64 peer_gid, unsigned short vlan_id, struct smcd_dev *smcd) |
| 24 | { |
| 25 | return smcd->ops->query_remote_gid(smcd, peer_gid, vlan_id ? 1 : 0, |
| 26 | vlan_id); |
| 27 | } |
| 28 | |
| 29 | int smc_ism_write(struct smcd_dev *smcd, const struct smc_ism_position *pos, |
| 30 | void *data, size_t len) |
| 31 | { |
| 32 | int rc; |
| 33 | |
| 34 | rc = smcd->ops->move_data(smcd, pos->token, pos->index, pos->signal, |
| 35 | pos->offset, data, len); |
| 36 | |
| 37 | return rc < 0 ? rc : 0; |
| 38 | } |
| 39 | |
| 40 | /* Set a connection using this DMBE. */ |
| 41 | void smc_ism_set_conn(struct smc_connection *conn) |
| 42 | { |
| 43 | unsigned long flags; |
| 44 | |
| 45 | spin_lock_irqsave(&conn->lgr->smcd->lock, flags); |
| 46 | conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn; |
| 47 | spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags); |
| 48 | } |
| 49 | |
| 50 | /* Unset a connection using this DMBE. */ |
| 51 | void smc_ism_unset_conn(struct smc_connection *conn) |
| 52 | { |
| 53 | unsigned long flags; |
| 54 | |
| 55 | if (!conn->rmb_desc) |
| 56 | return; |
| 57 | |
| 58 | spin_lock_irqsave(&conn->lgr->smcd->lock, flags); |
| 59 | conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL; |
| 60 | spin_unlock_irqrestore(&conn->lgr->smcd->lock, flags); |
| 61 | } |
| 62 | |
| 63 | /* Register a VLAN identifier with the ISM device. Use a reference count |
| 64 | * and add a VLAN identifier only when the first DMB using this VLAN is |
| 65 | * registered. |
| 66 | */ |
| 67 | int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid) |
| 68 | { |
| 69 | struct smc_ism_vlanid *new_vlan, *vlan; |
| 70 | unsigned long flags; |
| 71 | int rc = 0; |
| 72 | |
| 73 | if (!vlanid) /* No valid vlan id */ |
| 74 | return -EINVAL; |
| 75 | |
| 76 | /* create new vlan entry, in case we need it */ |
| 77 | new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL); |
| 78 | if (!new_vlan) |
| 79 | return -ENOMEM; |
| 80 | new_vlan->vlanid = vlanid; |
| 81 | refcount_set(&new_vlan->refcnt, 1); |
| 82 | |
| 83 | /* if there is an existing entry, increase count and return */ |
| 84 | spin_lock_irqsave(&smcd->lock, flags); |
| 85 | list_for_each_entry(vlan, &smcd->vlan, list) { |
| 86 | if (vlan->vlanid == vlanid) { |
| 87 | refcount_inc(&vlan->refcnt); |
| 88 | kfree(new_vlan); |
| 89 | goto out; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* no existing entry found. |
| 94 | * add new entry to device; might fail, e.g., if HW limit reached |
| 95 | */ |
| 96 | if (smcd->ops->add_vlan_id(smcd, vlanid)) { |
| 97 | kfree(new_vlan); |
| 98 | rc = -EIO; |
| 99 | goto out; |
| 100 | } |
| 101 | list_add_tail(&new_vlan->list, &smcd->vlan); |
| 102 | out: |
| 103 | spin_unlock_irqrestore(&smcd->lock, flags); |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | /* Unregister a VLAN identifier with the ISM device. Use a reference count |
| 108 | * and remove a VLAN identifier only when the last DMB using this VLAN is |
| 109 | * unregistered. |
| 110 | */ |
| 111 | int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid) |
| 112 | { |
| 113 | struct smc_ism_vlanid *vlan; |
| 114 | unsigned long flags; |
| 115 | bool found = false; |
| 116 | int rc = 0; |
| 117 | |
| 118 | if (!vlanid) /* No valid vlan id */ |
| 119 | return -EINVAL; |
| 120 | |
| 121 | spin_lock_irqsave(&smcd->lock, flags); |
| 122 | list_for_each_entry(vlan, &smcd->vlan, list) { |
| 123 | if (vlan->vlanid == vlanid) { |
| 124 | if (!refcount_dec_and_test(&vlan->refcnt)) |
| 125 | goto out; |
| 126 | found = true; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | if (!found) { |
| 131 | rc = -ENOENT; |
| 132 | goto out; /* VLAN id not in table */ |
| 133 | } |
| 134 | |
| 135 | /* Found and the last reference just gone */ |
| 136 | if (smcd->ops->del_vlan_id(smcd, vlanid)) |
| 137 | rc = -EIO; |
| 138 | list_del(&vlan->list); |
| 139 | kfree(vlan); |
| 140 | out: |
| 141 | spin_unlock_irqrestore(&smcd->lock, flags); |
| 142 | return rc; |
| 143 | } |
| 144 | |
| 145 | int smc_ism_unregister_dmb(struct smcd_dev *smcd, struct smc_buf_desc *dmb_desc) |
| 146 | { |
| 147 | struct smcd_dmb dmb; |
| 148 | |
| 149 | memset(&dmb, 0, sizeof(dmb)); |
| 150 | dmb.dmb_tok = dmb_desc->token; |
| 151 | dmb.sba_idx = dmb_desc->sba_idx; |
| 152 | dmb.cpu_addr = dmb_desc->cpu_addr; |
| 153 | dmb.dma_addr = dmb_desc->dma_addr; |
| 154 | dmb.dmb_len = dmb_desc->len; |
| 155 | return smcd->ops->unregister_dmb(smcd, &dmb); |
| 156 | } |
| 157 | |
| 158 | int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len, |
| 159 | struct smc_buf_desc *dmb_desc) |
| 160 | { |
| 161 | struct smcd_dmb dmb; |
| 162 | int rc; |
| 163 | |
| 164 | memset(&dmb, 0, sizeof(dmb)); |
| 165 | dmb.dmb_len = dmb_len; |
| 166 | dmb.sba_idx = dmb_desc->sba_idx; |
| 167 | dmb.vlan_id = lgr->vlan_id; |
| 168 | dmb.rgid = lgr->peer_gid; |
| 169 | rc = lgr->smcd->ops->register_dmb(lgr->smcd, &dmb); |
| 170 | if (!rc) { |
| 171 | dmb_desc->sba_idx = dmb.sba_idx; |
| 172 | dmb_desc->token = dmb.dmb_tok; |
| 173 | dmb_desc->cpu_addr = dmb.cpu_addr; |
| 174 | dmb_desc->dma_addr = dmb.dma_addr; |
| 175 | dmb_desc->len = dmb.dmb_len; |
| 176 | } |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | struct smc_ism_event_work { |
| 181 | struct work_struct work; |
| 182 | struct smcd_dev *smcd; |
| 183 | struct smcd_event event; |
| 184 | }; |
| 185 | |
| 186 | /* worker for SMC-D events */ |
| 187 | static void smc_ism_event_work(struct work_struct *work) |
| 188 | { |
| 189 | struct smc_ism_event_work *wrk = |
| 190 | container_of(work, struct smc_ism_event_work, work); |
| 191 | |
| 192 | switch (wrk->event.type) { |
| 193 | case ISM_EVENT_GID: /* GID event, token is peer GID */ |
| 194 | smc_smcd_terminate(wrk->smcd, wrk->event.tok); |
| 195 | break; |
| 196 | case ISM_EVENT_DMB: |
| 197 | break; |
| 198 | } |
| 199 | kfree(wrk); |
| 200 | } |
| 201 | |
| 202 | static void smcd_release(struct device *dev) |
| 203 | { |
| 204 | struct smcd_dev *smcd = container_of(dev, struct smcd_dev, dev); |
| 205 | |
| 206 | kfree(smcd->conn); |
| 207 | kfree(smcd); |
| 208 | } |
| 209 | |
| 210 | struct smcd_dev *smcd_alloc_dev(struct device *parent, const char *name, |
| 211 | const struct smcd_ops *ops, int max_dmbs) |
| 212 | { |
| 213 | struct smcd_dev *smcd; |
| 214 | |
| 215 | smcd = kzalloc(sizeof(*smcd), GFP_KERNEL); |
| 216 | if (!smcd) |
| 217 | return NULL; |
| 218 | smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *), |
| 219 | GFP_KERNEL); |
| 220 | if (!smcd->conn) { |
| 221 | kfree(smcd); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | smcd->dev.parent = parent; |
| 226 | smcd->dev.release = smcd_release; |
| 227 | device_initialize(&smcd->dev); |
| 228 | dev_set_name(&smcd->dev, name); |
| 229 | smcd->ops = ops; |
| 230 | |
| 231 | spin_lock_init(&smcd->lock); |
| 232 | INIT_LIST_HEAD(&smcd->vlan); |
| 233 | smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)", |
| 234 | WQ_MEM_RECLAIM, name); |
| 235 | return smcd; |
| 236 | } |
| 237 | EXPORT_SYMBOL_GPL(smcd_alloc_dev); |
| 238 | |
| 239 | int smcd_register_dev(struct smcd_dev *smcd) |
| 240 | { |
| 241 | spin_lock(&smcd_dev_list.lock); |
| 242 | list_add_tail(&smcd->list, &smcd_dev_list.list); |
| 243 | spin_unlock(&smcd_dev_list.lock); |
| 244 | |
| 245 | return device_add(&smcd->dev); |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(smcd_register_dev); |
| 248 | |
| 249 | void smcd_unregister_dev(struct smcd_dev *smcd) |
| 250 | { |
| 251 | spin_lock(&smcd_dev_list.lock); |
| 252 | list_del(&smcd->list); |
| 253 | spin_unlock(&smcd_dev_list.lock); |
| 254 | flush_workqueue(smcd->event_wq); |
| 255 | destroy_workqueue(smcd->event_wq); |
| 256 | smc_smcd_terminate(smcd, 0); |
| 257 | |
| 258 | device_del(&smcd->dev); |
| 259 | } |
| 260 | EXPORT_SYMBOL_GPL(smcd_unregister_dev); |
| 261 | |
| 262 | void smcd_free_dev(struct smcd_dev *smcd) |
| 263 | { |
| 264 | put_device(&smcd->dev); |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(smcd_free_dev); |
| 267 | |
| 268 | /* SMCD Device event handler. Called from ISM device interrupt handler. |
| 269 | * Parameters are smcd device pointer, |
| 270 | * - event->type (0 --> DMB, 1 --> GID), |
| 271 | * - event->code (event code), |
| 272 | * - event->tok (either DMB token when event type 0, or GID when event type 1) |
| 273 | * - event->time (time of day) |
| 274 | * - event->info (debug info). |
| 275 | * |
| 276 | * Context: |
| 277 | * - Function called in IRQ context from ISM device driver event handler. |
| 278 | */ |
| 279 | void smcd_handle_event(struct smcd_dev *smcd, struct smcd_event *event) |
| 280 | { |
| 281 | struct smc_ism_event_work *wrk; |
| 282 | |
| 283 | /* copy event to event work queue, and let it be handled there */ |
| 284 | wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC); |
| 285 | if (!wrk) |
| 286 | return; |
| 287 | INIT_WORK(&wrk->work, smc_ism_event_work); |
| 288 | wrk->smcd = smcd; |
| 289 | wrk->event = *event; |
| 290 | queue_work(smcd->event_wq, &wrk->work); |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(smcd_handle_event); |
| 293 | |
| 294 | /* SMCD Device interrupt handler. Called from ISM device interrupt handler. |
| 295 | * Parameters are smcd device pointer and DMB number. Find the connection and |
| 296 | * schedule the tasklet for this connection. |
| 297 | * |
| 298 | * Context: |
| 299 | * - Function called in IRQ context from ISM device driver IRQ handler. |
| 300 | */ |
| 301 | void smcd_handle_irq(struct smcd_dev *smcd, unsigned int dmbno) |
| 302 | { |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(smcd_handle_irq); |