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