mpt.c revision 1.2 1 1.2 thorpej /* $NetBSD: mpt.c,v 1.2 2003/04/16 23:02:14 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2000, 2001 by Greg Ansley
5 1.1 thorpej *
6 1.1 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1 thorpej * modification, are permitted provided that the following conditions
8 1.1 thorpej * are met:
9 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1 thorpej * notice immediately at the beginning of the file, without modification,
11 1.1 thorpej * this list of conditions, and the following disclaimer.
12 1.1 thorpej * 2. The name of the author may not be used to endorse or promote products
13 1.1 thorpej * derived from this software without specific prior written permission.
14 1.1 thorpej *
15 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 1.1 thorpej * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 thorpej * SUCH DAMAGE.
26 1.1 thorpej */
27 1.1 thorpej /*
28 1.1 thorpej * Additional Copyright (c) 2002 by Matthew Jacob under same license.
29 1.1 thorpej */
30 1.1 thorpej
31 1.1 thorpej /*
32 1.1 thorpej * mpt.c:
33 1.1 thorpej *
34 1.1 thorpej * Generic routines for LSI Fusion adapters.
35 1.1 thorpej *
36 1.1 thorpej * Adapted from the FreeBSD "mpt" driver by Jason R. Thorpe for
37 1.1 thorpej * Wasabi Systems, Inc.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej #include <dev/ic/mpt.h>
41 1.1 thorpej
42 1.1 thorpej #define MPT_MAX_TRYS 3
43 1.1 thorpej #define MPT_MAX_WAIT 300000
44 1.1 thorpej
45 1.1 thorpej static int maxwait_ack = 0;
46 1.1 thorpej static int maxwait_int = 0;
47 1.1 thorpej static int maxwait_state = 0;
48 1.1 thorpej
49 1.1 thorpej static __inline u_int32_t
50 1.1 thorpej mpt_rd_db(mpt_softc_t *mpt)
51 1.1 thorpej {
52 1.1 thorpej return mpt_read(mpt, MPT_OFFSET_DOORBELL);
53 1.1 thorpej }
54 1.1 thorpej
55 1.1 thorpej static __inline u_int32_t
56 1.1 thorpej mpt_rd_intr(mpt_softc_t *mpt)
57 1.1 thorpej {
58 1.1 thorpej return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
59 1.1 thorpej }
60 1.1 thorpej
61 1.1 thorpej /* Busy wait for a door bell to be read by IOC */
62 1.1 thorpej static int
63 1.1 thorpej mpt_wait_db_ack(mpt_softc_t *mpt)
64 1.1 thorpej {
65 1.1 thorpej int i;
66 1.1 thorpej for (i=0; i < MPT_MAX_WAIT; i++) {
67 1.1 thorpej if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
68 1.1 thorpej maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
69 1.1 thorpej return MPT_OK;
70 1.1 thorpej }
71 1.1 thorpej
72 1.1 thorpej DELAY(100);
73 1.1 thorpej }
74 1.1 thorpej return MPT_FAIL;
75 1.1 thorpej }
76 1.1 thorpej
77 1.1 thorpej /* Busy wait for a door bell interrupt */
78 1.1 thorpej static int
79 1.1 thorpej mpt_wait_db_int(mpt_softc_t *mpt)
80 1.1 thorpej {
81 1.1 thorpej int i;
82 1.1 thorpej for (i=0; i < MPT_MAX_WAIT; i++) {
83 1.1 thorpej if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
84 1.1 thorpej maxwait_int = i > maxwait_int ? i : maxwait_int;
85 1.1 thorpej return MPT_OK;
86 1.1 thorpej }
87 1.1 thorpej DELAY(100);
88 1.1 thorpej }
89 1.1 thorpej return MPT_FAIL;
90 1.1 thorpej }
91 1.1 thorpej
92 1.1 thorpej /* Wait for IOC to transition to a give state */
93 1.1 thorpej void
94 1.1 thorpej mpt_check_doorbell(mpt_softc_t *mpt)
95 1.1 thorpej {
96 1.1 thorpej u_int32_t db = mpt_rd_db(mpt);
97 1.1 thorpej if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
98 1.1 thorpej mpt_prt(mpt, "Device not running");
99 1.1 thorpej mpt_print_db(db);
100 1.1 thorpej }
101 1.1 thorpej }
102 1.1 thorpej
103 1.1 thorpej /* Wait for IOC to transition to a give state */
104 1.1 thorpej static int
105 1.1 thorpej mpt_wait_state(mpt_softc_t *mpt, enum DB_STATE_BITS state)
106 1.1 thorpej {
107 1.1 thorpej int i;
108 1.1 thorpej
109 1.1 thorpej for (i = 0; i < MPT_MAX_WAIT; i++) {
110 1.1 thorpej u_int32_t db = mpt_rd_db(mpt);
111 1.1 thorpej if (MPT_STATE(db) == state) {
112 1.1 thorpej maxwait_state = i > maxwait_state ? i : maxwait_state;
113 1.1 thorpej return (MPT_OK);
114 1.1 thorpej }
115 1.1 thorpej DELAY(100);
116 1.1 thorpej }
117 1.1 thorpej return (MPT_FAIL);
118 1.1 thorpej }
119 1.1 thorpej
120 1.1 thorpej
121 1.1 thorpej /* Issue the reset COMMAND to the IOC */
122 1.1 thorpej int
123 1.1 thorpej mpt_soft_reset(mpt_softc_t *mpt)
124 1.1 thorpej {
125 1.1 thorpej if (mpt->verbose) {
126 1.1 thorpej mpt_prt(mpt, "soft reset");
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej /* Have to use hard reset if we are not in Running state */
130 1.1 thorpej if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
131 1.1 thorpej mpt_prt(mpt, "soft reset failed: device not running");
132 1.1 thorpej return MPT_FAIL;
133 1.1 thorpej }
134 1.1 thorpej
135 1.1 thorpej /* If door bell is in use we don't have a chance of getting
136 1.1 thorpej * a word in since the IOC probably crashed in message
137 1.1 thorpej * processing. So don't waste our time.
138 1.1 thorpej */
139 1.1 thorpej if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
140 1.1 thorpej mpt_prt(mpt, "soft reset failed: doorbell wedged");
141 1.1 thorpej return MPT_FAIL;
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej /* Send the reset request to the IOC */
145 1.1 thorpej mpt_write(mpt, MPT_OFFSET_DOORBELL,
146 1.1 thorpej MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
147 1.1 thorpej if (mpt_wait_db_ack(mpt) != MPT_OK) {
148 1.1 thorpej mpt_prt(mpt, "soft reset failed: ack timeout");
149 1.1 thorpej return MPT_FAIL;
150 1.1 thorpej }
151 1.1 thorpej
152 1.1 thorpej /* Wait for the IOC to reload and come out of reset state */
153 1.1 thorpej if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
154 1.1 thorpej mpt_prt(mpt, "soft reset failed: device did not start running");
155 1.1 thorpej return MPT_FAIL;
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej return MPT_OK;
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej /* This is a magic diagnostic reset that resets all the ARM
162 1.1 thorpej * processors in the chip.
163 1.1 thorpej */
164 1.1 thorpej void
165 1.1 thorpej mpt_hard_reset(mpt_softc_t *mpt)
166 1.1 thorpej {
167 1.1 thorpej /* This extra read comes for the Linux source
168 1.1 thorpej * released by LSI. It's function is undocumented!
169 1.1 thorpej */
170 1.1 thorpej if (mpt->verbose) {
171 1.1 thorpej mpt_prt(mpt, "hard reset");
172 1.1 thorpej }
173 1.1 thorpej mpt_read(mpt, MPT_OFFSET_FUBAR);
174 1.1 thorpej
175 1.1 thorpej /* Enable diagnostic registers */
176 1.1 thorpej mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPT_DIAG_SEQUENCE_1);
177 1.1 thorpej mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPT_DIAG_SEQUENCE_2);
178 1.1 thorpej mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPT_DIAG_SEQUENCE_3);
179 1.1 thorpej mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPT_DIAG_SEQUENCE_4);
180 1.1 thorpej mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPT_DIAG_SEQUENCE_5);
181 1.1 thorpej
182 1.1 thorpej /* Diag. port is now active so we can now hit the reset bit */
183 1.1 thorpej mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, MPT_DIAG_RESET_IOC);
184 1.1 thorpej
185 1.1 thorpej DELAY(10000);
186 1.1 thorpej
187 1.1 thorpej /* Disable Diagnostic Register */
188 1.1 thorpej mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
189 1.1 thorpej
190 1.1 thorpej /* Restore the config register values */
191 1.1 thorpej /* Hard resets are known to screw up the BAR for diagnostic
192 1.1 thorpej memory accesses (Mem1). */
193 1.1 thorpej mpt_set_config_regs(mpt);
194 1.1 thorpej if (mpt->mpt2 != NULL) {
195 1.1 thorpej mpt_set_config_regs(mpt->mpt2);
196 1.1 thorpej }
197 1.1 thorpej
198 1.1 thorpej /* Note that if there is no valid firmware to run, the doorbell will
199 1.1 thorpej remain in the reset state (0x00000000) */
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej /*
203 1.1 thorpej * Reset the IOC when needed. Try software command first then if needed
204 1.1 thorpej * poke at the magic diagnostic reset. Note that a hard reset resets
205 1.1 thorpej * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
206 1.1 thorpej * fouls up the PCI configuration registers.
207 1.1 thorpej */
208 1.1 thorpej int
209 1.1 thorpej mpt_reset(mpt_softc_t *mpt)
210 1.1 thorpej {
211 1.1 thorpej int ret;
212 1.1 thorpej
213 1.1 thorpej /* Try a soft reset */
214 1.1 thorpej if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
215 1.1 thorpej /* Failed; do a hard reset */
216 1.1 thorpej mpt_hard_reset(mpt);
217 1.1 thorpej
218 1.1 thorpej /* Wait for the IOC to reload and come out of reset state */
219 1.1 thorpej ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
220 1.1 thorpej if (ret != MPT_OK) {
221 1.1 thorpej mpt_prt(mpt, "failed to reset device");
222 1.1 thorpej }
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej return ret;
226 1.1 thorpej }
227 1.1 thorpej
228 1.1 thorpej /* Return a command buffer to the free queue */
229 1.1 thorpej void
230 1.1 thorpej mpt_free_request(mpt_softc_t *mpt, request_t *req)
231 1.1 thorpej {
232 1.1 thorpej if (req == NULL || req != &mpt->request_pool[req->index]) {
233 1.1 thorpej panic("mpt_free_request bad req ptr\n");
234 1.1 thorpej return;
235 1.1 thorpej }
236 1.1 thorpej req->sequence = 0;
237 1.1 thorpej req->xfer = NULL;
238 1.1 thorpej req->debug = REQ_FREE;
239 1.1 thorpej SLIST_INSERT_HEAD(&mpt->request_free_list, req, link);
240 1.1 thorpej }
241 1.1 thorpej
242 1.1 thorpej /* Get a command buffer from the free queue */
243 1.1 thorpej request_t *
244 1.1 thorpej mpt_get_request(mpt_softc_t *mpt)
245 1.1 thorpej {
246 1.1 thorpej request_t *req;
247 1.1 thorpej req = SLIST_FIRST(&mpt->request_free_list);
248 1.1 thorpej if (req != NULL) {
249 1.1 thorpej if (req != &mpt->request_pool[req->index]) {
250 1.1 thorpej panic("mpt_get_request: corrupted request free list\n");
251 1.1 thorpej }
252 1.1 thorpej if (req->xfer != NULL) {
253 1.1 thorpej panic("mpt_get_request: corrupted request free list (xfer)\n");
254 1.1 thorpej }
255 1.1 thorpej SLIST_REMOVE_HEAD(&mpt->request_free_list, link);
256 1.1 thorpej req->debug = REQ_IN_PROGRESS;
257 1.1 thorpej }
258 1.1 thorpej return req;
259 1.1 thorpej }
260 1.1 thorpej
261 1.1 thorpej /* Pass the command to the IOC */
262 1.1 thorpej void
263 1.1 thorpej mpt_send_cmd(mpt_softc_t *mpt, request_t *req)
264 1.1 thorpej {
265 1.1 thorpej req->sequence = mpt->sequence++;
266 1.1 thorpej if (mpt->verbose > 1) {
267 1.1 thorpej u_int32_t *pReq;
268 1.1 thorpej pReq = req->req_vbuf;
269 1.1 thorpej mpt_prt(mpt, "Send Request %d (0x%x):",
270 1.1 thorpej req->index, req->req_pbuf);
271 1.1 thorpej mpt_prt(mpt, "%08x %08x %08x %08x",
272 1.1 thorpej pReq[0], pReq[1], pReq[2], pReq[3]);
273 1.1 thorpej mpt_prt(mpt, "%08x %08x %08x %08x",
274 1.1 thorpej pReq[4], pReq[5], pReq[6], pReq[7]);
275 1.1 thorpej mpt_prt(mpt, "%08x %08x %08x %08x",
276 1.1 thorpej pReq[8], pReq[9], pReq[10], pReq[11]);
277 1.1 thorpej mpt_prt(mpt, "%08x %08x %08x %08x",
278 1.1 thorpej pReq[12], pReq[13], pReq[14], pReq[15]);
279 1.1 thorpej }
280 1.2 thorpej MPT_SYNC_REQ(mpt, req, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
281 1.1 thorpej req->debug = REQ_ON_CHIP;
282 1.1 thorpej mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (u_int32_t) req->req_pbuf);
283 1.1 thorpej }
284 1.1 thorpej
285 1.1 thorpej /*
286 1.1 thorpej * Give the reply buffer back to the IOC after we have
287 1.1 thorpej * finished processing it.
288 1.1 thorpej */
289 1.1 thorpej void
290 1.1 thorpej mpt_free_reply(mpt_softc_t *mpt, u_int32_t ptr)
291 1.1 thorpej {
292 1.1 thorpej mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
293 1.1 thorpej }
294 1.1 thorpej
295 1.1 thorpej /* Get a reply from the IOC */
296 1.1 thorpej u_int32_t
297 1.1 thorpej mpt_pop_reply_queue(mpt_softc_t *mpt)
298 1.1 thorpej {
299 1.1 thorpej return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
300 1.1 thorpej }
301 1.1 thorpej
302 1.1 thorpej /*
303 1.1 thorpej * Send a command to the IOC via the handshake register.
304 1.1 thorpej *
305 1.1 thorpej * Only done at initialization time and for certain unusual
306 1.1 thorpej * commands such as device/bus reset as specified by LSI.
307 1.1 thorpej */
308 1.1 thorpej int
309 1.1 thorpej mpt_send_handshake_cmd(mpt_softc_t *mpt, size_t len, void *cmd)
310 1.1 thorpej {
311 1.1 thorpej int i;
312 1.1 thorpej u_int32_t data, *data32;
313 1.1 thorpej
314 1.1 thorpej /* Check condition of the IOC */
315 1.1 thorpej data = mpt_rd_db(mpt);
316 1.1 thorpej if (((MPT_STATE(data) != MPT_DB_STATE_READY) &&
317 1.1 thorpej (MPT_STATE(data) != MPT_DB_STATE_RUNNING) &&
318 1.1 thorpej (MPT_STATE(data) != MPT_DB_STATE_FAULT)) ||
319 1.1 thorpej ( MPT_DB_IS_IN_USE(data) )) {
320 1.1 thorpej mpt_prt(mpt, "handshake aborted due to invalid doorbell state");
321 1.1 thorpej mpt_print_db(data);
322 1.1 thorpej return(EBUSY);
323 1.1 thorpej }
324 1.1 thorpej
325 1.1 thorpej /* We move things in 32 bit chunks */
326 1.1 thorpej len = (len + 3) >> 2;
327 1.1 thorpej data32 = cmd;
328 1.1 thorpej
329 1.1 thorpej /* Clear any left over pending doorbell interupts */
330 1.1 thorpej if (MPT_DB_INTR(mpt_rd_intr(mpt)))
331 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
332 1.1 thorpej
333 1.1 thorpej /*
334 1.1 thorpej * Tell the handshake reg. we are going to send a command
335 1.1 thorpej * and how long it is going to be.
336 1.1 thorpej */
337 1.1 thorpej data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
338 1.1 thorpej (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
339 1.1 thorpej mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
340 1.1 thorpej
341 1.1 thorpej /* Wait for the chip to notice */
342 1.1 thorpej if (mpt_wait_db_int(mpt) != MPT_OK) {
343 1.1 thorpej mpt_prt(mpt, "mpt_send_handshake_cmd timeout1");
344 1.1 thorpej return ETIMEDOUT;
345 1.1 thorpej }
346 1.1 thorpej
347 1.1 thorpej /* Clear the interrupt */
348 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
349 1.1 thorpej
350 1.1 thorpej if (mpt_wait_db_ack(mpt) != MPT_OK) {
351 1.1 thorpej mpt_prt(mpt, "mpt_send_handshake_cmd timeout2");
352 1.1 thorpej return ETIMEDOUT;
353 1.1 thorpej }
354 1.1 thorpej
355 1.1 thorpej /* Send the command */
356 1.1 thorpej for (i = 0; i < len; i++) {
357 1.1 thorpej mpt_write(mpt, MPT_OFFSET_DOORBELL, *data32++);
358 1.1 thorpej if (mpt_wait_db_ack(mpt) != MPT_OK) {
359 1.1 thorpej mpt_prt(mpt,
360 1.1 thorpej "mpt_send_handshake_cmd timeout! index = %d", i);
361 1.1 thorpej return ETIMEDOUT;
362 1.1 thorpej }
363 1.1 thorpej }
364 1.1 thorpej return MPT_OK;
365 1.1 thorpej }
366 1.1 thorpej
367 1.1 thorpej /* Get the response from the handshake register */
368 1.1 thorpej int
369 1.1 thorpej mpt_recv_handshake_reply(mpt_softc_t *mpt, size_t reply_len, void *reply)
370 1.1 thorpej {
371 1.1 thorpej int left, reply_left;
372 1.1 thorpej u_int16_t *data16;
373 1.1 thorpej MSG_DEFAULT_REPLY *hdr;
374 1.1 thorpej
375 1.1 thorpej /* We move things out in 16 bit chunks */
376 1.1 thorpej reply_len >>= 1;
377 1.1 thorpej data16 = (u_int16_t *)reply;
378 1.1 thorpej
379 1.1 thorpej hdr = (MSG_DEFAULT_REPLY *)reply;
380 1.1 thorpej
381 1.1 thorpej /* Get first word */
382 1.1 thorpej if (mpt_wait_db_int(mpt) != MPT_OK) {
383 1.1 thorpej mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1");
384 1.1 thorpej return ETIMEDOUT;
385 1.1 thorpej }
386 1.1 thorpej *data16++ = mpt_read(mpt, MPT_OFFSET_DOORBELL) & MPT_DB_DATA_MASK;
387 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
388 1.1 thorpej
389 1.1 thorpej /* Get Second Word */
390 1.1 thorpej if (mpt_wait_db_int(mpt) != MPT_OK) {
391 1.1 thorpej mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2");
392 1.1 thorpej return ETIMEDOUT;
393 1.1 thorpej }
394 1.1 thorpej *data16++ = mpt_read(mpt, MPT_OFFSET_DOORBELL) & MPT_DB_DATA_MASK;
395 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
396 1.1 thorpej
397 1.1 thorpej /* With the second word, we can now look at the length */
398 1.1 thorpej if (mpt->verbose > 1 && ((reply_len >> 1) != hdr->MsgLength)) {
399 1.1 thorpej mpt_prt(mpt, "reply length does not match message length: "
400 1.1 thorpej "got 0x%02x, expected 0x%02x",
401 1.1 thorpej hdr->MsgLength << 2, reply_len << 1);
402 1.1 thorpej }
403 1.1 thorpej
404 1.1 thorpej /* Get rest of the reply; but don't overflow the provided buffer */
405 1.1 thorpej left = (hdr->MsgLength << 1) - 2;
406 1.1 thorpej reply_left = reply_len - 2;
407 1.1 thorpej while (left--) {
408 1.1 thorpej u_int16_t datum;
409 1.1 thorpej
410 1.1 thorpej if (mpt_wait_db_int(mpt) != MPT_OK) {
411 1.1 thorpej mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3");
412 1.1 thorpej return ETIMEDOUT;
413 1.1 thorpej }
414 1.1 thorpej datum = mpt_read(mpt, MPT_OFFSET_DOORBELL);
415 1.1 thorpej
416 1.1 thorpej if (reply_left-- > 0)
417 1.1 thorpej *data16++ = datum & MPT_DB_DATA_MASK;
418 1.1 thorpej
419 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
420 1.1 thorpej }
421 1.1 thorpej
422 1.1 thorpej /* One more wait & clear at the end */
423 1.1 thorpej if (mpt_wait_db_int(mpt) != MPT_OK) {
424 1.1 thorpej mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4");
425 1.1 thorpej return ETIMEDOUT;
426 1.1 thorpej }
427 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
428 1.1 thorpej
429 1.1 thorpej if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
430 1.1 thorpej if (mpt->verbose > 1)
431 1.1 thorpej mpt_print_reply(hdr);
432 1.1 thorpej return (MPT_FAIL | hdr->IOCStatus);
433 1.1 thorpej }
434 1.1 thorpej
435 1.1 thorpej return (0);
436 1.1 thorpej }
437 1.1 thorpej
438 1.1 thorpej static int
439 1.1 thorpej mpt_get_iocfacts(mpt_softc_t *mpt, MSG_IOC_FACTS_REPLY *freplp)
440 1.1 thorpej {
441 1.1 thorpej MSG_IOC_FACTS f_req;
442 1.1 thorpej int error;
443 1.1 thorpej
444 1.1 thorpej bzero(&f_req, sizeof f_req);
445 1.1 thorpej f_req.Function = MPI_FUNCTION_IOC_FACTS;
446 1.1 thorpej f_req.MsgContext = 0x12071942;
447 1.1 thorpej error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
448 1.1 thorpej if (error)
449 1.1 thorpej return(error);
450 1.1 thorpej error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
451 1.1 thorpej return (error);
452 1.1 thorpej }
453 1.1 thorpej
454 1.1 thorpej static int
455 1.1 thorpej mpt_get_portfacts(mpt_softc_t *mpt, MSG_PORT_FACTS_REPLY *freplp)
456 1.1 thorpej {
457 1.1 thorpej MSG_PORT_FACTS f_req;
458 1.1 thorpej int error;
459 1.1 thorpej
460 1.1 thorpej /* XXX: Only getting PORT FACTS for Port 0 */
461 1.1 thorpej bzero(&f_req, sizeof f_req);
462 1.1 thorpej f_req.Function = MPI_FUNCTION_PORT_FACTS;
463 1.1 thorpej f_req.MsgContext = 0x12071943;
464 1.1 thorpej error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
465 1.1 thorpej if (error)
466 1.1 thorpej return(error);
467 1.1 thorpej error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
468 1.1 thorpej return (error);
469 1.1 thorpej }
470 1.1 thorpej
471 1.1 thorpej /*
472 1.1 thorpej * Send the initialization request. This is where we specify how many
473 1.1 thorpej * SCSI busses and how many devices per bus we wish to emulate.
474 1.1 thorpej * This is also the command that specifies the max size of the reply
475 1.1 thorpej * frames from the IOC that we will be allocating.
476 1.1 thorpej */
477 1.1 thorpej static int
478 1.1 thorpej mpt_send_ioc_init(mpt_softc_t *mpt, u_int32_t who)
479 1.1 thorpej {
480 1.1 thorpej int error = 0;
481 1.1 thorpej MSG_IOC_INIT init;
482 1.1 thorpej MSG_IOC_INIT_REPLY reply;
483 1.1 thorpej
484 1.1 thorpej bzero(&init, sizeof init);
485 1.1 thorpej init.WhoInit = who;
486 1.1 thorpej init.Function = MPI_FUNCTION_IOC_INIT;
487 1.1 thorpej if (mpt->is_fc) {
488 1.1 thorpej init.MaxDevices = 255;
489 1.1 thorpej } else {
490 1.1 thorpej init.MaxDevices = 16;
491 1.1 thorpej }
492 1.1 thorpej init.MaxBuses = 1;
493 1.1 thorpej init.ReplyFrameSize = MPT_REPLY_SIZE;
494 1.1 thorpej init.MsgContext = 0x12071941;
495 1.1 thorpej
496 1.1 thorpej if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
497 1.1 thorpej return(error);
498 1.1 thorpej }
499 1.1 thorpej
500 1.1 thorpej error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
501 1.1 thorpej return (error);
502 1.1 thorpej }
503 1.1 thorpej
504 1.1 thorpej
505 1.1 thorpej /*
506 1.1 thorpej * Utiltity routine to read configuration headers and pages
507 1.1 thorpej */
508 1.1 thorpej
509 1.1 thorpej static int
510 1.1 thorpej mpt_read_cfg_header(mpt_softc_t *, int, int, int, fCONFIG_PAGE_HEADER *);
511 1.1 thorpej
512 1.1 thorpej static int
513 1.1 thorpej mpt_read_cfg_header(mpt_softc_t *mpt, int PageType, int PageNumber,
514 1.1 thorpej int PageAddress, fCONFIG_PAGE_HEADER *rslt)
515 1.1 thorpej {
516 1.1 thorpej int count;
517 1.1 thorpej request_t *req;
518 1.1 thorpej MSG_CONFIG *cfgp;
519 1.1 thorpej MSG_CONFIG_REPLY *reply;
520 1.1 thorpej
521 1.1 thorpej req = mpt_get_request(mpt);
522 1.1 thorpej
523 1.1 thorpej cfgp = req->req_vbuf;
524 1.1 thorpej bzero(cfgp, sizeof *cfgp);
525 1.1 thorpej
526 1.1 thorpej cfgp->Action = MPI_CONFIG_ACTION_PAGE_HEADER;
527 1.1 thorpej cfgp->Function = MPI_FUNCTION_CONFIG;
528 1.1 thorpej cfgp->Header.PageNumber = (U8) PageNumber;
529 1.1 thorpej cfgp->Header.PageType = (U8) PageType;
530 1.1 thorpej cfgp->PageAddress = PageAddress;
531 1.1 thorpej MPI_pSGE_SET_FLAGS(((SGE_SIMPLE32 *) &cfgp->PageBufferSGE),
532 1.1 thorpej (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
533 1.1 thorpej MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
534 1.1 thorpej cfgp->MsgContext = req->index | 0x80000000;
535 1.1 thorpej
536 1.1 thorpej mpt_check_doorbell(mpt);
537 1.1 thorpej mpt_send_cmd(mpt, req);
538 1.1 thorpej count = 0;
539 1.1 thorpej do {
540 1.1 thorpej DELAY(500);
541 1.1 thorpej mpt_intr(mpt);
542 1.1 thorpej if (++count == 1000) {
543 1.1 thorpej mpt_prt(mpt, "read_cfg_header timed out");
544 1.1 thorpej return (-1);
545 1.1 thorpej }
546 1.1 thorpej } while (req->debug == REQ_ON_CHIP);
547 1.1 thorpej
548 1.1 thorpej reply = (MSG_CONFIG_REPLY *) MPT_REPLY_PTOV(mpt, req->sequence);
549 1.1 thorpej if ((reply->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
550 1.1 thorpej mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x",
551 1.1 thorpej reply->IOCStatus);
552 1.1 thorpej mpt_free_reply(mpt, (req->sequence << 1));
553 1.1 thorpej return (-1);
554 1.1 thorpej }
555 1.1 thorpej bcopy(&reply->Header, rslt, sizeof (fCONFIG_PAGE_HEADER));
556 1.1 thorpej mpt_free_reply(mpt, (req->sequence << 1));
557 1.1 thorpej mpt_free_request(mpt, req);
558 1.1 thorpej return (0);
559 1.1 thorpej }
560 1.1 thorpej
561 1.1 thorpej #define CFG_DATA_OFF 128
562 1.1 thorpej
563 1.1 thorpej int
564 1.1 thorpej mpt_read_cfg_page(mpt_softc_t *mpt, int PageAddress, fCONFIG_PAGE_HEADER *hdr)
565 1.1 thorpej {
566 1.1 thorpej int count;
567 1.1 thorpej request_t *req;
568 1.1 thorpej SGE_SIMPLE32 *se;
569 1.1 thorpej MSG_CONFIG *cfgp;
570 1.1 thorpej size_t amt;
571 1.1 thorpej MSG_CONFIG_REPLY *reply;
572 1.1 thorpej
573 1.1 thorpej req = mpt_get_request(mpt);
574 1.1 thorpej
575 1.1 thorpej cfgp = req->req_vbuf;
576 1.1 thorpej bzero(cfgp, MPT_REQUEST_AREA);
577 1.1 thorpej cfgp->Action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
578 1.1 thorpej cfgp->Function = MPI_FUNCTION_CONFIG;
579 1.1 thorpej cfgp->Header = *hdr;
580 1.1 thorpej amt = (cfgp->Header.PageLength * sizeof (u_int32_t));
581 1.1 thorpej cfgp->Header.PageType &= MPI_CONFIG_PAGETYPE_MASK;
582 1.1 thorpej cfgp->PageAddress = PageAddress;
583 1.1 thorpej se = (SGE_SIMPLE32 *) &cfgp->PageBufferSGE;
584 1.1 thorpej se->Address = req->req_pbuf + CFG_DATA_OFF;
585 1.1 thorpej MPI_pSGE_SET_LENGTH(se, amt);
586 1.1 thorpej MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
587 1.1 thorpej MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
588 1.1 thorpej MPI_SGE_FLAGS_END_OF_LIST));
589 1.1 thorpej
590 1.1 thorpej cfgp->MsgContext = req->index | 0x80000000;
591 1.1 thorpej
592 1.1 thorpej mpt_check_doorbell(mpt);
593 1.1 thorpej mpt_send_cmd(mpt, req);
594 1.1 thorpej count = 0;
595 1.1 thorpej do {
596 1.1 thorpej DELAY(500);
597 1.1 thorpej mpt_intr(mpt);
598 1.1 thorpej if (++count == 1000) {
599 1.1 thorpej mpt_prt(mpt, "read_cfg_page timed out");
600 1.1 thorpej return (-1);
601 1.1 thorpej }
602 1.1 thorpej } while (req->debug == REQ_ON_CHIP);
603 1.1 thorpej
604 1.1 thorpej reply = (MSG_CONFIG_REPLY *) MPT_REPLY_PTOV(mpt, req->sequence);
605 1.1 thorpej if ((reply->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
606 1.1 thorpej mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x",
607 1.1 thorpej reply->IOCStatus);
608 1.1 thorpej mpt_free_reply(mpt, (req->sequence << 1));
609 1.1 thorpej return (-1);
610 1.1 thorpej }
611 1.1 thorpej mpt_free_reply(mpt, (req->sequence << 1));
612 1.1 thorpej #if 0 /* XXXJRT */
613 1.1 thorpej bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
614 1.1 thorpej BUS_DMASYNC_POSTREAD);
615 1.1 thorpej #endif
616 1.1 thorpej if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_PORT &&
617 1.1 thorpej cfgp->Header.PageNumber == 0) {
618 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_PORT_0);
619 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_PORT &&
620 1.1 thorpej cfgp->Header.PageNumber == 1) {
621 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_PORT_1);
622 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_PORT &&
623 1.1 thorpej cfgp->Header.PageNumber == 2) {
624 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_PORT_2);
625 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_DEVICE &&
626 1.1 thorpej cfgp->Header.PageNumber == 0) {
627 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_DEVICE_0);
628 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_DEVICE &&
629 1.1 thorpej cfgp->Header.PageNumber == 1) {
630 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_DEVICE_1);
631 1.1 thorpej }
632 1.1 thorpej bcopy(((caddr_t)req->req_vbuf)+CFG_DATA_OFF, hdr, amt);
633 1.1 thorpej mpt_free_request(mpt, req);
634 1.1 thorpej return (0);
635 1.1 thorpej }
636 1.1 thorpej
637 1.1 thorpej int
638 1.1 thorpej mpt_write_cfg_page(mpt_softc_t *mpt, int PageAddress, fCONFIG_PAGE_HEADER *hdr)
639 1.1 thorpej {
640 1.1 thorpej int count, hdr_attr;
641 1.1 thorpej request_t *req;
642 1.1 thorpej SGE_SIMPLE32 *se;
643 1.1 thorpej MSG_CONFIG *cfgp;
644 1.1 thorpej size_t amt;
645 1.1 thorpej MSG_CONFIG_REPLY *reply;
646 1.1 thorpej
647 1.1 thorpej req = mpt_get_request(mpt);
648 1.1 thorpej
649 1.1 thorpej cfgp = req->req_vbuf;
650 1.1 thorpej bzero(cfgp, sizeof *cfgp);
651 1.1 thorpej
652 1.1 thorpej hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
653 1.1 thorpej if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
654 1.1 thorpej hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
655 1.1 thorpej mpt_prt(mpt, "page type 0x%x not changeable",
656 1.1 thorpej hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
657 1.1 thorpej return (-1);
658 1.1 thorpej }
659 1.1 thorpej hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
660 1.1 thorpej
661 1.1 thorpej cfgp->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
662 1.1 thorpej cfgp->Function = MPI_FUNCTION_CONFIG;
663 1.1 thorpej cfgp->Header = *hdr;
664 1.1 thorpej amt = (cfgp->Header.PageLength * sizeof (u_int32_t));
665 1.1 thorpej cfgp->PageAddress = PageAddress;
666 1.1 thorpej
667 1.1 thorpej se = (SGE_SIMPLE32 *) &cfgp->PageBufferSGE;
668 1.1 thorpej se->Address = req->req_pbuf + CFG_DATA_OFF;
669 1.1 thorpej MPI_pSGE_SET_LENGTH(se, amt);
670 1.1 thorpej MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
671 1.1 thorpej MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
672 1.1 thorpej MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_HOST_TO_IOC));
673 1.1 thorpej
674 1.1 thorpej cfgp->MsgContext = req->index | 0x80000000;
675 1.1 thorpej
676 1.1 thorpej if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_PORT &&
677 1.1 thorpej cfgp->Header.PageNumber == 0) {
678 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_PORT_0);
679 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_PORT &&
680 1.1 thorpej cfgp->Header.PageNumber == 1) {
681 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_PORT_1);
682 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_PORT &&
683 1.1 thorpej cfgp->Header.PageNumber == 2) {
684 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_PORT_2);
685 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_DEVICE &&
686 1.1 thorpej cfgp->Header.PageNumber == 0) {
687 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_DEVICE_0);
688 1.1 thorpej } else if (cfgp->Header.PageType == MPI_CONFIG_PAGETYPE_SCSI_DEVICE &&
689 1.1 thorpej cfgp->Header.PageNumber == 1) {
690 1.1 thorpej amt = sizeof (fCONFIG_PAGE_SCSI_DEVICE_1);
691 1.1 thorpej }
692 1.1 thorpej bcopy(hdr, ((caddr_t)req->req_vbuf)+CFG_DATA_OFF, amt);
693 1.1 thorpej /* Restore stripped out attributes */
694 1.1 thorpej hdr->PageType |= hdr_attr;
695 1.1 thorpej
696 1.1 thorpej mpt_check_doorbell(mpt);
697 1.1 thorpej mpt_send_cmd(mpt, req);
698 1.1 thorpej count = 0;
699 1.1 thorpej do {
700 1.1 thorpej DELAY(500);
701 1.1 thorpej mpt_intr(mpt);
702 1.1 thorpej if (++count == 1000) {
703 1.1 thorpej hdr->PageType |= hdr_attr;
704 1.1 thorpej mpt_prt(mpt, "mpt_write_cfg_page timed out");
705 1.1 thorpej return (-1);
706 1.1 thorpej }
707 1.1 thorpej } while (req->debug == REQ_ON_CHIP);
708 1.1 thorpej
709 1.1 thorpej reply = (MSG_CONFIG_REPLY *) MPT_REPLY_PTOV(mpt, req->sequence);
710 1.1 thorpej if ((reply->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
711 1.1 thorpej mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x",
712 1.1 thorpej reply->IOCStatus);
713 1.1 thorpej mpt_free_reply(mpt, (req->sequence << 1));
714 1.1 thorpej return (-1);
715 1.1 thorpej }
716 1.1 thorpej mpt_free_reply(mpt, (req->sequence << 1));
717 1.1 thorpej
718 1.1 thorpej mpt_free_request(mpt, req);
719 1.1 thorpej return (0);
720 1.1 thorpej }
721 1.1 thorpej
722 1.1 thorpej /*
723 1.1 thorpej * Read SCSI configuration information
724 1.1 thorpej */
725 1.1 thorpej static int
726 1.1 thorpej mpt_read_config_info_spi(mpt_softc_t *mpt)
727 1.1 thorpej {
728 1.1 thorpej int rv, i;
729 1.1 thorpej
730 1.1 thorpej rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 0,
731 1.1 thorpej 0, &mpt->mpt_port_page0.Header);
732 1.1 thorpej if (rv) {
733 1.1 thorpej return (-1);
734 1.1 thorpej }
735 1.1 thorpej if (mpt->verbose > 1) {
736 1.1 thorpej mpt_prt(mpt, "SPI Port Page 0 Header: %x %x %x %x",
737 1.1 thorpej mpt->mpt_port_page0.Header.PageVersion,
738 1.1 thorpej mpt->mpt_port_page0.Header.PageLength,
739 1.1 thorpej mpt->mpt_port_page0.Header.PageNumber,
740 1.1 thorpej mpt->mpt_port_page0.Header.PageType);
741 1.1 thorpej }
742 1.1 thorpej
743 1.1 thorpej rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 1,
744 1.1 thorpej 0, &mpt->mpt_port_page1.Header);
745 1.1 thorpej if (rv) {
746 1.1 thorpej return (-1);
747 1.1 thorpej }
748 1.1 thorpej if (mpt->verbose > 1) {
749 1.1 thorpej mpt_prt(mpt, "SPI Port Page 1 Header: %x %x %x %x",
750 1.1 thorpej mpt->mpt_port_page1.Header.PageVersion,
751 1.1 thorpej mpt->mpt_port_page1.Header.PageLength,
752 1.1 thorpej mpt->mpt_port_page1.Header.PageNumber,
753 1.1 thorpej mpt->mpt_port_page1.Header.PageType);
754 1.1 thorpej }
755 1.1 thorpej
756 1.1 thorpej rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 2,
757 1.1 thorpej 0, &mpt->mpt_port_page2.Header);
758 1.1 thorpej if (rv) {
759 1.1 thorpej return (-1);
760 1.1 thorpej }
761 1.1 thorpej
762 1.1 thorpej if (mpt->verbose > 1) {
763 1.1 thorpej mpt_prt(mpt, "SPI Port Page 2 Header: %x %x %x %x",
764 1.1 thorpej mpt->mpt_port_page1.Header.PageVersion,
765 1.1 thorpej mpt->mpt_port_page1.Header.PageLength,
766 1.1 thorpej mpt->mpt_port_page1.Header.PageNumber,
767 1.1 thorpej mpt->mpt_port_page1.Header.PageType);
768 1.1 thorpej }
769 1.1 thorpej
770 1.1 thorpej for (i = 0; i < 16; i++) {
771 1.1 thorpej rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_DEVICE,
772 1.1 thorpej 0, i, &mpt->mpt_dev_page0[i].Header);
773 1.1 thorpej if (rv) {
774 1.1 thorpej return (-1);
775 1.1 thorpej }
776 1.1 thorpej if (mpt->verbose > 1) {
777 1.1 thorpej mpt_prt(mpt,
778 1.1 thorpej "SPI Target %d Device Page 0 Header: %x %x %x %x",
779 1.1 thorpej i, mpt->mpt_dev_page0[i].Header.PageVersion,
780 1.1 thorpej mpt->mpt_dev_page0[i].Header.PageLength,
781 1.1 thorpej mpt->mpt_dev_page0[i].Header.PageNumber,
782 1.1 thorpej mpt->mpt_dev_page0[i].Header.PageType);
783 1.1 thorpej }
784 1.1 thorpej
785 1.1 thorpej rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_DEVICE,
786 1.1 thorpej 1, i, &mpt->mpt_dev_page1[i].Header);
787 1.1 thorpej if (rv) {
788 1.1 thorpej return (-1);
789 1.1 thorpej }
790 1.1 thorpej if (mpt->verbose > 1) {
791 1.1 thorpej mpt_prt(mpt,
792 1.1 thorpej "SPI Target %d Device Page 1 Header: %x %x %x %x",
793 1.1 thorpej i, mpt->mpt_dev_page1[i].Header.PageVersion,
794 1.1 thorpej mpt->mpt_dev_page1[i].Header.PageLength,
795 1.1 thorpej mpt->mpt_dev_page1[i].Header.PageNumber,
796 1.1 thorpej mpt->mpt_dev_page1[i].Header.PageType);
797 1.1 thorpej }
798 1.1 thorpej }
799 1.1 thorpej
800 1.1 thorpej /*
801 1.1 thorpej * At this point, we don't *have* to fail. As long as we have
802 1.1 thorpej * valid config header information, we can (barely) lurch
803 1.1 thorpej * along.
804 1.1 thorpej */
805 1.1 thorpej
806 1.1 thorpej rv = mpt_read_cfg_page(mpt, 0, &mpt->mpt_port_page0.Header);
807 1.1 thorpej if (rv) {
808 1.1 thorpej mpt_prt(mpt, "failed to read SPI Port Page 0");
809 1.1 thorpej } else if (mpt->verbose > 1) {
810 1.1 thorpej mpt_prt(mpt,
811 1.1 thorpej "SPI Port Page 0: Capabilities %x PhysicalInterface %x",
812 1.1 thorpej mpt->mpt_port_page0.Capabilities,
813 1.1 thorpej mpt->mpt_port_page0.PhysicalInterface);
814 1.1 thorpej }
815 1.1 thorpej
816 1.1 thorpej rv = mpt_read_cfg_page(mpt, 0, &mpt->mpt_port_page1.Header);
817 1.1 thorpej if (rv) {
818 1.1 thorpej mpt_prt(mpt, "failed to read SPI Port Page 1");
819 1.1 thorpej } else if (mpt->verbose > 1) {
820 1.1 thorpej mpt_prt(mpt,
821 1.1 thorpej "SPI Port Page 1: Configuration %x OnBusTimerValue %x",
822 1.1 thorpej mpt->mpt_port_page1.Configuration,
823 1.1 thorpej mpt->mpt_port_page1.OnBusTimerValue);
824 1.1 thorpej }
825 1.1 thorpej
826 1.1 thorpej rv = mpt_read_cfg_page(mpt, 0, &mpt->mpt_port_page2.Header);
827 1.1 thorpej if (rv) {
828 1.1 thorpej mpt_prt(mpt, "failed to read SPI Port Page 2");
829 1.1 thorpej } else if (mpt->verbose > 1) {
830 1.1 thorpej mpt_prt(mpt,
831 1.1 thorpej "SPI Port Page 2: Flags %x Settings %x",
832 1.1 thorpej mpt->mpt_port_page2.PortFlags,
833 1.1 thorpej mpt->mpt_port_page2.PortSettings);
834 1.1 thorpej for (i = 0; i < 16; i++) {
835 1.1 thorpej mpt_prt(mpt,
836 1.1 thorpej "SPI Port Page 2 Tgt %d: timo %x SF %x Flags %x",
837 1.1 thorpej i, mpt->mpt_port_page2.DeviceSettings[i].Timeout,
838 1.1 thorpej mpt->mpt_port_page2.DeviceSettings[i].SyncFactor,
839 1.1 thorpej mpt->mpt_port_page2.DeviceSettings[i].DeviceFlags);
840 1.1 thorpej }
841 1.1 thorpej }
842 1.1 thorpej
843 1.1 thorpej for (i = 0; i < 16; i++) {
844 1.1 thorpej rv = mpt_read_cfg_page(mpt, i, &mpt->mpt_dev_page0[i].Header);
845 1.1 thorpej if (rv) {
846 1.1 thorpej mpt_prt(mpt, "cannot read SPI Tgt %d Device Page 0", i);
847 1.1 thorpej continue;
848 1.1 thorpej }
849 1.1 thorpej if (mpt->verbose > 1) {
850 1.1 thorpej mpt_prt(mpt,
851 1.1 thorpej "SPI Tgt %d Page 0: NParms %x Information %x",
852 1.1 thorpej i, mpt->mpt_dev_page0[i].NegotiatedParameters,
853 1.1 thorpej mpt->mpt_dev_page0[i].Information);
854 1.1 thorpej }
855 1.1 thorpej rv = mpt_read_cfg_page(mpt, i, &mpt->mpt_dev_page1[i].Header);
856 1.1 thorpej if (rv) {
857 1.1 thorpej mpt_prt(mpt, "cannot read SPI Tgt %d Device Page 1", i);
858 1.1 thorpej continue;
859 1.1 thorpej }
860 1.1 thorpej if (mpt->verbose > 1) {
861 1.1 thorpej mpt_prt(mpt,
862 1.1 thorpej "SPI Tgt %d Page 1: RParms %x Configuration %x",
863 1.1 thorpej i, mpt->mpt_dev_page1[i].RequestedParameters,
864 1.1 thorpej mpt->mpt_dev_page1[i].Configuration);
865 1.1 thorpej }
866 1.1 thorpej }
867 1.1 thorpej return (0);
868 1.1 thorpej }
869 1.1 thorpej
870 1.1 thorpej /*
871 1.1 thorpej * Validate SPI configuration information.
872 1.1 thorpej *
873 1.1 thorpej * In particular, validate SPI Port Page 1.
874 1.1 thorpej */
875 1.1 thorpej static int
876 1.1 thorpej mpt_set_initial_config_spi(mpt_softc_t *mpt)
877 1.1 thorpej {
878 1.1 thorpej int i, pp1val = ((1 << mpt->mpt_ini_id) << 16) | mpt->mpt_ini_id;
879 1.1 thorpej
880 1.1 thorpej mpt->mpt_disc_enable = 0xff;
881 1.1 thorpej mpt->mpt_tag_enable = 0;
882 1.1 thorpej
883 1.1 thorpej if (mpt->mpt_port_page1.Configuration != pp1val) {
884 1.1 thorpej fCONFIG_PAGE_SCSI_PORT_1 tmp;
885 1.1 thorpej mpt_prt(mpt,
886 1.1 thorpej "SPI Port Page 1 Config value bad (%x)- should be %x",
887 1.1 thorpej mpt->mpt_port_page1.Configuration, pp1val);
888 1.1 thorpej tmp = mpt->mpt_port_page1;
889 1.1 thorpej tmp.Configuration = pp1val;
890 1.1 thorpej if (mpt_write_cfg_page(mpt, 0, &tmp.Header)) {
891 1.1 thorpej return (-1);
892 1.1 thorpej }
893 1.1 thorpej if (mpt_read_cfg_page(mpt, 0, &tmp.Header)) {
894 1.1 thorpej return (-1);
895 1.1 thorpej }
896 1.1 thorpej if (tmp.Configuration != pp1val) {
897 1.1 thorpej mpt_prt(mpt,
898 1.1 thorpej "failed to reset SPI Port Page 1 Config value");
899 1.1 thorpej return (-1);
900 1.1 thorpej }
901 1.1 thorpej mpt->mpt_port_page1 = tmp;
902 1.1 thorpej }
903 1.1 thorpej
904 1.1 thorpej for (i = 0; i < 16; i++) {
905 1.1 thorpej fCONFIG_PAGE_SCSI_DEVICE_1 tmp;
906 1.1 thorpej tmp = mpt->mpt_dev_page1[i];
907 1.1 thorpej tmp.RequestedParameters = 0;
908 1.1 thorpej tmp.Configuration = 0;
909 1.1 thorpej if (mpt->verbose > 1) {
910 1.1 thorpej mpt_prt(mpt,
911 1.1 thorpej "Set Tgt %d SPI DevicePage 1 values to %x 0 %x",
912 1.1 thorpej i, tmp.RequestedParameters, tmp.Configuration);
913 1.1 thorpej }
914 1.1 thorpej if (mpt_write_cfg_page(mpt, i, &tmp.Header)) {
915 1.1 thorpej return (-1);
916 1.1 thorpej }
917 1.1 thorpej if (mpt_read_cfg_page(mpt, i, &tmp.Header)) {
918 1.1 thorpej return (-1);
919 1.1 thorpej }
920 1.1 thorpej mpt->mpt_dev_page1[i] = tmp;
921 1.1 thorpej if (mpt->verbose > 1) {
922 1.1 thorpej mpt_prt(mpt,
923 1.1 thorpej "SPI Tgt %d Page 1: RParm %x Configuration %x", i,
924 1.1 thorpej mpt->mpt_dev_page1[i].RequestedParameters,
925 1.1 thorpej mpt->mpt_dev_page1[i].Configuration);
926 1.1 thorpej }
927 1.1 thorpej }
928 1.1 thorpej return (0);
929 1.1 thorpej }
930 1.1 thorpej
931 1.1 thorpej /*
932 1.1 thorpej * Enable IOC port
933 1.1 thorpej */
934 1.1 thorpej static int
935 1.1 thorpej mpt_send_port_enable(mpt_softc_t *mpt, int port)
936 1.1 thorpej {
937 1.1 thorpej int count;
938 1.1 thorpej request_t *req;
939 1.1 thorpej MSG_PORT_ENABLE *enable_req;
940 1.1 thorpej
941 1.1 thorpej req = mpt_get_request(mpt);
942 1.1 thorpej
943 1.1 thorpej enable_req = req->req_vbuf;
944 1.1 thorpej bzero(enable_req, sizeof *enable_req);
945 1.1 thorpej
946 1.1 thorpej enable_req->Function = MPI_FUNCTION_PORT_ENABLE;
947 1.1 thorpej enable_req->MsgContext = req->index | 0x80000000;
948 1.1 thorpej enable_req->PortNumber = port;
949 1.1 thorpej
950 1.1 thorpej mpt_check_doorbell(mpt);
951 1.1 thorpej if (mpt->verbose > 1) {
952 1.1 thorpej mpt_prt(mpt, "enabling port %d", port);
953 1.1 thorpej }
954 1.1 thorpej mpt_send_cmd(mpt, req);
955 1.1 thorpej
956 1.1 thorpej count = 0;
957 1.1 thorpej do {
958 1.1 thorpej DELAY(500);
959 1.1 thorpej mpt_intr(mpt);
960 1.1 thorpej if (++count == 100000) {
961 1.1 thorpej mpt_prt(mpt, "port enable timed out");
962 1.1 thorpej return (-1);
963 1.1 thorpej }
964 1.1 thorpej } while (req->debug == REQ_ON_CHIP);
965 1.1 thorpej mpt_free_request(mpt, req);
966 1.1 thorpej return (0);
967 1.1 thorpej }
968 1.1 thorpej
969 1.1 thorpej /*
970 1.1 thorpej * Enable/Disable asynchronous event reporting.
971 1.1 thorpej *
972 1.1 thorpej * NB: this is the first command we send via shared memory
973 1.1 thorpej * instead of the handshake register.
974 1.1 thorpej */
975 1.1 thorpej static int
976 1.1 thorpej mpt_send_event_request(mpt_softc_t *mpt, int onoff)
977 1.1 thorpej {
978 1.1 thorpej request_t *req;
979 1.1 thorpej MSG_EVENT_NOTIFY *enable_req;
980 1.1 thorpej
981 1.1 thorpej req = mpt_get_request(mpt);
982 1.1 thorpej
983 1.1 thorpej enable_req = req->req_vbuf;
984 1.1 thorpej bzero(enable_req, sizeof *enable_req);
985 1.1 thorpej
986 1.1 thorpej enable_req->Function = MPI_FUNCTION_EVENT_NOTIFICATION;
987 1.1 thorpej enable_req->MsgContext = req->index | 0x80000000;
988 1.1 thorpej enable_req->Switch = onoff;
989 1.1 thorpej
990 1.1 thorpej mpt_check_doorbell(mpt);
991 1.1 thorpej if (mpt->verbose > 1) {
992 1.1 thorpej mpt_prt(mpt, "%sabling async events", onoff? "en" : "dis");
993 1.1 thorpej }
994 1.1 thorpej mpt_send_cmd(mpt, req);
995 1.1 thorpej
996 1.1 thorpej return (0);
997 1.1 thorpej }
998 1.1 thorpej
999 1.1 thorpej /*
1000 1.1 thorpej * Un-mask the interupts on the chip.
1001 1.1 thorpej */
1002 1.1 thorpej void
1003 1.1 thorpej mpt_enable_ints(mpt_softc_t *mpt)
1004 1.1 thorpej {
1005 1.1 thorpej /* Unmask every thing except door bell int */
1006 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
1007 1.1 thorpej }
1008 1.1 thorpej
1009 1.1 thorpej /*
1010 1.1 thorpej * Mask the interupts on the chip.
1011 1.1 thorpej */
1012 1.1 thorpej void
1013 1.1 thorpej mpt_disable_ints(mpt_softc_t *mpt)
1014 1.1 thorpej {
1015 1.1 thorpej /* Mask all interrupts */
1016 1.1 thorpej mpt_write(mpt, MPT_OFFSET_INTR_MASK,
1017 1.1 thorpej MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
1018 1.1 thorpej }
1019 1.1 thorpej
1020 1.1 thorpej /* (Re)Initialize the chip for use */
1021 1.1 thorpej int
1022 1.1 thorpej mpt_init(mpt_softc_t *mpt, u_int32_t who)
1023 1.1 thorpej {
1024 1.1 thorpej int try;
1025 1.1 thorpej MSG_IOC_FACTS_REPLY facts;
1026 1.1 thorpej MSG_PORT_FACTS_REPLY pfp;
1027 1.1 thorpej u_int32_t pptr;
1028 1.1 thorpej int val;
1029 1.1 thorpej
1030 1.1 thorpej /* Put all request buffers (back) on the free list */
1031 1.1 thorpej SLIST_INIT(&mpt->request_free_list);
1032 1.1 thorpej for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
1033 1.1 thorpej mpt_free_request(mpt, &mpt->request_pool[val]);
1034 1.1 thorpej }
1035 1.1 thorpej
1036 1.1 thorpej if (mpt->verbose > 1) {
1037 1.1 thorpej mpt_prt(mpt, "doorbell req = %s",
1038 1.1 thorpej mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
1039 1.1 thorpej }
1040 1.1 thorpej
1041 1.1 thorpej /*
1042 1.1 thorpej * Start by making sure we're not at FAULT or RESET state
1043 1.1 thorpej */
1044 1.1 thorpej switch (mpt_rd_db(mpt) & MPT_DB_STATE_MASK) {
1045 1.1 thorpej case MPT_DB_STATE_RESET:
1046 1.1 thorpej case MPT_DB_STATE_FAULT:
1047 1.1 thorpej if (mpt_reset(mpt) != MPT_OK) {
1048 1.1 thorpej return (EIO);
1049 1.1 thorpej }
1050 1.1 thorpej default:
1051 1.1 thorpej break;
1052 1.1 thorpej }
1053 1.1 thorpej
1054 1.1 thorpej for (try = 0; try < MPT_MAX_TRYS; try++) {
1055 1.1 thorpej /*
1056 1.1 thorpej * No need to reset if the IOC is already in the READY state.
1057 1.1 thorpej *
1058 1.1 thorpej * Force reset if initialization failed previously.
1059 1.1 thorpej * Note that a hard_reset of the second channel of a '929
1060 1.1 thorpej * will stop operation of the first channel. Hopefully, if the
1061 1.1 thorpej * first channel is ok, the second will not require a hard
1062 1.1 thorpej * reset.
1063 1.1 thorpej */
1064 1.1 thorpej if ((mpt_rd_db(mpt) & MPT_DB_STATE_MASK) !=
1065 1.1 thorpej MPT_DB_STATE_READY) {
1066 1.1 thorpej if (mpt_reset(mpt) != MPT_OK) {
1067 1.1 thorpej DELAY(10000);
1068 1.1 thorpej continue;
1069 1.1 thorpej }
1070 1.1 thorpej }
1071 1.1 thorpej
1072 1.1 thorpej if (mpt_get_iocfacts(mpt, &facts) != MPT_OK) {
1073 1.1 thorpej mpt_prt(mpt, "mpt_get_iocfacts failed");
1074 1.1 thorpej continue;
1075 1.1 thorpej }
1076 1.1 thorpej
1077 1.1 thorpej if (mpt->verbose > 1) {
1078 1.1 thorpej mpt_prt(mpt,
1079 1.1 thorpej "IOCFACTS: GlobalCredits=%d BlockSize=%u "
1080 1.1 thorpej "Request Frame Size %u\n", facts.GlobalCredits,
1081 1.1 thorpej facts.BlockSize, facts.RequestFrameSize);
1082 1.1 thorpej }
1083 1.1 thorpej mpt->mpt_global_credits = facts.GlobalCredits;
1084 1.1 thorpej mpt->request_frame_size = facts.RequestFrameSize;
1085 1.1 thorpej
1086 1.1 thorpej if (mpt_get_portfacts(mpt, &pfp) != MPT_OK) {
1087 1.1 thorpej mpt_prt(mpt, "mpt_get_portfacts failed");
1088 1.1 thorpej continue;
1089 1.1 thorpej }
1090 1.1 thorpej
1091 1.1 thorpej if (mpt->verbose > 1) {
1092 1.1 thorpej mpt_prt(mpt,
1093 1.1 thorpej "PORTFACTS: Type %x PFlags %x IID %d MaxDev %d\n",
1094 1.1 thorpej pfp.PortType, pfp.ProtocolFlags, pfp.PortSCSIID,
1095 1.1 thorpej pfp.MaxDevices);
1096 1.1 thorpej }
1097 1.1 thorpej
1098 1.1 thorpej if (pfp.PortType != MPI_PORTFACTS_PORTTYPE_SCSI &&
1099 1.1 thorpej pfp.PortType != MPI_PORTFACTS_PORTTYPE_FC) {
1100 1.1 thorpej mpt_prt(mpt, "Unsupported Port Type (%x)",
1101 1.1 thorpej pfp.PortType);
1102 1.1 thorpej return (ENXIO);
1103 1.1 thorpej }
1104 1.1 thorpej if (!(pfp.ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR)) {
1105 1.1 thorpej mpt_prt(mpt, "initiator role unsupported");
1106 1.1 thorpej return (ENXIO);
1107 1.1 thorpej }
1108 1.1 thorpej if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_FC) {
1109 1.1 thorpej mpt->is_fc = 1;
1110 1.1 thorpej } else {
1111 1.1 thorpej mpt->is_fc = 0;
1112 1.1 thorpej }
1113 1.1 thorpej mpt->mpt_ini_id = pfp.PortSCSIID;
1114 1.1 thorpej
1115 1.1 thorpej if (mpt_send_ioc_init(mpt, who) != MPT_OK) {
1116 1.1 thorpej mpt_prt(mpt, "mpt_send_ioc_init failed");
1117 1.1 thorpej continue;
1118 1.1 thorpej }
1119 1.1 thorpej
1120 1.1 thorpej if (mpt->verbose > 1) {
1121 1.1 thorpej mpt_prt(mpt, "mpt_send_ioc_init ok");
1122 1.1 thorpej }
1123 1.1 thorpej
1124 1.1 thorpej if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
1125 1.1 thorpej mpt_prt(mpt, "IOC failed to go to run state");
1126 1.1 thorpej continue;
1127 1.1 thorpej }
1128 1.1 thorpej if (mpt->verbose > 1) {
1129 1.1 thorpej mpt_prt(mpt, "IOC now at RUNSTATE");
1130 1.1 thorpej }
1131 1.1 thorpej
1132 1.1 thorpej /*
1133 1.1 thorpej * Give it reply buffers
1134 1.1 thorpej *
1135 1.1 thorpej * Do *not* except global credits.
1136 1.1 thorpej */
1137 1.1 thorpej for (val = 0, pptr = mpt->reply_phys;
1138 1.1 thorpej (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
1139 1.1 thorpej pptr += MPT_REPLY_SIZE) {
1140 1.1 thorpej mpt_free_reply(mpt, pptr);
1141 1.1 thorpej if (++val == mpt->mpt_global_credits - 1)
1142 1.1 thorpej break;
1143 1.1 thorpej }
1144 1.1 thorpej
1145 1.1 thorpej /*
1146 1.1 thorpej * Enable asynchronous event reporting
1147 1.1 thorpej */
1148 1.1 thorpej mpt_send_event_request(mpt, 1);
1149 1.1 thorpej
1150 1.1 thorpej
1151 1.1 thorpej /*
1152 1.1 thorpej * Read set up initial configuration information
1153 1.1 thorpej * (SPI only for now)
1154 1.1 thorpej */
1155 1.1 thorpej
1156 1.1 thorpej if (mpt->is_fc == 0) {
1157 1.1 thorpej if (mpt_read_config_info_spi(mpt)) {
1158 1.1 thorpej return (EIO);
1159 1.1 thorpej }
1160 1.1 thorpej if (mpt_set_initial_config_spi(mpt)) {
1161 1.1 thorpej return (EIO);
1162 1.1 thorpej }
1163 1.1 thorpej }
1164 1.1 thorpej
1165 1.1 thorpej /*
1166 1.1 thorpej * Now enable the port
1167 1.1 thorpej */
1168 1.1 thorpej if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
1169 1.1 thorpej mpt_prt(mpt, "failed to enable port 0");
1170 1.1 thorpej continue;
1171 1.1 thorpej }
1172 1.1 thorpej
1173 1.1 thorpej if (mpt->verbose > 1) {
1174 1.1 thorpej mpt_prt(mpt, "enabled port 0");
1175 1.1 thorpej }
1176 1.1 thorpej
1177 1.1 thorpej /* Everything worked */
1178 1.1 thorpej break;
1179 1.1 thorpej }
1180 1.1 thorpej
1181 1.1 thorpej if (try >= MPT_MAX_TRYS) {
1182 1.1 thorpej mpt_prt(mpt, "failed to initialize IOC");
1183 1.1 thorpej return (EIO);
1184 1.1 thorpej }
1185 1.1 thorpej
1186 1.1 thorpej if (mpt->verbose > 1) {
1187 1.1 thorpej mpt_prt(mpt, "enabling interrupts");
1188 1.1 thorpej }
1189 1.1 thorpej
1190 1.1 thorpej mpt_enable_ints(mpt);
1191 1.1 thorpej return (0);
1192 1.1 thorpej }
1193