2 * Copyright (c) 2000 Alfred Perlstein <alfred@freebsd.org>
3 * Copyright (c) 2000 Paul Saab <ps@freebsd.org>
5 * Copyright (c) 2000 John Baldwin <jhb@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <net/ethernet.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
44 #include <netinet/udp.h>
52 #include <bootstrap.h>
57 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
58 static pxe_t *pxe_p = NULL; /* !PXE */
61 static int pxe_debug = 0;
64 void pxe_enable(void *pxeinfo);
65 static void (*pxe_call)(int func, void *ptr);
66 static void pxenv_call(int func, void *ptr);
67 static void bangpxe_call(int func, void *ptr);
69 static int pxe_init(void);
70 static int pxe_print(int verbose);
71 static void pxe_cleanup(void);
73 static void pxe_perror(int error);
74 static int pxe_netif_match(struct netif *nif, void *machdep_hint);
75 static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
76 static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
77 static ssize_t pxe_netif_get(struct iodesc *, void **, time_t);
78 static ssize_t pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
79 static void pxe_netif_end(struct netif *nif);
81 extern struct netif_stats pxe_st[];
82 extern uint16_t __bangpxeseg;
83 extern uint16_t __bangpxeoff;
84 extern void __bangpxeentry(void);
85 extern uint16_t __pxenvseg;
86 extern uint16_t __pxenvoff;
87 extern void __pxenventry(void);
89 struct netif_dif pxe_ifs[] = {
90 /* dif_unit dif_nsel dif_stats dif_private */
94 struct netif_stats pxe_st[nitems(pxe_ifs)];
96 struct netif_driver pxenetif = {
97 .netif_bname = "pxenet",
98 .netif_match = pxe_netif_match,
99 .netif_probe = pxe_netif_probe,
100 .netif_init = pxe_netif_init,
101 .netif_get = pxe_netif_get,
102 .netif_put = pxe_netif_put,
103 .netif_end = pxe_netif_end,
104 .netif_ifs = pxe_ifs,
105 .netif_nifs = nitems(pxe_ifs)
108 struct netif_driver *netif_drivers[] = {
113 struct devsw pxedisk = {
117 .dv_strategy = NULL, /* Will be set in pxe_init */
118 .dv_open = NULL, /* Will be set in pxe_init */
119 .dv_close = NULL, /* Will be set in pxe_init */
121 .dv_print = pxe_print,
122 .dv_cleanup = pxe_cleanup,
126 * This function is called by the loader to enable PXE support if we
127 * are booted by PXE. The passed in pointer is a pointer to the PXENV+
131 pxe_enable(void *pxeinfo)
133 pxenv_p = (pxenv_t *)pxeinfo;
134 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
135 pxenv_p->PXEPtr.offset);
140 * return true if pxe structures are found/initialized,
141 * also figures out our IP information via the pxe cached info struct
146 t_PXENV_GET_CACHED_INFO *gci_p;
150 extern struct devsw netdev;
155 /* look for "PXENV+" */
156 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
161 /* make sure the size is something we can handle */
162 if (pxenv_p->Length > sizeof(*pxenv_p)) {
163 printf("PXENV+ structure too large, ignoring\n");
170 * add up each byte in the structure, the total should be 0
173 checkptr = (uint8_t *) pxenv_p;
174 for (counter = 0; counter < pxenv_p->Length; counter++)
175 checksum += *checkptr++;
177 printf("PXENV+ structure failed checksum, ignoring\n");
183 * PXENV+ passed, so use that if !PXE is not available or
184 * the checksum fails.
186 pxe_call = pxenv_call;
187 if (pxenv_p->Version >= 0x0200) {
189 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
194 checkptr = (uint8_t *)pxe_p;
195 for (counter = 0; counter < pxe_p->StructLength;
197 checksum += *checkptr++;
202 pxe_call = bangpxe_call;
207 pxedisk.dv_open = netdev.dv_open;
208 pxedisk.dv_close = netdev.dv_close;
209 pxedisk.dv_strategy = netdev.dv_strategy;
211 printf("\nPXE version %d.%d, real mode entry point ",
212 (uint8_t) (pxenv_p->Version >> 8),
213 (uint8_t) (pxenv_p->Version & 0xFF));
214 if (pxe_call == bangpxe_call)
215 printf("@%04x:%04x\n",
216 pxe_p->EntryPointSP.segment,
217 pxe_p->EntryPointSP.offset);
219 printf("@%04x:%04x\n",
220 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
222 gci_p = bio_alloc(sizeof(*gci_p));
227 bzero(gci_p, sizeof(*gci_p));
228 gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
229 pxe_call(PXENV_GET_CACHED_INFO, gci_p);
230 if (gci_p->Status != 0) {
231 pxe_perror(gci_p->Status);
232 bio_free(gci_p, sizeof(*gci_p));
236 free(bootp_response);
237 if ((bootp_response = malloc(gci_p->BufferSize)) != NULL) {
238 bootp_response_size = gci_p->BufferSize;
239 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
240 bootp_response, bootp_response_size);
242 bio_free(gci_p, sizeof(*gci_p));
247 pxe_print(int verbose)
249 if (pxe_call == NULL)
252 printf("%s devices:", pxedisk.dv_name);
253 if (pager_output("\n") != 0)
255 printf(" %s0:", pxedisk.dv_name);
257 printf(" %s:%s", inet_ntoa(rootip), rootpath);
259 return (pager_output("\n"));
265 t_PXENV_UNLOAD_STACK *unload_stack_p;
266 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p;
268 if (pxe_call == NULL)
271 undi_shutdown_p = bio_alloc(sizeof(*undi_shutdown_p));
272 if (undi_shutdown_p != NULL) {
273 bzero(undi_shutdown_p, sizeof(*undi_shutdown_p));
274 pxe_call(PXENV_UNDI_SHUTDOWN, undi_shutdown_p);
277 if (pxe_debug && undi_shutdown_p->Status != 0)
278 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
279 undi_shutdown_p->Status);
281 bio_free(undi_shutdown_p, sizeof(*undi_shutdown_p));
284 unload_stack_p = bio_alloc(sizeof(*unload_stack_p));
285 if (unload_stack_p != NULL) {
286 bzero(unload_stack_p, sizeof(*unload_stack_p));
287 pxe_call(PXENV_UNLOAD_STACK, unload_stack_p);
290 if (pxe_debug && unload_stack_p->Status != 0)
291 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
292 unload_stack_p->Status);
294 bio_free(unload_stack_p, sizeof(*unload_stack_p));
305 pxenv_call(int func, void *ptr)
309 printf("pxenv_call %x\n", func);
312 bzero(&v86, sizeof(v86));
314 __pxenvseg = pxenv_p->RMEntry.segment;
315 __pxenvoff = pxenv_p->RMEntry.offset;
317 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
318 v86.es = VTOPSEG(ptr);
319 v86.edi = VTOPOFF(ptr);
320 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
327 bangpxe_call(int func, void *ptr)
331 printf("bangpxe_call %x\n", func);
334 bzero(&v86, sizeof(v86));
336 __bangpxeseg = pxe_p->EntryPointSP.segment;
337 __bangpxeoff = pxe_p->EntryPointSP.offset;
339 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
340 v86.edx = VTOPSEG(ptr);
341 v86.eax = VTOPOFF(ptr);
342 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
350 pxe_netif_match(struct netif *nif, void *machdep_hint)
356 pxe_netif_probe(struct netif *nif, void *machdep_hint)
358 if (pxe_call == NULL)
365 pxe_netif_end(struct netif *nif)
367 t_PXENV_UNDI_CLOSE *undi_close_p;
369 undi_close_p = bio_alloc(sizeof(*undi_close_p));
370 if (undi_close_p != NULL) {
371 bzero(undi_close_p, sizeof(*undi_close_p));
372 pxe_call(PXENV_UNDI_CLOSE, undi_close_p);
373 if (undi_close_p->Status != 0)
374 printf("undi close failed: %x\n", undi_close_p->Status);
375 bio_free(undi_close_p, sizeof(*undi_close_p));
380 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
382 t_PXENV_UNDI_GET_INFORMATION *undi_info_p;
383 t_PXENV_UNDI_OPEN *undi_open_p;
387 undi_info_p = bio_alloc(sizeof(*undi_info_p));
388 if (undi_info_p == NULL)
391 bzero(undi_info_p, sizeof(*undi_info_p));
392 pxe_call(PXENV_UNDI_GET_INFORMATION, undi_info_p);
393 if (undi_info_p->Status != 0) {
394 printf("undi get info failed: %x\n", undi_info_p->Status);
395 bio_free(undi_info_p, sizeof(*undi_info_p));
399 /* Make sure the CurrentNodeAddress is valid. */
400 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
401 if (undi_info_p->CurrentNodeAddress[i] != 0)
404 if (i < undi_info_p->HwAddrLen) {
405 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
406 if (undi_info_p->CurrentNodeAddress[i] != 0xff)
410 if (i < undi_info_p->HwAddrLen)
411 mac = undi_info_p->CurrentNodeAddress;
413 mac = undi_info_p->PermNodeAddress;
415 len = min(sizeof (desc->myea), undi_info_p->HwAddrLen);
416 for (i = 0; i < len; ++i)
417 desc->myea[i] = mac[i];
419 if (bootp_response != NULL)
420 desc->xid = bootp_response->bp_xid;
424 bio_free(undi_info_p, sizeof(*undi_info_p));
425 undi_open_p = bio_alloc(sizeof(*undi_open_p));
426 if (undi_open_p == NULL)
428 bzero(undi_open_p, sizeof(*undi_open_p));
429 undi_open_p->PktFilter = FLTR_DIRECTED | FLTR_BRDCST;
430 pxe_call(PXENV_UNDI_OPEN, undi_open_p);
431 if (undi_open_p->Status != 0)
432 printf("undi open failed: %x\n", undi_open_p->Status);
433 bio_free(undi_open_p, sizeof(*undi_open_p));
437 pxe_netif_receive_isr(t_PXENV_UNDI_ISR *isr, void **pkt, ssize_t *retsize)
439 static bool data_pending;
440 char *buf, *ptr, *frame;
447 * We can save ourselves the next two pxe calls because we already know
448 * we weren't done grabbing everything.
451 data_pending = false;
456 * We explicitly don't check for OURS/NOT_OURS as a result of START;
457 * it's been reported that some cards are known to mishandle these.
459 bzero(isr, sizeof(*isr));
460 isr->FuncFlag = PXENV_UNDI_ISR_IN_START;
461 pxe_call(PXENV_UNDI_ISR, isr);
462 /* We could translate Status... */
463 if (isr->Status != 0) {
467 bzero(isr, sizeof(*isr));
468 isr->FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
469 pxe_call(PXENV_UNDI_ISR, isr);
470 if (isr->Status != 0) {
473 if (isr->FuncFlag == PXENV_UNDI_ISR_OUT_BUSY) {
475 * Let the caller decide if we need to be restarted. It will
476 * currently blindly restart us, but it could check timeout in
483 * By design, we'll hardly ever hit this terminal condition unless we
484 * pick up nothing but tx interrupts here. More frequently, we will
485 * process rx buffers until we hit the terminal condition in the middle.
487 while (isr->FuncFlag != PXENV_UNDI_ISR_OUT_DONE) {
489 * This might have given us PXENV_UNDI_ISR_OUT_TRANSMIT, in
490 * which case we can just disregard and move on to the next
493 if (isr->FuncFlag != PXENV_UNDI_ISR_OUT_RECEIVE)
498 * Grab size from the first Frame that we picked up,
499 * allocate an rx buf to hold. Careful here, as we may
500 * see a fragmented frame that's spread out across
501 * multiple GET_NEXT calls.
503 size = isr->FrameLength;
504 buf = malloc(size + ETHER_ALIGN);
508 ptr = buf + ETHER_ALIGN;
511 frame = (char *)((uintptr_t)isr->Frame.segment << 4);
512 frame += isr->Frame.offset;
513 bcopy(PTOV(frame), ptr, isr->BufferLength);
514 ptr += isr->BufferLength;
515 rsize += isr->BufferLength;
518 * Stop here before we risk catching the start of another frame.
519 * It would be nice to continue reading until we actually get a
520 * PXENV_UNDI_ISR_OUT_DONE, but our network stack in libsa isn't
521 * suitable for reading more than one packet at a time.
529 bzero(isr, sizeof(*isr));
530 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
531 pxe_call(PXENV_UNDI_ISR, isr);
532 if (isr->Status != 0) {
539 * We may have never picked up a frame at all (all tx), in which case
540 * the caller should restart us.
552 pxe_netif_receive(void **pkt, ssize_t *size)
554 t_PXENV_UNDI_ISR *isr;
557 isr = bio_alloc(sizeof(*isr));
562 * This completely ignores the timeout specified in pxe_netif_get(), but
563 * we shouldn't be running long enough here for that to make a
567 /* We'll only really re-enter for PXENV_UNDI_ISR_OUT_BUSY. */
568 ret = pxe_netif_receive_isr(isr, pkt, size);
573 bio_free(isr, sizeof(*isr));
578 pxe_netif_get(struct iodesc *desc, void **pkt, time_t timeout)
587 while ((getsecs() - t) < timeout) {
588 ret = pxe_netif_receive(&ptr, &size);
595 return (ret == 0 ? size : -1);
599 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
601 t_PXENV_UNDI_TRANSMIT *trans_p;
602 t_PXENV_UNDI_TBD *tbd_p;
606 trans_p = bio_alloc(sizeof(*trans_p));
607 tbd_p = bio_alloc(sizeof(*tbd_p));
608 data = bio_alloc(len);
610 if (trans_p != NULL && tbd_p != NULL && data != NULL) {
611 bzero(trans_p, sizeof(*trans_p));
612 bzero(tbd_p, sizeof(*tbd_p));
614 trans_p->TBD.segment = VTOPSEG(tbd_p);
615 trans_p->TBD.offset = VTOPOFF(tbd_p);
617 tbd_p->ImmedLength = len;
618 tbd_p->Xmit.segment = VTOPSEG(data);
619 tbd_p->Xmit.offset = VTOPOFF(data);
620 bcopy(pkt, data, len);
622 pxe_call(PXENV_UNDI_TRANSMIT, trans_p);
623 if (trans_p->Status == 0)
628 bio_free(tbd_p, sizeof(*tbd_p));
629 bio_free(trans_p, sizeof(*trans_p));