aic79xx.c revision 1.15 1 /* $NetBSD: aic79xx.c,v 1.15 2003/08/29 02:38:58 thorpej Exp $ */
2
3 /*
4 * Core routines and tables shareable across OS platforms.
5 *
6 * Copyright (c) 1994-2002 Justin T. Gibbs.
7 * Copyright (c) 2000-2003 Adaptec Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
21 * 3. Neither the names of the above-listed copyright holders nor the names
22 * of any contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * Alternatively, this software may be distributed under the terms of the
26 * GNU General Public License ("GPL") version 2 as published by the Free
27 * Software Foundation.
28 *
29 * NO WARRANTY
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGES.
41 *
42 * Id: //depot/aic7xxx/aic7xxx/aic79xx.c#196 $
43 *
44 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.c,v 1.18 2003/06/06 23:48:18 gibbs Exp $
45 */
46 /*
47 * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc.
48 * - April 2003
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: aic79xx.c,v 1.15 2003/08/29 02:38:58 thorpej Exp $");
53
54 #include <dev/ic/aic79xx_osm.h>
55 #include <dev/ic/aic79xx_inline.h>
56 #include <dev/ic/aic7xxx_cam.h>
57
58 #include <dev/microcode/aic7xxx/aicasm.h>
59 #include <dev/microcode/aic7xxx/aicasm_insformat.h>
60
61
62 /******************************** Globals *************************************/
63 struct ahd_softc_tailq ahd_tailq = TAILQ_HEAD_INITIALIZER(ahd_tailq);
64
65 /***************************** Lookup Tables **********************************/
66 char *ahd_chip_names[] =
67 {
68 "NONE",
69 "aic7901",
70 "aic7902",
71 "aic7901A"
72 };
73
74 /*
75 * Hardware error codes.
76 */
77 struct ahd_hard_error_entry {
78 uint8_t errno;
79 char *errmesg;
80 };
81
82 static struct ahd_hard_error_entry ahd_hard_errors[] = {
83 { DSCTMOUT, "Discard Timer has timed out" },
84 { ILLOPCODE, "Illegal Opcode in sequencer program" },
85 { SQPARERR, "Sequencer Parity Error" },
86 { DPARERR, "Data-path Parity Error" },
87 { MPARERR, "Scratch or SCB Memory Parity Error" },
88 { CIOPARERR, "CIOBUS Parity Error" },
89 };
90 static const u_int num_errors = NUM_ELEMENTS(ahd_hard_errors);
91
92 static struct ahd_phase_table_entry ahd_phase_table[] =
93 {
94 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
95 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
96 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
97 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
98 { P_COMMAND, MSG_NOOP, "in Command phase" },
99 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
100 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
101 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
102 { P_BUSFREE, MSG_NOOP, "while idle" },
103 { 0, MSG_NOOP, "in unknown phase" }
104 };
105
106 /*
107 * In most cases we only wish to itterate over real phases, so
108 * exclude the last element from the count.
109 */
110 static const u_int num_phases = NUM_ELEMENTS(ahd_phase_table) - 1;
111
112 /* Our Sequencer Program */
113 #include <dev/microcode/aic7xxx/aic79xx_seq.h>
114
115 /**************************** Function Declarations ***************************/
116 static void ahd_handle_transmission_error(struct ahd_softc *ahd);
117 static void ahd_handle_lqiphase_error(struct ahd_softc *ahd,
118 u_int lqistat1);
119 static int ahd_handle_pkt_busfree(struct ahd_softc *ahd,
120 u_int busfreetime);
121 static int ahd_handle_nonpkt_busfree(struct ahd_softc *ahd);
122 static void ahd_handle_proto_violation(struct ahd_softc *ahd);
123 static void ahd_force_renegotiation(struct ahd_softc *ahd,
124 struct ahd_devinfo *devinfo);
125
126 static struct ahd_tmode_tstate*
127 ahd_alloc_tstate(struct ahd_softc *ahd,
128 u_int scsi_id, char channel);
129 #ifdef AHD_TARGET_MODE
130 static void ahd_free_tstate(struct ahd_softc *ahd,
131 u_int scsi_id, char channel, int force);
132 #endif
133 static void ahd_devlimited_syncrate(struct ahd_softc *ahd,
134 struct ahd_initiator_tinfo *,
135 u_int *period,
136 u_int *ppr_options,
137 role_t role);
138 static void ahd_update_neg_table(struct ahd_softc *ahd,
139 struct ahd_devinfo *devinfo,
140 struct ahd_transinfo *tinfo);
141 static void ahd_update_pending_scbs(struct ahd_softc *ahd);
142 static void ahd_fetch_devinfo(struct ahd_softc *ahd,
143 struct ahd_devinfo *devinfo);
144 static void ahd_scb_devinfo(struct ahd_softc *ahd,
145 struct ahd_devinfo *devinfo,
146 struct scb *scb);
147 static void ahd_setup_initiator_msgout(struct ahd_softc *ahd,
148 struct ahd_devinfo *devinfo,
149 struct scb *scb);
150 static void ahd_build_transfer_msg(struct ahd_softc *ahd,
151 struct ahd_devinfo *devinfo);
152 static void ahd_construct_sdtr(struct ahd_softc *ahd,
153 struct ahd_devinfo *devinfo,
154 u_int period, u_int offset);
155 static void ahd_construct_wdtr(struct ahd_softc *ahd,
156 struct ahd_devinfo *devinfo,
157 u_int bus_width);
158 static void ahd_construct_ppr(struct ahd_softc *ahd,
159 struct ahd_devinfo *devinfo,
160 u_int period, u_int offset,
161 u_int bus_width, u_int ppr_options);
162 static void ahd_clear_msg_state(struct ahd_softc *ahd);
163 static void ahd_handle_message_phase(struct ahd_softc *ahd);
164 typedef enum {
165 AHDMSG_1B,
166 AHDMSG_2B,
167 AHDMSG_EXT
168 } ahd_msgtype;
169 static int ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type,
170 u_int msgval, int full);
171 static int ahd_parse_msg(struct ahd_softc *ahd,
172 struct ahd_devinfo *devinfo);
173 static int ahd_handle_msg_reject(struct ahd_softc *ahd,
174 struct ahd_devinfo *devinfo);
175 static void ahd_handle_ign_wide_residue(struct ahd_softc *ahd,
176 struct ahd_devinfo *devinfo);
177 static void ahd_reinitialize_dataptrs(struct ahd_softc *ahd);
178 static void ahd_handle_devreset(struct ahd_softc *ahd,
179 struct ahd_devinfo *devinfo,
180 u_int lun, cam_status status,
181 char *message, int verbose_level);
182 #if AHD_TARGET_MODE
183 static void ahd_setup_target_msgin(struct ahd_softc *ahd,
184 struct ahd_devinfo *devinfo,
185 struct scb *scb);
186 #endif
187
188 static u_int ahd_sglist_size(struct ahd_softc *ahd);
189 static u_int ahd_sglist_allocsize(struct ahd_softc *ahd);
190 static void ahd_initialize_hscbs(struct ahd_softc *ahd);
191 static int ahd_init_scbdata(struct ahd_softc *ahd);
192 static void ahd_fini_scbdata(struct ahd_softc *ahd);
193 static void ahd_setup_iocell_workaround(struct ahd_softc *ahd);
194 static void ahd_iocell_first_selection(struct ahd_softc *ahd);
195 static void ahd_add_col_list(struct ahd_softc *ahd,
196 struct scb *scb, u_int col_idx);
197 static void ahd_rem_col_list(struct ahd_softc *ahd,
198 struct scb *scb);
199 static void ahd_chip_init(struct ahd_softc *ahd);
200 static void ahd_qinfifo_requeue(struct ahd_softc *ahd,
201 struct scb *prev_scb,
202 struct scb *scb);
203 static int ahd_qinfifo_count(struct ahd_softc *ahd);
204 static int ahd_search_scb_list(struct ahd_softc *ahd, int target,
205 char channel, int lun, u_int tag,
206 role_t role, uint32_t status,
207 ahd_search_action action,
208 u_int *list_head, u_int tid);
209 static void ahd_stitch_tid_list(struct ahd_softc *ahd,
210 u_int tid_prev, u_int tid_cur,
211 u_int tid_next);
212 static void ahd_add_scb_to_free_list(struct ahd_softc *ahd,
213 u_int scbid);
214 static u_int ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
215 u_int prev, u_int next, u_int tid);
216 static void ahd_reset_current_bus(struct ahd_softc *ahd);
217 static ahd_callback_t ahd_reset_poll;
218 static ahd_callback_t ahd_stat_timer;
219 #ifdef AHD_DUMP_SEQ
220 static void ahd_dumpseq(struct ahd_softc *ahd);
221 #endif
222 static void ahd_loadseq(struct ahd_softc *ahd);
223 static int ahd_check_patch(struct ahd_softc *ahd,
224 struct patch **start_patch,
225 u_int start_instr, u_int *skip_addr);
226 static u_int ahd_resolve_seqaddr(struct ahd_softc *ahd,
227 u_int address);
228 static void ahd_download_instr(struct ahd_softc *ahd,
229 u_int instrptr, uint8_t *dconsts);
230 static int ahd_probe_stack_size(struct ahd_softc *ahd);
231 #ifdef AHD_TARGET_MODE
232 static void ahd_queue_lstate_event(struct ahd_softc *ahd,
233 struct ahd_tmode_lstate *lstate,
234 u_int initiator_id,
235 u_int event_type,
236 u_int event_arg);
237 static void ahd_update_scsiid(struct ahd_softc *ahd,
238 u_int targid_mask);
239 static int ahd_handle_target_cmd(struct ahd_softc *ahd,
240 struct target_cmd *cmd);
241 #endif
242
243 /************************** Added for porting to NetBSD ***********************/
244 static int ahd_createdmamem(bus_dma_tag_t tag,
245 int size,
246 int flags,
247 bus_dmamap_t *mapp,
248 caddr_t *vaddr,
249 bus_addr_t *baddr,
250 bus_dma_segment_t *seg,
251 int *nseg,
252 const char *myname, const char *what);
253
254 static void ahd_freedmamem(bus_dma_tag_t tag,
255 int size,
256 bus_dmamap_t map,
257 caddr_t vaddr,
258 bus_dma_segment_t *seg,
259 int nseg);
260 static void ahd_update_xfer_mode(struct ahd_softc *ahc,
261 struct ahd_devinfo *devinfo);
262
263 /******************************** Private Inlines *****************************/
264 static __inline void ahd_assert_atn(struct ahd_softc *ahd);
265 static __inline int ahd_currently_packetized(struct ahd_softc *ahd);
266 static __inline int ahd_set_active_fifo(struct ahd_softc *ahd);
267
268 static __inline void
269 ahd_assert_atn(struct ahd_softc *ahd)
270 {
271 ahd_outb(ahd, SCSISIGO, ATNO);
272 }
273
274 /*
275 * Determine if the current connection has a packetized
276 * agreement. This does not necessarily mean that we
277 * are currently in a packetized transfer. We could
278 * just as easily be sending or receiving a message.
279 */
280 static __inline int
281 ahd_currently_packetized(struct ahd_softc *ahd)
282 {
283 ahd_mode_state saved_modes;
284 int packetized;
285
286 saved_modes = ahd_save_modes(ahd);
287 if ((ahd->bugs & AHD_PKTIZED_STATUS_BUG) != 0) {
288 /*
289 * The packetized bit refers to the last
290 * connection, not the current one. Check
291 * for non-zero LQISTATE instead.
292 */
293 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
294 packetized = ahd_inb(ahd, LQISTATE) != 0;
295 } else {
296 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
297 packetized = ahd_inb(ahd, LQISTAT2) & PACKETIZED;
298 }
299 ahd_restore_modes(ahd, saved_modes);
300 return (packetized);
301 }
302
303 static __inline int
304 ahd_set_active_fifo(struct ahd_softc *ahd)
305 {
306 u_int active_fifo;
307
308 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
309 active_fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
310 switch (active_fifo) {
311 case 0:
312 case 1:
313 ahd_set_modes(ahd, active_fifo, active_fifo);
314 return (1);
315 default:
316 return (0);
317 }
318 }
319
320 /************************* Sequencer Execution Control ************************/
321 /*
322 * Restart the sequencer program from address zero
323 */
324 void
325 ahd_restart(struct ahd_softc *ahd)
326 {
327
328 ahd_pause(ahd);
329
330 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
331
332 /* No more pending messages */
333 ahd_clear_msg_state(ahd);
334 ahd_outb(ahd, SCSISIGO, 0); /* De-assert BSY */
335 ahd_outb(ahd, MSG_OUT, MSG_NOOP); /* No message to send */
336 ahd_outb(ahd, SXFRCTL1, ahd_inb(ahd, SXFRCTL1) & ~BITBUCKET);
337 ahd_outb(ahd, SEQINTCTL, 0);
338 ahd_outb(ahd, LASTPHASE, P_BUSFREE);
339 ahd_outb(ahd, SEQ_FLAGS, 0);
340 ahd_outb(ahd, SAVED_SCSIID, 0xFF);
341 ahd_outb(ahd, SAVED_LUN, 0xFF);
342
343 /*
344 * Ensure that the sequencer's idea of TQINPOS
345 * matches our own. The sequencer increments TQINPOS
346 * only after it sees a DMA complete and a reset could
347 * occur before the increment leaving the kernel to believe
348 * the command arrived but the sequencer to not.
349 */
350 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
351
352 /* Always allow reselection */
353 ahd_outb(ahd, SCSISEQ1,
354 ahd_inb(ahd, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
355 /* Ensure that no DMA operations are in progress */
356 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
357 ahd_outb(ahd, SCBHCNT, 0);
358 ahd_outb(ahd, CCSCBCTL, CCSCBRESET);
359 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
360 ahd_unpause(ahd);
361 }
362
363 void
364 ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo)
365 {
366 ahd_mode_state saved_modes;
367
368 #ifdef AHD_DEBUG
369 if ((ahd_debug & AHD_SHOW_FIFOS) != 0)
370 printf("%s: Clearing FIFO %d\n", ahd_name(ahd), fifo);
371 #endif
372 saved_modes = ahd_save_modes(ahd);
373 ahd_set_modes(ahd, fifo, fifo);
374 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
375 if ((ahd_inb(ahd, SG_STATE) & FETCH_INPROG) != 0)
376 ahd_outb(ahd, CCSGCTL, CCSGRESET);
377 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
378 ahd_outb(ahd, SG_STATE, 0);
379 ahd_restore_modes(ahd, saved_modes);
380 }
381
382 /************************* Input/Output Queues ********************************/
383 /*
384 * Flush and completed commands that are sitting in the command
385 * complete queues down on the chip but have yet to be DMA'ed back up.
386 */
387 void
388 ahd_flush_qoutfifo(struct ahd_softc *ahd)
389 {
390 struct scb *scb;
391 ahd_mode_state saved_modes;
392 u_int saved_scbptr;
393 u_int ccscbctl;
394 u_int scbid;
395 u_int next_scbid;
396
397 saved_modes = ahd_save_modes(ahd);
398 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
399 saved_scbptr = ahd_get_scbptr(ahd);
400
401 /*
402 * Wait for any inprogress DMA to complete and clear DMA state
403 * if this if for an SCB in the qinfifo.
404 */
405 while ((ccscbctl = ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0) {
406
407 if ((ccscbctl & (CCSCBDIR|CCARREN)) == (CCSCBDIR|CCARREN)) {
408 if ((ccscbctl & ARRDONE) != 0)
409 break;
410 } else if ((ccscbctl & CCSCBDONE) != 0)
411 break;
412 ahd_delay(200);
413 }
414 if ((ccscbctl & CCSCBDIR) != 0)
415 ahd_outb(ahd, CCSCBCTL, ccscbctl & ~(CCARREN|CCSCBEN));
416
417 /*
418 * Complete any SCBs that just finished being
419 * DMA'ed into the qoutfifo.
420 */
421 ahd_run_qoutfifo(ahd);
422
423 /*
424 * Manually update/complete any completed SCBs that are waiting to be
425 * DMA'ed back up to the host.
426 */
427 scbid = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
428 while (!SCBID_IS_NULL(scbid)) {
429 uint8_t *hscb_ptr;
430 u_int i;
431
432 ahd_set_scbptr(ahd, scbid);
433 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
434 scb = ahd_lookup_scb(ahd, scbid);
435 if (scb == NULL) {
436 printf("%s: Warning - DMA-up and complete "
437 "SCB %d invalid\n", ahd_name(ahd), scbid);
438 continue;
439 }
440 hscb_ptr = (uint8_t *)scb->hscb;
441 for (i = 0; i < sizeof(struct hardware_scb); i++)
442 *hscb_ptr++ = ahd_inb_scbram(ahd, SCB_BASE + i);
443
444 ahd_complete_scb(ahd, scb);
445 scbid = next_scbid;
446 }
447 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
448
449 scbid = ahd_inw(ahd, COMPLETE_SCB_HEAD);
450 while (!SCBID_IS_NULL(scbid)) {
451
452 ahd_set_scbptr(ahd, scbid);
453 next_scbid = ahd_inw_scbram(ahd, SCB_NEXT_COMPLETE);
454 scb = ahd_lookup_scb(ahd, scbid);
455 if (scb == NULL) {
456 printf("%s: Warning - Complete SCB %d invalid\n",
457 ahd_name(ahd), scbid);
458 continue;
459 }
460
461 ahd_complete_scb(ahd, scb);
462 scbid = next_scbid;
463 }
464 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
465 ahd_set_scbptr(ahd, saved_scbptr);
466
467 /*
468 * Flush the good status FIFO for compelted packetized commands.
469 */
470 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
471 while ((ahd_inb(ahd, LQISTAT2) & LQIGSAVAIL) != 0) {
472 scbid = (ahd_inb(ahd, GSFIFO+1) << 8)
473 | ahd_inb(ahd, GSFIFO);
474 scb = ahd_lookup_scb(ahd, scbid);
475 if (scb == NULL) {
476 printf("%s: Warning - GSFIFO SCB %d invalid\n",
477 ahd_name(ahd), scbid);
478 continue;
479 }
480 ahd_complete_scb(ahd, scb);
481 }
482
483 /*
484 * Restore state.
485 */
486 ahd_restore_modes(ahd, saved_modes);
487 ahd->flags |= AHD_UPDATE_PEND_CMDS;
488 }
489
490 void
491 ahd_run_qoutfifo(struct ahd_softc *ahd)
492 {
493 struct scb *scb;
494 u_int scb_index;
495
496 if ((ahd->flags & AHD_RUNNING_QOUTFIFO) != 0)
497 panic("ahd_run_qoutfifo recursion");
498 ahd->flags |= AHD_RUNNING_QOUTFIFO;
499 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_POSTREAD);
500 while ((ahd->qoutfifo[ahd->qoutfifonext]
501 & QOUTFIFO_ENTRY_VALID_LE) == ahd->qoutfifonext_valid_tag) {
502
503 scb_index = ahd_le16toh(ahd->qoutfifo[ahd->qoutfifonext]
504 & ~QOUTFIFO_ENTRY_VALID_LE);
505 scb = ahd_lookup_scb(ahd, scb_index);
506 if (scb == NULL) {
507 printf("%s: WARNING no command for scb %d "
508 "(cmdcmplt)\nQOUTPOS = %d\n",
509 ahd_name(ahd), scb_index,
510 ahd->qoutfifonext);
511 ahd_dump_card_state(ahd);
512 } else
513 ahd_complete_scb(ahd, scb);
514
515 ahd->qoutfifonext = (ahd->qoutfifonext+1) & (AHD_QOUT_SIZE-1);
516 if (ahd->qoutfifonext == 0)
517 ahd->qoutfifonext_valid_tag ^= QOUTFIFO_ENTRY_VALID_LE;
518 }
519 ahd->flags &= ~AHD_RUNNING_QOUTFIFO;
520 }
521
522 /************************* Interrupt Handling *********************************/
523 void
524 ahd_handle_hwerrint(struct ahd_softc *ahd)
525 {
526 /*
527 * Some catastrophic hardware error has occurred.
528 * Print it for the user and disable the controller.
529 */
530 int i;
531 int error;
532
533 error = ahd_inb(ahd, ERROR);
534 for (i = 0; i < num_errors; i++) {
535 if ((error & ahd_hard_errors[i].errno) != 0)
536 printf("%s: hwerrint, %s\n",
537 ahd_name(ahd), ahd_hard_errors[i].errmesg);
538 }
539
540 ahd_dump_card_state(ahd);
541 panic("BRKADRINT");
542
543 /* Tell everyone that this HBA is no longer available */
544 ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
545 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
546 CAM_NO_HBA);
547
548 /* Tell the system that this controller has gone away. */
549 ahd_free(ahd);
550 }
551
552 void
553 ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat)
554 {
555 u_int seqintcode;
556
557 /*
558 * Save the sequencer interrupt code and clear the SEQINT
559 * bit. We will unpause the sequencer, if appropriate,
560 * after servicing the request.
561 */
562 seqintcode = ahd_inb(ahd, SEQINTCODE);
563 ahd_outb(ahd, CLRINT, CLRSEQINT);
564 if ((ahd->bugs & AHD_INTCOLLISION_BUG) != 0) {
565 /*
566 * Unpause the sequencer and let it clear
567 * SEQINT by writing NO_SEQINT to it. This
568 * will cause the sequencer to be paused again,
569 * which is the expected state of this routine.
570 */
571 ahd_unpause(ahd);
572 while (!ahd_is_paused(ahd))
573 ;
574 ahd_outb(ahd, CLRINT, CLRSEQINT);
575 }
576 ahd_update_modes(ahd);
577 #ifdef AHD_DEBUG
578 if ((ahd_debug & AHD_SHOW_MISC) != 0)
579 printf("%s: Handle Seqint Called for code %d\n",
580 ahd_name(ahd), seqintcode);
581 #endif
582 switch (seqintcode) {
583 case BAD_SCB_STATUS:
584 {
585 struct scb *scb;
586 u_int scbid;
587 int cmds_pending;
588
589 scbid = ahd_get_scbptr(ahd);
590 scb = ahd_lookup_scb(ahd, scbid);
591 if (scb != NULL) {
592 ahd_complete_scb(ahd, scb);
593 } else {
594 printf("%s: WARNING no command for scb %d "
595 "(bad status)\n", ahd_name(ahd), scbid);
596 ahd_dump_card_state(ahd);
597 }
598 cmds_pending = ahd_inw(ahd, CMDS_PENDING);
599 if (cmds_pending > 0)
600 ahd_outw(ahd, CMDS_PENDING, cmds_pending - 1);
601 break;
602 }
603 case ENTERING_NONPACK:
604 {
605 struct scb *scb;
606 u_int scbid;
607
608 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
609 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
610 scbid = ahd_get_scbptr(ahd);
611 scb = ahd_lookup_scb(ahd, scbid);
612 if (scb == NULL) {
613 /*
614 * Somehow need to know if this
615 * is from a selection or reselection.
616 * From that, we can termine target
617 * ID so we at least have an I_T nexus.
618 */
619 } else {
620 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
621 ahd_outb(ahd, SAVED_LUN, scb->hscb->lun);
622 ahd_outb(ahd, SEQ_FLAGS, 0x0);
623 }
624 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0
625 && (ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
626 /*
627 * Phase change after read stream with
628 * CRC error with P0 asserted on last
629 * packet.
630 */
631 #ifdef AHD_DEBUG
632 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
633 printf("%s: Assuming LQIPHASE_NLQ with "
634 "P0 assertion\n", ahd_name(ahd));
635 #endif
636 }
637 #ifdef AHD_DEBUG
638 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
639 printf("%s: Entering NONPACK\n", ahd_name(ahd));
640 #endif
641 break;
642 }
643 case INVALID_SEQINT:
644 printf("%s: Invalid Sequencer interrupt occurred.\n",
645 ahd_name(ahd));
646 ahd_dump_card_state(ahd);
647 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
648 break;
649 case STATUS_OVERRUN:
650 {
651 struct scb *scb;
652 u_int scbid;
653
654 scbid = ahd_get_scbptr(ahd);
655 scb = ahd_lookup_scb(ahd, scbid);
656 if (scb != NULL)
657 ahd_print_path(ahd, scb);
658 else
659 printf("%s: ", ahd_name(ahd));
660 printf("SCB %d Packetized Status Overrun", scbid);
661 ahd_dump_card_state(ahd);
662 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
663 break;
664 }
665 case CFG4ISTAT_INTR:
666 {
667 struct scb *scb;
668 u_int scbid;
669
670 scbid = ahd_get_scbptr(ahd);
671 scb = ahd_lookup_scb(ahd, scbid);
672 if (scb == NULL) {
673 ahd_dump_card_state(ahd);
674 printf("CFG4ISTAT: Free SCB %d referenced", scbid);
675 panic("For safety");
676 }
677 ahd_outq(ahd, HADDR, scb->sense_busaddr);
678 ahd_outw(ahd, HCNT, AHD_SENSE_BUFSIZE);
679 ahd_outb(ahd, HCNT + 2, 0);
680 ahd_outb(ahd, SG_CACHE_PRE, SG_LAST_SEG);
681 ahd_outb(ahd, DFCNTRL, PRELOADEN|SCSIEN|HDMAEN);
682 break;
683 }
684 case ILLEGAL_PHASE:
685 {
686 u_int bus_phase;
687
688 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
689 printf("%s: ILLEGAL_PHASE 0x%x\n",
690 ahd_name(ahd), bus_phase);
691
692 switch (bus_phase) {
693 case P_DATAOUT:
694 case P_DATAIN:
695 case P_DATAOUT_DT:
696 case P_DATAIN_DT:
697 case P_MESGOUT:
698 case P_STATUS:
699 case P_MESGIN:
700 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
701 printf("%s: Issued Bus Reset.\n", ahd_name(ahd));
702 break;
703 case P_COMMAND:
704 {
705 struct ahd_devinfo devinfo;
706 struct scb *scb;
707 struct ahd_initiator_tinfo *targ_info;
708 struct ahd_tmode_tstate *tstate;
709 struct ahd_transinfo *tinfo;
710 u_int scbid;
711
712 /*
713 * If a target takes us into the command phase
714 * assume that it has been externally reset and
715 * has thus lost our previous packetized negotiation
716 * agreement. Since we have not sent an identify
717 * message and may not have fully qualified the
718 * connection, we change our command to TUR, assert
719 * ATN and ABORT the task when we go to message in
720 * phase. The OSM will see the REQUEUE_REQUEST
721 * status and retry the command.
722 */
723 scbid = ahd_get_scbptr(ahd);
724 scb = ahd_lookup_scb(ahd, scbid);
725 if (scb == NULL) {
726 printf("Invalid phase with no valid SCB. "
727 "Resetting bus.\n");
728 ahd_reset_channel(ahd, 'A',
729 /*Initiate Reset*/TRUE);
730 break;
731 }
732 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
733 SCB_GET_TARGET(ahd, scb),
734 SCB_GET_LUN(scb),
735 SCB_GET_CHANNEL(ahd, scb),
736 ROLE_INITIATOR);
737 targ_info = ahd_fetch_transinfo(ahd,
738 devinfo.channel,
739 devinfo.our_scsiid,
740 devinfo.target,
741 &tstate);
742 tinfo = &targ_info->curr;
743 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
744 AHD_TRANS_ACTIVE, /*paused*/TRUE);
745 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
746 /*offset*/0, /*ppr_options*/0,
747 AHD_TRANS_ACTIVE, /*paused*/TRUE);
748 ahd_outb(ahd, SCB_CDB_STORE, 0);
749 ahd_outb(ahd, SCB_CDB_STORE+1, 0);
750 ahd_outb(ahd, SCB_CDB_STORE+2, 0);
751 ahd_outb(ahd, SCB_CDB_STORE+3, 0);
752 ahd_outb(ahd, SCB_CDB_STORE+4, 0);
753 ahd_outb(ahd, SCB_CDB_STORE+5, 0);
754 ahd_outb(ahd, SCB_CDB_LEN, 6);
755 scb->hscb->control &= ~(TAG_ENB|SCB_TAG_TYPE);
756 scb->hscb->control |= MK_MESSAGE;
757 ahd_outb(ahd, SCB_CONTROL, scb->hscb->control);
758 ahd_outb(ahd, MSG_OUT, HOST_MSG);
759 ahd_outb(ahd, SAVED_SCSIID, scb->hscb->scsiid);
760 /*
761 * The lun is 0, regardless of the SCB's lun
762 * as we have not sent an identify message.
763 */
764 ahd_outb(ahd, SAVED_LUN, 0);
765 ahd_outb(ahd, SEQ_FLAGS, 0);
766 ahd_assert_atn(ahd);
767 scb->flags &= ~(SCB_PACKETIZED);
768 scb->flags |= SCB_ABORT|SCB_CMDPHASE_ABORT;
769 ahd_freeze_devq(ahd, scb);
770 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
771 ahd_freeze_scb(scb);
772
773 /*
774 * Allow the sequencer to continue with
775 * non-pack processing.
776 */
777 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
778 ahd_outb(ahd, CLRLQOINT1, CLRLQOPHACHGINPKT);
779 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
780 ahd_outb(ahd, CLRLQOINT1, 0);
781 }
782 #ifdef AHD_DEBUG
783 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
784 ahd_print_path(ahd, scb);
785 printf("Unexpected command phase from "
786 "packetized target\n");
787 }
788 #endif
789 break;
790 }
791 }
792 break;
793 }
794 case CFG4OVERRUN:
795 {
796 struct scb *scb;
797 u_int scb_index;
798
799 #ifdef AHD_DEBUG
800 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
801 printf("%s: CFG4OVERRUN mode = %x\n", ahd_name(ahd),
802 ahd_inb(ahd, MODE_PTR));
803 }
804 #endif
805 scb_index = ahd_get_scbptr(ahd);
806 scb = ahd_lookup_scb(ahd, scb_index);
807 if (scb == NULL) {
808 /*
809 * Attempt to transfer to an SCB that is
810 * not outstanding.
811 */
812 ahd_assert_atn(ahd);
813 ahd_outb(ahd, MSG_OUT, HOST_MSG);
814 ahd->msgout_buf[0] = MSG_ABORT_TASK;
815 ahd->msgout_len = 1;
816 ahd->msgout_index = 0;
817 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
818 /*
819 * Clear status received flag to prevent any
820 * attempt to complete this bogus SCB.
821 */
822 ahd_outb(ahd, SCB_CONTROL,
823 ahd_inb(ahd, SCB_CONTROL) & ~STATUS_RCVD);
824 }
825 break;
826 }
827 case DUMP_CARD_STATE:
828 {
829 ahd_dump_card_state(ahd);
830 break;
831 }
832 case PDATA_REINIT:
833 {
834 #ifdef AHD_DEBUG
835 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
836 printf("%s: PDATA_REINIT - DFCNTRL = 0x%x "
837 "SG_CACHE_SHADOW = 0x%x\n",
838 ahd_name(ahd), ahd_inb(ahd, DFCNTRL),
839 ahd_inb(ahd, SG_CACHE_SHADOW));
840 }
841 #endif
842 ahd_reinitialize_dataptrs(ahd);
843 break;
844 }
845 case HOST_MSG_LOOP:
846 {
847 struct ahd_devinfo devinfo;
848
849 /*
850 * The sequencer has encountered a message phase
851 * that requires host assistance for completion.
852 * While handling the message phase(s), we will be
853 * notified by the sequencer after each byte is
854 * transfered so we can track bus phase changes.
855 *
856 * If this is the first time we've seen a HOST_MSG_LOOP
857 * interrupt, initialize the state of the host message
858 * loop.
859 */
860 ahd_fetch_devinfo(ahd, &devinfo);
861 if (ahd->msg_type == MSG_TYPE_NONE) {
862 struct scb *scb;
863 u_int scb_index;
864 u_int bus_phase;
865
866 bus_phase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
867 if (bus_phase != P_MESGIN
868 && bus_phase != P_MESGOUT) {
869 printf("ahd_intr: HOST_MSG_LOOP bad "
870 "phase 0x%x\n", bus_phase);
871 /*
872 * Probably transitioned to bus free before
873 * we got here. Just punt the message.
874 */
875 ahd_dump_card_state(ahd);
876 ahd_clear_intstat(ahd);
877 ahd_restart(ahd);
878 return;
879 }
880
881 scb_index = ahd_get_scbptr(ahd);
882 scb = ahd_lookup_scb(ahd, scb_index);
883 if (devinfo.role == ROLE_INITIATOR) {
884 if (bus_phase == P_MESGOUT)
885 ahd_setup_initiator_msgout(ahd,
886 &devinfo,
887 scb);
888 else {
889 ahd->msg_type =
890 MSG_TYPE_INITIATOR_MSGIN;
891 ahd->msgin_index = 0;
892 }
893 }
894 #if AHD_TARGET_MODE
895 else {
896 if (bus_phase == P_MESGOUT) {
897 ahd->msg_type =
898 MSG_TYPE_TARGET_MSGOUT;
899 ahd->msgin_index = 0;
900 }
901 else
902 ahd_setup_target_msgin(ahd,
903 &devinfo,
904 scb);
905 }
906 #endif
907 }
908
909 ahd_handle_message_phase(ahd);
910 break;
911 }
912 case NO_MATCH:
913 {
914 /* Ensure we don't leave the selection hardware on */
915 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
916 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
917
918 printf("%s:%c:%d: no active SCB for reconnecting "
919 "target - issuing BUS DEVICE RESET\n",
920 ahd_name(ahd), 'A', ahd_inb(ahd, SELID) >> 4);
921 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
922 "REG0 == 0x%x ACCUM = 0x%x\n",
923 ahd_inb(ahd, SAVED_SCSIID), ahd_inb(ahd, SAVED_LUN),
924 ahd_inw(ahd, REG0), ahd_inb(ahd, ACCUM));
925 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
926 "SINDEX == 0x%x\n",
927 ahd_inb(ahd, SEQ_FLAGS), ahd_get_scbptr(ahd),
928 ahd_find_busy_tcl(ahd,
929 BUILD_TCL(ahd_inb(ahd, SAVED_SCSIID),
930 ahd_inb(ahd, SAVED_LUN))),
931 ahd_inw(ahd, SINDEX));
932 printf("SELID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
933 "SCB_CONTROL == 0x%x\n",
934 ahd_inb(ahd, SELID), ahd_inb_scbram(ahd, SCB_SCSIID),
935 ahd_inb_scbram(ahd, SCB_LUN),
936 ahd_inb_scbram(ahd, SCB_CONTROL));
937 printf("SCSIBUS[0] == 0x%x, SCSISIGI == 0x%x\n",
938 ahd_inb(ahd, SCSIBUS), ahd_inb(ahd, SCSISIGI));
939 printf("SXFRCTL0 == 0x%x\n", ahd_inb(ahd, SXFRCTL0));
940 printf("SEQCTL0 == 0x%x\n", ahd_inb(ahd, SEQCTL0));
941 ahd_dump_card_state(ahd);
942 ahd->msgout_buf[0] = MSG_BUS_DEV_RESET;
943 ahd->msgout_len = 1;
944 ahd->msgout_index = 0;
945 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
946 ahd_outb(ahd, MSG_OUT, HOST_MSG);
947 ahd_assert_atn(ahd);
948 break;
949 }
950 case PROTO_VIOLATION:
951 {
952 ahd_handle_proto_violation(ahd);
953 break;
954 }
955 case IGN_WIDE_RES:
956 {
957 struct ahd_devinfo devinfo;
958
959 ahd_fetch_devinfo(ahd, &devinfo);
960 ahd_handle_ign_wide_residue(ahd, &devinfo);
961 break;
962 }
963 case BAD_PHASE:
964 {
965 u_int lastphase;
966
967 lastphase = ahd_inb(ahd, LASTPHASE);
968 printf("%s:%c:%d: unknown scsi bus phase %x, "
969 "lastphase = 0x%x. Attempting to continue\n",
970 ahd_name(ahd), 'A',
971 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
972 lastphase, ahd_inb(ahd, SCSISIGI));
973 break;
974 }
975 case MISSED_BUSFREE:
976 {
977 u_int lastphase;
978
979 lastphase = ahd_inb(ahd, LASTPHASE);
980 printf("%s:%c:%d: Missed busfree. "
981 "Lastphase = 0x%x, Curphase = 0x%x\n",
982 ahd_name(ahd), 'A',
983 SCSIID_TARGET(ahd, ahd_inb(ahd, SAVED_SCSIID)),
984 lastphase, ahd_inb(ahd, SCSISIGI));
985 ahd_restart(ahd);
986 return;
987 }
988 case DATA_OVERRUN:
989 {
990 /*
991 * When the sequencer detects an overrun, it
992 * places the controller in "BITBUCKET" mode
993 * and allows the target to complete its transfer.
994 * Unfortunately, none of the counters get updated
995 * when the controller is in this mode, so we have
996 * no way of knowing how large the overrun was.
997 */
998 struct scb *scb;
999 u_int scbindex;
1000 #ifdef AHD_DEBUG
1001 u_int lastphase;
1002 #endif
1003
1004 scbindex = ahd_get_scbptr(ahd);
1005 scb = ahd_lookup_scb(ahd, scbindex);
1006 #ifdef AHD_DEBUG
1007 lastphase = ahd_inb(ahd, LASTPHASE);
1008 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1009 ahd_print_path(ahd, scb);
1010 printf("data overrun detected %s. Tag == 0x%x.\n",
1011 ahd_lookup_phase_entry(lastphase)->phasemsg,
1012 SCB_GET_TAG(scb));
1013 ahd_print_path(ahd, scb);
1014 printf("%s seen Data Phase. Length = %ld. "
1015 "NumSGs = %d.\n",
1016 ahd_inb(ahd, SEQ_FLAGS) & DPHASE
1017 ? "Have" : "Haven't",
1018 ahd_get_transfer_length(scb), scb->sg_count);
1019 ahd_dump_sglist(scb);
1020 }
1021 #endif
1022
1023 /*
1024 * Set this and it will take effect when the
1025 * target does a command complete.
1026 */
1027 ahd_freeze_devq(ahd, scb);
1028 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1029 ahd_freeze_scb(scb);
1030 break;
1031 }
1032 case MKMSG_FAILED:
1033 {
1034 struct ahd_devinfo devinfo;
1035 struct scb *scb;
1036 u_int scbid;
1037
1038 ahd_fetch_devinfo(ahd, &devinfo);
1039 printf("%s:%c:%d:%d: Attempt to issue message failed\n",
1040 ahd_name(ahd), devinfo.channel, devinfo.target,
1041 devinfo.lun);
1042 scbid = ahd_get_scbptr(ahd);
1043 scb = ahd_lookup_scb(ahd, scbid);
1044 if (scb != NULL
1045 && (scb->flags & SCB_RECOVERY_SCB) != 0)
1046 /*
1047 * Ensure that we didn't put a second instance of this
1048 * SCB into the QINFIFO.
1049 */
1050 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
1051 SCB_GET_CHANNEL(ahd, scb),
1052 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1053 ROLE_INITIATOR, /*status*/0,
1054 SEARCH_REMOVE);
1055 ahd_outb(ahd, SCB_CONTROL,
1056 ahd_inb(ahd, SCB_CONTROL) & ~MK_MESSAGE);
1057 break;
1058 }
1059 case TASKMGMT_FUNC_COMPLETE:
1060 {
1061 u_int scbid;
1062 struct scb *scb;
1063
1064 scbid = ahd_get_scbptr(ahd);
1065 scb = ahd_lookup_scb(ahd, scbid);
1066 if (scb != NULL) {
1067 u_int lun;
1068 u_int tag;
1069 cam_status error;
1070
1071 ahd_print_path(ahd, scb);
1072 printf("Task Management Func 0x%x Complete\n",
1073 scb->hscb->task_management);
1074 lun = CAM_LUN_WILDCARD;
1075 tag = SCB_LIST_NULL;
1076
1077 switch (scb->hscb->task_management) {
1078 case SIU_TASKMGMT_ABORT_TASK:
1079 tag = SCB_GET_TAG(scb);
1080 case SIU_TASKMGMT_ABORT_TASK_SET:
1081 case SIU_TASKMGMT_CLEAR_TASK_SET:
1082 lun = scb->hscb->lun;
1083 error = CAM_REQ_ABORTED;
1084 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1085 'A', lun, tag, ROLE_INITIATOR,
1086 error);
1087 break;
1088 case SIU_TASKMGMT_LUN_RESET:
1089 lun = scb->hscb->lun;
1090 case SIU_TASKMGMT_TARGET_RESET:
1091 {
1092 struct ahd_devinfo devinfo;
1093
1094 ahd_scb_devinfo(ahd, &devinfo, scb);
1095 error = CAM_BDR_SENT;
1096 ahd_handle_devreset(ahd, &devinfo, lun,
1097 CAM_BDR_SENT,
1098 lun != CAM_LUN_WILDCARD
1099 ? "Lun Reset"
1100 : "Target Reset",
1101 /*verbose_level*/0);
1102 break;
1103 }
1104 default:
1105 panic("Unexpected TaskMgmt Func\n");
1106 break;
1107 }
1108 }
1109 break;
1110 }
1111 case TASKMGMT_CMD_CMPLT_OKAY:
1112 {
1113 u_int scbid;
1114 struct scb *scb;
1115
1116 /*
1117 * An ABORT TASK TMF failed to be delivered before
1118 * the targeted command completed normally.
1119 */
1120 scbid = ahd_get_scbptr(ahd);
1121 scb = ahd_lookup_scb(ahd, scbid);
1122 if (scb != NULL) {
1123 /*
1124 * Remove the second instance of this SCB from
1125 * the QINFIFO if it is still there.
1126 */
1127 ahd_print_path(ahd, scb);
1128 printf("SCB completes before TMF\n");
1129 /*
1130 * Handle losing the race. Wait until any
1131 * current selection completes. We will then
1132 * set the TMF back to zero in this SCB so that
1133 * the sequencer doesn't bother to issue another
1134 * sequencer interrupt for its completion.
1135 */
1136 while ((ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
1137 && (ahd_inb(ahd, SSTAT0) & SELDO) == 0
1138 && (ahd_inb(ahd, SSTAT1) & SELTO) == 0)
1139 ;
1140 ahd_outb(ahd, SCB_TASK_MANAGEMENT, 0);
1141 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
1142 SCB_GET_CHANNEL(ahd, scb),
1143 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1144 ROLE_INITIATOR, /*status*/0,
1145 SEARCH_REMOVE);
1146 }
1147 break;
1148 }
1149 case TRACEPOINT0:
1150 case TRACEPOINT1:
1151 case TRACEPOINT2:
1152 case TRACEPOINT3:
1153 printf("%s: Tracepoint %d\n", ahd_name(ahd),
1154 seqintcode - TRACEPOINT0);
1155 break;
1156 case NO_SEQINT:
1157 break;
1158 case SAW_HWERR:
1159 ahd_handle_hwerrint(ahd);
1160 break;
1161 default:
1162 printf("%s: Unexpected SEQINTCODE %d\n", ahd_name(ahd),
1163 seqintcode);
1164 break;
1165 }
1166 /*
1167 * The sequencer is paused immediately on
1168 * a SEQINT, so we should restart it when
1169 * we're done.
1170 */
1171 ahd_unpause(ahd);
1172 }
1173
1174 void
1175 ahd_handle_scsiint(struct ahd_softc *ahd, u_int intstat)
1176 {
1177 struct scb *scb;
1178 u_int status0;
1179 u_int status3;
1180 u_int status;
1181 u_int lqistat1;
1182 u_int lqostat0;
1183 u_int scbid;
1184 u_int busfreetime;
1185
1186 ahd_update_modes(ahd);
1187 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1188
1189 status3 = ahd_inb(ahd, SSTAT3) & (NTRAMPERR|OSRAMPERR);
1190 status0 = ahd_inb(ahd, SSTAT0) & (IOERR|OVERRUN|SELDI|SELDO);
1191 status = ahd_inb(ahd, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
1192 lqistat1 = ahd_inb(ahd, LQISTAT1);
1193 lqostat0 = ahd_inb(ahd, LQOSTAT0);
1194 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
1195 if ((status0 & (SELDI|SELDO)) != 0) {
1196 u_int simode0;
1197
1198 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1199 simode0 = ahd_inb(ahd, SIMODE0);
1200 status0 &= simode0 & (IOERR|OVERRUN|SELDI|SELDO);
1201 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1202 }
1203 scbid = ahd_get_scbptr(ahd);
1204 scb = ahd_lookup_scb(ahd, scbid);
1205 if (scb != NULL
1206 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1207 scb = NULL;
1208
1209 /* Make sure the sequencer is in a safe location. */
1210 ahd_clear_critical_section(ahd);
1211
1212 if ((status0 & IOERR) != 0) {
1213 u_int now_lvd;
1214
1215 now_lvd = ahd_inb(ahd, SBLKCTL) & ENAB40;
1216 printf("%s: Transceiver State Has Changed to %s mode\n",
1217 ahd_name(ahd), now_lvd ? "LVD" : "SE");
1218 ahd_outb(ahd, CLRSINT0, CLRIOERR);
1219 /*
1220 * A change in I/O mode is equivalent to a bus reset.
1221 */
1222 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1223 ahd_pause(ahd);
1224 ahd_setup_iocell_workaround(ahd);
1225 ahd_unpause(ahd);
1226 } else if ((status0 & OVERRUN) != 0) {
1227 printf("%s: SCSI offset overrun detected. Resetting bus.\n",
1228 ahd_name(ahd));
1229 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1230 } else if ((status & SCSIRSTI) != 0) {
1231 printf("%s: Someone reset channel A\n", ahd_name(ahd));
1232 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/FALSE);
1233 } else if ((status & SCSIPERR) != 0) {
1234 ahd_handle_transmission_error(ahd);
1235 } else if (lqostat0 != 0) {
1236 printf("%s: lqostat0 == 0x%x!\n", ahd_name(ahd), lqostat0);
1237 ahd_outb(ahd, CLRLQOINT0, lqostat0);
1238 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
1239 ahd_outb(ahd, CLRLQOINT1, 0);
1240 }
1241 } else if ((status & SELTO) != 0) {
1242 u_int scbid;
1243
1244 /* Stop the selection */
1245 ahd_outb(ahd, SCSISEQ0, 0);
1246
1247 /* No more pending messages */
1248 ahd_clear_msg_state(ahd);
1249
1250 /* Clear interrupt state */
1251 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
1252
1253 /*
1254 * Although the driver does not care about the
1255 * 'Selection in Progress' status bit, the busy
1256 * LED does. SELINGO is only cleared by a sucessfull
1257 * selection, so we must manually clear it to insure
1258 * the LED turns off just incase no future successful
1259 * selections occur (e.g. no devices on the bus).
1260 */
1261 ahd_outb(ahd, CLRSINT0, CLRSELINGO);
1262
1263 scbid = ahd_inw(ahd, WAITING_TID_HEAD);
1264 scb = ahd_lookup_scb(ahd, scbid);
1265 if (scb == NULL) {
1266 printf("%s: ahd_intr - referenced scb not "
1267 "valid during SELTO scb(0x%x)\n",
1268 ahd_name(ahd), scbid);
1269 ahd_dump_card_state(ahd);
1270 } else {
1271 struct ahd_devinfo devinfo;
1272 #ifdef AHD_DEBUG
1273 if ((ahd_debug & AHD_SHOW_SELTO) != 0) {
1274 ahd_print_path(ahd, scb);
1275 printf("Saw Selection Timeout for SCB 0x%x\n",
1276 scbid);
1277 }
1278 #endif
1279 /*
1280 * Force a renegotiation with this target just in
1281 * case the cable was pulled and will later be
1282 * re-attached. The target may forget its negotiation
1283 * settings with us should it attempt to reselect
1284 * during the interruption. The target will not issue
1285 * a unit attention in this case, so we must always
1286 * renegotiate.
1287 */
1288 ahd_scb_devinfo(ahd, &devinfo, scb);
1289 ahd_force_renegotiation(ahd, &devinfo);
1290 ahd_set_transaction_status(scb, CAM_SEL_TIMEOUT);
1291 ahd_freeze_devq(ahd, scb);
1292 }
1293 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1294 ahd_iocell_first_selection(ahd);
1295 ahd_unpause(ahd);
1296 } else if ((status0 & (SELDI|SELDO)) != 0) {
1297 ahd_iocell_first_selection(ahd);
1298 ahd_unpause(ahd);
1299 } else if (status3 != 0) {
1300 printf("%s: SCSI Cell parity error SSTAT3 == 0x%x\n",
1301 ahd_name(ahd), status3);
1302 ahd_outb(ahd, CLRSINT3, status3);
1303 } else if ((lqistat1 & (LQIPHASE_LQ|LQIPHASE_NLQ)) != 0) {
1304 ahd_handle_lqiphase_error(ahd, lqistat1);
1305 } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
1306 /*
1307 * This status can be delayed during some
1308 * streaming operations. The SCSIPHASE
1309 * handler has already dealt with this case
1310 * so just clear the error.
1311 */
1312 ahd_outb(ahd, CLRLQIINT1, CLRLQICRCI_NLQ);
1313 } else if ((status & BUSFREE) != 0) {
1314 u_int lqostat1;
1315 int restart;
1316 int clear_fifo;
1317 int packetized;
1318 u_int mode;
1319
1320 /*
1321 * Clear our selection hardware as soon as possible.
1322 * We may have an entry in the waiting Q for this target,
1323 * that is affected by this busfree and we don't want to
1324 * go about selecting the target while we handle the event.
1325 */
1326 ahd_outb(ahd, SCSISEQ0, 0);
1327
1328 /*
1329 * Determine what we were up to at the time of
1330 * the busfree.
1331 */
1332 mode = AHD_MODE_SCSI;
1333 busfreetime = ahd_inb(ahd, SSTAT2) & BUSFREETIME;
1334 lqostat1 = ahd_inb(ahd, LQOSTAT1);
1335 switch (busfreetime) {
1336 case BUSFREE_DFF0:
1337 case BUSFREE_DFF1:
1338 {
1339 u_int scbid;
1340 struct scb *scb;
1341
1342 mode = busfreetime == BUSFREE_DFF0
1343 ? AHD_MODE_DFF0 : AHD_MODE_DFF1;
1344 ahd_set_modes(ahd, mode, mode);
1345 scbid = ahd_get_scbptr(ahd);
1346 scb = ahd_lookup_scb(ahd, scbid);
1347 if (scb == NULL) {
1348 printf("%s: Invalid SCB %d in DFF%d "
1349 "during unexpected busfree\n",
1350 ahd_name(ahd), scbid, mode);
1351 packetized = 0;
1352 } else
1353 packetized = (scb->flags & SCB_PACKETIZED) != 0;
1354 clear_fifo = 1;
1355 break;
1356 }
1357 case BUSFREE_LQO:
1358 clear_fifo = 0;
1359 packetized = 1;
1360 break;
1361 default:
1362 clear_fifo = 0;
1363 packetized = (lqostat1 & LQOBUSFREE) != 0;
1364 if (!packetized
1365 && ahd_inb(ahd, LASTPHASE) == P_BUSFREE)
1366 packetized = 1;
1367 break;
1368 }
1369
1370 #ifdef AHD_DEBUG
1371 if ((ahd_debug & AHD_SHOW_MISC) != 0)
1372 printf("Saw Busfree. Busfreetime = 0x%x.\n",
1373 busfreetime);
1374 #endif
1375 /*
1376 * Busfrees that occur in non-packetized phases are
1377 * handled by the nonpkt_busfree handler.
1378 */
1379 if (packetized && ahd_inb(ahd, LASTPHASE) == P_BUSFREE) {
1380 restart = ahd_handle_pkt_busfree(ahd, busfreetime);
1381 } else {
1382 packetized = 0;
1383 restart = ahd_handle_nonpkt_busfree(ahd);
1384 }
1385 /*
1386 * Clear the busfree interrupt status. The setting of
1387 * the interrupt is a pulse, so in a perfect world, we
1388 * would not need to muck with the ENBUSFREE logic. This
1389 * would ensure that if the bus moves on to another
1390 * connection, busfree protection is still in force. If
1391 * BUSFREEREV is broken, however, we must manually clear
1392 * the ENBUSFREE if the busfree occurred during a non-pack
1393 * connection so that we don't get false positives during
1394 * future, packetized, connections.
1395 */
1396 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
1397 if (packetized == 0
1398 && (ahd->bugs & AHD_BUSFREEREV_BUG) != 0)
1399 ahd_outb(ahd, SIMODE1,
1400 ahd_inb(ahd, SIMODE1) & ~ENBUSFREE);
1401
1402 if (clear_fifo)
1403 ahd_clear_fifo(ahd, mode);
1404
1405 ahd_clear_msg_state(ahd);
1406 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1407 if (restart) {
1408 ahd_restart(ahd);
1409 } else {
1410 ahd_unpause(ahd);
1411 }
1412 } else {
1413 printf("%s: Missing case in ahd_handle_scsiint. status = %x\n",
1414 ahd_name(ahd), status);
1415 ahd_dump_card_state(ahd);
1416 ahd_clear_intstat(ahd);
1417 ahd_unpause(ahd);
1418 }
1419 }
1420
1421 static void
1422 ahd_handle_transmission_error(struct ahd_softc *ahd)
1423 {
1424 struct scb *scb;
1425 u_int scbid;
1426 u_int lqistat1;
1427 u_int lqistat2;
1428 u_int msg_out;
1429 u_int curphase;
1430 u_int lastphase;
1431 u_int perrdiag;
1432 u_int cur_col;
1433 int silent;
1434
1435 scb = NULL;
1436 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1437 lqistat1 = ahd_inb(ahd, LQISTAT1) & ~(LQIPHASE_LQ|LQIPHASE_NLQ);
1438 lqistat2 = ahd_inb(ahd, LQISTAT2);
1439 if ((lqistat1 & (LQICRCI_NLQ|LQICRCI_LQ)) == 0
1440 && (ahd->bugs & AHD_NLQICRC_DELAYED_BUG) != 0) {
1441 u_int lqistate;
1442
1443 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
1444 lqistate = ahd_inb(ahd, LQISTATE);
1445 if ((lqistate >= 0x1E && lqistate <= 0x24)
1446 || (lqistate == 0x29)) {
1447 #ifdef AHD_DEBUG
1448 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0) {
1449 printf("%s: NLQCRC found via LQISTATE\n",
1450 ahd_name(ahd));
1451 }
1452 #endif
1453 lqistat1 |= LQICRCI_NLQ;
1454 }
1455 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1456 }
1457
1458 ahd_outb(ahd, CLRLQIINT1, lqistat1);
1459 lastphase = ahd_inb(ahd, LASTPHASE);
1460 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
1461 perrdiag = ahd_inb(ahd, PERRDIAG);
1462 msg_out = MSG_INITIATOR_DET_ERR;
1463 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR);
1464
1465 /*
1466 * Try to find the SCB associated with this error.
1467 */
1468 silent = FALSE;
1469 if (lqistat1 == 0
1470 || (lqistat1 & LQICRCI_NLQ) != 0) {
1471 if ((lqistat1 & (LQICRCI_NLQ|LQIOVERI_NLQ)) != 0)
1472 ahd_set_active_fifo(ahd);
1473 scbid = ahd_get_scbptr(ahd);
1474 scb = ahd_lookup_scb(ahd, scbid);
1475 if (scb != NULL && SCB_IS_SILENT(scb))
1476 silent = TRUE;
1477 }
1478
1479 cur_col = 0;
1480 if (silent == FALSE) {
1481 printf("%s: Transmission error detected\n", ahd_name(ahd));
1482 ahd_lqistat1_print(lqistat1, &cur_col, 50);
1483 ahd_lastphase_print(lastphase, &cur_col, 50);
1484 ahd_scsisigi_print(curphase, &cur_col, 50);
1485 ahd_perrdiag_print(perrdiag, &cur_col, 50);
1486 printf("\n");
1487 ahd_dump_card_state(ahd);
1488 }
1489
1490 if ((lqistat1 & (LQIOVERI_LQ|LQIOVERI_NLQ)) != 0) {
1491 if (silent == FALSE) {
1492 printf("%s: Gross protocol error during incoming "
1493 "packet. lqistat1 == 0x%x. Resetting bus.\n",
1494 ahd_name(ahd), lqistat1);
1495 }
1496 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1497 return;
1498 } else if ((lqistat1 & LQICRCI_LQ) != 0) {
1499 /*
1500 * A CRC error has been detected on an incoming LQ.
1501 * The bus is currently hung on the last ACK.
1502 * Hit LQIRETRY to release the last ack, and
1503 * wait for the sequencer to determine that ATNO
1504 * is asserted while in message out to take us
1505 * to our host message loop. No NONPACKREQ or
1506 * LQIPHASE type errors will occur in this
1507 * scenario. After this first LQIRETRY, the LQI
1508 * manager will be in ISELO where it will
1509 * happily sit until another packet phase begins.
1510 * Unexpected bus free detection is enabled
1511 * through any phases that occur after we release
1512 * this last ack until the LQI manager sees a
1513 * packet phase. This implies we may have to
1514 * ignore a perfectly valid "unexected busfree"
1515 * after our "initiator detected error" message is
1516 * sent. A busfree is the expected response after
1517 * we tell the target that it's L_Q was corrupted.
1518 * (SPI4R09 10.7.3.3.3)
1519 */
1520 ahd_outb(ahd, LQCTL2, LQIRETRY);
1521 printf("LQIRetry for LQICRCI_LQ to release ACK\n");
1522 } else if ((lqistat1 & LQICRCI_NLQ) != 0) {
1523 /*
1524 * We detected a CRC error in a NON-LQ packet.
1525 * The hardware has varying behavior in this situation
1526 * depending on whether this packet was part of a
1527 * stream or not.
1528 *
1529 * PKT by PKT mode:
1530 * The hardware has already acked the complete packet.
1531 * If the target honors our outstanding ATN condition,
1532 * we should be (or soon will be) in MSGOUT phase.
1533 * This will trigger the LQIPHASE_LQ status bit as the
1534 * hardware was expecting another LQ. Unexpected
1535 * busfree detection is enabled. Once LQIPHASE_LQ is
1536 * true (first entry into host message loop is much
1537 * the same), we must clear LQIPHASE_LQ and hit
1538 * LQIRETRY so the hardware is ready to handle
1539 * a future LQ. NONPACKREQ will not be asserted again
1540 * once we hit LQIRETRY until another packet is
1541 * processed. The target may either go busfree
1542 * or start another packet in response to our message.
1543 *
1544 * Read Streaming P0 asserted:
1545 * If we raise ATN and the target completes the entire
1546 * stream (P0 asserted during the last packet), the
1547 * hardware will ack all data and return to the ISTART
1548 * state. When the target reponds to our ATN condition,
1549 * LQIPHASE_LQ will be asserted. We should respond to
1550 * this with an LQIRETRY to prepare for any future
1551 * packets. NONPACKREQ will not be asserted again
1552 * once we hit LQIRETRY until another packet is
1553 * processed. The target may either go busfree or
1554 * start another packet in response to our message.
1555 * Busfree detection is enabled.
1556 *
1557 * Read Streaming P0 not asserted:
1558 * If we raise ATN and the target transitions to
1559 * MSGOUT in or after a packet where P0 is not
1560 * asserted, the hardware will assert LQIPHASE_NLQ.
1561 * We should respond to the LQIPHASE_NLQ with an
1562 * LQIRETRY. Should the target stay in a non-pkt
1563 * phase after we send our message, the hardware
1564 * will assert LQIPHASE_LQ. Recovery is then just as
1565 * listed above for the read streaming with P0 asserted.
1566 * Busfree detection is enabled.
1567 */
1568 if (silent == FALSE)
1569 printf("LQICRC_NLQ\n");
1570 if (scb == NULL) {
1571 printf("%s: No SCB valid for LQICRC_NLQ. "
1572 "Resetting bus\n", ahd_name(ahd));
1573 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1574 return;
1575 }
1576 } else if ((lqistat1 & LQIBADLQI) != 0) {
1577 printf("Need to handle BADLQI!\n");
1578 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1579 return;
1580 } else if ((perrdiag & (PARITYERR|PREVPHASE)) == PARITYERR) {
1581 if ((curphase & ~P_DATAIN_DT) != 0) {
1582 /* Ack the byte. So we can continue. */
1583 if (silent == FALSE)
1584 printf("Acking %s to clear perror\n",
1585 ahd_lookup_phase_entry(curphase)->phasemsg);
1586 ahd_inb(ahd, SCSIDAT);
1587 }
1588
1589 if (curphase == P_MESGIN)
1590 msg_out = MSG_PARITY_ERROR;
1591 }
1592
1593 /*
1594 * We've set the hardware to assert ATN if we
1595 * get a parity error on "in" phases, so all we
1596 * need to do is stuff the message buffer with
1597 * the appropriate message. "In" phases have set
1598 * mesg_out to something other than MSG_NOP.
1599 */
1600 ahd->send_msg_perror = msg_out;
1601 if (scb != NULL && msg_out == MSG_INITIATOR_DET_ERR)
1602 scb->flags |= SCB_TRANSMISSION_ERROR;
1603 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1604 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1605 ahd_unpause(ahd);
1606 }
1607
1608 static void
1609 ahd_handle_lqiphase_error(struct ahd_softc *ahd, u_int lqistat1)
1610 {
1611 /*
1612 * Clear the sources of the interrupts.
1613 */
1614 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1615 ahd_outb(ahd, CLRLQIINT1, lqistat1);
1616
1617 /*
1618 * If the "illegal" phase changes were in response
1619 * to our ATN to flag a CRC error, AND we ended up
1620 * on packet boundaries, clear the error, restart the
1621 * LQI manager as appropriate, and go on our merry
1622 * way toward sending the message. Otherwise, reset
1623 * the bus to clear the error.
1624 */
1625 ahd_set_active_fifo(ahd);
1626 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0
1627 && (ahd_inb(ahd, MDFFSTAT) & DLZERO) != 0) {
1628 if ((lqistat1 & LQIPHASE_LQ) != 0) {
1629 printf("LQIRETRY for LQIPHASE_LQ\n");
1630 ahd_outb(ahd, LQCTL2, LQIRETRY);
1631 } else if ((lqistat1 & LQIPHASE_NLQ) != 0) {
1632 printf("LQIRETRY for LQIPHASE_NLQ\n");
1633 ahd_outb(ahd, LQCTL2, LQIRETRY);
1634 } else
1635 panic("ahd_handle_lqiphase_error: No phase errors\n");
1636 ahd_dump_card_state(ahd);
1637 ahd_outb(ahd, CLRINT, CLRSCSIINT);
1638 ahd_unpause(ahd);
1639 } else {
1640 printf("Reseting Channel for LQI Phase error\n");
1641 ahd_dump_card_state(ahd);
1642 ahd_reset_channel(ahd, 'A', /*Initiate Reset*/TRUE);
1643 }
1644 }
1645
1646 /*
1647 * Packetized unexpected or expected busfree.
1648 * Entered in mode based on busfreetime.
1649 */
1650 static int
1651 ahd_handle_pkt_busfree(struct ahd_softc *ahd, u_int busfreetime)
1652 {
1653 u_int lqostat1;
1654
1655 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
1656 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
1657 lqostat1 = ahd_inb(ahd, LQOSTAT1);
1658 if ((lqostat1 & LQOBUSFREE) != 0) {
1659 struct scb *scb;
1660 u_int scbid;
1661 u_int saved_scbptr;
1662 u_int waiting_h;
1663 u_int waiting_t;
1664 u_int next;
1665
1666 if ((busfreetime & BUSFREE_LQO) == 0)
1667 printf("%s: Warning, BUSFREE time is 0x%x. "
1668 "Expected BUSFREE_LQO.\n",
1669 ahd_name(ahd), busfreetime);
1670 /*
1671 * The LQO manager detected an unexpected busfree
1672 * either:
1673 *
1674 * 1) During an outgoing LQ.
1675 * 2) After an outgoing LQ but before the first
1676 * REQ of the command packet.
1677 * 3) During an outgoing command packet.
1678 *
1679 * In all cases, CURRSCB is pointing to the
1680 * SCB that encountered the failure. Clean
1681 * up the queue, clear SELDO and LQOBUSFREE,
1682 * and allow the sequencer to restart the select
1683 * out at its lesure.
1684 */
1685 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1686 scbid = ahd_inw(ahd, CURRSCB);
1687 scb = ahd_lookup_scb(ahd, scbid);
1688 if (scb == NULL)
1689 panic("SCB not valid during LQOBUSFREE");
1690 /*
1691 * Clear the status.
1692 */
1693 ahd_outb(ahd, CLRLQOINT1, CLRLQOBUSFREE);
1694 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0)
1695 ahd_outb(ahd, CLRLQOINT1, 0);
1696 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
1697 ahd_flush_device_writes(ahd);
1698 ahd_outb(ahd, CLRSINT0, CLRSELDO);
1699
1700 /*
1701 * Return the LQO manager to its idle loop. It will
1702 * not do this automatically if the busfree occurs
1703 * after the first REQ of either the LQ or command
1704 * packet or between the LQ and command packet.
1705 */
1706 ahd_outb(ahd, LQCTL2, ahd_inb(ahd, LQCTL2) | LQOTOIDLE);
1707
1708 /*
1709 * Update the waiting for selection queue so
1710 * we restart on the correct SCB.
1711 */
1712 waiting_h = ahd_inw(ahd, WAITING_TID_HEAD);
1713 saved_scbptr = ahd_get_scbptr(ahd);
1714 if (waiting_h != scbid) {
1715
1716 ahd_outw(ahd, WAITING_TID_HEAD, scbid);
1717 waiting_t = ahd_inw(ahd, WAITING_TID_TAIL);
1718 if (waiting_t == waiting_h) {
1719 ahd_outw(ahd, WAITING_TID_TAIL, scbid);
1720 next = SCB_LIST_NULL;
1721 } else {
1722 ahd_set_scbptr(ahd, waiting_h);
1723 next = ahd_inw_scbram(ahd, SCB_NEXT2);
1724 }
1725 ahd_set_scbptr(ahd, scbid);
1726 ahd_outw(ahd, SCB_NEXT2, next);
1727 }
1728 ahd_set_scbptr(ahd, saved_scbptr);
1729 if (scb->crc_retry_count < AHD_MAX_LQ_CRC_ERRORS) {
1730 if (SCB_IS_SILENT(scb) == FALSE) {
1731 ahd_print_path(ahd, scb);
1732 printf("Probable outgoing LQ CRC error. "
1733 "Retrying command\n");
1734 }
1735 scb->crc_retry_count++;
1736 } else {
1737 ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
1738 ahd_freeze_scb(scb);
1739 ahd_freeze_devq(ahd, scb);
1740 }
1741 /* Return unpausing the sequencer. */
1742 return (0);
1743 } else if ((ahd_inb(ahd, PERRDIAG) & PARITYERR) != 0) {
1744 /*
1745 * Ignore what are really parity errors that
1746 * occur on the last REQ of a free running
1747 * clock prior to going busfree. Some drives
1748 * do not properly active negate just before
1749 * going busfree resulting in a parity glitch.
1750 */
1751 ahd_outb(ahd, CLRSINT1, CLRSCSIPERR|CLRBUSFREE);
1752 #ifdef AHD_DEBUG
1753 if ((ahd_debug & AHD_SHOW_MASKED_ERRORS) != 0)
1754 printf("%s: Parity on last REQ detected "
1755 "during busfree phase.\n",
1756 ahd_name(ahd));
1757 #endif
1758 /* Return unpausing the sequencer. */
1759 return (0);
1760 }
1761 if (ahd->src_mode != AHD_MODE_SCSI) {
1762 u_int scbid;
1763 struct scb *scb;
1764
1765 scbid = ahd_get_scbptr(ahd);
1766 scb = ahd_lookup_scb(ahd, scbid);
1767 ahd_print_path(ahd, scb);
1768 printf("Unexpected PKT busfree condition\n");
1769 ahd_dump_card_state(ahd);
1770 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb), 'A',
1771 SCB_GET_LUN(scb), SCB_GET_TAG(scb),
1772 ROLE_INITIATOR, CAM_UNEXP_BUSFREE);
1773
1774 /* Return restarting the sequencer. */
1775 return (1);
1776 }
1777 printf("%s: Unexpected PKT busfree condition\n", ahd_name(ahd));
1778 ahd_dump_card_state(ahd);
1779 /* Restart the sequencer. */
1780 return (1);
1781 }
1782
1783 /*
1784 * Non-packetized unexpected or expected busfree.
1785 */
1786 static int
1787 ahd_handle_nonpkt_busfree(struct ahd_softc *ahd)
1788 {
1789 struct ahd_devinfo devinfo;
1790 struct scb *scb;
1791 u_int lastphase;
1792 u_int saved_scsiid;
1793 u_int saved_lun;
1794 u_int target;
1795 u_int initiator_role_id;
1796 u_int scbid;
1797 u_int ppr_busfree;
1798 int printerror;
1799
1800 /*
1801 * Look at what phase we were last in. If its message out,
1802 * chances are pretty good that the busfree was in response
1803 * to one of our abort requests.
1804 */
1805 lastphase = ahd_inb(ahd, LASTPHASE);
1806 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
1807 saved_lun = ahd_inb(ahd, SAVED_LUN);
1808 target = SCSIID_TARGET(ahd, saved_scsiid);
1809 initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
1810 ahd_compile_devinfo(&devinfo, initiator_role_id,
1811 target, saved_lun, 'A', ROLE_INITIATOR);
1812 printerror = 1;
1813
1814 scbid = ahd_get_scbptr(ahd);
1815 scb = ahd_lookup_scb(ahd, scbid);
1816 if (scb != NULL
1817 && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1818 scb = NULL;
1819
1820 ppr_busfree = (ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0;
1821 if (lastphase == P_MESGOUT) {
1822 u_int tag;
1823
1824 tag = SCB_LIST_NULL;
1825 if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT_TAG, TRUE)
1826 || ahd_sent_msg(ahd, AHDMSG_1B, MSG_ABORT, TRUE)) {
1827 int found;
1828 int sent_msg;
1829
1830 if (scb == NULL) {
1831 ahd_print_devinfo(ahd, &devinfo);
1832 printf("Abort for unidentified "
1833 "connection completed.\n");
1834 /* restart the sequencer. */
1835 return (1);
1836 }
1837 sent_msg = ahd->msgout_buf[ahd->msgout_index - 1];
1838 ahd_print_path(ahd, scb);
1839 printf("SCB %d - Abort%s Completed.\n",
1840 SCB_GET_TAG(scb),
1841 sent_msg == MSG_ABORT_TAG ? "" : " Tag");
1842
1843 if (sent_msg == MSG_ABORT_TAG)
1844 tag = SCB_GET_TAG(scb);
1845
1846 if ((scb->flags & SCB_CMDPHASE_ABORT) != 0) {
1847 /*
1848 * This abort is in response to an
1849 * unexpected switch to command phase
1850 * for a packetized connection. Since
1851 * the identify message was never sent,
1852 * "saved lun" is 0. We really want to
1853 * abort only the SCB that encountered
1854 * this error, which could have a different
1855 * lun. The SCB will be retried so the OS
1856 * will see the UA after renegotiating to
1857 * packetized.
1858 */
1859 tag = SCB_GET_TAG(scb);
1860 saved_lun = scb->hscb->lun;
1861 }
1862 found = ahd_abort_scbs(ahd, target, 'A', saved_lun,
1863 tag, ROLE_INITIATOR,
1864 CAM_REQ_ABORTED);
1865 printf("found == 0x%x\n", found);
1866 printerror = 0;
1867 } else if (ahd_sent_msg(ahd, AHDMSG_1B,
1868 MSG_BUS_DEV_RESET, TRUE)) {
1869 #ifdef __FreeBSD__
1870 /*
1871 * Don't mark the user's request for this BDR
1872 * as completing with CAM_BDR_SENT. CAM3
1873 * specifies CAM_REQ_CMP.
1874 */
1875 if (scb != NULL
1876 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
1877 && ahd_match_scb(ahd, scb, target, 'A',
1878 CAM_LUN_WILDCARD, SCB_LIST_NULL,
1879 ROLE_INITIATOR))
1880 ahd_set_transaction_status(scb, CAM_REQ_CMP);
1881 #endif
1882 ahd_handle_devreset(ahd, &devinfo, CAM_LUN_WILDCARD,
1883 CAM_BDR_SENT, "Bus Device Reset",
1884 /*verbose_level*/0);
1885 printerror = 0;
1886 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, FALSE)
1887 && ppr_busfree == 0) {
1888 struct ahd_initiator_tinfo *tinfo;
1889 struct ahd_tmode_tstate *tstate;
1890
1891 /*
1892 * PPR Rejected. Try non-ppr negotiation
1893 * and retry command.
1894 */
1895 #ifdef AHD_DEBUG
1896 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1897 printf("PPR negotiation rejected busfree.\n");
1898 #endif
1899 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
1900 devinfo.our_scsiid,
1901 devinfo.target, &tstate);
1902 tinfo->curr.transport_version = 2;
1903 tinfo->goal.transport_version = 2;
1904 tinfo->goal.ppr_options = 0;
1905 ahd_qinfifo_requeue_tail(ahd, scb);
1906 printerror = 0;
1907 } else if ((ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, FALSE)
1908 || ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, FALSE))
1909 && ppr_busfree == 0) {
1910 /*
1911 * Negotiation Rejected. Go-async and
1912 * retry command.
1913 */
1914 #ifdef AHD_DEBUG
1915 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1916 printf("Negotiation rejected busfree.\n");
1917 #endif
1918 ahd_set_width(ahd, &devinfo,
1919 MSG_EXT_WDTR_BUS_8_BIT,
1920 AHD_TRANS_CUR|AHD_TRANS_GOAL,
1921 /*paused*/TRUE);
1922 ahd_set_syncrate(ahd, &devinfo,
1923 /*period*/0, /*offset*/0,
1924 /*ppr_options*/0,
1925 AHD_TRANS_CUR|AHD_TRANS_GOAL,
1926 /*paused*/TRUE);
1927 ahd_qinfifo_requeue_tail(ahd, scb);
1928 printerror = 0;
1929 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_IDE_BUSFREE) != 0
1930 && ahd_sent_msg(ahd, AHDMSG_1B,
1931 MSG_INITIATOR_DET_ERR, TRUE)) {
1932
1933 #ifdef AHD_DEBUG
1934 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1935 printf("Expected IDE Busfree\n");
1936 #endif
1937 printerror = 0;
1938 } else if ((ahd->msg_flags & MSG_FLAG_EXPECT_QASREJ_BUSFREE)
1939 && ahd_sent_msg(ahd, AHDMSG_1B,
1940 MSG_MESSAGE_REJECT, TRUE)) {
1941
1942 #ifdef AHD_DEBUG
1943 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1944 printf("Expected QAS Reject Busfree\n");
1945 #endif
1946 printerror = 0;
1947 }
1948 }
1949
1950 /*
1951 * The busfree required flag is honored at the end of
1952 * the message phases. We check it last in case we
1953 * had to send some other message that caused a busfree.
1954 */
1955 if (printerror != 0
1956 && (lastphase == P_MESGIN || lastphase == P_MESGOUT)
1957 && ((ahd->msg_flags & MSG_FLAG_EXPECT_PPR_BUSFREE) != 0)) {
1958
1959 ahd_freeze_devq(ahd, scb);
1960 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
1961 ahd_freeze_scb(scb);
1962 if ((ahd->msg_flags & MSG_FLAG_IU_REQ_CHANGED) != 0) {
1963 ahd_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1964 SCB_GET_CHANNEL(ahd, scb),
1965 SCB_GET_LUN(scb), SCB_LIST_NULL,
1966 ROLE_INITIATOR, CAM_REQ_ABORTED);
1967 } else {
1968 #ifdef AHD_DEBUG
1969 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
1970 printf("PPR Negotiation Busfree.\n");
1971 #endif
1972 ahd_done(ahd, scb);
1973 }
1974 printerror = 0;
1975 }
1976 if (printerror != 0) {
1977 int aborted;
1978
1979 aborted = 0;
1980 if (scb != NULL) {
1981 u_int tag;
1982
1983 if ((scb->hscb->control & TAG_ENB) != 0)
1984 tag = SCB_GET_TAG(scb);
1985 else
1986 tag = SCB_LIST_NULL;
1987 ahd_print_path(ahd, scb);
1988 aborted = ahd_abort_scbs(ahd, target, 'A',
1989 SCB_GET_LUN(scb), tag,
1990 ROLE_INITIATOR,
1991 CAM_UNEXP_BUSFREE);
1992 } else {
1993 /*
1994 * We had not fully identified this connection,
1995 * so we cannot abort anything.
1996 */
1997 printf("%s: ", ahd_name(ahd));
1998 }
1999 if (lastphase != P_BUSFREE)
2000 ahd_force_renegotiation(ahd, &devinfo);
2001 printf("Unexpected busfree %s, %d SCBs aborted, "
2002 "PRGMCNT == 0x%x\n",
2003 ahd_lookup_phase_entry(lastphase)->phasemsg,
2004 aborted,
2005 ahd_inb(ahd, PRGMCNT)
2006 | (ahd_inb(ahd, PRGMCNT+1) << 8));
2007 ahd_dump_card_state(ahd);
2008 }
2009 /* Always restart the sequencer. */
2010 return (1);
2011 }
2012
2013 static void
2014 ahd_handle_proto_violation(struct ahd_softc *ahd)
2015 {
2016 struct ahd_devinfo devinfo;
2017 struct scb *scb;
2018 u_int scbid;
2019 u_int seq_flags;
2020 u_int curphase;
2021 u_int lastphase;
2022 int found;
2023
2024 ahd_fetch_devinfo(ahd, &devinfo);
2025 scbid = ahd_get_scbptr(ahd);
2026 scb = ahd_lookup_scb(ahd, scbid);
2027 seq_flags = ahd_inb(ahd, SEQ_FLAGS);
2028 curphase = ahd_inb(ahd, SCSISIGI) & PHASE_MASK;
2029 lastphase = ahd_inb(ahd, LASTPHASE);
2030 if ((seq_flags & NOT_IDENTIFIED) != 0) {
2031
2032 /*
2033 * The reconnecting target either did not send an
2034 * identify message, or did, but we didn't find an SCB
2035 * to match.
2036 */
2037 ahd_print_devinfo(ahd, &devinfo);
2038 printf("Target did not send an IDENTIFY message. "
2039 "LASTPHASE = 0x%x.\n", lastphase);
2040 scb = NULL;
2041 } else if (scb == NULL) {
2042 /*
2043 * We don't seem to have an SCB active for this
2044 * transaction. Print an error and reset the bus.
2045 */
2046 ahd_print_devinfo(ahd, &devinfo);
2047 printf("No SCB found during protocol violation\n");
2048 goto proto_violation_reset;
2049 } else {
2050 ahd_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
2051 if ((seq_flags & NO_CDB_SENT) != 0) {
2052 ahd_print_path(ahd, scb);
2053 printf("No or incomplete CDB sent to device.\n");
2054 } else if ((ahd_inb_scbram(ahd, SCB_CONTROL)
2055 & STATUS_RCVD) == 0) {
2056 /*
2057 * The target never bothered to provide status to
2058 * us prior to completing the command. Since we don't
2059 * know the disposition of this command, we must attempt
2060 * to abort it. Assert ATN and prepare to send an abort
2061 * message.
2062 */
2063 ahd_print_path(ahd, scb);
2064 printf("Completed command without status.\n");
2065 } else {
2066 ahd_print_path(ahd, scb);
2067 printf("Unknown protocol violation.\n");
2068 ahd_dump_card_state(ahd);
2069 }
2070 }
2071 if ((lastphase & ~P_DATAIN_DT) == 0
2072 || lastphase == P_COMMAND) {
2073 proto_violation_reset:
2074 /*
2075 * Target either went directly to data
2076 * phase or didn't respond to our ATN.
2077 * The only safe thing to do is to blow
2078 * it away with a bus reset.
2079 */
2080 found = ahd_reset_channel(ahd, 'A', TRUE);
2081 printf("%s: Issued Channel %c Bus Reset. "
2082 "%d SCBs aborted\n", ahd_name(ahd), 'A', found);
2083 } else {
2084 /*
2085 * Leave the selection hardware off in case
2086 * this abort attempt will affect yet to
2087 * be sent commands.
2088 */
2089 ahd_outb(ahd, SCSISEQ0,
2090 ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2091 ahd_assert_atn(ahd);
2092 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2093 if (scb == NULL) {
2094 ahd_print_devinfo(ahd, &devinfo);
2095 ahd->msgout_buf[0] = MSG_ABORT_TASK;
2096 ahd->msgout_len = 1;
2097 ahd->msgout_index = 0;
2098 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2099 } else {
2100 ahd_print_path(ahd, scb);
2101 scb->flags |= SCB_ABORT;
2102 }
2103 printf("Protocol violation %s. Attempting to abort.\n",
2104 ahd_lookup_phase_entry(curphase)->phasemsg);
2105 }
2106 }
2107
2108 /*
2109 * Force renegotiation to occur the next time we initiate
2110 * a command to the current device.
2111 */
2112 static void
2113 ahd_force_renegotiation(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
2114 {
2115 struct ahd_initiator_tinfo *targ_info;
2116 struct ahd_tmode_tstate *tstate;
2117
2118 #ifdef AHD_DEBUG
2119 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
2120 ahd_print_devinfo(ahd, devinfo);
2121 printf("Forcing renegotiation\n");
2122 }
2123 #endif
2124 targ_info = ahd_fetch_transinfo(ahd,
2125 devinfo->channel,
2126 devinfo->our_scsiid,
2127 devinfo->target,
2128 &tstate);
2129 ahd_update_neg_request(ahd, devinfo, tstate,
2130 targ_info, AHD_NEG_IF_NON_ASYNC);
2131 }
2132
2133 #define AHD_MAX_STEPS 2000
2134 void
2135 ahd_clear_critical_section(struct ahd_softc *ahd)
2136 {
2137 ahd_mode_state saved_modes;
2138 int stepping;
2139 int steps;
2140 int first_instr;
2141 u_int simode0;
2142 u_int simode1;
2143 u_int simode3;
2144 u_int lqimode0;
2145 u_int lqimode1;
2146 u_int lqomode0;
2147 u_int lqomode1;
2148
2149 if (ahd->num_critical_sections == 0)
2150 return;
2151
2152 stepping = FALSE;
2153 steps = 0;
2154 first_instr = 0;
2155 simode0 = 0;
2156 simode1 = 0;
2157 simode3 = 0;
2158 lqimode0 = 0;
2159 lqimode1 = 0;
2160 lqomode0 = 0;
2161 lqomode1 = 0;
2162 saved_modes = ahd_save_modes(ahd);
2163 for (;;) {
2164 struct cs *cs;
2165 u_int seqaddr;
2166 u_int i;
2167
2168 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2169 seqaddr = ahd_inb(ahd, CURADDR)
2170 | (ahd_inb(ahd, CURADDR+1) << 8);
2171
2172 cs = ahd->critical_sections;
2173 for (i = 0; i < ahd->num_critical_sections; i++, cs++) {
2174
2175 if (cs->begin < seqaddr && cs->end >= seqaddr)
2176 break;
2177 }
2178
2179 if (i == ahd->num_critical_sections)
2180 break;
2181
2182 if (steps > AHD_MAX_STEPS) {
2183 printf("%s: Infinite loop in critical section\n"
2184 "%s: First Instruction 0x%x now 0x%x\n",
2185 ahd_name(ahd), ahd_name(ahd), first_instr,
2186 seqaddr);
2187 ahd_dump_card_state(ahd);
2188 panic("critical section loop");
2189 }
2190
2191 steps++;
2192 #ifdef AHD_DEBUG
2193 if ((ahd_debug & AHD_SHOW_MISC) != 0)
2194 printf("%s: Single stepping at 0x%x\n", ahd_name(ahd),
2195 seqaddr);
2196 #endif
2197 if (stepping == FALSE) {
2198
2199 first_instr = seqaddr;
2200 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2201 simode0 = ahd_inb(ahd, SIMODE0);
2202 simode3 = ahd_inb(ahd, SIMODE3);
2203 lqimode0 = ahd_inb(ahd, LQIMODE0);
2204 lqimode1 = ahd_inb(ahd, LQIMODE1);
2205 lqomode0 = ahd_inb(ahd, LQOMODE0);
2206 lqomode1 = ahd_inb(ahd, LQOMODE1);
2207 ahd_outb(ahd, SIMODE0, 0);
2208 ahd_outb(ahd, SIMODE3, 0);
2209 ahd_outb(ahd, LQIMODE0, 0);
2210 ahd_outb(ahd, LQIMODE1, 0);
2211 ahd_outb(ahd, LQOMODE0, 0);
2212 ahd_outb(ahd, LQOMODE1, 0);
2213 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2214 simode1 = ahd_inb(ahd, SIMODE1);
2215 ahd_outb(ahd, SIMODE1, ENBUSFREE);
2216 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) | STEP);
2217 stepping = TRUE;
2218 }
2219 ahd_outb(ahd, CLRSINT1, CLRBUSFREE);
2220 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2221 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
2222 ahd_outb(ahd, HCNTRL, ahd->unpause);
2223 do {
2224 ahd_delay(200);
2225 } while (!ahd_is_paused(ahd));
2226 ahd_update_modes(ahd);
2227 }
2228 if (stepping) {
2229 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
2230 ahd_outb(ahd, SIMODE0, simode0);
2231 ahd_outb(ahd, SIMODE3, simode3);
2232 ahd_outb(ahd, LQIMODE0, lqimode0);
2233 ahd_outb(ahd, LQIMODE1, lqimode1);
2234 ahd_outb(ahd, LQOMODE0, lqomode0);
2235 ahd_outb(ahd, LQOMODE1, lqomode1);
2236 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2237 ahd_outb(ahd, SEQCTL0, ahd_inb(ahd, SEQCTL0) & ~STEP);
2238 ahd_outb(ahd, SIMODE1, simode1);
2239 /*
2240 * SCSIINT seems to glitch occassionally when
2241 * the interrupt masks are restored. Clear SCSIINT
2242 * one more time so that only persistent errors
2243 * are seen as a real interrupt.
2244 */
2245 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2246 }
2247 ahd_restore_modes(ahd, saved_modes);
2248 }
2249
2250 /*
2251 * Clear any pending interrupt status.
2252 */
2253 void
2254 ahd_clear_intstat(struct ahd_softc *ahd)
2255 {
2256 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
2257 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
2258 /* Clear any interrupt conditions this may have caused */
2259 ahd_outb(ahd, CLRLQIINT0, CLRLQIATNQAS|CLRLQICRCT1|CLRLQICRCT2
2260 |CLRLQIBADLQT|CLRLQIATNLQ|CLRLQIATNCMD);
2261 ahd_outb(ahd, CLRLQIINT1, CLRLQIPHASE_LQ|CLRLQIPHASE_NLQ|CLRLIQABORT
2262 |CLRLQICRCI_LQ|CLRLQICRCI_NLQ|CLRLQIBADLQI
2263 |CLRLQIOVERI_LQ|CLRLQIOVERI_NLQ|CLRNONPACKREQ);
2264 ahd_outb(ahd, CLRLQOINT0, CLRLQOTARGSCBPERR|CLRLQOSTOPT2|CLRLQOATNLQ
2265 |CLRLQOATNPKT|CLRLQOTCRC);
2266 ahd_outb(ahd, CLRLQOINT1, CLRLQOINITSCBPERR|CLRLQOSTOPI2|CLRLQOBADQAS
2267 |CLRLQOBUSFREE|CLRLQOPHACHGINPKT);
2268 if ((ahd->bugs & AHD_CLRLQO_AUTOCLR_BUG) != 0) {
2269 ahd_outb(ahd, CLRLQOINT0, 0);
2270 ahd_outb(ahd, CLRLQOINT1, 0);
2271 }
2272 ahd_outb(ahd, CLRSINT3, CLRNTRAMPERR|CLROSRAMPERR);
2273 ahd_outb(ahd, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
2274 |CLRBUSFREE|CLRSCSIPERR|CLRREQINIT);
2275 ahd_outb(ahd, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO
2276 |CLRIOERR|CLROVERRUN);
2277 ahd_outb(ahd, CLRINT, CLRSCSIINT);
2278 }
2279
2280 /**************************** Debugging Routines ******************************/
2281 #ifdef AHD_DEBUG
2282 uint32_t ahd_debug = AHD_DEBUG_OPTS;
2283 #endif
2284 void
2285 ahd_print_scb(struct scb *scb)
2286 {
2287 struct hardware_scb *hscb;
2288 int i;
2289
2290 hscb = scb->hscb;
2291 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
2292 (void *)scb,
2293 hscb->control,
2294 hscb->scsiid,
2295 hscb->lun,
2296 hscb->cdb_len);
2297 printf("Shared Data: ");
2298 for (i = 0; i < sizeof(hscb->shared_data.idata.cdb); i++)
2299 printf("%#02x", hscb->shared_data.idata.cdb[i]);
2300 printf(" dataptr:%#x%x datacnt:%#x sgptr:%#x tag:%#x\n",
2301 (uint32_t)((ahd_le64toh(hscb->dataptr) >> 32) & 0xFFFFFFFF),
2302 (uint32_t)(ahd_le64toh(hscb->dataptr) & 0xFFFFFFFF),
2303 ahd_le32toh(hscb->datacnt),
2304 ahd_le32toh(hscb->sgptr),
2305 SCB_GET_TAG(scb));
2306 ahd_dump_sglist(scb);
2307 }
2308
2309 void
2310 ahd_dump_sglist(struct scb *scb)
2311 {
2312 int i;
2313
2314 if (scb->sg_count > 0) {
2315 if ((scb->ahd_softc->flags & AHD_64BIT_ADDRESSING) != 0) {
2316 struct ahd_dma64_seg *sg_list;
2317
2318 sg_list = (struct ahd_dma64_seg*)scb->sg_list;
2319 for (i = 0; i < scb->sg_count; i++) {
2320 uint64_t addr;
2321 uint32_t len;
2322
2323 addr = ahd_le64toh(sg_list[i].addr);
2324 len = ahd_le32toh(sg_list[i].len);
2325 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
2326 i,
2327 (uint32_t)((addr >> 32) & 0xFFFFFFFF),
2328 (uint32_t)(addr & 0xFFFFFFFF),
2329 sg_list[i].len & AHD_SG_LEN_MASK,
2330 (sg_list[i].len & AHD_DMA_LAST_SEG)
2331 ? " Last" : "");
2332 }
2333 } else {
2334 struct ahd_dma_seg *sg_list;
2335
2336 sg_list = (struct ahd_dma_seg*)scb->sg_list;
2337 for (i = 0; i < scb->sg_count; i++) {
2338 uint32_t len;
2339
2340 len = ahd_le32toh(sg_list[i].len);
2341 printf("sg[%d] - Addr 0x%x%x : Length %d%s\n",
2342 i,
2343 (len >> 24) & SG_HIGH_ADDR_BITS,
2344 ahd_le32toh(sg_list[i].addr),
2345 len & AHD_SG_LEN_MASK,
2346 len & AHD_DMA_LAST_SEG ? " Last" : "");
2347 }
2348 }
2349 }
2350 }
2351
2352 /************************* Transfer Negotiation *******************************/
2353 /*
2354 * Allocate per target mode instance (ID we respond to as a target)
2355 * transfer negotiation data structures.
2356 */
2357 static struct ahd_tmode_tstate *
2358 ahd_alloc_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel)
2359 {
2360 struct ahd_tmode_tstate *master_tstate;
2361 struct ahd_tmode_tstate *tstate;
2362 int i;
2363
2364 master_tstate = ahd->enabled_targets[ahd->our_id];
2365 if (ahd->enabled_targets[scsi_id] != NULL
2366 && ahd->enabled_targets[scsi_id] != master_tstate)
2367 panic("%s: ahd_alloc_tstate - Target already allocated",
2368 ahd_name(ahd));
2369 tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT | M_ZERO);
2370 if (tstate == NULL)
2371 return (NULL);
2372
2373 /*
2374 * If we have allocated a master tstate, copy user settings from
2375 * the master tstate (taken from SRAM or the EEPROM) for this
2376 * channel, but reset our current and goal settings to async/narrow
2377 * until an initiator talks to us.
2378 */
2379 if (master_tstate != NULL) {
2380 memcpy(tstate, master_tstate, sizeof(*tstate));
2381 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
2382 for (i = 0; i < 16; i++) {
2383 memset(&tstate->transinfo[i].curr, 0,
2384 sizeof(tstate->transinfo[i].curr));
2385 memset(&tstate->transinfo[i].goal, 0,
2386 sizeof(tstate->transinfo[i].goal));
2387 }
2388 } else
2389 memset(tstate, 0, sizeof(*tstate));
2390 ahd->enabled_targets[scsi_id] = tstate;
2391 return (tstate);
2392 }
2393
2394 #ifdef AHD_TARGET_MODE
2395 /*
2396 * Free per target mode instance (ID we respond to as a target)
2397 * transfer negotiation data structures.
2398 */
2399 static void
2400 ahd_free_tstate(struct ahd_softc *ahd, u_int scsi_id, char channel, int force)
2401 {
2402 struct ahd_tmode_tstate *tstate;
2403
2404 /*
2405 * Don't clean up our "master" tstate.
2406 * It has our default user settings.
2407 */
2408 if (scsi_id == ahd->our_id
2409 && force == FALSE)
2410 return;
2411
2412 tstate = ahd->enabled_targets[scsi_id];
2413 if (tstate != NULL)
2414 free(tstate, M_DEVBUF);
2415 ahd->enabled_targets[scsi_id] = NULL;
2416 }
2417 #endif
2418
2419 /*
2420 * Called when we have an active connection to a target on the bus,
2421 * this function finds the nearest period to the input period limited
2422 * by the capabilities of the bus connectivity of and sync settings for
2423 * the target.
2424 */
2425 void
2426 ahd_devlimited_syncrate(struct ahd_softc *ahd,
2427 struct ahd_initiator_tinfo *tinfo,
2428 u_int *period, u_int *ppr_options, role_t role)
2429 {
2430 struct ahd_transinfo *transinfo;
2431 u_int maxsync;
2432
2433 if ((ahd_inb(ahd, SBLKCTL) & ENAB40) != 0
2434 && (ahd_inb(ahd, SSTAT2) & EXP_ACTIVE) == 0) {
2435 maxsync = AHD_SYNCRATE_PACED;
2436 } else {
2437 maxsync = AHD_SYNCRATE_ULTRA;
2438 /* Can't do DT related options on an SE bus */
2439 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
2440 }
2441 /*
2442 * Never allow a value higher than our current goal
2443 * period otherwise we may allow a target initiated
2444 * negotiation to go above the limit as set by the
2445 * user. In the case of an initiator initiated
2446 * sync negotiation, we limit based on the user
2447 * setting. This allows the system to still accept
2448 * incoming negotiations even if target initiated
2449 * negotiation is not performed.
2450 */
2451 if (role == ROLE_TARGET)
2452 transinfo = &tinfo->user;
2453 else
2454 transinfo = &tinfo->goal;
2455 *ppr_options &= (transinfo->ppr_options|MSG_EXT_PPR_PCOMP_EN);
2456 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
2457 maxsync = MAX(maxsync, AHD_SYNCRATE_ULTRA2);
2458 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2459 }
2460 if (transinfo->period == 0) {
2461 *period = 0;
2462 *ppr_options = 0;
2463 } else {
2464 *period = MAX(*period, transinfo->period);
2465 ahd_find_syncrate(ahd, period, ppr_options, maxsync);
2466 }
2467 }
2468
2469 /*
2470 * Look up the valid period to SCSIRATE conversion in our table.
2471 * Return the period and offset that should be sent to the target
2472 * if this was the beginning of an SDTR.
2473 */
2474 void
2475 ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
2476 u_int *ppr_options, u_int maxsync)
2477 {
2478 if (*period < maxsync)
2479 *period = maxsync;
2480
2481 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) != 0
2482 && *period > AHD_SYNCRATE_MIN_DT)
2483 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2484
2485 if (*period > AHD_SYNCRATE_MIN)
2486 *period = 0;
2487
2488 /* Honor PPR option conformance rules. */
2489 if (*period > AHD_SYNCRATE_PACED)
2490 *ppr_options &= ~MSG_EXT_PPR_RTI;
2491
2492 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
2493 *ppr_options &= (MSG_EXT_PPR_DT_REQ|MSG_EXT_PPR_QAS_REQ);
2494
2495 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0)
2496 *ppr_options &= MSG_EXT_PPR_QAS_REQ;
2497
2498 /* Skip all PACED only entries if IU is not available */
2499 if ((*ppr_options & MSG_EXT_PPR_IU_REQ) == 0
2500 && *period < AHD_SYNCRATE_DT)
2501 *period = AHD_SYNCRATE_DT;
2502
2503 /* Skip all DT only entries if DT is not available */
2504 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
2505 && *period < AHD_SYNCRATE_ULTRA2)
2506 *period = AHD_SYNCRATE_ULTRA2;
2507 }
2508
2509 /*
2510 * Truncate the given synchronous offset to a value the
2511 * current adapter type and syncrate are capable of.
2512 */
2513 void
2514 ahd_validate_offset(struct ahd_softc *ahd,
2515 struct ahd_initiator_tinfo *tinfo,
2516 u_int period, u_int *offset, int wide,
2517 role_t role)
2518 {
2519 u_int maxoffset;
2520
2521 /* Limit offset to what we can do */
2522 if (period == 0)
2523 maxoffset = 0;
2524 else if (period <= AHD_SYNCRATE_PACED) {
2525 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
2526 maxoffset = MAX_OFFSET_PACED_BUG;
2527 else
2528 maxoffset = MAX_OFFSET_PACED;
2529 } else
2530 maxoffset = MAX_OFFSET_NON_PACED;
2531 *offset = MIN(*offset, maxoffset);
2532 if (tinfo != NULL) {
2533 if (role == ROLE_TARGET)
2534 *offset = MIN(*offset, tinfo->user.offset);
2535 else
2536 *offset = MIN(*offset, tinfo->goal.offset);
2537 }
2538 }
2539
2540 /*
2541 * Truncate the given transfer width parameter to a value the
2542 * current adapter type is capable of.
2543 */
2544 void
2545 ahd_validate_width(struct ahd_softc *ahd, struct ahd_initiator_tinfo *tinfo,
2546 u_int *bus_width, role_t role)
2547 {
2548 switch (*bus_width) {
2549 default:
2550 if (ahd->features & AHD_WIDE) {
2551 /* Respond Wide */
2552 *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2553 break;
2554 }
2555 /* FALLTHROUGH */
2556 case MSG_EXT_WDTR_BUS_8_BIT:
2557 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2558 break;
2559 }
2560 if (tinfo != NULL) {
2561 if (role == ROLE_TARGET)
2562 *bus_width = MIN(tinfo->user.width, *bus_width);
2563 else
2564 *bus_width = MIN(tinfo->goal.width, *bus_width);
2565 }
2566 }
2567
2568 /*
2569 * Update the bitmask of targets for which the controller should
2570 * negotiate with at the next convenient oportunity. This currently
2571 * means the next time we send the initial identify messages for
2572 * a new transaction.
2573 */
2574 int
2575 ahd_update_neg_request(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2576 struct ahd_tmode_tstate *tstate,
2577 struct ahd_initiator_tinfo *tinfo, ahd_neg_type neg_type)
2578 {
2579 u_int auto_negotiate_orig;
2580
2581 auto_negotiate_orig = tstate->auto_negotiate;
2582 if (neg_type == AHD_NEG_ALWAYS) {
2583 /*
2584 * Force our "current" settings to be
2585 * unknown so that unless a bus reset
2586 * occurs the need to renegotiate is
2587 * recorded persistently.
2588 */
2589 if ((ahd->features & AHD_WIDE) != 0)
2590 tinfo->curr.width = AHD_WIDTH_UNKNOWN;
2591 tinfo->curr.period = AHD_PERIOD_UNKNOWN;
2592 tinfo->curr.offset = AHD_OFFSET_UNKNOWN;
2593 }
2594 if (tinfo->curr.period != tinfo->goal.period
2595 || tinfo->curr.width != tinfo->goal.width
2596 || tinfo->curr.offset != tinfo->goal.offset
2597 || tinfo->curr.ppr_options != tinfo->goal.ppr_options
2598 || (neg_type == AHD_NEG_IF_NON_ASYNC
2599 && (tinfo->goal.offset != 0
2600 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
2601 || tinfo->goal.ppr_options != 0)))
2602 tstate->auto_negotiate |= devinfo->target_mask;
2603 else
2604 tstate->auto_negotiate &= ~devinfo->target_mask;
2605
2606 return (auto_negotiate_orig != tstate->auto_negotiate);
2607 }
2608
2609 /*
2610 * Update the user/goal/curr tables of synchronous negotiation
2611 * parameters as well as, in the case of a current or active update,
2612 * any data structures on the host controller. In the case of an
2613 * active update, the specified target is currently talking to us on
2614 * the bus, so the transfer parameter update must take effect
2615 * immediately.
2616 */
2617 void
2618 ahd_set_syncrate(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2619 u_int period, u_int offset, u_int ppr_options,
2620 u_int type, int paused)
2621 {
2622 struct ahd_initiator_tinfo *tinfo;
2623 struct ahd_tmode_tstate *tstate;
2624 u_int old_period;
2625 u_int old_offset;
2626 u_int old_ppr;
2627 int active;
2628 int update_needed;
2629
2630 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
2631 update_needed = 0;
2632
2633 if (period == 0 || offset == 0) {
2634 period = 0;
2635 offset = 0;
2636 }
2637
2638 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
2639 devinfo->target, &tstate);
2640
2641 if ((type & AHD_TRANS_USER) != 0) {
2642 tinfo->user.period = period;
2643 tinfo->user.offset = offset;
2644 tinfo->user.ppr_options = ppr_options;
2645 }
2646
2647 if ((type & AHD_TRANS_GOAL) != 0) {
2648 tinfo->goal.period = period;
2649 tinfo->goal.offset = offset;
2650 tinfo->goal.ppr_options = ppr_options;
2651 }
2652
2653 old_period = tinfo->curr.period;
2654 old_offset = tinfo->curr.offset;
2655 old_ppr = tinfo->curr.ppr_options;
2656
2657 if ((type & AHD_TRANS_CUR) != 0
2658 && (old_period != period
2659 || old_offset != offset
2660 || old_ppr != ppr_options)) {
2661
2662 update_needed++;
2663
2664 tinfo->curr.period = period;
2665 tinfo->curr.offset = offset;
2666 tinfo->curr.ppr_options = ppr_options;
2667
2668 ahd_send_async(ahd, devinfo->channel, devinfo->target,
2669 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
2670
2671 if (bootverbose) {
2672 if (offset != 0) {
2673 int options;
2674
2675 printf("%s: target %d synchronous with "
2676 "period = 0x%x, offset = 0x%x",
2677 ahd_name(ahd), devinfo->target,
2678 period, offset);
2679 options = 0;
2680 if ((ppr_options & MSG_EXT_PPR_RD_STRM) != 0) {
2681 printf("(RDSTRM");
2682 options++;
2683 }
2684 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
2685 printf("%s", options ? "|DT" : "(DT");
2686 options++;
2687 }
2688 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
2689 printf("%s", options ? "|IU" : "(IU");
2690 options++;
2691 }
2692 if ((ppr_options & MSG_EXT_PPR_RTI) != 0) {
2693 printf("%s", options ? "|RTI" : "(RTI");
2694 options++;
2695 }
2696 if ((ppr_options & MSG_EXT_PPR_QAS_REQ) != 0) {
2697 printf("%s", options ? "|QAS" : "(QAS");
2698 options++;
2699 }
2700 if (options != 0)
2701 printf(")\n");
2702 else
2703 printf("\n");
2704 } else {
2705 printf("%s: target %d using "
2706 "asynchronous transfers%s\n",
2707 ahd_name(ahd), devinfo->target,
2708 (ppr_options & MSG_EXT_PPR_QAS_REQ) != 0
2709 ? "(QAS)" : "");
2710 }
2711 }
2712 }
2713 /*
2714 * Always refresh the neg-table to handle the case of the
2715 * sequencer setting the ENATNO bit for a MK_MESSAGE request.
2716 * We will always renegotiate in that case if this is a
2717 * packetized request. Also manage the busfree expected flag
2718 * from this common routine so that we catch changes due to
2719 * WDTR or SDTR messages.
2720 */
2721 if ((type & AHD_TRANS_CUR) != 0) {
2722 if (!paused)
2723 ahd_pause(ahd);
2724 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
2725 if (!paused)
2726 ahd_unpause(ahd);
2727 if (ahd->msg_type != MSG_TYPE_NONE) {
2728 if ((old_ppr & MSG_EXT_PPR_IU_REQ)
2729 != (ppr_options & MSG_EXT_PPR_IU_REQ)) {
2730 #ifdef AHD_DEBUG
2731 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
2732 ahd_print_devinfo(ahd, devinfo);
2733 printf("Expecting IU Change busfree\n");
2734 }
2735 #endif
2736 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
2737 | MSG_FLAG_IU_REQ_CHANGED;
2738 }
2739 if ((old_ppr & MSG_EXT_PPR_IU_REQ) != 0) {
2740 #ifdef AHD_DEBUG
2741 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2742 printf("PPR with IU_REQ outstanding\n");
2743 #endif
2744 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE;
2745 }
2746 }
2747 }
2748
2749 update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
2750 tinfo, AHD_NEG_TO_GOAL);
2751
2752 if (update_needed)
2753 ahd_update_xfer_mode(ahd, devinfo);
2754 ahd->sc_req = 0;
2755
2756 if (update_needed && active)
2757 ahd_update_pending_scbs(ahd);
2758 }
2759
2760 /*
2761 * Update the user/goal/curr tables of wide negotiation
2762 * parameters as well as, in the case of a current or active update,
2763 * any data structures on the host controller. In the case of an
2764 * active update, the specified target is currently talking to us on
2765 * the bus, so the transfer parameter update must take effect
2766 * immediately.
2767 */
2768 void
2769 ahd_set_width(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2770 u_int width, u_int type, int paused)
2771 {
2772 struct ahd_initiator_tinfo *tinfo;
2773 struct ahd_tmode_tstate *tstate;
2774 u_int oldwidth;
2775 int active;
2776 int update_needed;
2777
2778 active = (type & AHD_TRANS_ACTIVE) == AHD_TRANS_ACTIVE;
2779 update_needed = 0;
2780 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
2781 devinfo->target, &tstate);
2782
2783 if ((type & AHD_TRANS_USER) != 0)
2784 tinfo->user.width = width;
2785
2786 if ((type & AHD_TRANS_GOAL) != 0)
2787 tinfo->goal.width = width;
2788
2789 oldwidth = tinfo->curr.width;
2790 if ((type & AHD_TRANS_CUR) != 0 && oldwidth != width) {
2791
2792 update_needed++;
2793
2794 tinfo->curr.width = width;
2795 ahd_send_async(ahd, devinfo->channel, devinfo->target,
2796 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
2797
2798 if (bootverbose) {
2799 printf("%s: target %d using %dbit transfers\n",
2800 ahd_name(ahd), devinfo->target,
2801 8 * (0x01 << width));
2802 }
2803 }
2804
2805 if ((type & AHD_TRANS_CUR) != 0) {
2806 if (!paused)
2807 ahd_pause(ahd);
2808 ahd_update_neg_table(ahd, devinfo, &tinfo->curr);
2809 if (!paused)
2810 ahd_unpause(ahd);
2811 }
2812
2813 update_needed += ahd_update_neg_request(ahd, devinfo, tstate,
2814 tinfo, AHD_NEG_TO_GOAL);
2815 if (update_needed && active)
2816 ahd_update_pending_scbs(ahd);
2817
2818 }
2819
2820 /*
2821 * Update the current state of tagged queuing for a given target.
2822 */
2823 void
2824 ahd_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2825 ahd_queue_alg alg)
2826 {
2827 ahd_platform_set_tags(ahd, devinfo, alg);
2828 ahd_send_async(ahd, devinfo->channel, devinfo->target,
2829 devinfo->lun, AC_TRANSFER_NEG, &alg);
2830 }
2831
2832 static void
2833 ahd_update_neg_table(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2834 struct ahd_transinfo *tinfo)
2835 {
2836 ahd_mode_state saved_modes;
2837 u_int period;
2838 u_int ppr_opts;
2839 u_int con_opts;
2840 u_int offset;
2841 u_int saved_negoaddr;
2842 uint8_t iocell_opts[sizeof(ahd->iocell_opts)];
2843
2844 saved_modes = ahd_save_modes(ahd);
2845 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2846
2847 saved_negoaddr = ahd_inb(ahd, NEGOADDR);
2848 ahd_outb(ahd, NEGOADDR, devinfo->target);
2849 period = tinfo->period;
2850 offset = tinfo->offset;
2851 memcpy(iocell_opts, ahd->iocell_opts, sizeof(ahd->iocell_opts));
2852 ppr_opts = tinfo->ppr_options & (MSG_EXT_PPR_QAS_REQ|MSG_EXT_PPR_DT_REQ
2853 |MSG_EXT_PPR_IU_REQ|MSG_EXT_PPR_RTI);
2854 con_opts = 0;
2855 if (period == 0)
2856 period = AHD_SYNCRATE_ASYNC;
2857 if (period == AHD_SYNCRATE_160) {
2858
2859 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0) {
2860 /*
2861 * When the SPI4 spec was finalized, PACE transfers
2862 * was not made a configurable option in the PPR
2863 * message. Instead it is assumed to be enabled for
2864 * any syncrate faster than 80MHz. Nevertheless,
2865 * Harpoon2A4 allows this to be configurable.
2866 *
2867 * Harpoon2A4 also assumes at most 2 data bytes per
2868 * negotiated REQ/ACK offset. Paced transfers take
2869 * 4, so we must adjust our offset.
2870 */
2871 ppr_opts |= PPROPT_PACE;
2872 offset *= 2;
2873
2874 /*
2875 * Harpoon2A assumed that there would be a
2876 * fallback rate between 160MHz and 80Mhz,
2877 * so 7 is used as the period factor rather
2878 * than 8 for 160MHz.
2879 */
2880 period = AHD_SYNCRATE_REVA_160;
2881 }
2882 if ((tinfo->ppr_options & MSG_EXT_PPR_PCOMP_EN) == 0)
2883 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &=
2884 ~AHD_PRECOMP_MASK;
2885 } else {
2886 /*
2887 * Precomp should be disabled for non-paced transfers.
2888 */
2889 iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;
2890
2891 if ((ahd->features & AHD_NEW_IOCELL_OPTS) != 0
2892 && (ppr_opts & MSG_EXT_PPR_DT_REQ) != 0) {
2893 /*
2894 * Slow down our CRC interval to be
2895 * compatible with devices that can't
2896 * handle a CRC at full speed.
2897 */
2898 con_opts |= ENSLOWCRC;
2899 }
2900 }
2901
2902 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PRECOMP_SLEW);
2903 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_PRECOMP_SLEW_INDEX]);
2904 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_AMPLITUDE);
2905 ahd_outb(ahd, ANNEXDAT, iocell_opts[AHD_AMPLITUDE_INDEX]);
2906
2907 ahd_outb(ahd, NEGPERIOD, period);
2908 ahd_outb(ahd, NEGPPROPTS, ppr_opts);
2909 ahd_outb(ahd, NEGOFFSET, offset);
2910
2911 if (tinfo->width == MSG_EXT_WDTR_BUS_16_BIT)
2912 con_opts |= WIDEXFER;
2913
2914 /*
2915 * During packetized transfers, the target will
2916 * give us the oportunity to send command packets
2917 * without us asserting attention.
2918 */
2919 if ((tinfo->ppr_options & MSG_EXT_PPR_IU_REQ) == 0)
2920 con_opts |= ENAUTOATNO;
2921 ahd_outb(ahd, NEGCONOPTS, con_opts);
2922 ahd_outb(ahd, NEGOADDR, saved_negoaddr);
2923 ahd_restore_modes(ahd, saved_modes);
2924 }
2925
2926 /*
2927 * When the transfer settings for a connection change, setup for
2928 * negotiation in pending SCBs to effect the change as quickly as
2929 * possible. We also cancel any negotiations that are scheduled
2930 * for inflight SCBs that have not been started yet.
2931 */
2932 static void
2933 ahd_update_pending_scbs(struct ahd_softc *ahd)
2934 {
2935 struct scb *pending_scb;
2936 int pending_scb_count;
2937 int i;
2938 int paused;
2939 u_int saved_scbptr;
2940 ahd_mode_state saved_modes;
2941
2942 /*
2943 * Traverse the pending SCB list and ensure that all of the
2944 * SCBs there have the proper settings. We can only safely
2945 * clear the negotiation required flag (setting requires the
2946 * execution queue to be modified) and this is only possible
2947 * if we are not already attempting to select out for this
2948 * SCB. For this reason, all callers only call this routine
2949 * if we are changing the negotiation settings for the currently
2950 * active transaction on the bus.
2951 */
2952 pending_scb_count = 0;
2953 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2954 struct ahd_devinfo devinfo;
2955 struct hardware_scb *pending_hscb;
2956 struct ahd_initiator_tinfo *tinfo;
2957 struct ahd_tmode_tstate *tstate;
2958
2959 ahd_scb_devinfo(ahd, &devinfo, pending_scb);
2960 tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
2961 devinfo.our_scsiid,
2962 devinfo.target, &tstate);
2963 pending_hscb = pending_scb->hscb;
2964 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
2965 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
2966 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
2967 pending_hscb->control &= ~MK_MESSAGE;
2968 }
2969 ahd_sync_scb(ahd, pending_scb,
2970 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2971 pending_scb_count++;
2972 }
2973
2974 if (pending_scb_count == 0)
2975 return;
2976
2977 if (ahd_is_paused(ahd)) {
2978 paused = 1;
2979 } else {
2980 paused = 0;
2981 ahd_pause(ahd);
2982 }
2983
2984 /*
2985 * Force the sequencer to reinitialize the selection for
2986 * the command at the head of the execution queue if it
2987 * has already been setup. The negotiation changes may
2988 * effect whether we select-out with ATN.
2989 */
2990 saved_modes = ahd_save_modes(ahd);
2991 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2992 ahd_outb(ahd, SCSISEQ0, ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
2993 saved_scbptr = ahd_get_scbptr(ahd);
2994 /* Ensure that the hscbs down on the card match the new information */
2995 for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
2996 struct hardware_scb *pending_hscb;
2997 u_int control;
2998 u_int scb_tag;
2999
3000 ahd_set_scbptr(ahd, i);
3001 scb_tag = i;
3002 pending_scb = ahd_lookup_scb(ahd, scb_tag);
3003 if (pending_scb == NULL)
3004 continue;
3005
3006 pending_hscb = pending_scb->hscb;
3007 control = ahd_inb_scbram(ahd, SCB_CONTROL);
3008 control &= ~MK_MESSAGE;
3009 control |= pending_hscb->control & MK_MESSAGE;
3010 ahd_outb(ahd, SCB_CONTROL, control);
3011 }
3012 ahd_set_scbptr(ahd, saved_scbptr);
3013 ahd_restore_modes(ahd, saved_modes);
3014
3015 if (paused == 0)
3016 ahd_unpause(ahd);
3017 }
3018
3019 /**************************** Pathing Information *****************************/
3020 static void
3021 ahd_fetch_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3022 {
3023 ahd_mode_state saved_modes;
3024 u_int saved_scsiid;
3025 role_t role;
3026 int our_id;
3027
3028 saved_modes = ahd_save_modes(ahd);
3029 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3030
3031 if (ahd_inb(ahd, SSTAT0) & TARGET)
3032 role = ROLE_TARGET;
3033 else
3034 role = ROLE_INITIATOR;
3035
3036 if (role == ROLE_TARGET
3037 && (ahd_inb(ahd, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
3038 /* We were selected, so pull our id from TARGIDIN */
3039 our_id = ahd_inb(ahd, TARGIDIN) & OID;
3040 } else if (role == ROLE_TARGET)
3041 our_id = ahd_inb(ahd, TOWNID);
3042 else
3043 our_id = ahd_inb(ahd, IOWNID);
3044
3045 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
3046 ahd_compile_devinfo(devinfo,
3047 our_id,
3048 SCSIID_TARGET(ahd, saved_scsiid),
3049 ahd_inb(ahd, SAVED_LUN),
3050 SCSIID_CHANNEL(ahd, saved_scsiid),
3051 role);
3052 ahd_restore_modes(ahd, saved_modes);
3053 }
3054
3055 void
3056 ahd_print_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3057 {
3058 printf("%s:%c:%d:%d: (0x%x) ", ahd_name(ahd), 'A',
3059 devinfo->target, devinfo->lun, ahd_get_scbptr(ahd));
3060 }
3061
3062 struct ahd_phase_table_entry*
3063 ahd_lookup_phase_entry(int phase)
3064 {
3065 struct ahd_phase_table_entry *entry;
3066 struct ahd_phase_table_entry *last_entry;
3067
3068 /*
3069 * num_phases doesn't include the default entry which
3070 * will be returned if the phase doesn't match.
3071 */
3072 last_entry = &ahd_phase_table[num_phases];
3073 for (entry = ahd_phase_table; entry < last_entry; entry++) {
3074 if (phase == entry->phase)
3075 break;
3076 }
3077 return (entry);
3078 }
3079
3080 void
3081 ahd_compile_devinfo(struct ahd_devinfo *devinfo, u_int our_id, u_int target,
3082 u_int lun, char channel, role_t role)
3083 {
3084 devinfo->our_scsiid = our_id;
3085 devinfo->target = target;
3086 devinfo->lun = lun;
3087 devinfo->target_offset = target;
3088 devinfo->channel = channel;
3089 devinfo->role = role;
3090 if (channel == 'B')
3091 devinfo->target_offset += 8;
3092 devinfo->target_mask = (0x01 << devinfo->target_offset);
3093 }
3094
3095 static void
3096 ahd_scb_devinfo(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3097 struct scb *scb)
3098 {
3099 role_t role;
3100 int our_id;
3101
3102 our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
3103 role = ROLE_INITIATOR;
3104 if ((scb->hscb->control & TARGET_SCB) != 0)
3105 role = ROLE_TARGET;
3106 ahd_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahd, scb),
3107 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahd, scb), role);
3108 }
3109
3110
3111 /************************ Message Phase Processing ****************************/
3112 /*
3113 * When an initiator transaction with the MK_MESSAGE flag either reconnects
3114 * or enters the initial message out phase, we are interrupted. Fill our
3115 * outgoing message buffer with the appropriate message and beging handing
3116 * the message phase(s) manually.
3117 */
3118 static void
3119 ahd_setup_initiator_msgout(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3120 struct scb *scb)
3121 {
3122 /*
3123 * To facilitate adding multiple messages together,
3124 * each routine should increment the index and len
3125 * variables instead of setting them explicitly.
3126 */
3127 ahd->msgout_index = 0;
3128 ahd->msgout_len = 0;
3129
3130 if (ahd_currently_packetized(ahd))
3131 ahd->msg_flags |= MSG_FLAG_PACKETIZED;
3132
3133 if (ahd->send_msg_perror
3134 && ahd_inb(ahd, MSG_OUT) == HOST_MSG) {
3135 ahd->msgout_buf[ahd->msgout_index++] = ahd->send_msg_perror;
3136 ahd->msgout_len++;
3137 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3138 #ifdef AHD_DEBUG
3139 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3140 printf("Setting up for Parity Error delivery\n");
3141 #endif
3142 return;
3143 } else if (scb == NULL) {
3144 printf("%s: WARNING. No pending message for "
3145 "I_T msgin. Issuing NO-OP\n", ahd_name(ahd));
3146 ahd->msgout_buf[ahd->msgout_index++] = MSG_NOOP;
3147 ahd->msgout_len++;
3148 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3149 return;
3150 }
3151
3152 if ((scb->flags & SCB_DEVICE_RESET) == 0
3153 && (scb->flags & SCB_PACKETIZED) == 0
3154 && ahd_inb(ahd, MSG_OUT) == MSG_IDENTIFYFLAG) {
3155 u_int identify_msg;
3156
3157 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
3158 if ((scb->hscb->control & DISCENB) != 0)
3159 identify_msg |= MSG_IDENTIFY_DISCFLAG;
3160 ahd->msgout_buf[ahd->msgout_index++] = identify_msg;
3161 ahd->msgout_len++;
3162
3163 if ((scb->hscb->control & TAG_ENB) != 0) {
3164 ahd->msgout_buf[ahd->msgout_index++] =
3165 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
3166 ahd->msgout_buf[ahd->msgout_index++] = SCB_GET_TAG(scb);
3167 ahd->msgout_len += 2;
3168 }
3169 }
3170
3171 if (scb->flags & SCB_DEVICE_RESET) {
3172 ahd->msgout_buf[ahd->msgout_index++] = MSG_BUS_DEV_RESET;
3173 ahd->msgout_len++;
3174 ahd_print_path(ahd, scb);
3175 printf("Bus Device Reset Message Sent\n");
3176 /*
3177 * Clear our selection hardware in advance of
3178 * the busfree. We may have an entry in the waiting
3179 * Q for this target, and we don't want to go about
3180 * selecting while we handle the busfree and blow it
3181 * away.
3182 */
3183 ahd_outb(ahd, SCSISEQ0, 0);
3184 } else if ((scb->flags & SCB_ABORT) != 0) {
3185
3186 if ((scb->hscb->control & TAG_ENB) != 0) {
3187 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT_TAG;
3188 } else {
3189 ahd->msgout_buf[ahd->msgout_index++] = MSG_ABORT;
3190 }
3191 ahd->msgout_len++;
3192 ahd_print_path(ahd, scb);
3193 printf("Abort%s Message Sent\n",
3194 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
3195 /*
3196 * Clear our selection hardware in advance of
3197 * the busfree. We may have an entry in the waiting
3198 * Q for this target, and we don't want to go about
3199 * selecting while we handle the busfree and blow it
3200 * away.
3201 */
3202 ahd_outb(ahd, SCSISEQ0, 0);
3203 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
3204 ahd_build_transfer_msg(ahd, devinfo);
3205 /*
3206 * Clear our selection hardware in advance of potential
3207 * PPR IU status change busfree. We may have an entry in
3208 * the waiting Q for this target, and we don't want to go
3209 * about selecting while we handle the busfree and blow
3210 * it away.
3211 */
3212 ahd_outb(ahd, SCSISEQ0, 0);
3213 } else {
3214 printf("ahd_intr: AWAITING_MSG for an SCB that "
3215 "does not have a waiting message\n");
3216 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
3217 devinfo->target_mask);
3218 panic("SCB = %d, SCB Control = %x:%x, MSG_OUT = %x "
3219 "SCB flags = %x", SCB_GET_TAG(scb), scb->hscb->control,
3220 ahd_inb(ahd, SCB_CONTROL), ahd_inb(ahd, MSG_OUT),
3221 scb->flags);
3222 }
3223
3224 /*
3225 * Clear the MK_MESSAGE flag from the SCB so we aren't
3226 * asked to send this message again.
3227 */
3228 ahd_outb(ahd, SCB_CONTROL,
3229 ahd_inb_scbram(ahd, SCB_CONTROL) & ~MK_MESSAGE);
3230 scb->hscb->control &= ~MK_MESSAGE;
3231 ahd->msgout_index = 0;
3232 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3233 }
3234
3235 /*
3236 * Build an appropriate transfer negotiation message for the
3237 * currently active target.
3238 */
3239 static void
3240 ahd_build_transfer_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3241 {
3242 /*
3243 * We need to initiate transfer negotiations.
3244 * If our current and goal settings are identical,
3245 * we want to renegotiate due to a check condition.
3246 */
3247 struct ahd_initiator_tinfo *tinfo;
3248 struct ahd_tmode_tstate *tstate;
3249 int dowide;
3250 int dosync;
3251 int doppr;
3252 u_int period;
3253 u_int ppr_options;
3254 u_int offset;
3255
3256 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
3257 devinfo->target, &tstate);
3258 /*
3259 * Filter our period based on the current connection.
3260 * If we can't perform DT transfers on this segment (not in LVD
3261 * mode for instance), then our decision to issue a PPR message
3262 * may change.
3263 */
3264 period = tinfo->goal.period;
3265 ppr_options = tinfo->goal.ppr_options;
3266 /* Target initiated PPR is not allowed in the SCSI spec */
3267 if (devinfo->role == ROLE_TARGET)
3268 ppr_options = 0;
3269 ahd_devlimited_syncrate(ahd, tinfo, &period,
3270 &ppr_options, devinfo->role);
3271 dowide = tinfo->curr.width != tinfo->goal.width;
3272 dosync = tinfo->curr.period != period;
3273 /*
3274 * Only use PPR if we have options that need it, even if the device
3275 * claims to support it. There might be an expander in the way
3276 * that doesn't.
3277 */
3278 doppr = ppr_options != 0;
3279
3280 if (!dowide && !dosync && !doppr) {
3281 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
3282 dosync = tinfo->goal.period != 0;
3283 }
3284
3285 if (!dowide && !dosync && !doppr) {
3286 /*
3287 * Force async with a WDTR message if we have a wide bus,
3288 * or just issue an SDTR with a 0 offset.
3289 */
3290 if ((ahd->features & AHD_WIDE) != 0)
3291 dowide = 1;
3292 else
3293 dosync = 1;
3294
3295 if (bootverbose) {
3296 ahd_print_devinfo(ahd, devinfo);
3297 printf("Ensuring async\n");
3298 }
3299 }
3300 /* Target initiated PPR is not allowed in the SCSI spec */
3301 if (devinfo->role == ROLE_TARGET)
3302 doppr = 0;
3303
3304 /*
3305 * Both the PPR message and SDTR message require the
3306 * goal syncrate to be limited to what the target device
3307 * is capable of handling (based on whether an LVD->SE
3308 * expander is on the bus), so combine these two cases.
3309 * Regardless, guarantee that if we are using WDTR and SDTR
3310 * messages that WDTR comes first.
3311 */
3312 if (doppr || (dosync && !dowide)) {
3313
3314 offset = tinfo->goal.offset;
3315 ahd_validate_offset(ahd, tinfo, period, &offset,
3316 doppr ? tinfo->goal.width
3317 : tinfo->curr.width,
3318 devinfo->role);
3319 if (doppr) {
3320 ahd_construct_ppr(ahd, devinfo, period, offset,
3321 tinfo->goal.width, ppr_options);
3322 } else {
3323 ahd_construct_sdtr(ahd, devinfo, period, offset);
3324 }
3325 } else {
3326 ahd_construct_wdtr(ahd, devinfo, tinfo->goal.width);
3327 }
3328 }
3329
3330 /*
3331 * Build a synchronous negotiation message in our message
3332 * buffer based on the input parameters.
3333 */
3334 static void
3335 ahd_construct_sdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3336 u_int period, u_int offset)
3337 {
3338 if (offset == 0)
3339 period = AHD_ASYNC_XFER_PERIOD;
3340 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3341 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR_LEN;
3342 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_SDTR;
3343 ahd->msgout_buf[ahd->msgout_index++] = period;
3344 ahd->msgout_buf[ahd->msgout_index++] = offset;
3345 ahd->msgout_len += 5;
3346 if (bootverbose) {
3347 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
3348 ahd_name(ahd), devinfo->channel, devinfo->target,
3349 devinfo->lun, period, offset);
3350 }
3351 }
3352
3353 /*
3354 * Build a wide negotiateion message in our message
3355 * buffer based on the input parameters.
3356 */
3357 static void
3358 ahd_construct_wdtr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3359 u_int bus_width)
3360 {
3361 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3362 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR_LEN;
3363 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_WDTR;
3364 ahd->msgout_buf[ahd->msgout_index++] = bus_width;
3365 ahd->msgout_len += 4;
3366 if (bootverbose) {
3367 printf("(%s:%c:%d:%d): Sending WDTR %x\n",
3368 ahd_name(ahd), devinfo->channel, devinfo->target,
3369 devinfo->lun, bus_width);
3370 }
3371 }
3372
3373 /*
3374 * Build a parallel protocol request message in our message
3375 * buffer based on the input parameters.
3376 */
3377 static void
3378 ahd_construct_ppr(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
3379 u_int period, u_int offset, u_int bus_width,
3380 u_int ppr_options)
3381 {
3382 /*
3383 * Always request precompensation from
3384 * the other target if we are running
3385 * at paced syncrates.
3386 */
3387 if (period <= AHD_SYNCRATE_PACED)
3388 ppr_options |= MSG_EXT_PPR_PCOMP_EN;
3389 if (offset == 0)
3390 period = AHD_ASYNC_XFER_PERIOD;
3391 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXTENDED;
3392 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR_LEN;
3393 ahd->msgout_buf[ahd->msgout_index++] = MSG_EXT_PPR;
3394 ahd->msgout_buf[ahd->msgout_index++] = period;
3395 ahd->msgout_buf[ahd->msgout_index++] = 0;
3396 ahd->msgout_buf[ahd->msgout_index++] = offset;
3397 ahd->msgout_buf[ahd->msgout_index++] = bus_width;
3398 ahd->msgout_buf[ahd->msgout_index++] = ppr_options;
3399 ahd->msgout_len += 8;
3400 if (bootverbose) {
3401 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period 0x%x, "
3402 "offset 0x%x, ppr_options 0x%x\n", ahd_name(ahd),
3403 devinfo->channel, devinfo->target, devinfo->lun,
3404 bus_width, period, offset, ppr_options);
3405 }
3406 }
3407
3408 /*
3409 * Clear any active message state.
3410 */
3411 static void
3412 ahd_clear_msg_state(struct ahd_softc *ahd)
3413 {
3414 ahd_mode_state saved_modes;
3415
3416 saved_modes = ahd_save_modes(ahd);
3417 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
3418 ahd->send_msg_perror = 0;
3419 ahd->msg_flags = MSG_FLAG_NONE;
3420 ahd->msgout_len = 0;
3421 ahd->msgin_index = 0;
3422 ahd->msg_type = MSG_TYPE_NONE;
3423 if ((ahd_inb(ahd, SCSISIGO) & ATNO) != 0) {
3424 /*
3425 * The target didn't care to respond to our
3426 * message request, so clear ATN.
3427 */
3428 ahd_outb(ahd, CLRSINT1, CLRATNO);
3429 }
3430 ahd_outb(ahd, MSG_OUT, MSG_NOOP);
3431 ahd_outb(ahd, SEQ_FLAGS2,
3432 ahd_inb(ahd, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
3433 ahd_restore_modes(ahd, saved_modes);
3434 }
3435
3436 /*
3437 * Manual message loop handler.
3438 */
3439 static void
3440 ahd_handle_message_phase(struct ahd_softc *ahd)
3441 {
3442 struct ahd_devinfo devinfo;
3443 u_int bus_phase;
3444 int end_session;
3445
3446 ahd_fetch_devinfo(ahd, &devinfo);
3447 end_session = FALSE;
3448 bus_phase = ahd_inb(ahd, LASTPHASE);
3449
3450 if ((ahd_inb(ahd, LQISTAT2) & LQIPHASE_OUTPKT) != 0) {
3451 printf("LQIRETRY for LQIPHASE_OUTPKT\n");
3452 ahd_outb(ahd, LQCTL2, LQIRETRY);
3453 }
3454 reswitch:
3455 switch (ahd->msg_type) {
3456 case MSG_TYPE_INITIATOR_MSGOUT:
3457 {
3458 int lastbyte;
3459 int phasemis;
3460 int msgdone;
3461
3462 if (ahd->msgout_len == 0 && ahd->send_msg_perror == 0)
3463 panic("HOST_MSG_LOOP interrupt with no active message");
3464
3465 #ifdef AHD_DEBUG
3466 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3467 ahd_print_devinfo(ahd, &devinfo);
3468 printf("INITIATOR_MSG_OUT");
3469 }
3470 #endif
3471 phasemis = bus_phase != P_MESGOUT;
3472 if (phasemis) {
3473 #ifdef AHD_DEBUG
3474 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3475 printf(" PHASEMIS %s\n",
3476 ahd_lookup_phase_entry(bus_phase)
3477 ->phasemsg);
3478 }
3479 #endif
3480 if (bus_phase == P_MESGIN) {
3481 /*
3482 * Change gears and see if
3483 * this messages is of interest to
3484 * us or should be passed back to
3485 * the sequencer.
3486 */
3487 ahd_outb(ahd, CLRSINT1, CLRATNO);
3488 ahd->send_msg_perror = 0;
3489 ahd->msg_type = MSG_TYPE_INITIATOR_MSGIN;
3490 ahd->msgin_index = 0;
3491 goto reswitch;
3492 }
3493 end_session = TRUE;
3494 break;
3495 }
3496
3497 if (ahd->send_msg_perror) {
3498 ahd_outb(ahd, CLRSINT1, CLRATNO);
3499 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3500 #ifdef AHD_DEBUG
3501 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3502 printf(" byte 0x%x\n", ahd->send_msg_perror);
3503 #endif
3504 /*
3505 * If we are notifying the target of a CRC error
3506 * during packetized operations, the target is
3507 * within its rights to acknowledge our message
3508 * with a busfree.
3509 */
3510 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0
3511 && ahd->send_msg_perror == MSG_INITIATOR_DET_ERR)
3512 ahd->msg_flags |= MSG_FLAG_EXPECT_IDE_BUSFREE;
3513
3514 ahd_outb(ahd, RETURN_2, ahd->send_msg_perror);
3515 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
3516 break;
3517 }
3518
3519 msgdone = ahd->msgout_index == ahd->msgout_len;
3520 if (msgdone) {
3521 /*
3522 * The target has requested a retry.
3523 * Re-assert ATN, reset our message index to
3524 * 0, and try again.
3525 */
3526 ahd->msgout_index = 0;
3527 ahd_assert_atn(ahd);
3528 }
3529
3530 lastbyte = ahd->msgout_index == (ahd->msgout_len - 1);
3531 if (lastbyte) {
3532 /* Last byte is signified by dropping ATN */
3533 ahd_outb(ahd, CLRSINT1, CLRATNO);
3534 }
3535
3536 /*
3537 * Clear our interrupt status and present
3538 * the next byte on the bus.
3539 */
3540 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3541 #ifdef AHD_DEBUG
3542 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3543 printf(" byte 0x%x\n",
3544 ahd->msgout_buf[ahd->msgout_index]);
3545 #endif
3546 ahd_outb(ahd, RETURN_2, ahd->msgout_buf[ahd->msgout_index++]);
3547 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_WRITE);
3548 break;
3549 }
3550 case MSG_TYPE_INITIATOR_MSGIN:
3551 {
3552 int phasemis;
3553 int message_done;
3554
3555 #ifdef AHD_DEBUG
3556 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3557 ahd_print_devinfo(ahd, &devinfo);
3558 printf("INITIATOR_MSG_IN");
3559 }
3560 #endif
3561 phasemis = bus_phase != P_MESGIN;
3562 if (phasemis) {
3563 #ifdef AHD_DEBUG
3564 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3565 printf(" PHASEMIS %s\n",
3566 ahd_lookup_phase_entry(bus_phase)
3567 ->phasemsg);
3568 }
3569 #endif
3570 ahd->msgin_index = 0;
3571 if (bus_phase == P_MESGOUT
3572 && (ahd->send_msg_perror != 0
3573 || (ahd->msgout_len != 0
3574 && ahd->msgout_index == 0))) {
3575 ahd->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
3576 goto reswitch;
3577 }
3578 end_session = TRUE;
3579 break;
3580 }
3581
3582 /* Pull the byte in without acking it */
3583 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIBUS);
3584 #ifdef AHD_DEBUG
3585 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
3586 printf(" byte 0x%x\n",
3587 ahd->msgin_buf[ahd->msgin_index]);
3588 #endif
3589
3590 message_done = ahd_parse_msg(ahd, &devinfo);
3591
3592 if (message_done) {
3593 /*
3594 * Clear our incoming message buffer in case there
3595 * is another message following this one.
3596 */
3597 ahd->msgin_index = 0;
3598
3599 /*
3600 * If this message illicited a response,
3601 * assert ATN so the target takes us to the
3602 * message out phase.
3603 */
3604 if (ahd->msgout_len != 0) {
3605 #ifdef AHD_DEBUG
3606 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0) {
3607 ahd_print_devinfo(ahd, &devinfo);
3608 printf("Asserting ATN for response\n");
3609 }
3610 #endif
3611 ahd_assert_atn(ahd);
3612 }
3613 } else
3614 ahd->msgin_index++;
3615
3616 if (message_done == MSGLOOP_TERMINATED) {
3617 end_session = TRUE;
3618 } else {
3619 /* Ack the byte */
3620 ahd_outb(ahd, CLRSINT1, CLRREQINIT);
3621 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_READ);
3622 }
3623 break;
3624 }
3625 case MSG_TYPE_TARGET_MSGIN:
3626 {
3627 int msgdone;
3628 int msgout_request;
3629
3630 /*
3631 * By default, the message loop will continue.
3632 */
3633 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
3634
3635 if (ahd->msgout_len == 0)
3636 panic("Target MSGIN with no active message");
3637
3638 /*
3639 * If we interrupted a mesgout session, the initiator
3640 * will not know this until our first REQ. So, we
3641 * only honor mesgout requests after we've sent our
3642 * first byte.
3643 */
3644 if ((ahd_inb(ahd, SCSISIGI) & ATNI) != 0
3645 && ahd->msgout_index > 0)
3646 msgout_request = TRUE;
3647 else
3648 msgout_request = FALSE;
3649
3650 if (msgout_request) {
3651
3652 /*
3653 * Change gears and see if
3654 * this messages is of interest to
3655 * us or should be passed back to
3656 * the sequencer.
3657 */
3658 ahd->msg_type = MSG_TYPE_TARGET_MSGOUT;
3659 ahd_outb(ahd, SCSISIGO, P_MESGOUT | BSYO);
3660 ahd->msgin_index = 0;
3661 /* Dummy read to REQ for first byte */
3662 ahd_inb(ahd, SCSIDAT);
3663 ahd_outb(ahd, SXFRCTL0,
3664 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
3665 break;
3666 }
3667
3668 msgdone = ahd->msgout_index == ahd->msgout_len;
3669 if (msgdone) {
3670 ahd_outb(ahd, SXFRCTL0,
3671 ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
3672 end_session = TRUE;
3673 break;
3674 }
3675
3676 /*
3677 * Present the next byte on the bus.
3678 */
3679 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) | SPIOEN);
3680 ahd_outb(ahd, SCSIDAT, ahd->msgout_buf[ahd->msgout_index++]);
3681 break;
3682 }
3683 case MSG_TYPE_TARGET_MSGOUT:
3684 {
3685 int lastbyte;
3686 int msgdone;
3687
3688 /*
3689 * By default, the message loop will continue.
3690 */
3691 ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
3692
3693 /*
3694 * The initiator signals that this is
3695 * the last byte by dropping ATN.
3696 */
3697 lastbyte = (ahd_inb(ahd, SCSISIGI) & ATNI) == 0;
3698
3699 /*
3700 * Read the latched byte, but turn off SPIOEN first
3701 * so that we don't inadvertently cause a REQ for the
3702 * next byte.
3703 */
3704 ahd_outb(ahd, SXFRCTL0, ahd_inb(ahd, SXFRCTL0) & ~SPIOEN);
3705 ahd->msgin_buf[ahd->msgin_index] = ahd_inb(ahd, SCSIDAT);
3706 msgdone = ahd_parse_msg(ahd, &devinfo);
3707 if (msgdone == MSGLOOP_TERMINATED) {
3708 /*
3709 * The message is *really* done in that it caused
3710 * us to go to bus free. The sequencer has already
3711 * been reset at this point, so pull the ejection
3712 * handle.
3713 */
3714 return;
3715 }
3716
3717 ahd->msgin_index++;
3718
3719 /*
3720 * XXX Read spec about initiator dropping ATN too soon
3721 * and use msgdone to detect it.
3722 */
3723 if (msgdone == MSGLOOP_MSGCOMPLETE) {
3724 ahd->msgin_index = 0;
3725
3726 /*
3727 * If this message illicited a response, transition
3728 * to the Message in phase and send it.
3729 */
3730 if (ahd->msgout_len != 0) {
3731 ahd_outb(ahd, SCSISIGO, P_MESGIN | BSYO);
3732 ahd_outb(ahd, SXFRCTL0,
3733 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
3734 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
3735 ahd->msgin_index = 0;
3736 break;
3737 }
3738 }
3739
3740 if (lastbyte)
3741 end_session = TRUE;
3742 else {
3743 /* Ask for the next byte. */
3744 ahd_outb(ahd, SXFRCTL0,
3745 ahd_inb(ahd, SXFRCTL0) | SPIOEN);
3746 }
3747
3748 break;
3749 }
3750 default:
3751 panic("Unknown REQINIT message type");
3752 }
3753
3754 if (end_session) {
3755 if ((ahd->msg_flags & MSG_FLAG_PACKETIZED) != 0) {
3756 printf("%s: Returning to Idle Loop\n",
3757 ahd_name(ahd));
3758 ahd_outb(ahd, LASTPHASE, P_BUSFREE);
3759 ahd_clear_msg_state(ahd);
3760 ahd_outb(ahd, SEQCTL0, FASTMODE|SEQRESET);
3761 } else {
3762 ahd_clear_msg_state(ahd);
3763 ahd_outb(ahd, RETURN_1, EXIT_MSG_LOOP);
3764 }
3765 }
3766 }
3767
3768 /*
3769 * See if we sent a particular extended message to the target.
3770 * If "full" is true, return true only if the target saw the full
3771 * message. If "full" is false, return true if the target saw at
3772 * least the first byte of the message.
3773 */
3774 static int
3775 ahd_sent_msg(struct ahd_softc *ahd, ahd_msgtype type, u_int msgval, int full)
3776 {
3777 int found;
3778 u_int index;
3779
3780 found = FALSE;
3781 index = 0;
3782
3783 while (index < ahd->msgout_len) {
3784 if (ahd->msgout_buf[index] == MSG_EXTENDED) {
3785 u_int end_index;
3786
3787 end_index = index + 1 + ahd->msgout_buf[index + 1];
3788 if (ahd->msgout_buf[index+2] == msgval
3789 && type == AHDMSG_EXT) {
3790
3791 if (full) {
3792 if (ahd->msgout_index > end_index)
3793 found = TRUE;
3794 } else if (ahd->msgout_index > index)
3795 found = TRUE;
3796 }
3797 index = end_index;
3798 } else if (ahd->msgout_buf[index] >= MSG_SIMPLE_TASK
3799 && ahd->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
3800
3801 /* Skip tag type and tag id or residue param*/
3802 index += 2;
3803 } else {
3804 /* Single byte message */
3805 if (type == AHDMSG_1B
3806 && ahd->msgout_index > index
3807 && (ahd->msgout_buf[index] == msgval
3808 || ((ahd->msgout_buf[index] & MSG_IDENTIFYFLAG) != 0
3809 && msgval == MSG_IDENTIFYFLAG)))
3810 found = TRUE;
3811 index++;
3812 }
3813
3814 if (found)
3815 break;
3816 }
3817 return (found);
3818 }
3819
3820 /*
3821 * Wait for a complete incoming message, parse it, and respond accordingly.
3822 */
3823 static int
3824 ahd_parse_msg(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3825 {
3826 struct ahd_initiator_tinfo *tinfo;
3827 struct ahd_tmode_tstate *tstate;
3828 int reject;
3829 int done;
3830 int response;
3831
3832 done = MSGLOOP_IN_PROG;
3833 response = FALSE;
3834 reject = FALSE;
3835 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
3836 devinfo->target, &tstate);
3837
3838 /*
3839 * Parse as much of the message as is available,
3840 * rejecting it if we don't support it. When
3841 * the entire message is available and has been
3842 * handled, return MSGLOOP_MSGCOMPLETE, indicating
3843 * that we have parsed an entire message.
3844 *
3845 * In the case of extended messages, we accept the length
3846 * byte outright and perform more checking once we know the
3847 * extended message type.
3848 */
3849 switch (ahd->msgin_buf[0]) {
3850 case MSG_DISCONNECT:
3851 case MSG_SAVEDATAPOINTER:
3852 case MSG_CMDCOMPLETE:
3853 case MSG_RESTOREPOINTERS:
3854 case MSG_IGN_WIDE_RESIDUE:
3855 /*
3856 * End our message loop as these are messages
3857 * the sequencer handles on its own.
3858 */
3859 done = MSGLOOP_TERMINATED;
3860 break;
3861 case MSG_MESSAGE_REJECT:
3862 response = ahd_handle_msg_reject(ahd, devinfo);
3863 /* FALLTHROUGH */
3864 case MSG_NOOP:
3865 done = MSGLOOP_MSGCOMPLETE;
3866 break;
3867 case MSG_EXTENDED:
3868 {
3869 /* Wait for enough of the message to begin validation */
3870 if (ahd->msgin_index < 2)
3871 break;
3872 switch (ahd->msgin_buf[2]) {
3873 case MSG_EXT_SDTR:
3874 {
3875 u_int period;
3876 u_int ppr_options;
3877 u_int offset;
3878 u_int saved_offset;
3879
3880 if (ahd->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
3881 reject = TRUE;
3882 break;
3883 }
3884
3885 /*
3886 * Wait until we have both args before validating
3887 * and acting on this message.
3888 *
3889 * Add one to MSG_EXT_SDTR_LEN to account for
3890 * the extended message preamble.
3891 */
3892 if (ahd->msgin_index < (MSG_EXT_SDTR_LEN + 1))
3893 break;
3894
3895 period = ahd->msgin_buf[3];
3896 ppr_options = 0;
3897 saved_offset = offset = ahd->msgin_buf[4];
3898 ahd_devlimited_syncrate(ahd, tinfo, &period,
3899 &ppr_options, devinfo->role);
3900 ahd_validate_offset(ahd, tinfo, period, &offset,
3901 tinfo->curr.width, devinfo->role);
3902 if (bootverbose) {
3903 printf("(%s:%c:%d:%d): Received "
3904 "SDTR period %x, offset %x\n\t"
3905 "Filtered to period %x, offset %x\n",
3906 ahd_name(ahd), devinfo->channel,
3907 devinfo->target, devinfo->lun,
3908 ahd->msgin_buf[3], saved_offset,
3909 period, offset);
3910 }
3911 ahd_set_syncrate(ahd, devinfo, period,
3912 offset, ppr_options,
3913 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
3914 /*paused*/TRUE);
3915
3916 /*
3917 * See if we initiated Sync Negotiation
3918 * and didn't have to fall down to async
3919 * transfers.
3920 */
3921 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, TRUE)) {
3922 /* We started it */
3923 if (saved_offset != offset) {
3924 /* Went too low - force async */
3925 reject = TRUE;
3926 }
3927 } else {
3928 /*
3929 * Send our own SDTR in reply
3930 */
3931 if (bootverbose
3932 && devinfo->role == ROLE_INITIATOR) {
3933 printf("(%s:%c:%d:%d): Target "
3934 "Initiated SDTR\n",
3935 ahd_name(ahd), devinfo->channel,
3936 devinfo->target, devinfo->lun);
3937 }
3938 ahd->msgout_index = 0;
3939 ahd->msgout_len = 0;
3940 ahd_construct_sdtr(ahd, devinfo,
3941 period, offset);
3942 ahd->msgout_index = 0;
3943 response = TRUE;
3944 }
3945 done = MSGLOOP_MSGCOMPLETE;
3946 break;
3947 }
3948 case MSG_EXT_WDTR:
3949 {
3950 u_int bus_width;
3951 u_int saved_width;
3952 u_int sending_reply;
3953
3954 sending_reply = FALSE;
3955 if (ahd->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
3956 reject = TRUE;
3957 break;
3958 }
3959
3960 /*
3961 * Wait until we have our arg before validating
3962 * and acting on this message.
3963 *
3964 * Add one to MSG_EXT_WDTR_LEN to account for
3965 * the extended message preamble.
3966 */
3967 if (ahd->msgin_index < (MSG_EXT_WDTR_LEN + 1))
3968 break;
3969
3970 bus_width = ahd->msgin_buf[3];
3971 saved_width = bus_width;
3972 ahd_validate_width(ahd, tinfo, &bus_width,
3973 devinfo->role);
3974 if (bootverbose) {
3975 printf("(%s:%c:%d:%d): Received WDTR "
3976 "%x filtered to %x\n",
3977 ahd_name(ahd), devinfo->channel,
3978 devinfo->target, devinfo->lun,
3979 saved_width, bus_width);
3980 }
3981
3982 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, TRUE)) {
3983 /*
3984 * Don't send a WDTR back to the
3985 * target, since we asked first.
3986 * If the width went higher than our
3987 * request, reject it.
3988 */
3989 if (saved_width > bus_width) {
3990 reject = TRUE;
3991 printf("(%s:%c:%d:%d): requested %dBit "
3992 "transfers. Rejecting...\n",
3993 ahd_name(ahd), devinfo->channel,
3994 devinfo->target, devinfo->lun,
3995 8 * (0x01 << bus_width));
3996 bus_width = 0;
3997 }
3998 } else {
3999 /*
4000 * Send our own WDTR in reply
4001 */
4002 if (bootverbose
4003 && devinfo->role == ROLE_INITIATOR) {
4004 printf("(%s:%c:%d:%d): Target "
4005 "Initiated WDTR\n",
4006 ahd_name(ahd), devinfo->channel,
4007 devinfo->target, devinfo->lun);
4008 }
4009 ahd->msgout_index = 0;
4010 ahd->msgout_len = 0;
4011 ahd_construct_wdtr(ahd, devinfo, bus_width);
4012 ahd->msgout_index = 0;
4013 response = TRUE;
4014 sending_reply = TRUE;
4015 }
4016 ahd_set_width(ahd, devinfo, bus_width,
4017 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4018 /*paused*/TRUE);
4019 /* After a wide message, we are async */
4020 ahd_set_syncrate(ahd, devinfo, /*period*/0,
4021 /*offset*/0, /*ppr_options*/0,
4022 AHD_TRANS_ACTIVE, /*paused*/TRUE);
4023 if (sending_reply == FALSE && reject == FALSE) {
4024
4025 if (tinfo->goal.offset) {
4026 ahd->msgout_index = 0;
4027 ahd->msgout_len = 0;
4028 ahd_build_transfer_msg(ahd, devinfo);
4029 ahd->msgout_index = 0;
4030 response = TRUE;
4031 }
4032 }
4033 done = MSGLOOP_MSGCOMPLETE;
4034 break;
4035 }
4036 case MSG_EXT_PPR:
4037 {
4038 u_int period;
4039 u_int offset;
4040 u_int bus_width;
4041 u_int ppr_options;
4042 u_int saved_width;
4043 u_int saved_offset;
4044 u_int saved_ppr_options;
4045
4046 if (ahd->msgin_buf[1] != MSG_EXT_PPR_LEN) {
4047 reject = TRUE;
4048 break;
4049 }
4050
4051 /*
4052 * Wait until we have all args before validating
4053 * and acting on this message.
4054 *
4055 * Add one to MSG_EXT_PPR_LEN to account for
4056 * the extended message preamble.
4057 */
4058 if (ahd->msgin_index < (MSG_EXT_PPR_LEN + 1))
4059 break;
4060
4061 period = ahd->msgin_buf[3];
4062 offset = ahd->msgin_buf[5];
4063 bus_width = ahd->msgin_buf[6];
4064 saved_width = bus_width;
4065 ppr_options = ahd->msgin_buf[7];
4066 /*
4067 * According to the spec, a DT only
4068 * period factor with no DT option
4069 * set implies async.
4070 */
4071 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
4072 && period <= 9)
4073 offset = 0;
4074 saved_ppr_options = ppr_options;
4075 saved_offset = offset;
4076
4077 /*
4078 * Transfer options are only available if we
4079 * are negotiating wide.
4080 */
4081 if (bus_width == 0)
4082 ppr_options &= MSG_EXT_PPR_QAS_REQ;
4083
4084 ahd_validate_width(ahd, tinfo, &bus_width,
4085 devinfo->role);
4086 ahd_devlimited_syncrate(ahd, tinfo, &period,
4087 &ppr_options, devinfo->role);
4088 ahd_validate_offset(ahd, tinfo, period, &offset,
4089 bus_width, devinfo->role);
4090
4091 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, TRUE)) {
4092 /*
4093 * If we are unable to do any of the
4094 * requested options (we went too low),
4095 * then we'll have to reject the message.
4096 */
4097 if (saved_width > bus_width
4098 || saved_offset != offset
4099 || saved_ppr_options != ppr_options) {
4100 reject = TRUE;
4101 period = 0;
4102 offset = 0;
4103 bus_width = 0;
4104 ppr_options = 0;
4105 }
4106 } else {
4107 if (devinfo->role != ROLE_TARGET)
4108 printf("(%s:%c:%d:%d): Target "
4109 "Initiated PPR\n",
4110 ahd_name(ahd), devinfo->channel,
4111 devinfo->target, devinfo->lun);
4112 else
4113 printf("(%s:%c:%d:%d): Initiator "
4114 "Initiated PPR\n",
4115 ahd_name(ahd), devinfo->channel,
4116 devinfo->target, devinfo->lun);
4117 ahd->msgout_index = 0;
4118 ahd->msgout_len = 0;
4119 ahd_construct_ppr(ahd, devinfo, period, offset,
4120 bus_width, ppr_options);
4121 ahd->msgout_index = 0;
4122 response = TRUE;
4123 }
4124 if (bootverbose) {
4125 printf("(%s:%c:%d:%d): Received PPR width %x, "
4126 "period %x, offset %x,options %x\n"
4127 "\tFiltered to width %x, period %x, "
4128 "offset %x, options %x\n",
4129 ahd_name(ahd), devinfo->channel,
4130 devinfo->target, devinfo->lun,
4131 saved_width, ahd->msgin_buf[3],
4132 saved_offset, saved_ppr_options,
4133 bus_width, period, offset, ppr_options);
4134 }
4135 ahd_set_width(ahd, devinfo, bus_width,
4136 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4137 /*paused*/TRUE);
4138 ahd_set_syncrate(ahd, devinfo, period,
4139 offset, ppr_options,
4140 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4141 /*paused*/TRUE);
4142
4143 done = MSGLOOP_MSGCOMPLETE;
4144 break;
4145 }
4146 default:
4147 /* Unknown extended message. Reject it. */
4148 reject = TRUE;
4149 break;
4150 }
4151 break;
4152 }
4153 #ifdef AHD_TARGET_MODE
4154 case MSG_BUS_DEV_RESET:
4155 ahd_handle_devreset(ahd, devinfo, CAM_LUN_WILDCARD,
4156 CAM_BDR_SENT,
4157 "Bus Device Reset Received",
4158 /*verbose_level*/0);
4159 ahd_restart(ahd);
4160 done = MSGLOOP_TERMINATED;
4161 break;
4162 case MSG_ABORT_TAG:
4163 case MSG_ABORT:
4164 case MSG_CLEAR_QUEUE:
4165 {
4166 int tag;
4167
4168 /* Target mode messages */
4169 if (devinfo->role != ROLE_TARGET) {
4170 reject = TRUE;
4171 break;
4172 }
4173 tag = SCB_LIST_NULL;
4174 if (ahd->msgin_buf[0] == MSG_ABORT_TAG)
4175 tag = ahd_inb(ahd, INITIATOR_TAG);
4176 ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
4177 devinfo->lun, tag, ROLE_TARGET,
4178 CAM_REQ_ABORTED);
4179
4180 tstate = ahd->enabled_targets[devinfo->our_scsiid];
4181 if (tstate != NULL) {
4182 struct ahd_tmode_lstate* lstate;
4183
4184 lstate = tstate->enabled_luns[devinfo->lun];
4185 if (lstate != NULL) {
4186 ahd_queue_lstate_event(ahd, lstate,
4187 devinfo->our_scsiid,
4188 ahd->msgin_buf[0],
4189 /*arg*/tag);
4190 ahd_send_lstate_events(ahd, lstate);
4191 }
4192 }
4193 ahd_restart(ahd);
4194 done = MSGLOOP_TERMINATED;
4195 break;
4196 }
4197 #endif
4198 case MSG_QAS_REQUEST:
4199 #ifdef AHD_DEBUG
4200 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
4201 printf("%s: QAS request. SCSISIGI == 0x%x\n",
4202 ahd_name(ahd), ahd_inb(ahd, SCSISIGI));
4203 #endif
4204 ahd->msg_flags |= MSG_FLAG_EXPECT_QASREJ_BUSFREE;
4205 /* FALLTHROUGH */
4206 case MSG_TERM_IO_PROC:
4207 default:
4208 reject = TRUE;
4209 break;
4210 }
4211
4212 if (reject) {
4213 /*
4214 * Setup to reject the message.
4215 */
4216 ahd->msgout_index = 0;
4217 ahd->msgout_len = 1;
4218 ahd->msgout_buf[0] = MSG_MESSAGE_REJECT;
4219 done = MSGLOOP_MSGCOMPLETE;
4220 response = TRUE;
4221 }
4222
4223 if (done != MSGLOOP_IN_PROG && !response)
4224 /* Clear the outgoing message buffer */
4225 ahd->msgout_len = 0;
4226
4227 return (done);
4228 }
4229
4230 /*
4231 * Process a message reject message.
4232 */
4233 static int
4234 ahd_handle_msg_reject(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4235 {
4236 /*
4237 * What we care about here is if we had an
4238 * outstanding SDTR or WDTR message for this
4239 * target. If we did, this is a signal that
4240 * the target is refusing negotiation.
4241 */
4242 struct scb *scb;
4243 struct ahd_initiator_tinfo *tinfo;
4244 struct ahd_tmode_tstate *tstate;
4245 u_int scb_index;
4246 u_int last_msg;
4247 int response = 0;
4248
4249 scb_index = ahd_get_scbptr(ahd);
4250 scb = ahd_lookup_scb(ahd, scb_index);
4251 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
4252 devinfo->our_scsiid,
4253 devinfo->target, &tstate);
4254 /* Might be necessary */
4255 last_msg = ahd_inb(ahd, LAST_MSG);
4256
4257 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
4258 if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_PPR, /*full*/TRUE)
4259 && tinfo->goal.period <= AHD_SYNCRATE_PACED) {
4260 /*
4261 * Target may not like our SPI-4 PPR Options.
4262 * Attempt to negotiate 80MHz which will turn
4263 * off these options.
4264 */
4265 if (bootverbose) {
4266 printf("(%s:%c:%d:%d): PPR Rejected. "
4267 "Trying simple U160 PPR\n",
4268 ahd_name(ahd), devinfo->channel,
4269 devinfo->target, devinfo->lun);
4270 }
4271 tinfo->goal.period = AHD_SYNCRATE_DT;
4272 tinfo->goal.ppr_options &= MSG_EXT_PPR_IU_REQ
4273 | MSG_EXT_PPR_QAS_REQ
4274 | MSG_EXT_PPR_DT_REQ;
4275 } else {
4276 /*
4277 * Target does not support the PPR message.
4278 * Attempt to negotiate SPI-2 style.
4279 */
4280 if (bootverbose) {
4281 printf("(%s:%c:%d:%d): PPR Rejected. "
4282 "Trying WDTR/SDTR\n",
4283 ahd_name(ahd), devinfo->channel,
4284 devinfo->target, devinfo->lun);
4285 }
4286 tinfo->goal.ppr_options = 0;
4287 tinfo->curr.transport_version = 2;
4288 tinfo->goal.transport_version = 2;
4289 }
4290 ahd->msgout_index = 0;
4291 ahd->msgout_len = 0;
4292 ahd_build_transfer_msg(ahd, devinfo);
4293 ahd->msgout_index = 0;
4294 response = 1;
4295 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
4296
4297 /* note 8bit xfers */
4298 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
4299 "8bit transfers\n", ahd_name(ahd),
4300 devinfo->channel, devinfo->target, devinfo->lun);
4301 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4302 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4303 /*paused*/TRUE);
4304 /*
4305 * No need to clear the sync rate. If the target
4306 * did not accept the command, our syncrate is
4307 * unaffected. If the target started the negotiation,
4308 * but rejected our response, we already cleared the
4309 * sync rate before sending our WDTR.
4310 */
4311 if (tinfo->goal.offset != tinfo->curr.offset) {
4312
4313 /* Start the sync negotiation */
4314 ahd->msgout_index = 0;
4315 ahd->msgout_len = 0;
4316 ahd_build_transfer_msg(ahd, devinfo);
4317 ahd->msgout_index = 0;
4318 response = 1;
4319 }
4320 } else if (ahd_sent_msg(ahd, AHDMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
4321 /* note asynch xfers and clear flag */
4322 ahd_set_syncrate(ahd, devinfo, /*period*/0,
4323 /*offset*/0, /*ppr_options*/0,
4324 AHD_TRANS_ACTIVE|AHD_TRANS_GOAL,
4325 /*paused*/TRUE);
4326 printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
4327 "Using asynchronous transfers\n",
4328 ahd_name(ahd), devinfo->channel,
4329 devinfo->target, devinfo->lun);
4330 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
4331 int tag_type;
4332 int mask;
4333
4334 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
4335
4336 if (tag_type == MSG_SIMPLE_TASK) {
4337 printf("(%s:%c:%d:%d): refuses tagged commands. "
4338 "Performing non-tagged I/O\n", ahd_name(ahd),
4339 devinfo->channel, devinfo->target, devinfo->lun);
4340 ahd_set_tags(ahd, devinfo, AHD_QUEUE_NONE);
4341 mask = ~0x23;
4342 } else {
4343 printf("(%s:%c:%d:%d): refuses %s tagged commands. "
4344 "Performing simple queue tagged I/O only\n",
4345 ahd_name(ahd), devinfo->channel, devinfo->target,
4346 devinfo->lun, tag_type == MSG_ORDERED_Q_TAG
4347 ? "ordered" : "head of queue");
4348 ahd_set_tags(ahd, devinfo, AHD_QUEUE_BASIC);
4349 mask = ~0x03;
4350 }
4351
4352 /*
4353 * Resend the identify for this CCB as the target
4354 * may believe that the selection is invalid otherwise.
4355 */
4356 ahd_outb(ahd, SCB_CONTROL,
4357 ahd_inb_scbram(ahd, SCB_CONTROL) & mask);
4358 scb->hscb->control &= mask;
4359 ahd_set_transaction_tag(scb, /*enabled*/FALSE,
4360 /*type*/MSG_SIMPLE_TASK);
4361 ahd_outb(ahd, MSG_OUT, MSG_IDENTIFYFLAG);
4362 ahd_assert_atn(ahd);
4363 ahd_busy_tcl(ahd, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
4364 SCB_GET_TAG(scb));
4365
4366 /*
4367 * Requeue all tagged commands for this target
4368 * currently in our posession so they can be
4369 * converted to untagged commands.
4370 */
4371 ahd_search_qinfifo(ahd, SCB_GET_TARGET(ahd, scb),
4372 SCB_GET_CHANNEL(ahd, scb),
4373 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
4374 ROLE_INITIATOR, CAM_REQUEUE_REQ,
4375 SEARCH_COMPLETE);
4376 } else if (ahd_sent_msg(ahd, AHDMSG_1B, MSG_IDENTIFYFLAG, TRUE)) {
4377 /*
4378 * Most likely the device believes that we had
4379 * previously negotiated packetized.
4380 */
4381 ahd->msg_flags |= MSG_FLAG_EXPECT_PPR_BUSFREE
4382 | MSG_FLAG_IU_REQ_CHANGED;
4383
4384 ahd_force_renegotiation(ahd, devinfo);
4385 ahd->msgout_index = 0;
4386 ahd->msgout_len = 0;
4387 ahd_build_transfer_msg(ahd, devinfo);
4388 ahd->msgout_index = 0;
4389 response = 1;
4390 } else {
4391 /*
4392 * Otherwise, we ignore it.
4393 */
4394 printf("%s:%c:%d: Message reject for %x -- ignored\n",
4395 ahd_name(ahd), devinfo->channel, devinfo->target,
4396 last_msg);
4397 }
4398 return (response);
4399 }
4400
4401 /*
4402 * Process an ignore wide residue message.
4403 */
4404 static void
4405 ahd_handle_ign_wide_residue(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4406 {
4407 u_int scb_index;
4408 struct scb *scb;
4409
4410 printf("%s: ahd_handle_ign_wide_residue\n", ahd_name(ahd));
4411
4412 scb_index = ahd_get_scbptr(ahd);
4413 scb = ahd_lookup_scb(ahd, scb_index);
4414 /*
4415 * XXX Actually check data direction in the sequencer?
4416 * Perhaps add datadir to some spare bits in the hscb?
4417 */
4418 if ((ahd_inb(ahd, SEQ_FLAGS) & DPHASE) == 0
4419 || ahd_get_transfer_dir(scb) != CAM_DIR_IN) {
4420 /*
4421 * Ignore the message if we haven't
4422 * seen an appropriate data phase yet.
4423 */
4424 } else {
4425 /*
4426 * If the residual occurred on the last
4427 * transfer and the transfer request was
4428 * expected to end on an odd count, do
4429 * nothing. Otherwise, subtract a byte
4430 * and update the residual count accordingly.
4431 */
4432 uint32_t sgptr;
4433
4434 sgptr = ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
4435 if ((sgptr & SG_LIST_NULL) != 0
4436 && (ahd_inb(ahd, SCB_TASK_ATTRIBUTE) & SCB_XFERLEN_ODD) != 0) {
4437 /*
4438 * If the residual occurred on the last
4439 * transfer and the transfer request was
4440 * expected to end on an odd count, do
4441 * nothing.
4442 */
4443 } else {
4444 uint32_t data_cnt;
4445 uint64_t data_addr;
4446 uint32_t sglen;
4447
4448 /* Pull in the rest of the sgptr */
4449 sgptr = ahd_inl_scbram(ahd, SCB_RESIDUAL_SGPTR);
4450 data_cnt = ahd_inl_scbram(ahd, SCB_RESIDUAL_DATACNT);
4451 if ((sgptr & SG_LIST_NULL) != 0) {
4452 /*
4453 * The residual data count is not updated
4454 * for the command run to completion case.
4455 * Explcitly zero the count.
4456 */
4457 data_cnt &= ~AHD_SG_LEN_MASK;
4458 }
4459 data_addr = ahd_inq(ahd, SHADDR);
4460 data_cnt += 1;
4461 data_addr -= 1;
4462 sgptr &= SG_PTR_MASK;
4463 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
4464 struct ahd_dma64_seg *sg;
4465
4466 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4467
4468 /*
4469 * The residual sg ptr points to the next S/G
4470 * to load so we must go back one.
4471 */
4472 sg--;
4473 sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
4474 if (sg != scb->sg_list
4475 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
4476
4477 sg--;
4478 sglen = ahd_le32toh(sg->len);
4479 /*
4480 * Preserve High Address and SG_LIST
4481 * bits while setting the count to 1.
4482 */
4483 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
4484 data_addr = ahd_le64toh(sg->addr)
4485 + (sglen & AHD_SG_LEN_MASK)
4486 - 1;
4487
4488 /*
4489 * Increment sg so it points to the
4490 * "next" sg.
4491 */
4492 sg++;
4493 sgptr = ahd_sg_virt_to_bus(ahd, scb,
4494 sg);
4495 }
4496 } else {
4497 struct ahd_dma_seg *sg;
4498
4499 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4500
4501 /*
4502 * The residual sg ptr points to the next S/G
4503 * to load so we must go back one.
4504 */
4505 sg--;
4506 sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
4507 if (sg != scb->sg_list
4508 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
4509
4510 sg--;
4511 sglen = ahd_le32toh(sg->len);
4512 /*
4513 * Preserve High Address and SG_LIST
4514 * bits while setting the count to 1.
4515 */
4516 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
4517 data_addr = ahd_le32toh(sg->addr)
4518 + (sglen & AHD_SG_LEN_MASK)
4519 - 1;
4520
4521 /*
4522 * Increment sg so it points to the
4523 * "next" sg.
4524 */
4525 sg++;
4526 sgptr = ahd_sg_virt_to_bus(ahd, scb,
4527 sg);
4528 }
4529 }
4530 /*
4531 * Toggle the "oddness" of the transfer length
4532 * to handle this mid-transfer ignore wide
4533 * residue. This ensures that the oddness is
4534 * correct for subsequent data transfers.
4535 */
4536 ahd_outb(ahd, SCB_TASK_ATTRIBUTE,
4537 ahd_inb(ahd, SCB_TASK_ATTRIBUTE) ^ SCB_XFERLEN_ODD);
4538
4539 ahd_outl(ahd, SCB_RESIDUAL_SGPTR, sgptr);
4540 ahd_outl(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
4541 /*
4542 * The FIFO's pointers will be updated if/when the
4543 * sequencer re-enters a data phase.
4544 */
4545 }
4546 }
4547 }
4548
4549
4550 /*
4551 * Reinitialize the data pointers for the active transfer
4552 * based on its current residual.
4553 */
4554 static void
4555 ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
4556 {
4557 struct scb *scb;
4558 ahd_mode_state saved_modes;
4559 u_int scb_index;
4560 u_int wait;
4561 uint32_t sgptr;
4562 uint32_t resid;
4563 uint64_t dataptr;
4564
4565 AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
4566 AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
4567
4568 scb_index = ahd_get_scbptr(ahd);
4569 scb = ahd_lookup_scb(ahd, scb_index);
4570
4571 /*
4572 * Release and reacquire the FIFO so we
4573 * have a clean slate.
4574 */
4575 ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
4576 wait = 1000;
4577 do {
4578 ahd_delay(100);
4579 } while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE));
4580 if (wait == 0) {
4581 ahd_print_path(ahd, scb);
4582 printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
4583 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
4584 }
4585 saved_modes = ahd_save_modes(ahd);
4586 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4587 ahd_outb(ahd, DFFSTAT,
4588 ahd_inb(ahd, DFFSTAT)
4589 | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
4590
4591 /*
4592 * Determine initial values for data_addr and data_cnt
4593 * for resuming the data phase.
4594 */
4595 sgptr = (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 3) << 24)
4596 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 2) << 16)
4597 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 1) << 8)
4598 | ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
4599 sgptr &= SG_PTR_MASK;
4600
4601 resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
4602 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
4603 | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
4604
4605 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
4606 struct ahd_dma64_seg *sg;
4607
4608 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4609
4610 /* The residual sg_ptr always points to the next sg */
4611 sg--;
4612
4613 dataptr = ahd_le64toh(sg->addr)
4614 + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
4615 - resid;
4616 ahd_outb(ahd, HADDR + 7, dataptr >> 56);
4617 ahd_outb(ahd, HADDR + 6, dataptr >> 48);
4618 ahd_outb(ahd, HADDR + 5, dataptr >> 40);
4619 ahd_outb(ahd, HADDR + 4, dataptr >> 32);
4620 } else {
4621 struct ahd_dma_seg *sg;
4622
4623 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4624
4625 /* The residual sg_ptr always points to the next sg */
4626 sg--;
4627
4628 dataptr = ahd_le32toh(sg->addr)
4629 + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
4630 - resid;
4631 ahd_outb(ahd, HADDR + 4,
4632 (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
4633 }
4634 ahd_outb(ahd, HADDR + 3, dataptr >> 24);
4635 ahd_outb(ahd, HADDR + 2, dataptr >> 16);
4636 ahd_outb(ahd, HADDR + 1, dataptr >> 8);
4637 ahd_outb(ahd, HADDR, dataptr);
4638 ahd_outb(ahd, HCNT + 2, resid >> 16);
4639 ahd_outb(ahd, HCNT + 1, resid >> 8);
4640 ahd_outb(ahd, HCNT, resid);
4641 }
4642
4643 /*
4644 * Handle the effects of issuing a bus device reset message.
4645 */
4646 static void
4647 ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4648 u_int lun, cam_status status, char *message,
4649 int verbose_level)
4650 {
4651 #ifdef AHD_TARGET_MODE
4652 struct ahd_tmode_tstate* tstate;
4653 #endif
4654 int found;
4655
4656 found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
4657 lun, SCB_LIST_NULL, devinfo->role,
4658 status);
4659
4660 #ifdef AHD_TARGET_MODE
4661 /*
4662 * Send an immediate notify ccb to all target mord peripheral
4663 * drivers affected by this action.
4664 */
4665 tstate = ahd->enabled_targets[devinfo->our_scsiid];
4666 if (tstate != NULL) {
4667 u_int cur_lun;
4668 u_int max_lun;
4669
4670 if (lun != CAM_LUN_WILDCARD) {
4671 cur_lun = 0;
4672 max_lun = AHD_NUM_LUNS - 1;
4673 } else {
4674 cur_lun = lun;
4675 max_lun = lun;
4676 }
4677 for (cur_lun <= max_lun; cur_lun++) {
4678 struct ahd_tmode_lstate* lstate;
4679
4680 lstate = tstate->enabled_luns[cur_lun];
4681 if (lstate == NULL)
4682 continue;
4683
4684 ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
4685 MSG_BUS_DEV_RESET, /*arg*/0);
4686 ahd_send_lstate_events(ahd, lstate);
4687 }
4688 }
4689 #endif
4690
4691 /*
4692 * Go back to async/narrow transfers and renegotiate.
4693 */
4694 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4695 AHD_TRANS_CUR, /*paused*/TRUE);
4696 ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
4697 /*ppr_options*/0, AHD_TRANS_CUR, /*paused*/TRUE);
4698
4699 ahd_send_async(ahd, devinfo->channel, devinfo->target,
4700 lun, AC_SENT_BDR, NULL);
4701
4702 if (message != NULL
4703 && (verbose_level <= bootverbose))
4704 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
4705 message, devinfo->channel, devinfo->target, found);
4706 }
4707
4708 #ifdef AHD_TARGET_MODE
4709 static void
4710 ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4711 struct scb *scb)
4712 {
4713
4714 /*
4715 * To facilitate adding multiple messages together,
4716 * each routine should increment the index and len
4717 * variables instead of setting them explicitly.
4718 */
4719 ahd->msgout_index = 0;
4720 ahd->msgout_len = 0;
4721
4722 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
4723 ahd_build_transfer_msg(ahd, devinfo);
4724 else
4725 panic("ahd_intr: AWAITING target message with no message");
4726
4727 ahd->msgout_index = 0;
4728 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
4729 }
4730 #endif
4731 /**************************** Initialization **********************************/
4732 static u_int
4733 ahd_sglist_size(struct ahd_softc *ahd)
4734 {
4735 bus_size_t list_size;
4736
4737 list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
4738 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
4739 list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
4740 return (list_size);
4741 }
4742
4743 /*
4744 * Calculate the optimum S/G List allocation size. S/G elements used
4745 * for a given transaction must be physically contiguous. Assume the
4746 * OS will allocate full pages to us, so it doesn't make sense to request
4747 * less than a page.
4748 */
4749 static u_int
4750 ahd_sglist_allocsize(struct ahd_softc *ahd)
4751 {
4752 bus_size_t sg_list_increment;
4753 bus_size_t sg_list_size;
4754 bus_size_t max_list_size;
4755 bus_size_t best_list_size;
4756
4757 /* Start out with the minimum required for AHD_NSEG. */
4758 sg_list_increment = ahd_sglist_size(ahd);
4759 sg_list_size = sg_list_increment;
4760
4761 /* Get us as close as possible to a page in size. */
4762 while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
4763 sg_list_size += sg_list_increment;
4764
4765 /*
4766 * Try to reduce the amount of wastage by allocating
4767 * multiple pages.
4768 */
4769 best_list_size = sg_list_size;
4770 max_list_size = roundup(sg_list_increment, PAGE_SIZE);
4771 if (max_list_size < 4 * PAGE_SIZE)
4772 max_list_size = 4 * PAGE_SIZE;
4773 if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
4774 max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
4775 while ((sg_list_size + sg_list_increment) <= max_list_size
4776 && (sg_list_size % PAGE_SIZE) != 0) {
4777 bus_size_t new_mod;
4778 bus_size_t best_mod;
4779
4780 sg_list_size += sg_list_increment;
4781 new_mod = sg_list_size % PAGE_SIZE;
4782 best_mod = best_list_size % PAGE_SIZE;
4783 if (new_mod > best_mod || new_mod == 0) {
4784 best_list_size = sg_list_size;
4785 }
4786 }
4787 return (best_list_size);
4788 }
4789
4790 int
4791 ahd_softc_init(struct ahd_softc *ahd)
4792 {
4793
4794 ahd->unpause = 0;
4795 ahd->pause = PAUSE;
4796 return (0);
4797 }
4798
4799 void
4800 ahd_set_unit(struct ahd_softc *ahd, int unit)
4801 {
4802 ahd->unit = unit;
4803 }
4804
4805 void
4806 ahd_set_name(struct ahd_softc *ahd, char *name)
4807 {
4808 if (ahd->name != NULL)
4809 free(ahd->name, M_DEVBUF);
4810 ahd->name = name;
4811 }
4812
4813 void
4814 ahd_free(struct ahd_softc *ahd)
4815 {
4816 int i;
4817
4818 switch (ahd->init_level) {
4819 default:
4820 case 2:
4821 ahd_shutdown(ahd);
4822 TAILQ_REMOVE(&ahd_tailq, ahd, links);
4823 /* FALLTHROUGH */
4824 case 1:
4825 bus_dmamap_unload(ahd->parent_dmat, ahd->shared_data_map.dmamap);
4826 bus_dmamap_destroy(ahd->parent_dmat, ahd->shared_data_map.dmamap);
4827 bus_dmamem_unmap(ahd->parent_dmat, (caddr_t)ahd->qoutfifo, ahd->shared_data_size);
4828 bus_dmamem_free(ahd->parent_dmat, &ahd->shared_data_map.dmasegs, ahd->shared_data_map.nseg);
4829 break;
4830 case 0:
4831 break;
4832 }
4833
4834 ahd_platform_free(ahd);
4835 ahd_fini_scbdata(ahd);
4836 for (i = 0; i < AHD_NUM_TARGETS; i++) {
4837 struct ahd_tmode_tstate *tstate;
4838
4839 tstate = ahd->enabled_targets[i];
4840 if (tstate != NULL) {
4841 #if AHD_TARGET_MODE
4842 int j;
4843
4844 for (j = 0; j < AHD_NUM_LUNS; j++) {
4845 struct ahd_tmode_lstate *lstate;
4846
4847 lstate = tstate->enabled_luns[j];
4848 if (lstate != NULL) {
4849 xpt_free_path(lstate->path);
4850 free(lstate, M_DEVBUF);
4851 }
4852 }
4853 #endif
4854 free(tstate, M_DEVBUF);
4855 }
4856 }
4857 #if AHD_TARGET_MODE
4858 if (ahd->black_hole != NULL) {
4859 xpt_free_path(ahd->black_hole->path);
4860 free(ahd->black_hole, M_DEVBUF);
4861 }
4862 #endif
4863 if (ahd->name != NULL)
4864 free(ahd->name, M_DEVBUF);
4865 if (ahd->seep_config != NULL)
4866 free(ahd->seep_config, M_DEVBUF);
4867 if (ahd->saved_stack != NULL)
4868 free(ahd->saved_stack, M_DEVBUF);
4869 #ifndef __FreeBSD__
4870 free(ahd, M_DEVBUF);
4871 #endif
4872 return;
4873 }
4874
4875 void
4876 ahd_shutdown(void *arg)
4877 {
4878 struct ahd_softc *ahd;
4879
4880 ahd = (struct ahd_softc *)arg;
4881
4882 #ifdef AHD_DEBUG
4883 printf("%s: ahd_shutdown\n", ahd_name(ahd));
4884 #endif
4885 /*
4886 * Stop periodic timer callbacks.
4887 */
4888 ahd_timer_stop(&ahd->reset_timer);
4889 ahd_timer_stop(&ahd->stat_timer);
4890
4891 /* This will reset most registers to 0, but not all */
4892 ahd_reset(ahd, /*reinit*/FALSE);
4893 }
4894
4895 /*
4896 * Reset the controller and record some information about it
4897 * that is only available just after a reset. If "reinit" is
4898 * non-zero, this reset occured after initial configuration
4899 * and the caller requests that the chip be fully reinitialized
4900 * to a runable state. Chip interrupts are *not* enabled after
4901 * a reinitialization. The caller must enable interrupts via
4902 * ahd_intr_enable().
4903 */
4904 int
4905 ahd_reset(struct ahd_softc *ahd, int reinit)
4906 {
4907 u_int sxfrctl1;
4908 int wait;
4909 uint32_t cmd;
4910 struct ahd_pci_busdata *bd = ahd->bus_data;
4911
4912 /*
4913 * Preserve the value of the SXFRCTL1 register for all channels.
4914 * It contains settings that affect termination and we don't want
4915 * to disturb the integrity of the bus.
4916 */
4917 ahd_pause(ahd);
4918 ahd_update_modes(ahd);
4919 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4920 sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
4921
4922 cmd = pci_conf_read(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG);
4923
4924 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
4925 uint32_t mod_cmd;
4926
4927 /*
4928 * A4 Razor #632
4929 * During the assertion of CHIPRST, the chip
4930 * does not disable its parity logic prior to
4931 * the start of the reset. This may cause a
4932 * parity error to be detected and thus a
4933 * spurious SERR or PERR assertion. Disble
4934 * PERR and SERR responses during the CHIPRST.
4935 */
4936 mod_cmd = cmd & ~(PCI_COMMAND_PARITY_ENABLE|PCI_COMMAND_SERR_ENABLE);
4937 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG, mod_cmd);
4938 }
4939 ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
4940
4941 /*
4942 * Ensure that the reset has finished. We delay 1000us
4943 * prior to reading the register to make sure the chip
4944 * has sufficiently completed its reset to handle register
4945 * accesses.
4946 */
4947 wait = 1000;
4948 do {
4949 ahd_delay(1000);
4950 } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
4951
4952 if (wait == 0) {
4953 printf("%s: WARNING - Failed chip reset! "
4954 "Trying to initialize anyway.\n", ahd_name(ahd));
4955 }
4956 ahd_outb(ahd, HCNTRL, ahd->pause);
4957
4958 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
4959 /*
4960 * Clear any latched PCI error status and restore
4961 * previous SERR and PERR response enables.
4962 */
4963 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG, 0x000000FF);
4964 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG,
4965 PCI_COMMAND_SERR_ENABLE|PCI_COMMAND_PARITY_ENABLE);
4966 }
4967
4968 /*
4969 * Mode should be SCSI after a chip reset, but lets
4970 * set it just to be safe. We touch the MODE_PTR
4971 * register directly so as to bypass the lazy update
4972 * ode in ahd_set_modes().
4973 */
4974 ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4975 ahd_outb(ahd, MODE_PTR,
4976 ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
4977
4978 /*
4979 * Restore SXFRCTL1.
4980 *
4981 * We must always initialize STPWEN to 1 before we
4982 * restore the saved values. STPWEN is initialized
4983 * to a tri-state condition which can only be cleared
4984 * by turning it on.
4985 */
4986 ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
4987 ahd_outb(ahd, SXFRCTL1, sxfrctl1);
4988
4989 /* Determine chip configuration */
4990 ahd->features &= ~AHD_WIDE;
4991 if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
4992 ahd->features |= AHD_WIDE;
4993
4994 /*
4995 * If a recovery action has forced a chip reset,
4996 * re-initialize the chip to our liking.
4997 */
4998 if (reinit != 0)
4999 ahd_chip_init(ahd);
5000
5001 return (0);
5002 }
5003
5004 /*
5005 * Determine the number of SCBs available on the controller
5006 */
5007 int
5008 ahd_probe_scbs(struct ahd_softc *ahd) {
5009 int i;
5010
5011 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
5012 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
5013 for (i = 0; i < AHD_SCB_MAX; i++) {
5014 int j;
5015 int ret;
5016
5017 ahd_set_scbptr(ahd, i);
5018 ahd_outw(ahd, SCB_BASE, i);
5019 for (j = 2; j < 64; j++)
5020 ahd_outb(ahd, SCB_BASE+j, 0);
5021 /* Start out life as unallocated (needing an abort) */
5022 ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
5023 ret = ahd_inw_scbram(ahd, SCB_BASE);
5024 if (ret != i) {
5025 printf("%s: ahd_probe_scbs (!=%d): returned 0x%x\n", ahd_name(ahd), i, ret);
5026 break;
5027 }
5028 ahd_set_scbptr(ahd, 0);
5029 ret = ahd_inw_scbram(ahd, SCB_BASE);
5030 if (ret != 0) {
5031 printf("ahd_probe_scbs (non zero): returned 0x%x\n", ret);
5032 break;
5033 }
5034 }
5035 return (i);
5036 }
5037
5038 static void
5039 ahd_initialize_hscbs(struct ahd_softc *ahd)
5040 {
5041 int i;
5042
5043 for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
5044 ahd_set_scbptr(ahd, i);
5045
5046 /* Clear the control byte. */
5047 ahd_outb(ahd, SCB_CONTROL, 0);
5048
5049 /* Set the next pointer */
5050 ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
5051 }
5052 }
5053
5054 static int
5055 ahd_init_scbdata(struct ahd_softc *ahd)
5056 {
5057 struct scb_data *scb_data;
5058 int i;
5059
5060 scb_data = &ahd->scb_data;
5061 TAILQ_INIT(&scb_data->free_scbs);
5062 for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
5063 LIST_INIT(&scb_data->free_scb_lists[i]);
5064 LIST_INIT(&scb_data->any_dev_free_scb_list);
5065 SLIST_INIT(&scb_data->hscb_maps);
5066 SLIST_INIT(&scb_data->sg_maps);
5067 SLIST_INIT(&scb_data->sense_maps);
5068
5069 /* Determine the number of hardware SCBs and initialize them */
5070 scb_data->maxhscbs = ahd_probe_scbs(ahd);
5071 if (scb_data->maxhscbs == 0) {
5072 printf("%s: No SCB space found\n", ahd_name(ahd));
5073 return (ENXIO);
5074 }
5075 ahd_initialize_hscbs(ahd);
5076
5077 /*
5078 * Create our DMA tags. These tags define the kinds of device
5079 * accessible memory allocations and memory mappings we will
5080 * need to perform during normal operation.
5081 *
5082 * Unless we need to further restrict the allocation, we rely
5083 * on the restrictions of the parent dmat, hence the common
5084 * use of MAXADDR and MAXSIZE.
5085 */
5086
5087 /* Perform initial CCB allocation */
5088 ahd_alloc_scbs(ahd);
5089
5090 if (scb_data->numscbs == 0) {
5091 printf("%s: ahd_init_scbdata - "
5092 "Unable to allocate initial scbs\n",
5093 ahd_name(ahd));
5094 goto error_exit;
5095 }
5096
5097 /*
5098 * Note that we were successfull
5099 */
5100 return (0);
5101
5102 error_exit:
5103
5104 return (ENOMEM);
5105 }
5106
5107 static struct scb *
5108 ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
5109 {
5110 struct scb *scb;
5111
5112 /*
5113 * Look on the pending list.
5114 */
5115 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
5116 if (SCB_GET_TAG(scb) == tag)
5117 return (scb);
5118 }
5119
5120 /*
5121 * Then on all of the collision free lists.
5122 */
5123 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
5124 struct scb *list_scb;
5125
5126 list_scb = scb;
5127 do {
5128 if (SCB_GET_TAG(list_scb) == tag)
5129 return (list_scb);
5130 list_scb = LIST_NEXT(list_scb, collision_links);
5131 } while (list_scb);
5132 }
5133
5134 /*
5135 * And finally on the generic free list.
5136 */
5137 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
5138 if (SCB_GET_TAG(scb) == tag)
5139 return (scb);
5140 }
5141
5142 return (NULL);
5143 }
5144
5145 static void
5146 ahd_fini_scbdata(struct ahd_softc *ahd)
5147 {
5148 struct scb_data *scb_data;
5149
5150 scb_data = &ahd->scb_data;
5151 if (scb_data == NULL)
5152 return;
5153
5154 switch (scb_data->init_level) {
5155 default:
5156 case 3:
5157 {
5158 struct map_node *sns_map;
5159
5160 while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
5161 SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
5162 ahd_freedmamem(ahd->parent_dmat, PAGE_SIZE,
5163 sns_map->dmamap, (caddr_t)sns_map->vaddr,
5164 &sns_map->dmasegs, sns_map->nseg);
5165 free(sns_map, M_DEVBUF);
5166 }
5167 /* FALLTHROUGH */
5168 }
5169 case 2:
5170 {
5171 struct map_node *sg_map;
5172
5173 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
5174 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
5175 ahd_freedmamem(ahd->parent_dmat, ahd_sglist_allocsize(ahd),
5176 sg_map->dmamap, (caddr_t)sg_map->vaddr,
5177 &sg_map->dmasegs, sg_map->nseg);
5178 free(sg_map, M_DEVBUF);
5179 }
5180 /* FALLTHROUGH */
5181 }
5182 case 1:
5183 {
5184 struct map_node *hscb_map;
5185
5186 while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
5187 SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
5188 ahd_freedmamem(ahd->parent_dmat, PAGE_SIZE,
5189 hscb_map->dmamap, (caddr_t)hscb_map->vaddr,
5190 &hscb_map->dmasegs, hscb_map->nseg);
5191 free(hscb_map, M_DEVBUF);
5192 }
5193 /* FALLTHROUGH */
5194 }
5195 case 0:
5196 break;
5197 }
5198 }
5199
5200 /*
5201 * DSP filter Bypass must be enabled until the first selection
5202 * after a change in bus mode (Razor #491 and #493).
5203 */
5204 static void
5205 ahd_setup_iocell_workaround(struct ahd_softc *ahd)
5206 {
5207 ahd_mode_state saved_modes;
5208
5209 saved_modes = ahd_save_modes(ahd);
5210 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5211 ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
5212 | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
5213 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
5214 #ifdef AHD_DEBUG
5215 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5216 printf("%s: Setting up iocell workaround\n", ahd_name(ahd));
5217 #endif
5218 ahd_restore_modes(ahd, saved_modes);
5219 }
5220
5221 static void
5222 ahd_iocell_first_selection(struct ahd_softc *ahd)
5223 {
5224 ahd_mode_state saved_modes;
5225 u_int sblkctl;
5226
5227 saved_modes = ahd_save_modes(ahd);
5228 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5229 sblkctl = ahd_inb(ahd, SBLKCTL);
5230 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5231 #ifdef AHD_DEBUG
5232 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5233 printf("%s: iocell first selection\n", ahd_name(ahd));
5234 #endif
5235 if ((sblkctl & ENAB40) != 0) {
5236 ahd_outb(ahd, DSPDATACTL,
5237 ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
5238 #ifdef AHD_DEBUG
5239 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5240 printf("%s: BYPASS now disabled\n", ahd_name(ahd));
5241 #endif
5242 }
5243 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
5244 ahd_outb(ahd, CLRINT, CLRSCSIINT);
5245 ahd_restore_modes(ahd, saved_modes);
5246 }
5247
5248 /*************************** SCB Management ***********************************/
5249 static void
5250 ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
5251 {
5252 struct scb_list *free_list;
5253 struct scb_tailq *free_tailq;
5254 struct scb *first_scb;
5255
5256 scb->flags |= SCB_ON_COL_LIST;
5257 AHD_SET_SCB_COL_IDX(scb, col_idx);
5258 free_list = &ahd->scb_data.free_scb_lists[col_idx];
5259 free_tailq = &ahd->scb_data.free_scbs;
5260 first_scb = LIST_FIRST(free_list);
5261 if (first_scb != NULL) {
5262 LIST_INSERT_AFTER(first_scb, scb, collision_links);
5263 } else {
5264 LIST_INSERT_HEAD(free_list, scb, collision_links);
5265 TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
5266 }
5267 }
5268
5269 static void
5270 ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
5271 {
5272 struct scb_list *free_list;
5273 struct scb_tailq *free_tailq;
5274 struct scb *first_scb;
5275 u_int col_idx;
5276
5277 scb->flags &= ~SCB_ON_COL_LIST;
5278 col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
5279 free_list = &ahd->scb_data.free_scb_lists[col_idx];
5280 free_tailq = &ahd->scb_data.free_scbs;
5281 first_scb = LIST_FIRST(free_list);
5282 if (first_scb == scb) {
5283 struct scb *next_scb;
5284
5285 /*
5286 * Maintain order in the collision free
5287 * lists for fairness if this device has
5288 * other colliding tags active.
5289 */
5290 next_scb = LIST_NEXT(scb, collision_links);
5291 if (next_scb != NULL) {
5292 TAILQ_INSERT_AFTER(free_tailq, scb,
5293 next_scb, links.tqe);
5294 }
5295 TAILQ_REMOVE(free_tailq, scb, links.tqe);
5296 }
5297 LIST_REMOVE(scb, collision_links);
5298 }
5299
5300 /*
5301 * Get a free scb. If there are none, see if we can allocate a new SCB.
5302 */
5303 struct scb *
5304 ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
5305 {
5306 struct scb *scb;
5307 int tries;
5308
5309 tries = 0;
5310 look_again:
5311 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
5312 if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
5313 ahd_rem_col_list(ahd, scb);
5314 goto found;
5315 }
5316 }
5317 if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
5318
5319 if (tries++ != 0)
5320 return (NULL);
5321 ahd_alloc_scbs(ahd);
5322 goto look_again;
5323 }
5324 LIST_REMOVE(scb, links.le);
5325 if (col_idx != AHD_NEVER_COL_IDX
5326 && (scb->col_scb != NULL)
5327 && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
5328 LIST_REMOVE(scb->col_scb, links.le);
5329 ahd_add_col_list(ahd, scb->col_scb, col_idx);
5330 }
5331 found:
5332 scb->flags |= SCB_ACTIVE;
5333 return (scb);
5334 }
5335
5336 /*
5337 * Return an SCB resource to the free list.
5338 */
5339 void
5340 ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
5341 {
5342
5343 /* Clean up for the next user */
5344 scb->flags = SCB_FLAG_NONE;
5345 scb->hscb->control = 0;
5346 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
5347
5348 if (scb->col_scb == NULL) {
5349
5350 /*
5351 * No collision possible. Just free normally.
5352 */
5353 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5354 scb, links.le);
5355 } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
5356
5357 /*
5358 * The SCB we might have collided with is on
5359 * a free collision list. Put both SCBs on
5360 * the generic list.
5361 */
5362 ahd_rem_col_list(ahd, scb->col_scb);
5363 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5364 scb, links.le);
5365 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5366 scb->col_scb, links.le);
5367 } else if ((scb->col_scb->flags
5368 & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
5369 && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
5370
5371 /*
5372 * The SCB we might collide with on the next allocation
5373 * is still active in a non-packetized, tagged, context.
5374 * Put us on the SCB collision list.
5375 */
5376 ahd_add_col_list(ahd, scb,
5377 AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
5378 } else {
5379 /*
5380 * The SCB we might collide with on the next allocation
5381 * is either active in a packetized context, or free.
5382 * Since we can't collide, put this SCB on the generic
5383 * free list.
5384 */
5385 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5386 scb, links.le);
5387 }
5388
5389 ahd_platform_scb_free(ahd, scb);
5390 }
5391
5392 void
5393 ahd_alloc_scbs(struct ahd_softc *ahd)
5394 {
5395 struct scb_data *scb_data;
5396 struct scb *next_scb;
5397 struct hardware_scb *hscb;
5398 struct map_node *hscb_map;
5399 struct map_node *sg_map;
5400 struct map_node *sense_map;
5401 uint8_t *segs;
5402 uint8_t *sense_data;
5403 bus_addr_t hscb_busaddr;
5404 bus_addr_t sg_busaddr;
5405 bus_addr_t sense_busaddr;
5406 int newcount;
5407 int i;
5408
5409 scb_data = &ahd->scb_data;
5410 if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
5411 /* Can't allocate any more */
5412 return;
5413
5414 KASSERT(scb_data->scbs_left >= 0);
5415 if (scb_data->scbs_left != 0) {
5416 int offset;
5417
5418 offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
5419 hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
5420 hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
5421 hscb_busaddr = hscb_map->physaddr + (offset * sizeof(*hscb));
5422 } else {
5423 hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT);
5424
5425 if (hscb_map == NULL)
5426 return;
5427
5428 memset(hscb_map, 0, sizeof(*hscb_map));
5429
5430 /* Allocate the next batch of hardware SCBs */
5431 if (ahd_createdmamem(ahd->parent_dmat, PAGE_SIZE, ahd->sc_dmaflags,
5432 &hscb_map->dmamap, (caddr_t *)&hscb_map->vaddr,
5433 &hscb_map->physaddr, &hscb_map->dmasegs,
5434 &hscb_map->nseg, ahd_name(ahd),
5435 "hardware SCB structures") < 0) {
5436 free(hscb_map, M_DEVBUF);
5437 return;
5438 }
5439
5440 SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
5441
5442 hscb = (struct hardware_scb *)hscb_map->vaddr;
5443 hscb_busaddr = hscb_map->physaddr;
5444 scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
5445 }
5446
5447 scb_data->init_level++;
5448
5449 if (scb_data->sgs_left != 0) {
5450 int offset;
5451
5452 offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
5453 - scb_data->sgs_left) * ahd_sglist_size(ahd);
5454 sg_map = SLIST_FIRST(&scb_data->sg_maps);
5455 segs = sg_map->vaddr + offset;
5456 sg_busaddr = sg_map->physaddr + offset;
5457 } else {
5458 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
5459
5460 if (sg_map == NULL)
5461 return;
5462
5463 bzero(sg_map, sizeof(*sg_map));
5464
5465 /* Allocate the next batch of S/G lists */
5466 if (ahd_createdmamem(ahd->parent_dmat, ahd_sglist_allocsize(ahd), ahd->sc_dmaflags,
5467 &sg_map->dmamap, (caddr_t *)&sg_map->vaddr,
5468 &sg_map->physaddr, &sg_map->dmasegs,
5469 &sg_map->nseg, ahd_name(ahd),
5470 "SG data structures") < 0) {
5471 free(sg_map, M_DEVBUF);
5472 return;
5473 }
5474
5475 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
5476
5477 segs = sg_map->vaddr;
5478 sg_busaddr = sg_map->physaddr;
5479 scb_data->sgs_left =
5480 ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
5481 #ifdef AHD_DEBUG
5482 if (ahd_debug & AHD_SHOW_MEMORY)
5483 printf("%s: ahd_alloc_scbs - Mapped SG data\n", ahd_name(ahd));
5484 #endif
5485 }
5486
5487 scb_data->init_level++;
5488
5489
5490 if (scb_data->sense_left != 0) {
5491 int offset;
5492
5493 offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
5494 sense_map = SLIST_FIRST(&scb_data->sense_maps);
5495 sense_data = sense_map->vaddr + offset;
5496 sense_busaddr = sense_map->physaddr + offset;
5497 } else {
5498 sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT);
5499
5500 if (sense_map == NULL)
5501 return;
5502
5503 bzero(sense_map, sizeof(*sense_map));
5504
5505 /* Allocate the next batch of sense buffers */
5506 if (ahd_createdmamem(ahd->parent_dmat, PAGE_SIZE, ahd->sc_dmaflags,
5507 &sense_map->dmamap, (caddr_t *)&sense_map->vaddr,
5508 &sense_map->physaddr, &sense_map->dmasegs,
5509 &sense_map->nseg, ahd_name(ahd),
5510 "Sense Data structures") < 0) {
5511 free(sense_map, M_DEVBUF);
5512 return;
5513 }
5514
5515 SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
5516
5517 sense_data = sense_map->vaddr;
5518 sense_busaddr = sense_map->physaddr;
5519 scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
5520 #ifdef AHD_DEBUG
5521 if (ahd_debug & AHD_SHOW_MEMORY)
5522 printf("%s: ahd_alloc_scbs - Mapped sense data\n", ahd_name(ahd));
5523 #endif
5524 }
5525
5526 scb_data->init_level++;
5527
5528 newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
5529 newcount = MIN(newcount, scb_data->sgs_left);
5530 newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
5531 scb_data->sense_left -= newcount;
5532 scb_data->scbs_left -= newcount;
5533 scb_data->sgs_left -= newcount;
5534
5535 for (i = 0; i < newcount; i++) {
5536 u_int col_tag;
5537
5538 struct scb_platform_data *pdata;
5539 #ifndef __linux__
5540 int error;
5541 #endif
5542 next_scb = (struct scb *)malloc(sizeof(*next_scb),
5543 M_DEVBUF, M_NOWAIT);
5544 if (next_scb == NULL)
5545 break;
5546
5547 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
5548 M_DEVBUF, M_NOWAIT);
5549 if (pdata == NULL) {
5550 free(next_scb, M_DEVBUF);
5551 break;
5552 }
5553 next_scb->platform_data = pdata;
5554 next_scb->hscb_map = hscb_map;
5555 next_scb->sg_map = sg_map;
5556 next_scb->sense_map = sense_map;
5557 next_scb->sg_list = segs;
5558 next_scb->sense_data = sense_data;
5559 next_scb->sense_busaddr = sense_busaddr;
5560 memset(hscb, 0, sizeof(*hscb));
5561 next_scb->hscb = hscb;
5562 hscb->hscb_busaddr = ahd_htole32(hscb_busaddr);
5563 KASSERT((vaddr_t)hscb >= (vaddr_t)hscb_map->vaddr &&
5564 (vaddr_t)hscb < (vaddr_t)hscb_map->vaddr + PAGE_SIZE);
5565
5566 /*
5567 * The sequencer always starts with the second entry.
5568 * The first entry is embedded in the scb.
5569 */
5570 next_scb->sg_list_busaddr = sg_busaddr;
5571 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
5572 next_scb->sg_list_busaddr
5573 += sizeof(struct ahd_dma64_seg);
5574 else
5575 next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
5576 next_scb->ahd_softc = ahd;
5577 next_scb->flags = SCB_FLAG_NONE;
5578
5579 error = bus_dmamap_create(ahd->parent_dmat,
5580 AHD_MAXTRANSFER_SIZE, AHD_NSEG, MAXBSIZE, 0,
5581 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW|ahd->sc_dmaflags,
5582 &next_scb->dmamap);
5583 if (error != 0) {
5584 free(next_scb, M_DEVBUF);
5585 free(pdata, M_DEVBUF);
5586 break;
5587 }
5588 next_scb->hscb->tag = ahd_htole16(scb_data->numscbs);
5589 col_tag = scb_data->numscbs ^ 0x100;
5590 next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
5591 if (next_scb->col_scb != NULL)
5592 next_scb->col_scb->col_scb = next_scb;
5593 ahd_free_scb(ahd, next_scb);
5594 hscb++;
5595 hscb_busaddr += sizeof(*hscb);
5596 segs += ahd_sglist_size(ahd);
5597 sg_busaddr += ahd_sglist_size(ahd);
5598 sense_data += AHD_SENSE_BUFSIZE;
5599 sense_busaddr += AHD_SENSE_BUFSIZE;
5600 scb_data->numscbs++;
5601 }
5602 }
5603
5604 void
5605 ahd_controller_info(struct ahd_softc *ahd, char *buf)
5606 {
5607 const char *speed;
5608 const char *type;
5609 int len;
5610
5611 len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
5612 buf += len;
5613
5614 speed = "Ultra320 ";
5615 if ((ahd->features & AHD_WIDE) != 0) {
5616 type = "Wide ";
5617 } else {
5618 type = "Single ";
5619 }
5620 len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
5621 speed, type, ahd->channel, ahd->our_id);
5622 buf += len;
5623
5624 sprintf(buf, "%s, %d SCBs", ahd->bus_description,
5625 ahd->scb_data.maxhscbs);
5626 }
5627
5628 static const char *channel_strings[] = {
5629 "Primary Low",
5630 "Primary High",
5631 "Secondary Low",
5632 "Secondary High"
5633 };
5634
5635 static const char *termstat_strings[] = {
5636 "Terminated Correctly",
5637 "Over Terminated",
5638 "Under Terminated",
5639 "Not Configured"
5640 };
5641
5642 /*
5643 * Start the board, ready for normal operation
5644 */
5645 int
5646 ahd_init(struct ahd_softc *ahd)
5647 {
5648 uint8_t *next_vaddr;
5649 bus_addr_t next_baddr;
5650 size_t driver_data_size;
5651 int i;
5652 int error;
5653 u_int warn_user;
5654 uint8_t current_sensing;
5655 uint8_t fstat;
5656
5657 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
5658
5659 ahd->stack_size = ahd_probe_stack_size(ahd);
5660 ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t),
5661 M_DEVBUF, M_NOWAIT);
5662 if (ahd->saved_stack == NULL)
5663 return (ENOMEM);
5664 /* Zero the memory */
5665 memset(ahd->saved_stack, 0, ahd->stack_size * sizeof(uint16_t));
5666
5667 /*
5668 * Verify that the compiler hasn't over-agressively
5669 * padded important structures.
5670 */
5671 if (sizeof(struct hardware_scb) != 64)
5672 panic("Hardware SCB size is incorrect");
5673
5674 #ifdef AHD_DEBUG
5675 if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
5676 ahd->flags |= AHD_SEQUENCER_DEBUG;
5677 #endif
5678
5679 /*
5680 * Default to allowing initiator operations.
5681 */
5682 ahd->flags |= AHD_INITIATORROLE;
5683
5684 /*
5685 * Only allow target mode features if this unit has them enabled.
5686 */
5687 if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
5688 ahd->features &= ~AHD_TARGETMODE;
5689
5690 /*
5691 * DMA tag for our command fifos and other data in system memory
5692 * the card's sequencer must be able to access. For initiator
5693 * roles, we need to allocate space for the qoutfifo. When providing
5694 * for the target mode role, we must additionally provide space for
5695 * the incoming target command fifo.
5696 */
5697 driver_data_size = AHD_SCB_MAX * sizeof(uint16_t)
5698 + sizeof(struct hardware_scb);
5699 if ((ahd->features & AHD_TARGETMODE) != 0)
5700 driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
5701 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
5702 driver_data_size += PKT_OVERRUN_BUFSIZE;
5703 ahd->shared_data_size = driver_data_size;
5704
5705 memset(&ahd->shared_data_map, 0, sizeof(ahd->shared_data_map));
5706 ahd->sc_dmaflags = BUS_DMA_NOWAIT;
5707
5708 if (ahd_createdmamem(ahd->parent_dmat, ahd->shared_data_size,
5709 ahd->sc_dmaflags,
5710 &ahd->shared_data_map.dmamap, (caddr_t *)&ahd->shared_data_map.vaddr,
5711 &ahd->shared_data_map.physaddr, &ahd->shared_data_map.dmasegs,
5712 &ahd->shared_data_map.nseg, ahd_name(ahd), "shared data") < 0)
5713 return (ENOMEM);
5714 ahd->qoutfifo = (void *) ahd->shared_data_map.vaddr;
5715
5716 ahd->init_level++;
5717
5718 next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
5719 next_baddr = ahd->shared_data_map.physaddr + AHD_QOUT_SIZE*sizeof(uint16_t);
5720 if ((ahd->features & AHD_TARGETMODE) != 0) {
5721 ahd->targetcmds = (struct target_cmd *)next_vaddr;
5722 next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
5723 next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
5724 }
5725
5726 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
5727 ahd->overrun_buf = next_vaddr;
5728 next_vaddr += PKT_OVERRUN_BUFSIZE;
5729 next_baddr += PKT_OVERRUN_BUFSIZE;
5730 }
5731
5732 /*
5733 * We need one SCB to serve as the "next SCB". Since the
5734 * tag identifier in this SCB will never be used, there is
5735 * no point in using a valid HSCB tag from an SCB pulled from
5736 * the standard free pool. So, we allocate this "sentinel"
5737 * specially from the DMA safe memory chunk used for the QOUTFIFO.
5738 */
5739 ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
5740 ahd->next_queued_hscb_map = &ahd->shared_data_map;
5741 ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
5742
5743 memset(&ahd->scb_data, 0, sizeof(struct scb_data));
5744
5745 /* Allocate SCB data now that parent_dmat is initialized */
5746 if (ahd_init_scbdata(ahd) != 0)
5747 return (ENOMEM);
5748
5749 if ((ahd->flags & AHD_INITIATORROLE) == 0)
5750 ahd->flags &= ~AHD_RESET_BUS_A;
5751
5752 /*
5753 * Before committing these settings to the chip, give
5754 * the OSM one last chance to modify our configuration.
5755 */
5756 ahd_platform_init(ahd);
5757
5758 /* Bring up the chip. */
5759 ahd_chip_init(ahd);
5760
5761 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
5762
5763 if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
5764 goto init_done;
5765
5766 /*
5767 * Verify termination based on current draw and
5768 * warn user if the bus is over/under terminated.
5769 */
5770 error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
5771 CURSENSE_ENB);
5772 if (error != 0) {
5773 printf("%s: current sensing timeout 1\n", ahd_name(ahd));
5774 goto init_done;
5775 }
5776 for (i = 20, fstat = FLX_FSTAT_BUSY;
5777 (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
5778 error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
5779 if (error != 0) {
5780 printf("%s: current sensing timeout 2\n",
5781 ahd_name(ahd));
5782 goto init_done;
5783 }
5784 }
5785 if (i == 0) {
5786 printf("%s: Timedout during current-sensing test\n",
5787 ahd_name(ahd));
5788 goto init_done;
5789 }
5790
5791 /* Latch Current Sensing status. */
5792 error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, ¤t_sensing);
5793 if (error != 0) {
5794 printf("%s: current sensing timeout 3\n", ahd_name(ahd));
5795 goto init_done;
5796 }
5797
5798 /* Diable current sensing. */
5799 ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
5800
5801 #ifdef AHD_DEBUG
5802 if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
5803 printf("%s: current_sensing == 0x%x\n",
5804 ahd_name(ahd), current_sensing);
5805 }
5806 #endif
5807 warn_user = 0;
5808 for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
5809 u_int term_stat;
5810
5811 term_stat = (current_sensing & FLX_CSTAT_MASK);
5812 switch (term_stat) {
5813 case FLX_CSTAT_OVER:
5814 case FLX_CSTAT_UNDER:
5815 warn_user++;
5816 case FLX_CSTAT_INVALID:
5817 case FLX_CSTAT_OKAY:
5818 if (warn_user == 0 && bootverbose == 0)
5819 break;
5820 printf("%s: %s Channel %s\n", ahd_name(ahd),
5821 channel_strings[i], termstat_strings[term_stat]);
5822 break;
5823 }
5824 }
5825 if (warn_user) {
5826 printf("%s: WARNING. Termination is not configured correctly.\n"
5827 "%s: WARNING. SCSI bus operations may FAIL.\n",
5828 ahd_name(ahd), ahd_name(ahd));
5829 }
5830 init_done:
5831 ahd_reset_current_bus(ahd);
5832 ahd_restart(ahd);
5833 ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
5834 ahd_stat_timer, ahd);
5835
5836 /* We have to wait until after any system dumps... */
5837 ahd->shutdown_hook = shutdownhook_establish(ahd_shutdown, ahd);
5838
5839 return (0);
5840 }
5841
5842 /*
5843 * (Re)initialize chip state after a chip reset.
5844 */
5845 static void
5846 ahd_chip_init(struct ahd_softc *ahd)
5847 {
5848 uint32_t busaddr;
5849 u_int sxfrctl1;
5850 u_int scsiseq_template;
5851 u_int wait;
5852 u_int i;
5853 u_int target;
5854
5855 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5856 /*
5857 * Take the LED out of diagnostic mode
5858 */
5859 ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
5860
5861 /*
5862 * Return HS_MAILBOX to its default value.
5863 */
5864 ahd->hs_mailbox = 0;
5865 ahd_outb(ahd, HS_MAILBOX, 0);
5866
5867 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
5868 ahd_outb(ahd, IOWNID, ahd->our_id);
5869 ahd_outb(ahd, TOWNID, ahd->our_id);
5870 sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
5871 sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
5872 if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
5873 && (ahd->seltime != STIMESEL_MIN)) {
5874 /*
5875 * The selection timer duration is twice as long
5876 * as it should be. Halve it by adding "1" to
5877 * the user specified setting.
5878 */
5879 sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
5880 } else {
5881 sxfrctl1 |= ahd->seltime;
5882 }
5883
5884 ahd_outb(ahd, SXFRCTL0, DFON);
5885 ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
5886 ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
5887
5888 /*
5889 * Now that termination is set, wait for up
5890 * to 500ms for our transceivers to settle. If
5891 * the adapter does not have a cable attached,
5892 * the transceivers may never settle, so don't
5893 * complain if we fail here.
5894 */
5895 for (wait = 10000;
5896 (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
5897 wait--)
5898 ahd_delay(100);
5899
5900 /* Clear any false bus resets due to the transceivers settling */
5901 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
5902 ahd_outb(ahd, CLRINT, CLRSCSIINT);
5903
5904 /* Initialize mode specific S/G state. */
5905 for (i = 0; i < 2; i++) {
5906 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
5907 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
5908 ahd_outb(ahd, SG_STATE, 0);
5909 ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
5910 ahd_outb(ahd, SEQIMODE,
5911 ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
5912 |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
5913 }
5914
5915 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5916 ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
5917 ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
5918 ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
5919 ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
5920 if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
5921 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
5922 } else {
5923 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
5924 }
5925 ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
5926 if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
5927 /*
5928 * Do not issue a target abort when a split completion
5929 * error occurs. Let our PCIX interrupt handler deal
5930 * with it instead. H2A4 Razor #625
5931 */
5932 ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
5933
5934 if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
5935 ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
5936
5937 /*
5938 * Tweak IOCELL settings.
5939 */
5940 if ((ahd->flags & AHD_HP_BOARD) != 0) {
5941 for (i = 0; i < NUMDSPS; i++) {
5942 ahd_outb(ahd, DSPSELECT, i);
5943 ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
5944 }
5945 #ifdef AHD_DEBUG
5946 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5947 printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
5948 WRTBIASCTL_HP_DEFAULT);
5949 #endif
5950 }
5951 ahd_setup_iocell_workaround(ahd);
5952
5953 /*
5954 * Enable LQI Manager interrupts.
5955 */
5956 ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
5957 | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
5958 | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
5959 ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
5960 /*
5961 * An interrupt from LQOBUSFREE is made redundant by the
5962 * BUSFREE interrupt. We choose to have the sequencer catch
5963 * LQOPHCHGINPKT errors manually for the command phase at the
5964 * start of a packetized selection case.
5965 ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE|ENLQOPHACHGINPKT);
5966 */
5967 ahd_outb(ahd, LQOMODE1, 0);
5968
5969 /*
5970 * Setup sequencer interrupt handlers.
5971 */
5972 ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
5973 ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
5974
5975 /*
5976 * Setup SCB Offset registers.
5977 */
5978 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
5979 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
5980 pkt_long_lun));
5981 } else {
5982 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
5983 }
5984 ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
5985 ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
5986 ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
5987 ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
5988 shared_data.idata.cdb));
5989 ahd_outb(ahd, QNEXTPTR,
5990 offsetof(struct hardware_scb, next_hscb_busaddr));
5991 ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
5992 ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
5993 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
5994 ahd_outb(ahd, LUNLEN,
5995 sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
5996 } else {
5997 ahd_outb(ahd, LUNLEN, LUNLEN_SINGLE_LEVEL_LUN);
5998 }
5999 ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
6000 ahd_outb(ahd, MAXCMD, 0xFF);
6001 ahd_outb(ahd, SCBAUTOPTR,
6002 AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
6003
6004 /* We haven't been enabled for target mode yet. */
6005 ahd_outb(ahd, MULTARGID, 0);
6006 ahd_outb(ahd, MULTARGID + 1, 0);
6007
6008 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6009 /* Initialize the negotiation table. */
6010 if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
6011 /*
6012 * Clear the spare bytes in the neg table to avoid
6013 * spurious parity errors.
6014 */
6015 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6016 ahd_outb(ahd, NEGOADDR, target);
6017 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
6018 for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
6019 ahd_outb(ahd, ANNEXDAT, 0);
6020 }
6021 }
6022
6023 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6024 struct ahd_devinfo devinfo;
6025 struct ahd_initiator_tinfo *tinfo;
6026 struct ahd_tmode_tstate *tstate;
6027
6028 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6029 target, &tstate);
6030 ahd_compile_devinfo(&devinfo, ahd->our_id,
6031 target, CAM_LUN_WILDCARD,
6032 'A', ROLE_INITIATOR);
6033 ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
6034 }
6035
6036 ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
6037 ahd_outb(ahd, CLRINT, CLRSCSIINT);
6038
6039 /*
6040 * Always enable abort on incoming L_Qs if this feature is
6041 * supported. We use this to catch invalid SCB references.
6042 */
6043 if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
6044 ahd_outb(ahd, LQCTL1, ABORTPENDING);
6045 else
6046 ahd_outb(ahd, LQCTL1, 0);
6047
6048 /* All of our queues are empty */
6049 ahd->qoutfifonext = 0;
6050 ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID_LE;
6051 ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID >> 8);
6052 for (i = 0; i < AHD_QOUT_SIZE; i++)
6053 ahd->qoutfifo[i] = 0;
6054 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
6055
6056 ahd->qinfifonext = 0;
6057 for (i = 0; i < AHD_QIN_SIZE; i++)
6058 ahd->qinfifo[i] = SCB_LIST_NULL;
6059
6060 if ((ahd->features & AHD_TARGETMODE) != 0) {
6061 /* All target command blocks start out invalid. */
6062 for (i = 0; i < AHD_TMODE_CMDS; i++)
6063 ahd->targetcmds[i].cmd_valid = 0;
6064 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
6065 ahd->tqinfifonext = 1;
6066 ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
6067 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
6068 }
6069
6070 /* Initialize Scratch Ram. */
6071 ahd_outb(ahd, SEQ_FLAGS, 0);
6072 ahd_outb(ahd, SEQ_FLAGS2, 0);
6073
6074 /* We don't have any waiting selections */
6075 ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
6076 ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
6077 for (i = 0; i < AHD_NUM_TARGETS; i++) {
6078 ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
6079 }
6080
6081 /*
6082 * Nobody is waiting to be DMAed into the QOUTFIFO.
6083 */
6084 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
6085 ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
6086 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
6087
6088 /*
6089 * The Freeze Count is 0.
6090 */
6091 ahd_outw(ahd, QFREEZE_COUNT, 0);
6092
6093 /*
6094 * Tell the sequencer where it can find our arrays in memory.
6095 */
6096 busaddr = ahd->shared_data_map.physaddr;
6097 ahd_outb(ahd, SHARED_DATA_ADDR, busaddr & 0xFF);
6098 ahd_outb(ahd, SHARED_DATA_ADDR + 1, (busaddr >> 8) & 0xFF);
6099 ahd_outb(ahd, SHARED_DATA_ADDR + 2, (busaddr >> 16) & 0xFF);
6100 ahd_outb(ahd, SHARED_DATA_ADDR + 3, (busaddr >> 24) & 0xFF);
6101 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR, busaddr & 0xFF);
6102 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 1, (busaddr >> 8) & 0xFF);
6103 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 2, (busaddr >> 16) & 0xFF);
6104 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 3, (busaddr >> 24) & 0xFF);
6105 /*
6106 * Setup the allowed SCSI Sequences based on operational mode.
6107 * If we are a target, we'll enable select in operations once
6108 * we've had a lun enabled.
6109 */
6110 scsiseq_template = ENAUTOATNP;
6111 if ((ahd->flags & AHD_INITIATORROLE) != 0)
6112 scsiseq_template |= ENRSELI;
6113 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
6114
6115 /* There are no busy SCBs yet. */
6116 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6117 int lun;
6118
6119 for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
6120 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
6121 }
6122
6123 /*
6124 * Initialize the group code to command length table.
6125 * Vendor Unique codes are set to 0 so we only capture
6126 * the first byte of the cdb. These can be overridden
6127 * when target mode is enabled.
6128 */
6129 ahd_outb(ahd, CMDSIZE_TABLE, 5);
6130 ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
6131 ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
6132 ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
6133 ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
6134 ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
6135 ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
6136 ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
6137
6138 /* Tell the sequencer of our initial queue positions */
6139 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6140 ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
6141 ahd->qinfifonext = 0;
6142 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6143 ahd_set_hescb_qoff(ahd, 0);
6144 ahd_set_snscb_qoff(ahd, 0);
6145 ahd_set_sescb_qoff(ahd, 0);
6146 ahd_set_sdscb_qoff(ahd, 0);
6147
6148 /*
6149 * Tell the sequencer which SCB will be the next one it receives.
6150 */
6151 busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
6152 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
6153 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
6154 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
6155 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
6156
6157 /*
6158 * Default to coalescing disabled.
6159 */
6160 ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
6161 ahd_outw(ahd, CMDS_PENDING, 0);
6162 ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
6163 ahd->int_coalescing_maxcmds,
6164 ahd->int_coalescing_mincmds);
6165 ahd_enable_coalescing(ahd, FALSE);
6166
6167 ahd_loadseq(ahd);
6168 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6169 }
6170
6171 /*
6172 * Setup default device and controller settings.
6173 * This should only be called if our probe has
6174 * determined that no configuration data is available.
6175 */
6176 int
6177 ahd_default_config(struct ahd_softc *ahd)
6178 {
6179 int targ;
6180
6181 ahd->our_id = 7;
6182
6183 /*
6184 * Allocate a tstate to house information for our
6185 * initiator presence on the bus as well as the user
6186 * data for any target mode initiator.
6187 */
6188 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
6189 printf("%s: unable to allocate ahd_tmode_tstate. "
6190 "Failing attach\n", ahd_name(ahd));
6191 return (ENOMEM);
6192 }
6193
6194 for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
6195 struct ahd_devinfo devinfo;
6196 struct ahd_initiator_tinfo *tinfo;
6197 struct ahd_tmode_tstate *tstate;
6198 uint16_t target_mask;
6199
6200 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6201 targ, &tstate);
6202 /*
6203 * We support SPC2 and SPI4.
6204 */
6205 tinfo->user.protocol_version = 4;
6206 tinfo->user.transport_version = 4;
6207
6208 target_mask = 0x01 << targ;
6209 ahd->user_discenable |= target_mask;
6210 tstate->discenable |= target_mask;
6211 ahd->user_tagenable |= target_mask;
6212 #ifdef AHD_FORCE_160
6213 tinfo->user.period = AHD_SYNCRATE_DT;
6214 #else
6215 tinfo->user.period = AHD_SYNCRATE_160;
6216 #endif
6217 tinfo->user.offset= MAX_OFFSET;
6218 tinfo->user.ppr_options = MSG_EXT_PPR_RDSTRM
6219 | MSG_EXT_PPR_WRFLOW
6220 | MSG_EXT_PPR_HOLDMCS
6221 | MSG_EXT_PPR_IU_REQ
6222 | MSG_EXT_PPR_QAS_REQ
6223 | MSG_EXT_PPR_DT_REQ;
6224 if ((ahd->features & AHD_RTI) != 0)
6225 tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
6226
6227 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
6228
6229 /*
6230 * Start out Async/Narrow/Untagged and with
6231 * conservative protocol support.
6232 */
6233 tinfo->goal.protocol_version = 2;
6234 tinfo->goal.transport_version = 2;
6235 tinfo->curr.protocol_version = 2;
6236 tinfo->curr.transport_version = 2;
6237 ahd_compile_devinfo(&devinfo, ahd->our_id,
6238 targ, CAM_LUN_WILDCARD,
6239 'A', ROLE_INITIATOR);
6240 tstate->tagenable &= ~target_mask;
6241 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6242 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
6243 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
6244 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
6245 /*paused*/TRUE);
6246 }
6247 return (0);
6248 }
6249
6250 /*
6251 * Parse device configuration information.
6252 */
6253 int
6254 ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
6255 {
6256 int targ;
6257 int max_targ;
6258
6259 max_targ = sc->max_targets & CFMAXTARG;
6260 ahd->our_id = sc->brtime_id & CFSCSIID;
6261
6262 /*
6263 * Allocate a tstate to house information for our
6264 * initiator presence on the bus as well as the user
6265 * data for any target mode initiator.
6266 */
6267 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
6268 printf("%s: unable to allocate ahd_tmode_tstate. "
6269 "Failing attach\n", ahd_name(ahd));
6270 return (ENOMEM);
6271 }
6272
6273 for (targ = 0; targ < max_targ; targ++) {
6274 struct ahd_devinfo devinfo;
6275 struct ahd_initiator_tinfo *tinfo;
6276 struct ahd_transinfo *user_tinfo;
6277 struct ahd_tmode_tstate *tstate;
6278 uint16_t target_mask;
6279
6280 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6281 targ, &tstate);
6282 user_tinfo = &tinfo->user;
6283
6284 /*
6285 * We support SPC2 and SPI4.
6286 */
6287 tinfo->user.protocol_version = 4;
6288 tinfo->user.transport_version = 4;
6289
6290 target_mask = 0x01 << targ;
6291 ahd->user_discenable &= ~target_mask;
6292 tstate->discenable &= ~target_mask;
6293 ahd->user_tagenable &= ~target_mask;
6294 if (sc->device_flags[targ] & CFDISC) {
6295 tstate->discenable |= target_mask;
6296 ahd->user_discenable |= target_mask;
6297 ahd->user_tagenable |= target_mask;
6298 } else {
6299 /*
6300 * Cannot be packetized without disconnection.
6301 */
6302 sc->device_flags[targ] &= ~CFPACKETIZED;
6303 }
6304
6305 user_tinfo->ppr_options = 0;
6306 user_tinfo->period = (sc->device_flags[targ] & CFXFER);
6307 if (user_tinfo->period < CFXFER_ASYNC) {
6308 if (user_tinfo->period <= AHD_PERIOD_10MHz)
6309 user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
6310 user_tinfo->offset = MAX_OFFSET;
6311 } else {
6312 user_tinfo->offset = 0;
6313 user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
6314 }
6315 #ifdef AHD_FORCE_160
6316 if (user_tinfo->period <= AHD_SYNCRATE_160)
6317 user_tinfo->period = AHD_SYNCRATE_DT;
6318 #endif
6319
6320 if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
6321 user_tinfo->ppr_options |= MSG_EXT_PPR_RDSTRM
6322 | MSG_EXT_PPR_WRFLOW
6323 | MSG_EXT_PPR_HOLDMCS
6324 | MSG_EXT_PPR_IU_REQ;
6325 if ((ahd->features & AHD_RTI) != 0)
6326 user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
6327 }
6328
6329 if ((sc->device_flags[targ] & CFQAS) != 0)
6330 user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
6331
6332 if ((sc->device_flags[targ] & CFWIDEB) != 0)
6333 user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
6334 else
6335 user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
6336 #ifdef AHD_DEBUG
6337 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6338 printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
6339 user_tinfo->period, user_tinfo->offset,
6340 user_tinfo->ppr_options);
6341 #endif
6342 /*
6343 * Start out Async/Narrow/Untagged and with
6344 * conservative protocol support.
6345 */
6346 tstate->tagenable &= ~target_mask;
6347 tinfo->goal.protocol_version = 2;
6348 tinfo->goal.transport_version = 2;
6349 tinfo->curr.protocol_version = 2;
6350 tinfo->curr.transport_version = 2;
6351 ahd_compile_devinfo(&devinfo, ahd->our_id,
6352 targ, CAM_LUN_WILDCARD,
6353 'A', ROLE_INITIATOR);
6354 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6355 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
6356 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
6357 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
6358 /*paused*/TRUE);
6359 }
6360
6361 ahd->flags &= ~AHD_SPCHK_ENB_A;
6362 if (sc->bios_control & CFSPARITY)
6363 ahd->flags |= AHD_SPCHK_ENB_A;
6364
6365 ahd->flags &= ~AHD_RESET_BUS_A;
6366 if (sc->bios_control & CFRESETB)
6367 ahd->flags |= AHD_RESET_BUS_A;
6368
6369 ahd->flags &= ~AHD_EXTENDED_TRANS_A;
6370 if (sc->bios_control & CFEXTEND)
6371 ahd->flags |= AHD_EXTENDED_TRANS_A;
6372
6373 ahd->flags &= ~AHD_BIOS_ENABLED;
6374 if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
6375 ahd->flags |= AHD_BIOS_ENABLED;
6376
6377 ahd->flags &= ~AHD_STPWLEVEL_A;
6378 if ((sc->adapter_control & CFSTPWLEVEL) != 0)
6379 ahd->flags |= AHD_STPWLEVEL_A;
6380
6381 return (0);
6382 }
6383
6384 /*
6385 * Parse device configuration information.
6386 */
6387 int
6388 ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
6389 {
6390 int error;
6391
6392 error = ahd_verify_vpd_cksum(vpd);
6393 if (error == 0)
6394 return (EINVAL);
6395 if ((vpd->bios_flags & VPDBOOTHOST) != 0)
6396 ahd->flags |= AHD_BOOT_CHANNEL;
6397 return (0);
6398 }
6399
6400 void
6401 ahd_intr_enable(struct ahd_softc *ahd, int enable)
6402 {
6403 u_int hcntrl;
6404
6405 hcntrl = ahd_inb(ahd, HCNTRL);
6406 hcntrl &= ~INTEN;
6407 ahd->pause &= ~INTEN;
6408 ahd->unpause &= ~INTEN;
6409 if (enable) {
6410 hcntrl |= INTEN;
6411 ahd->pause |= INTEN;
6412 ahd->unpause |= INTEN;
6413 }
6414 ahd_outb(ahd, HCNTRL, hcntrl);
6415 }
6416
6417 void
6418 ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
6419 u_int mincmds)
6420 {
6421 if (timer > AHD_TIMER_MAX_US)
6422 timer = AHD_TIMER_MAX_US;
6423 ahd->int_coalescing_timer = timer;
6424
6425 if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
6426 maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
6427 if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
6428 mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
6429 ahd->int_coalescing_maxcmds = maxcmds;
6430 ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
6431 ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
6432 ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
6433 }
6434
6435 void
6436 ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
6437 {
6438
6439 ahd->hs_mailbox &= ~ENINT_COALESCE;
6440 if (enable)
6441 ahd->hs_mailbox |= ENINT_COALESCE;
6442 ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
6443 ahd_flush_device_writes(ahd);
6444 ahd_run_qoutfifo(ahd);
6445 }
6446
6447 /*
6448 * Ensure that the card is paused in a location
6449 * outside of all critical sections and that all
6450 * pending work is completed prior to returning.
6451 * This routine should only be called from outside
6452 * an interrupt context.
6453 */
6454 void
6455 ahd_pause_and_flushwork(struct ahd_softc *ahd)
6456 {
6457 u_int intstat;
6458 u_int maxloops;
6459 u_int qfreeze_cnt;
6460
6461 maxloops = 1000;
6462 ahd->flags |= AHD_ALL_INTERRUPTS;
6463 ahd_pause(ahd);
6464 /*
6465 * Increment the QFreeze Count so that the sequencer
6466 * will not start new selections. We do this only
6467 * until we are safely paused without further selections
6468 * pending.
6469 */
6470 ahd_outw(ahd, QFREEZE_COUNT, ahd_inw(ahd, QFREEZE_COUNT) + 1);
6471 ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
6472 do {
6473 struct scb *waiting_scb;
6474
6475 ahd_unpause(ahd);
6476 ahd_intr(ahd);
6477 ahd_pause(ahd);
6478 ahd_clear_critical_section(ahd);
6479 intstat = ahd_inb(ahd, INTSTAT);
6480 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6481 if ((ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
6482 ahd_outb(ahd, SCSISEQ0,
6483 ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
6484 /*
6485 * In the non-packetized case, the sequencer (for Rev A),
6486 * relies on ENSELO remaining set after SELDO. The hardware
6487 * auto-clears ENSELO in the packetized case.
6488 */
6489 waiting_scb = ahd_lookup_scb(ahd,
6490 ahd_inw(ahd, WAITING_TID_HEAD));
6491 if (waiting_scb != NULL
6492 && (waiting_scb->flags & SCB_PACKETIZED) == 0
6493 && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0)
6494 ahd_outb(ahd, SCSISEQ0,
6495 ahd_inb(ahd, SCSISEQ0) | ENSELO);
6496 } while (--maxloops
6497 && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
6498 && ((intstat & INT_PEND) != 0
6499 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
6500 || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
6501 if (maxloops == 0) {
6502 printf("Infinite interrupt loop, INTSTAT = %x",
6503 ahd_inb(ahd, INTSTAT));
6504 }
6505 qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
6506 if (qfreeze_cnt == 0) {
6507 printf("%s: ahd_pause_and_flushwork with 0 qfreeze count!\n",
6508 ahd_name(ahd));
6509 } else {
6510 qfreeze_cnt--;
6511 }
6512 ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
6513 if (qfreeze_cnt == 0)
6514 ahd_outb(ahd, SEQ_FLAGS2,
6515 ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
6516
6517 ahd_flush_qoutfifo(ahd);
6518
6519 ahd_platform_flushwork(ahd);
6520 ahd->flags &= ~AHD_ALL_INTERRUPTS;
6521 }
6522
6523 int
6524 ahd_suspend(struct ahd_softc *ahd)
6525 {
6526
6527 ahd_pause_and_flushwork(ahd);
6528
6529 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
6530 ahd_unpause(ahd);
6531 return (EBUSY);
6532 }
6533 ahd_shutdown(ahd);
6534 return (0);
6535 }
6536
6537 int
6538 ahd_resume(struct ahd_softc *ahd)
6539 {
6540
6541 ahd_reset(ahd, /*reinit*/TRUE);
6542 ahd_intr_enable(ahd, TRUE);
6543 ahd_restart(ahd);
6544 return (0);
6545 }
6546
6547 /************************** Busy Target Table *********************************/
6548 /*
6549 * Set SCBPTR to the SCB that contains the busy
6550 * table entry for TCL. Return the offset into
6551 * the SCB that contains the entry for TCL.
6552 * saved_scbid is dereferenced and set to the
6553 * scbid that should be restored once manipualtion
6554 * of the TCL entry is complete.
6555 */
6556 static __inline u_int
6557 ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
6558 {
6559 /*
6560 * Index to the SCB that contains the busy entry.
6561 */
6562 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
6563 *saved_scbid = ahd_get_scbptr(ahd);
6564 ahd_set_scbptr(ahd, TCL_LUN(tcl)
6565 | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
6566
6567 /*
6568 * And now calculate the SCB offset to the entry.
6569 * Each entry is 2 bytes wide, hence the
6570 * multiplication by 2.
6571 */
6572 return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
6573 }
6574
6575 /*
6576 * Return the untagged transaction id for a given target/channel lun.
6577 */
6578 u_int
6579 ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
6580 {
6581 u_int scbid;
6582 u_int scb_offset;
6583 u_int saved_scbptr;
6584
6585 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
6586 scbid = ahd_inw_scbram(ahd, scb_offset);
6587 ahd_set_scbptr(ahd, saved_scbptr);
6588 return (scbid);
6589 }
6590
6591 void
6592 ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
6593 {
6594 u_int scb_offset;
6595 u_int saved_scbptr;
6596
6597 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
6598 ahd_outw(ahd, scb_offset, scbid);
6599 ahd_set_scbptr(ahd, saved_scbptr);
6600 }
6601
6602 /************************** SCB and SCB queue management **********************/
6603 int
6604 ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
6605 char channel, int lun, u_int tag, role_t role)
6606 {
6607 int targ = SCB_GET_TARGET(ahd, scb);
6608 char chan = SCB_GET_CHANNEL(ahd, scb);
6609 int slun = SCB_GET_LUN(scb);
6610 int match;
6611
6612 match = ((chan == channel) || (channel == ALL_CHANNELS));
6613 if (match != 0)
6614 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
6615 if (match != 0)
6616 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
6617 if (match != 0) {
6618 #if AHD_TARGET_MODE
6619 int group;
6620
6621 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
6622 if (role == ROLE_INITIATOR) {
6623 match = (group != XPT_FC_GROUP_TMODE)
6624 && ((tag == SCB_GET_TAG(scb))
6625 || (tag == SCB_LIST_NULL));
6626 } else if (role == ROLE_TARGET) {
6627 match = (group == XPT_FC_GROUP_TMODE)
6628 && ((tag == scb->io_ctx->csio.tag_id)
6629 || (tag == SCB_LIST_NULL));
6630 }
6631 #else /* !AHD_TARGET_MODE */
6632 match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
6633 #endif /* AHD_TARGET_MODE */
6634 }
6635
6636 return match;
6637 }
6638
6639 void
6640 ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
6641 {
6642 int target;
6643 char channel;
6644 int lun;
6645
6646 target = SCB_GET_TARGET(ahd, scb);
6647 lun = SCB_GET_LUN(scb);
6648 channel = SCB_GET_CHANNEL(ahd, scb);
6649
6650 ahd_search_qinfifo(ahd, target, channel, lun,
6651 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
6652 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
6653
6654 ahd_platform_freeze_devq(ahd, scb);
6655 }
6656
6657 void
6658 ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
6659 {
6660 struct scb *prev_scb;
6661 ahd_mode_state saved_modes;
6662
6663 saved_modes = ahd_save_modes(ahd);
6664 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6665 prev_scb = NULL;
6666 if (ahd_qinfifo_count(ahd) != 0) {
6667 u_int prev_tag;
6668 u_int prev_pos;
6669
6670 prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
6671 prev_tag = ahd->qinfifo[prev_pos];
6672 prev_scb = ahd_lookup_scb(ahd, prev_tag);
6673 }
6674 ahd_qinfifo_requeue(ahd, prev_scb, scb);
6675 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6676 ahd_restore_modes(ahd, saved_modes);
6677 }
6678
6679 static void
6680 ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
6681 struct scb *scb)
6682 {
6683 if (prev_scb == NULL) {
6684 uint32_t busaddr;
6685
6686 busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
6687 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
6688 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
6689 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
6690 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
6691 } else {
6692 prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
6693 ahd_sync_scb(ahd, prev_scb,
6694 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
6695 }
6696 ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
6697 ahd->qinfifonext++;
6698 scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
6699 ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
6700 }
6701
6702 static int
6703 ahd_qinfifo_count(struct ahd_softc *ahd)
6704 {
6705 u_int qinpos;
6706 u_int wrap_qinpos;
6707 u_int wrap_qinfifonext;
6708
6709 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
6710 qinpos = ahd_get_snscb_qoff(ahd);
6711 wrap_qinpos = AHD_QIN_WRAP(qinpos);
6712 wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
6713 if (wrap_qinfifonext >= wrap_qinpos)
6714 return (wrap_qinfifonext - wrap_qinpos);
6715 else
6716 return (wrap_qinfifonext
6717 + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos);
6718 }
6719
6720 void
6721 ahd_reset_cmds_pending(struct ahd_softc *ahd)
6722 {
6723 struct scb *scb;
6724 ahd_mode_state saved_modes;
6725 u_int pending_cmds;
6726
6727 saved_modes = ahd_save_modes(ahd);
6728 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6729
6730 /*
6731 * Don't count any commands as outstanding that the
6732 * sequencer has already marked for completion.
6733 */
6734 ahd_flush_qoutfifo(ahd);
6735
6736 pending_cmds = 0;
6737 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
6738 pending_cmds++;
6739 }
6740 ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
6741 ahd_restore_modes(ahd, saved_modes);
6742 ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
6743 }
6744
6745 int
6746 ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
6747 int lun, u_int tag, role_t role, uint32_t status,
6748 ahd_search_action action)
6749 {
6750 struct scb *scb;
6751 struct scb *prev_scb;
6752 ahd_mode_state saved_modes;
6753 u_int qinstart;
6754 u_int qinpos;
6755 u_int qintail;
6756 u_int tid_next;
6757 u_int tid_prev;
6758 u_int scbid;
6759 u_int savedscbptr;
6760 uint32_t busaddr;
6761 int found;
6762 int targets;
6763 int pending_cmds;
6764 int qincount;
6765
6766 /* Must be in CCHAN mode */
6767 saved_modes = ahd_save_modes(ahd);
6768 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6769
6770 /*
6771 * Halt any pending SCB DMA. The sequencer will reinitiate
6772 * this DMA if the qinfifo is not empty once we unpause.
6773 */
6774 if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
6775 == (CCARREN|CCSCBEN|CCSCBDIR)) {
6776 ahd_outb(ahd, CCSCBCTL,
6777 ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
6778 while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
6779 ;
6780 }
6781 /* Determine sequencer's position in the qinfifo. */
6782 qintail = AHD_QIN_WRAP(ahd->qinfifonext);
6783 qinstart = ahd_get_snscb_qoff(ahd);
6784 qinpos = AHD_QIN_WRAP(qinstart);
6785 found = 0;
6786 prev_scb = NULL;
6787
6788 pending_cmds = 0;
6789 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
6790 pending_cmds++;
6791 }
6792 qincount = ahd_qinfifo_count(ahd);
6793
6794 if (action == SEARCH_PRINT) {
6795 printf("qinstart = 0x%x qinfifonext = 0x%x\n",
6796 qinstart, ahd->qinfifonext);
6797 }
6798
6799 /*
6800 * Start with an empty queue. Entries that are not chosen
6801 * for removal will be re-added to the queue as we go.
6802 */
6803 ahd->qinfifonext = qinstart;
6804 busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
6805 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
6806 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
6807 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
6808 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
6809
6810 while (qinpos != qintail) {
6811 scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
6812 if (scb == NULL) {
6813 panic("Loop 1\n");
6814 }
6815
6816 if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
6817 /*
6818 * We found an scb that needs to be acted on.
6819 */
6820 found++;
6821 switch (action) {
6822 case SEARCH_COMPLETE:
6823 {
6824 cam_status ostat;
6825 cam_status cstat;
6826
6827 ostat = ahd_get_scsi_status(scb);
6828 if (ostat == CAM_REQ_INPROG)
6829 ahd_set_scsi_status(scb, status);
6830 cstat = ahd_get_transaction_status(scb);
6831 if (cstat != CAM_REQ_CMP)
6832 ahd_freeze_scb(scb);
6833 if ((scb->flags & SCB_ACTIVE) == 0)
6834 printf("Inactive SCB in qinfifo\n");
6835 if (scb->xs->error != CAM_REQ_CMP)
6836 printf("SEARCH_COMPLETE(0x%x): ostat 0x%x, cstat 0x%x, xs_error 0x%x\n",
6837 SCB_GET_TAG(scb), ostat, cstat, scb->xs->error);
6838 ahd_done(ahd, scb);
6839
6840 /* FALLTHROUGH */
6841 }
6842 case SEARCH_REMOVE:
6843 break;
6844 case SEARCH_PRINT:
6845 printf(" 0x%x", ahd->qinfifo[qinpos]);
6846 /* FALLTHROUGH */
6847 case SEARCH_COUNT:
6848 ahd_qinfifo_requeue(ahd, prev_scb, scb);
6849 prev_scb = scb;
6850 break;
6851 }
6852 } else {
6853 ahd_qinfifo_requeue(ahd, prev_scb, scb);
6854 prev_scb = scb;
6855 }
6856 qinpos = AHD_QIN_WRAP(qinpos+1);
6857 }
6858
6859 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6860
6861 if (action == SEARCH_PRINT)
6862 printf("\nWAITING_TID_QUEUES:\n");
6863
6864 /*
6865 * Search waiting for selection lists. We traverse the
6866 * list of "their ids" waiting for selection and, if
6867 * appropriate, traverse the SCBs of each "their id"
6868 * looking for matches.
6869 */
6870 savedscbptr = ahd_get_scbptr(ahd);
6871 tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
6872 tid_prev = SCB_LIST_NULL;
6873 targets = 0;
6874 for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
6875 u_int tid_head;
6876
6877 /*
6878 * We limit based on the number of SCBs since
6879 * MK_MESSAGE SCBs are not in the per-tid lists.
6880 */
6881 targets++;
6882 if (targets > AHD_SCB_MAX) {
6883 panic("TID LIST LOOP");
6884 }
6885 if (scbid >= ahd->scb_data.numscbs) {
6886 printf("%s: Waiting TID List inconsistency. "
6887 "SCB index == 0x%x, yet numscbs == 0x%x.",
6888 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
6889 ahd_dump_card_state(ahd);
6890 panic("for safety");
6891 }
6892 scb = ahd_lookup_scb(ahd, scbid);
6893 if (scb == NULL) {
6894 printf("%s: SCB = 0x%x Not Active!\n",
6895 ahd_name(ahd), scbid);
6896 panic("Waiting TID List traversal\n");
6897 break;
6898 }
6899 ahd_set_scbptr(ahd, scbid);
6900 tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
6901 if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
6902 SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
6903 tid_prev = scbid;
6904 continue;
6905 }
6906
6907 /*
6908 * We found a list of scbs that needs to be searched.
6909 */
6910 if (action == SEARCH_PRINT)
6911 printf(" %d ( ", SCB_GET_TARGET(ahd, scb));
6912 tid_head = scbid;
6913 found += ahd_search_scb_list(ahd, target, channel,
6914 lun, tag, role, status,
6915 action, &tid_head,
6916 SCB_GET_TARGET(ahd, scb));
6917 if (tid_head != scbid)
6918 ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
6919 if (!SCBID_IS_NULL(tid_head))
6920 tid_prev = tid_head;
6921 if (action == SEARCH_PRINT)
6922 printf(")\n");
6923 }
6924 ahd_set_scbptr(ahd, savedscbptr);
6925 ahd_restore_modes(ahd, saved_modes);
6926 return (found);
6927 }
6928
6929 static int
6930 ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
6931 int lun, u_int tag, role_t role, uint32_t status,
6932 ahd_search_action action, u_int *list_head, u_int tid)
6933 {
6934 struct scb *scb;
6935 u_int scbid;
6936 u_int next;
6937 u_int prev;
6938 int found;
6939
6940 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
6941 found = 0;
6942 prev = SCB_LIST_NULL;
6943 next = *list_head;
6944 for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
6945 if (scbid >= ahd->scb_data.numscbs) {
6946 printf("%s:SCB List inconsistency. "
6947 "SCB == 0x%x, yet numscbs == 0x%x.",
6948 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
6949 ahd_dump_card_state(ahd);
6950 panic("for safety");
6951 }
6952 scb = ahd_lookup_scb(ahd, scbid);
6953 if (scb == NULL) {
6954 printf("%s: SCB = %d Not Active!\n",
6955 ahd_name(ahd), scbid);
6956 panic("Waiting List traversal\n");
6957 }
6958 ahd_set_scbptr(ahd, scbid);
6959 next = ahd_inw_scbram(ahd, SCB_NEXT);
6960 if (ahd_match_scb(ahd, scb, target, channel,
6961 lun, SCB_LIST_NULL, role) == 0) {
6962 prev = scbid;
6963 continue;
6964 }
6965 found++;
6966 switch (action) {
6967 case SEARCH_COMPLETE:
6968 {
6969 cam_status ostat;
6970 cam_status cstat;
6971
6972 ostat = ahd_get_scsi_status(scb);
6973 if (ostat == CAM_REQ_INPROG)
6974 ahd_set_scsi_status(scb, status);
6975 cstat = ahd_get_transaction_status(scb);
6976 if (cstat != CAM_REQ_CMP)
6977 ahd_freeze_scb(scb);
6978 if ((scb->flags & SCB_ACTIVE) == 0)
6979 printf("Inactive SCB in Waiting List\n");
6980 ahd_done(ahd, scb);
6981 /* FALLTHROUGH */
6982 }
6983 case SEARCH_REMOVE:
6984 ahd_rem_wscb(ahd, scbid, prev, next, tid);
6985 if (prev == SCB_LIST_NULL)
6986 *list_head = next;
6987 break;
6988 case SEARCH_PRINT:
6989 printf("0x%x ", scbid);
6990 case SEARCH_COUNT:
6991 prev = scbid;
6992 break;
6993 }
6994 if (found > AHD_SCB_MAX)
6995 panic("SCB LIST LOOP");
6996 }
6997 if (action == SEARCH_COMPLETE
6998 || action == SEARCH_REMOVE)
6999 ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
7000 return (found);
7001 }
7002
7003 static void
7004 ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
7005 u_int tid_cur, u_int tid_next)
7006 {
7007 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
7008
7009 if (SCBID_IS_NULL(tid_cur)) {
7010
7011 /* Bypass current TID list */
7012 if (SCBID_IS_NULL(tid_prev)) {
7013 ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
7014 } else {
7015 ahd_set_scbptr(ahd, tid_prev);
7016 ahd_outw(ahd, SCB_NEXT2, tid_next);
7017 }
7018 if (SCBID_IS_NULL(tid_next))
7019 ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
7020 } else {
7021
7022 /* Stitch through tid_cur */
7023 if (SCBID_IS_NULL(tid_prev)) {
7024 ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
7025 } else {
7026 ahd_set_scbptr(ahd, tid_prev);
7027 ahd_outw(ahd, SCB_NEXT2, tid_cur);
7028 }
7029 ahd_set_scbptr(ahd, tid_cur);
7030 ahd_outw(ahd, SCB_NEXT2, tid_next);
7031
7032 if (SCBID_IS_NULL(tid_next))
7033 ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
7034 }
7035 }
7036
7037 /*
7038 * Manipulate the waiting for selection list and return the
7039 * scb that follows the one that we remove.
7040 */
7041 static u_int
7042 ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
7043 u_int prev, u_int next, u_int tid)
7044 {
7045 u_int tail_offset;
7046
7047 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
7048 if (!SCBID_IS_NULL(prev)) {
7049 ahd_set_scbptr(ahd, prev);
7050 ahd_outw(ahd, SCB_NEXT, next);
7051 }
7052
7053 /*
7054 * SCBs that had MK_MESSAGE set in them will not
7055 * be queued to the per-target lists, so don't
7056 * blindly clear the tail pointer.
7057 */
7058 tail_offset = WAITING_SCB_TAILS + (2 * tid);
7059 if (SCBID_IS_NULL(next)
7060 && ahd_inw(ahd, tail_offset) == scbid)
7061 ahd_outw(ahd, tail_offset, prev);
7062 ahd_add_scb_to_free_list(ahd, scbid);
7063 return (next);
7064 }
7065
7066 /*
7067 * Add the SCB as selected by SCBPTR onto the on chip list of
7068 * free hardware SCBs. This list is empty/unused if we are not
7069 * performing SCB paging.
7070 */
7071 static void
7072 ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
7073 {
7074 /* XXX Need some other mechanism to designate "free". */
7075 /*
7076 * Invalidate the tag so that our abort
7077 * routines don't think it's active.
7078 ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
7079 */
7080 }
7081
7082 /******************************** Error Handling ******************************/
7083 /*
7084 * Abort all SCBs that match the given description (target/channel/lun/tag),
7085 * setting their status to the passed in status if the status has not already
7086 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
7087 * is paused before it is called.
7088 */
7089 int
7090 ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
7091 int lun, u_int tag, role_t role, uint32_t status)
7092 {
7093 struct scb *scbp;
7094 struct scb *scbp_next;
7095 u_int i, j;
7096 u_int maxtarget;
7097 u_int minlun;
7098 u_int maxlun;
7099 int found;
7100 ahd_mode_state saved_modes;
7101
7102 /* restore this when we're done */
7103 saved_modes = ahd_save_modes(ahd);
7104 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7105
7106 found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
7107 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
7108
7109 /*
7110 * Clean out the busy target table for any untagged commands.
7111 */
7112 i = 0;
7113 maxtarget = 16;
7114 if (target != CAM_TARGET_WILDCARD) {
7115 i = target;
7116 if (channel == 'B')
7117 i += 8;
7118 maxtarget = i + 1;
7119 }
7120
7121 if (lun == CAM_LUN_WILDCARD) {
7122 minlun = 0;
7123 maxlun = AHD_NUM_LUNS_NONPKT;
7124 } else if (lun >= AHD_NUM_LUNS_NONPKT) {
7125 minlun = maxlun = 0;
7126 } else {
7127 minlun = lun;
7128 maxlun = lun + 1;
7129 }
7130
7131 if (role != ROLE_TARGET) {
7132 for (;i < maxtarget; i++) {
7133 for (j = minlun;j < maxlun; j++) {
7134 u_int scbid;
7135 u_int tcl;
7136
7137 tcl = BUILD_TCL_RAW(i, 'A', j);
7138 scbid = ahd_find_busy_tcl(ahd, tcl);
7139 scbp = ahd_lookup_scb(ahd, scbid);
7140 if (scbp == NULL
7141 || ahd_match_scb(ahd, scbp, target, channel,
7142 lun, tag, role) == 0)
7143 continue;
7144 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
7145 }
7146 }
7147 }
7148
7149 /*
7150 * Don't abort commands that have already completed,
7151 * but haven't quite made it up to the host yet.
7152 */
7153 ahd_flush_qoutfifo(ahd);
7154
7155 /*
7156 * Go through the pending CCB list and look for
7157 * commands for this target that are still active.
7158 * These are other tagged commands that were
7159 * disconnected when the reset occurred.
7160 */
7161 scbp_next = LIST_FIRST(&ahd->pending_scbs);
7162 while (scbp_next != NULL) {
7163 scbp = scbp_next;
7164 scbp_next = LIST_NEXT(scbp, pending_links);
7165 if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
7166 cam_status ostat;
7167
7168 ostat = ahd_get_scsi_status(scbp);
7169 if (ostat == CAM_REQ_INPROG)
7170 ahd_set_scsi_status(scbp, status);
7171 if (ahd_get_transaction_status(scbp) != CAM_REQ_CMP)
7172 ahd_freeze_scb(scbp);
7173 if ((scbp->flags & SCB_ACTIVE) == 0)
7174 printf("Inactive SCB on pending list\n");
7175 ahd_done(ahd, scbp);
7176 found++;
7177 }
7178 }
7179 ahd_restore_modes(ahd, saved_modes);
7180 ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
7181 ahd->flags |= AHD_UPDATE_PEND_CMDS;
7182 return found;
7183 }
7184
7185 static void
7186 ahd_reset_current_bus(struct ahd_softc *ahd)
7187 {
7188 uint8_t scsiseq;
7189
7190 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7191 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
7192 scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
7193 ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
7194 ahd_delay(AHD_BUSRESET_DELAY);
7195 /* Turn off the bus reset */
7196 ahd_outb(ahd, SCSISEQ0, scsiseq);
7197 if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
7198 /*
7199 * 2A Razor #474
7200 * Certain chip state is not cleared for
7201 * SCSI bus resets that we initiate, so
7202 * we must reset the chip.
7203 */
7204 ahd_delay(AHD_BUSRESET_DELAY);
7205 ahd_reset(ahd, /*reinit*/TRUE);
7206 ahd_intr_enable(ahd, /*enable*/TRUE);
7207 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7208 }
7209
7210 ahd_clear_intstat(ahd);
7211 }
7212
7213 int
7214 ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
7215 {
7216 struct ahd_devinfo devinfo;
7217 u_int initiator;
7218 u_int target;
7219 u_int max_scsiid;
7220 int found;
7221 u_int fifo;
7222 u_int next_fifo;
7223
7224
7225 ahd->pending_device = NULL;
7226
7227 ahd_compile_devinfo(&devinfo,
7228 CAM_TARGET_WILDCARD,
7229 CAM_TARGET_WILDCARD,
7230 CAM_LUN_WILDCARD,
7231 channel, ROLE_UNKNOWN);
7232 ahd_pause(ahd);
7233
7234 /* Make sure the sequencer is in a safe location. */
7235 ahd_clear_critical_section(ahd);
7236
7237 #if AHD_TARGET_MODE
7238 if ((ahd->flags & AHD_TARGETROLE) != 0) {
7239 ahd_run_tqinfifo(ahd, /*paused*/TRUE);
7240 }
7241 #endif
7242 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7243
7244 /*
7245 * Disable selections so no automatic hardware
7246 * functions will modify chip state.
7247 */
7248 ahd_outb(ahd, SCSISEQ0, 0);
7249 ahd_outb(ahd, SCSISEQ1, 0);
7250
7251 /*
7252 * Safely shut down our DMA engines. Always start with
7253 * the FIFO that is not currently active (if any are
7254 * actively connected).
7255 */
7256 next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
7257 if (next_fifo > CURRFIFO_1)
7258 /* If disconneced, arbitrarily start with FIFO1. */
7259 next_fifo = fifo = 0;
7260 do {
7261 next_fifo ^= CURRFIFO_1;
7262 ahd_set_modes(ahd, next_fifo, next_fifo);
7263 ahd_outb(ahd, DFCNTRL,
7264 ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
7265 while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
7266 ahd_delay(10);
7267 /*
7268 * Set CURRFIFO to the now inactive channel.
7269 */
7270 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7271 ahd_outb(ahd, DFFSTAT, next_fifo);
7272 } while (next_fifo != fifo);
7273
7274 /*
7275 * Reset the bus if we are initiating this reset
7276 */
7277 ahd_clear_msg_state(ahd);
7278 ahd_outb(ahd, SIMODE1,
7279 ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST|ENBUSFREE));
7280
7281 if (initiate_reset)
7282 ahd_reset_current_bus(ahd);
7283
7284 ahd_clear_intstat(ahd);
7285
7286 /*
7287 * Clean up all the state information for the
7288 * pending transactions on this bus.
7289 */
7290 found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
7291 CAM_LUN_WILDCARD, SCB_LIST_NULL,
7292 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
7293
7294 /*
7295 * Cleanup anything left in the FIFOs.
7296 */
7297 ahd_clear_fifo(ahd, 0);
7298 ahd_clear_fifo(ahd, 1);
7299
7300 /*
7301 * Revert to async/narrow transfers until we renegotiate.
7302 */
7303 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
7304 for (target = 0; target <= max_scsiid; target++) {
7305
7306 if (ahd->enabled_targets[target] == NULL)
7307 continue;
7308 for (initiator = 0; initiator <= max_scsiid; initiator++) {
7309 struct ahd_devinfo devinfo;
7310
7311 ahd_compile_devinfo(&devinfo, target, initiator,
7312 CAM_LUN_WILDCARD,
7313 'A', ROLE_UNKNOWN);
7314 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
7315 AHD_TRANS_CUR, /*paused*/TRUE);
7316 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
7317 /*offset*/0, /*ppr_options*/0,
7318 AHD_TRANS_CUR, /*paused*/TRUE);
7319 }
7320 }
7321
7322 #ifdef AHD_TARGET_MODE
7323 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
7324
7325 /*
7326 * Send an immediate notify ccb to all target more peripheral
7327 * drivers affected by this action.
7328 */
7329 for (target = 0; target <= max_scsiid; target++) {
7330 struct ahd_tmode_tstate* tstate;
7331 u_int lun;
7332
7333 tstate = ahd->enabled_targets[target];
7334 if (tstate == NULL)
7335 continue;
7336 for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
7337 struct ahd_tmode_lstate* lstate;
7338
7339 lstate = tstate->enabled_luns[lun];
7340 if (lstate == NULL)
7341 continue;
7342
7343 ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
7344 EVENT_TYPE_BUS_RESET, /*arg*/0);
7345 ahd_send_lstate_events(ahd, lstate);
7346 }
7347 }
7348 #endif
7349
7350 /* Notify the XPT that a bus reset occurred */
7351 ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
7352 CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
7353 ahd_restart(ahd);
7354
7355 /*
7356 * Freeze the SIMQ until our poller can determine that
7357 * the bus reset has really gone away. We set the initial
7358 * timer to 0 to have the check performed as soon as possible
7359 * from the timer context.
7360 */
7361 if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) {
7362 ahd->flags |= AHD_RESET_POLL_ACTIVE;
7363 ahd_freeze_simq(ahd);
7364 ahd_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd);
7365 }
7366 return (found);
7367 }
7368
7369
7370 #define AHD_RESET_POLL_US 1000
7371 static void
7372 ahd_reset_poll(void *arg)
7373 {
7374 struct ahd_softc *ahd;
7375 u_int scsiseq1;
7376 u_long l;
7377 int s;
7378
7379 ahd_list_lock(&l);
7380 ahd = (void*)arg;
7381 if (ahd == NULL) {
7382 printf("ahd_reset_poll: Instance %p no longer exists\n", arg);
7383 ahd_list_unlock(&l);
7384 return;
7385 }
7386 ahd_lock(ahd, &s);
7387 ahd_pause(ahd);
7388 ahd_update_modes(ahd);
7389 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7390 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
7391 if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) {
7392 ahd_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_US,
7393 ahd_reset_poll, ahd);
7394 ahd_unpause(ahd);
7395 ahd_unlock(ahd, &s);
7396 ahd_list_unlock(&l);
7397 return;
7398 }
7399
7400 /* Reset is now low. Complete chip reinitialization. */
7401 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
7402 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
7403 ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP));
7404 ahd_unpause(ahd);
7405 ahd->flags &= ~AHD_RESET_POLL_ACTIVE;
7406 ahd_unlock(ahd, &s);
7407 ahd_release_simq(ahd);
7408 ahd_list_unlock(&l);
7409 }
7410
7411 /**************************** Statistics Processing ***************************/
7412 static void
7413 ahd_stat_timer(void *arg)
7414 {
7415 struct ahd_softc *ahd;
7416 u_long l;
7417 int s;
7418 int enint_coal;
7419
7420 ahd_list_lock(&l);
7421 ahd = (void *)arg;
7422 if (ahd == NULL) {
7423 printf("ahd_stat_timer: Instance %p no longer exists\n", arg);
7424 ahd_list_unlock(&l);
7425 return;
7426 }
7427 ahd_lock(ahd, &s);
7428
7429 enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
7430 if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
7431 enint_coal |= ENINT_COALESCE;
7432 else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
7433 enint_coal &= ~ENINT_COALESCE;
7434
7435 if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
7436 ahd_enable_coalescing(ahd, enint_coal);
7437 #ifdef AHD_DEBUG
7438 if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
7439 printf("%s: Interrupt coalescing "
7440 "now %sabled. Cmds %d\n",
7441 ahd_name(ahd),
7442 (enint_coal & ENINT_COALESCE) ? "en" : "dis",
7443 ahd->cmdcmplt_total);
7444 #endif
7445 }
7446
7447 ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
7448 ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
7449 ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
7450 ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
7451 ahd_stat_timer, ahd);
7452 ahd_unlock(ahd, &s);
7453 ahd_list_unlock(&l);
7454 }
7455
7456 /****************************** Status Processing *****************************/
7457 void
7458 ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
7459 {
7460 if (scb->hscb->shared_data.istatus.scsi_status != 0) {
7461 ahd_handle_scsi_status(ahd, scb);
7462 } else {
7463 ahd_calc_residual(ahd, scb);
7464 ahd_done(ahd, scb);
7465 }
7466 }
7467
7468 void
7469 ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
7470 {
7471 struct hardware_scb *hscb;
7472 u_int qfreeze_cnt;
7473
7474 /*
7475 * The sequencer freezes its select-out queue
7476 * anytime a SCSI status error occurs. We must
7477 * handle the error and decrement the QFREEZE count
7478 * to allow the sequencer to continue.
7479 */
7480 hscb = scb->hscb;
7481
7482 /* Freeze the queue until the client sees the error. */
7483 ahd_freeze_devq(ahd, scb);
7484 ahd_freeze_scb(scb);
7485 qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
7486 if (qfreeze_cnt == 0) {
7487 printf("%s: Bad status with 0 qfreeze count!\n", ahd_name(ahd));
7488 } else {
7489 qfreeze_cnt--;
7490 ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
7491 }
7492 if (qfreeze_cnt == 0)
7493 ahd_outb(ahd, SEQ_FLAGS2,
7494 ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
7495
7496 /* Don't want to clobber the original sense code */
7497 if ((scb->flags & SCB_SENSE) != 0) {
7498 /*
7499 * Clear the SCB_SENSE Flag and perform
7500 * a normal command completion.
7501 */
7502 scb->flags &= ~SCB_SENSE;
7503 ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
7504 ahd_done(ahd, scb);
7505 return;
7506 }
7507 ahd_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
7508 ahd_set_xfer_status(scb, hscb->shared_data.istatus.scsi_status);
7509 switch (hscb->shared_data.istatus.scsi_status) {
7510 case STATUS_PKT_SENSE:
7511 {
7512 struct scsi_status_iu_header *siu;
7513
7514 ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
7515 siu = (struct scsi_status_iu_header *)scb->sense_data;
7516 ahd_set_scsi_status(scb, siu->status);
7517 #ifdef AHD_DEBUG
7518 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
7519 ahd_print_path(ahd, scb);
7520 printf("SCB 0x%x Received PKT Status of 0x%x\n",
7521 SCB_GET_TAG(scb), siu->status);
7522 }
7523 #endif
7524 if ((siu->flags & SIU_RSPVALID) != 0) {
7525 scsipi_printaddr(scb->xs->xs_periph);
7526 if (scsi_4btoul(siu->pkt_failures_length) < 4) {
7527 printf("Unable to parse pkt_failures\n");
7528 } else {
7529
7530 switch (SIU_PKTFAIL_CODE(siu)) {
7531 case SIU_PFC_NONE:
7532 printf("No packet failure found\n");
7533 break;
7534 case SIU_PFC_CIU_FIELDS_INVALID:
7535 printf("Invalid Command IU Field\n");
7536 break;
7537 case SIU_PFC_TMF_NOT_SUPPORTED:
7538 printf("TMF not supportd\n");
7539 break;
7540 case SIU_PFC_TMF_FAILED:
7541 printf("TMF failed\n");
7542 break;
7543 case SIU_PFC_INVALID_TYPE_CODE:
7544 printf("Invalid L_Q Type code\n");
7545 break;
7546 case SIU_PFC_ILLEGAL_REQUEST:
7547 printf("Illegal request\n");
7548 default:
7549 break;
7550 }
7551 }
7552 if (siu->status == SCSI_STATUS_OK)
7553 ahd_set_transaction_status(scb, CAM_REQ_CMP_ERR);
7554 }
7555 if ((siu->flags & SIU_SNSVALID) != 0) {
7556 scb->flags |= SCB_PKT_SENSE;
7557 #ifdef AHD_DEBUG
7558 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
7559 printf("Sense data available (%d)\n", siu->sense_length[0]);
7560 printf("SK 0x%x ASC 0x%x ASCQ 0x%x\n",
7561 ((uint8_t)scb->sense_data[SIU_SENSE_OFFSET(siu)+2]) & 0x0F,
7562 ((uint8_t)scb->sense_data[SIU_SENSE_OFFSET(siu)+12]),
7563 ((uint8_t)scb->sense_data[SIU_SENSE_OFFSET(siu)+13]));
7564 }
7565 #endif
7566 }
7567 ahd_done(ahd, scb);
7568 break;
7569 }
7570 case SCSI_STATUS_CMD_TERMINATED:
7571 case SCSI_STATUS_CHECK_COND:
7572 {
7573 struct ahd_devinfo devinfo;
7574 struct ahd_dma_seg *sg;
7575 struct scsipi_sense *sc;
7576 struct ahd_initiator_tinfo *targ_info;
7577 struct ahd_tmode_tstate *tstate;
7578 struct ahd_transinfo *tinfo;
7579 #ifdef AHD_DEBUG
7580 if (ahd_debug & AHD_SHOW_SENSE) {
7581 ahd_print_path(ahd, scb);
7582 printf("SCB %d: requests Check Status\n",
7583 SCB_GET_TAG(scb));
7584 }
7585 #endif
7586
7587 if (ahd_perform_autosense(scb) == 0)
7588 break;
7589
7590 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
7591 SCB_GET_TARGET(ahd, scb),
7592 SCB_GET_LUN(scb),
7593 SCB_GET_CHANNEL(ahd, scb),
7594 ROLE_INITIATOR);
7595 targ_info = ahd_fetch_transinfo(ahd,
7596 devinfo.channel,
7597 devinfo.our_scsiid,
7598 devinfo.target,
7599 &tstate);
7600 tinfo = &targ_info->curr;
7601 sg = scb->sg_list;
7602 sc = (struct scsipi_sense *)hscb->shared_data.idata.cdb;
7603 /*
7604 * Save off the residual if there is one.
7605 */
7606 ahd_update_residual(ahd, scb);
7607 #ifdef AHD_DEBUG
7608 if (ahd_debug & AHD_SHOW_SENSE) {
7609 ahd_print_path(ahd, scb);
7610 printf("Sending Sense\n");
7611 }
7612 #endif
7613 scb->sg_count = 0;
7614 sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
7615 ahd_get_sense_bufsize(ahd, scb),
7616 /*last*/TRUE);
7617 sc->opcode = REQUEST_SENSE;
7618 sc->byte2 = 0;
7619 sc->unused[0] = 0;
7620 sc->unused[1] = 0;
7621 sc->length = ahd_get_sense_bufsize(ahd, scb);
7622 sc->control = 0;
7623
7624 /*
7625 * We can't allow the target to disconnect.
7626 * This will be an untagged transaction and
7627 * having the target disconnect will make this
7628 * transaction indestinguishable from outstanding
7629 * tagged transactions.
7630 */
7631 hscb->control = 0;
7632
7633 /*
7634 * This request sense could be because the
7635 * the device lost power or in some other
7636 * way has lost our transfer negotiations.
7637 * Renegotiate if appropriate. Unit attention
7638 * errors will be reported before any data
7639 * phases occur.
7640 */
7641 if (ahd_get_residual(scb) == ahd_get_transfer_length(scb)) {
7642 ahd_update_neg_request(ahd, &devinfo,
7643 tstate, targ_info,
7644 AHD_NEG_IF_NON_ASYNC);
7645 }
7646 if (tstate->auto_negotiate & devinfo.target_mask) {
7647 hscb->control |= MK_MESSAGE;
7648 scb->flags &=
7649 ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
7650 scb->flags |= SCB_AUTO_NEGOTIATE;
7651 }
7652 hscb->cdb_len = sizeof(*sc);
7653 ahd_setup_data_scb(ahd, scb);
7654 scb->flags |= SCB_SENSE;
7655 ahd_queue_scb(ahd, scb);
7656 /*
7657 * Ensure we have enough time to actually
7658 * retrieve the sense.
7659 */
7660 ahd_scb_timer_reset(scb, 5 * 1000000);
7661 break;
7662 }
7663 case SCSI_STATUS_OK:
7664 printf("%s: Interrupted for status of 0? (SCB 0x%x)\n",
7665 ahd_name(ahd), SCB_GET_TAG(scb));
7666 /* FALLTHROUGH */
7667 default:
7668 ahd_done(ahd, scb);
7669 break;
7670 }
7671 }
7672
7673 /*
7674 * Calculate the residual for a just completed SCB.
7675 */
7676 void
7677 ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
7678 {
7679 struct hardware_scb *hscb;
7680 struct initiator_status *spkt;
7681 uint32_t sgptr;
7682 uint32_t resid_sgptr;
7683 uint32_t resid;
7684
7685 /*
7686 * 5 cases.
7687 * 1) No residual.
7688 * SG_STATUS_VALID clear in sgptr.
7689 * 2) Transferless command
7690 * 3) Never performed any transfers.
7691 * sgptr has SG_FULL_RESID set.
7692 * 4) No residual but target did not
7693 * save data pointers after the
7694 * last transfer, so sgptr was
7695 * never updated.
7696 * 5) We have a partial residual.
7697 * Use residual_sgptr to determine
7698 * where we are.
7699 */
7700
7701 hscb = scb->hscb;
7702 sgptr = ahd_le32toh(hscb->sgptr);
7703 if ((sgptr & SG_STATUS_VALID) == 0)
7704 /* Case 1 */
7705 return;
7706 sgptr &= ~SG_STATUS_VALID;
7707
7708 if ((sgptr & SG_LIST_NULL) != 0)
7709 /* Case 2 */
7710 return;
7711
7712 /*
7713 * Residual fields are the same in both
7714 * target and initiator status packets,
7715 * so we can always use the initiator fields
7716 * regardless of the role for this SCB.
7717 */
7718 spkt = &hscb->shared_data.istatus;
7719 resid_sgptr = ahd_le32toh(spkt->residual_sgptr);
7720 if ((sgptr & SG_FULL_RESID) != 0) {
7721 /* Case 3 */
7722 resid = ahd_get_transfer_length(scb);
7723 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
7724 /* Case 4 */
7725 return;
7726 } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
7727 ahd_print_path(ahd, scb);
7728 printf("data overrun detected Tag == 0x%x.\n",
7729 SCB_GET_TAG(scb));
7730 ahd_freeze_devq(ahd, scb);
7731 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
7732 ahd_freeze_scb(scb);
7733 return;
7734 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
7735 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
7736 /* NOTREACHED */
7737 } else {
7738 struct ahd_dma_seg *sg;
7739
7740 /*
7741 * Remainder of the SG where the transfer
7742 * stopped.
7743 */
7744 resid = ahd_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
7745 sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
7746
7747 /* The residual sg_ptr always points to the next sg */
7748 sg--;
7749
7750 /*
7751 * Add up the contents of all residual
7752 * SG segments that are after the SG where
7753 * the transfer stopped.
7754 */
7755 while ((ahd_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
7756 sg++;
7757 resid += ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
7758 }
7759 }
7760
7761 if ((scb->flags & SCB_SENSE) == 0)
7762 ahd_set_residual(scb, resid);
7763 /*else
7764 ahd_set_sense_residual(scb, resid);*/
7765
7766 #ifdef AHD_DEBUG
7767 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
7768 ahd_print_path(ahd, scb);
7769 printf("Handled %sResidual of %d bytes\n",
7770 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
7771 }
7772 #endif
7773 }
7774
7775 /******************************* Target Mode **********************************/
7776 #ifdef AHD_TARGET_MODE
7777 /*
7778 * Add a target mode event to this lun's queue
7779 */
7780 static void
7781 ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
7782 u_int initiator_id, u_int event_type, u_int event_arg)
7783 {
7784 struct ahd_tmode_event *event;
7785 int pending;
7786
7787 xpt_freeze_devq(lstate->path, /*count*/1);
7788 if (lstate->event_w_idx >= lstate->event_r_idx)
7789 pending = lstate->event_w_idx - lstate->event_r_idx;
7790 else
7791 pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
7792 - (lstate->event_r_idx - lstate->event_w_idx);
7793
7794 if (event_type == EVENT_TYPE_BUS_RESET
7795 || event_type == MSG_BUS_DEV_RESET) {
7796 /*
7797 * Any earlier events are irrelevant, so reset our buffer.
7798 * This has the effect of allowing us to deal with reset
7799 * floods (an external device holding down the reset line)
7800 * without losing the event that is really interesting.
7801 */
7802 lstate->event_r_idx = 0;
7803 lstate->event_w_idx = 0;
7804 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
7805 }
7806
7807 if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
7808 xpt_print_path(lstate->path);
7809 printf("immediate event %x:%x lost\n",
7810 lstate->event_buffer[lstate->event_r_idx].event_type,
7811 lstate->event_buffer[lstate->event_r_idx].event_arg);
7812 lstate->event_r_idx++;
7813 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
7814 lstate->event_r_idx = 0;
7815 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
7816 }
7817
7818 event = &lstate->event_buffer[lstate->event_w_idx];
7819 event->initiator_id = initiator_id;
7820 event->event_type = event_type;
7821 event->event_arg = event_arg;
7822 lstate->event_w_idx++;
7823 if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
7824 lstate->event_w_idx = 0;
7825 }
7826
7827 /*
7828 * Send any target mode events queued up waiting
7829 * for immediate notify resources.
7830 */
7831 void
7832 ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
7833 {
7834 struct ccb_hdr *ccbh;
7835 struct ccb_immed_notify *inot;
7836
7837 while (lstate->event_r_idx != lstate->event_w_idx
7838 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
7839 struct ahd_tmode_event *event;
7840
7841 event = &lstate->event_buffer[lstate->event_r_idx];
7842 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
7843 inot = (struct ccb_immed_notify *)ccbh;
7844 switch (event->event_type) {
7845 case EVENT_TYPE_BUS_RESET:
7846 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
7847 break;
7848 default:
7849 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
7850 inot->message_args[0] = event->event_type;
7851 inot->message_args[1] = event->event_arg;
7852 break;
7853 }
7854 inot->initiator_id = event->initiator_id;
7855 inot->sense_len = 0;
7856 xpt_done((union ccb *)inot);
7857 lstate->event_r_idx++;
7858 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
7859 lstate->event_r_idx = 0;
7860 }
7861 }
7862 #endif
7863
7864 /******************** Sequencer Program Patching/Download *********************/
7865
7866 #ifdef AHD_DUMP_SEQ
7867 void
7868 ahd_dumpseq(struct ahd_softc* ahd)
7869 {
7870 int i;
7871 int max_prog;
7872
7873 max_prog = 2048;
7874
7875 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
7876 ahd_outb(ahd, PRGMCNT, 0);
7877 ahd_outb(ahd, PRGMCNT+1, 0);
7878 for (i = 0; i < max_prog; i++) {
7879 uint8_t ins_bytes[4];
7880
7881 ahd_insb(ahd, SEQRAM, ins_bytes, 4);
7882 printf("0x%08x\n", ins_bytes[0] << 24
7883 | ins_bytes[1] << 16
7884 | ins_bytes[2] << 8
7885 | ins_bytes[3]);
7886 }
7887 }
7888 #endif
7889
7890 static void
7891 ahd_loadseq(struct ahd_softc *ahd)
7892 {
7893 struct cs cs_table[num_critical_sections];
7894 u_int begin_set[num_critical_sections];
7895 u_int end_set[num_critical_sections];
7896 struct patch *cur_patch;
7897 u_int cs_count;
7898 u_int cur_cs;
7899 u_int i;
7900 int downloaded;
7901 u_int skip_addr;
7902 u_int sg_prefetch_cnt;
7903 u_int sg_prefetch_cnt_limit;
7904 u_int sg_prefetch_align;
7905 u_int sg_size;
7906 uint8_t download_consts[DOWNLOAD_CONST_COUNT];
7907
7908 if (bootverbose)
7909 printf("%s: Downloading Sequencer Program...",
7910 ahd_name(ahd));
7911
7912 #if DOWNLOAD_CONST_COUNT != 7
7913 #error "Download Const Mismatch"
7914 #endif
7915 /*
7916 * Start out with 0 critical sections
7917 * that apply to this firmware load.
7918 */
7919 cs_count = 0;
7920 cur_cs = 0;
7921 memset(begin_set, 0, sizeof(begin_set));
7922 memset(end_set, 0, sizeof(end_set));
7923
7924 /*
7925 * Setup downloadable constant table.
7926 *
7927 * The computation for the S/G prefetch variables is
7928 * a bit complicated. We would like to always fetch
7929 * in terms of cachelined sized increments. However,
7930 * if the cacheline is not an even multiple of the
7931 * SG element size or is larger than our SG RAM, using
7932 * just the cache size might leave us with only a portion
7933 * of an SG element at the tail of a prefetch. If the
7934 * cacheline is larger than our S/G prefetch buffer less
7935 * the size of an SG element, we may round down to a cacheline
7936 * that doesn't contain any or all of the S/G of interest
7937 * within the bounds of our S/G ram. Provide variables to
7938 * the sequencer that will allow it to handle these edge
7939 * cases.
7940 */
7941 /* Start by aligning to the nearest cacheline. */
7942 sg_prefetch_align = ahd->pci_cachesize;
7943 if (sg_prefetch_align == 0)
7944 sg_prefetch_align = 8;
7945 /* Round down to the nearest power of 2. */
7946 while (powerof2(sg_prefetch_align) == 0)
7947 sg_prefetch_align--;
7948 /*
7949 * If the cacheline boundary is greater than half our prefetch RAM
7950 * we risk not being able to fetch even a single complete S/G
7951 * segment if we align to that boundary.
7952 */
7953 if (sg_prefetch_align > CCSGADDR_MAX/2)
7954 sg_prefetch_align = CCSGADDR_MAX/2;
7955 /* Start by fetching a single cacheline. */
7956 sg_prefetch_cnt = sg_prefetch_align;
7957 /*
7958 * Increment the prefetch count by cachelines until
7959 * at least one S/G element will fit.
7960 */
7961 sg_size = sizeof(struct ahd_dma_seg);
7962 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
7963 sg_size = sizeof(struct ahd_dma64_seg);
7964 while (sg_prefetch_cnt < sg_size)
7965 sg_prefetch_cnt += sg_prefetch_align;
7966 /*
7967 * If the cacheline is not an even multiple of
7968 * the S/G size, we may only get a partial S/G when
7969 * we align. Add a cacheline if this is the case.
7970 */
7971 if ((sg_prefetch_align % sg_size) != 0
7972 && (sg_prefetch_cnt < CCSGADDR_MAX))
7973 sg_prefetch_cnt += sg_prefetch_align;
7974 /*
7975 * Lastly, compute a value that the sequencer can use
7976 * to determine if the remainder of the CCSGRAM buffer
7977 * has a full S/G element in it.
7978 */
7979 sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
7980 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
7981 download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
7982 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
7983 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
7984 download_consts[SG_SIZEOF] = sg_size;
7985 download_consts[PKT_OVERRUN_BUFOFFSET] =
7986 (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
7987 download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
7988 cur_patch = patches;
7989 downloaded = 0;
7990 skip_addr = 0;
7991 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
7992 ahd_outb(ahd, PRGMCNT, 0);
7993 ahd_outb(ahd, PRGMCNT+1, 0);
7994
7995 for (i = 0; i < sizeof(seqprog)/4; i++) {
7996 if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
7997 /*
7998 * Don't download this instruction as it
7999 * is in a patch that was removed.
8000 */
8001 continue;
8002 }
8003 /*
8004 * Move through the CS table until we find a CS
8005 * that might apply to this instruction.
8006 */
8007 for (; cur_cs < num_critical_sections; cur_cs++) {
8008 if (critical_sections[cur_cs].end <= i) {
8009 if (begin_set[cs_count] == TRUE
8010 && end_set[cs_count] == FALSE) {
8011 cs_table[cs_count].end = downloaded;
8012 end_set[cs_count] = TRUE;
8013 cs_count++;
8014 }
8015 continue;
8016 }
8017 if (critical_sections[cur_cs].begin <= i
8018 && begin_set[cs_count] == FALSE) {
8019 cs_table[cs_count].begin = downloaded;
8020 begin_set[cs_count] = TRUE;
8021 }
8022 break;
8023 }
8024 ahd_download_instr(ahd, i, download_consts);
8025 downloaded++;
8026 }
8027
8028 ahd->num_critical_sections = cs_count;
8029 if (cs_count != 0) {
8030
8031 cs_count *= sizeof(struct cs);
8032 ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
8033 if (ahd->critical_sections == NULL)
8034 panic("ahd_loadseq: Could not malloc");
8035 memcpy(ahd->critical_sections, cs_table, cs_count);
8036 }
8037 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
8038
8039 if (bootverbose) {
8040 printf(" %d instructions downloaded\n", downloaded);
8041 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
8042 ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
8043 }
8044 }
8045
8046 static int
8047 ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
8048 u_int start_instr, u_int *skip_addr)
8049 {
8050 struct patch *cur_patch;
8051 struct patch *last_patch;
8052 u_int num_patches;
8053
8054 num_patches = sizeof(patches)/sizeof(struct patch);
8055 last_patch = &patches[num_patches];
8056 cur_patch = *start_patch;
8057
8058 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
8059
8060 if (cur_patch->patch_func(ahd) == 0) {
8061
8062 /* Start rejecting code */
8063 *skip_addr = start_instr + cur_patch->skip_instr;
8064 cur_patch += cur_patch->skip_patch;
8065 } else {
8066 /* Accepted this patch. Advance to the next
8067 * one and wait for our intruction pointer to
8068 * hit this point.
8069 */
8070 cur_patch++;
8071 }
8072 }
8073
8074 *start_patch = cur_patch;
8075 if (start_instr < *skip_addr)
8076 /* Still skipping */
8077 return (0);
8078
8079 return (1);
8080 }
8081
8082 static u_int
8083 ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
8084 {
8085 struct patch *cur_patch;
8086 int address_offset;
8087 u_int skip_addr;
8088 u_int i;
8089
8090 address_offset = 0;
8091 cur_patch = patches;
8092 skip_addr = 0;
8093
8094 for (i = 0; i < address;) {
8095
8096 ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
8097
8098 if (skip_addr > i) {
8099 int end_addr;
8100
8101 end_addr = MIN(address, skip_addr);
8102 address_offset += end_addr - i;
8103 i = skip_addr;
8104 } else {
8105 i++;
8106 }
8107 }
8108 return (address - address_offset);
8109 }
8110
8111 static void
8112 ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
8113 {
8114 union ins_formats instr;
8115 struct ins_format1 *fmt1_ins;
8116 struct ins_format3 *fmt3_ins;
8117 u_int opcode;
8118
8119 /*
8120 * The firmware is always compiled into a little endian format.
8121 */
8122 instr.integer = ahd_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
8123
8124 fmt1_ins = &instr.format1;
8125 fmt3_ins = NULL;
8126
8127 /* Pull the opcode */
8128 opcode = instr.format1.opcode;
8129 switch (opcode) {
8130 case AIC_OP_JMP:
8131 case AIC_OP_JC:
8132 case AIC_OP_JNC:
8133 case AIC_OP_CALL:
8134 case AIC_OP_JNE:
8135 case AIC_OP_JNZ:
8136 case AIC_OP_JE:
8137 case AIC_OP_JZ:
8138 {
8139 fmt3_ins = &instr.format3;
8140 fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
8141 /* FALLTHROUGH */
8142 }
8143 case AIC_OP_OR:
8144 case AIC_OP_AND:
8145 case AIC_OP_XOR:
8146 case AIC_OP_ADD:
8147 case AIC_OP_ADC:
8148 case AIC_OP_BMOV:
8149 if (fmt1_ins->parity != 0) {
8150 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
8151 }
8152 fmt1_ins->parity = 0;
8153 /* FALLTHROUGH */
8154 case AIC_OP_ROL:
8155 {
8156 int i, count;
8157
8158 /* Calculate odd parity for the instruction */
8159 for (i = 0, count = 0; i < 31; i++) {
8160 uint32_t mask;
8161
8162 mask = 0x01 << i;
8163 if ((instr.integer & mask) != 0)
8164 count++;
8165 }
8166 if ((count & 0x01) == 0)
8167 instr.format1.parity = 1;
8168
8169 /* The sequencer is a little endian cpu */
8170 instr.integer = ahd_htole32(instr.integer);
8171 ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
8172 break;
8173 }
8174 default:
8175 panic("Unknown opcode encountered in seq program");
8176 break;
8177 }
8178 }
8179
8180 static int
8181 ahd_probe_stack_size(struct ahd_softc *ahd)
8182 {
8183 int last_probe;
8184
8185 last_probe = 0;
8186 while (1) {
8187 int i;
8188
8189 /*
8190 * We avoid using 0 as a pattern to avoid
8191 * confusion if the stack implementation
8192 * "back-fills" with zeros when "poping'
8193 * entries.
8194 */
8195 for (i = 1; i <= last_probe+1; i++) {
8196 ahd_outb(ahd, STACK, i & 0xFF);
8197 ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
8198 }
8199
8200 /* Verify */
8201 for (i = last_probe+1; i > 0; i--) {
8202 u_int stack_entry;
8203
8204 stack_entry = ahd_inb(ahd, STACK)
8205 |(ahd_inb(ahd, STACK) << 8);
8206 if (stack_entry != i)
8207 goto sized;
8208 }
8209 last_probe++;
8210 }
8211 sized:
8212 return (last_probe);
8213 }
8214
8215 void
8216 ahd_dump_all_cards_state(void)
8217 {
8218 struct ahd_softc *list_ahd;
8219
8220 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
8221 ahd_dump_card_state(list_ahd);
8222 }
8223 }
8224
8225 int
8226 ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
8227 const char *name, u_int address, u_int value,
8228 u_int *cur_column, u_int wrap_point)
8229 {
8230 int printed;
8231 u_int printed_mask;
8232 char line[1024];
8233
8234 line[0] = 0;
8235
8236 if (cur_column != NULL && *cur_column >= wrap_point) {
8237 printf("\n");
8238 *cur_column = 0;
8239 }
8240 printed = snprintf(line, sizeof(line), "%s[0x%x]", name, value);
8241 if (table == NULL) {
8242 printed += snprintf(&line[printed], (sizeof line) - printed,
8243 " ");
8244 printf("%s", line);
8245 if (cur_column != NULL)
8246 *cur_column += printed;
8247 return (printed);
8248 }
8249 printed_mask = 0;
8250 while (printed_mask != 0xFF) {
8251 int entry;
8252
8253 for (entry = 0; entry < num_entries; entry++) {
8254 if (((value & table[entry].mask)
8255 != table[entry].value)
8256 || ((printed_mask & table[entry].mask)
8257 == table[entry].mask))
8258 continue;
8259 printed += snprintf(&line[printed],
8260 (sizeof line) - printed, "%s%s",
8261 printed_mask == 0 ? ":(" : "|",
8262 table[entry].name);
8263 printed_mask |= table[entry].mask;
8264
8265 break;
8266 }
8267 if (entry >= num_entries)
8268 break;
8269 }
8270 if (printed_mask != 0)
8271 printed += snprintf(&line[printed],
8272 (sizeof line) - printed, ") ");
8273 else
8274 printed += snprintf(&line[printed],
8275 (sizeof line) - printed, " ");
8276 if (cur_column != NULL)
8277 *cur_column += printed;
8278 printf("%s", line);
8279
8280 return (printed);
8281 }
8282
8283 void
8284 ahd_dump_card_state(struct ahd_softc *ahd)
8285 {
8286 struct scb *scb;
8287 ahd_mode_state saved_modes;
8288 u_int dffstat;
8289 int paused;
8290 u_int scb_index;
8291 u_int saved_scb_index;
8292 u_int cur_col;
8293 int i;
8294
8295 if (ahd_is_paused(ahd)) {
8296 paused = 1;
8297 } else {
8298 paused = 0;
8299 ahd_pause(ahd);
8300 }
8301 saved_modes = ahd_save_modes(ahd);
8302 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8303 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
8304 "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
8305 ahd_name(ahd),
8306 ahd_inb(ahd, CURADDR) | (ahd_inb(ahd, CURADDR+1) << 8),
8307 ahd_build_mode_state(ahd, ahd->saved_src_mode,
8308 ahd->saved_dst_mode));
8309 if (paused)
8310 printf("Card was paused\n");
8311 /*
8312 * Mode independent registers.
8313 */
8314 cur_col = 0;
8315 ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
8316 ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
8317 ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
8318 ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
8319 ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
8320 ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
8321 ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
8322 ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
8323 ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
8324 ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
8325 ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
8326 ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
8327 ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
8328 ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
8329 ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
8330 ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
8331 ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
8332 ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
8333 ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
8334 ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
8335 ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
8336 ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
8337 ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
8338 ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
8339 ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
8340 ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
8341 ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
8342 printf("\n");
8343 printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
8344 "CURRSCB 0x%x NEXTSCB 0x%x\n",
8345 ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
8346 ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
8347 ahd_inw(ahd, NEXTSCB));
8348 cur_col = 0;
8349 /* QINFIFO */
8350 ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
8351 CAM_LUN_WILDCARD, SCB_LIST_NULL,
8352 ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
8353 saved_scb_index = ahd_get_scbptr(ahd);
8354 printf("Pending list:");
8355 i = 0;
8356 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
8357 if (i++ > AHD_SCB_MAX)
8358 break;
8359 /*cur_col =*/ printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
8360 ahd_inb(ahd, SCB_FIFO_USE_COUNT));
8361 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
8362 ahd_scb_control_print(ahd_inb(ahd, SCB_CONTROL), &cur_col, 60);
8363 ahd_scb_scsiid_print(ahd_inb(ahd, SCB_SCSIID), &cur_col, 60);
8364 }
8365 printf("\nTotal %d\n", i);
8366
8367 printf("Kernel Free SCB list: ");
8368 i = 0;
8369 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
8370 struct scb *list_scb;
8371
8372 list_scb = scb;
8373 do {
8374 printf("%d ", SCB_GET_TAG(list_scb));
8375 list_scb = LIST_NEXT(list_scb, collision_links);
8376 } while (list_scb && i++ < AHD_SCB_MAX);
8377 }
8378
8379 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
8380 if (i++ > AHD_SCB_MAX)
8381 break;
8382 printf("%d ", SCB_GET_TAG(scb));
8383 }
8384 printf("\n");
8385
8386 printf("Sequencer Complete DMA-inprog list: ");
8387 scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
8388 i = 0;
8389 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8390 ahd_set_scbptr(ahd, scb_index);
8391 printf("%d ", scb_index);
8392 scb_index = ahd_inw(ahd, SCB_NEXT_COMPLETE);
8393 }
8394 printf("\n");
8395
8396 printf("Sequencer Complete list: ");
8397 scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
8398 i = 0;
8399 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8400 ahd_set_scbptr(ahd, scb_index);
8401 printf("%d ", scb_index);
8402 scb_index = ahd_inw(ahd, SCB_NEXT_COMPLETE);
8403 }
8404 printf("\n");
8405
8406
8407 printf("Sequencer DMA-Up and Complete list: ");
8408 scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
8409 i = 0;
8410 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8411 ahd_set_scbptr(ahd, scb_index);
8412 printf("%d ", scb_index);
8413 scb_index = ahd_inw(ahd, SCB_NEXT_COMPLETE);
8414 }
8415 printf("\n");
8416 ahd_set_scbptr(ahd, saved_scb_index);
8417 dffstat = ahd_inb(ahd, DFFSTAT);
8418 for (i = 0; i < 2; i++) {
8419 #ifdef AHD_DEBUG
8420 struct scb *fifo_scb;
8421 #endif
8422 u_int fifo_scbptr;
8423
8424 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
8425 fifo_scbptr = ahd_get_scbptr(ahd);
8426 printf("\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
8427 ahd_name(ahd), i,
8428 (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
8429 ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
8430 cur_col = 0;
8431 ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
8432 ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
8433 ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
8434 ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
8435 ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
8436 &cur_col, 50);
8437 ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
8438 ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
8439 ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
8440 ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
8441 if (cur_col > 50) {
8442 printf("\n");
8443 cur_col = 0;
8444 }
8445 printf("\nSHADDR = 0x%x%x, SHCNT = 0x%x ",
8446 ahd_inl(ahd, SHADDR+4),
8447 ahd_inl(ahd, SHADDR),
8448 (ahd_inb(ahd, SHCNT)
8449 | (ahd_inb(ahd, SHCNT + 1) << 8)
8450 | (ahd_inb(ahd, SHCNT + 2) << 16)));
8451 printf("HADDR = 0x%x%x, HCNT = 0x%x \n",
8452 ahd_inl(ahd, HADDR+4),
8453 ahd_inl(ahd, HADDR),
8454 (ahd_inb(ahd, HCNT)
8455 | (ahd_inb(ahd, HCNT + 1) << 8)
8456 | (ahd_inb(ahd, HCNT + 2) << 16)));
8457 ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
8458 #ifdef AHD_DEBUG
8459 if ((ahd_debug & AHD_SHOW_SG) != 0) {
8460 fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
8461 if (fifo_scb != NULL)
8462 ahd_dump_sglist(fifo_scb);
8463 }
8464 #endif
8465 }
8466 printf("\nLQIN: ");
8467 for (i = 0; i < 20; i++)
8468 printf("0x%x ", ahd_inb(ahd, LQIN + i));
8469 printf("\n");
8470 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
8471 printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
8472 ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
8473 ahd_inb(ahd, OPTIONMODE));
8474 printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
8475 ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
8476 ahd_inb(ahd, MAXCMDCNT));
8477 ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
8478 printf("\n");
8479 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
8480 cur_col = 0;
8481 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
8482 printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
8483 ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
8484 ahd_inw(ahd, DINDEX));
8485 printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
8486 ahd_name(ahd), ahd_get_scbptr(ahd), ahd_inw(ahd, SCB_NEXT),
8487 ahd_inw(ahd, SCB_NEXT2));
8488 printf("CDB %x %x %x %x %x %x\n",
8489 ahd_inb(ahd, SCB_CDB_STORE),
8490 ahd_inb(ahd, SCB_CDB_STORE+1),
8491 ahd_inb(ahd, SCB_CDB_STORE+2),
8492 ahd_inb(ahd, SCB_CDB_STORE+3),
8493 ahd_inb(ahd, SCB_CDB_STORE+4),
8494 ahd_inb(ahd, SCB_CDB_STORE+5));
8495 printf("STACK:");
8496 for (i = 0; i < ahd->stack_size; i++) {
8497 ahd->saved_stack[i] =
8498 ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
8499 printf(" 0x%x", ahd->saved_stack[i]);
8500 }
8501 for (i = ahd->stack_size-1; i >= 0; i--) {
8502 ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
8503 ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
8504 }
8505 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
8506 ahd_platform_dump_card_state(ahd);
8507 ahd_restore_modes(ahd, saved_modes);
8508 if (paused == 0)
8509 ahd_unpause(ahd);
8510 }
8511
8512 void
8513 ahd_dump_scbs(struct ahd_softc *ahd)
8514 {
8515 ahd_mode_state saved_modes;
8516 u_int saved_scb_index;
8517 int i;
8518
8519 saved_modes = ahd_save_modes(ahd);
8520 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8521 saved_scb_index = ahd_get_scbptr(ahd);
8522 for (i = 0; i < AHD_SCB_MAX; i++) {
8523 ahd_set_scbptr(ahd, i);
8524 printf("%3d", i);
8525 printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
8526 ahd_inb(ahd, SCB_CONTROL),
8527 ahd_inb(ahd, SCB_SCSIID), ahd_inw(ahd, SCB_NEXT),
8528 ahd_inw(ahd, SCB_NEXT2), ahd_inl(ahd, SCB_SGPTR),
8529 ahd_inl(ahd, SCB_RESIDUAL_SGPTR));
8530 }
8531 printf("\n");
8532 ahd_set_scbptr(ahd, saved_scb_index);
8533 ahd_restore_modes(ahd, saved_modes);
8534 }
8535
8536 /**************************** Flexport Logic **********************************/
8537 /*
8538 * Read count 16bit words from 16bit word address start_addr from the
8539 * SEEPROM attached to the controller, into buf, using the controller's
8540 * SEEPROM reading state machine. Optionally treat the data as a byte
8541 * stream in terms of byte order.
8542 */
8543 int
8544 ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
8545 u_int start_addr, u_int count, int bytestream)
8546 {
8547 u_int cur_addr;
8548 u_int end_addr;
8549 int error;
8550
8551 /*
8552 * If we never make it through the loop even once,
8553 * we were passed invalid arguments.
8554 */
8555 error = EINVAL;
8556 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8557 end_addr = start_addr + count;
8558 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
8559
8560 ahd_outb(ahd, SEEADR, cur_addr);
8561 ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
8562
8563 error = ahd_wait_seeprom(ahd);
8564 if (error) {
8565 printf("%s: ahd_wait_seeprom timed out\n", ahd_name(ahd));
8566 break;
8567 }
8568 if (bytestream != 0) {
8569 uint8_t *bytestream_ptr;
8570
8571 bytestream_ptr = (uint8_t *)buf;
8572 *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
8573 *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
8574 } else {
8575 /*
8576 * ahd_inw() already handles machine byte order.
8577 */
8578 *buf = ahd_inw(ahd, SEEDAT);
8579 }
8580 buf++;
8581 }
8582 return (error);
8583 }
8584
8585 /*
8586 * Write count 16bit words from buf, into SEEPROM attache to the
8587 * controller starting at 16bit word address start_addr, using the
8588 * controller's SEEPROM writing state machine.
8589 */
8590 int
8591 ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
8592 u_int start_addr, u_int count)
8593 {
8594 u_int cur_addr;
8595 u_int end_addr;
8596 int error;
8597 int retval;
8598
8599 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8600 error = ENOENT;
8601
8602 /* Place the chip into write-enable mode */
8603 ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
8604 ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
8605 error = ahd_wait_seeprom(ahd);
8606 if (error)
8607 return (error);
8608
8609 /*
8610 * Write the data. If we don't get throught the loop at
8611 * least once, the arguments were invalid.
8612 */
8613 retval = EINVAL;
8614 end_addr = start_addr + count;
8615 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
8616 ahd_outw(ahd, SEEDAT, *buf++);
8617 ahd_outb(ahd, SEEADR, cur_addr);
8618 ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
8619
8620 retval = ahd_wait_seeprom(ahd);
8621 if (retval)
8622 break;
8623 }
8624
8625 /*
8626 * Disable writes.
8627 */
8628 ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
8629 ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
8630 error = ahd_wait_seeprom(ahd);
8631 if (error)
8632 return (error);
8633 return (retval);
8634 }
8635
8636 /*
8637 * Wait ~100us for the serial eeprom to satisfy our request.
8638 */
8639 int
8640 ahd_wait_seeprom(struct ahd_softc *ahd)
8641 {
8642 int cnt;
8643
8644 cnt = 2000;
8645 while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
8646 ahd_delay(5);
8647
8648 if (cnt == 0)
8649 return (ETIMEDOUT);
8650 return (0);
8651 }
8652
8653 /*
8654 * Validate the two checksums in the per_channel
8655 * vital product data struct.
8656 */
8657 int
8658 ahd_verify_vpd_cksum(struct vpd_config *vpd)
8659 {
8660 int i;
8661 int maxaddr;
8662 uint32_t checksum;
8663 uint8_t *vpdarray;
8664
8665 vpdarray = (uint8_t *)vpd;
8666 maxaddr = offsetof(struct vpd_config, vpd_checksum);
8667 checksum = 0;
8668 for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
8669 checksum = checksum + vpdarray[i];
8670 if (checksum == 0
8671 || (-checksum & 0xFF) != vpd->vpd_checksum)
8672 return (0);
8673
8674 checksum = 0;
8675 maxaddr = offsetof(struct vpd_config, checksum);
8676 for (i = offsetof(struct vpd_config, default_target_flags);
8677 i < maxaddr; i++)
8678 checksum = checksum + vpdarray[i];
8679 if (checksum == 0
8680 || (-checksum & 0xFF) != vpd->checksum)
8681 return (0);
8682 return (1);
8683 }
8684
8685 int
8686 ahd_verify_cksum(struct seeprom_config *sc)
8687 {
8688 int i;
8689 int maxaddr;
8690 uint32_t checksum;
8691 uint16_t *scarray;
8692
8693 maxaddr = (sizeof(*sc)/2) - 1;
8694 checksum = 0;
8695 scarray = (uint16_t *)sc;
8696
8697 for (i = 0; i < maxaddr; i++)
8698 checksum = checksum + scarray[i];
8699 if (checksum == 0
8700 || (checksum & 0xFFFF) != sc->checksum) {
8701 return (0);
8702 } else {
8703 return (1);
8704 }
8705 }
8706
8707 int
8708 ahd_acquire_seeprom(struct ahd_softc *ahd)
8709 {
8710 /*
8711 * We should be able to determine the SEEPROM type
8712 * from the flexport logic, but unfortunately not
8713 * all implementations have this logic and there is
8714 * no programatic method for determining if the logic
8715 * is present.
8716 */
8717
8718 return (1);
8719 #if 0
8720 uint8_t seetype;
8721 int error;
8722
8723 error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
8724 if (error != 0
8725 || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
8726 return (0);
8727 return (1);
8728 #endif
8729 }
8730
8731 void
8732 ahd_release_seeprom(struct ahd_softc *ahd)
8733 {
8734 /* Currently a no-op */
8735 }
8736
8737 int
8738 ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
8739 {
8740 int error;
8741
8742 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8743 if (addr > 7)
8744 panic("ahd_write_flexport: address out of range");
8745 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
8746 error = ahd_wait_flexport(ahd);
8747 if (error != 0)
8748 return (error);
8749 ahd_outb(ahd, BRDDAT, value);
8750 ahd_flush_device_writes(ahd);
8751 ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
8752 ahd_flush_device_writes(ahd);
8753 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
8754 ahd_flush_device_writes(ahd);
8755 ahd_outb(ahd, BRDCTL, 0);
8756 ahd_flush_device_writes(ahd);
8757 return (0);
8758 }
8759
8760 int
8761 ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
8762 {
8763 int error;
8764
8765 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8766 if (addr > 7)
8767 panic("ahd_read_flexport: address out of range");
8768 ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
8769 error = ahd_wait_flexport(ahd);
8770 if (error != 0)
8771 return (error);
8772 *value = ahd_inb(ahd, BRDDAT);
8773 ahd_outb(ahd, BRDCTL, 0);
8774 ahd_flush_device_writes(ahd);
8775 return (0);
8776 }
8777
8778 /*
8779 * Wait at most 2 seconds for flexport arbitration to succeed.
8780 */
8781 int
8782 ahd_wait_flexport(struct ahd_softc *ahd)
8783 {
8784 int cnt;
8785
8786 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8787 cnt = 1000000 * 2 / 5;
8788 while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
8789 ahd_delay(5);
8790
8791 if (cnt == 0)
8792 return (ETIMEDOUT);
8793 return (0);
8794 }
8795
8796 /************************* Target Mode ****************************************/
8797 #ifdef AHD_TARGET_MODE
8798 cam_status
8799 ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
8800 struct ahd_tmode_tstate **tstate,
8801 struct ahd_tmode_lstate **lstate,
8802 int notfound_failure)
8803 {
8804
8805 if ((ahd->features & AHD_TARGETMODE) == 0)
8806 return (CAM_REQ_INVALID);
8807
8808 /*
8809 * Handle the 'black hole' device that sucks up
8810 * requests to unattached luns on enabled targets.
8811 */
8812 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
8813 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
8814 *tstate = NULL;
8815 *lstate = ahd->black_hole;
8816 } else {
8817 u_int max_id;
8818
8819 max_id = (ahd->features & AHD_WIDE) ? 15 : 7;
8820 if (ccb->ccb_h.target_id > max_id)
8821 return (CAM_TID_INVALID);
8822
8823 if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
8824 return (CAM_LUN_INVALID);
8825
8826 *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
8827 *lstate = NULL;
8828 if (*tstate != NULL)
8829 *lstate =
8830 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
8831 }
8832
8833 if (notfound_failure != 0 && *lstate == NULL)
8834 return (CAM_PATH_INVALID);
8835
8836 return (CAM_REQ_CMP);
8837 }
8838
8839 void
8840 ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
8841 {
8842 #if NOT_YET
8843 struct ahd_tmode_tstate *tstate;
8844 struct ahd_tmode_lstate *lstate;
8845 struct ccb_en_lun *cel;
8846 cam_status status;
8847 u_int target;
8848 u_int lun;
8849 u_int target_mask;
8850 u_long s;
8851 char channel;
8852
8853 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
8854 /*notfound_failure*/FALSE);
8855
8856 if (status != CAM_REQ_CMP) {
8857 ccb->ccb_h.status = status;
8858 return;
8859 }
8860
8861 if ((ahd->features & AHD_MULTIROLE) != 0) {
8862 u_int our_id;
8863
8864 our_id = ahd->our_id;
8865 if (ccb->ccb_h.target_id != our_id) {
8866 if ((ahd->features & AHD_MULTI_TID) != 0
8867 && (ahd->flags & AHD_INITIATORROLE) != 0) {
8868 /*
8869 * Only allow additional targets if
8870 * the initiator role is disabled.
8871 * The hardware cannot handle a re-select-in
8872 * on the initiator id during a re-select-out
8873 * on a different target id.
8874 */
8875 status = CAM_TID_INVALID;
8876 } else if ((ahd->flags & AHD_INITIATORROLE) != 0
8877 || ahd->enabled_luns > 0) {
8878 /*
8879 * Only allow our target id to change
8880 * if the initiator role is not configured
8881 * and there are no enabled luns which
8882 * are attached to the currently registered
8883 * scsi id.
8884 */
8885 status = CAM_TID_INVALID;
8886 }
8887 }
8888 }
8889
8890 if (status != CAM_REQ_CMP) {
8891 ccb->ccb_h.status = status;
8892 return;
8893 }
8894
8895 /*
8896 * We now have an id that is valid.
8897 * If we aren't in target mode, switch modes.
8898 */
8899 if ((ahd->flags & AHD_TARGETROLE) == 0
8900 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
8901 u_long s;
8902
8903 printf("Configuring Target Mode\n");
8904 ahd_lock(ahd, &s);
8905 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
8906 ccb->ccb_h.status = CAM_BUSY;
8907 ahd_unlock(ahd, &s);
8908 return;
8909 }
8910 ahd->flags |= AHD_TARGETROLE;
8911 if ((ahd->features & AHD_MULTIROLE) == 0)
8912 ahd->flags &= ~AHD_INITIATORROLE;
8913 ahd_pause(ahd);
8914 ahd_loadseq(ahd);
8915 ahd_restart(ahd);
8916 ahd_unlock(ahd, &s);
8917 }
8918 cel = &ccb->cel;
8919 target = ccb->ccb_h.target_id;
8920 lun = ccb->ccb_h.target_lun;
8921 channel = SIM_CHANNEL(ahd, sim);
8922 target_mask = 0x01 << target;
8923 if (channel == 'B')
8924 target_mask <<= 8;
8925
8926 if (cel->enable != 0) {
8927 u_int scsiseq1;
8928
8929 /* Are we already enabled?? */
8930 if (lstate != NULL) {
8931 xpt_print_path(ccb->ccb_h.path);
8932 printf("Lun already enabled\n");
8933 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
8934 return;
8935 }
8936
8937 if (cel->grp6_len != 0
8938 || cel->grp7_len != 0) {
8939 /*
8940 * Don't (yet?) support vendor
8941 * specific commands.
8942 */
8943 ccb->ccb_h.status = CAM_REQ_INVALID;
8944 printf("Non-zero Group Codes\n");
8945 return;
8946 }
8947
8948 /*
8949 * Seems to be okay.
8950 * Setup our data structures.
8951 */
8952 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
8953 tstate = ahd_alloc_tstate(ahd, target, channel);
8954 if (tstate == NULL) {
8955 xpt_print_path(ccb->ccb_h.path);
8956 printf("Couldn't allocate tstate\n");
8957 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
8958 return;
8959 }
8960 }
8961 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
8962 if (lstate == NULL) {
8963 xpt_print_path(ccb->ccb_h.path);
8964 printf("Couldn't allocate lstate\n");
8965 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
8966 return;
8967 }
8968 memset(lstate, 0, sizeof(*lstate));
8969 status = xpt_create_path(&lstate->path, /*periph*/NULL,
8970 xpt_path_path_id(ccb->ccb_h.path),
8971 xpt_path_target_id(ccb->ccb_h.path),
8972 xpt_path_lun_id(ccb->ccb_h.path));
8973 if (status != CAM_REQ_CMP) {
8974 free(lstate, M_DEVBUF);
8975 xpt_print_path(ccb->ccb_h.path);
8976 printf("Couldn't allocate path\n");
8977 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
8978 return;
8979 }
8980 SLIST_INIT(&lstate->accept_tios);
8981 SLIST_INIT(&lstate->immed_notifies);
8982 ahd_lock(ahd, &s);
8983 ahd_pause(ahd);
8984 if (target != CAM_TARGET_WILDCARD) {
8985 tstate->enabled_luns[lun] = lstate;
8986 ahd->enabled_luns++;
8987
8988 if ((ahd->features & AHD_MULTI_TID) != 0) {
8989 u_int targid_mask;
8990
8991 targid_mask = ahd_inb(ahd, TARGID)
8992 | (ahd_inb(ahd, TARGID + 1) << 8);
8993
8994 targid_mask |= target_mask;
8995 ahd_outb(ahd, TARGID, targid_mask);
8996 ahd_outb(ahd, TARGID+1, (targid_mask >> 8));
8997
8998 ahd_update_scsiid(ahd, targid_mask);
8999 } else {
9000 u_int our_id;
9001 char channel;
9002
9003 channel = SIM_CHANNEL(ahd, sim);
9004 our_id = SIM_SCSI_ID(ahd, sim);
9005
9006 /*
9007 * This can only happen if selections
9008 * are not enabled
9009 */
9010 if (target != our_id) {
9011 u_int sblkctl;
9012 char cur_channel;
9013 int swap;
9014
9015 sblkctl = ahd_inb(ahd, SBLKCTL);
9016 cur_channel = (sblkctl & SELBUSB)
9017 ? 'B' : 'A';
9018 if ((ahd->features & AHD_TWIN) == 0)
9019 cur_channel = 'A';
9020 swap = cur_channel != channel;
9021 ahd->our_id = target;
9022
9023 if (swap)
9024 ahd_outb(ahd, SBLKCTL,
9025 sblkctl ^ SELBUSB);
9026
9027 ahd_outb(ahd, SCSIID, target);
9028
9029 if (swap)
9030 ahd_outb(ahd, SBLKCTL, sblkctl);
9031 }
9032 }
9033 } else
9034 ahd->black_hole = lstate;
9035 /* Allow select-in operations */
9036 if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
9037 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
9038 scsiseq1 |= ENSELI;
9039 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
9040 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
9041 scsiseq1 |= ENSELI;
9042 ahd_outb(ahd, SCSISEQ1, scsiseq1);
9043 }
9044 ahd_unpause(ahd);
9045 ahd_unlock(ahd, &s);
9046 ccb->ccb_h.status = CAM_REQ_CMP;
9047 xpt_print_path(ccb->ccb_h.path);
9048 printf("Lun now enabled for target mode\n");
9049 } else {
9050 struct scb *scb;
9051 int i, empty;
9052
9053 if (lstate == NULL) {
9054 ccb->ccb_h.status = CAM_LUN_INVALID;
9055 return;
9056 }
9057
9058 ahd_lock(ahd, &s);
9059
9060 ccb->ccb_h.status = CAM_REQ_CMP;
9061 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
9062 struct ccb_hdr *ccbh;
9063
9064 ccbh = &scb->io_ctx->ccb_h;
9065 if (ccbh->func_code == XPT_CONT_TARGET_IO
9066 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
9067 printf("CTIO pending\n");
9068 ccb->ccb_h.status = CAM_REQ_INVALID;
9069 ahd_unlock(ahd, &s);
9070 return;
9071 }
9072 }
9073
9074 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
9075 printf("ATIOs pending\n");
9076 ccb->ccb_h.status = CAM_REQ_INVALID;
9077 }
9078
9079 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
9080 printf("INOTs pending\n");
9081 ccb->ccb_h.status = CAM_REQ_INVALID;
9082 }
9083
9084 if (ccb->ccb_h.status != CAM_REQ_CMP) {
9085 ahd_unlock(ahd, &s);
9086 return;
9087 }
9088
9089 xpt_print_path(ccb->ccb_h.path);
9090 printf("Target mode disabled\n");
9091 xpt_free_path(lstate->path);
9092 free(lstate, M_DEVBUF);
9093
9094 ahd_pause(ahd);
9095 /* Can we clean up the target too? */
9096 if (target != CAM_TARGET_WILDCARD) {
9097 tstate->enabled_luns[lun] = NULL;
9098 ahd->enabled_luns--;
9099 for (empty = 1, i = 0; i < 8; i++)
9100 if (tstate->enabled_luns[i] != NULL) {
9101 empty = 0;
9102 break;
9103 }
9104
9105 if (empty) {
9106 ahd_free_tstate(ahd, target, channel,
9107 /*force*/FALSE);
9108 if (ahd->features & AHD_MULTI_TID) {
9109 u_int targid_mask;
9110
9111 targid_mask = ahd_inb(ahd, TARGID)
9112 | (ahd_inb(ahd, TARGID + 1)
9113 << 8);
9114
9115 targid_mask &= ~target_mask;
9116 ahd_outb(ahd, TARGID, targid_mask);
9117 ahd_outb(ahd, TARGID+1,
9118 (targid_mask >> 8));
9119 ahd_update_scsiid(ahd, targid_mask);
9120 }
9121 }
9122 } else {
9123
9124 ahd->black_hole = NULL;
9125
9126 /*
9127 * We can't allow selections without
9128 * our black hole device.
9129 */
9130 empty = TRUE;
9131 }
9132 if (ahd->enabled_luns == 0) {
9133 /* Disallow select-in */
9134 u_int scsiseq1;
9135
9136 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
9137 scsiseq1 &= ~ENSELI;
9138 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
9139 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
9140 scsiseq1 &= ~ENSELI;
9141 ahd_outb(ahd, SCSISEQ1, scsiseq1);
9142
9143 if ((ahd->features & AHD_MULTIROLE) == 0) {
9144 printf("Configuring Initiator Mode\n");
9145 ahd->flags &= ~AHD_TARGETROLE;
9146 ahd->flags |= AHD_INITIATORROLE;
9147 ahd_pause(ahd);
9148 ahd_loadseq(ahd);
9149 ahd_restart(ahd);
9150 /*
9151 * Unpaused. The extra unpause
9152 * that follows is harmless.
9153 */
9154 }
9155 }
9156 ahd_unpause(ahd);
9157 ahd_unlock(ahd, &s);
9158 }
9159 #endif
9160 }
9161
9162 static void
9163 ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
9164 {
9165 #if NOT_YET
9166 u_int scsiid_mask;
9167 u_int scsiid;
9168
9169 if ((ahd->features & AHD_MULTI_TID) == 0)
9170 panic("ahd_update_scsiid called on non-multitid unit\n");
9171
9172 /*
9173 * Since we will rely on the TARGID mask
9174 * for selection enables, ensure that OID
9175 * in SCSIID is not set to some other ID
9176 * that we don't want to allow selections on.
9177 */
9178 if ((ahd->features & AHD_ULTRA2) != 0)
9179 scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
9180 else
9181 scsiid = ahd_inb(ahd, SCSIID);
9182 scsiid_mask = 0x1 << (scsiid & OID);
9183 if ((targid_mask & scsiid_mask) == 0) {
9184 u_int our_id;
9185
9186 /* ffs counts from 1 */
9187 our_id = ffs(targid_mask);
9188 if (our_id == 0)
9189 our_id = ahd->our_id;
9190 else
9191 our_id--;
9192 scsiid &= TID;
9193 scsiid |= our_id;
9194 }
9195 if ((ahd->features & AHD_ULTRA2) != 0)
9196 ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
9197 else
9198 ahd_outb(ahd, SCSIID, scsiid);
9199 #endif
9200 }
9201
9202 #ifdef AHD_TARGET_MODE
9203 void
9204 ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
9205 {
9206 struct target_cmd *cmd;
9207
9208 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
9209 while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
9210
9211 /*
9212 * Only advance through the queue if we
9213 * have the resources to process the command.
9214 */
9215 if (ahd_handle_target_cmd(ahd, cmd) != 0)
9216 break;
9217
9218 cmd->cmd_valid = 0;
9219 ahd_dmamap_sync(ahd, ahd->parent_dmat /*shared_data_dmat*/,
9220 ahd->shared_data_map.dmamap,
9221 ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
9222 sizeof(struct target_cmd),
9223 BUS_DMASYNC_PREREAD);
9224 ahd->tqinfifonext++;
9225
9226 /*
9227 * Lazily update our position in the target mode incoming
9228 * command queue as seen by the sequencer.
9229 */
9230 if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
9231 u_int hs_mailbox;
9232
9233 hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
9234 hs_mailbox &= ~HOST_TQINPOS;
9235 hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
9236 ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
9237 }
9238 }
9239 }
9240 #endif
9241
9242 static int
9243 ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
9244 {
9245 struct ahd_tmode_tstate *tstate;
9246 struct ahd_tmode_lstate *lstate;
9247 struct ccb_accept_tio *atio;
9248 uint8_t *byte;
9249 int initiator;
9250 int target;
9251 int lun;
9252
9253 initiator = SCSIID_TARGET(ahd, cmd->scsiid);
9254 target = SCSIID_OUR_ID(cmd->scsiid);
9255 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
9256
9257 byte = cmd->bytes;
9258 tstate = ahd->enabled_targets[target];
9259 lstate = NULL;
9260 if (tstate != NULL)
9261 lstate = tstate->enabled_luns[lun];
9262
9263 /*
9264 * Commands for disabled luns go to the black hole driver.
9265 */
9266 if (lstate == NULL)
9267 lstate = ahd->black_hole;
9268
9269 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
9270 if (atio == NULL) {
9271 ahd->flags |= AHD_TQINFIFO_BLOCKED;
9272 /*
9273 * Wait for more ATIOs from the peripheral driver for this lun.
9274 */
9275 return (1);
9276 } else
9277 ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
9278 #ifdef AHD_DEBUG
9279 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
9280 printf("%s: incoming command from %d for %d:%d%s\n",
9281 ahd_name(ahd),
9282 initiator, target, lun,
9283 lstate == ahd->black_hole ? "(Black Holed)" : "");
9284 #endif
9285 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
9286
9287 if (lstate == ahd->black_hole) {
9288 /* Fill in the wildcards */
9289 atio->ccb_h.target_id = target;
9290 atio->ccb_h.target_lun = lun;
9291 }
9292
9293 /*
9294 * Package it up and send it off to
9295 * whomever has this lun enabled.
9296 */
9297 atio->sense_len = 0;
9298 atio->init_id = initiator;
9299 if (byte[0] != 0xFF) {
9300 /* Tag was included */
9301 atio->tag_action = *byte++;
9302 atio->tag_id = *byte++;
9303 atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
9304 } else {
9305 atio->ccb_h.flags = 0;
9306 }
9307 byte++;
9308
9309 /* Okay. Now determine the cdb size based on the command code */
9310 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
9311 case 0:
9312 atio->cdb_len = 6;
9313 break;
9314 case 1:
9315 case 2:
9316 atio->cdb_len = 10;
9317 break;
9318 case 4:
9319 atio->cdb_len = 16;
9320 break;
9321 case 5:
9322 atio->cdb_len = 12;
9323 break;
9324 case 3:
9325 default:
9326 /* Only copy the opcode. */
9327 atio->cdb_len = 1;
9328 printf("Reserved or VU command code type encountered\n");
9329 break;
9330 }
9331
9332 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
9333
9334 atio->ccb_h.status |= CAM_CDB_RECVD;
9335
9336 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
9337 /*
9338 * We weren't allowed to disconnect.
9339 * We're hanging on the bus until a
9340 * continue target I/O comes in response
9341 * to this accept tio.
9342 */
9343 #ifdef AHD_DEBUG
9344 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
9345 printf("Received Immediate Command %d:%d:%d - %p\n",
9346 initiator, target, lun, ahd->pending_device);
9347 #endif
9348 ahd->pending_device = lstate;
9349 ahd_freeze_ccb((union ccb *)atio);
9350 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
9351 }
9352 xpt_done((union ccb*)atio);
9353 return (0);
9354 }
9355
9356 #endif
9357
9358 static int
9359 ahd_createdmamem(tag, size, flags, mapp, vaddr, baddr, seg, nseg, myname, what)
9360 bus_dma_tag_t tag;
9361 int size;
9362 int flags;
9363 bus_dmamap_t *mapp;
9364 caddr_t *vaddr;
9365 bus_addr_t *baddr;
9366 bus_dma_segment_t *seg;
9367 int *nseg;
9368 const char *myname, *what;
9369 {
9370 int error, level = 0;
9371
9372 if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
9373 seg, 1, nseg, BUS_DMA_NOWAIT)) != 0) {
9374 printf("%s: failed to allocate DMA mem for %s, error = %d\n",
9375 myname, what, error);
9376 goto out;
9377 }
9378 level++;
9379
9380 if ((error = bus_dmamem_map(tag, seg, *nseg, size, vaddr,
9381 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
9382 printf("%s: failed to map DMA mem for %s, error = %d\n",
9383 myname, what, error);
9384 goto out;
9385 }
9386 level++;
9387
9388 if ((error = bus_dmamap_create(tag, size, 1, size, 0,
9389 BUS_DMA_NOWAIT | flags, mapp)) != 0) {
9390 printf("%s: failed to create DMA map for %s, error = %d\n",
9391 myname, what, error);
9392 goto out;
9393 }
9394 level++;
9395
9396
9397 if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
9398 BUS_DMA_NOWAIT)) != 0) {
9399 printf("%s: failed to load DMA map for %s, error = %d\n",
9400 myname, what, error);
9401 goto out;
9402 }
9403
9404 *baddr = (*mapp)->dm_segs[0].ds_addr;
9405
9406 return 0;
9407 out:
9408 printf("ahd_createdmamem error (%d)\n", level);
9409 switch (level) {
9410 case 3:
9411 bus_dmamap_destroy(tag, *mapp);
9412 /* FALLTHROUGH */
9413 case 2:
9414 bus_dmamem_unmap(tag, *vaddr, size);
9415 /* FALLTHROUGH */
9416 case 1:
9417 bus_dmamem_free(tag, seg, *nseg);
9418 break;
9419 default:
9420 break;
9421 }
9422
9423 return error;
9424 }
9425
9426 static void
9427 ahd_freedmamem(tag, size, map, vaddr, seg, nseg)
9428 bus_dma_tag_t tag;
9429 int size;
9430 bus_dmamap_t map;
9431 caddr_t vaddr;
9432 bus_dma_segment_t *seg;
9433 int nseg;
9434 {
9435
9436 bus_dmamap_unload(tag, map);
9437 bus_dmamap_destroy(tag, map);
9438 bus_dmamem_unmap(tag, vaddr, size);
9439 bus_dmamem_free(tag, seg, nseg);
9440 }
9441
9442 static void
9443 ahd_update_xfer_mode(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
9444 {
9445 struct scsipi_xfer_mode xm;
9446 struct ahd_initiator_tinfo *tinfo;
9447 struct ahd_tmode_tstate *tstate;
9448
9449 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
9450 devinfo->target, &tstate);
9451
9452 xm.xm_target = devinfo->target;
9453 xm.xm_mode = 0;
9454 xm.xm_period = tinfo->curr.period;
9455 xm.xm_offset = tinfo->curr.offset;
9456 if (tinfo->curr.width == 1)
9457 xm.xm_mode |= PERIPH_CAP_WIDE16;
9458 if (tinfo->curr.period)
9459 xm.xm_mode |= PERIPH_CAP_SYNC;
9460 if (tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ)
9461 xm.xm_mode |= PERIPH_CAP_DT;
9462 if (tstate->tagenable & devinfo->target_mask)
9463 xm.xm_mode |= PERIPH_CAP_TQING;
9464
9465 tinfo->goal.width = tinfo->curr.width;
9466 tinfo->goal.period = tinfo->curr.period;
9467 tinfo->goal.offset = tinfo->curr.offset;
9468 tinfo->goal.ppr_options = tinfo->curr.ppr_options;
9469
9470 ahd_update_neg_request(ahd, devinfo, tstate,
9471 tinfo, AHD_NEG_TO_GOAL);
9472
9473 scsipi_async_event(&ahd->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
9474 }
9475