1 // SPDX-License-Identifier: GPL-2.0 1 2 /* Shared Memory Communications Direct over IS 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.l 24 .mutex = __MUTEX_INITIALIZER(smcd_dev_ 25 }; 26 27 static bool smc_ism_v2_capable; 28 static u8 smc_ism_v2_system_eid[SMC_MAX_EID_LE 29 30 #if IS_ENABLED(CONFIG_ISM) 31 static void smcd_register_dev(struct ism_dev * 32 static void smcd_unregister_dev(struct ism_dev 33 static void smcd_handle_event(struct ism_dev * 34 static void smcd_handle_irq(struct ism_dev *is 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 static void smc_ism_create_system_eid(void) 47 { 48 struct smc_ism_seid *seid = 49 (struct smc_ism_seid *)smc_ism 50 #if IS_ENABLED(CONFIG_S390) 51 struct cpuid id; 52 u16 ident_tail; 53 char tmp[5]; 54 55 memcpy(seid->seid_string, "IBM-SYSZ-IS 56 get_cpu_id(&id); 57 ident_tail = (u16)(id.ident & SMC_ISM_ 58 snprintf(tmp, 5, "%04X", ident_tail); 59 memcpy(seid->serial_number, tmp, 4); 60 snprintf(tmp, 5, "%04X", id.machine); 61 memcpy(seid->type, tmp, 4); 62 #else 63 memset(seid, 0, SMC_MAX_EID_LEN); 64 #endif 65 } 66 67 /* Test if an ISM communication is possible - 68 int smc_ism_cantalk(struct smcd_gid *peer_gid, 69 struct smcd_dev *smcd) 70 { 71 return smcd->ops->query_remote_gid(smc 72 vla 73 } 74 75 void smc_ism_get_system_eid(u8 **eid) 76 { 77 if (!smc_ism_v2_capable) 78 *eid = NULL; 79 else 80 *eid = smc_ism_v2_system_eid; 81 } 82 83 u16 smc_ism_get_chid(struct smcd_dev *smcd) 84 { 85 return smcd->ops->get_chid(smcd); 86 } 87 88 /* HW supports ISM V2 and thus System EID is d 89 bool smc_ism_is_v2_capable(void) 90 { 91 return smc_ism_v2_capable; 92 } 93 94 void smc_ism_set_v2_capable(void) 95 { 96 smc_ism_v2_capable = true; 97 } 98 99 /* Set a connection using this DMBE. */ 100 void smc_ism_set_conn(struct smc_connection *c 101 { 102 unsigned long flags; 103 104 spin_lock_irqsave(&conn->lgr->smcd->lo 105 conn->lgr->smcd->conn[conn->rmb_desc-> 106 spin_unlock_irqrestore(&conn->lgr->smc 107 } 108 109 /* Unset a connection using this DMBE. */ 110 void smc_ism_unset_conn(struct smc_connection 111 { 112 unsigned long flags; 113 114 if (!conn->rmb_desc) 115 return; 116 117 spin_lock_irqsave(&conn->lgr->smcd->lo 118 conn->lgr->smcd->conn[conn->rmb_desc-> 119 spin_unlock_irqrestore(&conn->lgr->smc 120 } 121 122 /* Register a VLAN identifier with the ISM dev 123 * and add a VLAN identifier only when the fir 124 * registered. 125 */ 126 int smc_ism_get_vlan(struct smcd_dev *smcd, un 127 { 128 struct smc_ism_vlanid *new_vlan, *vlan 129 unsigned long flags; 130 int rc = 0; 131 132 if (!vlanid) /* No 133 return -EINVAL; 134 if (!smcd->ops->add_vlan_id) 135 return -EOPNOTSUPP; 136 137 /* create new vlan entry, in case we n 138 new_vlan = kzalloc(sizeof(*new_vlan), 139 if (!new_vlan) 140 return -ENOMEM; 141 new_vlan->vlanid = vlanid; 142 refcount_set(&new_vlan->refcnt, 1); 143 144 /* if there is an existing entry, incr 145 spin_lock_irqsave(&smcd->lock, flags); 146 list_for_each_entry(vlan, &smcd->vlan, 147 if (vlan->vlanid == vlanid) { 148 refcount_inc(&vlan->re 149 kfree(new_vlan); 150 goto out; 151 } 152 } 153 154 /* no existing entry found. 155 * add new entry to device; might fail 156 */ 157 if (smcd->ops->add_vlan_id(smcd, vlani 158 kfree(new_vlan); 159 rc = -EIO; 160 goto out; 161 } 162 list_add_tail(&new_vlan->list, &smcd-> 163 out: 164 spin_unlock_irqrestore(&smcd->lock, fl 165 return rc; 166 } 167 168 /* Unregister a VLAN identifier with the ISM d 169 * and remove a VLAN identifier only when the 170 * unregistered. 171 */ 172 int smc_ism_put_vlan(struct smcd_dev *smcd, un 173 { 174 struct smc_ism_vlanid *vlan; 175 unsigned long flags; 176 bool found = false; 177 int rc = 0; 178 179 if (!vlanid) /* No 180 return -EINVAL; 181 if (!smcd->ops->del_vlan_id) 182 return -EOPNOTSUPP; 183 184 spin_lock_irqsave(&smcd->lock, flags); 185 list_for_each_entry(vlan, &smcd->vlan, 186 if (vlan->vlanid == vlanid) { 187 if (!refcount_dec_and_ 188 goto out; 189 found = true; 190 break; 191 } 192 } 193 if (!found) { 194 rc = -ENOENT; 195 goto out; /* VLA 196 } 197 198 /* Found and the last reference just g 199 if (smcd->ops->del_vlan_id(smcd, vlani 200 rc = -EIO; 201 list_del(&vlan->list); 202 kfree(vlan); 203 out: 204 spin_unlock_irqrestore(&smcd->lock, fl 205 return rc; 206 } 207 208 int smc_ism_unregister_dmb(struct smcd_dev *sm 209 { 210 struct smcd_dmb dmb; 211 int rc = 0; 212 213 if (!dmb_desc->dma_addr) 214 return rc; 215 216 memset(&dmb, 0, sizeof(dmb)); 217 dmb.dmb_tok = dmb_desc->token; 218 dmb.sba_idx = dmb_desc->sba_idx; 219 dmb.cpu_addr = dmb_desc->cpu_addr; 220 dmb.dma_addr = dmb_desc->dma_addr; 221 dmb.dmb_len = dmb_desc->len; 222 rc = smcd->ops->unregister_dmb(smcd, & 223 if (!rc || rc == ISM_ERROR) { 224 dmb_desc->cpu_addr = NULL; 225 dmb_desc->dma_addr = 0; 226 } 227 228 return rc; 229 } 230 231 int smc_ism_register_dmb(struct smc_link_group 232 struct smc_buf_desc * 233 { 234 struct smcd_dmb dmb; 235 int rc; 236 237 memset(&dmb, 0, sizeof(dmb)); 238 dmb.dmb_len = dmb_len; 239 dmb.sba_idx = dmb_desc->sba_idx; 240 dmb.vlan_id = lgr->vlan_id; 241 dmb.rgid = lgr->peer_gid.gid; 242 rc = lgr->smcd->ops->register_dmb(lgr- 243 if (!rc) { 244 dmb_desc->sba_idx = dmb.sba_id 245 dmb_desc->token = dmb.dmb_tok; 246 dmb_desc->cpu_addr = dmb.cpu_a 247 dmb_desc->dma_addr = dmb.dma_a 248 dmb_desc->len = dmb.dmb_len; 249 } 250 return rc; 251 } 252 253 bool smc_ism_support_dmb_nocopy(struct smcd_de 254 { 255 /* for now only loopback-ism supports 256 * merging sndbuf with peer DMB to avo 257 * data copies between them. 258 */ 259 return (smcd->ops->support_dmb_nocopy 260 smcd->ops->support_dmb_nocopy( 261 } 262 263 int smc_ism_attach_dmb(struct smcd_dev *dev, u 264 struct smc_buf_desc *dm 265 { 266 struct smcd_dmb dmb; 267 int rc = 0; 268 269 if (!dev->ops->attach_dmb) 270 return -EINVAL; 271 272 memset(&dmb, 0, sizeof(dmb)); 273 dmb.dmb_tok = token; 274 rc = dev->ops->attach_dmb(dev, &dmb); 275 if (!rc) { 276 dmb_desc->sba_idx = dmb.sba_id 277 dmb_desc->token = dmb.dmb_tok; 278 dmb_desc->cpu_addr = dmb.cpu_a 279 dmb_desc->dma_addr = dmb.dma_a 280 dmb_desc->len = dmb.dmb_len; 281 } 282 return rc; 283 } 284 285 int smc_ism_detach_dmb(struct smcd_dev *dev, u 286 { 287 if (!dev->ops->detach_dmb) 288 return -EINVAL; 289 290 return dev->ops->detach_dmb(dev, token 291 } 292 293 static int smc_nl_handle_smcd_dev(struct smcd_ 294 struct sk_bu 295 struct netli 296 { 297 char smc_pnet[SMC_MAX_PNETID_LEN + 1]; 298 struct smc_pci_dev smc_pci_dev; 299 struct nlattr *port_attrs; 300 struct nlattr *attrs; 301 struct ism_dev *ism; 302 int use_cnt = 0; 303 void *nlh; 304 305 ism = smcd->priv; 306 nlh = genlmsg_put(skb, NETLINK_CB(cb-> 307 &smc_gen_nl_family, 308 SMC_NETLINK_GET_DEV_ 309 if (!nlh) 310 goto errmsg; 311 attrs = nla_nest_start(skb, SMC_GEN_DE 312 if (!attrs) 313 goto errout; 314 use_cnt = atomic_read(&smcd->lgr_cnt); 315 if (nla_put_u32(skb, SMC_NLA_DEV_USE_C 316 goto errattr; 317 if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRI 318 goto errattr; 319 memset(&smc_pci_dev, 0, sizeof(smc_pci 320 smc_set_pci_values(to_pci_dev(ism->dev 321 if (nla_put_u32(skb, SMC_NLA_DEV_PCI_F 322 goto errattr; 323 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_C 324 goto errattr; 325 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_V 326 goto errattr; 327 if (nla_put_u16(skb, SMC_NLA_DEV_PCI_D 328 goto errattr; 329 if (nla_put_string(skb, SMC_NLA_DEV_PC 330 goto errattr; 331 332 port_attrs = nla_nest_start(skb, SMC_N 333 if (!port_attrs) 334 goto errattr; 335 if (nla_put_u8(skb, SMC_NLA_DEV_PORT_P 336 goto errportattr; 337 memcpy(smc_pnet, smcd->pnetid, SMC_MAX 338 smc_pnet[SMC_MAX_PNETID_LEN] = 0; 339 if (nla_put_string(skb, SMC_NLA_DEV_PO 340 goto errportattr; 341 342 nla_nest_end(skb, port_attrs); 343 nla_nest_end(skb, attrs); 344 genlmsg_end(skb, nlh); 345 return 0; 346 347 errportattr: 348 nla_nest_cancel(skb, port_attrs); 349 errattr: 350 nla_nest_cancel(skb, attrs); 351 errout: 352 nlmsg_cancel(skb, nlh); 353 errmsg: 354 return -EMSGSIZE; 355 } 356 357 static void smc_nl_prep_smcd_dev(struct smcd_d 358 struct sk_buf 359 struct netlin 360 { 361 struct smc_nl_dmp_ctx *cb_ctx = smc_nl 362 int snum = cb_ctx->pos[0]; 363 struct smcd_dev *smcd; 364 int num = 0; 365 366 mutex_lock(&dev_list->mutex); 367 list_for_each_entry(smcd, &dev_list->l 368 if (num < snum) 369 goto next; 370 if (smc_ism_is_loopback(smcd)) 371 goto next; 372 if (smc_nl_handle_smcd_dev(smc 373 goto errout; 374 next: 375 num++; 376 } 377 errout: 378 mutex_unlock(&dev_list->mutex); 379 cb_ctx->pos[0] = num; 380 } 381 382 int smcd_nl_get_device(struct sk_buff *skb, st 383 { 384 smc_nl_prep_smcd_dev(&smcd_dev_list, s 385 return skb->len; 386 } 387 388 #if IS_ENABLED(CONFIG_ISM) 389 struct smc_ism_event_work { 390 struct work_struct work; 391 struct smcd_dev *smcd; 392 struct ism_event event; 393 }; 394 395 #define ISM_EVENT_REQUEST 0x0001 396 #define ISM_EVENT_RESPONSE 0x0002 397 #define ISM_EVENT_REQUEST_IR 0x0000 398 #define ISM_EVENT_CODE_SHUTDOWN 0x80 399 #define ISM_EVENT_CODE_TESTLINK 0x83 400 401 union smcd_sw_event_info { 402 u64 info; 403 struct { 404 u8 uid[SMC_LGR_ID 405 unsigned short vlan_id; 406 u16 code; 407 }; 408 }; 409 410 static void smcd_handle_sw_event(struct smc_is 411 { 412 struct smcd_gid peer_gid = { .gid = wr 413 .gid_ext 414 union smcd_sw_event_info ev_info; 415 416 ev_info.info = wrk->event.info; 417 switch (wrk->event.code) { 418 case ISM_EVENT_CODE_SHUTDOWN: /* Pee 419 smc_smcd_terminate(wrk->smcd, 420 break; 421 case ISM_EVENT_CODE_TESTLINK: /* Act 422 if (ev_info.code == ISM_EVENT_ 423 wrk->smcd->ops->signal_eve 424 ev_info.code = ISM_EVE 425 wrk->smcd->ops->signal 426 427 428 429 430 } 431 break; 432 } 433 } 434 435 /* worker for SMC-D events */ 436 static void smc_ism_event_work(struct work_str 437 { 438 struct smc_ism_event_work *wrk = 439 container_of(work, struct smc_ 440 struct smcd_gid smcd_gid = { .gid = wr 441 .gid_ext 442 443 switch (wrk->event.type) { 444 case ISM_EVENT_GID: /* GID event, 445 smc_smcd_terminate(wrk->smcd, 446 break; 447 case ISM_EVENT_DMB: 448 break; 449 case ISM_EVENT_SWR: /* Software de 450 smcd_handle_sw_event(wrk); 451 break; 452 } 453 kfree(wrk); 454 } 455 456 static struct smcd_dev *smcd_alloc_dev(struct 457 const s 458 { 459 struct smcd_dev *smcd; 460 461 smcd = devm_kzalloc(parent, sizeof(*sm 462 if (!smcd) 463 return NULL; 464 smcd->conn = devm_kcalloc(parent, max_ 465 sizeof(struc 466 if (!smcd->conn) 467 return NULL; 468 469 smcd->event_wq = alloc_ordered_workque 470 471 if (!smcd->event_wq) 472 return NULL; 473 474 smcd->ops = ops; 475 476 spin_lock_init(&smcd->lock); 477 spin_lock_init(&smcd->lgr_lock); 478 INIT_LIST_HEAD(&smcd->vlan); 479 INIT_LIST_HEAD(&smcd->lgr_list); 480 init_waitqueue_head(&smcd->lgrs_delete 481 return smcd; 482 } 483 484 static void smcd_register_dev(struct ism_dev * 485 { 486 const struct smcd_ops *ops = ism_get_s 487 struct smcd_dev *smcd, *fentry; 488 489 if (!ops) 490 return; 491 492 smcd = smcd_alloc_dev(&ism->pdev->dev, 493 ISM_NR_DMBS); 494 if (!smcd) 495 return; 496 smcd->priv = ism; 497 smcd->client = &smc_ism_client; 498 ism_set_priv(ism, &smc_ism_client, smc 499 if (smc_pnetid_by_dev_port(&ism->pdev- 500 smc_pnetid_by_table_smcd(smcd) 501 502 if (smcd->ops->supports_v2()) 503 smc_ism_set_v2_capable(); 504 mutex_lock(&smcd_dev_list.mutex); 505 /* sort list: 506 * - devices without pnetid before dev 507 * - loopback-ism always at the very b 508 */ 509 if (!smcd->pnetid[0]) { 510 fentry = list_first_entry_or_n 511 512 if (fentry && smc_ism_is_loopb 513 list_add(&smcd->list, 514 else 515 list_add(&smcd->list, 516 } else { 517 list_add_tail(&smcd->list, &sm 518 } 519 mutex_unlock(&smcd_dev_list.mutex); 520 521 pr_warn_ratelimited("smc: adding smcd 522 dev_name(&ism->dev 523 smcd->pnetid_by_us 524 525 return; 526 } 527 528 static void smcd_unregister_dev(struct ism_dev 529 { 530 struct smcd_dev *smcd = ism_get_priv(i 531 532 pr_warn_ratelimited("smc: removing smc 533 dev_name(&ism->dev 534 smcd->going_away = 1; 535 smc_smcd_terminate_all(smcd); 536 mutex_lock(&smcd_dev_list.mutex); 537 list_del_init(&smcd->list); 538 mutex_unlock(&smcd_dev_list.mutex); 539 destroy_workqueue(smcd->event_wq); 540 } 541 542 /* SMCD Device event handler. Called from ISM 543 * Parameters are ism device pointer, 544 * - event->type (0 --> DMB, 1 --> GID), 545 * - event->code (event code), 546 * - event->tok (either DMB token when event t 547 * - event->time (time of day) 548 * - event->info (debug info). 549 * 550 * Context: 551 * - Function called in IRQ context from ISM d 552 */ 553 static void smcd_handle_event(struct ism_dev * 554 { 555 struct smcd_dev *smcd = ism_get_priv(i 556 struct smc_ism_event_work *wrk; 557 558 if (smcd->going_away) 559 return; 560 /* copy event to event work queue, and 561 wrk = kmalloc(sizeof(*wrk), GFP_ATOMIC 562 if (!wrk) 563 return; 564 INIT_WORK(&wrk->work, smc_ism_event_wo 565 wrk->smcd = smcd; 566 wrk->event = *event; 567 queue_work(smcd->event_wq, &wrk->work) 568 } 569 570 /* SMCD Device interrupt handler. Called from 571 * Parameters are the ism device pointer, DMB 572 * Find the connection and schedule the taskle 573 * 574 * Context: 575 * - Function called in IRQ context from ISM d 576 */ 577 static void smcd_handle_irq(struct ism_dev *is 578 u16 dmbemask) 579 { 580 struct smcd_dev *smcd = ism_get_priv(i 581 struct smc_connection *conn = NULL; 582 unsigned long flags; 583 584 spin_lock_irqsave(&smcd->lock, flags); 585 conn = smcd->conn[dmbno]; 586 if (conn && !conn->killed) 587 tasklet_schedule(&conn->rx_tsk 588 spin_unlock_irqrestore(&smcd->lock, fl 589 } 590 #endif 591 592 int smc_ism_signal_shutdown(struct smc_link_gr 593 { 594 int rc = 0; 595 #if IS_ENABLED(CONFIG_ISM) 596 union smcd_sw_event_info ev_info; 597 598 if (lgr->peer_shutdown) 599 return 0; 600 if (!lgr->smcd->ops->signal_event) 601 return 0; 602 603 memcpy(ev_info.uid, lgr->id, SMC_LGR_I 604 ev_info.vlan_id = lgr->vlan_id; 605 ev_info.code = ISM_EVENT_REQUEST; 606 rc = lgr->smcd->ops->signal_event(lgr- 607 ISM_ 608 ISM_ 609 ev_i 610 #endif 611 return rc; 612 } 613 614 int smc_ism_init(void) 615 { 616 int rc = 0; 617 618 smc_ism_v2_capable = false; 619 smc_ism_create_system_eid(); 620 621 #if IS_ENABLED(CONFIG_ISM) 622 rc = ism_register_client(&smc_ism_clie 623 #endif 624 return rc; 625 } 626 627 void smc_ism_exit(void) 628 { 629 #if IS_ENABLED(CONFIG_ISM) 630 ism_unregister_client(&smc_ism_client) 631 #endif 632 } 633
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.