Merge from vendor branch HEIMDAL:
[dragonfly.git] / sys / dev / atm / hfa / fore_init.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/sys/dev/hfa/fore_init.c,v 1.6 1999/08/29 10:28:09 bde Exp $
27  *      @(#) $DragonFly: src/sys/dev/atm/hfa/fore_init.c,v 1.6 2005/02/01 00:51:50 joerg Exp $
28  */
29
30 /*
31  * FORE Systems 200-Series Adapter Support
32  * ---------------------------------------
33  *
34  * Cell Processor (CP) initialization routines
35  *
36  */
37
38 #include "fore_include.h"
39
40 /*
41  * Local functions
42  */
43 static void     fore_get_prom (Fore_unit *);
44
45 /*
46  * Begin CP Initialization
47  *
48  * This function will poll for the successful downloading and starting of
49  * the CP microcode program.  After the microcode is running, we will allocate
50  * any needed kernel memory (must do it in non-interrupt mode), build the CP
51  * queue configurations and issue an Initialize command to the CP.
52  *
53  * Arguments:
54  *      fup             pointer to device unit structure
55  *
56  * Returns:
57  *      none
58  */
59 void
60 fore_initialize(void *xfup)
61 {
62         Fore_unit       *fup = xfup;
63         Aali            *aap;
64         Init_parms      *inp;
65         caddr_t         errmsg;
66         u_long          vers;
67
68         /*
69          * Must wait until firmware has been downloaded and is running
70          */
71         if (CP_READ(fup->fu_mon->mon_bstat) != BOOT_RUNNING) {
72
73                 /*
74                  * Try again later
75                  */
76                 callout_reset(&fup->fu_init_timer, hz, fore_initialize, fup);
77                 return;
78         }
79
80         /*
81          * Allocate queues and whatever else is needed
82          */
83         if (fore_xmit_allocate(fup)) {
84                 errmsg = "transmit queue allocation";
85                 goto failed;
86         }
87         if (fore_recv_allocate(fup)) {
88                 errmsg = "receive queue allocation";
89                 goto failed;
90         }
91         if (fore_buf_allocate(fup)) {
92                 errmsg = "buffer supply queue allocation";
93                 goto failed;
94         }
95         if (fore_cmd_allocate(fup)) {
96                 errmsg = "command queue allocation";
97                 goto failed;
98         }
99
100         /*
101          * CP microcode is downloaded - locate shared memory interface 
102          */
103         aap = (Aali *)(fup->fu_ram + CP_READ(fup->fu_mon->mon_appl));
104         fup->fu_aali = aap;
105
106         /*
107          * Pick out any interesting info from the microcode
108          */
109         vers = CP_READ(aap->aali_ucode_ver);
110         if (vers < FORE_MIN_UCODE) {
111                 errmsg = "unsupported microcode version";
112                 goto failed;
113         }
114         snprintf(fup->fu_config.ac_firm_vers,
115             sizeof(fup->fu_config.ac_firm_vers), "%ld.%ld.%ld",
116                 (vers >> 16) & 0xff, (vers >> 8) & 0xff, vers & 0xff);
117
118 #ifdef notdef
119         /*
120          * Turn on CP debugging
121          */
122         aap->aali_hostlog = 1;
123 #endif
124
125         /*
126          * Build the initialization block
127          */
128         inp = &aap->aali_init;
129         inp->init_numvcc = CP_WRITE(FORE_MAX_VCC);
130         inp->init_cmd_elem = CP_WRITE(CMD_QUELEN);
131         inp->init_xmit_elem = CP_WRITE(XMIT_QUELEN);
132         inp->init_recv_elem = CP_WRITE(RECV_QUELEN);
133         inp->init_recv_ext = CP_WRITE(RECV_EXTRA_SEGS);
134         inp->init_xmit_ext = CP_WRITE(XMIT_EXTRA_SEGS);
135         inp->init_buf1s.bfs_quelen = CP_WRITE(BUF1_SM_QUELEN);
136         inp->init_buf1s.bfs_bufsize = CP_WRITE(BUF1_SM_SIZE);
137         inp->init_buf1s.bfs_cppool = CP_WRITE(BUF1_SM_CPPOOL);
138         inp->init_buf1s.bfs_entsize = CP_WRITE(BUF1_SM_ENTSIZE);
139         inp->init_buf1l.bfs_quelen = CP_WRITE(BUF1_LG_QUELEN);
140         inp->init_buf1l.bfs_bufsize = CP_WRITE(BUF1_LG_SIZE);
141         inp->init_buf1l.bfs_cppool = CP_WRITE(BUF1_LG_CPPOOL);
142         inp->init_buf1l.bfs_entsize = CP_WRITE(BUF1_LG_ENTSIZE);
143         inp->init_buf2s.bfs_quelen = CP_WRITE(0);
144         inp->init_buf2s.bfs_bufsize = CP_WRITE(0);
145         inp->init_buf2s.bfs_cppool = CP_WRITE(0);
146         inp->init_buf2s.bfs_entsize = CP_WRITE(0);
147         inp->init_buf2l.bfs_quelen = CP_WRITE(0);
148         inp->init_buf2l.bfs_bufsize = CP_WRITE(0);
149         inp->init_buf2l.bfs_cppool = CP_WRITE(0);
150         inp->init_buf2l.bfs_entsize = CP_WRITE(0);
151
152         /*
153          * Enable device interrupts
154          */
155         aap->aali_intr_ena = CP_WRITE(1);
156
157         /*
158          * Issue the Initialize command to the CP and wait for
159          * the CP to interrupt to signal completion
160          */
161         inp->init_status = CP_WRITE(QSTAT_PENDING);
162         inp->init_cmd = CP_WRITE(CMD_INIT | CMD_INTR_REQ);
163         return;
164
165 failed:
166         /*
167          * Initialization failure
168          */
169         fore_interface_free(fup);
170         log(LOG_ERR, "fore initialization failed: intf=%s%d, err=%s\n",
171                 fup->fu_pif.pif_name, fup->fu_pif.pif_unit, errmsg);
172         return;
173 }
174
175
176 /*
177  * Complete CP Initialization 
178  *
179  * Called after the CP has successfully completed processing of the 
180  * Initialize command.  We will now finish off our part of the 
181  * initialization process by setting up all the host-based queue 
182  * management structures.
183  *
184  * Called at interrupt level.
185  *
186  * Arguments:
187  *      fup             pointer to device unit structure
188  *
189  * Returns:
190  *      none
191  */
192 void
193 fore_initialize_complete(fup)
194         Fore_unit       *fup;
195 {
196         Aali            *aap = fup->fu_aali;
197
198         /*
199          * Log an initialization failure
200          */
201         if (CP_READ(aap->aali_init.init_status) & QSTAT_ERROR) {
202
203                 log(LOG_ERR, 
204                         "fore initialization failed: intf=%s%d, hbeat=0x%lx\n",
205                         fup->fu_pif.pif_name, fup->fu_pif.pif_unit,
206                         (u_long)CP_READ(aap->aali_heartbeat));
207                 return;
208         }
209
210         ATM_DEBUG1("heap=0x%lx\n", aap->aali_heap);
211         ATM_DEBUG1("heaplen=0x%lx\n", aap->aali_heaplen);
212         ATM_DEBUG1("cmd_q=0x%lx\n", aap->aali_cmd_q);
213         ATM_DEBUG1("xmit_q=0x%lx\n", aap->aali_xmit_q);
214         ATM_DEBUG1("recv_q=0x%lx\n", aap->aali_recv_q);
215         ATM_DEBUG1("buf1s_q=0x%lx\n", aap->aali_buf1s_q);
216         ATM_DEBUG1("buf1l_q=0x%lx\n", aap->aali_buf1l_q);
217         ATM_DEBUG1("buf2s_q=0x%lx\n", aap->aali_buf2s_q);
218         ATM_DEBUG1("buf2l_q=0x%lx\n", aap->aali_buf2l_q);
219
220         /*
221          * Initialize all of our queues
222          */
223         fore_xmit_initialize(fup);
224         fore_recv_initialize(fup);
225         fore_buf_initialize(fup);
226         fore_cmd_initialize(fup);
227
228         /*
229          * Mark device initialization completed
230          */
231         fup->fu_flags |= CUF_INITED;
232
233         fore_get_prom(fup);
234 }
235
236
237 /*
238  * Get device PROM values from CP
239  * 
240  * This function will issue a GET_PROM command to the CP in order to
241  * initiate the DMA transfer of the CP's PROM structure to the host.
242  * This will be called after CP initialization has completed.
243  * There is (currently) no retry if this fails.
244  *
245  * Called at interrupt level.
246  *
247  * Arguments:
248  *      fup     pointer to device unit structure
249  *
250  * Returns:
251  *      none
252  *
253  */
254 static void
255 fore_get_prom(fup)
256         Fore_unit       *fup;
257 {
258         H_cmd_queue     *hcp;
259         Cmd_queue       *cqp;
260
261         /*
262          * Queue command at end of command queue
263          */
264         hcp = fup->fu_cmd_tail;
265         if ((*hcp->hcq_status) & QSTAT_FREE) {
266
267                 /*
268                  * Queue entry available, so set our view of things up
269                  */
270                 hcp->hcq_code = CMD_GET_PROM;
271                 hcp->hcq_arg = NULL;
272                 fup->fu_cmd_tail = hcp->hcq_next;
273
274                 /*
275                  * Now set the CP-resident queue entry - the CP will grab
276                  * the command when the op-code is set.
277                  */
278                 cqp = hcp->hcq_cpelem;
279                 (*hcp->hcq_status) = QSTAT_PENDING;
280
281                 fup->fu_promd = DMA_GET_ADDR(fup->fu_prom, sizeof(Fore_prom),
282                         FORE_PROM_ALIGN, 0);
283                 if (fup->fu_promd == NULL) {
284                         fup->fu_stats->st_drv.drv_cm_nodma++;
285                         return;
286                 }
287                 cqp->cmdq_prom.prom_buffer = (CP_dma) CP_WRITE(fup->fu_promd);
288                 cqp->cmdq_prom.prom_cmd = CP_WRITE(CMD_GET_PROM | CMD_INTR_REQ);
289
290         } else {
291                 /*
292                  * Command queue full
293                  */
294                 fup->fu_stats->st_drv.drv_cm_full++;
295         }
296
297         return;
298 }