Debian Codegraph @

prov/opx/src/fi_opx_hfi1_sdma.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2022-2025 by Cornelis Networks.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include <assert.h>
34 #include <string.h>
35 #include "rdma/opx/fi_opx_hfi1_sdma.h"
36
37 void fi_opx_hfi1_sdma_hit_zero(struct fi_opx_completion_counter *cc)
38 {
39 assert(cc->byte_counter == 0);
40 struct fi_opx_hfi1_dput_params *params = (struct fi_opx_hfi1_dput_params *) cc->container;
41 if (params->work_elem.complete) {
42 FI_WARN(&fi_opx_provider, FI_LOG_EP_DATA,
43 "SDMA Work Entry hit zero more than once! cc->byte_counter = %ld\n", cc->byte_counter);
44 assert(0);
45 return;
46 }
47
48 assert(params->sdma_we == NULL || !fi_opx_hfi1_sdma_has_unsent_packets(params->sdma_we));
49
50 assert(!cc->next);
51
52 // Set the work element to complete so it can be removed from the work pending queue and freed
53 params->work_elem.complete = true;
54 FI_DBG_TRACE(fi_opx_global.prov, FI_LOG_EP_DATA, "=================== SDMA HIT ZERO\n");
55 }
56
57 void fi_opx_hfi1_sdma_bounce_buf_hit_zero(struct fi_opx_completion_counter *cc)
58 {
59 assert(cc->byte_counter == 0);
60 struct fi_opx_hfi1_sdma_work_entry *sdma_we = (struct fi_opx_hfi1_sdma_work_entry *) cc->container;
61 assert(sdma_we->pending_bounce_buf);
62 sdma_we->pending_bounce_buf = false;
63
64 if (cc->next) {
65 assert(cc->next->byte_counter >= cc->initial_byte_count);
66 cc->next->byte_counter -= cc->initial_byte_count;
67 if (cc->next->byte_counter == 0) {
68 cc->next->hit_zero(cc->next);
69 }
70 cc->next = NULL;
71 }
72 }
73
74 int fi_opx_hfi1_dput_sdma_pending_completion(union fi_opx_hfi1_deferred_work *work)
75 {
76 struct fi_opx_hfi1_dput_params *params = &work->dput;
77 struct fi_opx_ep *opx_ep = params->opx_ep;
78
79 assert(params->work_elem.work_type == OPX_WORK_TYPE_LAST);
80
81 struct fi_opx_hfi1_sdma_work_entry *we = (struct fi_opx_hfi1_sdma_work_entry *) params->sdma_reqs.head;
82 while (we) {
83 // If we're using the SDMA WE bounce buffer, we need to wait for
84 // the hit_zero to mark the work element as complete. The replay
85 // iovecs are pointing to the SDMA WE bounce buffers, so we can't
86 // free the SDMA WEs until the replays are cleared.
87 if (!params->work_elem.complete && we->use_bounce_buf) {
88 FI_OPX_DEBUG_COUNTERS_INC(work->dput.opx_ep->debug_counters.sdma.eagain_pending_dc);
89 return -FI_EAGAIN;
90 }
91
92 if (we->comp_state == OPX_SDMA_COMP_PENDING_WRITEV || we->comp_state == OPX_SDMA_COMP_QUEUED) {
93 FI_OPX_DEBUG_COUNTERS_INC(opx_ep->debug_counters.sdma.eagain_pending_writev);
94 FI_DBG_TRACE(fi_opx_global.prov, FI_LOG_EP_DATA, "FI_EAGAIN\n");
95 return -FI_EAGAIN;
96 }
97 assert(we->comp_state == OPX_SDMA_COMP_COMPLETE || we->comp_state == OPX_SDMA_COMP_ERROR);
98
99 slist_remove_head(&params->sdma_reqs);
100 we->next = NULL;
101 fi_opx_hfi1_sdma_return_we(opx_ep, we);
102 we = (struct fi_opx_hfi1_sdma_work_entry *) params->sdma_reqs.head;
103 }
104
105 assert(slist_empty(&params->sdma_reqs));
106
107 if (!params->work_elem.complete) {
108 assert(params->sdma_no_bounce_buf);
109 FI_OPX_DEBUG_COUNTERS_INC(work->dput.opx_ep->debug_counters.sdma.eagain_pending_dc);
110 return -FI_EAGAIN;
111 }
112
113 if (params->origin_byte_counter) {
114 // If we're not doing delivery_competion, then origin_byte_counter
115 // should have already been zero'd and NULL'd at the end of do_dput_sdma(...)
116 assert(params->sdma_no_bounce_buf);
117 assert((*params->origin_byte_counter) >= params->origin_bytes_sent);
118 *params->origin_byte_counter -= params->origin_bytes_sent;
119 params->origin_byte_counter = NULL;
120 }
121
122 if (params->user_cc) {
123 assert(params->user_cc->byte_counter >= params->cc->initial_byte_count);
124 params->user_cc->byte_counter -= params->cc->initial_byte_count;
125 if (params->user_cc->byte_counter == 0) {
126 params->user_cc->hit_zero(params->user_cc);
127 }
128 }
129 OPX_BUF_FREE(params->cc);
130
131 FI_DBG_TRACE(fi_opx_global.prov, FI_LOG_EP_DATA,
132 "===================================== PENDING DPUT %u COMPLETE\n", work->work_elem.complete);
133 return FI_SUCCESS;
134 }
135
136 void fi_opx_hfi1_sdma_handle_errors(struct fi_opx_ep *opx_ep, int writev_rc, struct iovec *iovs, const int num_iovs,
137 const char *file, const char *func, const int line)
138 {
139 const pid_t pid = getpid();
140
141 if (errno == ECOMM || errno == EINTR || errno == EFAULT) {
142 int err = fi_opx_context_check_status(opx_ep->hfi, OPX_HFI1_TYPE, opx_ep);
143 if (err != FI_SUCCESS) {
144 FI_WARN(fi_opx_global.prov, FI_LOG_EP_DATA, "Link down detected\n");
145 return;
146 }
147 }
148
149 fprintf(stderr, "(%d) %s:%s():%d ERROR: SDMA Abort errno=%d (%s)\n", pid, file, func, line, errno,
150 strerror(errno));
151 fprintf(stderr,
152 "(%d) ===================================== SDMA_WE -- "
153 "called writev rc=%d Params were: "
154 "fd=%d iovecs=%p num_iovs=%d \n",
155 pid, writev_rc, opx_ep->hfi->fd, iovs, num_iovs);
156 fprintf(stderr, "(%d) hfi->info.sdma.queue_size == %0hu\n", pid, opx_ep->hfi->info.sdma.queue_size);
157 fprintf(stderr, "(%d) hfi->info.sdma.fill_index == %0hu\n", pid, opx_ep->hfi->info.sdma.fill_index);
158 fprintf(stderr, "(%d) hfi->info.sdma.done_index == %0hu\n", pid, opx_ep->hfi->info.sdma.done_index);
159 fprintf(stderr, "(%d) hfi->info.sdma.available == %0hu\n", pid, opx_ep->hfi->info.sdma.available_counter);
160 fprintf(stderr, "(%d) hfi->info.sdma.completion_queue == %p\n", pid, opx_ep->hfi->info.sdma.completion_queue);
161
162 struct iovec *iov_ptr = iovs;
163 int req_num = 0;
164
165 do {
166 struct fi_opx_hfi1_sdma_header_vec *header_vec =
167 (struct fi_opx_hfi1_sdma_header_vec *) iov_ptr[0].iov_base;
168 size_t header_vec_len = iov_ptr[0].iov_len;
169 uint8_t meminfo_set = (header_vec_len == OPX_SDMA_REQ_HDR_SIZE[1]) ? 1 : 0;
170 struct sdma_req_info *req_info = OPX_SDMA_REQ_INFO_PTR(header_vec, meminfo_set);
171 uint8_t req_info_iovs = (req_info->ctrl >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK;
172 uint8_t req_info_meminfo_set =
173 #ifdef OPX_HMEM
174 (req_info->ctrl >> HFI1_SDMA_REQ_MEMINFO_SHIFT) & HFI1_SDMA_REQ_MEMINFO_MASK;
175 #else
176 0;
177 #endif
178 uint8_t req_info_opcode = (req_info->ctrl >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK;
179
180 fprintf(stderr,
181 "(%d) [%d] header_vec=%p len=%lu meminfo_set=%hhu req_meminfo_set=%hhu req_iovs=%hhu req_opcode=%hhu/(%s)\n",
182 pid, req_num, header_vec, header_vec_len, meminfo_set, req_info_meminfo_set, req_info_iovs,
183 req_info_opcode, (req_info_opcode == EAGER) ? "EAGER (non-TID)" : "EXPECTED (TID)");
184 fprintf(stderr, "(%d) [%d] req.npkts=%hu req.fragsize=%hu req.cmp_idx=%hu req.ctrl=%#04hX\n", pid,
185 req_num, req_info->npkts, req_info->fragsize, req_info->comp_idx, req_info->ctrl);
186
187 enum fi_hmem_iface hmem_iface = FI_HMEM_SYSTEM;
188 uint64_t hmem_device = 0ul;
189 #ifdef OPX_HMEM
190 if (meminfo_set) {
191 struct sdma_req_meminfo *meminfo = (struct sdma_req_meminfo *) (req_info + 1);
192
193 // We currently only ever use one payload IOV that would require
194 // setting meminfo, and it will be the fist one:
195 // index 0 (the first payload IOV, or iov[1]).
196 const unsigned meminfo_idx = 0;
197 const unsigned hfi1_mem_type = HFI1_MEMINFO_TYPE_ENTRY_GET(meminfo->types, meminfo_idx);
198 hmem_iface = (enum fi_hmem_iface) OPX_HMEM_OFI_MEM_TYPE[hfi1_mem_type];
199 hmem_device = meminfo->context[0];
200 fprintf(stderr,
201 "(%d) [%d] hmem_iface=%u hmem_device=%lu meminfo->types=%#16.16llX meminfo->context[0]=%#16.16llX meminfo->context[15]=%#16.16llX\n",
202 pid, req_num, hmem_iface, hmem_device, meminfo->types, meminfo->context[0],
203 meminfo->context[15]);
204 }
205 #endif
206 fprintf(stderr, "(%d) [%d] PBC: %#16.16lX\n", pid, req_num, header_vec->scb.scb_9B.qw0);
207
208 if (OPX_HFI1_TYPE & (OPX_HFI1_WFR | OPX_HFI1_JKR_9B)) {
209 fi_opx_hfi1_dump_packet_hdr(&header_vec->scb.scb_9B.hdr, OPX_HFI1_TYPE, func, line);
210 } else {
211 fi_opx_hfi1_dump_packet_hdr(&header_vec->scb.scb_16B.hdr, OPX_HFI1_TYPE, func, line);
212 }
213
214 fprintf(stderr, "(%d) [%d] req data iov=%p len=%lu\n", pid, req_num, iov_ptr[1].iov_base,
215 iov_ptr[1].iov_len);
216
217 if (hmem_iface == FI_HMEM_SYSTEM) {
218 fprintf(stderr, "(%d) [%d] First 8 bytes of %p == %#16.16lX\n", pid, req_num,
219 iov_ptr[1].iov_base, *((uint64_t *) iov_ptr[1].iov_base));
220 } else {
221 uint64_t first_qw;
222 opx_copy_from_hmem(hmem_iface, hmem_device, OPX_HMEM_NO_HANDLE, &first_qw, iov_ptr[1].iov_base,
223 sizeof(uint64_t), OPX_HMEM_DEV_REG_THRESHOLD_NOT_SET);
224 fprintf(stderr, "(%d) [%d] First 8 bytes of %p == %#16.16lX\n", pid, req_num,
225 iov_ptr[1].iov_base, first_qw);
226 }
227
228 if (req_info_iovs > 2) {
229 if (req_info_opcode == EAGER) {
230 fprintf(stderr,
231 "(%d) [%d] ERROR: Request specifies 3 IOVs, but opcode is set to EAGER!\n", pid,
232 req_num);
233 }
234 fprintf(stderr, "(%d) [%d] tid iov=%p len=%lu pairs=%lu\n", pid, req_num, iov_ptr[2].iov_base,
235 iov_ptr[2].iov_len, iov_ptr[2].iov_len / sizeof(uint32_t));
236 if (iov_ptr[2].iov_len < sizeof(uint32_t)) {
237 fprintf(stderr,
238 "(%d) [%d] ERROR: Request opcode is set to EXPECTED (TID), but TID IOV's length is < minimum!\n",
239 pid, req_num);
240 }
241 uint32_t kdeth = (uint32_t) (header_vec->scb.scb_9B.hdr.qw_9B[2] >> 32);
242 uint32_t tidctrl = (kdeth >> FI_OPX_HFI1_KDETH_TIDCTRL_SHIFT) & FI_OPX_HFI1_KDETH_TIDCTRL;
243 uint32_t tididx = (kdeth >> FI_OPX_HFI1_KDETH_TID_SHIFT) & FI_OPX_HFI1_KDETH_TID;
244 uint32_t tidOMshift = (kdeth >> KDETH_OM_SHIFT) & KDETH_OM_MASK;
245 uint32_t tidoffset = (kdeth >> KDETH_OFFSET_SHIFT) & KDETH_OFFSET_MASK;
246 uint32_t actual_offset = tidoffset
247 << (tidOMshift ? KDETH_OM_LARGE_SHIFT : KDETH_OM_SMALL_SHIFT);
248
249 fprintf(stderr,
250 "(%d) [%d] kdeth=%08X tidctrl=%08X tididx=%08X tidOMshift=%08X tidoffset=%08X actual offset=%08X\n",
251 pid, req_num, kdeth, tidctrl, tididx, tidOMshift, tidoffset, actual_offset);
252
253 uint32_t *tidpairs = (uint32_t *) iov_ptr[2].iov_base;
254 for (int j = 0; j < (iov_ptr[2].iov_len / sizeof(uint32_t)); ++j) {
255 fprintf(stderr, "(%d) [%d] tid [%u]=%#8.8X LEN %u, CTRL %u, IDX %u\n", pid, req_num,
256 j, tidpairs[j], (int) FI_OPX_EXP_TID_GET((tidpairs[j]), LEN),
257 (int) FI_OPX_EXP_TID_GET((tidpairs[j]), CTRL),
258 (int) FI_OPX_EXP_TID_GET((tidpairs[j]), IDX));
259 }
260 } else if (req_info_opcode == EXPECTED) {
261 fprintf(stderr,
262 "(%d) [%d] ERROR: Request opcode is set to EXPECTED (TID), but only has 2 IOVs (missing TID IOV)!\n",
263 pid, req_num);
264 }
265
266 #ifdef OPX_SDMA_DEBUG
267 ssize_t retry_rc = writev(opx_ep->hfi->fd, iov_ptr, req_info_iovs);
268
269 if (retry_rc > 0) {
270 fprintf(stderr, "(%d) [%d] Retry succeeded!\n", pid, req_num);
271 } else {
272 fprintf(stderr, "(%d) [%d] Retry FAILED retry_rc=%ld errno=%d (%s)\n", pid, req_num, retry_rc,
273 errno, strerror(errno));
274 }
275 #endif
276 ++req_num;
277 iov_ptr += req_info_iovs;
278 } while (iov_ptr < (iovs + num_iovs));
279
280 FI_WARN(&fi_opx_provider, FI_LOG_FABRIC, "SDMA Error, not handled, aborting\n");
281 abort();
282 }
283
284 void opx_hfi1_sdma_process_pending(struct fi_opx_ep *opx_ep)
285 {
286 struct slist *queue = &opx_ep->tx->sdma_pending_queue;
287
288 struct opx_sdma_request *request = (struct opx_sdma_request *) queue->head;
289 while (request && request->comp_entry.status != QUEUED) {
290 slist_remove_head(queue);
291 assert(request->fill_index != OPX_SDMA_FILL_INDEX_INVALID);
292 assert(*request->comp_state == OPX_SDMA_COMP_QUEUED);
293 if (OFI_UNLIKELY(request->comp_entry.status == ERROR)) {
294 FI_DBG_TRACE(
295 fi_opx_global.prov, FI_LOG_EP_DATA,
296 "===================================== SDMA Request (%p) -- Found error in queued entry, status=%d, error=%d\n",
297 request, request->comp_entry.status, request->comp_entry.errcode);
298 *request->comp_state = OPX_SDMA_COMP_ERROR;
299 } else {
300 assert(request->comp_entry.status == COMPLETE);
301 *request->comp_state = OPX_SDMA_COMP_COMPLETE;
302 }
303 OPX_BUF_FREE(request);
304 request = (struct opx_sdma_request *) queue->head;
305 }
306 }
307
308 __OPX_FORCE_INLINE__
309 int opx_hfi1_sdma_writev(struct fi_opx_ep *opx_ep, struct iovec *iovecs, int iovs_used, uint16_t avail,
310 uint16_t fill_index, const char *file, const char *func, const int line)
311 {
312 opx_ep->hfi->info.sdma.fill_index = fill_index;
313 opx_ep->hfi->info.sdma.available_counter = avail;
314
315 #ifdef OPX_DEBUG_COUNTERS_SDMA
316 FI_OPX_DEBUG_COUNTERS_INC(opx_ep->debug_counters.sdma.writev_count);
317
318 union fi_opx_timer_stamp timestamp;
319 uint64_t writev_start_ns = fi_opx_timer_now(&timestamp, &opx_ep->reliability->state.service->tx.timer);
320 #endif
321
322 OPX_TRACER_TRACE(OPX_TRACER_BEGIN, "WRITEV");
323 ssize_t writev_rc = writev(opx_ep->hfi->fd, iovecs, iovs_used);
324 OPX_TRACER_TRACE(OPX_TRACER_END_SUCCESS, "WRITEV");
325
326 #ifdef OPX_DEBUG_COUNTERS_SDMA
327 uint64_t writev_end_ns = fi_opx_timer_now(&timestamp, &opx_ep->reliability->state.service->tx.timer);
328
329 uint64_t writev_time_ns = writev_end_ns - writev_start_ns;
330
331 FI_OPX_DEBUG_COUNTERS_INC_N(writev_time_ns, opx_ep->debug_counters.sdma.writev_time_ns_total);
332 FI_OPX_DEBUG_COUNTERS_MAX_OF(opx_ep->debug_counters.sdma.writev_time_ns_max, writev_time_ns);
333 FI_OPX_DEBUG_COUNTERS_MIN_OF(opx_ep->debug_counters.sdma.writev_time_ns_min, writev_time_ns);
334 #endif
335
336 if (writev_rc <= 0) {
337 fi_opx_hfi1_sdma_handle_errors(opx_ep, writev_rc, iovecs, iovs_used, file, func, line);
338 }
339
340 FI_OPX_DEBUG_COUNTERS_INC(opx_ep->debug_counters.sdma.writev_calls[iovs_used]);
341 return (writev_rc);
342 }
343
344 void opx_hfi1_sdma_process_requests(struct fi_opx_ep *opx_ep)
345 {
346 FI_OPX_DEBUG_COUNTERS_INC(opx_ep->debug_counters.sdma.proc_reqs_calls);
347
348 if (!opx_ep->hfi->info.sdma.available_counter) {
349 FI_OPX_DEBUG_COUNTERS_INC(opx_ep->debug_counters.sdma.proc_reqs_no_fill_slots);
350 return;
351 }
352
353 struct opx_sdma_queue *queue = &opx_ep->tx->sdma_request_queue;
354 assert(!slist_empty(&queue->list));
355
356 struct iovec iovecs[OPX_SDMA_HFI_MAX_IOVS_PER_WRITE];
357 int iovs_used = 0;
358 int iovs_free = OPX_SDMA_HFI_MAX_IOVS_PER_WRITE;
359 uint16_t avail = opx_ep->hfi->info.sdma.available_counter;
360 uint16_t fill_index = opx_ep->hfi->info.sdma.fill_index;
361
362 while (!slist_empty(&queue->list) && avail) {
363 struct opx_sdma_request *request = (struct opx_sdma_request *) slist_remove_head(&queue->list);
364 assert(*request->comp_state == OPX_SDMA_COMP_PENDING_WRITEV);
365 assert(request->fill_index == OPX_SDMA_FILL_INDEX_INVALID);
366
367 #ifdef OPX_SDMA_DEBUG
368 // For debugging SDMA issues, issue one writev per request, so
369 // that in the event of a failure, it's easy to identify the
370 // offending request.
371 if (iovs_used)
372 #else
373 if (iovs_free < request->num_iovs)
374 #endif
375 {
376 int err = opx_hfi1_sdma_writev(opx_ep, iovecs, iovs_used, avail, fill_index, __FILE__, __func__,
377 __LINE__);
378 if (err < 0) {
379 /* Error occured in writev. Add the request back to queue */
380 slist_insert_head((struct slist_entry *) request, &queue->list);
381 return;
382 }
383 iovs_used = 0;
384 iovs_free = OPX_SDMA_HFI_MAX_IOVS_PER_WRITE;
385 }
386
387 struct sdma_req_info *req_info = OPX_SDMA_REQ_INFO_PTR(&request->header_vec, request->set_meminfo);
388 req_info->comp_idx = fill_index;
389 request->fill_index = fill_index;
390 OPX_TRACER_TRACE_SDMA(OPX_TRACER_BEGIN, "SDMA_COMPLETE_%hu", fill_index);
391
392 assert(opx_ep->hfi->info.sdma.queued_entries[fill_index] == NULL);
393 request->comp_entry.status = QUEUED;
394 request->comp_entry.errcode = 0;
395 opx_ep->hfi->info.sdma.queued_entries[fill_index] = &request->comp_entry;
396
397 fill_index = (fill_index + 1) % (opx_ep->hfi->info.sdma.queue_size);
398 --avail;
399
400 for (int i = 0; i < request->num_iovs; ++i) {
401 iovecs[iovs_used + i] = request->iovecs[i];
402 }
403 iovs_used += request->num_iovs;
404 iovs_free -= request->num_iovs;
405 queue->num_iovs -= request->num_iovs;
406 --queue->num_reqs;
407
408 *(request->comp_state) = OPX_SDMA_COMP_QUEUED;
409
410 slist_insert_tail((struct slist_entry *) request, &opx_ep->tx->sdma_pending_queue);
411 }
412
413 assert(iovs_used);
414
415 opx_hfi1_sdma_writev(opx_ep, iovecs, iovs_used, avail, fill_index, __FILE__, __func__, __LINE__);
416
417 queue->slots_avail = avail;
418 }