net/smc: use separate work queues for different worker types
[linux-2.6-microblaze.git] / net / smc / smc_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  *  Basic Transport Functions exploiting Infiniband API
6  *
7  *  Copyright IBM Corp. 2016
8  *
9  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
10  */
11
12 #include <linux/socket.h>
13 #include <linux/if_vlan.h>
14 #include <linux/random.h>
15 #include <linux/workqueue.h>
16 #include <linux/wait.h>
17 #include <linux/reboot.h>
18 #include <linux/mutex.h>
19 #include <net/tcp.h>
20 #include <net/sock.h>
21 #include <rdma/ib_verbs.h>
22 #include <rdma/ib_cache.h>
23
24 #include "smc.h"
25 #include "smc_clc.h"
26 #include "smc_core.h"
27 #include "smc_ib.h"
28 #include "smc_wr.h"
29 #include "smc_llc.h"
30 #include "smc_cdc.h"
31 #include "smc_close.h"
32 #include "smc_ism.h"
33
34 #define SMC_LGR_NUM_INCR                256
35 #define SMC_LGR_FREE_DELAY_SERV         (600 * HZ)
36 #define SMC_LGR_FREE_DELAY_CLNT         (SMC_LGR_FREE_DELAY_SERV + 10 * HZ)
37
38 static struct smc_lgr_list smc_lgr_list = {     /* established link groups */
39         .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
40         .list = LIST_HEAD_INIT(smc_lgr_list.list),
41         .num = 0,
42 };
43
44 static atomic_t lgr_cnt = ATOMIC_INIT(0); /* number of existing link groups */
45 static DECLARE_WAIT_QUEUE_HEAD(lgrs_deleted);
46
47 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
48                          struct smc_buf_desc *buf_desc);
49 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft);
50
51 static void smc_link_down_work(struct work_struct *work);
52
53 /* return head of link group list and its lock for a given link group */
54 static inline struct list_head *smc_lgr_list_head(struct smc_link_group *lgr,
55                                                   spinlock_t **lgr_lock)
56 {
57         if (lgr->is_smcd) {
58                 *lgr_lock = &lgr->smcd->lgr_lock;
59                 return &lgr->smcd->lgr_list;
60         }
61
62         *lgr_lock = &smc_lgr_list.lock;
63         return &smc_lgr_list.list;
64 }
65
66 static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
67 {
68         /* client link group creation always follows the server link group
69          * creation. For client use a somewhat higher removal delay time,
70          * otherwise there is a risk of out-of-sync link groups.
71          */
72         if (!lgr->freeing) {
73                 mod_delayed_work(system_wq, &lgr->free_work,
74                                  (!lgr->is_smcd && lgr->role == SMC_CLNT) ?
75                                                 SMC_LGR_FREE_DELAY_CLNT :
76                                                 SMC_LGR_FREE_DELAY_SERV);
77         }
78 }
79
80 /* Register connection's alert token in our lookup structure.
81  * To use rbtrees we have to implement our own insert core.
82  * Requires @conns_lock
83  * @smc         connection to register
84  * Returns 0 on success, != otherwise.
85  */
86 static void smc_lgr_add_alert_token(struct smc_connection *conn)
87 {
88         struct rb_node **link, *parent = NULL;
89         u32 token = conn->alert_token_local;
90
91         link = &conn->lgr->conns_all.rb_node;
92         while (*link) {
93                 struct smc_connection *cur = rb_entry(*link,
94                                         struct smc_connection, alert_node);
95
96                 parent = *link;
97                 if (cur->alert_token_local > token)
98                         link = &parent->rb_left;
99                 else
100                         link = &parent->rb_right;
101         }
102         /* Put the new node there */
103         rb_link_node(&conn->alert_node, parent, link);
104         rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
105 }
106
107 /* assign an SMC-R link to the connection */
108 static int smcr_lgr_conn_assign_link(struct smc_connection *conn, bool first)
109 {
110         enum smc_link_state expected = first ? SMC_LNK_ACTIVATING :
111                                        SMC_LNK_ACTIVE;
112         int i, j;
113
114         /* do link balancing */
115         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
116                 struct smc_link *lnk = &conn->lgr->lnk[i];
117
118                 if (lnk->state != expected || lnk->link_is_asym)
119                         continue;
120                 if (conn->lgr->role == SMC_CLNT) {
121                         conn->lnk = lnk; /* temporary, SMC server assigns link*/
122                         break;
123                 }
124                 if (conn->lgr->conns_num % 2) {
125                         for (j = i + 1; j < SMC_LINKS_PER_LGR_MAX; j++) {
126                                 struct smc_link *lnk2;
127
128                                 lnk2 = &conn->lgr->lnk[j];
129                                 if (lnk2->state == expected &&
130                                     !lnk2->link_is_asym) {
131                                         conn->lnk = lnk2;
132                                         break;
133                                 }
134                         }
135                 }
136                 if (!conn->lnk)
137                         conn->lnk = lnk;
138                 break;
139         }
140         if (!conn->lnk)
141                 return SMC_CLC_DECL_NOACTLINK;
142         return 0;
143 }
144
145 /* Register connection in link group by assigning an alert token
146  * registered in a search tree.
147  * Requires @conns_lock
148  * Note that '0' is a reserved value and not assigned.
149  */
150 static int smc_lgr_register_conn(struct smc_connection *conn, bool first)
151 {
152         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
153         static atomic_t nexttoken = ATOMIC_INIT(0);
154         int rc;
155
156         if (!conn->lgr->is_smcd) {
157                 rc = smcr_lgr_conn_assign_link(conn, first);
158                 if (rc)
159                         return rc;
160         }
161         /* find a new alert_token_local value not yet used by some connection
162          * in this link group
163          */
164         sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
165         while (!conn->alert_token_local) {
166                 conn->alert_token_local = atomic_inc_return(&nexttoken);
167                 if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
168                         conn->alert_token_local = 0;
169         }
170         smc_lgr_add_alert_token(conn);
171         conn->lgr->conns_num++;
172         return 0;
173 }
174
175 /* Unregister connection and reset the alert token of the given connection<
176  */
177 static void __smc_lgr_unregister_conn(struct smc_connection *conn)
178 {
179         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
180         struct smc_link_group *lgr = conn->lgr;
181
182         rb_erase(&conn->alert_node, &lgr->conns_all);
183         lgr->conns_num--;
184         conn->alert_token_local = 0;
185         sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
186 }
187
188 /* Unregister connection from lgr
189  */
190 static void smc_lgr_unregister_conn(struct smc_connection *conn)
191 {
192         struct smc_link_group *lgr = conn->lgr;
193
194         if (!lgr)
195                 return;
196         write_lock_bh(&lgr->conns_lock);
197         if (conn->alert_token_local) {
198                 __smc_lgr_unregister_conn(conn);
199         }
200         write_unlock_bh(&lgr->conns_lock);
201         conn->lgr = NULL;
202 }
203
204 void smc_lgr_cleanup_early(struct smc_connection *conn)
205 {
206         struct smc_link_group *lgr = conn->lgr;
207         struct list_head *lgr_list;
208         spinlock_t *lgr_lock;
209
210         if (!lgr)
211                 return;
212
213         smc_conn_free(conn);
214         lgr_list = smc_lgr_list_head(lgr, &lgr_lock);
215         spin_lock_bh(lgr_lock);
216         /* do not use this link group for new connections */
217         if (!list_empty(lgr_list))
218                 list_del_init(lgr_list);
219         spin_unlock_bh(lgr_lock);
220         __smc_lgr_terminate(lgr, true);
221 }
222
223 static void smcr_lgr_link_deactivate_all(struct smc_link_group *lgr)
224 {
225         int i;
226
227         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
228                 struct smc_link *lnk = &lgr->lnk[i];
229
230                 if (smc_link_usable(lnk))
231                         lnk->state = SMC_LNK_INACTIVE;
232         }
233         wake_up_all(&lgr->llc_msg_waiter);
234         wake_up_all(&lgr->llc_flow_waiter);
235 }
236
237 static void smc_lgr_free(struct smc_link_group *lgr);
238
239 static void smc_lgr_free_work(struct work_struct *work)
240 {
241         struct smc_link_group *lgr = container_of(to_delayed_work(work),
242                                                   struct smc_link_group,
243                                                   free_work);
244         spinlock_t *lgr_lock;
245         bool conns;
246
247         smc_lgr_list_head(lgr, &lgr_lock);
248         spin_lock_bh(lgr_lock);
249         if (lgr->freeing) {
250                 spin_unlock_bh(lgr_lock);
251                 return;
252         }
253         read_lock_bh(&lgr->conns_lock);
254         conns = RB_EMPTY_ROOT(&lgr->conns_all);
255         read_unlock_bh(&lgr->conns_lock);
256         if (!conns) { /* number of lgr connections is no longer zero */
257                 spin_unlock_bh(lgr_lock);
258                 return;
259         }
260         list_del_init(&lgr->list); /* remove from smc_lgr_list */
261         lgr->freeing = 1; /* this instance does the freeing, no new schedule */
262         spin_unlock_bh(lgr_lock);
263         cancel_delayed_work(&lgr->free_work);
264
265         if (!lgr->is_smcd && !lgr->terminating)
266                 smc_llc_send_link_delete_all(lgr, true,
267                                              SMC_LLC_DEL_PROG_INIT_TERM);
268         if (lgr->is_smcd && !lgr->terminating)
269                 smc_ism_signal_shutdown(lgr);
270         if (!lgr->is_smcd)
271                 smcr_lgr_link_deactivate_all(lgr);
272         smc_lgr_free(lgr);
273 }
274
275 static void smc_lgr_terminate_work(struct work_struct *work)
276 {
277         struct smc_link_group *lgr = container_of(work, struct smc_link_group,
278                                                   terminate_work);
279
280         __smc_lgr_terminate(lgr, true);
281 }
282
283 /* return next unique link id for the lgr */
284 static u8 smcr_next_link_id(struct smc_link_group *lgr)
285 {
286         u8 link_id;
287         int i;
288
289         while (1) {
290                 link_id = ++lgr->next_link_id;
291                 if (!link_id)   /* skip zero as link_id */
292                         link_id = ++lgr->next_link_id;
293                 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
294                         if (smc_link_usable(&lgr->lnk[i]) &&
295                             lgr->lnk[i].link_id == link_id)
296                                 continue;
297                 }
298                 break;
299         }
300         return link_id;
301 }
302
303 int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
304                    u8 link_idx, struct smc_init_info *ini)
305 {
306         u8 rndvec[3];
307         int rc;
308
309         get_device(&ini->ib_dev->ibdev->dev);
310         atomic_inc(&ini->ib_dev->lnk_cnt);
311         lnk->link_id = smcr_next_link_id(lgr);
312         lnk->lgr = lgr;
313         lnk->link_idx = link_idx;
314         lnk->smcibdev = ini->ib_dev;
315         lnk->ibport = ini->ib_port;
316         lnk->path_mtu = ini->ib_dev->pattr[ini->ib_port - 1].active_mtu;
317         smc_llc_link_set_uid(lnk);
318         INIT_WORK(&lnk->link_down_wrk, smc_link_down_work);
319         if (!ini->ib_dev->initialized) {
320                 rc = (int)smc_ib_setup_per_ibdev(ini->ib_dev);
321                 if (rc)
322                         goto out;
323         }
324         get_random_bytes(rndvec, sizeof(rndvec));
325         lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
326                 (rndvec[2] << 16);
327         rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
328                                   ini->vlan_id, lnk->gid, &lnk->sgid_index);
329         if (rc)
330                 goto out;
331         rc = smc_llc_link_init(lnk);
332         if (rc)
333                 goto out;
334         rc = smc_wr_alloc_link_mem(lnk);
335         if (rc)
336                 goto clear_llc_lnk;
337         rc = smc_ib_create_protection_domain(lnk);
338         if (rc)
339                 goto free_link_mem;
340         rc = smc_ib_create_queue_pair(lnk);
341         if (rc)
342                 goto dealloc_pd;
343         rc = smc_wr_create_link(lnk);
344         if (rc)
345                 goto destroy_qp;
346         lnk->state = SMC_LNK_ACTIVATING;
347         return 0;
348
349 destroy_qp:
350         smc_ib_destroy_queue_pair(lnk);
351 dealloc_pd:
352         smc_ib_dealloc_protection_domain(lnk);
353 free_link_mem:
354         smc_wr_free_link_mem(lnk);
355 clear_llc_lnk:
356         smc_llc_link_clear(lnk, false);
357 out:
358         put_device(&ini->ib_dev->ibdev->dev);
359         memset(lnk, 0, sizeof(struct smc_link));
360         lnk->state = SMC_LNK_UNUSED;
361         if (!atomic_dec_return(&ini->ib_dev->lnk_cnt))
362                 wake_up(&ini->ib_dev->lnks_deleted);
363         return rc;
364 }
365
366 /* create a new SMC link group */
367 static int smc_lgr_create(struct smc_sock *smc, struct smc_init_info *ini)
368 {
369         struct smc_link_group *lgr;
370         struct list_head *lgr_list;
371         struct smc_link *lnk;
372         spinlock_t *lgr_lock;
373         u8 link_idx;
374         int rc = 0;
375         int i;
376
377         if (ini->is_smcd && ini->vlan_id) {
378                 if (smc_ism_get_vlan(ini->ism_dev, ini->vlan_id)) {
379                         rc = SMC_CLC_DECL_ISMVLANERR;
380                         goto out;
381                 }
382         }
383
384         lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
385         if (!lgr) {
386                 rc = SMC_CLC_DECL_MEM;
387                 goto ism_put_vlan;
388         }
389         lgr->tx_wq = alloc_workqueue("smc_tx_wq-%*phN", 0, 0,
390                                      SMC_LGR_ID_SIZE, &lgr->id);
391         if (!lgr->tx_wq) {
392                 rc = -ENOMEM;
393                 goto free_lgr;
394         }
395         lgr->is_smcd = ini->is_smcd;
396         lgr->sync_err = 0;
397         lgr->terminating = 0;
398         lgr->freeing = 0;
399         lgr->vlan_id = ini->vlan_id;
400         mutex_init(&lgr->sndbufs_lock);
401         mutex_init(&lgr->rmbs_lock);
402         rwlock_init(&lgr->conns_lock);
403         for (i = 0; i < SMC_RMBE_SIZES; i++) {
404                 INIT_LIST_HEAD(&lgr->sndbufs[i]);
405                 INIT_LIST_HEAD(&lgr->rmbs[i]);
406         }
407         lgr->next_link_id = 0;
408         smc_lgr_list.num += SMC_LGR_NUM_INCR;
409         memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
410         INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
411         INIT_WORK(&lgr->terminate_work, smc_lgr_terminate_work);
412         lgr->conns_all = RB_ROOT;
413         if (ini->is_smcd) {
414                 /* SMC-D specific settings */
415                 get_device(&ini->ism_dev->dev);
416                 lgr->peer_gid = ini->ism_peer_gid;
417                 lgr->smcd = ini->ism_dev;
418                 lgr_list = &ini->ism_dev->lgr_list;
419                 lgr_lock = &lgr->smcd->lgr_lock;
420                 lgr->peer_shutdown = 0;
421                 atomic_inc(&ini->ism_dev->lgr_cnt);
422         } else {
423                 /* SMC-R specific settings */
424                 lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
425                 memcpy(lgr->peer_systemid, ini->ib_lcl->id_for_peer,
426                        SMC_SYSTEMID_LEN);
427                 memcpy(lgr->pnet_id, ini->ib_dev->pnetid[ini->ib_port - 1],
428                        SMC_MAX_PNETID_LEN);
429                 smc_llc_lgr_init(lgr, smc);
430
431                 link_idx = SMC_SINGLE_LINK;
432                 lnk = &lgr->lnk[link_idx];
433                 rc = smcr_link_init(lgr, lnk, link_idx, ini);
434                 if (rc)
435                         goto free_wq;
436                 lgr_list = &smc_lgr_list.list;
437                 lgr_lock = &smc_lgr_list.lock;
438                 atomic_inc(&lgr_cnt);
439         }
440         smc->conn.lgr = lgr;
441         spin_lock_bh(lgr_lock);
442         list_add_tail(&lgr->list, lgr_list);
443         spin_unlock_bh(lgr_lock);
444         return 0;
445
446 free_wq:
447         destroy_workqueue(lgr->tx_wq);
448 free_lgr:
449         kfree(lgr);
450 ism_put_vlan:
451         if (ini->is_smcd && ini->vlan_id)
452                 smc_ism_put_vlan(ini->ism_dev, ini->vlan_id);
453 out:
454         if (rc < 0) {
455                 if (rc == -ENOMEM)
456                         rc = SMC_CLC_DECL_MEM;
457                 else
458                         rc = SMC_CLC_DECL_INTERR;
459         }
460         return rc;
461 }
462
463 static int smc_write_space(struct smc_connection *conn)
464 {
465         int buffer_len = conn->peer_rmbe_size;
466         union smc_host_cursor prod;
467         union smc_host_cursor cons;
468         int space;
469
470         smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
471         smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
472         /* determine rx_buf space */
473         space = buffer_len - smc_curs_diff(buffer_len, &cons, &prod);
474         return space;
475 }
476
477 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
478                              struct smc_wr_buf *wr_buf)
479 {
480         struct smc_connection *conn = &smc->conn;
481         union smc_host_cursor cons, fin;
482         int rc = 0;
483         int diff;
484
485         smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
486         smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
487         /* set prod cursor to old state, enforce tx_rdma_writes() */
488         smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
489         smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
490
491         if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
492                 /* cons cursor advanced more than fin, and prod was set
493                  * fin above, so now prod is smaller than cons. Fix that.
494                  */
495                 diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
496                 smc_curs_add(conn->sndbuf_desc->len,
497                              &conn->tx_curs_sent, diff);
498                 smc_curs_add(conn->sndbuf_desc->len,
499                              &conn->tx_curs_fin, diff);
500
501                 smp_mb__before_atomic();
502                 atomic_add(diff, &conn->sndbuf_space);
503                 smp_mb__after_atomic();
504
505                 smc_curs_add(conn->peer_rmbe_size,
506                              &conn->local_tx_ctrl.prod, diff);
507                 smc_curs_add(conn->peer_rmbe_size,
508                              &conn->local_tx_ctrl_fin, diff);
509         }
510         /* recalculate, value is used by tx_rdma_writes() */
511         atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));
512
513         if (smc->sk.sk_state != SMC_INIT &&
514             smc->sk.sk_state != SMC_CLOSED) {
515                 rc = smcr_cdc_msg_send_validation(conn, pend, wr_buf);
516                 if (!rc) {
517                         queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, 0);
518                         smc->sk.sk_data_ready(&smc->sk);
519                 }
520         } else {
521                 smc_wr_tx_put_slot(conn->lnk,
522                                    (struct smc_wr_tx_pend_priv *)pend);
523         }
524         return rc;
525 }
526
527 struct smc_link *smc_switch_conns(struct smc_link_group *lgr,
528                                   struct smc_link *from_lnk, bool is_dev_err)
529 {
530         struct smc_link *to_lnk = NULL;
531         struct smc_cdc_tx_pend *pend;
532         struct smc_connection *conn;
533         struct smc_wr_buf *wr_buf;
534         struct smc_sock *smc;
535         struct rb_node *node;
536         int i, rc = 0;
537
538         /* link is inactive, wake up tx waiters */
539         smc_wr_wakeup_tx_wait(from_lnk);
540
541         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
542                 if (!smc_link_active(&lgr->lnk[i]) || i == from_lnk->link_idx)
543                         continue;
544                 if (is_dev_err && from_lnk->smcibdev == lgr->lnk[i].smcibdev &&
545                     from_lnk->ibport == lgr->lnk[i].ibport) {
546                         continue;
547                 }
548                 to_lnk = &lgr->lnk[i];
549                 break;
550         }
551         if (!to_lnk) {
552                 smc_lgr_terminate_sched(lgr);
553                 return NULL;
554         }
555 again:
556         read_lock_bh(&lgr->conns_lock);
557         for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
558                 conn = rb_entry(node, struct smc_connection, alert_node);
559                 if (conn->lnk != from_lnk)
560                         continue;
561                 smc = container_of(conn, struct smc_sock, conn);
562                 /* conn->lnk not yet set in SMC_INIT state */
563                 if (smc->sk.sk_state == SMC_INIT)
564                         continue;
565                 if (smc->sk.sk_state == SMC_CLOSED ||
566                     smc->sk.sk_state == SMC_PEERCLOSEWAIT1 ||
567                     smc->sk.sk_state == SMC_PEERCLOSEWAIT2 ||
568                     smc->sk.sk_state == SMC_APPFINCLOSEWAIT ||
569                     smc->sk.sk_state == SMC_APPCLOSEWAIT1 ||
570                     smc->sk.sk_state == SMC_APPCLOSEWAIT2 ||
571                     smc->sk.sk_state == SMC_PEERFINCLOSEWAIT ||
572                     smc->sk.sk_state == SMC_PEERABORTWAIT ||
573                     smc->sk.sk_state == SMC_PROCESSABORT) {
574                         spin_lock_bh(&conn->send_lock);
575                         conn->lnk = to_lnk;
576                         spin_unlock_bh(&conn->send_lock);
577                         continue;
578                 }
579                 sock_hold(&smc->sk);
580                 read_unlock_bh(&lgr->conns_lock);
581                 /* pre-fetch buffer outside of send_lock, might sleep */
582                 rc = smc_cdc_get_free_slot(conn, to_lnk, &wr_buf, NULL, &pend);
583                 if (rc) {
584                         smcr_link_down_cond_sched(to_lnk);
585                         return NULL;
586                 }
587                 /* avoid race with smcr_tx_sndbuf_nonempty() */
588                 spin_lock_bh(&conn->send_lock);
589                 conn->lnk = to_lnk;
590                 rc = smc_switch_cursor(smc, pend, wr_buf);
591                 spin_unlock_bh(&conn->send_lock);
592                 sock_put(&smc->sk);
593                 if (rc) {
594                         smcr_link_down_cond_sched(to_lnk);
595                         return NULL;
596                 }
597                 goto again;
598         }
599         read_unlock_bh(&lgr->conns_lock);
600         return to_lnk;
601 }
602
603 static void smcr_buf_unuse(struct smc_buf_desc *rmb_desc,
604                            struct smc_link_group *lgr)
605 {
606         int rc;
607
608         if (rmb_desc->is_conf_rkey && !list_empty(&lgr->list)) {
609                 /* unregister rmb with peer */
610                 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
611                 if (!rc) {
612                         /* protect against smc_llc_cli_rkey_exchange() */
613                         mutex_lock(&lgr->llc_conf_mutex);
614                         smc_llc_do_delete_rkey(lgr, rmb_desc);
615                         rmb_desc->is_conf_rkey = false;
616                         mutex_unlock(&lgr->llc_conf_mutex);
617                         smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
618                 }
619         }
620
621         if (rmb_desc->is_reg_err) {
622                 /* buf registration failed, reuse not possible */
623                 mutex_lock(&lgr->rmbs_lock);
624                 list_del(&rmb_desc->list);
625                 mutex_unlock(&lgr->rmbs_lock);
626
627                 smc_buf_free(lgr, true, rmb_desc);
628         } else {
629                 rmb_desc->used = 0;
630         }
631 }
632
633 static void smc_buf_unuse(struct smc_connection *conn,
634                           struct smc_link_group *lgr)
635 {
636         if (conn->sndbuf_desc)
637                 conn->sndbuf_desc->used = 0;
638         if (conn->rmb_desc && lgr->is_smcd)
639                 conn->rmb_desc->used = 0;
640         else if (conn->rmb_desc)
641                 smcr_buf_unuse(conn->rmb_desc, lgr);
642 }
643
644 /* remove a finished connection from its link group */
645 void smc_conn_free(struct smc_connection *conn)
646 {
647         struct smc_link_group *lgr = conn->lgr;
648
649         if (!lgr)
650                 return;
651         if (lgr->is_smcd) {
652                 if (!list_empty(&lgr->list))
653                         smc_ism_unset_conn(conn);
654                 tasklet_kill(&conn->rx_tsklet);
655         } else {
656                 smc_cdc_tx_dismiss_slots(conn);
657                 if (current_work() != &conn->abort_work)
658                         cancel_work_sync(&conn->abort_work);
659         }
660         if (!list_empty(&lgr->list)) {
661                 smc_lgr_unregister_conn(conn);
662                 smc_buf_unuse(conn, lgr); /* allow buffer reuse */
663         }
664
665         if (!lgr->conns_num)
666                 smc_lgr_schedule_free_work(lgr);
667 }
668
669 /* unregister a link from a buf_desc */
670 static void smcr_buf_unmap_link(struct smc_buf_desc *buf_desc, bool is_rmb,
671                                 struct smc_link *lnk)
672 {
673         if (is_rmb)
674                 buf_desc->is_reg_mr[lnk->link_idx] = false;
675         if (!buf_desc->is_map_ib[lnk->link_idx])
676                 return;
677         if (is_rmb) {
678                 if (buf_desc->mr_rx[lnk->link_idx]) {
679                         smc_ib_put_memory_region(
680                                         buf_desc->mr_rx[lnk->link_idx]);
681                         buf_desc->mr_rx[lnk->link_idx] = NULL;
682                 }
683                 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_FROM_DEVICE);
684         } else {
685                 smc_ib_buf_unmap_sg(lnk, buf_desc, DMA_TO_DEVICE);
686         }
687         sg_free_table(&buf_desc->sgt[lnk->link_idx]);
688         buf_desc->is_map_ib[lnk->link_idx] = false;
689 }
690
691 /* unmap all buffers of lgr for a deleted link */
692 static void smcr_buf_unmap_lgr(struct smc_link *lnk)
693 {
694         struct smc_link_group *lgr = lnk->lgr;
695         struct smc_buf_desc *buf_desc, *bf;
696         int i;
697
698         for (i = 0; i < SMC_RMBE_SIZES; i++) {
699                 mutex_lock(&lgr->rmbs_lock);
700                 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list)
701                         smcr_buf_unmap_link(buf_desc, true, lnk);
702                 mutex_unlock(&lgr->rmbs_lock);
703                 mutex_lock(&lgr->sndbufs_lock);
704                 list_for_each_entry_safe(buf_desc, bf, &lgr->sndbufs[i],
705                                          list)
706                         smcr_buf_unmap_link(buf_desc, false, lnk);
707                 mutex_unlock(&lgr->sndbufs_lock);
708         }
709 }
710
711 static void smcr_rtoken_clear_link(struct smc_link *lnk)
712 {
713         struct smc_link_group *lgr = lnk->lgr;
714         int i;
715
716         for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
717                 lgr->rtokens[i][lnk->link_idx].rkey = 0;
718                 lgr->rtokens[i][lnk->link_idx].dma_addr = 0;
719         }
720 }
721
722 /* must be called under lgr->llc_conf_mutex lock */
723 void smcr_link_clear(struct smc_link *lnk, bool log)
724 {
725         struct smc_ib_device *smcibdev;
726
727         if (!lnk->lgr || lnk->state == SMC_LNK_UNUSED)
728                 return;
729         lnk->peer_qpn = 0;
730         smc_llc_link_clear(lnk, log);
731         smcr_buf_unmap_lgr(lnk);
732         smcr_rtoken_clear_link(lnk);
733         smc_ib_modify_qp_reset(lnk);
734         smc_wr_free_link(lnk);
735         smc_ib_destroy_queue_pair(lnk);
736         smc_ib_dealloc_protection_domain(lnk);
737         smc_wr_free_link_mem(lnk);
738         put_device(&lnk->smcibdev->ibdev->dev);
739         smcibdev = lnk->smcibdev;
740         memset(lnk, 0, sizeof(struct smc_link));
741         lnk->state = SMC_LNK_UNUSED;
742         if (!atomic_dec_return(&smcibdev->lnk_cnt))
743                 wake_up(&smcibdev->lnks_deleted);
744 }
745
746 static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
747                           struct smc_buf_desc *buf_desc)
748 {
749         int i;
750
751         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
752                 smcr_buf_unmap_link(buf_desc, is_rmb, &lgr->lnk[i]);
753
754         if (buf_desc->pages)
755                 __free_pages(buf_desc->pages, buf_desc->order);
756         kfree(buf_desc);
757 }
758
759 static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
760                           struct smc_buf_desc *buf_desc)
761 {
762         if (is_dmb) {
763                 /* restore original buf len */
764                 buf_desc->len += sizeof(struct smcd_cdc_msg);
765                 smc_ism_unregister_dmb(lgr->smcd, buf_desc);
766         } else {
767                 kfree(buf_desc->cpu_addr);
768         }
769         kfree(buf_desc);
770 }
771
772 static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
773                          struct smc_buf_desc *buf_desc)
774 {
775         if (lgr->is_smcd)
776                 smcd_buf_free(lgr, is_rmb, buf_desc);
777         else
778                 smcr_buf_free(lgr, is_rmb, buf_desc);
779 }
780
781 static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
782 {
783         struct smc_buf_desc *buf_desc, *bf_desc;
784         struct list_head *buf_list;
785         int i;
786
787         for (i = 0; i < SMC_RMBE_SIZES; i++) {
788                 if (is_rmb)
789                         buf_list = &lgr->rmbs[i];
790                 else
791                         buf_list = &lgr->sndbufs[i];
792                 list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
793                                          list) {
794                         list_del(&buf_desc->list);
795                         smc_buf_free(lgr, is_rmb, buf_desc);
796                 }
797         }
798 }
799
800 static void smc_lgr_free_bufs(struct smc_link_group *lgr)
801 {
802         /* free send buffers */
803         __smc_lgr_free_bufs(lgr, false);
804         /* free rmbs */
805         __smc_lgr_free_bufs(lgr, true);
806 }
807
808 /* remove a link group */
809 static void smc_lgr_free(struct smc_link_group *lgr)
810 {
811         int i;
812
813         if (!lgr->is_smcd) {
814                 mutex_lock(&lgr->llc_conf_mutex);
815                 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
816                         if (lgr->lnk[i].state != SMC_LNK_UNUSED)
817                                 smcr_link_clear(&lgr->lnk[i], false);
818                 }
819                 mutex_unlock(&lgr->llc_conf_mutex);
820                 smc_llc_lgr_clear(lgr);
821         }
822
823         smc_lgr_free_bufs(lgr);
824         destroy_workqueue(lgr->tx_wq);
825         if (lgr->is_smcd) {
826                 smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
827                 put_device(&lgr->smcd->dev);
828                 if (!atomic_dec_return(&lgr->smcd->lgr_cnt))
829                         wake_up(&lgr->smcd->lgrs_deleted);
830         } else {
831                 if (!atomic_dec_return(&lgr_cnt))
832                         wake_up(&lgrs_deleted);
833         }
834         kfree(lgr);
835 }
836
837 static void smcd_unregister_all_dmbs(struct smc_link_group *lgr)
838 {
839         int i;
840
841         for (i = 0; i < SMC_RMBE_SIZES; i++) {
842                 struct smc_buf_desc *buf_desc;
843
844                 list_for_each_entry(buf_desc, &lgr->rmbs[i], list) {
845                         buf_desc->len += sizeof(struct smcd_cdc_msg);
846                         smc_ism_unregister_dmb(lgr->smcd, buf_desc);
847                 }
848         }
849 }
850
851 static void smc_sk_wake_ups(struct smc_sock *smc)
852 {
853         smc->sk.sk_write_space(&smc->sk);
854         smc->sk.sk_data_ready(&smc->sk);
855         smc->sk.sk_state_change(&smc->sk);
856 }
857
858 /* kill a connection */
859 static void smc_conn_kill(struct smc_connection *conn, bool soft)
860 {
861         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
862
863         if (conn->lgr->is_smcd && conn->lgr->peer_shutdown)
864                 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
865         else
866                 smc_close_abort(conn);
867         conn->killed = 1;
868         smc->sk.sk_err = ECONNABORTED;
869         smc_sk_wake_ups(smc);
870         if (conn->lgr->is_smcd) {
871                 smc_ism_unset_conn(conn);
872                 if (soft)
873                         tasklet_kill(&conn->rx_tsklet);
874                 else
875                         tasklet_unlock_wait(&conn->rx_tsklet);
876         } else {
877                 smc_cdc_tx_dismiss_slots(conn);
878         }
879         smc_lgr_unregister_conn(conn);
880         smc_close_active_abort(smc);
881 }
882
883 static void smc_lgr_cleanup(struct smc_link_group *lgr)
884 {
885         if (lgr->is_smcd) {
886                 smc_ism_signal_shutdown(lgr);
887                 smcd_unregister_all_dmbs(lgr);
888         } else {
889                 u32 rsn = lgr->llc_termination_rsn;
890
891                 if (!rsn)
892                         rsn = SMC_LLC_DEL_PROG_INIT_TERM;
893                 smc_llc_send_link_delete_all(lgr, false, rsn);
894                 smcr_lgr_link_deactivate_all(lgr);
895         }
896 }
897
898 /* terminate link group
899  * @soft: true if link group shutdown can take its time
900  *        false if immediate link group shutdown is required
901  */
902 static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
903 {
904         struct smc_connection *conn;
905         struct smc_sock *smc;
906         struct rb_node *node;
907
908         if (lgr->terminating)
909                 return; /* lgr already terminating */
910         /* cancel free_work sync, will terminate when lgr->freeing is set */
911         cancel_delayed_work_sync(&lgr->free_work);
912         lgr->terminating = 1;
913
914         /* kill remaining link group connections */
915         read_lock_bh(&lgr->conns_lock);
916         node = rb_first(&lgr->conns_all);
917         while (node) {
918                 read_unlock_bh(&lgr->conns_lock);
919                 conn = rb_entry(node, struct smc_connection, alert_node);
920                 smc = container_of(conn, struct smc_sock, conn);
921                 sock_hold(&smc->sk); /* sock_put below */
922                 lock_sock(&smc->sk);
923                 smc_conn_kill(conn, soft);
924                 release_sock(&smc->sk);
925                 sock_put(&smc->sk); /* sock_hold above */
926                 read_lock_bh(&lgr->conns_lock);
927                 node = rb_first(&lgr->conns_all);
928         }
929         read_unlock_bh(&lgr->conns_lock);
930         smc_lgr_cleanup(lgr);
931         smc_lgr_free(lgr);
932 }
933
934 /* unlink link group and schedule termination */
935 void smc_lgr_terminate_sched(struct smc_link_group *lgr)
936 {
937         spinlock_t *lgr_lock;
938
939         smc_lgr_list_head(lgr, &lgr_lock);
940         spin_lock_bh(lgr_lock);
941         if (list_empty(&lgr->list) || lgr->terminating || lgr->freeing) {
942                 spin_unlock_bh(lgr_lock);
943                 return; /* lgr already terminating */
944         }
945         list_del_init(&lgr->list);
946         lgr->freeing = 1;
947         spin_unlock_bh(lgr_lock);
948         schedule_work(&lgr->terminate_work);
949 }
950
951 /* Called when peer lgr shutdown (regularly or abnormally) is received */
952 void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid, unsigned short vlan)
953 {
954         struct smc_link_group *lgr, *l;
955         LIST_HEAD(lgr_free_list);
956
957         /* run common cleanup function and build free list */
958         spin_lock_bh(&dev->lgr_lock);
959         list_for_each_entry_safe(lgr, l, &dev->lgr_list, list) {
960                 if ((!peer_gid || lgr->peer_gid == peer_gid) &&
961                     (vlan == VLAN_VID_MASK || lgr->vlan_id == vlan)) {
962                         if (peer_gid) /* peer triggered termination */
963                                 lgr->peer_shutdown = 1;
964                         list_move(&lgr->list, &lgr_free_list);
965                         lgr->freeing = 1;
966                 }
967         }
968         spin_unlock_bh(&dev->lgr_lock);
969
970         /* cancel the regular free workers and actually free lgrs */
971         list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
972                 list_del_init(&lgr->list);
973                 schedule_work(&lgr->terminate_work);
974         }
975 }
976
977 /* Called when an SMCD device is removed or the smc module is unloaded */
978 void smc_smcd_terminate_all(struct smcd_dev *smcd)
979 {
980         struct smc_link_group *lgr, *lg;
981         LIST_HEAD(lgr_free_list);
982
983         spin_lock_bh(&smcd->lgr_lock);
984         list_splice_init(&smcd->lgr_list, &lgr_free_list);
985         list_for_each_entry(lgr, &lgr_free_list, list)
986                 lgr->freeing = 1;
987         spin_unlock_bh(&smcd->lgr_lock);
988
989         list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
990                 list_del_init(&lgr->list);
991                 __smc_lgr_terminate(lgr, false);
992         }
993
994         if (atomic_read(&smcd->lgr_cnt))
995                 wait_event(smcd->lgrs_deleted, !atomic_read(&smcd->lgr_cnt));
996 }
997
998 /* Called when an SMCR device is removed or the smc module is unloaded.
999  * If smcibdev is given, all SMCR link groups using this device are terminated.
1000  * If smcibdev is NULL, all SMCR link groups are terminated.
1001  */
1002 void smc_smcr_terminate_all(struct smc_ib_device *smcibdev)
1003 {
1004         struct smc_link_group *lgr, *lg;
1005         LIST_HEAD(lgr_free_list);
1006         int i;
1007
1008         spin_lock_bh(&smc_lgr_list.lock);
1009         if (!smcibdev) {
1010                 list_splice_init(&smc_lgr_list.list, &lgr_free_list);
1011                 list_for_each_entry(lgr, &lgr_free_list, list)
1012                         lgr->freeing = 1;
1013         } else {
1014                 list_for_each_entry_safe(lgr, lg, &smc_lgr_list.list, list) {
1015                         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1016                                 if (lgr->lnk[i].smcibdev == smcibdev)
1017                                         smcr_link_down_cond_sched(&lgr->lnk[i]);
1018                         }
1019                 }
1020         }
1021         spin_unlock_bh(&smc_lgr_list.lock);
1022
1023         list_for_each_entry_safe(lgr, lg, &lgr_free_list, list) {
1024                 list_del_init(&lgr->list);
1025                 smc_llc_set_termination_rsn(lgr, SMC_LLC_DEL_OP_INIT_TERM);
1026                 __smc_lgr_terminate(lgr, false);
1027         }
1028
1029         if (smcibdev) {
1030                 if (atomic_read(&smcibdev->lnk_cnt))
1031                         wait_event(smcibdev->lnks_deleted,
1032                                    !atomic_read(&smcibdev->lnk_cnt));
1033         } else {
1034                 if (atomic_read(&lgr_cnt))
1035                         wait_event(lgrs_deleted, !atomic_read(&lgr_cnt));
1036         }
1037 }
1038
1039 /* set new lgr type and clear all asymmetric link tagging */
1040 void smcr_lgr_set_type(struct smc_link_group *lgr, enum smc_lgr_type new_type)
1041 {
1042         char *lgr_type = "";
1043         int i;
1044
1045         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
1046                 if (smc_link_usable(&lgr->lnk[i]))
1047                         lgr->lnk[i].link_is_asym = false;
1048         if (lgr->type == new_type)
1049                 return;
1050         lgr->type = new_type;
1051
1052         switch (lgr->type) {
1053         case SMC_LGR_NONE:
1054                 lgr_type = "NONE";
1055                 break;
1056         case SMC_LGR_SINGLE:
1057                 lgr_type = "SINGLE";
1058                 break;
1059         case SMC_LGR_SYMMETRIC:
1060                 lgr_type = "SYMMETRIC";
1061                 break;
1062         case SMC_LGR_ASYMMETRIC_PEER:
1063                 lgr_type = "ASYMMETRIC_PEER";
1064                 break;
1065         case SMC_LGR_ASYMMETRIC_LOCAL:
1066                 lgr_type = "ASYMMETRIC_LOCAL";
1067                 break;
1068         }
1069         pr_warn_ratelimited("smc: SMC-R lg %*phN state changed: "
1070                             "%s, pnetid %.16s\n", SMC_LGR_ID_SIZE, &lgr->id,
1071                             lgr_type, lgr->pnet_id);
1072 }
1073
1074 /* set new lgr type and tag a link as asymmetric */
1075 void smcr_lgr_set_type_asym(struct smc_link_group *lgr,
1076                             enum smc_lgr_type new_type, int asym_lnk_idx)
1077 {
1078         smcr_lgr_set_type(lgr, new_type);
1079         lgr->lnk[asym_lnk_idx].link_is_asym = true;
1080 }
1081
1082 /* abort connection, abort_work scheduled from tasklet context */
1083 static void smc_conn_abort_work(struct work_struct *work)
1084 {
1085         struct smc_connection *conn = container_of(work,
1086                                                    struct smc_connection,
1087                                                    abort_work);
1088         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
1089
1090         smc_conn_kill(conn, true);
1091         sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
1092 }
1093
1094 void smcr_port_add(struct smc_ib_device *smcibdev, u8 ibport)
1095 {
1096         struct smc_link_group *lgr, *n;
1097
1098         list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1099                 struct smc_link *link;
1100
1101                 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1102                             SMC_MAX_PNETID_LEN) ||
1103                     lgr->type == SMC_LGR_SYMMETRIC ||
1104                     lgr->type == SMC_LGR_ASYMMETRIC_PEER)
1105                         continue;
1106
1107                 /* trigger local add link processing */
1108                 link = smc_llc_usable_link(lgr);
1109                 if (link)
1110                         smc_llc_add_link_local(link);
1111         }
1112 }
1113
1114 /* link is down - switch connections to alternate link,
1115  * must be called under lgr->llc_conf_mutex lock
1116  */
1117 static void smcr_link_down(struct smc_link *lnk)
1118 {
1119         struct smc_link_group *lgr = lnk->lgr;
1120         struct smc_link *to_lnk;
1121         int del_link_id;
1122
1123         if (!lgr || lnk->state == SMC_LNK_UNUSED || list_empty(&lgr->list))
1124                 return;
1125
1126         smc_ib_modify_qp_reset(lnk);
1127         to_lnk = smc_switch_conns(lgr, lnk, true);
1128         if (!to_lnk) { /* no backup link available */
1129                 smcr_link_clear(lnk, true);
1130                 return;
1131         }
1132         smcr_lgr_set_type(lgr, SMC_LGR_SINGLE);
1133         del_link_id = lnk->link_id;
1134
1135         if (lgr->role == SMC_SERV) {
1136                 /* trigger local delete link processing */
1137                 smc_llc_srv_delete_link_local(to_lnk, del_link_id);
1138         } else {
1139                 if (lgr->llc_flow_lcl.type != SMC_LLC_FLOW_NONE) {
1140                         /* another llc task is ongoing */
1141                         mutex_unlock(&lgr->llc_conf_mutex);
1142                         wait_event_timeout(lgr->llc_flow_waiter,
1143                                 (list_empty(&lgr->list) ||
1144                                  lgr->llc_flow_lcl.type == SMC_LLC_FLOW_NONE),
1145                                 SMC_LLC_WAIT_TIME);
1146                         mutex_lock(&lgr->llc_conf_mutex);
1147                 }
1148                 if (!list_empty(&lgr->list)) {
1149                         smc_llc_send_delete_link(to_lnk, del_link_id,
1150                                                  SMC_LLC_REQ, true,
1151                                                  SMC_LLC_DEL_LOST_PATH);
1152                         smcr_link_clear(lnk, true);
1153                 }
1154                 wake_up(&lgr->llc_flow_waiter); /* wake up next waiter */
1155         }
1156 }
1157
1158 /* must be called under lgr->llc_conf_mutex lock */
1159 void smcr_link_down_cond(struct smc_link *lnk)
1160 {
1161         if (smc_link_downing(&lnk->state))
1162                 smcr_link_down(lnk);
1163 }
1164
1165 /* will get the lgr->llc_conf_mutex lock */
1166 void smcr_link_down_cond_sched(struct smc_link *lnk)
1167 {
1168         if (smc_link_downing(&lnk->state))
1169                 schedule_work(&lnk->link_down_wrk);
1170 }
1171
1172 void smcr_port_err(struct smc_ib_device *smcibdev, u8 ibport)
1173 {
1174         struct smc_link_group *lgr, *n;
1175         int i;
1176
1177         list_for_each_entry_safe(lgr, n, &smc_lgr_list.list, list) {
1178                 if (strncmp(smcibdev->pnetid[ibport - 1], lgr->pnet_id,
1179                             SMC_MAX_PNETID_LEN))
1180                         continue; /* lgr is not affected */
1181                 if (list_empty(&lgr->list))
1182                         continue;
1183                 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1184                         struct smc_link *lnk = &lgr->lnk[i];
1185
1186                         if (smc_link_usable(lnk) &&
1187                             lnk->smcibdev == smcibdev && lnk->ibport == ibport)
1188                                 smcr_link_down_cond_sched(lnk);
1189                 }
1190         }
1191 }
1192
1193 static void smc_link_down_work(struct work_struct *work)
1194 {
1195         struct smc_link *link = container_of(work, struct smc_link,
1196                                              link_down_wrk);
1197         struct smc_link_group *lgr = link->lgr;
1198
1199         if (list_empty(&lgr->list))
1200                 return;
1201         wake_up_all(&lgr->llc_msg_waiter);
1202         mutex_lock(&lgr->llc_conf_mutex);
1203         smcr_link_down(link);
1204         mutex_unlock(&lgr->llc_conf_mutex);
1205 }
1206
1207 /* Determine vlan of internal TCP socket.
1208  * @vlan_id: address to store the determined vlan id into
1209  */
1210 int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini)
1211 {
1212         struct dst_entry *dst = sk_dst_get(clcsock->sk);
1213         struct net_device *ndev;
1214         int i, nest_lvl, rc = 0;
1215
1216         ini->vlan_id = 0;
1217         if (!dst) {
1218                 rc = -ENOTCONN;
1219                 goto out;
1220         }
1221         if (!dst->dev) {
1222                 rc = -ENODEV;
1223                 goto out_rel;
1224         }
1225
1226         ndev = dst->dev;
1227         if (is_vlan_dev(ndev)) {
1228                 ini->vlan_id = vlan_dev_vlan_id(ndev);
1229                 goto out_rel;
1230         }
1231
1232         rtnl_lock();
1233         nest_lvl = ndev->lower_level;
1234         for (i = 0; i < nest_lvl; i++) {
1235                 struct list_head *lower = &ndev->adj_list.lower;
1236
1237                 if (list_empty(lower))
1238                         break;
1239                 lower = lower->next;
1240                 ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
1241                 if (is_vlan_dev(ndev)) {
1242                         ini->vlan_id = vlan_dev_vlan_id(ndev);
1243                         break;
1244                 }
1245         }
1246         rtnl_unlock();
1247
1248 out_rel:
1249         dst_release(dst);
1250 out:
1251         return rc;
1252 }
1253
1254 static bool smcr_lgr_match(struct smc_link_group *lgr,
1255                            struct smc_clc_msg_local *lcl,
1256                            enum smc_lgr_role role, u32 clcqpn)
1257 {
1258         int i;
1259
1260         if (memcmp(lgr->peer_systemid, lcl->id_for_peer, SMC_SYSTEMID_LEN) ||
1261             lgr->role != role)
1262                 return false;
1263
1264         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1265                 if (!smc_link_active(&lgr->lnk[i]))
1266                         continue;
1267                 if ((lgr->role == SMC_SERV || lgr->lnk[i].peer_qpn == clcqpn) &&
1268                     !memcmp(lgr->lnk[i].peer_gid, &lcl->gid, SMC_GID_SIZE) &&
1269                     !memcmp(lgr->lnk[i].peer_mac, lcl->mac, sizeof(lcl->mac)))
1270                         return true;
1271         }
1272         return false;
1273 }
1274
1275 static bool smcd_lgr_match(struct smc_link_group *lgr,
1276                            struct smcd_dev *smcismdev, u64 peer_gid)
1277 {
1278         return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
1279 }
1280
1281 /* create a new SMC connection (and a new link group if necessary) */
1282 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
1283 {
1284         struct smc_connection *conn = &smc->conn;
1285         struct list_head *lgr_list;
1286         struct smc_link_group *lgr;
1287         enum smc_lgr_role role;
1288         spinlock_t *lgr_lock;
1289         int rc = 0;
1290
1291         lgr_list = ini->is_smcd ? &ini->ism_dev->lgr_list : &smc_lgr_list.list;
1292         lgr_lock = ini->is_smcd ? &ini->ism_dev->lgr_lock : &smc_lgr_list.lock;
1293         ini->first_contact_local = 1;
1294         role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
1295         if (role == SMC_CLNT && ini->first_contact_peer)
1296                 /* create new link group as well */
1297                 goto create;
1298
1299         /* determine if an existing link group can be reused */
1300         spin_lock_bh(lgr_lock);
1301         list_for_each_entry(lgr, lgr_list, list) {
1302                 write_lock_bh(&lgr->conns_lock);
1303                 if ((ini->is_smcd ?
1304                      smcd_lgr_match(lgr, ini->ism_dev, ini->ism_peer_gid) :
1305                      smcr_lgr_match(lgr, ini->ib_lcl, role, ini->ib_clcqpn)) &&
1306                     !lgr->sync_err &&
1307                     lgr->vlan_id == ini->vlan_id &&
1308                     (role == SMC_CLNT || ini->is_smcd ||
1309                      lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
1310                         /* link group found */
1311                         ini->first_contact_local = 0;
1312                         conn->lgr = lgr;
1313                         rc = smc_lgr_register_conn(conn, false);
1314                         write_unlock_bh(&lgr->conns_lock);
1315                         if (!rc && delayed_work_pending(&lgr->free_work))
1316                                 cancel_delayed_work(&lgr->free_work);
1317                         break;
1318                 }
1319                 write_unlock_bh(&lgr->conns_lock);
1320         }
1321         spin_unlock_bh(lgr_lock);
1322         if (rc)
1323                 return rc;
1324
1325         if (role == SMC_CLNT && !ini->first_contact_peer &&
1326             ini->first_contact_local) {
1327                 /* Server reuses a link group, but Client wants to start
1328                  * a new one
1329                  * send out_of_sync decline, reason synchr. error
1330                  */
1331                 return SMC_CLC_DECL_SYNCERR;
1332         }
1333
1334 create:
1335         if (ini->first_contact_local) {
1336                 rc = smc_lgr_create(smc, ini);
1337                 if (rc)
1338                         goto out;
1339                 lgr = conn->lgr;
1340                 write_lock_bh(&lgr->conns_lock);
1341                 rc = smc_lgr_register_conn(conn, true);
1342                 write_unlock_bh(&lgr->conns_lock);
1343                 if (rc)
1344                         goto out;
1345         }
1346         conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
1347         conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
1348         conn->urg_state = SMC_URG_READ;
1349         INIT_WORK(&smc->conn.abort_work, smc_conn_abort_work);
1350         if (ini->is_smcd) {
1351                 conn->rx_off = sizeof(struct smcd_cdc_msg);
1352                 smcd_cdc_rx_init(conn); /* init tasklet for this conn */
1353         } else {
1354                 conn->rx_off = 0;
1355         }
1356 #ifndef KERNEL_HAS_ATOMIC64
1357         spin_lock_init(&conn->acurs_lock);
1358 #endif
1359
1360 out:
1361         return rc;
1362 }
1363
1364 /* convert the RMB size into the compressed notation - minimum 16K.
1365  * In contrast to plain ilog2, this rounds towards the next power of 2,
1366  * so the socket application gets at least its desired sndbuf / rcvbuf size.
1367  */
1368 static u8 smc_compress_bufsize(int size)
1369 {
1370         u8 compressed;
1371
1372         if (size <= SMC_BUF_MIN_SIZE)
1373                 return 0;
1374
1375         size = (size - 1) >> 14;
1376         compressed = ilog2(size) + 1;
1377         if (compressed >= SMC_RMBE_SIZES)
1378                 compressed = SMC_RMBE_SIZES - 1;
1379         return compressed;
1380 }
1381
1382 /* convert the RMB size from compressed notation into integer */
1383 int smc_uncompress_bufsize(u8 compressed)
1384 {
1385         u32 size;
1386
1387         size = 0x00000001 << (((int)compressed) + 14);
1388         return (int)size;
1389 }
1390
1391 /* try to reuse a sndbuf or rmb description slot for a certain
1392  * buffer size; if not available, return NULL
1393  */
1394 static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
1395                                              struct mutex *lock,
1396                                              struct list_head *buf_list)
1397 {
1398         struct smc_buf_desc *buf_slot;
1399
1400         mutex_lock(lock);
1401         list_for_each_entry(buf_slot, buf_list, list) {
1402                 if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
1403                         mutex_unlock(lock);
1404                         return buf_slot;
1405                 }
1406         }
1407         mutex_unlock(lock);
1408         return NULL;
1409 }
1410
1411 /* one of the conditions for announcing a receiver's current window size is
1412  * that it "results in a minimum increase in the window size of 10% of the
1413  * receive buffer space" [RFC7609]
1414  */
1415 static inline int smc_rmb_wnd_update_limit(int rmbe_size)
1416 {
1417         return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
1418 }
1419
1420 /* map an rmb buf to a link */
1421 static int smcr_buf_map_link(struct smc_buf_desc *buf_desc, bool is_rmb,
1422                              struct smc_link *lnk)
1423 {
1424         int rc;
1425
1426         if (buf_desc->is_map_ib[lnk->link_idx])
1427                 return 0;
1428
1429         rc = sg_alloc_table(&buf_desc->sgt[lnk->link_idx], 1, GFP_KERNEL);
1430         if (rc)
1431                 return rc;
1432         sg_set_buf(buf_desc->sgt[lnk->link_idx].sgl,
1433                    buf_desc->cpu_addr, buf_desc->len);
1434
1435         /* map sg table to DMA address */
1436         rc = smc_ib_buf_map_sg(lnk, buf_desc,
1437                                is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1438         /* SMC protocol depends on mapping to one DMA address only */
1439         if (rc != 1) {
1440                 rc = -EAGAIN;
1441                 goto free_table;
1442         }
1443
1444         /* create a new memory region for the RMB */
1445         if (is_rmb) {
1446                 rc = smc_ib_get_memory_region(lnk->roce_pd,
1447                                               IB_ACCESS_REMOTE_WRITE |
1448                                               IB_ACCESS_LOCAL_WRITE,
1449                                               buf_desc, lnk->link_idx);
1450                 if (rc)
1451                         goto buf_unmap;
1452                 smc_ib_sync_sg_for_device(lnk, buf_desc, DMA_FROM_DEVICE);
1453         }
1454         buf_desc->is_map_ib[lnk->link_idx] = true;
1455         return 0;
1456
1457 buf_unmap:
1458         smc_ib_buf_unmap_sg(lnk, buf_desc,
1459                             is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1460 free_table:
1461         sg_free_table(&buf_desc->sgt[lnk->link_idx]);
1462         return rc;
1463 }
1464
1465 /* register a new rmb on IB device,
1466  * must be called under lgr->llc_conf_mutex lock
1467  */
1468 int smcr_link_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
1469 {
1470         if (list_empty(&link->lgr->list))
1471                 return -ENOLINK;
1472         if (!rmb_desc->is_reg_mr[link->link_idx]) {
1473                 /* register memory region for new rmb */
1474                 if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
1475                         rmb_desc->is_reg_err = true;
1476                         return -EFAULT;
1477                 }
1478                 rmb_desc->is_reg_mr[link->link_idx] = true;
1479         }
1480         return 0;
1481 }
1482
1483 static int _smcr_buf_map_lgr(struct smc_link *lnk, struct mutex *lock,
1484                              struct list_head *lst, bool is_rmb)
1485 {
1486         struct smc_buf_desc *buf_desc, *bf;
1487         int rc = 0;
1488
1489         mutex_lock(lock);
1490         list_for_each_entry_safe(buf_desc, bf, lst, list) {
1491                 if (!buf_desc->used)
1492                         continue;
1493                 rc = smcr_buf_map_link(buf_desc, is_rmb, lnk);
1494                 if (rc)
1495                         goto out;
1496         }
1497 out:
1498         mutex_unlock(lock);
1499         return rc;
1500 }
1501
1502 /* map all used buffers of lgr for a new link */
1503 int smcr_buf_map_lgr(struct smc_link *lnk)
1504 {
1505         struct smc_link_group *lgr = lnk->lgr;
1506         int i, rc = 0;
1507
1508         for (i = 0; i < SMC_RMBE_SIZES; i++) {
1509                 rc = _smcr_buf_map_lgr(lnk, &lgr->rmbs_lock,
1510                                        &lgr->rmbs[i], true);
1511                 if (rc)
1512                         return rc;
1513                 rc = _smcr_buf_map_lgr(lnk, &lgr->sndbufs_lock,
1514                                        &lgr->sndbufs[i], false);
1515                 if (rc)
1516                         return rc;
1517         }
1518         return 0;
1519 }
1520
1521 /* register all used buffers of lgr for a new link,
1522  * must be called under lgr->llc_conf_mutex lock
1523  */
1524 int smcr_buf_reg_lgr(struct smc_link *lnk)
1525 {
1526         struct smc_link_group *lgr = lnk->lgr;
1527         struct smc_buf_desc *buf_desc, *bf;
1528         int i, rc = 0;
1529
1530         mutex_lock(&lgr->rmbs_lock);
1531         for (i = 0; i < SMC_RMBE_SIZES; i++) {
1532                 list_for_each_entry_safe(buf_desc, bf, &lgr->rmbs[i], list) {
1533                         if (!buf_desc->used)
1534                                 continue;
1535                         rc = smcr_link_reg_rmb(lnk, buf_desc);
1536                         if (rc)
1537                                 goto out;
1538                 }
1539         }
1540 out:
1541         mutex_unlock(&lgr->rmbs_lock);
1542         return rc;
1543 }
1544
1545 static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
1546                                                 bool is_rmb, int bufsize)
1547 {
1548         struct smc_buf_desc *buf_desc;
1549
1550         /* try to alloc a new buffer */
1551         buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
1552         if (!buf_desc)
1553                 return ERR_PTR(-ENOMEM);
1554
1555         buf_desc->order = get_order(bufsize);
1556         buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
1557                                       __GFP_NOMEMALLOC | __GFP_COMP |
1558                                       __GFP_NORETRY | __GFP_ZERO,
1559                                       buf_desc->order);
1560         if (!buf_desc->pages) {
1561                 kfree(buf_desc);
1562                 return ERR_PTR(-EAGAIN);
1563         }
1564         buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
1565         buf_desc->len = bufsize;
1566         return buf_desc;
1567 }
1568
1569 /* map buf_desc on all usable links,
1570  * unused buffers stay mapped as long as the link is up
1571  */
1572 static int smcr_buf_map_usable_links(struct smc_link_group *lgr,
1573                                      struct smc_buf_desc *buf_desc, bool is_rmb)
1574 {
1575         int i, rc = 0;
1576
1577         /* protect against parallel link reconfiguration */
1578         mutex_lock(&lgr->llc_conf_mutex);
1579         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1580                 struct smc_link *lnk = &lgr->lnk[i];
1581
1582                 if (!smc_link_usable(lnk))
1583                         continue;
1584                 if (smcr_buf_map_link(buf_desc, is_rmb, lnk)) {
1585                         rc = -ENOMEM;
1586                         goto out;
1587                 }
1588         }
1589 out:
1590         mutex_unlock(&lgr->llc_conf_mutex);
1591         return rc;
1592 }
1593
1594 #define SMCD_DMBE_SIZES         7 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
1595
1596 static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
1597                                                 bool is_dmb, int bufsize)
1598 {
1599         struct smc_buf_desc *buf_desc;
1600         int rc;
1601
1602         if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES)
1603                 return ERR_PTR(-EAGAIN);
1604
1605         /* try to alloc a new DMB */
1606         buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
1607         if (!buf_desc)
1608                 return ERR_PTR(-ENOMEM);
1609         if (is_dmb) {
1610                 rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
1611                 if (rc) {
1612                         kfree(buf_desc);
1613                         return (rc == -ENOMEM) ? ERR_PTR(-EAGAIN) : ERR_PTR(rc);
1614                 }
1615                 buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
1616                 /* CDC header stored in buf. So, pretend it was smaller */
1617                 buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
1618         } else {
1619                 buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
1620                                              __GFP_NOWARN | __GFP_NORETRY |
1621                                              __GFP_NOMEMALLOC);
1622                 if (!buf_desc->cpu_addr) {
1623                         kfree(buf_desc);
1624                         return ERR_PTR(-EAGAIN);
1625                 }
1626                 buf_desc->len = bufsize;
1627         }
1628         return buf_desc;
1629 }
1630
1631 static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
1632 {
1633         struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
1634         struct smc_connection *conn = &smc->conn;
1635         struct smc_link_group *lgr = conn->lgr;
1636         struct list_head *buf_list;
1637         int bufsize, bufsize_short;
1638         struct mutex *lock;     /* lock buffer list */
1639         int sk_buf_size;
1640
1641         if (is_rmb)
1642                 /* use socket recv buffer size (w/o overhead) as start value */
1643                 sk_buf_size = smc->sk.sk_rcvbuf / 2;
1644         else
1645                 /* use socket send buffer size (w/o overhead) as start value */
1646                 sk_buf_size = smc->sk.sk_sndbuf / 2;
1647
1648         for (bufsize_short = smc_compress_bufsize(sk_buf_size);
1649              bufsize_short >= 0; bufsize_short--) {
1650
1651                 if (is_rmb) {
1652                         lock = &lgr->rmbs_lock;
1653                         buf_list = &lgr->rmbs[bufsize_short];
1654                 } else {
1655                         lock = &lgr->sndbufs_lock;
1656                         buf_list = &lgr->sndbufs[bufsize_short];
1657                 }
1658                 bufsize = smc_uncompress_bufsize(bufsize_short);
1659                 if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
1660                         continue;
1661
1662                 /* check for reusable slot in the link group */
1663                 buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
1664                 if (buf_desc) {
1665                         memset(buf_desc->cpu_addr, 0, bufsize);
1666                         break; /* found reusable slot */
1667                 }
1668
1669                 if (is_smcd)
1670                         buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
1671                 else
1672                         buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
1673
1674                 if (PTR_ERR(buf_desc) == -ENOMEM)
1675                         break;
1676                 if (IS_ERR(buf_desc))
1677                         continue;
1678
1679                 buf_desc->used = 1;
1680                 mutex_lock(lock);
1681                 list_add(&buf_desc->list, buf_list);
1682                 mutex_unlock(lock);
1683                 break; /* found */
1684         }
1685
1686         if (IS_ERR(buf_desc))
1687                 return PTR_ERR(buf_desc);
1688
1689         if (!is_smcd) {
1690                 if (smcr_buf_map_usable_links(lgr, buf_desc, is_rmb)) {
1691                         smcr_buf_unuse(buf_desc, lgr);
1692                         return -ENOMEM;
1693                 }
1694         }
1695
1696         if (is_rmb) {
1697                 conn->rmb_desc = buf_desc;
1698                 conn->rmbe_size_short = bufsize_short;
1699                 smc->sk.sk_rcvbuf = bufsize * 2;
1700                 atomic_set(&conn->bytes_to_rcv, 0);
1701                 conn->rmbe_update_limit =
1702                         smc_rmb_wnd_update_limit(buf_desc->len);
1703                 if (is_smcd)
1704                         smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
1705         } else {
1706                 conn->sndbuf_desc = buf_desc;
1707                 smc->sk.sk_sndbuf = bufsize * 2;
1708                 atomic_set(&conn->sndbuf_space, bufsize);
1709         }
1710         return 0;
1711 }
1712
1713 void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
1714 {
1715         if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
1716                 return;
1717         smc_ib_sync_sg_for_cpu(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1718 }
1719
1720 void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
1721 {
1722         if (!conn->lgr || conn->lgr->is_smcd || !smc_link_active(conn->lnk))
1723                 return;
1724         smc_ib_sync_sg_for_device(conn->lnk, conn->sndbuf_desc, DMA_TO_DEVICE);
1725 }
1726
1727 void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
1728 {
1729         int i;
1730
1731         if (!conn->lgr || conn->lgr->is_smcd)
1732                 return;
1733         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1734                 if (!smc_link_active(&conn->lgr->lnk[i]))
1735                         continue;
1736                 smc_ib_sync_sg_for_cpu(&conn->lgr->lnk[i], conn->rmb_desc,
1737                                        DMA_FROM_DEVICE);
1738         }
1739 }
1740
1741 void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
1742 {
1743         int i;
1744
1745         if (!conn->lgr || conn->lgr->is_smcd)
1746                 return;
1747         for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1748                 if (!smc_link_active(&conn->lgr->lnk[i]))
1749                         continue;
1750                 smc_ib_sync_sg_for_device(&conn->lgr->lnk[i], conn->rmb_desc,
1751                                           DMA_FROM_DEVICE);
1752         }
1753 }
1754
1755 /* create the send and receive buffer for an SMC socket;
1756  * receive buffers are called RMBs;
1757  * (even though the SMC protocol allows more than one RMB-element per RMB,
1758  * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
1759  * extra RMB for every connection in a link group
1760  */
1761 int smc_buf_create(struct smc_sock *smc, bool is_smcd)
1762 {
1763         int rc;
1764
1765         /* create send buffer */
1766         rc = __smc_buf_create(smc, is_smcd, false);
1767         if (rc)
1768                 return rc;
1769         /* create rmb */
1770         rc = __smc_buf_create(smc, is_smcd, true);
1771         if (rc) {
1772                 mutex_lock(&smc->conn.lgr->sndbufs_lock);
1773                 list_del(&smc->conn.sndbuf_desc->list);
1774                 mutex_unlock(&smc->conn.lgr->sndbufs_lock);
1775                 smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
1776                 smc->conn.sndbuf_desc = NULL;
1777         }
1778         return rc;
1779 }
1780
1781 static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
1782 {
1783         int i;
1784
1785         for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
1786                 if (!test_and_set_bit(i, lgr->rtokens_used_mask))
1787                         return i;
1788         }
1789         return -ENOSPC;
1790 }
1791
1792 static int smc_rtoken_find_by_link(struct smc_link_group *lgr, int lnk_idx,
1793                                    u32 rkey)
1794 {
1795         int i;
1796
1797         for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1798                 if (test_bit(i, lgr->rtokens_used_mask) &&
1799                     lgr->rtokens[i][lnk_idx].rkey == rkey)
1800                         return i;
1801         }
1802         return -ENOENT;
1803 }
1804
1805 /* set rtoken for a new link to an existing rmb */
1806 void smc_rtoken_set(struct smc_link_group *lgr, int link_idx, int link_idx_new,
1807                     __be32 nw_rkey_known, __be64 nw_vaddr, __be32 nw_rkey)
1808 {
1809         int rtok_idx;
1810
1811         rtok_idx = smc_rtoken_find_by_link(lgr, link_idx, ntohl(nw_rkey_known));
1812         if (rtok_idx == -ENOENT)
1813                 return;
1814         lgr->rtokens[rtok_idx][link_idx_new].rkey = ntohl(nw_rkey);
1815         lgr->rtokens[rtok_idx][link_idx_new].dma_addr = be64_to_cpu(nw_vaddr);
1816 }
1817
1818 /* set rtoken for a new link whose link_id is given */
1819 void smc_rtoken_set2(struct smc_link_group *lgr, int rtok_idx, int link_id,
1820                      __be64 nw_vaddr, __be32 nw_rkey)
1821 {
1822         u64 dma_addr = be64_to_cpu(nw_vaddr);
1823         u32 rkey = ntohl(nw_rkey);
1824         bool found = false;
1825         int link_idx;
1826
1827         for (link_idx = 0; link_idx < SMC_LINKS_PER_LGR_MAX; link_idx++) {
1828                 if (lgr->lnk[link_idx].link_id == link_id) {
1829                         found = true;
1830                         break;
1831                 }
1832         }
1833         if (!found)
1834                 return;
1835         lgr->rtokens[rtok_idx][link_idx].rkey = rkey;
1836         lgr->rtokens[rtok_idx][link_idx].dma_addr = dma_addr;
1837 }
1838
1839 /* add a new rtoken from peer */
1840 int smc_rtoken_add(struct smc_link *lnk, __be64 nw_vaddr, __be32 nw_rkey)
1841 {
1842         struct smc_link_group *lgr = smc_get_lgr(lnk);
1843         u64 dma_addr = be64_to_cpu(nw_vaddr);
1844         u32 rkey = ntohl(nw_rkey);
1845         int i;
1846
1847         for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1848                 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1849                     lgr->rtokens[i][lnk->link_idx].dma_addr == dma_addr &&
1850                     test_bit(i, lgr->rtokens_used_mask)) {
1851                         /* already in list */
1852                         return i;
1853                 }
1854         }
1855         i = smc_rmb_reserve_rtoken_idx(lgr);
1856         if (i < 0)
1857                 return i;
1858         lgr->rtokens[i][lnk->link_idx].rkey = rkey;
1859         lgr->rtokens[i][lnk->link_idx].dma_addr = dma_addr;
1860         return i;
1861 }
1862
1863 /* delete an rtoken from all links */
1864 int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
1865 {
1866         struct smc_link_group *lgr = smc_get_lgr(lnk);
1867         u32 rkey = ntohl(nw_rkey);
1868         int i, j;
1869
1870         for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
1871                 if (lgr->rtokens[i][lnk->link_idx].rkey == rkey &&
1872                     test_bit(i, lgr->rtokens_used_mask)) {
1873                         for (j = 0; j < SMC_LINKS_PER_LGR_MAX; j++) {
1874                                 lgr->rtokens[i][j].rkey = 0;
1875                                 lgr->rtokens[i][j].dma_addr = 0;
1876                         }
1877                         clear_bit(i, lgr->rtokens_used_mask);
1878                         return 0;
1879                 }
1880         }
1881         return -ENOENT;
1882 }
1883
1884 /* save rkey and dma_addr received from peer during clc handshake */
1885 int smc_rmb_rtoken_handling(struct smc_connection *conn,
1886                             struct smc_link *lnk,
1887                             struct smc_clc_msg_accept_confirm *clc)
1888 {
1889         conn->rtoken_idx = smc_rtoken_add(lnk, clc->r0.rmb_dma_addr,
1890                                           clc->r0.rmb_rkey);
1891         if (conn->rtoken_idx < 0)
1892                 return conn->rtoken_idx;
1893         return 0;
1894 }
1895
1896 static void smc_core_going_away(void)
1897 {
1898         struct smc_ib_device *smcibdev;
1899         struct smcd_dev *smcd;
1900
1901         mutex_lock(&smc_ib_devices.mutex);
1902         list_for_each_entry(smcibdev, &smc_ib_devices.list, list) {
1903                 int i;
1904
1905                 for (i = 0; i < SMC_MAX_PORTS; i++)
1906                         set_bit(i, smcibdev->ports_going_away);
1907         }
1908         mutex_unlock(&smc_ib_devices.mutex);
1909
1910         mutex_lock(&smcd_dev_list.mutex);
1911         list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1912                 smcd->going_away = 1;
1913         }
1914         mutex_unlock(&smcd_dev_list.mutex);
1915 }
1916
1917 /* Clean up all SMC link groups */
1918 static void smc_lgrs_shutdown(void)
1919 {
1920         struct smcd_dev *smcd;
1921
1922         smc_core_going_away();
1923
1924         smc_smcr_terminate_all(NULL);
1925
1926         mutex_lock(&smcd_dev_list.mutex);
1927         list_for_each_entry(smcd, &smcd_dev_list.list, list)
1928                 smc_smcd_terminate_all(smcd);
1929         mutex_unlock(&smcd_dev_list.mutex);
1930 }
1931
1932 static int smc_core_reboot_event(struct notifier_block *this,
1933                                  unsigned long event, void *ptr)
1934 {
1935         smc_lgrs_shutdown();
1936         smc_ib_unregister_client();
1937         return 0;
1938 }
1939
1940 static struct notifier_block smc_reboot_notifier = {
1941         .notifier_call = smc_core_reboot_event,
1942 };
1943
1944 int __init smc_core_init(void)
1945 {
1946         return register_reboot_notifier(&smc_reboot_notifier);
1947 }
1948
1949 /* Called (from smc_exit) when module is removed */
1950 void smc_core_exit(void)
1951 {
1952         unregister_reboot_notifier(&smc_reboot_notifier);
1953         smc_lgrs_shutdown();
1954 }