Whamcloud - gitweb
LU-16213 kfilnd: Finalize replay TNs with deleted peer
[fs/lustre-release.git] / lnet / klnds / kfilnd / kfilnd_tn.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright 2022 Hewlett Packard Enterprise Development LP
24  */
25 /*
26  * This file is part of Lustre, http://www.lustre.org/
27  */
28 /*
29  * kfilnd transaction and state machine processing.
30  */
31
32 #include "kfilnd_tn.h"
33 #include "kfilnd_ep.h"
34 #include "kfilnd_dev.h"
35 #include "kfilnd_dom.h"
36 #include "kfilnd_peer.h"
37 #include <asm/checksum.h>
38
39 static struct kmem_cache *tn_cache;
40 static struct kmem_cache *imm_buf_cache;
41
42 static __sum16 kfilnd_tn_cksum(void *ptr, int nob)
43 {
44         if (cksum)
45                 return csum_fold(csum_partial(ptr, nob, 0));
46         return NO_CHECKSUM;
47 }
48
49 static int kfilnd_tn_msgtype2size(enum kfilnd_msg_type type)
50 {
51         const int hdr_size = offsetof(struct kfilnd_msg, proto);
52
53         switch (type) {
54         case KFILND_MSG_IMMEDIATE:
55                 return offsetof(struct kfilnd_msg, proto.immed.payload[0]);
56
57         case KFILND_MSG_BULK_PUT_REQ:
58         case KFILND_MSG_BULK_GET_REQ:
59                 return hdr_size + sizeof(struct kfilnd_bulk_req_msg);
60
61         default:
62                 return -1;
63         }
64 }
65
66 static void kfilnd_tn_pack_hello_req(struct kfilnd_transaction *tn)
67 {
68         struct kfilnd_msg *msg = tn->tn_tx_msg.msg;
69
70         /* Pack the protocol header and payload. */
71         msg->proto.hello.version = KFILND_MSG_VERSION;
72         msg->proto.hello.rx_base = kfilnd_peer_target_rx_base(tn->tn_kp);
73         msg->proto.hello.session_key = tn->tn_kp->kp_local_session_key;
74
75         /* TODO: Support multiple RX contexts per peer. */
76         msg->proto.hello.rx_count = 1;
77
78         /* Pack the transport header. */
79         msg->magic = KFILND_MSG_MAGIC;
80
81         /* Mesage version zero is only valid for hello requests. */
82         msg->version = 0;
83         msg->type = KFILND_MSG_HELLO_REQ;
84         msg->nob = sizeof(struct kfilnd_hello_msg) +
85                 offsetof(struct kfilnd_msg, proto);
86         msg->cksum = NO_CHECKSUM;
87         msg->srcnid = lnet_nid_to_nid4(&tn->tn_ep->end_dev->kfd_ni->ni_nid);
88         msg->dstnid = tn->tn_kp->kp_nid;
89
90         /* Checksum entire message. */
91         msg->cksum = kfilnd_tn_cksum(msg, msg->nob);
92
93         tn->tn_tx_msg.length = msg->nob;
94 }
95
96 static void kfilnd_tn_pack_hello_rsp(struct kfilnd_transaction *tn)
97 {
98         struct kfilnd_msg *msg = tn->tn_tx_msg.msg;
99
100         /* Pack the protocol header and payload. */
101         msg->proto.hello.version = tn->tn_kp->kp_version;
102         msg->proto.hello.rx_base = kfilnd_peer_target_rx_base(tn->tn_kp);
103         msg->proto.hello.session_key = tn->tn_kp->kp_local_session_key;
104
105         /* TODO: Support multiple RX contexts per peer. */
106         msg->proto.hello.rx_count = 1;
107
108         /* Pack the transport header. */
109         msg->magic = KFILND_MSG_MAGIC;
110
111         /* Mesage version zero is only valid for hello requests. */
112         msg->version = 0;
113         msg->type = KFILND_MSG_HELLO_RSP;
114         msg->nob = sizeof(struct kfilnd_hello_msg) +
115                 offsetof(struct kfilnd_msg, proto);
116         msg->cksum = NO_CHECKSUM;
117         msg->srcnid = lnet_nid_to_nid4(&tn->tn_ep->end_dev->kfd_ni->ni_nid);
118         msg->dstnid = tn->tn_kp->kp_nid;
119
120         /* Checksum entire message. */
121         msg->cksum = kfilnd_tn_cksum(msg, msg->nob);
122
123         tn->tn_tx_msg.length = msg->nob;
124 }
125
126 static void kfilnd_tn_pack_bulk_req(struct kfilnd_transaction *tn)
127 {
128         struct kfilnd_msg *msg = tn->tn_tx_msg.msg;
129
130         /* Pack the protocol header and payload. */
131         lnet_hdr_to_nid4(&tn->tn_lntmsg->msg_hdr, &msg->proto.bulk_req.hdr);
132         msg->proto.bulk_req.key = tn->tn_mr_key;
133         msg->proto.bulk_req.response_rx = tn->tn_response_rx;
134
135         /* Pack the transport header. */
136         msg->magic = KFILND_MSG_MAGIC;
137         msg->version = KFILND_MSG_VERSION;
138         msg->type = tn->msg_type;
139         msg->nob = sizeof(struct kfilnd_bulk_req_msg) +
140                 offsetof(struct kfilnd_msg, proto);
141         msg->cksum = NO_CHECKSUM;
142         msg->srcnid = lnet_nid_to_nid4(&tn->tn_ep->end_dev->kfd_ni->ni_nid);
143         msg->dstnid = tn->tn_kp->kp_nid;
144
145         /* Checksum entire message. */
146         msg->cksum = kfilnd_tn_cksum(msg, msg->nob);
147
148         tn->tn_tx_msg.length = msg->nob;
149 }
150
151 static void kfilnd_tn_pack_immed_msg(struct kfilnd_transaction *tn)
152 {
153         struct kfilnd_msg *msg = tn->tn_tx_msg.msg;
154
155         /* Pack the protocol header and payload. */
156         lnet_hdr_to_nid4(&tn->tn_lntmsg->msg_hdr, &msg->proto.immed.hdr);
157
158         lnet_copy_kiov2flat(KFILND_IMMEDIATE_MSG_SIZE,
159                             msg,
160                             offsetof(struct kfilnd_msg,
161                                      proto.immed.payload),
162                             tn->tn_num_iovec, tn->tn_kiov, 0,
163                             tn->tn_nob);
164
165         /* Pack the transport header. */
166         msg->magic = KFILND_MSG_MAGIC;
167         msg->version = KFILND_MSG_VERSION;
168         msg->type = tn->msg_type;
169         msg->nob = offsetof(struct kfilnd_msg, proto.immed.payload[tn->tn_nob]);
170         msg->cksum = NO_CHECKSUM;
171         msg->srcnid = lnet_nid_to_nid4(&tn->tn_ep->end_dev->kfd_ni->ni_nid);
172         msg->dstnid = tn->tn_kp->kp_nid;
173
174         /* Checksum entire message. */
175         msg->cksum = kfilnd_tn_cksum(msg, msg->nob);
176
177         tn->tn_tx_msg.length = msg->nob;
178 }
179
180 static int kfilnd_tn_unpack_msg(struct kfilnd_ep *ep, struct kfilnd_msg *msg,
181                                 unsigned int nob)
182 {
183         const unsigned int hdr_size = offsetof(struct kfilnd_msg, proto);
184
185         if (nob < hdr_size) {
186                 KFILND_EP_ERROR(ep, "Short message: %u", nob);
187                 return -EPROTO;
188         }
189
190         /* TODO: Support byte swapping on mixed endian systems. */
191         if (msg->magic != KFILND_MSG_MAGIC) {
192                 KFILND_EP_ERROR(ep, "Bad magic: %#x", msg->magic);
193                 return -EPROTO;
194         }
195
196         /* TODO: Allow for older versions. */
197         if (msg->version > KFILND_MSG_VERSION) {
198                 KFILND_EP_ERROR(ep, "Bad version: %#x", msg->version);
199                 return -EPROTO;
200         }
201
202         if (msg->nob > nob) {
203                 KFILND_EP_ERROR(ep, "Short message: got=%u, expected=%u", nob,
204                                 msg->nob);
205                 return -EPROTO;
206         }
207
208         /* If kfilnd_tn_cksum() returns a non-zero value, checksum is bad. */
209         if (msg->cksum != NO_CHECKSUM && kfilnd_tn_cksum(msg, msg->nob)) {
210                 KFILND_EP_ERROR(ep, "Bad checksum");
211                 return -EPROTO;
212         }
213
214         if (msg->dstnid != lnet_nid_to_nid4(&ep->end_dev->kfd_ni->ni_nid)) {
215                 KFILND_EP_ERROR(ep, "Bad destination nid: %s",
216                                 libcfs_nid2str(msg->dstnid));
217                 return -EPROTO;
218         }
219
220         if (msg->srcnid == LNET_NID_ANY) {
221                 KFILND_EP_ERROR(ep, "Bad source nid: %s",
222                                 libcfs_nid2str(msg->srcnid));
223                 return -EPROTO;
224         }
225
226         if (msg->nob < kfilnd_tn_msgtype2size(msg->type)) {
227                 KFILND_EP_ERROR(ep, "Short %s: %d(%d)\n",
228                                 msg_type_to_str(msg->type),
229                                 msg->nob, kfilnd_tn_msgtype2size(msg->type));
230                 return -EPROTO;
231         }
232
233         switch ((enum kfilnd_msg_type)msg->type) {
234         case KFILND_MSG_IMMEDIATE:
235         case KFILND_MSG_BULK_PUT_REQ:
236         case KFILND_MSG_BULK_GET_REQ:
237                 if (msg->version == 0) {
238                         KFILND_EP_ERROR(ep,
239                                         "Bad message type and version: type=%s version=%u",
240                                         msg_type_to_str(msg->type),
241                                         msg->version);
242                         return -EPROTO;
243                 }
244                 break;
245
246         case KFILND_MSG_HELLO_REQ:
247         case KFILND_MSG_HELLO_RSP:
248                 if (msg->version != 0) {
249                         KFILND_EP_ERROR(ep,
250                                         "Bad message type and version: type=%s version=%u",
251                                         msg_type_to_str(msg->type),
252                                         msg->version);
253                         return -EPROTO;
254                 }
255                 break;
256
257         default:
258                 CERROR("Unknown message type %x\n", msg->type);
259                 return -EPROTO;
260         }
261         return 0;
262 }
263
264 static void kfilnd_tn_record_state_change(struct kfilnd_transaction *tn)
265 {
266         unsigned int data_size_bucket =
267                 kfilnd_msg_len_to_data_size_bucket(tn->lnet_msg_len);
268         struct kfilnd_tn_duration_stat *stat;
269
270         if (tn->is_initiator)
271                 stat = &tn->tn_ep->end_dev->initiator_state_stats.state[tn->tn_state].data_size[data_size_bucket];
272         else
273                 stat = &tn->tn_ep->end_dev->target_state_stats.state[tn->tn_state].data_size[data_size_bucket];
274
275         atomic64_add(ktime_to_ns(ktime_sub(ktime_get(), tn->tn_state_ts)),
276                      &stat->accumulated_duration);
277         atomic_inc(&stat->accumulated_count);
278 }
279
280 static void kfilnd_tn_state_change(struct kfilnd_transaction *tn,
281                                    enum tn_states new_state)
282 {
283         KFILND_TN_DEBUG(tn, "%s -> %s state change",
284                         tn_state_to_str(tn->tn_state),
285                         tn_state_to_str(new_state));
286
287         kfilnd_tn_record_state_change(tn);
288
289         tn->tn_state = new_state;
290         tn->tn_state_ts = ktime_get();
291 }
292
293 static void kfilnd_tn_status_update(struct kfilnd_transaction *tn, int status,
294                                     enum lnet_msg_hstatus hstatus)
295 {
296         /* Only the first non-ok status will take. */
297         if (tn->tn_status == 0) {
298                 KFILND_TN_DEBUG(tn, "%d -> %d status change", tn->tn_status,
299                                 status);
300                 tn->tn_status = status;
301         }
302
303         if (tn->hstatus == LNET_MSG_STATUS_OK) {
304                 KFILND_TN_DEBUG(tn, "%d -> %d health status change",
305                                 tn->hstatus, hstatus);
306                 tn->hstatus = hstatus;
307         }
308 }
309
310 static bool kfilnd_tn_has_failed(struct kfilnd_transaction *tn)
311 {
312         return tn->tn_status != 0;
313 }
314
315 /**
316  * kfilnd_tn_process_rx_event() - Process an immediate receive event.
317  *
318  * For each immediate receive, a transaction structure needs to be allocated to
319  * process the receive.
320  */
321 void kfilnd_tn_process_rx_event(struct kfilnd_immediate_buffer *bufdesc,
322                                 struct kfilnd_msg *rx_msg, int msg_size)
323 {
324         struct kfilnd_transaction *tn;
325         bool alloc_msg = true;
326         int rc;
327         enum tn_events event = TN_EVENT_RX_HELLO;
328
329         /* Increment buf ref count for this work */
330         atomic_inc(&bufdesc->immed_ref);
331
332         /* Unpack the message */
333         rc = kfilnd_tn_unpack_msg(bufdesc->immed_end, rx_msg, msg_size);
334         if (rc || CFS_FAIL_CHECK(CFS_KFI_FAIL_MSG_UNPACK)) {
335                 kfilnd_ep_imm_buffer_put(bufdesc);
336                 KFILND_EP_ERROR(bufdesc->immed_end,
337                                 "Failed to unpack message %d", rc);
338                 return;
339         }
340
341         switch ((enum kfilnd_msg_type)rx_msg->type) {
342         case KFILND_MSG_IMMEDIATE:
343         case KFILND_MSG_BULK_PUT_REQ:
344         case KFILND_MSG_BULK_GET_REQ:
345                 event = TN_EVENT_RX_OK;
346                 fallthrough;
347         case KFILND_MSG_HELLO_RSP:
348                 alloc_msg = false;
349                 fallthrough;
350         case KFILND_MSG_HELLO_REQ:
351                 /* Context points to a received buffer and status is the length.
352                  * Allocate a Tn structure, set its values, then launch the
353                  * receive.
354                  */
355                 tn = kfilnd_tn_alloc(bufdesc->immed_end->end_dev,
356                                      bufdesc->immed_end->end_cpt,
357                                      rx_msg->srcnid, alloc_msg, false,
358                                      false);
359                 if (IS_ERR(tn)) {
360                         kfilnd_ep_imm_buffer_put(bufdesc);
361                         KFILND_EP_ERROR(bufdesc->immed_end,
362                                         "Failed to allocate transaction struct: rc=%ld",
363                                         PTR_ERR(tn));
364                         return;
365                 }
366
367                 tn->tn_rx_msg.msg = rx_msg;
368                 tn->tn_rx_msg.length = msg_size;
369                 tn->tn_posted_buf = bufdesc;
370
371                 KFILND_EP_DEBUG(bufdesc->immed_end, "%s transaction ID %u",
372                                 msg_type_to_str((enum kfilnd_msg_type)rx_msg->type),
373                                 tn->tn_mr_key);
374                 break;
375
376         default:
377                 KFILND_EP_ERROR(bufdesc->immed_end,
378                                 "Unhandled kfilnd message type: %d",
379                                 (enum kfilnd_msg_type)rx_msg->type);
380                 LBUG();
381         };
382
383         kfilnd_tn_event_handler(tn, event, 0);
384 }
385
386 static void kfilnd_tn_record_duration(struct kfilnd_transaction *tn)
387 {
388         unsigned int data_size_bucket =
389                 kfilnd_msg_len_to_data_size_bucket(tn->lnet_msg_len);
390         struct kfilnd_tn_duration_stat *stat;
391
392         if (tn->is_initiator)
393                 stat = &tn->tn_ep->end_dev->initiator_stats.data_size[data_size_bucket];
394         else
395                 stat = &tn->tn_ep->end_dev->target_stats.data_size[data_size_bucket];
396
397         atomic64_add(ktime_to_ns(ktime_sub(ktime_get(), tn->tn_alloc_ts)),
398                      &stat->accumulated_duration);
399         atomic_inc(&stat->accumulated_count);
400 }
401
402 /**
403  * kfilnd_tn_finalize() - Cleanup resources and finalize LNet operation.
404  *
405  * All state machine functions should call kfilnd_tn_finalize() instead of
406  * kfilnd_tn_free(). Once all expected asynchronous events have been received,
407  * if the transaction lock has not been released, it will now be released,
408  * transaction resources cleaned up, and LNet finalized will be called.
409  */
410 static void kfilnd_tn_finalize(struct kfilnd_transaction *tn, bool *tn_released)
411 {
412         if (!*tn_released) {
413                 mutex_unlock(&tn->tn_lock);
414                 *tn_released = true;
415         }
416
417         /* Release the reference on the multi-receive buffer. */
418         if (tn->tn_posted_buf)
419                 kfilnd_ep_imm_buffer_put(tn->tn_posted_buf);
420
421         /* Finalize LNet operation. */
422         if (tn->tn_lntmsg) {
423                 tn->tn_lntmsg->msg_health_status = tn->hstatus;
424                 lnet_finalize(tn->tn_lntmsg, tn->tn_status);
425         }
426
427         if (tn->tn_getreply) {
428                 tn->tn_getreply->msg_health_status = tn->hstatus;
429                 lnet_set_reply_msg_len(tn->tn_ep->end_dev->kfd_ni,
430                                        tn->tn_getreply,
431                                        tn->tn_status ? 0 : tn->tn_nob);
432                 lnet_finalize(tn->tn_getreply, tn->tn_status);
433         }
434
435         if (KFILND_TN_PEER_VALID(tn))
436                 kfilnd_peer_put(tn->tn_kp);
437
438         kfilnd_tn_record_state_change(tn);
439         kfilnd_tn_record_duration(tn);
440
441         kfilnd_tn_free(tn);
442 }
443
444 /**
445  * kfilnd_tn_cancel_tag_recv() - Attempt to cancel a tagged receive.
446  * @tn: Transaction to have tagged received cancelled.
447  *
448  * Return: 0 on success. Else, negative errno. If an error occurs, resources may
449  * be leaked.
450  */
451 static int kfilnd_tn_cancel_tag_recv(struct kfilnd_transaction *tn)
452 {
453         int rc;
454
455         /* Issue a cancel. A return code of zero means the operation issued an
456          * async cancel. A return code of -ENOENT means the tagged receive was
457          * not found. The assumption here is that a tagged send landed thus
458          * removing the tagged receive buffer from hardware. For both cases,
459          * async events should occur.
460          */
461         rc = kfilnd_ep_cancel_tagged_recv(tn->tn_ep, tn);
462         if (rc != 0 && rc != -ENOENT) {
463                 KFILND_TN_ERROR(tn, "Failed to cancel tag receive. Resources may leak.");
464                 return rc;
465         }
466
467         return 0;
468 }
469
470 static void kfilnd_tn_timeout_work(struct work_struct *work)
471 {
472         struct kfilnd_transaction *tn =
473                 container_of(work, struct kfilnd_transaction, timeout_work);
474
475         KFILND_TN_ERROR(tn, "Bulk operation timeout");
476         kfilnd_tn_event_handler(tn, TN_EVENT_TIMEOUT, 0);
477 }
478
479 static void kfilnd_tn_timeout(cfs_timer_cb_arg_t data)
480 {
481         struct kfilnd_transaction *tn = cfs_from_timer(tn, data, timeout_timer);
482
483         queue_work(kfilnd_wq, &tn->timeout_work);
484 }
485
486 static bool kfilnd_tn_timeout_cancel(struct kfilnd_transaction *tn)
487 {
488         return del_timer(&tn->timeout_timer);
489 }
490
491 static void kfilnd_tn_timeout_enable(struct kfilnd_transaction *tn)
492 {
493         ktime_t remaining_time = max_t(ktime_t, 0,
494                                        tn->deadline - ktime_get_seconds());
495         unsigned long expires = remaining_time * HZ + jiffies;
496
497         if (CFS_FAIL_CHECK(CFS_KFI_FAIL_BULK_TIMEOUT))
498                 expires = jiffies;
499
500         cfs_timer_setup(&tn->timeout_timer, kfilnd_tn_timeout,
501                         (unsigned long)tn, 0);
502         mod_timer(&tn->timeout_timer, expires);
503 }
504
505 /*  The following are the state machine routines for the transactions. */
506 static int kfilnd_tn_state_send_failed(struct kfilnd_transaction *tn,
507                                        enum tn_events event, int status,
508                                        bool *tn_released)
509 {
510         int rc;
511
512         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
513                         status);
514
515         switch (event) {
516         case TN_EVENT_INIT_BULK:
517                 /* Need to cancel the tagged receive to prevent resources from
518                  * being leaked.
519                  */
520                 rc = kfilnd_tn_cancel_tag_recv(tn);
521
522                 switch (rc) {
523                 /* Async event will progress transaction. */
524                 case 0:
525                         kfilnd_tn_state_change(tn, TN_STATE_FAIL);
526                         return 0;
527
528                 /* Need to replay TN_EVENT_INIT_BULK event while in the
529                  * TN_STATE_SEND_FAILED state.
530                  */
531                 case -EAGAIN:
532                         KFILND_TN_DEBUG(tn,
533                                         "Need to replay cancel tagged recv");
534                         return -EAGAIN;
535
536                 default:
537                         KFILND_TN_ERROR(tn,
538                                         "Unexpected error during cancel tagged receive: rc=%d",
539                                         rc);
540                         LBUG();
541                 }
542                 break;
543
544         default:
545                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
546                 LBUG();
547         }
548 }
549
550 static int kfilnd_tn_state_tagged_recv_posted(struct kfilnd_transaction *tn,
551                                               enum tn_events event, int status,
552                                               bool *tn_released)
553 {
554         int rc;
555
556         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
557                         status);
558
559         switch (event) {
560         case TN_EVENT_INIT_BULK:
561                 tn->tn_target_addr = kfilnd_peer_get_kfi_addr(tn->tn_kp);
562                 KFILND_TN_DEBUG(tn, "Using peer %s(%#llx)",
563                                 libcfs_nid2str(tn->tn_kp->kp_nid),
564                                 tn->tn_target_addr);
565
566                 kfilnd_tn_pack_bulk_req(tn);
567
568                 rc = kfilnd_ep_post_send(tn->tn_ep, tn);
569                 switch (rc) {
570                 /* Async event will progress immediate send. */
571                 case 0:
572                         kfilnd_tn_state_change(tn, TN_STATE_WAIT_COMP);
573                         return 0;
574
575                 /* Need to replay TN_EVENT_INIT_BULK event while in the
576                  * TN_STATE_TAGGED_RECV_POSTED state.
577                  */
578                 case -EAGAIN:
579                         KFILND_TN_DEBUG(tn,
580                                         "Need to replay post send to %s(%#llx)",
581                                         libcfs_nid2str(tn->tn_kp->kp_nid),
582                                         tn->tn_target_addr);
583                         return -EAGAIN;
584
585                 /* Need to transition to the TN_STATE_SEND_FAILED to cleanup
586                  * posted tagged receive buffer.
587                  */
588                 default:
589                         KFILND_TN_ERROR(tn,
590                                         "Failed to post send to %s(%#llx): rc=%d",
591                                         libcfs_nid2str(tn->tn_kp->kp_nid),
592                                         tn->tn_target_addr, rc);
593                         kfilnd_tn_status_update(tn, rc,
594                                                 LNET_MSG_STATUS_LOCAL_ERROR);
595                         kfilnd_tn_state_change(tn, TN_STATE_SEND_FAILED);
596
597                         /* Propogate TN_EVENT_INIT_BULK event to
598                          * TN_STATE_SEND_FAILED handler.
599                          */
600                         return kfilnd_tn_state_send_failed(tn, event, rc,
601                                                            tn_released);
602                 }
603
604         default:
605                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
606                 LBUG();
607         }
608 }
609
610 static int kfilnd_tn_state_idle(struct kfilnd_transaction *tn,
611                                 enum tn_events event, int status,
612                                 bool *tn_released)
613 {
614         struct kfilnd_msg *msg;
615         int rc;
616         bool finalize = false;
617         struct lnet_hdr hdr;
618         struct lnet_nid srcnid;
619
620         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
621                         status);
622
623         /* For new peers, send a hello request message and queue the true LNet
624          * message for replay.
625          */
626         if (kfilnd_peer_is_new_peer(tn->tn_kp) &&
627             (event == TN_EVENT_INIT_IMMEDIATE || event == TN_EVENT_INIT_BULK)) {
628                 if (kfilnd_peer_deleted(tn->tn_kp)) {
629                         /* We'll assign a NETWORK_TIMEOUT message health status
630                          * below because we don't know why this peer was marked
631                          * for removal
632                          */
633                         rc = -ESTALE;
634                         KFILND_TN_DEBUG(tn,
635                                         "Dropping message to stale peer %s\n",
636                                         libcfs_nid2str(tn->tn_kp->kp_nid));
637                 } else if (ktime_after(tn->deadline, ktime_get_seconds())) {
638                         /* If transaction deadline has not been met, return
639                          * -EAGAIN. This will cause this transaction event to be
640                          * replayed. During this time, an async message from the
641                          * peer should occur at which point the kfilnd version
642                          * should be negotiated.
643                          */
644                         KFILND_TN_DEBUG(tn, "%s hello response pending",
645                                         libcfs_nid2str(tn->tn_kp->kp_nid));
646                         return -EAGAIN;
647                 } else {
648                         rc = -ETIMEDOUT;
649                 }
650
651                 kfilnd_tn_status_update(tn, rc,
652                                         LNET_MSG_STATUS_NETWORK_TIMEOUT);
653                 rc = 0;
654                 goto out;
655         }
656
657         switch (event) {
658         case TN_EVENT_INIT_IMMEDIATE:
659         case TN_EVENT_TX_HELLO:
660                 tn->tn_target_addr = kfilnd_peer_get_kfi_addr(tn->tn_kp);
661                 KFILND_TN_DEBUG(tn, "Using peer %s(%#llx)",
662                                 libcfs_nid2str(tn->tn_kp->kp_nid),
663                                 tn->tn_target_addr);
664
665                 if (event == TN_EVENT_INIT_IMMEDIATE)
666                         kfilnd_tn_pack_immed_msg(tn);
667                 else
668                         kfilnd_tn_pack_hello_req(tn);
669
670                 /* Send immediate message. */
671                 rc = kfilnd_ep_post_send(tn->tn_ep, tn);
672                 switch (rc) {
673                 /* Async event will progress immediate send. */
674                 case 0:
675                         kfilnd_tn_state_change(tn, TN_STATE_IMM_SEND);
676                         return 0;
677
678                 /* Need to TN_EVENT_INIT_IMMEDIATE event while in TN_STATE_IDLE
679                  * state.
680                  */
681                 case -EAGAIN:
682                         KFILND_TN_DEBUG(tn, "Need to replay send to %s(%#llx)",
683                                         libcfs_nid2str(tn->tn_kp->kp_nid),
684                                         tn->tn_target_addr);
685                         return -EAGAIN;
686
687                 default:
688                         KFILND_TN_ERROR(tn,
689                                         "Failed to post send to %s(%#llx): rc=%d",
690                                         libcfs_nid2str(tn->tn_kp->kp_nid),
691                                         tn->tn_target_addr, rc);
692                         if (event == TN_EVENT_TX_HELLO)
693                                 kfilnd_peer_clear_hello_pending(tn->tn_kp);
694                         kfilnd_tn_status_update(tn, rc,
695                                                 LNET_MSG_STATUS_LOCAL_ERROR);
696                 }
697                 break;
698
699         case TN_EVENT_INIT_BULK:
700                 /* Post tagged receive buffer used to land bulk response. */
701                 rc = kfilnd_ep_post_tagged_recv(tn->tn_ep, tn);
702
703                 switch (rc) {
704                 /* Transition to TN_STATE_TAGGED_RECV_POSTED on success. */
705                 case 0:
706                         kfilnd_tn_state_change(tn, TN_STATE_TAGGED_RECV_POSTED);
707
708                         /* Propogate TN_EVENT_INIT_BULK event to
709                          * TN_STATE_TAGGED_RECV_POSTED handler.
710                          */
711                         return kfilnd_tn_state_tagged_recv_posted(tn, event,
712                                                                   rc,
713                                                                   tn_released);
714
715                 /* Need to replay TN_EVENT_INIT_BULK event in the TN_STATE_IDLE
716                  * state.
717                  */
718                 case -EAGAIN:
719                         KFILND_TN_DEBUG(tn, "Need to replay tagged recv");
720                         return -EAGAIN;
721
722                 default:
723                         KFILND_TN_ERROR(tn, "Failed to post tagged recv %d",
724                                         rc);
725                         kfilnd_tn_status_update(tn, rc,
726                                                 LNET_MSG_STATUS_LOCAL_ERROR);
727                 }
728                 break;
729
730         case TN_EVENT_RX_OK:
731                 if (kfilnd_peer_needs_hello(tn->tn_kp)) {
732                         rc = kfilnd_send_hello_request(tn->tn_ep->end_dev,
733                                                        tn->tn_ep->end_cpt,
734                                                        tn->tn_kp);
735                         if (rc)
736                                 KFILND_TN_ERROR(tn,
737                                                 "Failed to send hello request: rc=%d",
738                                                 rc);
739                         rc = 0;
740                 }
741
742                 /* If this is a new peer then we cannot progress the transaction
743                  * and must drop it
744                  */
745                 if (kfilnd_peer_is_new_peer(tn->tn_kp)) {
746                         KFILND_TN_ERROR(tn,
747                                         "Dropping message from %s due to stale peer",
748                                         libcfs_nid2str(tn->tn_kp->kp_nid));
749                         kfilnd_tn_status_update(tn, -EPROTO,
750                                                 LNET_MSG_STATUS_LOCAL_DROPPED);
751                         rc = 0;
752                         goto out;
753                 }
754
755                 LASSERT(kfilnd_peer_is_new_peer(tn->tn_kp) == false);
756                 msg = tn->tn_rx_msg.msg;
757
758                 /* Update the NID address with the new preferred RX context. */
759                 kfilnd_peer_alive(tn->tn_kp);
760
761                 /* Pass message up to LNet
762                  * The TN will be reused in this call chain so we need to
763                  * release the lock on the TN before proceeding.
764                  */
765                 KFILND_TN_DEBUG(tn, "%s -> TN_STATE_IMM_RECV state change",
766                                 tn_state_to_str(tn->tn_state));
767
768                 /* TODO: Do not manually update this state change. */
769                 tn->tn_state = TN_STATE_IMM_RECV;
770                 mutex_unlock(&tn->tn_lock);
771                 *tn_released = true;
772                 lnet_nid4_to_nid(msg->srcnid, &srcnid);
773                 if (msg->type == KFILND_MSG_IMMEDIATE) {
774                         lnet_hdr_from_nid4(&hdr, &msg->proto.immed.hdr);
775                         rc = lnet_parse(tn->tn_ep->end_dev->kfd_ni,
776                                         &hdr, &srcnid, tn, 0);
777                 } else {
778                         lnet_hdr_from_nid4(&hdr, &msg->proto.bulk_req.hdr);
779                         rc = lnet_parse(tn->tn_ep->end_dev->kfd_ni,
780                                         &hdr, &srcnid, tn, 1);
781                 }
782
783                 /* If successful, transaction has been accepted by LNet and we
784                  * cannot process the transaction anymore within this context.
785                  */
786                 if (!rc)
787                         return 0;
788
789                 KFILND_TN_ERROR(tn, "Failed to parse LNet message: rc=%d", rc);
790                 kfilnd_tn_status_update(tn, rc, LNET_MSG_STATUS_LOCAL_ERROR);
791                 break;
792
793         case TN_EVENT_RX_HELLO:
794                 msg = tn->tn_rx_msg.msg;
795
796                 switch (msg->type) {
797                 case KFILND_MSG_HELLO_REQ:
798                         kfilnd_peer_process_hello(tn->tn_kp, msg);
799                         tn->tn_target_addr = kfilnd_peer_get_kfi_addr(tn->tn_kp);
800                         KFILND_TN_DEBUG(tn, "Using peer %s(%#llx)",
801                                         libcfs_nid2str(tn->tn_kp->kp_nid),
802                                         tn->tn_target_addr);
803
804                         kfilnd_tn_pack_hello_rsp(tn);
805
806                         /* Send immediate message. */
807                         rc = kfilnd_ep_post_send(tn->tn_ep, tn);
808                         switch (rc) {
809                         case 0:
810                                 kfilnd_tn_state_change(tn, TN_STATE_IMM_SEND);
811                                 return 0;
812
813                         case -EAGAIN:
814                                 KFILND_TN_DEBUG(tn, "Need to replay send to %s(%#llx)",
815                                                 libcfs_nid2str(tn->tn_kp->kp_nid),
816                                                 tn->tn_target_addr);
817                                 return -EAGAIN;
818
819                         default:
820                                 KFILND_TN_ERROR(tn,
821                                                 "Failed to post send to %s(%#llx): rc=%d",
822                                                 libcfs_nid2str(tn->tn_kp->kp_nid),
823                                                 tn->tn_target_addr, rc);
824                                 kfilnd_tn_status_update(tn, rc,
825                                                         LNET_MSG_STATUS_LOCAL_ERROR);
826                         }
827                         break;
828
829                 case KFILND_MSG_HELLO_RSP:
830                         rc = 0;
831                         kfilnd_peer_process_hello(tn->tn_kp, msg);
832                         finalize = true;
833                         break;
834
835                 default:
836                         KFILND_TN_ERROR(tn, "Invalid message type: %s",
837                                         msg_type_to_str(msg->type));
838                         LBUG();
839                 }
840                 break;
841
842         default:
843                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
844                 LBUG();
845         }
846
847 out:
848         if (kfilnd_tn_has_failed(tn))
849                 finalize = true;
850
851         if (finalize)
852                 kfilnd_tn_finalize(tn, tn_released);
853
854         return rc;
855 }
856
857 static int kfilnd_tn_state_imm_send(struct kfilnd_transaction *tn,
858                                     enum tn_events event, int status,
859                                     bool *tn_released)
860 {
861         enum lnet_msg_hstatus hstatus;
862
863         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
864                         status);
865
866         switch (event) {
867         case TN_EVENT_TX_FAIL:
868                 if (status == -ETIMEDOUT || status == -EIO)
869                         hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
870                 else
871                         hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
872
873                 kfilnd_tn_status_update(tn, status, hstatus);
874                 kfilnd_peer_down(tn->tn_kp);
875                 if (tn->msg_type == KFILND_MSG_HELLO_REQ)
876                         kfilnd_peer_clear_hello_pending(tn->tn_kp);
877                 break;
878
879         case TN_EVENT_TX_OK:
880                 kfilnd_peer_alive(tn->tn_kp);
881                 break;
882
883         default:
884                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
885                 LBUG();
886         }
887
888         kfilnd_tn_finalize(tn, tn_released);
889
890         return 0;
891 }
892
893 static int kfilnd_tn_state_imm_recv(struct kfilnd_transaction *tn,
894                                     enum tn_events event, int status,
895                                     bool *tn_released)
896 {
897         int rc = 0;
898         bool finalize = false;
899
900         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
901                         status);
902
903         switch (event) {
904         case TN_EVENT_INIT_TAG_RMA:
905         case TN_EVENT_SKIP_TAG_RMA:
906                 /* Release the buffer we received the request on. All relevant
907                  * information to perform the RMA operation is stored in the
908                  * transaction structure. This should be done before the RMA
909                  * operation to prevent two contexts from potentially processing
910                  * the same transaction.
911                  *
912                  * TODO: Prevent this from returning -EAGAIN.
913                  */
914                 if (tn->tn_posted_buf) {
915                         kfilnd_ep_imm_buffer_put(tn->tn_posted_buf);
916                         tn->tn_posted_buf = NULL;
917                 }
918
919                 /* Update the KFI address to use the response RX context. */
920                 tn->tn_target_addr =
921                         kfi_rx_addr(KFILND_BASE_ADDR(tn->tn_kp->kp_addr),
922                                     tn->tn_response_rx, KFILND_FAB_RX_CTX_BITS);
923                 KFILND_TN_DEBUG(tn, "Using peer %s(0x%llx)",
924                                 libcfs_nid2str(tn->tn_kp->kp_nid),
925                                 tn->tn_target_addr);
926
927                 /* Initiate the RMA operation to push/pull the LNet payload or
928                  * send a tagged message to finalize the bulk operation if the
929                  * RMA operation should be skipped.
930                  */
931                 if (event == TN_EVENT_INIT_TAG_RMA) {
932                         if (tn->sink_buffer)
933                                 rc = kfilnd_ep_post_read(tn->tn_ep, tn);
934                         else
935                                 rc = kfilnd_ep_post_write(tn->tn_ep, tn);
936
937                         switch (rc) {
938                         /* Async tagged RMA event will progress transaction. */
939                         case 0:
940                                 kfilnd_tn_state_change(tn,
941                                                        TN_STATE_WAIT_TAG_RMA_COMP);
942                                 return 0;
943
944                         /* Need to replay TN_EVENT_INIT_TAG_RMA event while in
945                          * the TN_STATE_IMM_RECV state.
946                          */
947                         case -EAGAIN:
948                                 KFILND_TN_DEBUG(tn,
949                                                 "Need to replay tagged %s to %s(%#llx)",
950                                                 tn->sink_buffer ? "read" : "write",
951                                                 libcfs_nid2str(tn->tn_kp->kp_nid),
952                                                 tn->tn_target_addr);
953                                 return -EAGAIN;
954
955                         default:
956                                 KFILND_TN_ERROR(tn,
957                                                 "Failed to post tagged %s to %s(%#llx): rc=%d",
958                                                 tn->sink_buffer ? "read" : "write",
959                                                 libcfs_nid2str(tn->tn_kp->kp_nid),
960                                                 tn->tn_target_addr, rc);
961                                 kfilnd_tn_status_update(tn, rc,
962                                                         LNET_MSG_STATUS_LOCAL_ERROR);
963                         }
964                 } else {
965                         kfilnd_tn_status_update(tn, status,
966                                                 LNET_MSG_STATUS_OK);
967
968                         /* Since the LNet initiator has posted a unique tagged
969                          * buffer specific for this LNet transaction and the
970                          * LNet target has decide not to push/pull to/for the
971                          * LNet initiator tagged buffer, a noop operation is
972                          * done to this tagged buffer (i/e payload transfer size
973                          * is zero). But, immediate data, which contains the
974                          * LNet target status for the transaction, is sent to
975                          * the LNet initiator. Immediate data only appears in
976                          * the completion event at the LNet initiator and not in
977                          * the tagged buffer.
978                          */
979                         tn->tagged_data = cpu_to_be64(abs(tn->tn_status));
980
981                         rc = kfilnd_ep_post_tagged_send(tn->tn_ep, tn);
982                         switch (rc) {
983                         /* Async tagged RMA event will progress transaction. */
984                         case 0:
985                                 kfilnd_tn_state_change(tn,
986                                                        TN_STATE_WAIT_TAG_COMP);
987                                 return 0;
988
989                         /* Need to replay TN_EVENT_SKIP_TAG_RMA event while in
990                          * the TN_STATE_IMM_RECV state.
991                          */
992                         case -EAGAIN:
993                                 KFILND_TN_DEBUG(tn,
994                                                 "Need to replay tagged send to %s(%#llx)",
995                                                 libcfs_nid2str(tn->tn_kp->kp_nid),
996                                                 tn->tn_target_addr);
997                                 return -EAGAIN;
998
999                         default:
1000                                 KFILND_TN_ERROR(tn,
1001                                                 "Failed to post tagged send to %s(%#llx): rc=%d",
1002                                                 libcfs_nid2str(tn->tn_kp->kp_nid),
1003                                                 tn->tn_target_addr, rc);
1004                                 kfilnd_tn_status_update(tn, rc,
1005                                                         LNET_MSG_STATUS_LOCAL_ERROR);
1006                         }
1007                 }
1008                 break;
1009
1010         case TN_EVENT_RX_OK:
1011                 finalize = true;
1012                 break;
1013
1014         default:
1015                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1016                 LBUG();
1017         }
1018
1019         if (kfilnd_tn_has_failed(tn))
1020                 finalize = true;
1021
1022         if (finalize)
1023                 kfilnd_tn_finalize(tn, tn_released);
1024
1025         return rc;
1026 }
1027
1028 static int kfilnd_tn_state_wait_comp(struct kfilnd_transaction *tn,
1029                                      enum tn_events event, int status,
1030                                      bool *tn_released)
1031 {
1032         int rc;
1033         enum lnet_msg_hstatus hstatus;
1034
1035         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1036                         status);
1037
1038         switch (event) {
1039         case TN_EVENT_TX_OK:
1040                 kfilnd_peer_alive(tn->tn_kp);
1041                 kfilnd_tn_timeout_enable(tn);
1042                 kfilnd_tn_state_change(tn, TN_STATE_WAIT_TAG_COMP);
1043                 break;
1044
1045         case TN_EVENT_TAG_RX_OK:
1046                 kfilnd_tn_state_change(tn, TN_STATE_WAIT_SEND_COMP);
1047                 break;
1048
1049         case TN_EVENT_TX_FAIL:
1050                 if (status == -ETIMEDOUT)
1051                         hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
1052                 else
1053                         hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
1054
1055                 kfilnd_tn_status_update(tn, status, hstatus);
1056                 kfilnd_peer_down(tn->tn_kp);
1057
1058                 /* Need to cancel the tagged receive to prevent resources from
1059                  * being leaked.
1060                  */
1061                 rc = kfilnd_tn_cancel_tag_recv(tn);
1062
1063                 switch (rc) {
1064                 /* Async cancel event will progress transaction. */
1065                 case 0:
1066                         kfilnd_tn_status_update(tn, status,
1067                                                 LNET_MSG_STATUS_LOCAL_ERROR);
1068                         kfilnd_tn_state_change(tn, TN_STATE_FAIL);
1069                         return 0;
1070
1071                 /* Need to replay TN_EVENT_INIT_BULK event while in the
1072                  * TN_STATE_SEND_FAILED state.
1073                  */
1074                 case -EAGAIN:
1075                         KFILND_TN_DEBUG(tn,
1076                                         "Need to replay cancel tagged recv");
1077                         return -EAGAIN;
1078
1079                 default:
1080                         KFILND_TN_ERROR(tn,
1081                                         "Unexpected error during cancel tagged receive: rc=%d",
1082                                         rc);
1083                         LBUG();
1084                 }
1085                 break;
1086
1087         case TN_EVENT_TAG_RX_FAIL:
1088                 kfilnd_tn_status_update(tn, status,
1089                                         LNET_MSG_STATUS_LOCAL_ERROR);
1090                 kfilnd_tn_state_change(tn, TN_STATE_FAIL);
1091                 break;
1092
1093         default:
1094                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1095                 LBUG();
1096         }
1097
1098         return 0;
1099 }
1100
1101 static int kfilnd_tn_state_wait_send_comp(struct kfilnd_transaction *tn,
1102                                           enum tn_events event, int status,
1103                                           bool *tn_released)
1104 {
1105         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1106                         status);
1107
1108         if (event == TN_EVENT_TX_OK) {
1109                 kfilnd_peer_alive(tn->tn_kp);
1110                 kfilnd_tn_finalize(tn, tn_released);
1111         } else {
1112                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1113                 LBUG();
1114         }
1115
1116         return 0;
1117 }
1118
1119 static int kfilnd_tn_state_wait_tag_rma_comp(struct kfilnd_transaction *tn,
1120                                              enum tn_events event, int status,
1121                                              bool *tn_released)
1122 {
1123         enum lnet_msg_hstatus hstatus;
1124
1125         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1126                         status);
1127
1128         switch (event) {
1129         case TN_EVENT_TAG_TX_OK:
1130                 kfilnd_peer_alive(tn->tn_kp);
1131                 break;
1132
1133         case TN_EVENT_TAG_TX_FAIL:
1134                 if (status == -ETIMEDOUT)
1135                         hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
1136                 else
1137                         hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
1138
1139                 kfilnd_tn_status_update(tn, status, hstatus);
1140                 kfilnd_peer_down(tn->tn_kp);
1141                 break;
1142
1143         default:
1144                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1145                 LBUG();
1146         }
1147
1148         kfilnd_tn_finalize(tn, tn_released);
1149
1150         return 0;
1151 }
1152
1153 static int kfilnd_tn_state_wait_tag_comp(struct kfilnd_transaction *tn,
1154                                          enum tn_events event, int status,
1155                                          bool *tn_released)
1156 {
1157         int rc;
1158         enum lnet_msg_hstatus hstatus;
1159
1160         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1161                         status);
1162
1163         switch (event) {
1164         case TN_EVENT_TAG_RX_FAIL:
1165         case TN_EVENT_TAG_RX_OK:
1166                 /* Status can be set for both TN_EVENT_TAG_RX_FAIL and
1167                  * TN_EVENT_TAG_RX_OK. For TN_EVENT_TAG_RX_OK, if status is set,
1168                  * LNet target returned -ENODATA.
1169                  */
1170                 if (status) {
1171                         if (event == TN_EVENT_TAG_RX_FAIL)
1172                                 kfilnd_tn_status_update(tn, status,
1173                                                         LNET_MSG_STATUS_LOCAL_ERROR);
1174                         else
1175                                 kfilnd_tn_status_update(tn, status,
1176                                                         LNET_MSG_STATUS_OK);
1177                 }
1178
1179                 if (!kfilnd_tn_timeout_cancel(tn)) {
1180                         kfilnd_tn_state_change(tn, TN_STATE_WAIT_TIMEOUT_COMP);
1181                         return 0;
1182                 }
1183                 break;
1184
1185         case TN_EVENT_TIMEOUT:
1186                 /* Need to cancel the tagged receive to prevent resources from
1187                  * being leaked.
1188                  */
1189                 rc = kfilnd_tn_cancel_tag_recv(tn);
1190
1191                 switch (rc) {
1192                 /* Async cancel event will progress transaction. */
1193                 case 0:
1194                         kfilnd_tn_state_change(tn,
1195                                                TN_STATE_WAIT_TIMEOUT_TAG_COMP);
1196                         return 0;
1197
1198                 /* Need to replay TN_EVENT_INIT_BULK event while in the
1199                  * TN_STATE_WAIT_TAG_COMP state.
1200                  */
1201                 case -EAGAIN:
1202                         KFILND_TN_DEBUG(tn,
1203                                         "Need to replay cancel tagged recv");
1204                         return -EAGAIN;
1205
1206                 default:
1207                         KFILND_TN_ERROR(tn,
1208                                         "Unexpected error during cancel tagged receive: rc=%d",
1209                                         rc);
1210                         LBUG();
1211                 }
1212                 break;
1213
1214         case TN_EVENT_TAG_TX_FAIL:
1215                 if (status == -ETIMEDOUT)
1216                         hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
1217                 else
1218                         hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
1219
1220                 kfilnd_tn_status_update(tn, status, hstatus);
1221                 kfilnd_peer_down(tn->tn_kp);
1222                 break;
1223
1224         case TN_EVENT_TAG_TX_OK:
1225                 kfilnd_peer_alive(tn->tn_kp);
1226                 break;
1227
1228         default:
1229                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1230                 LBUG();
1231         }
1232
1233         kfilnd_tn_finalize(tn, tn_released);
1234
1235         return 0;
1236 }
1237
1238 static int kfilnd_tn_state_fail(struct kfilnd_transaction *tn,
1239                                 enum tn_events event, int status,
1240                                 bool *tn_released)
1241 {
1242         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1243                         status);
1244
1245         switch (event) {
1246         case TN_EVENT_TX_FAIL:
1247                 kfilnd_peer_down(tn->tn_kp);
1248                 break;
1249
1250         case TN_EVENT_TX_OK:
1251                 kfilnd_peer_alive(tn->tn_kp);
1252                 break;
1253
1254         case TN_EVENT_TAG_RX_FAIL:
1255         case TN_EVENT_TAG_RX_CANCEL:
1256                 break;
1257
1258         default:
1259                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1260                 LBUG();
1261         }
1262
1263         kfilnd_tn_finalize(tn, tn_released);
1264
1265         return 0;
1266 }
1267
1268 static int kfilnd_tn_state_wait_timeout_tag_comp(struct kfilnd_transaction *tn,
1269                                                  enum tn_events event,
1270                                                  int status, bool *tn_released)
1271 {
1272         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1273                         status);
1274
1275         switch (event) {
1276         case TN_EVENT_TAG_RX_CANCEL:
1277                 kfilnd_tn_status_update(tn, -ETIMEDOUT,
1278                                         LNET_MSG_STATUS_REMOTE_TIMEOUT);
1279                 kfilnd_peer_down(tn->tn_kp);
1280                 break;
1281
1282         case TN_EVENT_TAG_RX_FAIL:
1283                 kfilnd_tn_status_update(tn, status,
1284                                         LNET_MSG_STATUS_LOCAL_ERROR);
1285                 break;
1286
1287         case TN_EVENT_TAG_RX_OK:
1288                 break;
1289
1290         default:
1291                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1292                 LBUG();
1293         }
1294
1295         kfilnd_tn_finalize(tn, tn_released);
1296
1297         return 0;
1298 }
1299
1300 static int kfilnd_tn_state_wait_timeout_comp(struct kfilnd_transaction *tn,
1301                                              enum tn_events event, int status,
1302                                              bool *tn_released)
1303 {
1304         KFILND_TN_DEBUG(tn, "%s event status %d", tn_event_to_str(event),
1305                         status);
1306
1307         if (event == TN_EVENT_TIMEOUT) {
1308                 kfilnd_tn_finalize(tn, tn_released);
1309         } else {
1310                 KFILND_TN_ERROR(tn, "Invalid %s event", tn_event_to_str(event));
1311                 LBUG();
1312         }
1313
1314         return 0;
1315 }
1316
1317 static int
1318 (* const kfilnd_tn_state_dispatch_table[TN_STATE_MAX])(struct kfilnd_transaction *tn,
1319                                                        enum tn_events event,
1320                                                        int status,
1321                                                        bool *tn_released) = {
1322         [TN_STATE_IDLE] = kfilnd_tn_state_idle,
1323         [TN_STATE_WAIT_TAG_COMP] = kfilnd_tn_state_wait_tag_comp,
1324         [TN_STATE_IMM_SEND] = kfilnd_tn_state_imm_send,
1325         [TN_STATE_TAGGED_RECV_POSTED] = kfilnd_tn_state_tagged_recv_posted,
1326         [TN_STATE_SEND_FAILED] = kfilnd_tn_state_send_failed,
1327         [TN_STATE_WAIT_COMP] = kfilnd_tn_state_wait_comp,
1328         [TN_STATE_WAIT_TIMEOUT_COMP] = kfilnd_tn_state_wait_timeout_comp,
1329         [TN_STATE_WAIT_SEND_COMP] = kfilnd_tn_state_wait_send_comp,
1330         [TN_STATE_WAIT_TIMEOUT_TAG_COMP] =
1331                 kfilnd_tn_state_wait_timeout_tag_comp,
1332         [TN_STATE_FAIL] = kfilnd_tn_state_fail,
1333         [TN_STATE_IMM_RECV] = kfilnd_tn_state_imm_recv,
1334         [TN_STATE_WAIT_TAG_RMA_COMP] = kfilnd_tn_state_wait_tag_rma_comp,
1335 };
1336
1337 /**
1338  * kfilnd_tn_event_handler() - Update transaction state machine with an event.
1339  * @tn: Transaction to be updated.
1340  * @event: Transaction event.
1341  * @status: Errno status associated with the event.
1342  *
1343  * When the transaction event handler is first called on a new transaction, the
1344  * transaction is now own by the transaction system. This means that will be
1345  * freed by the system as the transaction is progressed through the state
1346  * machine.
1347  */
1348 void kfilnd_tn_event_handler(struct kfilnd_transaction *tn,
1349                              enum tn_events event, int status)
1350 {
1351         bool tn_released = false;
1352         int rc;
1353
1354         if (!tn)
1355                 return;
1356
1357         mutex_lock(&tn->tn_lock);
1358         rc = kfilnd_tn_state_dispatch_table[tn->tn_state](tn, event, status,
1359                                                           &tn_released);
1360         if (rc == -EAGAIN) {
1361                 tn->replay_event = event;
1362                 tn->replay_status = status;
1363                 kfilnd_ep_queue_tn_replay(tn->tn_ep, tn);
1364         }
1365
1366         if (!tn_released)
1367                 mutex_unlock(&tn->tn_lock);
1368 }
1369
1370 /**
1371  * kfilnd_tn_free() - Free a transaction.
1372  */
1373 void kfilnd_tn_free(struct kfilnd_transaction *tn)
1374 {
1375         spin_lock(&tn->tn_ep->tn_list_lock);
1376         list_del(&tn->tn_entry);
1377         spin_unlock(&tn->tn_ep->tn_list_lock);
1378
1379         KFILND_TN_DEBUG(tn, "Transaction freed");
1380
1381         if (tn->tn_mr_key)
1382                 kfilnd_ep_put_key(tn->tn_ep, tn->tn_mr_key);
1383
1384         /* Free send message buffer if needed. */
1385         if (tn->tn_tx_msg.msg)
1386                 kmem_cache_free(imm_buf_cache, tn->tn_tx_msg.msg);
1387
1388         kmem_cache_free(tn_cache, tn);
1389 }
1390
1391 /**
1392  * kfilnd_tn_alloc() - Allocate a new KFI LND transaction.
1393  * @dev: KFI LND device used to look the KFI LND endpoint to associate with the
1394  * transaction.
1395  * @cpt: CPT of the transaction.
1396  * @target_nid: Target NID of the transaction.
1397  * @alloc_msg: Allocate an immediate message for the transaction.
1398  * @is_initiator: Is initiator of LNet transaction.
1399  * @key: Is transaction memory region key need.
1400  *
1401  * During transaction allocation, each transaction is associated with a KFI LND
1402  * endpoint use to post data transfer operations. The CPT argument is used to
1403  * lookup the KFI LND endpoint within the KFI LND device.
1404  *
1405  * Return: On success, valid pointer. Else, negative errno pointer.
1406  */
1407 struct kfilnd_transaction *kfilnd_tn_alloc(struct kfilnd_dev *dev, int cpt,
1408                                            lnet_nid_t target_nid,
1409                                            bool alloc_msg, bool is_initiator,
1410                                            bool key)
1411 {
1412         struct kfilnd_transaction *tn;
1413         struct kfilnd_peer *kp;
1414         int rc;
1415
1416         if (!dev) {
1417                 rc = -EINVAL;
1418                 goto err;
1419         }
1420
1421         kp = kfilnd_peer_get(dev, target_nid);
1422         if (IS_ERR(kp)) {
1423                 rc = PTR_ERR(kp);
1424                 goto err;
1425         }
1426
1427         tn = kfilnd_tn_alloc_for_peer(dev, cpt, kp, alloc_msg, is_initiator,
1428                                       key);
1429         if (IS_ERR(tn)) {
1430                 rc = PTR_ERR(tn);
1431                 kfilnd_peer_put(kp);
1432                 goto err;
1433         }
1434
1435         return tn;
1436
1437 err:
1438         return ERR_PTR(rc);
1439 }
1440
1441 /* See kfilnd_tn_alloc()
1442  * Note: Caller must have a reference on @kp
1443  */
1444 struct kfilnd_transaction *kfilnd_tn_alloc_for_peer(struct kfilnd_dev *dev,
1445                                                     int cpt,
1446                                                     struct kfilnd_peer *kp,
1447                                                     bool alloc_msg,
1448                                                     bool is_initiator,
1449                                                     bool key)
1450 {
1451         struct kfilnd_transaction *tn;
1452         struct kfilnd_ep *ep;
1453         int rc;
1454         ktime_t tn_alloc_ts;
1455
1456         if (!dev) {
1457                 rc = -EINVAL;
1458                 goto err;
1459         }
1460
1461         tn_alloc_ts = ktime_get();
1462
1463         /* If the CPT does not fall into the LNet NI CPT range, force the CPT
1464          * into the LNet NI CPT range. This should never happen.
1465          */
1466         ep = dev->cpt_to_endpoint[cpt];
1467         if (!ep) {
1468                 CWARN("%s used invalid cpt=%d\n",
1469                       libcfs_nidstr(&dev->kfd_ni->ni_nid), cpt);
1470                 ep = dev->kfd_endpoints[0];
1471         }
1472
1473         tn = kmem_cache_zalloc(tn_cache, GFP_KERNEL);
1474         if (!tn) {
1475                 rc = -ENOMEM;
1476                 goto err;
1477         }
1478
1479         if (alloc_msg) {
1480                 tn->tn_tx_msg.msg = kmem_cache_alloc(imm_buf_cache, GFP_KERNEL);
1481                 if (!tn->tn_tx_msg.msg) {
1482                         rc = -ENOMEM;
1483                         goto err_free_tn;
1484                 }
1485         }
1486
1487         if (key) {
1488                 rc = kfilnd_ep_get_key(ep);
1489                 if (rc < 0)
1490                         goto err_free_tn;
1491                 tn->tn_mr_key = rc;
1492         }
1493
1494         tn->tn_kp = kp;
1495         refcount_inc(&kp->kp_cnt);
1496
1497         mutex_init(&tn->tn_lock);
1498         tn->tn_ep = ep;
1499         tn->tn_response_rx = ep->end_context_id;
1500         tn->tn_state = TN_STATE_IDLE;
1501         tn->hstatus = LNET_MSG_STATUS_OK;
1502         tn->deadline = ktime_get_seconds() + lnet_get_lnd_timeout();
1503         tn->is_initiator = is_initiator;
1504         INIT_WORK(&tn->timeout_work, kfilnd_tn_timeout_work);
1505
1506         /* Add the transaction to an endpoint.  This is like
1507          * incrementing a ref counter.
1508          */
1509         spin_lock(&ep->tn_list_lock);
1510         list_add_tail(&tn->tn_entry, &ep->tn_list);
1511         spin_unlock(&ep->tn_list_lock);
1512
1513         tn->tn_alloc_ts = tn_alloc_ts;
1514         tn->tn_state_ts = ktime_get();
1515
1516         KFILND_EP_DEBUG(ep, "Transaction ID %u allocated", tn->tn_mr_key);
1517
1518         return tn;
1519
1520 err_free_tn:
1521         if (tn->tn_tx_msg.msg)
1522                 kmem_cache_free(imm_buf_cache, tn->tn_tx_msg.msg);
1523         kmem_cache_free(tn_cache, tn);
1524 err:
1525         return ERR_PTR(rc);
1526 }
1527
1528 /**
1529  * kfilnd_tn_cleanup() - Cleanup KFI LND transaction system.
1530  *
1531  * This function should only be called when there are no outstanding
1532  * transactions.
1533  */
1534 void kfilnd_tn_cleanup(void)
1535 {
1536         kmem_cache_destroy(imm_buf_cache);
1537         kmem_cache_destroy(tn_cache);
1538 }
1539
1540 /**
1541  * kfilnd_tn_init() - Initialize KFI LND transaction system.
1542  *
1543  * Return: On success, zero. Else, negative errno.
1544  */
1545 int kfilnd_tn_init(void)
1546 {
1547         tn_cache = kmem_cache_create("kfilnd_tn",
1548                                      sizeof(struct kfilnd_transaction), 0,
1549                                      SLAB_HWCACHE_ALIGN, NULL);
1550         if (!tn_cache)
1551                 goto err;
1552
1553         imm_buf_cache = kmem_cache_create("kfilnd_imm_buf",
1554                                           KFILND_IMMEDIATE_MSG_SIZE, 0,
1555                                           SLAB_HWCACHE_ALIGN, NULL);
1556         if (!imm_buf_cache)
1557                 goto err_tn_cache_destroy;
1558
1559         return 0;
1560
1561 err_tn_cache_destroy:
1562         kmem_cache_destroy(tn_cache);
1563 err:
1564         return -ENOMEM;
1565 }
1566
1567 /**
1568  * kfilnd_tn_set_kiov_buf() - Set the buffer used for a transaction.
1569  * @tn: Transaction to have buffer set.
1570  * @kiov: LNet KIOV buffer.
1571  * @num_iov: Number of IOVs.
1572  * @offset: Offset into IOVs where the buffer starts.
1573  * @len: Length of the buffer.
1574  *
1575  * This function takes the user provided IOV, offset, and len, and sets the
1576  * transaction buffer. The user provided IOV is an LNet KIOV. When the
1577  * transaction buffer is configured, the user provided offset is applied
1578  * when the transaction buffer is configured (i.e. the transaction buffer
1579  * offset is zero).
1580  */
1581 int kfilnd_tn_set_kiov_buf(struct kfilnd_transaction *tn,
1582                            struct bio_vec *kiov, size_t num_iov,
1583                            size_t offset, size_t len)
1584 {
1585         size_t i;
1586         size_t cur_len = 0;
1587         size_t cur_offset = offset;
1588         size_t cur_iov = 0;
1589         size_t tmp_len;
1590         size_t tmp_offset;
1591
1592         for (i = 0; (i < num_iov) && (cur_len < len); i++) {
1593                 /* Skip KIOVs until a KIOV with a length less than the current
1594                  * offset is found.
1595                  */
1596                 if (kiov[i].bv_len <= cur_offset) {
1597                         cur_offset -= kiov[i].bv_len;
1598                         continue;
1599                 }
1600
1601                 tmp_len = kiov[i].bv_len - cur_offset;
1602                 tmp_offset = kiov[i].bv_len - tmp_len + kiov[i].bv_offset;
1603
1604                 if (tmp_len + cur_len > len)
1605                         tmp_len = len - cur_len;
1606
1607                 /* tn_kiov is an array of size LNET_MAX_IOV */
1608                 if (cur_iov >= LNET_MAX_IOV)
1609                         return -EINVAL;
1610
1611                 tn->tn_kiov[cur_iov].bv_page = kiov[i].bv_page;
1612                 tn->tn_kiov[cur_iov].bv_len = tmp_len;
1613                 tn->tn_kiov[cur_iov].bv_offset = tmp_offset;
1614
1615                 cur_iov++;
1616                 cur_len += tmp_len;
1617                 cur_offset = 0;
1618         }
1619
1620         tn->tn_num_iovec = cur_iov;
1621         tn->tn_nob = cur_len;
1622
1623         return 0;
1624 }