Debian Codegraph @

ptl_am/ptl.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2013. Intel Corporation. All rights reserved.
3 * Copyright (c) 2006-2012. QLogic Corporation. All rights reserved.
4 * Copyright (c) 2003-2006, PathScale, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #include "psm_user.h"
36 #include "psm_mq_internal.h"
37 #include "psm_am_internal.h"
38 #include "kcopyrw.h"
39 #include "knemrw.h"
40 #include "scifrw.h"
41
42 static
43 psm_error_t
44 ptl_handle_rtsmatch_request(psm_mq_req_t req, int was_posted, amsh_am_token_t *tok)
45 {
46 psm_amarg_t args[5] = {};
47 psm_epaddr_t epaddr = req->rts_peer;
48 ptl_t *ptl = epaddr->ptl;
49 int pid = 0;
50 int used_get = 0;
51
52 psmi_assert((tok != NULL && was_posted) || (tok == NULL && !was_posted));
53
54 _IPATH_VDBG("[shm][rndv][recv] req=%p dest=%p len=%d tok=%p\n",
55 req, req->buf, req->recv_msglen, tok);
56
57 args[0].u64w0 = (uint64_t)(uintptr_t) req->ptl_req_ptr;
58 args[1].u64w0 = (uint64_t)(uintptr_t) req;
59 args[2].u64w0 = (uint64_t)(uintptr_t) req->buf;
60 args[3].u32w0 = req->recv_msglen;
61 args[3].u32w1 = tok != NULL ? 1 : 0;
62 args[4].u64w0 = 0;
63
64 /* First check: is the peer local? */
65 #ifdef PSM_HAVE_SCIF
66 int shmidx = epaddr->_shmidx;
67 if(shmidx < PTL_AMSH_MAX_LOCAL_PROCS) {
68 #endif
69 /* Use kassist if enabled */
70 if ((ptl->ep->psmi_kassist_mode & PSMI_KASSIST_GET) &&
71 req->recv_msglen > 0 &&
72 (pid = psmi_epaddr_kcopy_pid(epaddr)))
73 {
74 if (ptl->ep->psmi_kassist_mode & PSMI_KASSIST_KCOPY) {
75 /* kcopy can be done in handler context or not. */
76 size_t nbytes = kcopy_get(ptl->ep->psmi_kassist_fd, pid,
77 (void *) req->rts_sbuf, req->buf, req->recv_msglen);
78 psmi_assert_always(nbytes == req->recv_msglen);
79 } else {
80 psmi_assert_always(ptl->ep->psmi_kassist_mode &
81 PSMI_KASSIST_KNEM);
82
83 /* knem copy can be done in handler context or not */
84 knem_get(ptl->ep->psmi_kassist_fd, (int64_t) req->rts_sbuf,
85 (void*) req->buf, req->recv_msglen);
86 }
87
88 used_get = 1;
89 }
90
91 /* If KNEM PUT is active register region for peer to PUT data to */
92 if (ptl->ep->psmi_kassist_mode == PSMI_KASSIST_KNEM_PUT)
93 args[4].u64w0 = knem_register_region(req->buf, req->recv_msglen,
94 PSMI_TRUE);
95
96 #ifdef PSM_HAVE_SCIF
97 } else if(ptl->ep->scif_dma_threshold <= req->recv_msglen) {
98 /* Remote node and threshold is met, consider using SCIF DMA */
99
100 if(epaddr->ep->scif_dma_mode == PSMI_SCIF_DMA_GET) {
101 /* Read via SCIF DMA */
102 scif_epd_t epd = epaddr->ep->amsh_qdir[shmidx].amsh_epd[1];
103
104 if(scif_vreadfrom(epd, req->buf, req->recv_msglen,
105 req->rts_sbuf, SCIF_RMA_USECACHE|SCIF_RMA_SYNC)) {
106 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
107 "ptl_handle_rtsmatch_request(): scif_vreadfrom failed: (%d) %s",
108 errno, strerror(errno));
109 }
110
111 /* Give the remote offset back to the sender. */
112 args[4].u64w0 = req->rts_sbuf;
113 used_get = 1;
114 }
115 else if(epaddr->ep->scif_dma_mode == PSMI_SCIF_DMA_PUT) {
116 /* Peer issues DMA commands on amsh_epd[0] */
117 scif_epd_t epd = epaddr->ep->amsh_qdir[shmidx].amsh_epd[1];
118
119 off_t reg;
120 if(scif_register_region(epd,
121 req->buf, req->recv_msglen, &reg) != PSM_OK) {
122 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
123 "ptl_handle_rtsmatch_request(): SCIF memory registration failed");
124 }
125
126 /* Stuff the SCIF registration offset into the buffer pointer.
127 This is needed later in psmi_am_mq_handler_rtsdone to unregister
128 the buffer. The registration is also passed across for the
129 sender side to issue a DMA write.*/
130 req->buf = (void*)reg;
131 args[4].u64w0 = reg;
132 }
133 }
134 #endif
135
136 if (tok != NULL) {
137 psmi_am_reqq_add(AMREQUEST_SHORT, tok->ptl, tok->tok.epaddr_from,
138 mq_handler_rtsmatch_hidx, args, 5, NULL, 0, NULL, 0);
139 }
140 else
141 psmi_amsh_short_request(ptl, epaddr, mq_handler_rtsmatch_hidx,
142 args, 5, NULL, 0, 0);
143
144 /* 0-byte completion or we used kcopy */
145 if (used_get == 1 || req->recv_msglen == 0)
146 psmi_mq_handle_rts_complete(req);
147 return PSM_OK;
148 }
149
150 static
151 psm_error_t
152 ptl_handle_rtsmatch(psm_mq_req_t req, int was_posted)
153 {
154 /* was_posted == 0 allows us to assume that we're not running this callback
155 * within am handler context (i.e. we can poll) */
156 psmi_assert(was_posted == 0);
157 return ptl_handle_rtsmatch_request(req, 0, NULL);
158 }
159
160 void
161 psmi_am_mq_handler(void *toki, psm_amarg_t *args, int narg, void *buf, size_t len)
162 {
163 amsh_am_token_t *tok = (amsh_am_token_t *) toki;
164 ptl_t *ptl = tok->ptl;
165 psm_mq_req_t req;
166 int rc;
167 int mode = args[0].u32w0;
168 uint64_t tag = args[1].u64;
169 uint32_t msglen = mode <= MQ_MSG_SHORT ? len : args[0].u32w1;
170
171 _IPATH_VDBG("mq=%p mode=%d, len=%d, msglen=%d\n",
172 tok->mq, mode, (int) len, msglen);
173
174 switch(mode) {
175 case MQ_MSG_TINY:
176 rc = psmi_mq_handle_tiny_envelope(tok->mq, tok->tok.epaddr_from, tag,
177 buf, (uint32_t) len);
178 return;
179 break;
180 case MQ_MSG_SHORT:
181 case MQ_MSG_LONG:
182 rc = psmi_mq_handle_envelope(tok->mq, mode, tok->tok.epaddr_from,
183 tag, (union psmi_egrid) 0U,
184 msglen, buf, (uint32_t) len);
185 return;
186 break;
187 default: {
188 void *sreq = (void *)(uintptr_t) args[2].u64w0;
189 uintptr_t sbuf = (uintptr_t) args[3].u64w0;
190 psmi_assert(narg == 5);
191 psmi_assert_always(mode == MQ_MSG_RTS);
192 rc = psmi_mq_handle_rts(tok->mq, tag, sbuf, msglen,
193 tok->tok.epaddr_from,
194 ptl_handle_rtsmatch, &req);
195 req->ptl_req_ptr = sreq;
196
197 /* Overload rts_sbuf to contain the cookie for remote region */
198 if(ptl->ep->psmi_kassist_mode & PSMI_KASSIST_KNEM)
199 req->rts_sbuf = (uintptr_t) args[4].u64w0;
200 #ifdef PSM_HAVE_SCIF
201 else if(ptl->ep->scif_dma_mode == PSMI_SCIF_DMA_GET &&
202 ptl->ep->scif_dma_threshold <= msglen &&
203 tok->tok.epaddr_from->_shmidx >= PTL_AMSH_MAX_LOCAL_PROCS) {
204 req->rts_sbuf = (uintptr_t) args[4].u64w0;
205 }
206 #endif
207
208 if (rc == MQ_RET_MATCH_OK) /* handler context: issue a reply */
209 ptl_handle_rtsmatch_request(req, 1, tok);
210 /* else will be called later */
211 }
212 }
213 return;
214 }
215
216 void
217 psmi_am_mq_handler_data(void *toki, psm_amarg_t *args, int narg, void *buf, size_t len)
218 {
219 amsh_am_token_t *tok = (amsh_am_token_t *) toki;
220 psm_mq_req_t req = STAILQ_FIRST(&tok->tok.epaddr_from->egrlong);
221 psmi_mq_handle_data(req, tok->tok.epaddr_from, 0, args[2].u32w0, buf, len);
222
223 return;
224 }
225
226 void
227 psmi_am_mq_handler_rtsmatch(void *toki, psm_amarg_t *args, int narg, void *buf, size_t len)
228 {
229 amsh_am_token_t *tok = (amsh_am_token_t *) toki;
230 ptl_t *ptl = tok->ptl;
231 psm_mq_req_t sreq = (psm_mq_req_t) (uintptr_t) args[0].u64w0;
232 void *dest = (void *)(uintptr_t) args[2].u64w0;
233 uint32_t msglen = args[3].u32w0;
234 int pid = 0;
235 psm_amarg_t rarg[1] = {};
236
237 _IPATH_VDBG("[rndv][send] req=%p dest_req=%p src=%p dest=%p len=%d\n",
238 sreq, (void*)(uintptr_t)args[1].u64w0, sreq->buf, dest, msglen);
239
240 if (msglen > 0) {
241 rarg[0].u64w0 = args[1].u64w0; /* rreq */
242
243 #ifdef PSM_HAVE_SCIF
244 int shmidx = tok->tok.epaddr_from->_shmidx;
245 if(shmidx < PTL_AMSH_MAX_LOCAL_PROCS) {
246 #endif
247 /* Try Intra-node kassist */
248 if (ptl->ep->psmi_kassist_mode & PSMI_KASSIST_MASK)
249 pid = psmi_epaddr_kcopy_pid(tok->tok.epaddr_from);
250 else
251 pid = 0;
252
253 if (!pid)
254 psmi_amsh_long_reply(tok, mq_handler_rtsdone_hidx, rarg, 1,
255 sreq->buf, msglen, dest, 0);
256 else if (ptl->ep->psmi_kassist_mode & PSMI_KASSIST_PUT)
257 {
258 if (ptl->ep->psmi_kassist_mode & PSMI_KASSIST_KCOPY) {
259 size_t nbytes = kcopy_put(ptl->ep->psmi_kassist_fd, sreq->buf,
260 pid, dest, msglen);
261 psmi_assert_always(nbytes == msglen);
262 } else {
263 int64_t cookie = args[4].u64w0;
264
265 psmi_assert_always(
266 ptl->ep->psmi_kassist_mode & PSMI_KASSIST_KNEM);
267
268 /* Do a PUT using KNEM */
269 knem_put(ptl->ep->psmi_kassist_fd,
270 sreq->buf, msglen, cookie);
271 }
272
273 /* Send response that PUT is complete */
274 psmi_amsh_short_reply(tok, mq_handler_rtsdone_hidx, rarg, 1,
275 NULL, 0, 0);
276 }
277 #ifdef PSM_HAVE_SCIF
278 } else {
279 /* Try SCIF DMA */
280 scif_epd_t epd =
281 tok->tok.epaddr_from->ep->amsh_qdir[shmidx].amsh_epd[0];
282
283 if(ptl->ep->scif_dma_mode == PSMI_SCIF_DMA_PUT &&
284 ptl->ep->scif_dma_threshold <= msglen) {
285 off_t target_offset = args[4].u64w0;
286
287 /* The DMA operation is NOT completed here. It is
288 initiated here, then the receiving side is notified.
289 The target issues a DMA fence to wait for the DMA
290 complete, then responds that it has completed handling
291 the transfer on that side. */
292 /* The 'v' form takes care of local registration. */
293 if(scif_vwriteto(epd, sreq->buf, msglen, target_offset,
294 SCIF_RMA_USECACHE)) {
295 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
296 "psmi_am_mq_handler_rtsmatch(): scif_vwriteto failed: (%d) %s", errno, strerror(errno));
297 }
298
299 /* Send response that PUT is complete */
300 psmi_amsh_short_reply(tok, mq_handler_rtsdone_hidx, rarg, 1,
301 NULL, 0, 0);
302 } else if(ptl->ep->scif_dma_mode == PSMI_SCIF_DMA_GET &&
303 ptl->ep->scif_dma_threshold <= msglen) {
304 /* GET mode: receiver has performed DMA read, so unregister. */
305 scif_unregister_region(epd, args[4].u64w0, msglen);
306 } else {
307 /* No form of DMA is enabled -- use the memory copying path */
308 psmi_amsh_long_reply(tok, mq_handler_rtsdone_hidx, rarg, 1,
309 sreq->buf, msglen, dest, 0);
310 }
311 }
312 #endif
313 } //msglen > 0
314
315 psmi_mq_handle_rts_complete(sreq);
316 }
317
318 void
319 psmi_am_mq_handler_rtsdone(void *toki, psm_amarg_t *args, int narg, void *buf, size_t len)
320 {
321 psm_mq_req_t rreq = (psm_mq_req_t) (uintptr_t) args[0].u64w0;
322 psmi_assert(narg == 1);
323
324 _IPATH_VDBG("[rndv][recv] req=%p dest=%p len=%d\n", rreq, rreq->buf, rreq->recv_msglen);
325
326 #ifdef PSM_HAVE_SCIF
327 amsh_am_token_t *tok = (amsh_am_token_t *) toki;
328 ptl_t *ptl = tok->ptl;
329
330 psm_epaddr_t rmt_epaddr = rreq->rts_peer;
331
332 if(ptl->ep->scif_dma_mode == PSMI_SCIF_DMA_PUT &&
333 ptl->ep->scif_dma_threshold <= rreq->recv_msglen &&
334 rmt_epaddr->_shmidx >= PTL_AMSH_MAX_LOCAL_PROCS) {
335 /* SCIF DMA commands are initiated on amsh_epd[0]; the receive (for put)
336 side registration is on amsh_epd[1]. */
337 scif_epd_t epd =
338 rmt_epaddr->ep->amsh_qdir[rmt_epaddr->_shmidx].amsh_epd[1];
339
340 int mark;
341 if(scif_fence_mark(epd, SCIF_FENCE_INIT_PEER, &mark)) {
342 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
343 "psmi_am_mq_handler_rtsdone(): scif_fence_mark failed: (%d) %s",
344 errno, strerror(errno));
345 }
346
347 /* When registered, the rreq->buf address is replaced with the SCIF
348 registration offset so that it can be used here. */
349 scif_unregister_region(epd, (off_t)rreq->buf, rreq->recv_msglen);
350
351 if(scif_fence_wait(epd, mark)) {
352 psmi_handle_error(PSMI_EP_NORETURN, PSM_INTERNAL_ERR,
353 "psmi_am_mq_handler_rtsdone(): scif_fence_wait failed: (%d) %s",
354 errno, strerror(errno));
355 }
356 }
357 #endif
358
359 psmi_mq_handle_rts_complete(rreq);
360 }
361
362 void
363 psmi_am_handler(void *toki, psm_amarg_t *args, int narg, void *buf, size_t len)
364 {
365 amsh_am_token_t *tok = (amsh_am_token_t *) toki;
366 psm_am_handler_fn_t hfn;
367
368 hfn = psm_am_get_handler_function(tok->mq->ep,
369 (psm_handler_t) args[0].u32w0);
370
371 /* Invoke handler function. For AM we do not support break functionality */
372 hfn(toki, tok->tok.epaddr_from, args+1, narg-1, buf, len);
373
374 return;
375 }