1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:cindent:
4 * Copyright (C) 2003 High Performance Computing Center North (HPC2N)
5 * Author: Niklas Edmundsson <nikke@hpc2n.umu.se>
8 * This file is part of Portals, http://www.sf.net/projects/lustre/
10 * Portals is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Portals is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Portals; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "scimacnal.h"
28 kscimacnal_read (nal_cb_t *nal, void *private,
29 void *dst_addr, user_ptr src_addr, size_t len)
31 CDEBUG(D_NET, "0x%Lx: reading %ld bytes from %p -> %p\n",
32 nal->ni.nid, (long)len, src_addr, dst_addr );
33 memcpy( dst_addr, src_addr, len );
39 kscimacnal_write(nal_cb_t *nal, void *private,
40 user_ptr dst_addr, void *src_addr, size_t len)
42 CDEBUG(D_NET, "0x%Lx: writing %ld bytes from %p -> %p\n",
43 nal->ni.nid, (long)len, src_addr, dst_addr );
44 memcpy( dst_addr, src_addr, len );
50 kscimacnal_malloc(nal_cb_t *nal, size_t len)
54 PORTAL_ALLOC(buf, len);
60 kscimacnal_free(nal_cb_t *nal, void *buf, size_t len)
62 PORTAL_FREE(buf, len);
67 kscimacnal_printf(nal_cb_t *nal, const char *fmt, ...)
72 if (portal_debug & D_NET) {
74 vsnprintf( msg, sizeof(msg), fmt, ap );
77 printk("Lustre: CPUId: %d %s",smp_processor_id(), msg);
83 kscimacnal_cli(nal_cb_t *nal, unsigned long *flags)
85 kscimacnal_data_t *data= nal->nal_data;
87 spin_lock_irqsave(&data->ksci_dispatch_lock,*flags);
92 kscimacnal_sti(nal_cb_t *nal, unsigned long *flags)
94 kscimacnal_data_t *data= nal->nal_data;
96 spin_unlock_irqrestore(&data->ksci_dispatch_lock,*flags);
101 kscimacnal_dist(nal_cb_t *nal, ptl_nid_t nid, unsigned long *dist)
103 /* FIXME: Network distance has a meaning, but is there no easy
104 * way to figure it out (depends on routing) */
106 if ( nal->ni.nid == nid ) {
117 char * get_mac_error(mac_status_t status)
120 case MAC_MSG_STAT_OK:
121 return "MAC_MSG_STAT_OK";
122 case MAC_MSG_STAT_FREED:
123 return "MAC_MSG_STAT_FREED";
124 case MAC_MSG_STAT_ABORTED:
125 return "MAC_MSG_STAT_ABORTED";
126 case MAC_MSG_STAT_TIMEDOUT:
127 return "MAC_MSG_STAT_TIMEDOUT";
128 case MAC_MSG_STAT_NODEUNREACH:
129 return "MAC_MSG_STAT_NODEUNREACH";
130 case MAC_MSG_STAT_NETDOWN:
131 return "MAC_MSG_STAT_NETDOWN";
132 case MAC_MSG_STAT_RESET:
133 return "MAC_MSG_STAT_RESET";
134 case MAC_MSG_STAT_INITFAILED:
135 return "MAC_MSG_STAT_INITFAILED";
136 case MAC_MSG_STAT_SYNCFAILED:
137 return "MAC_MSG_STAT_SYNCFAILED";
138 case MAC_MSG_STAT_BADPROTO:
139 return "MAC_MSG_STAT_BADPROTO";
140 case MAC_MSG_STAT_NOBUFSPACE:
141 return "MAC_MSG_STAT_NOBUFSPACE";
142 case MAC_MSG_STAT_CONGESTION:
143 return "MAC_MSG_STAT_CONGESTION";
144 case MAC_MSG_STAT_OTHER:
145 return "MAC_MSG_STAT_OTHER";
147 return "Unknown error";
152 /* FIXME add routing code here ? */
154 /* Called by ScaMac when transmission is complete (ie. message is released) */
156 kscimacnal_txrelease(mac_mblk_t *msg, mac_msg_status_t status, void *context)
158 kscimacnal_tx_t *ktx = (kscimacnal_tx_t *)context;
161 LASSERT (ktx != NULL);
162 /* Unmap any mapped pages */
163 for(i=0; i<ktx->ktx_nmapped; i++) {
164 kunmap(ktx->ktx_kpages[i]);
167 CDEBUG(D_NET, "kunmapped %d pages\n", ktx->ktx_nmapped);
169 /* Euh, there is no feedback when transmission fails?! */
171 case MAC_MSG_STAT_OK: /* normal */
174 CERROR("%s (%d):\n", get_mac_error(status), status);
179 lib_finalize(ktx->ktx_nal, ktx->ktx_private, ktx->ktx_cookie);
181 PORTAL_FREE(ktx, (sizeof(kscimacnal_tx_t)));
185 /* Called by portals when it wants to send a message.
186 * Since ScaMAC has it's own TX thread we don't bother setting up our own. */
188 /* FIXME: Read comments in qswnal_cb.c for _sendmsg and fix return-on-error
191 kscimacnal_sendmsg(nal_cb_t *nal,
198 unsigned int payload_niov,
199 struct iovec *payload_iov,
200 ptl_kiov_t *payload_kiov,
203 kscimacnal_tx_t *ktx=NULL;
204 kscimacnal_data_t *ksci = nal->nal_data;
206 int buf_len = sizeof(ptl_hdr_t) + payload_len;
207 mac_mblk_t *msg=NULL, *lastblk, *newblk;
208 unsigned long physaddr;
211 CDEBUG(D_NET, "sending %d bytes from %p/%p to nid "LPX64" niov: %d\n",
212 payload_len, payload_iov, payload_kiov, nid, payload_niov);
214 /* Basic sanity checks */
215 LASSERT(ksci != NULL);
216 LASSERT(hdr != NULL);
217 LASSERT (payload_len == 0 || payload_niov > 0);
218 LASSERT (payload_niov <= PTL_MD_MAX_IOV);
219 /* It must be OK to kmap() if required */
220 LASSERT (payload_kiov == NULL || !in_interrupt ());
221 /* payload is either all vaddrs or all pages */
222 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
224 /* Do real check if we can send this */
225 if (buf_len > mac_get_mtusize(ksci->ksci_machandle)) {
226 CERROR("kscimacnal:request exceeds TX MTU size (%ld).\n",
227 mac_get_mtusize(ksci->ksci_machandle));
232 /* save transaction info for later finalize and cleanup */
233 PORTAL_ALLOC(ktx, (sizeof(kscimacnal_tx_t)));
238 ktx->ktx_nmapped = 0; /* Start with no mapped pages :) */
240 /* *SIGH* hdr is a stack variable in the calling function, so we
241 * need to copy it to a buffer. Zerocopy magic (or is it just
242 * deferred memcpy?) is annoying sometimes. */
243 memcpy(&ktx->ktx_hdr, hdr, sizeof(ptl_hdr_t));
245 /* First, put the header in the main message mblk */
246 msg = mac_alloc_mblk(&ktx->ktx_hdr, sizeof(ptl_hdr_t),
247 kscimacnal_txrelease, ktx);
249 PORTAL_FREE(ktx, (sizeof(kscimacnal_tx_t)));
252 mac_put_mblk(msg, sizeof(ptl_hdr_t));
255 /* Allocate additional mblks for each iov as needed.
256 * Essentially lib_copy_(k)iov2buf with a twist or two */
257 while (payload_len > 0)
262 LASSERT (payload_niov > 0);
264 if(payload_iov != NULL) {
265 nob = MIN (payload_iov->iov_len, payload_len);
266 addr = payload_iov->iov_base;
269 nob = MIN (payload_kiov->kiov_len, payload_len);
270 /* Bollocks. We need to handle paged IO for things to
271 * work but there is no good way to do this. We
272 * do it by kmap():ing all pages and keep them
273 * mapped until scimac is done with them. */
274 /* FIXME: kunmap() on error */
275 addr = kmap(payload_kiov->kiov_page);
276 ktx->ktx_kpages[ktx->ktx_nmapped++] =
277 payload_kiov->kiov_page;
279 /* We don't need a callback on the additional mblks,
280 * since all release callbacks seems to be called when
281 * the entire message has been sent */
282 newblk=mac_alloc_mblk(addr, nob, NULL, NULL);
286 PORTAL_FREE(ktx, (sizeof(kscimacnal_tx_t)));
289 mac_put_mblk(newblk, nob);
290 mac_link_mblk(lastblk, newblk);
295 if(payload_iov != NULL) {
303 CDEBUG(D_NET, "kmapped %d pages\n", ktx->ktx_nmapped);
306 ktx->ktx_private = private;
307 ktx->ktx_cookie = cookie;
309 CDEBUG(D_NET, "mac_send %d bytes to nid: 0x%Lx\n", buf_len, nid);
311 physaddr = htonl(nid);
313 if((rc=mac_send(ksci->ksci_machandle, msg,
314 (mac_physaddr_t *) &physaddr))) {
315 CERROR("kscimacnal: mac_send() failed, rc=%d\n", rc);
317 PORTAL_FREE(ktx, (sizeof(kscimacnal_tx_t)));
326 kscimacnal_send (nal_cb_t *nal,
333 unsigned int payload_niov,
334 struct iovec *payload_iov,
337 return (kscimacnal_sendmsg (nal, private, cookie, hdr, type, nid, pid,
338 payload_niov, payload_iov, NULL, payload_nob));
342 kscimacnal_send_pages (nal_cb_t *nal,
349 unsigned int payload_niov,
350 ptl_kiov_t *payload_kiov,
353 return (kscimacnal_sendmsg (nal, private, cookie, hdr, type, nid, pid,
354 payload_niov, NULL, payload_kiov, payload_nob));
359 kscimacnal_fwd_packet (void *arg, kpr_fwd_desc_t *fwd)
361 CERROR ("forwarding not implemented\n");
365 /* Process a received portals packet */
366 /* Called by the ScaMac RX thread when a packet is received */
368 kscimacnal_rx(mac_handle_t *handle, mac_mblk_t *msg, mac_msg_type_t type,
371 ptl_hdr_t *hdr = NULL;
374 kscimacnal_data_t *ksci = userdata;
376 LASSERT(ksci != NULL);
378 if ( !ksci->ksci_init || ksci->ksci_shuttingdown ||
379 type == MAC_MSG_TYPE_CTRL || type == MAC_MSG_TYPE_OTHER ) {
380 /* We're not interested in messages not for us, ignore */
385 size = mac_msg_size(msg);
387 CDEBUG(D_NET,"msg %p type %d, size %ld bytes (%ld mblks)\n",
388 msg, type, size, mac_msg_mblks(msg));
390 if( size < sizeof( ptl_hdr_t ) ) {
391 /* XXX what's this for? */
392 if (ksci->ksci_shuttingdown)
394 CERROR("kscimacnal: did not receive complete portal header,"
395 "size= %ld\n", size);
396 /* Free the message before exiting */
401 /* Provide everything we know */
405 krx.userdata = userdata;
407 /* mac_msg_next returns the next mblk with unread data */
408 hdr = mac_get_mblk(mac_msg_next(msg), sizeof(ptl_hdr_t) );
411 CERROR("kscimacnal: no data block in message %p\n", msg);
416 if ( hdr->dest_nid == kscimacnal_lib.ni.nid ) {
417 PROF_START(lib_parse);
418 /* sets wanted_len, iovs etc and calls our callback */
419 lib_parse(&kscimacnal_lib, hdr, &krx);
420 PROF_FINISH(lib_parse);
421 #if 0 /* FIXME: Is it possible to detect this? */
422 } else if (kgmnal_ispeer(hdr->dest_nid)) {
423 /* should have gone direct to peer */
424 CERROR("dropping packet from 0x%llx to 0x%llx:"
425 "target is a peer\n",
426 hdr->src_nid, hdr->dest_nid);
427 kgmnal_requeue_rx(&krx);
428 #endif /* if 0 FIXME */
430 /* forward to gateway */
431 CERROR("forwarding not implemented, mynid=0x%llx dest=0x%llx\n",
432 kscimacnal_lib.ni.nid, hdr->dest_nid);
437 CDEBUG(D_NET, "msg %p: Done\n", msg);
441 /* Called by portals to process a recieved packet */
443 kscimacnal_recvmsg(nal_cb_t *nal,
452 kscimacnal_rx_t *krx = private;
456 ptl_size_t iovused=0;
459 LASSERT (krx != NULL);
460 LASSERT (krx->msg != NULL);
462 CDEBUG(D_NET,"msg %p: mlen=%d, rlen=%d, niov=%d\n",
463 krx->msg, mlen, rlen, niov);
465 /* What was actually received must be >= what sender claims to have
466 * sent. This is an LASSERT, since lib-move doesn't check cb return
467 * code yet. Also, rlen seems to be negative when mlen==0 so don't
470 LASSERT (mlen==0 || mac_msg_size(krx->msg) >= sizeof(ptl_hdr_t)+rlen);
471 LASSERT (mlen==0 || mlen <= rlen);
472 /* It must be OK to kmap() if required */
473 LASSERT (kiov == NULL || !in_interrupt ());
474 /* Either all pages or all vaddrs */
475 LASSERT (!(kiov != NULL && iov != NULL));
479 /* mac_msg_next returns next mblk with unread data (ie. can
481 while (mlen != 0 && (mblk = mac_msg_next(krx->msg))) {
482 pkt_len = mac_mblk_len(mblk);
483 src = mac_get_mblk(mblk, pkt_len); /* Next unread block */
485 CDEBUG(D_NET,"msg %p: mblk: %p pkt_len: %ld src: %p\n",
486 krx->msg, mblk, pkt_len, src);
488 LASSERT(src != NULL);
490 /* Essentially lib_copy_buf2(k)iov but with continuation
491 * support, we "gracefully" thrash the argument vars ;) */
492 while (pkt_len > 0) {
498 LASSERT(iovused < iov->iov_len);
500 base = iov->iov_base;
503 LASSERT(iovused < kiov->kiov_len);
504 len = kiov->kiov_len;
507 base = kmap(kiov->kiov_page);
511 nob = MIN (len-iovused, pkt_len);
512 CDEBUG(D_NET, "base: %p len: %d src: %p nob: %d "
514 base, len, src, nob, iovused);
516 memcpy (base+iovused, src, nob);
520 if(nob+iovused < len) {
521 /* We didn't use all of the iov */
531 kunmap(kiov->kiov_page);
538 /* Just to make sure the last page is unmapped */
539 if(kiov!=NULL && base!=NULL) {
540 kunmap(kiov->kiov_page);
545 CDEBUG(D_NET, "Calling lib_finalize.\n");
547 PROF_START(lib_finalize);
548 lib_finalize(nal, private, cookie);
549 PROF_FINISH(lib_finalize);
551 CDEBUG(D_NET, "Done.\n");
558 kscimacnal_recv(nal_cb_t *nal,
566 return (kscimacnal_recvmsg (nal, private, cookie, niov, iov, NULL, mlen, rlen));
571 kscimacnal_recv_pages (nal_cb_t *nal,
579 return (kscimacnal_recvmsg (nal, private, cookie, niov, NULL, kiov, mlen, rlen));
583 nal_cb_t kscimacnal_lib = {
584 nal_data: &kscimacnal_data, /* NAL private data */
585 cb_send: kscimacnal_send,
586 cb_send_pages: kscimacnal_send_pages,
587 cb_recv: kscimacnal_recv,
588 cb_recv_pages: kscimacnal_recv_pages,
589 cb_read: kscimacnal_read,
590 cb_write: kscimacnal_write,
591 cb_malloc: kscimacnal_malloc,
592 cb_free: kscimacnal_free,
593 cb_printf: kscimacnal_printf,
594 cb_cli: kscimacnal_cli,
595 cb_sti: kscimacnal_sti,
596 cb_dist: kscimacnal_dist