home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / DRIVERSS.ZIP / HEAD.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-03-19  |  30.6 KB  |  1,233 lines

  1.     include    defs.asm
  2.  
  3. ;  Copyright, 1988-9, 1990, Russell Nelson
  4.  
  5. ;   This program is free software; you can redistribute it and/or modify
  6. ;   it under the terms of the GNU General Public License as published by
  7. ;   the Free Software Foundation, version 1.
  8. ;
  9. ;   This program is distributed in the hope that it will be useful,
  10. ;   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;   GNU General Public License for more details.
  13. ;
  14. ;   You should have received a copy of the GNU General Public License
  15. ;   along with this program; if not, write to the Free Software
  16. ;   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. code    segment word public
  19.     assume    cs:code, ds:code
  20.  
  21.     public    phd_environ
  22.     org    2ch
  23. phd_environ    dw    ?
  24.  
  25.     public    phd_dioa
  26.     org    80h
  27. phd_dioa    label    byte
  28.  
  29.     org    100h
  30. start:
  31.     jmp    start_1
  32.     extrn    start_1: near
  33.     even                ;put the stack on a word boundary.
  34.  
  35. ;we use our dioa for a stack space.  Very hard usage has shown that only
  36. ;  27 bytes were being used, so 128 should be sufficient.
  37. our_stack    label    byte
  38.  
  39.  
  40.     extrn    int_no: byte
  41.     public    packet_int_no, is_at, sys_features, flagbyte
  42. packet_int_no    db    ?,?,?,?        ; interrupt to communicate.
  43. is_at        db    0        ; =1 if we're on an AT.
  44. sys_features    db    0        ; 2h = MC   40h = 2nd 8259
  45. flagbyte    db    0
  46.     even
  47.  
  48. functions    label    word
  49.     dw    f_not_implemented    ;0
  50.     dw    f_driver_info        ;1
  51.     dw    f_access_type        ;2
  52.     dw    f_release_type        ;3
  53.     dw    f_send_pkt        ;4
  54.     dw    f_terminate        ;5
  55.     dw    f_get_address        ;6
  56.     dw    f_reset_interface    ;7
  57.     dw    f_stop            ;8
  58.     dw    f_not_implemented    ;9
  59.     dw    f_get_parameters    ;10
  60.     dw    f_not_implemented    ;11
  61.     dw    f_as_send_pkt        ;12
  62.     dw    f_drop_pkt        ;13
  63.     dw    f_not_implemented    ;14
  64.     dw    f_not_implemented    ;15
  65.     dw    f_not_implemented    ;16
  66.     dw    f_not_implemented    ;17
  67.     dw    f_not_implemented    ;18
  68.     dw    f_not_implemented    ;19
  69.     dw    f_set_rcv_mode        ;20
  70.     dw    f_get_rcv_mode        ;21
  71.     dw    f_set_multicast_list    ;22
  72.     dw    f_get_multicast_list    ;23
  73.     dw    f_get_statistics    ;24
  74.     dw    f_set_address        ;25
  75.  
  76.     extrn    driver_class: byte
  77.     extrn    driver_type: byte
  78.     extrn    driver_name: byte
  79.     extrn    driver_function: byte
  80.     extrn    parameter_list: byte
  81.  
  82.     extrn    send_pkt: near
  83.     extrn    as_send_pkt: near
  84.     extrn    drop_pkt: near
  85.     extrn    get_address: near
  86.     extrn    set_address: near
  87.     extrn    terminate: near
  88.     extrn    reset_interface: near
  89.     extrn    xmit: near
  90.     extrn    recv: near
  91.     extrn    recv_exiting: near
  92.     extrn    etopen: near
  93.  
  94.     extrn    rcv_modes: word        ;count of modes followed by mode handles.
  95.  
  96.     extrn    set_multicast_list: near
  97.  
  98. linc    macro    n            ; inc a 32 bit integer
  99.     local    a
  100.     inc    n            ;increment the low word
  101.     jne    a            ;go if not overflow
  102.     inc    n+2            ;increment the high word
  103. a:
  104.     endm
  105.  
  106. per_handle    struc
  107. in_use        db    0        ;non-zero if this handle is in use.
  108. packet_type    db    MAX_P_LEN dup(0);associated packet type.
  109. packet_type_len    dw    0        ;associated packet type length.
  110. receiver    dd    0        ;receiver handler.
  111. receiver_sig    db    8 dup(?)    ;signature at the receiver handler.
  112. class        db    ?        ;interface class
  113. per_handle    ends
  114.  
  115. handles        per_handle MAX_HANDLE dup(<>)
  116. end_handles    label    byte
  117.  
  118.     public    multicast_count, multicast_addrs, multicast_broad
  119. multicast_count    dw    0        ;count of stored multicast addresses.
  120. multicast_broad    db    0ffh,0ffh,0ffh,0ffh,0ffh,0ffh    ; entry for broadcast
  121. multicast_addrs    db    MAX_MULTICAST*EADDR_LEN dup(?)
  122.  
  123. have_my_address    db    0        ;nonzero if our address has been set.
  124. my_address    db    MAX_ADDR_LEN dup(?)
  125. my_address_len    dw    ?
  126.  
  127. rcv_mode_num    dw    3
  128.  
  129. free_handle    dw    0        ; temp, a handle not in use
  130. found_handle    dw    0        ; temp, handle for our packet
  131. receive_ptr    dd    0        ; the pkt receive service routine
  132.  
  133.     public    send_head, send_tail
  134. send_head    dd    0        ; head of transmit queue
  135. send_tail    dd    0        ; tail of transmit queue
  136.  
  137. statistics_list    label    dword
  138. packets_in    dw    ?,?
  139. packets_out    dw    ?,?
  140. bytes_in    dw    ?,?
  141. bytes_out    dw    ?,?
  142. errors_in    dw    ?,?
  143. errors_out    dw    ?,?
  144. packets_dropped    dw    ?,?        ;dropped due to no type handler.
  145.  
  146. savess        dw    ?        ;saved during the stack swap.
  147. savesp        dw    ?
  148.  
  149. regs    struc                ; stack offsets of incoming regs
  150. _ES    dw    ?
  151. _DS    dw    ?
  152. _BP    dw    ?
  153. _DI    dw    ?
  154. _SI    dw    ?
  155. _DX    dw    ?
  156. _CX    dw    ?
  157. _BX    dw    ?
  158. _AX    dw    ?
  159. _IP    dw    ?
  160. _CS    dw    ?
  161. _F    dw    ?            ; flags, Carry flag is bit 0
  162. regs    ends
  163.  
  164. CY    equ    0001h
  165. EI    equ    0200h
  166.  
  167.  
  168. bytes    struc                ; stack offsets of incoming regs
  169.     dw    ?            ; es, ds, bp, di, si are 16 bits
  170.     dw    ?
  171.     dw    ?
  172.     dw    ?
  173.     dw    ?
  174. _DL    db    ?
  175. _DH    db    ?
  176. _CL    db    ?
  177. _CH    db    ?
  178. _BL    db    ?
  179. _BH    db    ?
  180. _AL    db    ?
  181. _AH    db    ?
  182. bytes    ends
  183.  
  184.     public    our_isr, their_isr
  185. their_isr    dd    0        ; original owner of pkt driver int
  186.  
  187. our_isr:
  188.     jmp    our_isr_0        ;the required signature.
  189.     db    'PKT DRVR',0
  190. our_isr_0:
  191.     assume    ds:nothing
  192.     push    ax
  193.     push    bx
  194.     push    cx
  195.     push    dx
  196.     push    si
  197.     push    di
  198.     push    bp
  199.     push    ds
  200.     push    es
  201.     cld
  202.     mov    bx,cs            ;set up ds.
  203.     mov    ds,bx
  204.     assume    ds:code
  205.     mov    bp,sp            ;we use bp to access the original regs.
  206.     and    _F[bp],not CY        ;start by clearing the carry flag.
  207.  
  208.     test    flagbyte,CALLED_ETOPEN    ; have we initialized the card?
  209.     jnz    our_isr_cont        ; yes
  210.     push    ax            ; save lots of registers
  211.     push    bx
  212.     push    cx
  213.     push    dx
  214.     push    si
  215.     push    di
  216.     push    bp
  217.     push    ds
  218.     push    es
  219.  
  220.     call    etopen            ; init the card
  221.  
  222.     pop    es            ; restore lots of registers
  223.     pop    ds
  224.     pop    bp
  225.     pop    di
  226.     pop    si
  227.     pop    dx
  228.     pop    cx
  229.     pop    bx
  230.     pop    ax
  231.     mov    dh,CANT_RESET        ; (actually can't initialize)
  232.     jc    our_isr_error
  233.     or    flagbyte,CALLED_ETOPEN    ; remember this fact
  234.  our_isr_cont:
  235.  
  236.     mov    bl,ah            ;jump to the correct function.
  237.     mov    bh,0
  238.     cmp    bx,25            ;only twenty five functions right now.
  239.     mov    dh,BAD_COMMAND        ;in case we find a bad number.
  240.     ja    our_isr_error
  241.     add    bx,bx            ;*2
  242.     call    functions[bx]
  243.     jnc    our_isr_return
  244. our_isr_error:
  245.     mov    _DH[bp],dh
  246.     or    _F[bp],CY        ;return their carry flag.
  247. our_isr_return:
  248.     pop    es
  249.     pop    ds
  250.     pop    bp
  251.     pop    di
  252.     pop    si
  253.     pop    dx
  254.     pop    cx
  255.     pop    bx
  256.     pop    ax
  257.     iret
  258.  
  259.     public    re_enable_interrupts
  260. re_enable_interrupts:
  261. ; Possibly re-enable interrupts.  We put this here so that other routines
  262. ; don't need to know how we put things on the stack.
  263.     test    _F[bp], EI        ; Were interrupts enabled on pkt driver entry?
  264.     je    re_enable_interrupts_1    ; No.
  265.     sti                ; Yes, re-enable interrupts now.
  266. re_enable_interrupts_1:
  267.     ret
  268.  
  269.  
  270. f_not_implemented:
  271.     mov    dh,BAD_COMMAND
  272.     stc
  273.     ret
  274.  
  275.  
  276. f_driver_info:
  277. ;    As of 1.08, the handle is optional, so we no longer verify it.
  278. ;    call    verify_handle
  279.     cmp    _AL[bp],0ffh        ; correct calling convention?
  280.     jne    f_driver_info_1        ; ne = incorrect, fail
  281.  
  282.                     ;For enhanced PD, if they call
  283.     cmp    _BX[bp],offset handles    ;with a handle, give them the
  284.                     ;class they think it is
  285.     jb    default_handle
  286.     cmp    _BX[bp],offset end_handles ;otherwise default to first class
  287.     jae    default_handle
  288.     mov    bx, _BX[bp]
  289.     cmp    [bx].in_use,0        ;if it's not in use, it's bad.
  290.     je    default_handle
  291.     mov    al, [bx].class
  292.     mov    _CH[bp], al
  293.     jmp    short got_handle
  294.  
  295. default_handle:
  296.     mov    al,driver_class
  297.     mov    _CH[bp],al
  298. got_handle:
  299.  
  300.     mov    _BX[bp],majver        ;version
  301.     mov    al,driver_class
  302.     mov    _CH[bp],al
  303.     mov    al,driver_type
  304.     cbw
  305.     mov    _DX[bp],ax
  306.     mov    _CL[bp],0        ;number zero.
  307.     mov    _DS[bp],ds        ; point to our name in their ds:si
  308.     mov    _SI[bp],offset driver_name
  309.     mov    al,driver_function
  310.     mov    _AL[bp],al
  311.     clc
  312.     ret
  313. f_driver_info_1:
  314.     stc
  315.     ret
  316.  
  317.  
  318. f_set_rcv_mode:
  319.     call    verify_handle
  320.  
  321.     cmp    cx,rcv_mode_num        ;are we already using that mode?
  322.     je    f_set_rcv_mode_4    ;yes, no need to check anything.
  323.  
  324.     mov    dx,bx            ;remember our handle.
  325.     mov    bx,offset handles    ; check that all handles are free
  326. f_set_rcv_mode_2:
  327.     cmp    bx,dx            ; is this our handle?
  328.     je    f_set_rcv_mode_3    ; yes, of course it's not free.
  329.     cmp    [bx].in_use,0        ; is this handle free?
  330.     jne    f_set_rcv_mode_1    ; ne = no, can't change
  331. f_set_rcv_mode_3:
  332.     add    bx,(size per_handle)    ; next handle
  333.     cmp    bx,offset end_handles    ; examined all handles?
  334.     jb    f_set_rcv_mode_2    ; b = no, continue examination
  335.  
  336.     mov    cx,_CX[bp]        ;get the desired receive mode.
  337.     cmp    cx,rcv_modes        ;do they have this many modes?
  338.     jae    f_set_rcv_mode_1    ;no - must be a bad mode for us.
  339.     mov    bx,cx
  340.     add    bx,bx            ;we're accessing words, not bytes.
  341.     mov    ax,rcv_modes[bx]+2    ;get the handler for this mode.
  342.     or    ax,ax            ;do they have one?
  343.     je    f_set_rcv_mode_1    ;no - must be a bad mode for us.
  344.     mov    rcv_mode_num,cx        ;yes - remember the number and
  345.     call    ax            ;  call it.
  346. f_set_rcv_mode_4:
  347.     clc
  348.     ret
  349. f_set_rcv_mode_1:
  350.     mov    dh,BAD_MODE
  351.     stc
  352.     ret
  353.  
  354.  
  355. f_get_rcv_mode:
  356.     call    verify_handle
  357.     mov    ax,rcv_mode_num        ;return the current receive mode.
  358.     mov    _AX[bp],ax
  359.     clc
  360.     ret
  361.  
  362.  
  363. f_set_multicast_list:
  364. ;    mov    cx,_CX[bp]        ;Tell them how much room they have.
  365.  
  366. ;verify that they supplied an even number of EADDR's.
  367.     mov    ax,cx
  368.     xor    dx,dx
  369.     mov    bx,EADDR_LEN
  370.     div    bx
  371.     or    dx,dx            ;zero remainder?
  372.     jne    f_set_multicast_list_2    ;no, we don't have an even number of
  373.                     ;  addresses.
  374.  
  375.     cmp    ax,MAX_MULTICAST    ;is this too many?
  376.     ja    f_set_multicast_list_3    ;yes - return NO_SPACE
  377. f_set_multicast_list_1:
  378.     mov    multicast_count,ax    ;remember the number of addresses.
  379.     push    cs
  380.     pop    es
  381.     mov    di,offset multicast_addrs
  382.     push    ds
  383.     mov    ds,_ES[bp]        ; get ds:si -> new list.
  384.     mov    si,_DI[bp]
  385.     push    cx
  386.     rep    movsb
  387.     pop    cx
  388.     pop    ds
  389.  
  390.     mov    si,offset multicast_addrs
  391.     call    set_multicast_list
  392.     ret
  393. f_set_multicast_list_2:
  394.     mov    dh,BAD_ADDRESS
  395.     stc
  396.     ret
  397. f_set_multicast_list_3:
  398.     mov    dh,NO_SPACE
  399.     stc
  400.     ret
  401.  
  402.  
  403. f_get_multicast_list:
  404.     mov    _ES[bp],ds        ;return what we have remembered.
  405.     mov    _DI[bp],offset multicast_addrs
  406.     mov    ax,EADDR_LEN        ;multiply the count by the length.
  407.     mul    multicast_count
  408.     mov    _CX[bp],ax        ;because they want total bytes.
  409.     clc
  410.     ret
  411.  
  412.  
  413. f_get_statistics:
  414.     call    verify_handle        ;just in case.
  415.     mov    _DS[bp],ds
  416.     mov    _SI[bp],offset statistics_list
  417.     clc
  418.     ret
  419.  
  420.  
  421. access_type_class:
  422.     mov    dh,NO_CLASS
  423.     stc
  424.     ret
  425.  
  426. access_type_type:
  427.     mov    dh,NO_TYPE
  428.     stc
  429.     ret
  430.  
  431. access_type_number:
  432.     mov    dh,NO_NUMBER
  433.     stc
  434.     ret
  435.  
  436. access_type_bad:
  437.     mov    dh,BAD_TYPE
  438.     stc
  439.     ret
  440.  
  441. ;register caller of pkt TYPE
  442. f_access_type:
  443.     mov    bx, offset driver_class
  444. access_type_9:
  445.     mov    al, [bx]        ;get the next class.
  446.     inc    bx
  447.     or    al,al            ;end of the list?
  448.     je    access_type_class    ;class failed (story of my life)
  449.     cmp    _AL[bp],al        ;our class?
  450.     jne    access_type_9        ;no, try again
  451. access_type_1:
  452.     cmp    _BX[bp],-1        ;generic type?
  453.     je    access_type_2        ;yes.
  454.     mov    al,driver_type
  455.     cbw
  456.     cmp    _BX[bp],ax        ;our type?
  457.     jne    access_type_type    ;no.
  458. access_type_2:
  459.     cmp    _DL[bp],0        ;generic number?
  460.     je    access_type_3
  461.     cmp    _DL[bp],1        ;our number?
  462.     jne    access_type_number
  463. access_type_3:
  464.     cmp    _CX[bp],MAX_P_LEN    ;is the type length too long?
  465.     ja    access_type_bad        ;yes - can't be ours.
  466.  
  467. ; now we do two things--look for an open handle, and check the existing
  468. ; handles to see if they're replicating a packet type.
  469.  
  470.     mov    free_handle,0        ;remember no free handle yet.
  471.     mov    bx,offset handles
  472. access_type_4:
  473.     cmp    [bx].in_use,0        ;is this handle in use?
  474.     je    access_type_5        ;no - don't check the type.
  475.     mov    al, _AL[bp]        ;is this handle the same class as
  476.     cmp    al, [bx].class        ;  they're want?
  477.     jne    short access_type_6
  478.     mov    es,_DS[bp]        ;get a pointer to their type
  479.     mov    di,_SI[bp]        ;  from their ds:si to our es:di
  480.     mov    cx,_CX[bp]        ;get the minimum of their length
  481.                     ;  and our length.  As currently
  482.                     ;  implemented, only one receiver
  483.                     ;  gets the packets, so we have to
  484.                     ;  ensure that the shortest prefix
  485.                     ;  is unique.
  486.     cmp    cx,[bx].packet_type_len    ;Are we less specific than they are?
  487.     jb    access_type_8        ;no.
  488.     mov    cx,[bx].packet_type_len    ;yes - use their count.
  489. access_type_8:
  490.     lea    si,[bx].packet_type
  491.     or    cx,cx            ; pass-all TYPE? (zero TYPE length)
  492.     jne    access_type_7        ; ne = no
  493.     mov    bx,offset handles+(MAX_HANDLE-1)*(size per_handle)
  494.     jmp    short access_type_5    ; put pass-all last
  495. access_type_7:
  496.     repe    cmpsb
  497.     jne    short access_type_6    ;go look at the next one.
  498. access_type_inuse:
  499.     mov    dh,TYPE_INUSE        ;a handle has been assigned for TYPE
  500.     stc                ;and we can't assign another
  501.     ret
  502. access_type_5:                ;handle is not in use
  503.     cmp    free_handle,0        ;found a free handle yet?
  504.     jne    access_type_6        ;yes.
  505.     mov    free_handle,bx        ;remember a free handle
  506. access_type_6:
  507.     add    bx,(size per_handle)    ;go to the next handle.
  508.     cmp    bx,offset end_handles    ;examined all handles?
  509.     jb    access_type_4        ;no, continue.
  510.  
  511.     mov    bx,free_handle        ;did we find a free handle?
  512.     or    bx,bx
  513.     je    access_type_space    ;no - return error.
  514.  
  515.     mov    [bx].in_use,1        ;remember that we're using it.
  516.  
  517.     mov    ax,_DI[bp]        ;remember the receiver type.
  518.     mov    [bx].receiver.offs,ax
  519.     mov    ax,_ES[bp]
  520.     mov    [bx].receiver.segm,ax
  521.  
  522.     push    ds
  523.     mov    ax,ds
  524.     mov    es,ax
  525.     mov    ds,_DS[bp]        ;remember their type.
  526.     mov    si,_SI[bp]
  527.     mov    cx,_CX[bp]
  528.     mov    es:[bx].packet_type_len,cx    ; remember the TYPE length
  529.     lea    di,[bx].packet_type
  530.     rep    movsb
  531.  
  532.     lds    si,es:[bx].receiver    ;copy the first 8 bytes
  533.     lea    di,[bx].receiver_sig    ; to the receiver signature.
  534.     mov    cx,8/2
  535.     rep    movsw
  536.  
  537.     pop    ds
  538.  
  539.     mov    al, _AL[bp]
  540.     mov    [bx].class, al
  541.  
  542.     mov    _AX[bp],bx        ;return the handle to them.
  543.  
  544.     clc
  545.     ret
  546.  
  547.  
  548. access_type_space:
  549.     mov    dh,NO_SPACE
  550.     stc
  551.     ret
  552.  
  553. f_release_type:
  554.     call    verify_handle        ;mark this handle as being unused.
  555.     mov    [bx].in_use,0
  556.     clc
  557.     ret
  558.  
  559.  
  560. f_send_pkt:
  561. ;ds:si -> buffer, cx = length
  562. ; XXX Should re-enable interrupts here, but some drivers are broken.
  563. ; Possibly re-enable interrupts.
  564. ;    test _F[bp], EI        ; Were interrupts enabled on pkt driver entry?
  565. ;    je    f_send_pkt_1    ; No.
  566. ;    sti            ; Yes, re-enable interrupts now.
  567. ;f_send_pkt_1:
  568.     push    ds        ; set up proper ds for the buffer
  569.     mov    ds,_DS[bp]    ; address of buffer from caller's ds.
  570.     assume    ds:nothing, es:nothing
  571. ;following two instructions not needed because si and cx haven't been changed.
  572. ;    mov    si,_SI[bp]
  573. ;    mov    cx,_CX[bp]    ; count of bytes in the packet.
  574.     linc    packets_out
  575.     add    bytes_out.offs,cx    ;add up the received bytes.
  576.     adc    bytes_out.segm,0
  577.  
  578. ; If -n option take Ethernet encapsulated Novell IPX packets (from BYU's 
  579. ; PDSHELL) and change them to be IEEE 802.3 encapsulated.
  580. EPROT_OFF    equ    EADDR_LEN*2
  581.     test    cs:flagbyte,N_OPTION
  582.     jz    f_send_pkt_2
  583.     cmp    ds:[si].EPROT_OFF,3781h ; if not Novell (prot 8137)
  584.     jne    f_send_pkt_2        ;  don't tread on it
  585.     push    ax            ; get scratch reg
  586.     mov    ax,[si].EPROT_OFF+4    ; get len
  587.     xchg    ah,al
  588.     inc    ax            ; make even (rounding up)
  589.     and    al,0feh
  590.     xchg    ah,al
  591.     mov    ds:[si].EPROT_OFF,ax    ; save in prot field
  592.     pop    ax            ; restore old contents
  593. f_send_pkt_2:
  594.     call    send_pkt
  595.     pop    ds
  596.     assume    ds:code
  597.     ret
  598.  
  599.  
  600. f_as_send_pkt:
  601. ;es:di -> iocb.
  602.     test    driver_function,4    ; is this a high-performance driver?
  603.     je    f_as_send_pkt_2        ; no.
  604. ; Possibly re-enable interrupts.
  605.     test _F[bp], EI            ; Were interrupts enabled on pkt driver entry?
  606.     je    f_as_send_pkt_1        ; No.
  607.     sti                ; Yes, re-enable interrupts now.
  608. f_as_send_pkt_1:
  609.     push    ds            ; set up proper ds for the buffer
  610.     lds    si,es:[di].buffer    ; ds:si -> buffer
  611.     assume    ds:nothing
  612.     mov    cx,es:[di].len        ; cx = length
  613.     linc    packets_out
  614.     add    bytes_out.offs,cx    ; add up the received bytes.
  615.     adc    bytes_out.segm,0
  616.  
  617. ;ds:si -> buffer, cx = length, es:di -> iocb.
  618.     call    as_send_pkt
  619.     pop    ds
  620.     assume    ds:code
  621.     ret
  622. f_as_send_pkt_2:
  623.     mov dh,    BAD_COMMAND        ; return an error.
  624.     stc
  625.     ret
  626.  
  627.  
  628. f_drop_pkt:
  629. ; es:di -> iocb.
  630.     test    driver_function,4    ; is this a high-performance driver?
  631.     je    f_as_send_pkt_1        ; no.
  632.     push    ds            ; Preserve ds
  633.     mov    si,offset send_head    ; Get head offset
  634. dp_loop:
  635.     mov    ax,ds:[si]        ; Get offset
  636.     mov    dx,ds:[si+2]        ; Get segment
  637.     mov    bx,ax
  638.     or    bx,dx            ; End of list?
  639.     je    dp_endlist        ; Yes
  640.     cmp    ax,di            ; Offsets equal?
  641.     jne    dp_getnext        ; No
  642.     mov    bx,es
  643.     cmp    dx,bx            ; Segments equal?
  644.     jne    dp_getnext        ; No
  645.     call    drop_pkt        ; Pass to driver
  646.     les    di,es:[di].next        ; Get next segment:offset
  647.     mov    ds:[si],di        ; Set next offset
  648.     mov    ds:[si+2],es        ; Set next segment
  649.     pop    ds            ; Restore ds
  650.     clc
  651.     ret
  652. dp_getnext:
  653.     mov    ds,dx            ; Get next segment
  654.     mov    si,ax            ; Get next iocb offset
  655.     lea    si,ds:[si].next        ; Get next iocb next ptr offset
  656.     jmp    dp_loop            ; Try again
  657. dp_endlist:
  658.     pop    ds            ; Restore ds
  659.     mov    dh,BAD_IOCB        ; Return error
  660.     stc                ; Set carry
  661.     ret
  662.  
  663.  
  664. f_terminate:
  665.     call    verify_handle        ; must have a handle
  666.  
  667. f_terminate_1:
  668.     mov    [bx].in_use,0        ; mark handle as free
  669.     mov    bx,offset handles    ; check that all handles are free
  670. f_terminate_2:
  671.     cmp    [bx].in_use,0        ; is this handle free?
  672.     jne    f_terminate_4        ; ne = no, so can't exit completely
  673.     add    bx,(size per_handle)    ; next handle
  674.     cmp    bx,offset end_handles    ; examined all handles?
  675.     jb    f_terminate_2        ; b = no, continue examination
  676.  
  677. ;
  678. ; Now disable interrupts
  679. ;
  680.     mov    al,int_no
  681.     or    al,al            ;are they using a hardware interrupt?
  682.     je    f_terminate_no_irq    ;no.
  683.     call    maskint
  684.  
  685. ;
  686. ; Now return the interrupt to their handler.
  687. ;
  688.     mov    ah,25h            ;get the old interrupt into es:bx
  689.     mov    al,int_no
  690.     add    al,8
  691.     cmp    al,8+8            ;is it a slave 8259 interrupt?
  692.     jb    f_terminate_3        ;no.
  693.     add    al,70h - (8+8)        ;map it to the real interrupt.
  694. f_terminate_3:
  695.     push    ds
  696.     lds    dx,their_recv_isr
  697.     int    21h
  698.     pop    ds
  699.  
  700. f_terminate_no_irq:
  701.     call    terminate        ;terminate the hardware.
  702.  
  703.     mov    al,packet_int_no    ;release our_isr.
  704.     mov    ah,25h
  705.     push    ds
  706.     lds    dx,their_isr
  707.     int    21h
  708.     pop    ds
  709.  
  710. ;
  711. ; Now free our memory
  712. ;
  713.     push    cs
  714.     pop    es
  715.     mov    ah,49h
  716.     int    21h
  717.     clc
  718.     ret
  719. f_terminate_4:
  720.     mov    dh, CANT_TERMINATE
  721.     stc
  722.     ret
  723.  
  724.  
  725.  
  726. f_get_address:
  727. ;    call    verify_handle
  728. ;    mov    es,_ES[bp]        ; get new one
  729. ;    mov    di,_DI[bp]        ; get pointer, es:di is ready
  730.     mov    cx,_CX[bp]        ;Tell them how much room they have.
  731.     cmp    have_my_address,0    ;has our address been set?
  732.     jne    get_address_set        ;yes - go report it.
  733.     call    get_address        ;no, can we get the address?
  734.     jc    get_address_space    ;no - we must not have enough space.
  735.     mov    _CX[bp],cx        ;Tell them how long our address is.
  736.     clc
  737.     ret
  738. get_address_set:
  739.     cmp    cx,my_address_len    ;is there enough room?
  740.     jb    get_address_space    ;no.
  741.     mov    cx,my_address_len    ;yes - get our address length.
  742.     mov    _CX[bp],cx        ;Tell them how long our address is.
  743.     mov    si,offset my_address    ;copy it into their area.
  744.     rep    movsb
  745.     clc
  746.     ret
  747.  
  748. get_address_space:
  749.     mov    dh,NO_SPACE
  750.     stc
  751.     ret
  752.  
  753.  
  754. f_set_address:
  755.     mov    bx,offset handles
  756.     mov    cl,0            ;number of handles in use.
  757. f_set_address_1:
  758.     add    cl,[bx].in_use        ;is this handle in use?
  759.     add    bx,(size per_handle)    ;go to the next handle.
  760.     cmp    bx,offset end_handles
  761.     jb    f_set_address_1
  762.  
  763.     cmp    cl,1            ;more than one handle in use?
  764.     ja    f_set_address_inuse    ;yes - we can't set the address
  765.  
  766.     mov    ds,_ES[bp]        ; set new one
  767.     assume    ds:nothing
  768.     mov    si,_DI[bp]        ; set pointer, ds:si is ready
  769.     mov    cx,_CX[bp]        ;Tell them how much address is being set.
  770.     call    set_address
  771. ;set_address restores ds.
  772.     jc    f_set_address_exit    ;Did it work?
  773.     mov    _CX[bp],cx        ;yes - return our address length.
  774.  
  775.     cmp    cx,MAX_ADDR_LEN        ;is it too long for us to remember?
  776.     ja    f_set_address_too_long    ;yes, return a too-long error.
  777.  
  778.     mov    ds,_ES[bp]        ; set new one
  779.     mov    si,_DI[bp]        ; set pointer, ds:si is ready
  780.     mov    ax,cs
  781.     mov    es,ax
  782.     mov    my_address_len,cx    ;remember how long our address is.
  783.     mov    di,offset my_address
  784.     rep    movsb
  785.     mov    have_my_address,1
  786.     mov    ds,ax            ;restoer ds.
  787.     assume    ds:code
  788.     clc
  789.     ret
  790. f_set_address_inuse:
  791.     mov    dh,CANT_SET
  792.     stc
  793.     ret
  794. f_set_address_too_long:
  795.     mov    dh,NO_SPACE
  796.     stc
  797. f_set_address_exit:
  798.     ret
  799.  
  800.  
  801. f_reset_interface:
  802.     call    verify_handle
  803.     call    reset_interface
  804.     clc
  805.     ret
  806.  
  807.  
  808. ; Stop the packet driver doing upcalls. Also a following terminate will
  809. ; always succed (no in use handles any longer).
  810. f_stop:
  811.     mov    bx,offset handles
  812. f_stop_2:
  813.     mov    [bx].in_use,0
  814.     add    bx,(size per_handle)    ; next handle
  815.     cmp    bx,offset end_handles
  816.     jb    f_stop_2
  817.     clc
  818.     ret
  819.  
  820.  
  821. f_get_parameters:
  822. ;strictly speaking, this function only works for high-performance drivers.
  823.     test    driver_function,4    ;is this a high-performance driver?
  824.     jne    f_get_parameters_1    ;yes.
  825.     mov    dh,BAD_COMMAND        ;no - return an error.
  826.     stc
  827.     ret
  828. f_get_parameters_1:
  829.     mov    _ES[bp],cs
  830.     mov    _DI[bp],offset parameter_list
  831.     clc
  832.     ret
  833.  
  834.  
  835. verify_handle:
  836. ;Ensure that their handle is real.  If it isn't, we pop off our return
  837. ;address, and return to *their* return address with cy set.
  838.     mov    bx,_BX[bp]        ;get the handle they gave us
  839.     cmp    bx,offset handles
  840.     jb    verify_handle_bad    ;no - must be bad.
  841.     cmp    bx,offset end_handles
  842.     jae    verify_handle_bad    ;no - must be bad.
  843.     cmp    [bx].in_use,0        ;if it's not in use, it's bad.
  844.     je    verify_handle_bad
  845.     ret
  846. verify_handle_bad:
  847.     mov    dh,BAD_HANDLE
  848.     add    sp,2            ;pop off our return address.
  849.     stc
  850.     ret
  851.  
  852.  
  853.     public    set_recv_isr
  854. set_recv_isr:
  855.     mov    ah,35h            ;get the old interrupt into es:bx
  856.     mov    al,int_no        ; board's interrupt vector
  857.     or    al,al
  858.     je    set_isr_no_irq
  859.     add    al,8
  860.     cmp    al,8+8            ;is it a slave 8259 interrupt?
  861.     jb    set_recv_isr_1        ;no.
  862.     add    al,70h - 8 - 8        ;map it to the real interrupt.
  863. set_recv_isr_1:
  864.     int    21h
  865.     mov    their_recv_isr.offs,bx    ;remember the old seg:off.
  866.     mov    their_recv_isr.segm,es
  867.  
  868.     mov    ah,25h            ;now set our recv interrupt.
  869.     mov    dx,offset recv_isr
  870.     int    21h
  871.  
  872.     mov    al,int_no        ; Now enable interrupts
  873.     call    unmaskint
  874.  
  875. set_isr_no_irq:
  876.     ret
  877.  
  878.     public    count_in_err
  879. count_in_err:
  880.     assume    ds:nothing
  881.     linc    errors_in
  882.     ret
  883.  
  884.     public    count_out_err
  885. count_out_err:
  886.     assume    ds:nothing
  887.     linc    errors_out
  888.     ret
  889.  
  890. their_recv_isr    dd    0        ; original owner of board int
  891.  
  892. recv_isr:
  893. ; In order to achieve back-to-back packet transmissions, we handle the
  894. ; latency-critical portion of transmit interrupts first.  The xmit
  895. ; interrupt routine should only start the next transmission, but do
  896. ; no other work.  It may only touch ax and dx (the only register necessary
  897. ; for doing "out" instructions) unless it first pushes any other registers
  898. ; itself.
  899.     push    ax
  900.     push    dx
  901.     call    xmit
  902.  
  903. ; Now switch stacks, push remaining registers, and do remaining interrupt work.
  904.     push    ds
  905.     mov    ax,cs            ;ds = cs.
  906.     mov    ds,ax
  907.     assume    ds:code
  908.  
  909.     mov    savesp,sp
  910.     mov    savess,ss
  911.  
  912.     mov    ss,ax
  913.     mov    sp,offset our_stack
  914.     cld
  915.  
  916.     push    bx
  917.     push    cx
  918.     push    si
  919.     push    di
  920.     push    bp
  921.     push    es
  922.  
  923. ; The following comment is wrong in that we now do a specific EOI command,
  924. ; and because we don't enable interrupts (even though we should).
  925.  
  926. ; Chips & Technologies 8259 clone chip seems to be very broken.  If you
  927. ; send it a Non Specific EOI command, it clears all In Service Register
  928. ; bits instead of just the one with the highest priority (as the Intel
  929. ; chip does and clones should do).  This bug causes our interrupt
  930. ; routine to be reentered if: 1. we reenable processor interrupts;
  931. ; 2. we reenable device interrupts; 3. a timer or other higher priority
  932. ; device interrupt now comes in; 4. the new interrupting device uses
  933. ; a Non Specific EOI; 5. our device interrupts again.  Because of
  934. ; this bug, we now completely mask our interrupts around the call
  935. ; to "recv", the real device interrupt handler.  This allows us
  936. ; to send an EOI instruction to the 8259 early, before we actually
  937. ; reenable device interrupts.  Since the interrupt is masked, we
  938. ; are still guaranteed not to get another interrupt from our device
  939. ; until the interrupt handler returns.  This has another benefit:
  940. ; we now no longer prevent other devices from interrupting while our
  941. ; interrupt handler is running.  This is especially useful if we have
  942. ; other (multiple) packet drivers trying to do low-latency transmits.
  943.     mov    al,int_no    ; Disable further device interrupts
  944.     call    maskint
  945.  
  946. ; The following is from Bill Rust, <wjr@ftp.com>
  947. ; this code dismisses the interrupt at the 8259. if the interrupt number
  948. ;  is > 8 then it requires fondling two PICs instead of just one.
  949.     mov    al, int_no    ; get hardware int #
  950.     cmp    al, 8        ; see if its on secondary PIC
  951.     jg    recv_isr_4
  952.     add    al, 60h        ; make specific EOI dismissal
  953.     out    20h, al
  954.     jmp    recv_isr_3    ; all done
  955. recv_isr_4:
  956.     add    al,60h - 8    ; make specific EOI (# between 9 & 15).
  957.     out    0a0h,al        ; Secondary 8259 (PC/AT only)
  958.     mov    al,62h        ; Acknowledge on primary 8259.
  959.     out    20h,al
  960. recv_isr_3:
  961.  
  962. ;    sti                ; Interrupts are now completely safe
  963.     call    recv
  964.  
  965.     cli                ;interrupts *must* be off between
  966.                     ;here and the stack restore, because
  967.                     ;if we have one of our interrupts
  968.                     ;pending, we would trash our stack.
  969.     mov    al,int_no    ; Now reenable device interrupts
  970.     call    unmaskint
  971.  
  972.     pop    es
  973.     pop    bp
  974.     pop    di
  975.     pop    si
  976.     pop    cx
  977.     pop    bx
  978.  
  979.     mov    ss,savess
  980.     mov    sp,savesp
  981.  
  982.     call    recv_exiting        ;this routine can enable interrupts.
  983. ; DDP - This is a BIG mistake.  This routine SHOULD NOT enable interrupts.
  984. ;    doing so can cause interrupt recursion and blow your stack.
  985. ;    Processor interrupts SHOULD NOT be enabled after enabling device
  986. ;    interrupts until after the "iret".  You will lose atleast 12 bytes
  987. ;    on the stack for each recursion.
  988.  
  989.     pop    ds
  990.     assume    ds:nothing
  991.     pop    dx
  992.     pop    ax
  993.     iret
  994.  
  995.  
  996.     public    maskint
  997. maskint:
  998.     or    al,al            ;are they using a hardware interrupt?
  999.     je    maskint_1        ;no, don't mask off the timer!
  1000.  
  1001.     assume    ds:code
  1002.     mov    dx,21h            ;assume the master 8259.
  1003.     cmp    al,8            ;using the slave 8259 on an AT?
  1004.     jb    mask_not_irq2
  1005.     mov    dx,0a1h            ;go disable it on slave 8259
  1006.     sub    al,8
  1007. mask_not_irq2:
  1008.     mov    cl,al
  1009.  
  1010.     in    al,dx            ;disable them on the correct 8259.
  1011.     mov    ah,1            ;set the bit.
  1012.     shl    ah,cl
  1013.     or    al,ah
  1014.     out    dx,al
  1015. maskint_1:
  1016.     ret
  1017.  
  1018.  
  1019.     public    unmaskint
  1020. unmaskint:
  1021.     assume    ds:code
  1022.     mov    dx,21h            ;assume the master 8259.
  1023.     mov    cl,al
  1024.     cmp    cl,8            ;using the slave 8259 on an AT?
  1025.     jb    unmask_not_irq2        ;no
  1026.     in    al,dx            ;get master mask
  1027.     and    al,not (1 shl 2)    ; and clear slave cascade bit in mask
  1028.     out    dx,al            ;set new master mask (enable slave int)
  1029.     mov    dx,0a1h            ;go enable int on slave 8259
  1030.     sub    cl,8
  1031. unmask_not_irq2:
  1032.  
  1033.     in    al,dx            ;enable interrupts on the correct 8259.
  1034.     mov    ah,1            ;clear the bit.
  1035.     shl    ah,cl
  1036.     not    ah
  1037.     and    al,ah
  1038.     out    dx,al
  1039.  
  1040.     ret
  1041.  
  1042.  
  1043.     public    recv_find
  1044. recv_find:
  1045. ;called when we want to determine what to do with a received packet.
  1046. ;enter with cx = packet length, es:di -> packet type, dl = packet class.
  1047. ;exit with es:di = 0 if the packet is not desired, or es:di -> packet buffer
  1048. ;  to be filled by the driver.
  1049.     assume    ds:code, es:nothing
  1050.     push    cx
  1051.  
  1052. ; If -n option take IEEE 802.3 encapsulated packets that could be Novell IPX 
  1053. ; and make them Ethernet encapsulated Novell IPX packets (for PDSHELL).
  1054.     test    flagbyte,N_OPTION
  1055.     jz    not_n_op
  1056.  
  1057. ; Make IEEE 802.3-like packets that could be Novell IPX into BlueBook class
  1058. ; Novell type 8137 packets.
  1059.     cmp    dl,IEEE8023        ;Is this an IEEE 802.3 packet?
  1060.     jne    recv_not_802_3        ;no
  1061.     cmp    word ptr es:[di],0ffffh    ;if this word not ffff
  1062.     jne    recv_not_8137        ;  then not Novell
  1063.     sub    di,2            ; back it up to the 8137 word.
  1064.     mov    es:[di],3781h        ; fake as Novell protocol (8137)
  1065.     mov    dl,BLUEBOOK
  1066.     jmp    short recv_not_8137
  1067. recv_not_802_3:
  1068. ; Convert incoming Ethernet type 8137 IPX packets to type 8138, as with -n in 
  1069. ; effect we can't send type 8137, and it will only confuse Netware.
  1070.     cmp    dl,BLUEBOOK        ;Is this a BLUEBOOK packet?
  1071.     jne    recv_not_8137        ;no, don't change it.
  1072.     cmp    word ptr es:[di],3781h    ;Is it an 8137 packet?
  1073.     jne    recv_not_8137        ;no, don't change it.
  1074.     mov    es:[di],word ptr 3881h    ;yes, mung it slightly.
  1075. recv_not_8137:
  1076. not_n_op:
  1077.  
  1078.     mov    bx,offset handles
  1079. recv_find_1:
  1080.     cmp    [bx].in_use,0        ;is this handle in use?
  1081.     je    recv_find_2        ;no - don't check the type.
  1082.  
  1083.     mov    ax,[bx].receiver.offs    ;do they have a receiver?
  1084.     or    ax,[bx].receiver.segm
  1085.     je    recv_find_2        ;no - they're not serious about it.
  1086.  
  1087.     mov    cx,[bx].packet_type_len    ;compare the packets.
  1088.     lea    si,[bx].packet_type
  1089.     jcxz    recv_find_3        ;if cx is zero, they want them all.
  1090.  
  1091.     cmp    [bx].class, dl        ;is this the right class?
  1092.     jne    recv_find_2        ;no- don't bother
  1093.  
  1094.     push    di
  1095.     repe    cmpsb
  1096.     pop    di
  1097.     je    recv_find_3        ;we've got it!
  1098. recv_find_2:
  1099.     add    bx,(size per_handle)    ;go to the next handle.
  1100.     cmp    bx,offset end_handles
  1101.     jb    recv_find_1
  1102.  
  1103.     linc    packets_dropped
  1104.  
  1105.     pop    cx            ;we didn't find it -- discard it.
  1106. recv_find_5:
  1107.     xor    di,di            ;"return" a null pointer.
  1108.     mov    es,di
  1109.     ret
  1110. recv_find_3:
  1111.     pop    cx            ; the packet_length
  1112.  
  1113.     linc    packets_in
  1114.     add    bytes_in.offs,cx    ;add up the received bytes.
  1115.     adc    bytes_in.segm,0
  1116.  
  1117.     les    di,[bx].receiver    ;remember the receiver upcall.
  1118.     mov    receive_ptr.offs,di
  1119.     mov    receive_ptr.segm,es
  1120.  
  1121.     test    flagbyte,W_OPTION    ;did they select the Windows option?
  1122.     je    recv_find_6        ;no, don't check for the upcall.
  1123.  
  1124. ; does the receiver signature match whats currently in memory?  if not,
  1125. ; jump to fake return
  1126.     push    si
  1127.     push    cx
  1128.     lea    si,[bx].receiver_sig
  1129.     mov    cx,8/2
  1130.     repe    cmpsw
  1131.     pop    cx
  1132.     pop    si
  1133.     jne    recv_find_5
  1134. recv_find_6:
  1135.  
  1136.     mov    found_handle,bx        ;remember what our handle was.
  1137.     mov    ax,0            ;allocate request.
  1138.     stc                ;with stc, flags must be an odd number
  1139.     push    ax            ; save a number that cant be flags
  1140.     pushf                ;save flags in case iret used.
  1141.     call    receive_ptr        ;ask the client for a buffer.
  1142.     ; on return, flags should be at top of stack. if an IRET has been used,
  1143.     ; then 0 will be at the top of the stack
  1144.     pop    bx
  1145.     cmp    bx,0
  1146.     je    recv_find_4        ;0 is at top of stack
  1147.     add    sp,2
  1148. recv_find_4:
  1149.     ret
  1150.  
  1151.  
  1152.     public    recv_copy
  1153. recv_copy:
  1154. ;called after we have copied the packet into the buffer.
  1155. ;enter with ds:si ->the packet, cx = length of the packet.
  1156. ;preserve bx.
  1157.     assume    ds:nothing, es:nothing
  1158.  
  1159.     push    bx
  1160.     mov    bx,found_handle
  1161.     mov    ax,1            ;store request.
  1162.     clc                ;with clc, flags must be an even number
  1163.     push    ax            ; save a number that can't be flags
  1164.     pushf                ;save flags incase iret used.
  1165.     call    receive_ptr        ;ask the client for a buffer.
  1166.     pop    bx
  1167.     cmp    bx,1            ;if this is a 1, IRET was used.
  1168.     je    recv_copy_1
  1169.     pop    bx
  1170. recv_copy_1:
  1171.     pop    bx
  1172.     ret
  1173.  
  1174.     public    send_queue
  1175. send_queue:
  1176. ; Queue an iocb.
  1177. ; Enter with es:di -> iocb, interrupts disabled.
  1178. ; Destroys ds:si.
  1179.     assume    ds:nothing, es:nothing
  1180.     mov    es:[di].next.offs,0    ; Zero next offset
  1181.     mov    es:[di].next.segm,0    ; Zero next segment
  1182.     mov    si,send_head.offs    ; Queue empty?
  1183.     or    si,send_head.segm
  1184.     jnz    sq_notempty        ; No
  1185.     mov    send_head.offs,di    ; Set head offset
  1186.     mov    send_head.segm,es    ; Set head segment
  1187.     jmp    sq_settail
  1188. sq_notempty:                ; Queue is not empty
  1189.     lds    si,send_tail        ; Get tail segment:offset
  1190.     mov    ds:[si].next.offs,di    ; Set next offset
  1191.     mov    ds:[si].next.segm,es    ; Set next segment
  1192. sq_settail:
  1193.     mov    send_tail.offs,di    ; Set tail offset
  1194.     mov    send_tail.segm,es    ; Set tail segment
  1195.     ret
  1196.  
  1197.  
  1198.     public    send_dequeue
  1199. send_dequeue:
  1200. ; Dequeue an iocb and possibly call its upcall.
  1201. ; Enter with device or processor interrupts disabled, ah = return code.
  1202. ; Exits with es:di -> iocb; destroys ds:si, ax, bx, cx, dx, bp.
  1203.     assume    ds:nothing, es:nothing
  1204.     les    di,send_head        ; Get head segment:offset
  1205.     lds    si,es:[di].next        ; Get next segment:offset
  1206.     mov    send_head.offs, si    ; Set head offset
  1207.     mov    send_head.segm, ds    ; Set head segment
  1208.     or    es:flags[di], DONE    ; Mark done
  1209.     mov    es:ret_code[di], ah    ; Set retcode
  1210.     test    es:[di].flags,CALLME    ; Does he want an upcall?
  1211.     je    send_dequeue_1        ; No.
  1212.     push    es            ; Push iocb segment
  1213.     push    di            ;  and offset
  1214.     clc                ; Clear carry.
  1215.     mov    ax,1            ; Push a number that cant be flags.
  1216.     push    ax
  1217.     pushf                ; Save flags in case iret used.
  1218.     call    es:[di].upcall        ; Call the client.
  1219.     pop    ax            ; Pop first word.
  1220.     cmp    ax,1            ; If this is a 1, IRET was used.
  1221.     je    send_dequeue_2        ; Far return used.
  1222.     add    sp,2            ; Pop flags.
  1223. send_dequeue_2:
  1224.     pop    di            ; Pop iocb segment
  1225.     pop    es            ;  and offset
  1226. send_dequeue_1:
  1227.     ret
  1228.  
  1229.  
  1230. code    ends
  1231.  
  1232.     end    start
  1233.