| 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(&ism_rgid, 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(&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(&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_obj(*new_vlan); |
| 146 |
if (!new_vlan) |
| 147 |
return -ENOMEM; |
| 148 |
new_vlan->vlanid = vlanid; |
| 149 |
refcount_set(&new_vlan->refcnt, 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(&vlan->refcnt); |
| 156 |
kfree(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(new_vlan); |
| 166 |
rc = -EIO; |
| 167 |
goto out; |
| 168 |
} |
| 169 |
list_add_tail(&new_vlan->list, &smcd->vlan); |
| 170 |
out: |
| 171 |
spin_unlock_irqrestore(&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(&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(&vlan->list); |
| 209 |
kfree(vlan); |
| 210 |
out: |
| 211 |
spin_unlock_irqrestore(&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(&dmb.rgid, &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, cb->nlh->nlmsg_seq, |
| 315 |
&smc_gen_nl_family, NLM_F_MULTI, |
| 316 |
SMC_NETLINK_GET_DEV_SMCD); |
| 317 |
if (!nlh) |
| 318 |
goto errmsg; |
| 319 |
attrs = nla_nest_start(skb, SMC_GEN_DEV_SMCD); |
| 320 |
if (!attrs) |
| 321 |
goto errout; |
| 322 |
use_cnt = atomic_read(&smcd->lgr_cnt); |
| 323 |
if (nla_put_u32(skb, SMC_NLA_DEV_USE_CNT, use_cnt)) |
| 324 |
goto errattr; |
| 325 |
if (nla_put_u8(skb, SMC_NLA_DEV_IS_CRIT, 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_pci_dev); |
| 329 |
if (nla_put_u32(skb, SMC_NLA_DEV_PCI_FID, smc_pci_dev.pci_fid)) |
| 330 |
goto errattr; |
| 331 |
if (nla_put_u16(skb, SMC_NLA_DEV_PCI_CHID, smc_pci_dev.pci_pchid)) |
| 332 |
goto errattr; |
| 333 |
if (nla_put_u16(skb, SMC_NLA_DEV_PCI_VENDOR, smc_pci_dev.pci_vendor)) |
| 334 |
goto errattr; |
| 335 |
if (nla_put_u16(skb, SMC_NLA_DEV_PCI_DEVICE, smc_pci_dev.pci_device)) |
| 336 |
goto errattr; |
| 337 |
if (nla_put_string(skb, SMC_NLA_DEV_PCI_ID, smc_pci_dev.pci_id)) |
| 338 |
goto errattr; |
| 339 |
|
| 340 |
port_attrs = nla_nest_start(skb, SMC_NLA_DEV_PORT); |
| 341 |
if (!port_attrs) |
| 342 |
goto errattr; |
| 343 |
if (nla_put_u8(skb, SMC_NLA_DEV_PORT_PNET_USR, 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, SMC_NLA_DEV_PORT_PNETID, smc_pnet)) |
| 348 |
goto errportattr; |
| 349 |
|
| 350 |
nla_nest_end(skb, port_attrs); |
| 351 |
nla_nest_end(skb, attrs); |
| 352 |
genlmsg_end(skb, nlh); |
| 353 |
return 0; |
| 354 |
|
| 355 |
errportattr: |
| 356 |
nla_nest_cancel(skb, port_attrs); |
| 357 |
errattr: |
| 358 |
nla_nest_cancel(skb, 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(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(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(&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(&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(&peer_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(wrk->smcd, &peer_gid, 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(&ism_rgid, &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(&smcd_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(wrk->smcd, &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(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_obj(*smcd); |
| 471 |
if (!smcd) |
| 472 |
return NULL; |
| 473 |
smcd->conn = kzalloc_objs(struct smc_connection *, max_dmbs); |
| 474 |
if (!smcd->conn) |
| 475 |
goto free_smcd; |
| 476 |
|
| 477 |
smcd->event_wq = alloc_ordered_workqueue("ism_evt_wq-%s)", |
| 478 |
WQ_MEM_RECLAIM, name); |
| 479 |
if (!smcd->event_wq) |
| 480 |
goto free_conn; |
| 481 |
|
| 482 |
spin_lock_init(&smcd->lock); |
| 483 |
spin_lock_init(&smcd->lgr_lock); |
| 484 |
INIT_LIST_HEAD(&smcd->vlan); |
| 485 |
INIT_LIST_HEAD(&smcd->lgr_list); |
| 486 |
init_waitqueue_head(&smcd->lgrs_deleted); |
| 487 |
return smcd; |
| 488 |
|
| 489 |
free_conn: |
| 490 |
kfree(smcd->conn); |
| 491 |
free_smcd: |
| 492 |
kfree(smcd); |
| 493 |
return NULL; |
| 494 |
} |
| 495 |
|
| 496 |
static void smcd_register_dev(struct dibs_dev *dibs) |
| 497 |
{ |
| 498 |
struct smcd_dev *smcd, *fentry; |
| 499 |
int max_dmbs; |
| 500 |
|
| 501 |
max_dmbs = dibs->ops->max_dmbs(); |
| 502 |
|
| 503 |
smcd = smcd_alloc_dev(dev_name(&dibs->dev), max_dmbs); |
| 504 |
if (!smcd) |
| 505 |
return; |
| 506 |
|
| 507 |
smcd->dibs = dibs; |
| 508 |
dibs_set_priv(dibs, &smc_dibs_client, smcd); |
| 509 |
|
| 510 |
if (smc_pnetid_by_dev_port(dibs->dev.parent, 0, smcd->pnetid)) |
| 511 |
smc_pnetid_by_table_smcd(smcd); |
| 512 |
|
| 513 |
if (smc_ism_is_loopback(dibs) || |
| 514 |
(dibs->ops->add_vlan_id && |
| 515 |
!dibs->ops->add_vlan_id(dibs, ISM_RESERVED_VLANID))) { |
| 516 |
smc_ism_set_v2_capable(); |
| 517 |
} |
| 518 |
|
| 519 |
mutex_lock(&smcd_dev_list.mutex); |
| 520 |
/* sort list: |
| 521 |
* - devices without pnetid before devices with pnetid; |
| 522 |
* - loopback-ism always at the very beginning; |
| 523 |
*/ |
| 524 |
if (!smcd->pnetid[0]) { |
| 525 |
fentry = list_first_entry_or_null(&smcd_dev_list.list, |
| 526 |
struct smcd_dev, list); |
| 527 |
if (fentry && smc_ism_is_loopback(fentry->dibs)) |
| 528 |
list_add(&smcd->list, &fentry->list); |
| 529 |
else |
| 530 |
list_add(&smcd->list, &smcd_dev_list.list); |
| 531 |
} else { |
| 532 |
list_add_tail(&smcd->list, &smcd_dev_list.list); |
| 533 |
} |
| 534 |
mutex_unlock(&smcd_dev_list.mutex); |
| 535 |
|
| 536 |
if (smc_pnet_is_pnetid_set(smcd->pnetid)) |
| 537 |
pr_warn_ratelimited("smc: adding smcd device %s with pnetid %.16s%s\n", |
| 538 |
dev_name(&dibs->dev), smcd->pnetid, |
| 539 |
smcd->pnetid_by_user ? |
| 540 |
" (user defined)" : |
| 541 |
""); |
| 542 |
else |
| 543 |
pr_warn_ratelimited("smc: adding smcd device %s without pnetid\n", |
| 544 |
dev_name(&dibs->dev)); |
| 545 |
return; |
| 546 |
} |
| 547 |
|
| 548 |
static void smcd_unregister_dev(struct dibs_dev *dibs) |
| 549 |
{ |
| 550 |
struct smcd_dev *smcd = dibs_get_priv(dibs, &smc_dibs_client); |
| 551 |
|
| 552 |
pr_warn_ratelimited("smc: removing smcd device %s\n", |
| 553 |
dev_name(&dibs->dev)); |
| 554 |
smcd->going_away = 1; |
| 555 |
smc_smcd_terminate_all(smcd); |
| 556 |
mutex_lock(&smcd_dev_list.mutex); |
| 557 |
list_del_init(&smcd->list); |
| 558 |
mutex_unlock(&smcd_dev_list.mutex); |
| 559 |
destroy_workqueue(smcd->event_wq); |
| 560 |
kfree(smcd->conn); |
| 561 |
kfree(smcd); |
| 562 |
} |
| 563 |
|
| 564 |
/* SMCD Device event handler. Called from ISM device interrupt handler. |
| 565 |
* Parameters are ism device pointer, |
| 566 |
* - event->type (0 --> DMB, 1 --> GID), |
| 567 |
* - event->code (event code), |
| 568 |
* - event->tok (either DMB token when event type 0, or GID when event type 1) |
| 569 |
* - event->time (time of day) |
| 570 |
* - event->info (debug info). |
| 571 |
* |
| 572 |
* Context: |
| 573 |
* - Function called in IRQ context from ISM device driver event handler. |
| 574 |
*/ |
| 575 |
static void smcd_handle_event(struct dibs_dev *dibs, |
| 576 |
const struct dibs_event *event) |
| 577 |
{ |
| 578 |
struct smcd_dev *smcd = dibs_get_priv(dibs, &smc_dibs_client); |
| 579 |
struct smc_ism_event_work *wrk; |
| 580 |
|
| 581 |
if (smcd->going_away) |
| 582 |
return; |
| 583 |
/* copy event to event work queue, and let it be handled there */ |
| 584 |
wrk = kmalloc_obj(*wrk, GFP_ATOMIC); |
| 585 |
if (!wrk) |
| 586 |
return; |
| 587 |
INIT_WORK(&wrk->work, smc_ism_event_work); |
| 588 |
wrk->smcd = smcd; |
| 589 |
wrk->event = *event; |
| 590 |
queue_work(smcd->event_wq, &wrk->work); |
| 591 |
} |
| 592 |
|
| 593 |
/* SMCD Device interrupt handler. Called from ISM device interrupt handler. |
| 594 |
* Parameters are the ism device pointer, DMB number, and the DMBE bitmask. |
| 595 |
* Find the connection and schedule the tasklet for this connection. |
| 596 |
* |
| 597 |
* Context: |
| 598 |
* - Function called in IRQ context from ISM device driver IRQ handler. |
| 599 |
*/ |
| 600 |
static void smcd_handle_irq(struct dibs_dev *dibs, unsigned int dmbno, |
| 601 |
u16 dmbemask) |
| 602 |
{ |
| 603 |
struct smcd_dev *smcd = dibs_get_priv(dibs, &smc_dibs_client); |
| 604 |
struct smc_connection *conn = NULL; |
| 605 |
unsigned long flags; |
| 606 |
|
| 607 |
spin_lock_irqsave(&smcd->lock, flags); |
| 608 |
conn = smcd->conn[dmbno]; |
| 609 |
if (conn && !conn->killed) |
| 610 |
tasklet_schedule(&conn->rx_tsklet); |
| 611 |
spin_unlock_irqrestore(&smcd->lock, flags); |
| 612 |
} |
| 613 |
|
| 614 |
int smc_ism_signal_shutdown(struct smc_link_group *lgr) |
| 615 |
{ |
| 616 |
int rc = 0; |
| 617 |
union smcd_sw_event_info ev_info; |
| 618 |
uuid_t ism_rgid; |
| 619 |
|
| 620 |
if (lgr->peer_shutdown) |
| 621 |
return 0; |
| 622 |
if (!lgr->smcd->dibs->ops->signal_event) |
| 623 |
return 0; |
| 624 |
|
| 625 |
memcpy(ev_info.uid, lgr->id, SMC_LGR_ID_SIZE); |
| 626 |
ev_info.vlan_id = lgr->vlan_id; |
| 627 |
ev_info.code = ISM_EVENT_REQUEST; |
| 628 |
copy_to_dibsgid(&ism_rgid, &lgr->peer_gid); |
| 629 |
rc = lgr->smcd->dibs->ops->signal_event(lgr->smcd->dibs, &ism_rgid, |
| 630 |
ISM_EVENT_REQUEST_IR, |
| 631 |
ISM_EVENT_CODE_SHUTDOWN, |
| 632 |
ev_info.info); |
| 633 |
return rc; |
| 634 |
} |
| 635 |
|
| 636 |
int smc_ism_init(void) |
| 637 |
{ |
| 638 |
int rc = 0; |
| 639 |
|
| 640 |
smc_ism_v2_capable = false; |
| 641 |
smc_ism_create_system_eid(); |
| 642 |
|
| 643 |
rc = dibs_register_client(&smc_dibs_client); |
| 644 |
return rc; |
| 645 |
} |
| 646 |
|
| 647 |
void smc_ism_exit(void) |
| 648 |
{ |
| 649 |
dibs_unregister_client(&smc_dibs_client); |
| 650 |
} |