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