| 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/if_vlan.h> |
| 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <asm/page.h> |
| 14 | |
| 15 | #include "smc.h" |
| 16 | #include "smc_core.h" |
| 17 | #include "smc_ism.h" |
| 18 | #include "smc_pnet.h" |
| 19 | #include "smc_netlink.h" |
| 20 | #include "linux/dibs.h" |
| 21 | |
| 22 | struct smcd_dev_list smcd_dev_list = { |
| 23 | .list = LIST_HEAD_INIT(smcd_dev_list.list), |
| 24 | .mutex = __MUTEX_INITIALIZER(smcd_dev_list.mutex) |
| 25 | }; |
| 26 | |
| 27 | static bool smc_ism_v2_capable; |
| 28 | static u8 smc_ism_v2_system_eid[SMC_MAX_EID_LEN]; |
| 29 | |
| 30 | static void smcd_register_dev(struct dibs_dev *dibs); |
| 31 | static void smcd_unregister_dev(struct dibs_dev *dibs); |
| 32 | static void smcd_handle_event(struct dibs_dev *dibs, |
| 33 | const struct dibs_event *event); |
| 34 | static void smcd_handle_irq(struct dibs_dev *dibs, unsigned int dmbno, |
| 35 | u16 dmbemask); |
| 36 | |
| 37 | static struct dibs_client_ops smc_client_ops = { |
| 38 | .add_dev = smcd_register_dev, |
| 39 | .del_dev = smcd_unregister_dev, |
| 40 | .handle_event = smcd_handle_event, |
| 41 | .handle_irq = smcd_handle_irq, |
| 42 | }; |
| 43 | |
| 44 | static struct dibs_client smc_dibs_client = { |
| 45 | .name = "SMC-D" , |
| 46 | .ops = &smc_client_ops, |
| 47 | }; |
| 48 | |
| 49 | static void smc_ism_create_system_eid(void) |
| 50 | { |
| 51 | struct smc_ism_seid *seid = |
| 52 | (struct smc_ism_seid *)smc_ism_v2_system_eid; |
| 53 | #if IS_ENABLED(CONFIG_S390) |
| 54 | struct cpuid id; |
| 55 | u16 ident_tail; |
| 56 | char tmp[5]; |
| 57 | |
| 58 | memcpy(seid->seid_string, "IBM-SYSZ-ISMSEID00000000" , 24); |
| 59 | get_cpu_id(&id); |
| 60 | ident_tail = (u16)(id.ident & SMC_ISM_IDENT_MASK); |
| 61 | snprintf(tmp, 5, "%04X" , ident_tail); |
| 62 | memcpy(seid->serial_number, tmp, 4); |
| 63 | snprintf(tmp, 5, "%04X" , id.machine); |
| 64 | memcpy(seid->type, tmp, 4); |
| 65 | #else |
| 66 | memset(seid, 0, SMC_MAX_EID_LEN); |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | /* Test if an ISM communication is possible - same CPC */ |
| 71 | int smc_ism_cantalk(struct smcd_gid *peer_gid, unsigned short vlan_id, |
| 72 | struct smcd_dev *smcd) |
| 73 | { |
| 74 | struct dibs_dev *dibs = smcd->dibs; |
| 75 | uuid_t ism_rgid; |
| 76 | |
| 77 | copy_to_dibsgid(dibs_gid: &ism_rgid, sgid: peer_gid); |
| 78 | return dibs->ops->query_remote_gid(dibs, &ism_rgid, vlan_id ? 1 : 0, |
| 79 | vlan_id); |
| 80 | } |
| 81 | |
| 82 | void smc_ism_get_system_eid(u8 **eid) |
| 83 | { |
| 84 | if (!smc_ism_v2_capable) |
| 85 | *eid = NULL; |
| 86 | else |
| 87 | *eid = smc_ism_v2_system_eid; |
| 88 | } |
| 89 | |
| 90 | u16 smc_ism_get_chid(struct smcd_dev *smcd) |
| 91 | { |
| 92 | return smcd->dibs->ops->get_fabric_id(smcd->dibs); |
| 93 | } |
| 94 | |
| 95 | /* HW supports ISM V2 and thus System EID is defined */ |
| 96 | bool smc_ism_is_v2_capable(void) |
| 97 | { |
| 98 | return smc_ism_v2_capable; |
| 99 | } |
| 100 | |
| 101 | void smc_ism_set_v2_capable(void) |
| 102 | { |
| 103 | smc_ism_v2_capable = true; |
| 104 | } |
| 105 | |
| 106 | /* Set a connection using this DMBE. */ |
| 107 | void smc_ism_set_conn(struct smc_connection *conn) |
| 108 | { |
| 109 | unsigned long flags; |
| 110 | |
| 111 | spin_lock_irqsave(&conn->lgr->smcd->lock, flags); |
| 112 | conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = conn; |
| 113 | spin_unlock_irqrestore(lock: &conn->lgr->smcd->lock, flags); |
| 114 | } |
| 115 | |
| 116 | /* Unset a connection using this DMBE. */ |
| 117 | void smc_ism_unset_conn(struct smc_connection *conn) |
| 118 | { |
| 119 | unsigned long flags; |
| 120 | |
| 121 | if (!conn->rmb_desc) |
| 122 | return; |
| 123 | |
| 124 | spin_lock_irqsave(&conn->lgr->smcd->lock, flags); |
| 125 | conn->lgr->smcd->conn[conn->rmb_desc->sba_idx] = NULL; |
| 126 | spin_unlock_irqrestore(lock: &conn->lgr->smcd->lock, flags); |
| 127 | } |
| 128 | |
| 129 | /* Register a VLAN identifier with the ISM device. Use a reference count |
| 130 | * and add a VLAN identifier only when the first DMB using this VLAN is |
| 131 | * registered. |
| 132 | */ |
| 133 | int smc_ism_get_vlan(struct smcd_dev *smcd, unsigned short vlanid) |
| 134 | { |
| 135 | struct smc_ism_vlanid *new_vlan, *vlan; |
| 136 | unsigned long flags; |
| 137 | int rc = 0; |
| 138 | |
| 139 | if (!vlanid) /* No valid vlan id */ |
| 140 | return -EINVAL; |
| 141 | if (!smcd->dibs->ops->add_vlan_id) |
| 142 | return -EOPNOTSUPP; |
| 143 | |
| 144 | /* create new vlan entry, in case we need it */ |
| 145 | new_vlan = kzalloc(sizeof(*new_vlan), GFP_KERNEL); |
| 146 | if (!new_vlan) |
| 147 | return -ENOMEM; |
| 148 | new_vlan->vlanid = vlanid; |
| 149 | refcount_set(r: &new_vlan->refcnt, n: 1); |
| 150 | |
| 151 | /* if there is an existing entry, increase count and return */ |
| 152 | spin_lock_irqsave(&smcd->lock, flags); |
| 153 | list_for_each_entry(vlan, &smcd->vlan, list) { |
| 154 | if (vlan->vlanid == vlanid) { |
| 155 | refcount_inc(r: &vlan->refcnt); |
| 156 | kfree(objp: new_vlan); |
| 157 | goto out; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /* no existing entry found. |
| 162 | * add new entry to device; might fail, e.g., if HW limit reached |
| 163 | */ |
| 164 | if (smcd->dibs->ops->add_vlan_id(smcd->dibs, vlanid)) { |
| 165 | kfree(objp: new_vlan); |
| 166 | rc = -EIO; |
| 167 | goto out; |
| 168 | } |
| 169 | list_add_tail(new: &new_vlan->list, head: &smcd->vlan); |
| 170 | out: |
| 171 | spin_unlock_irqrestore(lock: &smcd->lock, flags); |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | /* Unregister a VLAN identifier with the ISM device. Use a reference count |
| 176 | * and remove a VLAN identifier only when the last DMB using this VLAN is |
| 177 | * unregistered. |
| 178 | */ |
| 179 | int smc_ism_put_vlan(struct smcd_dev *smcd, unsigned short vlanid) |
| 180 | { |
| 181 | struct smc_ism_vlanid *vlan; |
| 182 | unsigned long flags; |
| 183 | bool found = false; |
| 184 | int rc = 0; |
| 185 | |
| 186 | if (!vlanid) /* No valid vlan id */ |
| 187 | return -EINVAL; |
| 188 | if (!smcd->dibs->ops->del_vlan_id) |
| 189 | return -EOPNOTSUPP; |
| 190 | |
| 191 | spin_lock_irqsave(&smcd->lock, flags); |
| 192 | list_for_each_entry(vlan, &smcd->vlan, list) { |
| 193 | if (vlan->vlanid == vlanid) { |
| 194 | if (!refcount_dec_and_test(r: &vlan->refcnt)) |
| 195 | goto out; |
| 196 | found = true; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | if (!found) { |
| 201 | rc = -ENOENT; |
| 202 | goto out; /* VLAN id not in table */ |
| 203 | } |
| 204 | |
| 205 | /* Found and the last reference just gone */ |
| 206 | if (smcd->dibs->ops->del_vlan_id(smcd->dibs, vlanid)) |
| 207 | rc = -EIO; |
| 208 | list_del(entry: &vlan->list); |
| 209 | kfree(objp: vlan); |
| 210 | out: |
| 211 | spin_unlock_irqrestore(lock: &smcd->lock, flags); |
| 212 | return rc; |
| 213 | } |
| 214 | |
| 215 | void smc_ism_unregister_dmb(struct smcd_dev *smcd, |
| 216 | struct smc_buf_desc *dmb_desc) |
| 217 | { |
| 218 | struct dibs_dmb dmb; |
| 219 | |
| 220 | if (!dmb_desc->dma_addr) |
| 221 | return; |
| 222 | |
| 223 | memset(&dmb, 0, sizeof(dmb)); |
| 224 | dmb.dmb_tok = dmb_desc->token; |
| 225 | dmb.idx = dmb_desc->sba_idx; |
| 226 | dmb.cpu_addr = dmb_desc->cpu_addr; |
| 227 | dmb.dma_addr = dmb_desc->dma_addr; |
| 228 | dmb.dmb_len = dmb_desc->len; |
| 229 | |
| 230 | smcd->dibs->ops->unregister_dmb(smcd->dibs, &dmb); |
| 231 | |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | int smc_ism_register_dmb(struct smc_link_group *lgr, int dmb_len, |
| 236 | struct smc_buf_desc *dmb_desc) |
| 237 | { |
| 238 | struct dibs_dev *dibs; |
| 239 | struct dibs_dmb dmb; |
| 240 | int rc; |
| 241 | |
| 242 | memset(&dmb, 0, sizeof(dmb)); |
| 243 | dmb.dmb_len = dmb_len; |
| 244 | dmb.idx = dmb_desc->sba_idx; |
| 245 | dmb.vlan_id = lgr->vlan_id; |
| 246 | copy_to_dibsgid(dibs_gid: &dmb.rgid, sgid: &lgr->peer_gid); |
| 247 | |
| 248 | dibs = lgr->smcd->dibs; |
| 249 | rc = dibs->ops->register_dmb(dibs, &dmb, &smc_dibs_client); |
| 250 | if (!rc) { |
| 251 | dmb_desc->sba_idx = dmb.idx; |
| 252 | dmb_desc->token = dmb.dmb_tok; |
| 253 | dmb_desc->cpu_addr = dmb.cpu_addr; |
| 254 | dmb_desc->dma_addr = dmb.dma_addr; |
| 255 | dmb_desc->len = dmb.dmb_len; |
| 256 | } |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | bool smc_ism_support_dmb_nocopy(struct smcd_dev *smcd) |
| 261 | { |
| 262 | /* for now only loopback-ism supports |
| 263 | * merging sndbuf with peer DMB to avoid |
| 264 | * data copies between them. |
| 265 | */ |
| 266 | return (smcd->dibs->ops->support_mmapped_rdmb && |
| 267 | smcd->dibs->ops->support_mmapped_rdmb(smcd->dibs)); |
| 268 | } |
| 269 | |
| 270 | int smc_ism_attach_dmb(struct smcd_dev *dev, u64 token, |
| 271 | struct smc_buf_desc *dmb_desc) |
| 272 | { |
| 273 | struct dibs_dmb dmb; |
| 274 | int rc = 0; |
| 275 | |
| 276 | if (!dev->dibs->ops->attach_dmb) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | memset(&dmb, 0, sizeof(dmb)); |
| 280 | dmb.dmb_tok = token; |
| 281 | rc = dev->dibs->ops->attach_dmb(dev->dibs, &dmb); |
| 282 | if (!rc) { |
| 283 | dmb_desc->sba_idx = dmb.idx; |
| 284 | dmb_desc->token = dmb.dmb_tok; |
| 285 | dmb_desc->cpu_addr = dmb.cpu_addr; |
| 286 | dmb_desc->dma_addr = dmb.dma_addr; |
| 287 | dmb_desc->len = dmb.dmb_len; |
| 288 | dmb_desc->is_attached = true; |
| 289 | } |
| 290 | return rc; |
| 291 | } |
| 292 | |
| 293 | int smc_ism_detach_dmb(struct smcd_dev *dev, u64 token) |
| 294 | { |
| 295 | if (!dev->dibs->ops->detach_dmb) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | return dev->dibs->ops->detach_dmb(dev->dibs, token); |
| 299 | } |
| 300 | |
| 301 | static int smc_nl_handle_smcd_dev(struct smcd_dev *smcd, |
| 302 | struct sk_buff *skb, |
| 303 | struct netlink_callback *cb) |
| 304 | { |
| 305 | char smc_pnet[SMC_MAX_PNETID_LEN + 1]; |
| 306 | struct smc_pci_dev smc_pci_dev; |
| 307 | struct nlattr *port_attrs; |
| 308 | struct dibs_dev *dibs; |
| 309 | struct nlattr *attrs; |
| 310 | int use_cnt = 0; |
| 311 | void *nlh; |
| 312 | |
| 313 | dibs = smcd->dibs; |
| 314 | nlh = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, seq: cb->nlh->nlmsg_seq, |
| 315 | family: &smc_gen_nl_family, NLM_F_MULTI, |
| 316 | cmd: SMC_NETLINK_GET_DEV_SMCD); |
| 317 | if (!nlh) |
| 318 | goto errmsg; |
| 319 | attrs = nla_nest_start(skb, attrtype: SMC_GEN_DEV_SMCD); |
| 320 | if (!attrs) |
| 321 | goto errout; |
| 322 | use_cnt = atomic_read(v: &smcd->lgr_cnt); |
| 323 | if (nla_put_u32(skb, attrtype: SMC_NLA_DEV_USE_CNT, value: use_cnt)) |
| 324 | goto errattr; |
| 325 | if (nla_put_u8(skb, attrtype: SMC_NLA_DEV_IS_CRIT, value: use_cnt > 0)) |
| 326 | goto errattr; |
| 327 | memset(&smc_pci_dev, 0, sizeof(smc_pci_dev)); |
| 328 | smc_set_pci_values(to_pci_dev(dibs->dev.parent), smc_dev: &smc_pci_dev); |
| 329 | if (nla_put_u32(skb, attrtype: SMC_NLA_DEV_PCI_FID, value: smc_pci_dev.pci_fid)) |
| 330 | goto errattr; |
| 331 | if (nla_put_u16(skb, attrtype: SMC_NLA_DEV_PCI_CHID, value: smc_pci_dev.pci_pchid)) |
| 332 | goto errattr; |
| 333 | if (nla_put_u16(skb, attrtype: SMC_NLA_DEV_PCI_VENDOR, value: smc_pci_dev.pci_vendor)) |
| 334 | goto errattr; |
| 335 | if (nla_put_u16(skb, attrtype: SMC_NLA_DEV_PCI_DEVICE, value: smc_pci_dev.pci_device)) |
| 336 | goto errattr; |
| 337 | if (nla_put_string(skb, attrtype: SMC_NLA_DEV_PCI_ID, str: smc_pci_dev.pci_id)) |
| 338 | goto errattr; |
| 339 | |
| 340 | port_attrs = nla_nest_start(skb, attrtype: SMC_NLA_DEV_PORT); |
| 341 | if (!port_attrs) |
| 342 | goto errattr; |
| 343 | if (nla_put_u8(skb, attrtype: SMC_NLA_DEV_PORT_PNET_USR, value: smcd->pnetid_by_user)) |
| 344 | goto errportattr; |
| 345 | memcpy(smc_pnet, smcd->pnetid, SMC_MAX_PNETID_LEN); |
| 346 | smc_pnet[SMC_MAX_PNETID_LEN] = 0; |
| 347 | if (nla_put_string(skb, attrtype: SMC_NLA_DEV_PORT_PNETID, str: smc_pnet)) |
| 348 | goto errportattr; |
| 349 | |
| 350 | nla_nest_end(skb, start: port_attrs); |
| 351 | nla_nest_end(skb, start: attrs); |
| 352 | genlmsg_end(skb, hdr: nlh); |
| 353 | return 0; |
| 354 | |
| 355 | errportattr: |
| 356 | nla_nest_cancel(skb, start: port_attrs); |
| 357 | errattr: |
| 358 | nla_nest_cancel(skb, start: attrs); |
| 359 | errout: |
| 360 | nlmsg_cancel(skb, nlh); |
| 361 | errmsg: |
| 362 | return -EMSGSIZE; |
| 363 | } |
| 364 | |
| 365 | static void smc_nl_prep_smcd_dev(struct smcd_dev_list *dev_list, |
| 366 | struct sk_buff *skb, |
| 367 | struct netlink_callback *cb) |
| 368 | { |
| 369 | struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(c: cb); |
| 370 | int snum = cb_ctx->pos[0]; |
| 371 | struct smcd_dev *smcd; |
| 372 | int num = 0; |
| 373 | |
| 374 | mutex_lock(&dev_list->mutex); |
| 375 | list_for_each_entry(smcd, &dev_list->list, list) { |
| 376 | if (num < snum) |
| 377 | goto next; |
| 378 | if (smc_ism_is_loopback(dibs: smcd->dibs)) |
| 379 | goto next; |
| 380 | if (smc_nl_handle_smcd_dev(smcd, skb, cb)) |
| 381 | goto errout; |
| 382 | next: |
| 383 | num++; |
| 384 | } |
| 385 | errout: |
| 386 | mutex_unlock(lock: &dev_list->mutex); |
| 387 | cb_ctx->pos[0] = num; |
| 388 | } |
| 389 | |
| 390 | int smcd_nl_get_device(struct sk_buff *skb, struct netlink_callback *cb) |
| 391 | { |
| 392 | smc_nl_prep_smcd_dev(dev_list: &smcd_dev_list, skb, cb); |
| 393 | return skb->len; |
| 394 | } |
| 395 | |
| 396 | struct smc_ism_event_work { |
| 397 | struct work_struct work; |
| 398 | struct smcd_dev *smcd; |
| 399 | struct dibs_event event; |
| 400 | }; |
| 401 | |
| 402 | #define ISM_EVENT_REQUEST 0x0001 |
| 403 | #define ISM_EVENT_RESPONSE 0x0002 |
| 404 | #define ISM_EVENT_REQUEST_IR 0x00000001 |
| 405 | #define ISM_EVENT_CODE_SHUTDOWN 0x80 |
| 406 | #define ISM_EVENT_CODE_TESTLINK 0x83 |
| 407 | |
| 408 | union smcd_sw_event_info { |
| 409 | u64 info; |
| 410 | struct { |
| 411 | u8 uid[SMC_LGR_ID_SIZE]; |
| 412 | unsigned short vlan_id; |
| 413 | u16 code; |
| 414 | }; |
| 415 | }; |
| 416 | |
| 417 | static void smcd_handle_sw_event(struct smc_ism_event_work *wrk) |
| 418 | { |
| 419 | struct dibs_dev *dibs = wrk->smcd->dibs; |
| 420 | union smcd_sw_event_info ev_info; |
| 421 | struct smcd_gid peer_gid; |
| 422 | uuid_t ism_rgid; |
| 423 | |
| 424 | copy_to_smcdgid(sgid: &peer_gid, dibs_gid: &wrk->event.gid); |
| 425 | ev_info.info = wrk->event.data; |
| 426 | switch (wrk->event.subtype) { |
| 427 | case ISM_EVENT_CODE_SHUTDOWN: /* Peer shut down DMBs */ |
| 428 | smc_smcd_terminate(dev: wrk->smcd, peer_gid: &peer_gid, vlan: ev_info.vlan_id); |
| 429 | break; |
| 430 | case ISM_EVENT_CODE_TESTLINK: /* Activity timer */ |
| 431 | if (ev_info.code == ISM_EVENT_REQUEST && |
| 432 | dibs->ops->signal_event) { |
| 433 | ev_info.code = ISM_EVENT_RESPONSE; |
| 434 | copy_to_dibsgid(dibs_gid: &ism_rgid, sgid: &peer_gid); |
| 435 | dibs->ops->signal_event(dibs, &ism_rgid, |
| 436 | ISM_EVENT_REQUEST_IR, |
| 437 | ISM_EVENT_CODE_TESTLINK, |
| 438 | ev_info.info); |
| 439 | } |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* worker for SMC-D events */ |
| 445 | static void smc_ism_event_work(struct work_struct *work) |
| 446 | { |
| 447 | struct smc_ism_event_work *wrk = |
| 448 | container_of(work, struct smc_ism_event_work, work); |
| 449 | struct smcd_gid smcd_gid; |
| 450 | |
| 451 | copy_to_smcdgid(sgid: &smcd_gid, dibs_gid: &wrk->event.gid); |
| 452 | |
| 453 | switch (wrk->event.type) { |
| 454 | case DIBS_DEV_EVENT: /* GID event, token is peer GID */ |
| 455 | smc_smcd_terminate(dev: wrk->smcd, peer_gid: &smcd_gid, VLAN_VID_MASK); |
| 456 | break; |
| 457 | case DIBS_BUF_EVENT: |
| 458 | break; |
| 459 | case DIBS_SW_EVENT: /* Software defined event */ |
| 460 | smcd_handle_sw_event(wrk); |
| 461 | break; |
| 462 | } |
| 463 | kfree(objp: wrk); |
| 464 | } |
| 465 | |
| 466 | static struct smcd_dev *smcd_alloc_dev(const char *name, int max_dmbs) |
| 467 | { |
| 468 | struct smcd_dev *smcd; |
| 469 | |
| 470 | smcd = kzalloc(sizeof(*smcd), GFP_KERNEL); |
| 471 | if (!smcd) |
| 472 | return NULL; |
| 473 | smcd->conn = kcalloc(max_dmbs, sizeof(struct smc_connection *), |
| 474 | GFP_KERNEL); |
| 475 | if (!smcd->conn) |
| 476 | goto free_smcd; |
| 477 | |
| 478 | smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)" , |
| 479 | WQ_MEM_RECLAIM, name); |
| 480 | if (!smcd->event_wq) |
| 481 | goto free_conn; |
| 482 | |
| 483 | spin_lock_init(&smcd->lock); |
| 484 | spin_lock_init(&smcd->lgr_lock); |
| 485 | INIT_LIST_HEAD(list: &smcd->vlan); |
| 486 | INIT_LIST_HEAD(list: &smcd->lgr_list); |
| 487 | init_waitqueue_head(&smcd->lgrs_deleted); |
| 488 | return smcd; |
| 489 | |
| 490 | free_conn: |
| 491 | kfree(objp: smcd->conn); |
| 492 | free_smcd: |
| 493 | kfree(objp: smcd); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | static void smcd_register_dev(struct dibs_dev *dibs) |
| 498 | { |
| 499 | struct smcd_dev *smcd, *fentry; |
| 500 | int max_dmbs; |
| 501 | |
| 502 | max_dmbs = dibs->ops->max_dmbs(); |
| 503 | |
| 504 | smcd = smcd_alloc_dev(name: dev_name(dev: &dibs->dev), max_dmbs); |
| 505 | if (!smcd) |
| 506 | return; |
| 507 | |
| 508 | smcd->dibs = dibs; |
| 509 | dibs_set_priv(dev: dibs, client: &smc_dibs_client, priv: smcd); |
| 510 | |
| 511 | if (smc_pnetid_by_dev_port(dev: dibs->dev.parent, port: 0, pnetid: smcd->pnetid)) |
| 512 | smc_pnetid_by_table_smcd(smcd); |
| 513 | |
| 514 | if (smc_ism_is_loopback(dibs) || |
| 515 | (dibs->ops->add_vlan_id && |
| 516 | !dibs->ops->add_vlan_id(dibs, ISM_RESERVED_VLANID))) { |
| 517 | smc_ism_set_v2_capable(); |
| 518 | } |
| 519 | |
| 520 | mutex_lock(&smcd_dev_list.mutex); |
| 521 | /* sort list: |
| 522 | * - devices without pnetid before devices with pnetid; |
| 523 | * - loopback-ism always at the very beginning; |
| 524 | */ |
| 525 | if (!smcd->pnetid[0]) { |
| 526 | fentry = list_first_entry_or_null(&smcd_dev_list.list, |
| 527 | struct smcd_dev, list); |
| 528 | if (fentry && smc_ism_is_loopback(dibs: fentry->dibs)) |
| 529 | list_add(new: &smcd->list, head: &fentry->list); |
| 530 | else |
| 531 | list_add(new: &smcd->list, head: &smcd_dev_list.list); |
| 532 | } else { |
| 533 | list_add_tail(new: &smcd->list, head: &smcd_dev_list.list); |
| 534 | } |
| 535 | mutex_unlock(lock: &smcd_dev_list.mutex); |
| 536 | |
| 537 | if (smc_pnet_is_pnetid_set(pnetid: smcd->pnetid)) |
| 538 | pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n" , |
| 539 | dev_name(&dibs->dev), smcd->pnetid, |
| 540 | smcd->pnetid_by_user ? |
| 541 | " (user defined)" : |
| 542 | "" ); |
| 543 | else |
| 544 | pr_warn_ratelimited("smc: adding smcd device %s without pnetid\n" , |
| 545 | dev_name(&dibs->dev)); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | static void smcd_unregister_dev(struct dibs_dev *dibs) |
| 550 | { |
| 551 | struct smcd_dev *smcd = dibs_get_priv(dev: dibs, client: &smc_dibs_client); |
| 552 | |
| 553 | pr_warn_ratelimited("smc: removing smcd device %s\n" , |
| 554 | dev_name(&dibs->dev)); |
| 555 | smcd->going_away = 1; |
| 556 | smc_smcd_terminate_all(dev: smcd); |
| 557 | mutex_lock(&smcd_dev_list.mutex); |
| 558 | list_del_init(entry: &smcd->list); |
| 559 | mutex_unlock(lock: &smcd_dev_list.mutex); |
| 560 | destroy_workqueue(wq: smcd->event_wq); |
| 561 | kfree(objp: smcd->conn); |
| 562 | kfree(objp: smcd); |
| 563 | } |
| 564 | |
| 565 | /* SMCD Device event handler. Called from ISM device interrupt handler. |
| 566 | * Parameters are ism device pointer, |
| 567 | * - event->type (0 --> DMB, 1 --> GID), |
| 568 | * - event->code (event code), |
| 569 | * - event->tok (either DMB token when event type 0, or GID when event type 1) |
| 570 | * - event->time (time of day) |
| 571 | * - event->info (debug info). |
| 572 | * |
| 573 | * Context: |
| 574 | * - Function called in IRQ context from ISM device driver event handler. |
| 575 | */ |
| 576 | static void smcd_handle_event(struct dibs_dev *dibs, |
| 577 | const struct dibs_event *event) |
| 578 | { |
| 579 | struct smcd_dev *smcd = dibs_get_priv(dev: dibs, client: &smc_dibs_client); |
| 580 | struct smc_ism_event_work *wrk; |
| 581 | |
| 582 | if (smcd->going_away) |
| 583 | return; |
| 584 | /* copy event to event work queue, and let it be handled there */ |
| 585 | wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC); |
| 586 | if (!wrk) |
| 587 | return; |
| 588 | INIT_WORK(&wrk->work, smc_ism_event_work); |
| 589 | wrk->smcd = smcd; |
| 590 | wrk->event = *event; |
| 591 | queue_work(wq: smcd->event_wq, work: &wrk->work); |
| 592 | } |
| 593 | |
| 594 | /* SMCD Device interrupt handler. Called from ISM device interrupt handler. |
| 595 | * Parameters are the ism device pointer, DMB number, and the DMBE bitmask. |
| 596 | * Find the connection and schedule the tasklet for this connection. |
| 597 | * |
| 598 | * Context: |
| 599 | * - Function called in IRQ context from ISM device driver IRQ handler. |
| 600 | */ |
| 601 | static void smcd_handle_irq(struct dibs_dev *dibs, unsigned int dmbno, |
| 602 | u16 dmbemask) |
| 603 | { |
| 604 | struct smcd_dev *smcd = dibs_get_priv(dev: dibs, client: &smc_dibs_client); |
| 605 | struct smc_connection *conn = NULL; |
| 606 | unsigned long flags; |
| 607 | |
| 608 | spin_lock_irqsave(&smcd->lock, flags); |
| 609 | conn = smcd->conn[dmbno]; |
| 610 | if (conn && !conn->killed) |
| 611 | tasklet_schedule(t: &conn->rx_tsklet); |
| 612 | spin_unlock_irqrestore(lock: &smcd->lock, flags); |
| 613 | } |
| 614 | |
| 615 | int smc_ism_signal_shutdown(struct smc_link_group *lgr) |
| 616 | { |
| 617 | int rc = 0; |
| 618 | union smcd_sw_event_info ev_info; |
| 619 | uuid_t ism_rgid; |
| 620 | |
| 621 | if (lgr->peer_shutdown) |
| 622 | return 0; |
| 623 | if (!lgr->smcd->dibs->ops->signal_event) |
| 624 | return 0; |
| 625 | |
| 626 | memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE); |
| 627 | ev_info.vlan_id = lgr->vlan_id; |
| 628 | ev_info.code = ISM_EVENT_REQUEST; |
| 629 | copy_to_dibsgid(dibs_gid: &ism_rgid, sgid: &lgr->peer_gid); |
| 630 | rc = lgr->smcd->dibs->ops->signal_event(lgr->smcd->dibs, &ism_rgid, |
| 631 | ISM_EVENT_REQUEST_IR, |
| 632 | ISM_EVENT_CODE_SHUTDOWN, |
| 633 | ev_info.info); |
| 634 | return rc; |
| 635 | } |
| 636 | |
| 637 | int smc_ism_init(void) |
| 638 | { |
| 639 | int rc = 0; |
| 640 | |
| 641 | smc_ism_v2_capable = false; |
| 642 | smc_ism_create_system_eid(); |
| 643 | |
| 644 | rc = dibs_register_client(client: &smc_dibs_client); |
| 645 | return rc; |
| 646 | } |
| 647 | |
| 648 | void smc_ism_exit(void) |
| 649 | { |
| 650 | dibs_unregister_client(client: &smc_dibs_client); |
| 651 | } |
| 652 | |