aic79xx.c revision 1.9 1 /* $NetBSD: aic79xx.c,v 1.9 2003/08/29 00:10:03 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 * //depot/aic7xxx/aic7xxx/aic79xx.c#190 $
43 *
44 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx.c,v 1.12 2003/05/04 00:20:07 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.9 2003/08/29 00:10:03 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, DATA_COUNT_ODD) == 1) {
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 |=
4450 (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 3) << 24)
4451 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 2) << 16)
4452 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 1) << 8);
4453 sgptr &= SG_PTR_MASK;
4454 data_cnt =
4455 (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+3) << 24)
4456 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+2) << 16)
4457 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT+1) << 8)
4458 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT));
4459
4460 data_addr = (((uint64_t)ahd_inb(ahd, SHADDR + 7)) << 56)
4461 | (((uint64_t)ahd_inb(ahd, SHADDR + 6)) << 48)
4462 | (((uint64_t)ahd_inb(ahd, SHADDR + 5)) << 40)
4463 | (((uint64_t)ahd_inb(ahd, SHADDR + 4)) << 32)
4464 | (ahd_inb(ahd, SHADDR + 3) << 24)
4465 | (ahd_inb(ahd, SHADDR + 2) << 16)
4466 | (ahd_inb(ahd, SHADDR + 1) << 8)
4467 | (ahd_inb(ahd, SHADDR));
4468
4469 data_cnt += 1;
4470 data_addr -= 1;
4471
4472 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
4473 struct ahd_dma64_seg *sg;
4474
4475 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4476
4477 /*
4478 * The residual sg ptr points to the next S/G
4479 * to load so we must go back one.
4480 */
4481 sg--;
4482 sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
4483 if (sg != scb->sg_list
4484 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
4485
4486 sg--;
4487 sglen = ahd_le32toh(sg->len);
4488 /*
4489 * Preserve High Address and SG_LIST
4490 * bits while setting the count to 1.
4491 */
4492 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
4493 data_addr = ahd_le64toh(sg->addr)
4494 + (sglen & AHD_SG_LEN_MASK)
4495 - 1;
4496
4497 /*
4498 * Increment sg so it points to the
4499 * "next" sg.
4500 */
4501 sg++;
4502 sgptr = ahd_sg_virt_to_bus(ahd, scb,
4503 sg);
4504 }
4505 } else {
4506 struct ahd_dma_seg *sg;
4507
4508 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4509
4510 /*
4511 * The residual sg ptr points to the next S/G
4512 * to load so we must go back one.
4513 */
4514 sg--;
4515 sglen = ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
4516 if (sg != scb->sg_list
4517 && sglen < (data_cnt & AHD_SG_LEN_MASK)) {
4518
4519 sg--;
4520 sglen = ahd_le32toh(sg->len);
4521 /*
4522 * Preserve High Address and SG_LIST
4523 * bits while setting the count to 1.
4524 */
4525 data_cnt = 1|(sglen&(~AHD_SG_LEN_MASK));
4526 data_addr = ahd_le32toh(sg->addr)
4527 + (sglen & AHD_SG_LEN_MASK)
4528 - 1;
4529
4530 /*
4531 * Increment sg so it points to the
4532 * "next" sg.
4533 */
4534 sg++;
4535 sgptr = ahd_sg_virt_to_bus(ahd, scb,
4536 sg);
4537 }
4538 }
4539 ahd_outb(ahd, SCB_RESIDUAL_SGPTR + 3, sgptr >> 24);
4540 ahd_outb(ahd, SCB_RESIDUAL_SGPTR + 2, sgptr >> 16);
4541 ahd_outb(ahd, SCB_RESIDUAL_SGPTR + 1, sgptr >> 8);
4542 ahd_outb(ahd, SCB_RESIDUAL_SGPTR, sgptr);
4543
4544 ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 3, data_cnt >> 24);
4545 ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 2, data_cnt >> 16);
4546 ahd_outb(ahd, SCB_RESIDUAL_DATACNT + 1, data_cnt >> 8);
4547 ahd_outb(ahd, SCB_RESIDUAL_DATACNT, data_cnt);
4548
4549 /*
4550 * The FIFO's pointers will be updated if/when the
4551 * sequencer re-enters a data phase.
4552 */
4553 }
4554 }
4555 }
4556
4557
4558 /*
4559 * Reinitialize the data pointers for the active transfer
4560 * based on its current residual.
4561 */
4562 static void
4563 ahd_reinitialize_dataptrs(struct ahd_softc *ahd)
4564 {
4565 struct scb *scb;
4566 ahd_mode_state saved_modes;
4567 u_int scb_index;
4568 u_int wait;
4569 uint32_t sgptr;
4570 uint32_t resid;
4571 uint64_t dataptr;
4572
4573 AHD_ASSERT_MODES(ahd, AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK,
4574 AHD_MODE_DFF0_MSK|AHD_MODE_DFF1_MSK);
4575
4576 scb_index = ahd_get_scbptr(ahd);
4577 scb = ahd_lookup_scb(ahd, scb_index);
4578
4579 /*
4580 * Release and reacquire the FIFO so we
4581 * have a clean slate.
4582 */
4583 ahd_outb(ahd, DFFSXFRCTL, CLRCHN);
4584 wait = 1000;
4585 do {
4586 ahd_delay(100);
4587 } while (--wait && !(ahd_inb(ahd, MDFFSTAT) & FIFOFREE));
4588 if (wait == 0) {
4589 ahd_print_path(ahd, scb);
4590 printf("ahd_reinitialize_dataptrs: Forcing FIFO free.\n");
4591 ahd_outb(ahd, DFFSXFRCTL, RSTCHN|CLRSHCNT);
4592 }
4593 saved_modes = ahd_save_modes(ahd);
4594 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4595 ahd_outb(ahd, DFFSTAT,
4596 ahd_inb(ahd, DFFSTAT)
4597 | (saved_modes == 0x11 ? CURRFIFO_1 : CURRFIFO_0));
4598
4599 /*
4600 * Determine initial values for data_addr and data_cnt
4601 * for resuming the data phase.
4602 */
4603 sgptr = (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 3) << 24)
4604 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 2) << 16)
4605 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR + 1) << 8)
4606 | ahd_inb_scbram(ahd, SCB_RESIDUAL_SGPTR);
4607 sgptr &= SG_PTR_MASK;
4608
4609 resid = (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 2) << 16)
4610 | (ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT + 1) << 8)
4611 | ahd_inb_scbram(ahd, SCB_RESIDUAL_DATACNT);
4612
4613 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0) {
4614 struct ahd_dma64_seg *sg;
4615
4616 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4617
4618 /* The residual sg_ptr always points to the next sg */
4619 sg--;
4620
4621 dataptr = ahd_le64toh(sg->addr)
4622 + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
4623 - resid;
4624 ahd_outb(ahd, HADDR + 7, dataptr >> 56);
4625 ahd_outb(ahd, HADDR + 6, dataptr >> 48);
4626 ahd_outb(ahd, HADDR + 5, dataptr >> 40);
4627 ahd_outb(ahd, HADDR + 4, dataptr >> 32);
4628 } else {
4629 struct ahd_dma_seg *sg;
4630
4631 sg = ahd_sg_bus_to_virt(ahd, scb, sgptr);
4632
4633 /* The residual sg_ptr always points to the next sg */
4634 sg--;
4635
4636 dataptr = ahd_le32toh(sg->addr)
4637 + (ahd_le32toh(sg->len) & AHD_SG_LEN_MASK)
4638 - resid;
4639 ahd_outb(ahd, HADDR + 4,
4640 (ahd_le32toh(sg->len) & ~AHD_SG_LEN_MASK) >> 24);
4641 }
4642 ahd_outb(ahd, HADDR + 3, dataptr >> 24);
4643 ahd_outb(ahd, HADDR + 2, dataptr >> 16);
4644 ahd_outb(ahd, HADDR + 1, dataptr >> 8);
4645 ahd_outb(ahd, HADDR, dataptr);
4646 ahd_outb(ahd, HCNT + 2, resid >> 16);
4647 ahd_outb(ahd, HCNT + 1, resid >> 8);
4648 ahd_outb(ahd, HCNT, resid);
4649 }
4650
4651 /*
4652 * Handle the effects of issuing a bus device reset message.
4653 */
4654 static void
4655 ahd_handle_devreset(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4656 u_int lun, cam_status status, char *message,
4657 int verbose_level)
4658 {
4659 #ifdef AHD_TARGET_MODE
4660 struct ahd_tmode_tstate* tstate;
4661 #endif
4662 int found;
4663
4664 found = ahd_abort_scbs(ahd, devinfo->target, devinfo->channel,
4665 lun, SCB_LIST_NULL, devinfo->role,
4666 status);
4667
4668 #ifdef AHD_TARGET_MODE
4669 /*
4670 * Send an immediate notify ccb to all target mord peripheral
4671 * drivers affected by this action.
4672 */
4673 tstate = ahd->enabled_targets[devinfo->our_scsiid];
4674 if (tstate != NULL) {
4675 u_int cur_lun;
4676 u_int max_lun;
4677
4678 if (lun != CAM_LUN_WILDCARD) {
4679 cur_lun = 0;
4680 max_lun = AHD_NUM_LUNS - 1;
4681 } else {
4682 cur_lun = lun;
4683 max_lun = lun;
4684 }
4685 for (cur_lun <= max_lun; cur_lun++) {
4686 struct ahd_tmode_lstate* lstate;
4687
4688 lstate = tstate->enabled_luns[cur_lun];
4689 if (lstate == NULL)
4690 continue;
4691
4692 ahd_queue_lstate_event(ahd, lstate, devinfo->our_scsiid,
4693 MSG_BUS_DEV_RESET, /*arg*/0);
4694 ahd_send_lstate_events(ahd, lstate);
4695 }
4696 }
4697 #endif
4698
4699 /*
4700 * Go back to async/narrow transfers and renegotiate.
4701 */
4702 ahd_set_width(ahd, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4703 AHD_TRANS_CUR, /*paused*/TRUE);
4704 ahd_set_syncrate(ahd, devinfo, /*period*/0, /*offset*/0,
4705 /*ppr_options*/0, AHD_TRANS_CUR, /*paused*/TRUE);
4706
4707 ahd_send_async(ahd, devinfo->channel, devinfo->target,
4708 lun, AC_SENT_BDR, NULL);
4709
4710 if (message != NULL
4711 && (verbose_level <= bootverbose))
4712 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahd_name(ahd),
4713 message, devinfo->channel, devinfo->target, found);
4714 }
4715
4716 #ifdef AHD_TARGET_MODE
4717 static void
4718 ahd_setup_target_msgin(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
4719 struct scb *scb)
4720 {
4721
4722 /*
4723 * To facilitate adding multiple messages together,
4724 * each routine should increment the index and len
4725 * variables instead of setting them explicitly.
4726 */
4727 ahd->msgout_index = 0;
4728 ahd->msgout_len = 0;
4729
4730 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
4731 ahd_build_transfer_msg(ahd, devinfo);
4732 else
4733 panic("ahd_intr: AWAITING target message with no message");
4734
4735 ahd->msgout_index = 0;
4736 ahd->msg_type = MSG_TYPE_TARGET_MSGIN;
4737 }
4738 #endif
4739 /**************************** Initialization **********************************/
4740 static u_int
4741 ahd_sglist_size(struct ahd_softc *ahd)
4742 {
4743 bus_size_t list_size;
4744
4745 list_size = sizeof(struct ahd_dma_seg) * AHD_NSEG;
4746 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
4747 list_size = sizeof(struct ahd_dma64_seg) * AHD_NSEG;
4748 return (list_size);
4749 }
4750
4751 /*
4752 * Calculate the optimum S/G List allocation size. S/G elements used
4753 * for a given transaction must be physically contiguous. Assume the
4754 * OS will allocate full pages to us, so it doesn't make sense to request
4755 * less than a page.
4756 */
4757 static u_int
4758 ahd_sglist_allocsize(struct ahd_softc *ahd)
4759 {
4760 bus_size_t sg_list_increment;
4761 bus_size_t sg_list_size;
4762 bus_size_t max_list_size;
4763 bus_size_t best_list_size;
4764
4765 /* Start out with the minimum required for AHD_NSEG. */
4766 sg_list_increment = ahd_sglist_size(ahd);
4767 sg_list_size = sg_list_increment;
4768
4769 /* Get us as close as possible to a page in size. */
4770 while ((sg_list_size + sg_list_increment) <= PAGE_SIZE)
4771 sg_list_size += sg_list_increment;
4772
4773 /*
4774 * Try to reduce the amount of wastage by allocating
4775 * multiple pages.
4776 */
4777 best_list_size = sg_list_size;
4778 max_list_size = roundup(sg_list_increment, PAGE_SIZE);
4779 if (max_list_size < 4 * PAGE_SIZE)
4780 max_list_size = 4 * PAGE_SIZE;
4781 if (max_list_size > (AHD_SCB_MAX_ALLOC * sg_list_increment))
4782 max_list_size = (AHD_SCB_MAX_ALLOC * sg_list_increment);
4783 while ((sg_list_size + sg_list_increment) <= max_list_size
4784 && (sg_list_size % PAGE_SIZE) != 0) {
4785 bus_size_t new_mod;
4786 bus_size_t best_mod;
4787
4788 sg_list_size += sg_list_increment;
4789 new_mod = sg_list_size % PAGE_SIZE;
4790 best_mod = best_list_size % PAGE_SIZE;
4791 if (new_mod > best_mod || new_mod == 0) {
4792 best_list_size = sg_list_size;
4793 }
4794 }
4795 return (best_list_size);
4796 }
4797
4798 int
4799 ahd_softc_init(struct ahd_softc *ahd)
4800 {
4801
4802 ahd->unpause = 0;
4803 ahd->pause = PAUSE;
4804 return (0);
4805 }
4806
4807 void
4808 ahd_set_unit(struct ahd_softc *ahd, int unit)
4809 {
4810 ahd->unit = unit;
4811 }
4812
4813 void
4814 ahd_set_name(struct ahd_softc *ahd, char *name)
4815 {
4816 if (ahd->name != NULL)
4817 free(ahd->name, M_DEVBUF);
4818 ahd->name = name;
4819 }
4820
4821 void
4822 ahd_free(struct ahd_softc *ahd)
4823 {
4824 int i;
4825
4826 switch (ahd->init_level) {
4827 default:
4828 case 2:
4829 ahd_shutdown(ahd);
4830 TAILQ_REMOVE(&ahd_tailq, ahd, links);
4831 /* FALLTHROUGH */
4832 case 1:
4833 bus_dmamap_unload(ahd->parent_dmat, ahd->shared_data_map.dmamap);
4834 bus_dmamap_destroy(ahd->parent_dmat, ahd->shared_data_map.dmamap);
4835 bus_dmamem_unmap(ahd->parent_dmat, (caddr_t)ahd->qoutfifo, ahd->shared_data_size);
4836 bus_dmamem_free(ahd->parent_dmat, &ahd->shared_data_map.dmasegs, ahd->shared_data_map.nseg);
4837 break;
4838 case 0:
4839 break;
4840 }
4841
4842 ahd_platform_free(ahd);
4843 ahd_fini_scbdata(ahd);
4844 for (i = 0; i < AHD_NUM_TARGETS; i++) {
4845 struct ahd_tmode_tstate *tstate;
4846
4847 tstate = ahd->enabled_targets[i];
4848 if (tstate != NULL) {
4849 #if AHD_TARGET_MODE
4850 int j;
4851
4852 for (j = 0; j < AHD_NUM_LUNS; j++) {
4853 struct ahd_tmode_lstate *lstate;
4854
4855 lstate = tstate->enabled_luns[j];
4856 if (lstate != NULL) {
4857 xpt_free_path(lstate->path);
4858 free(lstate, M_DEVBUF);
4859 }
4860 }
4861 #endif
4862 free(tstate, M_DEVBUF);
4863 }
4864 }
4865 #if AHD_TARGET_MODE
4866 if (ahd->black_hole != NULL) {
4867 xpt_free_path(ahd->black_hole->path);
4868 free(ahd->black_hole, M_DEVBUF);
4869 }
4870 #endif
4871 if (ahd->name != NULL)
4872 free(ahd->name, M_DEVBUF);
4873 if (ahd->seep_config != NULL)
4874 free(ahd->seep_config, M_DEVBUF);
4875 if (ahd->saved_stack != NULL)
4876 free(ahd->saved_stack, M_DEVBUF);
4877 #ifndef __FreeBSD__
4878 free(ahd, M_DEVBUF);
4879 #endif
4880 return;
4881 }
4882
4883 void
4884 ahd_shutdown(void *arg)
4885 {
4886 struct ahd_softc *ahd;
4887
4888 ahd = (struct ahd_softc *)arg;
4889
4890 #ifdef AHD_DEBUG
4891 printf("%s: ahd_shutdown\n", ahd_name(ahd));
4892 #endif
4893 /*
4894 * Stop periodic timer callbacks.
4895 */
4896 ahd_timer_stop(&ahd->reset_timer);
4897 ahd_timer_stop(&ahd->stat_timer);
4898
4899 /* This will reset most registers to 0, but not all */
4900 ahd_reset(ahd);
4901 }
4902
4903 /*
4904 * Reset the controller and record some information about it
4905 * that is only available just after a reset.
4906 */
4907 int
4908 ahd_reset(struct ahd_softc *ahd)
4909 {
4910 u_int sxfrctl1;
4911 int wait;
4912 uint32_t cmd;
4913 struct ahd_pci_busdata *bd = ahd->bus_data;
4914
4915 /*
4916 * Preserve the value of the SXFRCTL1 register for all channels.
4917 * It contains settings that affect termination and we don't want
4918 * to disturb the integrity of the bus.
4919 */
4920 ahd_pause(ahd);
4921 ahd_update_modes(ahd);
4922 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4923 sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
4924
4925 cmd = pci_conf_read(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG);
4926
4927 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
4928 uint32_t mod_cmd;
4929
4930 /*
4931 * A4 Razor #632
4932 * During the assertion of CHIPRST, the chip
4933 * does not disable its parity logic prior to
4934 * the start of the reset. This may cause a
4935 * parity error to be detected and thus a
4936 * spurious SERR or PERR assertion. Disble
4937 * PERR and SERR responses during the CHIPRST.
4938 */
4939 mod_cmd = cmd & ~(PCI_COMMAND_PARITY_ENABLE|PCI_COMMAND_SERR_ENABLE);
4940 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG, mod_cmd);
4941 }
4942 ahd_outb(ahd, HCNTRL, CHIPRST | ahd->pause);
4943
4944 /*
4945 * Ensure that the reset has finished. We delay 1000us
4946 * prior to reading the register to make sure the chip
4947 * has sufficiently completed its reset to handle register
4948 * accesses.
4949 */
4950 wait = 1000;
4951 do {
4952 ahd_delay(1000);
4953 } while (--wait && !(ahd_inb(ahd, HCNTRL) & CHIPRSTACK));
4954
4955 if (wait == 0) {
4956 printf("%s: WARNING - Failed chip reset! "
4957 "Trying to initialize anyway.\n", ahd_name(ahd));
4958 }
4959 ahd_outb(ahd, HCNTRL, ahd->pause);
4960
4961 if ((ahd->bugs & AHD_PCIX_CHIPRST_BUG) != 0) {
4962 /*
4963 * Clear any latched PCI error status and restore
4964 * previous SERR and PERR response enables.
4965 */
4966 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG, 0x000000FF);
4967 pci_conf_write(bd->pc, bd->tag, PCI_COMMAND_STATUS_REG,
4968 PCI_COMMAND_SERR_ENABLE|PCI_COMMAND_PARITY_ENABLE);
4969 }
4970
4971 /*
4972 * Mode should be SCSI after a chip reset, but lets
4973 * set it just to be safe. We touch the MODE_PTR
4974 * register directly so as to bypass the lazy update
4975 * ode in ahd_set_modes().
4976 */
4977 ahd_known_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
4978 ahd_outb(ahd, MODE_PTR,
4979 ahd_build_mode_state(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI));
4980
4981 /*
4982 * Restore SXFRCTL1.
4983 *
4984 * We must always initialize STPWEN to 1 before we
4985 * restore the saved values. STPWEN is initialized
4986 * to a tri-state condition which can only be cleared
4987 * by turning it on.
4988 */
4989 ahd_outb(ahd, SXFRCTL1, sxfrctl1|STPWEN);
4990 ahd_outb(ahd, SXFRCTL1, sxfrctl1);
4991
4992 /* Determine chip configuration */
4993 ahd->features &= ~AHD_WIDE;
4994 if ((ahd_inb(ahd, SBLKCTL) & SELWIDE) != 0)
4995 ahd->features |= AHD_WIDE;
4996
4997 /*
4998 * If a recovery action has forced a chip reset,
4999 * re-initialize the chip to our liking.
5000 */
5001 if (ahd->init_level > 0)
5002 ahd_chip_init(ahd);
5003
5004 return (0);
5005 }
5006
5007 /*
5008 * Determine the number of SCBs available on the controller
5009 */
5010 int
5011 ahd_probe_scbs(struct ahd_softc *ahd) {
5012 int i;
5013
5014 AHD_ASSERT_MODES(ahd, ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK),
5015 ~(AHD_MODE_UNKNOWN_MSK|AHD_MODE_CFG_MSK));
5016 for (i = 0; i < AHD_SCB_MAX; i++) {
5017 int j;
5018 int ret;
5019
5020 ahd_set_scbptr(ahd, i);
5021 ahd_outw(ahd, SCB_BASE, i);
5022 for (j = 2; j < 64; j++)
5023 ahd_outb(ahd, SCB_BASE+j, 0);
5024 /* Start out life as unallocated (needing an abort) */
5025 ahd_outb(ahd, SCB_CONTROL, MK_MESSAGE);
5026 ret = ahd_inw_scbram(ahd, SCB_BASE);
5027 if (ret != i) {
5028 printf("%s: ahd_probe_scbs (!=%d): returned 0x%x\n", ahd_name(ahd), i, ret);
5029 break;
5030 }
5031 ahd_set_scbptr(ahd, 0);
5032 ret = ahd_inw_scbram(ahd, SCB_BASE);
5033 if (ret != 0) {
5034 printf("ahd_probe_scbs (non zero): returned 0x%x\n", ret);
5035 break;
5036 }
5037 }
5038 return (i);
5039 }
5040
5041 static void
5042 ahd_initialize_hscbs(struct ahd_softc *ahd)
5043 {
5044 int i;
5045
5046 for (i = 0; i < ahd->scb_data.maxhscbs; i++) {
5047 ahd_set_scbptr(ahd, i);
5048
5049 /* Clear the control byte. */
5050 ahd_outb(ahd, SCB_CONTROL, 0);
5051
5052 /* Set the next pointer */
5053 ahd_outw(ahd, SCB_NEXT, SCB_LIST_NULL);
5054 }
5055 }
5056
5057 static int
5058 ahd_init_scbdata(struct ahd_softc *ahd)
5059 {
5060 struct scb_data *scb_data;
5061 int i;
5062
5063 scb_data = &ahd->scb_data;
5064 TAILQ_INIT(&scb_data->free_scbs);
5065 for (i = 0; i < AHD_NUM_TARGETS * AHD_NUM_LUNS_NONPKT; i++)
5066 LIST_INIT(&scb_data->free_scb_lists[i]);
5067 LIST_INIT(&scb_data->any_dev_free_scb_list);
5068 SLIST_INIT(&scb_data->hscb_maps);
5069 SLIST_INIT(&scb_data->sg_maps);
5070 SLIST_INIT(&scb_data->sense_maps);
5071
5072 /* Determine the number of hardware SCBs and initialize them */
5073 scb_data->maxhscbs = ahd_probe_scbs(ahd);
5074 if (scb_data->maxhscbs == 0) {
5075 printf("%s: No SCB space found\n", ahd_name(ahd));
5076 return (ENXIO);
5077 }
5078 ahd_initialize_hscbs(ahd);
5079
5080 /*
5081 * Create our DMA tags. These tags define the kinds of device
5082 * accessible memory allocations and memory mappings we will
5083 * need to perform during normal operation.
5084 *
5085 * Unless we need to further restrict the allocation, we rely
5086 * on the restrictions of the parent dmat, hence the common
5087 * use of MAXADDR and MAXSIZE.
5088 */
5089
5090 /* Perform initial CCB allocation */
5091 ahd_alloc_scbs(ahd);
5092
5093 if (scb_data->numscbs == 0) {
5094 printf("%s: ahd_init_scbdata - "
5095 "Unable to allocate initial scbs\n",
5096 ahd_name(ahd));
5097 goto error_exit;
5098 }
5099
5100 /*
5101 * Note that we were successfull
5102 */
5103 return (0);
5104
5105 error_exit:
5106
5107 return (ENOMEM);
5108 }
5109
5110 static struct scb *
5111 ahd_find_scb_by_tag(struct ahd_softc *ahd, u_int tag)
5112 {
5113 struct scb *scb;
5114
5115 /*
5116 * Look on the pending list.
5117 */
5118 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
5119 if (SCB_GET_TAG(scb) == tag)
5120 return (scb);
5121 }
5122
5123 /*
5124 * Then on all of the collision free lists.
5125 */
5126 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
5127 struct scb *list_scb;
5128
5129 list_scb = scb;
5130 do {
5131 if (SCB_GET_TAG(list_scb) == tag)
5132 return (list_scb);
5133 list_scb = LIST_NEXT(list_scb, collision_links);
5134 } while (list_scb);
5135 }
5136
5137 /*
5138 * And finally on the generic free list.
5139 */
5140 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
5141 if (SCB_GET_TAG(scb) == tag)
5142 return (scb);
5143 }
5144
5145 return (NULL);
5146 }
5147
5148 static void
5149 ahd_fini_scbdata(struct ahd_softc *ahd)
5150 {
5151 struct scb_data *scb_data;
5152
5153 scb_data = &ahd->scb_data;
5154 if (scb_data == NULL)
5155 return;
5156
5157 switch (scb_data->init_level) {
5158 default:
5159 case 3:
5160 {
5161 struct map_node *sns_map;
5162
5163 while ((sns_map = SLIST_FIRST(&scb_data->sense_maps)) != NULL) {
5164 SLIST_REMOVE_HEAD(&scb_data->sense_maps, links);
5165 ahd_freedmamem(ahd->parent_dmat, PAGE_SIZE,
5166 sns_map->dmamap, (caddr_t)sns_map->vaddr,
5167 &sns_map->dmasegs, sns_map->nseg);
5168 free(sns_map, M_DEVBUF);
5169 }
5170 /* FALLTHROUGH */
5171 }
5172 case 2:
5173 {
5174 struct map_node *sg_map;
5175
5176 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps)) != NULL) {
5177 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
5178 ahd_freedmamem(ahd->parent_dmat, ahd_sglist_allocsize(ahd),
5179 sg_map->dmamap, (caddr_t)sg_map->vaddr,
5180 &sg_map->dmasegs, sg_map->nseg);
5181 free(sg_map, M_DEVBUF);
5182 }
5183 /* FALLTHROUGH */
5184 }
5185 case 1:
5186 {
5187 struct map_node *hscb_map;
5188
5189 while ((hscb_map = SLIST_FIRST(&scb_data->hscb_maps)) != NULL) {
5190 SLIST_REMOVE_HEAD(&scb_data->hscb_maps, links);
5191 ahd_freedmamem(ahd->parent_dmat, PAGE_SIZE,
5192 hscb_map->dmamap, (caddr_t)hscb_map->vaddr,
5193 &hscb_map->dmasegs, hscb_map->nseg);
5194 free(hscb_map, M_DEVBUF);
5195 }
5196 /* FALLTHROUGH */
5197 }
5198 case 0:
5199 break;
5200 }
5201 }
5202
5203 /*
5204 * DSP filter Bypass must be enabled until the first selection
5205 * after a change in bus mode (Razor #491 and #493).
5206 */
5207 static void
5208 ahd_setup_iocell_workaround(struct ahd_softc *ahd)
5209 {
5210 ahd_mode_state saved_modes;
5211
5212 saved_modes = ahd_save_modes(ahd);
5213 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5214 ahd_outb(ahd, DSPDATACTL, ahd_inb(ahd, DSPDATACTL)
5215 | BYPASSENAB | RCVROFFSTDIS | XMITOFFSTDIS);
5216 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) | (ENSELDO|ENSELDI));
5217 #ifdef AHD_DEBUG
5218 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5219 printf("%s: Setting up iocell workaround\n", ahd_name(ahd));
5220 #endif
5221 ahd_restore_modes(ahd, saved_modes);
5222 }
5223
5224 static void
5225 ahd_iocell_first_selection(struct ahd_softc *ahd)
5226 {
5227 ahd_mode_state saved_modes;
5228 u_int sblkctl;
5229
5230 saved_modes = ahd_save_modes(ahd);
5231 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5232 sblkctl = ahd_inb(ahd, SBLKCTL);
5233 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5234 #ifdef AHD_DEBUG
5235 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5236 printf("%s: iocell first selection\n", ahd_name(ahd));
5237 #endif
5238 if ((sblkctl & ENAB40) != 0) {
5239 ahd_outb(ahd, DSPDATACTL,
5240 ahd_inb(ahd, DSPDATACTL) & ~BYPASSENAB);
5241 #ifdef AHD_DEBUG
5242 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5243 printf("%s: BYPASS now disabled\n", ahd_name(ahd));
5244 #endif
5245 }
5246 ahd_outb(ahd, SIMODE0, ahd_inb(ahd, SIMODE0) & ~(ENSELDO|ENSELDI));
5247 ahd_outb(ahd, CLRINT, CLRSCSIINT);
5248 ahd_restore_modes(ahd, saved_modes);
5249 }
5250
5251 /*************************** SCB Management ***********************************/
5252 static void
5253 ahd_add_col_list(struct ahd_softc *ahd, struct scb *scb, u_int col_idx)
5254 {
5255 struct scb_list *free_list;
5256 struct scb_tailq *free_tailq;
5257 struct scb *first_scb;
5258
5259 scb->flags |= SCB_ON_COL_LIST;
5260 AHD_SET_SCB_COL_IDX(scb, col_idx);
5261 free_list = &ahd->scb_data.free_scb_lists[col_idx];
5262 free_tailq = &ahd->scb_data.free_scbs;
5263 first_scb = LIST_FIRST(free_list);
5264 if (first_scb != NULL) {
5265 LIST_INSERT_AFTER(first_scb, scb, collision_links);
5266 } else {
5267 LIST_INSERT_HEAD(free_list, scb, collision_links);
5268 TAILQ_INSERT_TAIL(free_tailq, scb, links.tqe);
5269 }
5270 }
5271
5272 static void
5273 ahd_rem_col_list(struct ahd_softc *ahd, struct scb *scb)
5274 {
5275 struct scb_list *free_list;
5276 struct scb_tailq *free_tailq;
5277 struct scb *first_scb;
5278 u_int col_idx;
5279
5280 scb->flags &= ~SCB_ON_COL_LIST;
5281 col_idx = AHD_GET_SCB_COL_IDX(ahd, scb);
5282 free_list = &ahd->scb_data.free_scb_lists[col_idx];
5283 free_tailq = &ahd->scb_data.free_scbs;
5284 first_scb = LIST_FIRST(free_list);
5285 if (first_scb == scb) {
5286 struct scb *next_scb;
5287
5288 /*
5289 * Maintain order in the collision free
5290 * lists for fairness if this device has
5291 * other colliding tags active.
5292 */
5293 next_scb = LIST_NEXT(scb, collision_links);
5294 if (next_scb != NULL) {
5295 TAILQ_INSERT_AFTER(free_tailq, scb,
5296 next_scb, links.tqe);
5297 }
5298 TAILQ_REMOVE(free_tailq, scb, links.tqe);
5299 }
5300 LIST_REMOVE(scb, collision_links);
5301 }
5302
5303 /*
5304 * Get a free scb. If there are none, see if we can allocate a new SCB.
5305 */
5306 struct scb *
5307 ahd_get_scb(struct ahd_softc *ahd, u_int col_idx)
5308 {
5309 struct scb *scb;
5310 int tries;
5311
5312 tries = 0;
5313 look_again:
5314 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
5315 if (AHD_GET_SCB_COL_IDX(ahd, scb) != col_idx) {
5316 ahd_rem_col_list(ahd, scb);
5317 goto found;
5318 }
5319 }
5320 if ((scb = LIST_FIRST(&ahd->scb_data.any_dev_free_scb_list)) == NULL) {
5321
5322 if (tries++ != 0)
5323 return (NULL);
5324 ahd_alloc_scbs(ahd);
5325 goto look_again;
5326 }
5327 LIST_REMOVE(scb, links.le);
5328 if (col_idx != AHD_NEVER_COL_IDX
5329 && (scb->col_scb != NULL)
5330 && (scb->col_scb->flags & SCB_ACTIVE) == 0) {
5331 LIST_REMOVE(scb->col_scb, links.le);
5332 ahd_add_col_list(ahd, scb->col_scb, col_idx);
5333 }
5334 found:
5335 scb->flags |= SCB_ACTIVE;
5336 return (scb);
5337 }
5338
5339 /*
5340 * Return an SCB resource to the free list.
5341 */
5342 void
5343 ahd_free_scb(struct ahd_softc *ahd, struct scb *scb)
5344 {
5345
5346 /* Clean up for the next user */
5347 scb->flags = SCB_FLAG_NONE;
5348 scb->hscb->control = 0;
5349 ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = NULL;
5350
5351 if (scb->col_scb == NULL) {
5352
5353 /*
5354 * No collision possible. Just free normally.
5355 */
5356 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5357 scb, links.le);
5358 } else if ((scb->col_scb->flags & SCB_ON_COL_LIST) != 0) {
5359
5360 /*
5361 * The SCB we might have collided with is on
5362 * a free collision list. Put both SCBs on
5363 * the generic list.
5364 */
5365 ahd_rem_col_list(ahd, scb->col_scb);
5366 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5367 scb, links.le);
5368 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5369 scb->col_scb, links.le);
5370 } else if ((scb->col_scb->flags
5371 & (SCB_PACKETIZED|SCB_ACTIVE)) == SCB_ACTIVE
5372 && (scb->col_scb->hscb->control & TAG_ENB) != 0) {
5373
5374 /*
5375 * The SCB we might collide with on the next allocation
5376 * is still active in a non-packetized, tagged, context.
5377 * Put us on the SCB collision list.
5378 */
5379 ahd_add_col_list(ahd, scb,
5380 AHD_GET_SCB_COL_IDX(ahd, scb->col_scb));
5381 } else {
5382 /*
5383 * The SCB we might collide with on the next allocation
5384 * is either active in a packetized context, or free.
5385 * Since we can't collide, put this SCB on the generic
5386 * free list.
5387 */
5388 LIST_INSERT_HEAD(&ahd->scb_data.any_dev_free_scb_list,
5389 scb, links.le);
5390 }
5391
5392 ahd_platform_scb_free(ahd, scb);
5393 }
5394
5395 void
5396 ahd_alloc_scbs(struct ahd_softc *ahd)
5397 {
5398 struct scb_data *scb_data;
5399 struct scb *next_scb;
5400 struct hardware_scb *hscb;
5401 struct map_node *hscb_map;
5402 struct map_node *sg_map;
5403 struct map_node *sense_map;
5404 uint8_t *segs;
5405 uint8_t *sense_data;
5406 bus_addr_t hscb_busaddr;
5407 bus_addr_t sg_busaddr;
5408 bus_addr_t sense_busaddr;
5409 int newcount;
5410 int i;
5411
5412 scb_data = &ahd->scb_data;
5413 if (scb_data->numscbs >= AHD_SCB_MAX_ALLOC)
5414 /* Can't allocate any more */
5415 return;
5416
5417 KASSERT(scb_data->scbs_left >= 0);
5418 if (scb_data->scbs_left != 0) {
5419 int offset;
5420
5421 offset = (PAGE_SIZE / sizeof(*hscb)) - scb_data->scbs_left;
5422 hscb_map = SLIST_FIRST(&scb_data->hscb_maps);
5423 hscb = &((struct hardware_scb *)hscb_map->vaddr)[offset];
5424 hscb_busaddr = hscb_map->physaddr + (offset * sizeof(*hscb));
5425 } else {
5426 hscb_map = malloc(sizeof(*hscb_map), M_DEVBUF, M_NOWAIT);
5427
5428 if (hscb_map == NULL)
5429 return;
5430
5431 memset(hscb_map, 0, sizeof(*hscb_map));
5432
5433 /* Allocate the next batch of hardware SCBs */
5434 if (ahd_createdmamem(ahd->parent_dmat, PAGE_SIZE, ahd->sc_dmaflags,
5435 &hscb_map->dmamap, (caddr_t *)&hscb_map->vaddr,
5436 &hscb_map->physaddr, &hscb_map->dmasegs,
5437 &hscb_map->nseg, ahd_name(ahd),
5438 "hardware SCB structures") < 0) {
5439 free(hscb_map, M_DEVBUF);
5440 return;
5441 }
5442
5443 SLIST_INSERT_HEAD(&scb_data->hscb_maps, hscb_map, links);
5444
5445 hscb = (struct hardware_scb *)hscb_map->vaddr;
5446 hscb_busaddr = hscb_map->physaddr;
5447 scb_data->scbs_left = PAGE_SIZE / sizeof(*hscb);
5448 }
5449
5450 scb_data->init_level++;
5451
5452 if (scb_data->sgs_left != 0) {
5453 int offset;
5454
5455 offset = ((ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd))
5456 - scb_data->sgs_left) * ahd_sglist_size(ahd);
5457 sg_map = SLIST_FIRST(&scb_data->sg_maps);
5458 segs = sg_map->vaddr + offset;
5459 sg_busaddr = sg_map->physaddr + offset;
5460 } else {
5461 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
5462
5463 if (sg_map == NULL)
5464 return;
5465
5466 bzero(sg_map, sizeof(*sg_map));
5467
5468 /* Allocate the next batch of S/G lists */
5469 if (ahd_createdmamem(ahd->parent_dmat, ahd_sglist_allocsize(ahd), ahd->sc_dmaflags,
5470 &sg_map->dmamap, (caddr_t *)&sg_map->vaddr,
5471 &sg_map->physaddr, &sg_map->dmasegs,
5472 &sg_map->nseg, ahd_name(ahd),
5473 "SG data structures") < 0) {
5474 free(sg_map, M_DEVBUF);
5475 return;
5476 }
5477
5478 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
5479
5480 segs = sg_map->vaddr;
5481 sg_busaddr = sg_map->physaddr;
5482 scb_data->sgs_left =
5483 ahd_sglist_allocsize(ahd) / ahd_sglist_size(ahd);
5484 #ifdef AHD_DEBUG
5485 if (ahd_debug & AHD_SHOW_MEMORY)
5486 printf("%s: ahd_alloc_scbs - Mapped SG data\n", ahd_name(ahd));
5487 #endif
5488 }
5489
5490 scb_data->init_level++;
5491
5492
5493 if (scb_data->sense_left != 0) {
5494 int offset;
5495
5496 offset = PAGE_SIZE - (AHD_SENSE_BUFSIZE * scb_data->sense_left);
5497 sense_map = SLIST_FIRST(&scb_data->sense_maps);
5498 sense_data = sense_map->vaddr + offset;
5499 sense_busaddr = sense_map->physaddr + offset;
5500 } else {
5501 sense_map = malloc(sizeof(*sense_map), M_DEVBUF, M_NOWAIT);
5502
5503 if (sense_map == NULL)
5504 return;
5505
5506 bzero(sense_map, sizeof(*sense_map));
5507
5508 /* Allocate the next batch of sense buffers */
5509 if (ahd_createdmamem(ahd->parent_dmat, PAGE_SIZE, ahd->sc_dmaflags,
5510 &sense_map->dmamap, (caddr_t *)&sense_map->vaddr,
5511 &sense_map->physaddr, &sense_map->dmasegs,
5512 &sense_map->nseg, ahd_name(ahd),
5513 "Sense Data structures") < 0) {
5514 free(sense_map, M_DEVBUF);
5515 return;
5516 }
5517
5518 SLIST_INSERT_HEAD(&scb_data->sense_maps, sense_map, links);
5519
5520 sense_data = sense_map->vaddr;
5521 sense_busaddr = sense_map->physaddr;
5522 scb_data->sense_left = PAGE_SIZE / AHD_SENSE_BUFSIZE;
5523 #ifdef AHD_DEBUG
5524 if (ahd_debug & AHD_SHOW_MEMORY)
5525 printf("%s: ahd_alloc_scbs - Mapped sense data\n", ahd_name(ahd));
5526 #endif
5527 }
5528
5529 scb_data->init_level++;
5530
5531 newcount = MIN(scb_data->sense_left, scb_data->scbs_left);
5532 newcount = MIN(newcount, scb_data->sgs_left);
5533 newcount = MIN(newcount, (AHD_SCB_MAX_ALLOC - scb_data->numscbs));
5534 scb_data->sense_left -= newcount;
5535 scb_data->scbs_left -= newcount;
5536 scb_data->sgs_left -= newcount;
5537
5538 for (i = 0; i < newcount; i++) {
5539 u_int col_tag;
5540
5541 struct scb_platform_data *pdata;
5542 #ifndef __linux__
5543 int error;
5544 #endif
5545 next_scb = (struct scb *)malloc(sizeof(*next_scb),
5546 M_DEVBUF, M_NOWAIT);
5547 if (next_scb == NULL)
5548 break;
5549
5550 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
5551 M_DEVBUF, M_NOWAIT);
5552 if (pdata == NULL) {
5553 free(next_scb, M_DEVBUF);
5554 break;
5555 }
5556 next_scb->platform_data = pdata;
5557 next_scb->hscb_map = hscb_map;
5558 next_scb->sg_map = sg_map;
5559 next_scb->sense_map = sense_map;
5560 next_scb->sg_list = segs;
5561 next_scb->sense_data = sense_data;
5562 next_scb->sense_busaddr = sense_busaddr;
5563 next_scb->hscb = hscb;
5564 hscb->hscb_busaddr = ahd_htole32(hscb_busaddr);
5565 KASSERT((vaddr_t)hscb >= (vaddr_t)hscb_map->vaddr &&
5566 (vaddr_t)hscb < (vaddr_t)hscb_map->vaddr + PAGE_SIZE);
5567
5568 /*
5569 * The sequencer always starts with the second entry.
5570 * The first entry is embedded in the scb.
5571 */
5572 next_scb->sg_list_busaddr = sg_busaddr;
5573 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
5574 next_scb->sg_list_busaddr
5575 += sizeof(struct ahd_dma64_seg);
5576 else
5577 next_scb->sg_list_busaddr += sizeof(struct ahd_dma_seg);
5578 next_scb->ahd_softc = ahd;
5579 next_scb->flags = SCB_FLAG_NONE;
5580
5581 error = bus_dmamap_create(ahd->parent_dmat,
5582 AHD_MAXTRANSFER_SIZE, AHD_NSEG, MAXBSIZE, 0,
5583 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW|ahd->sc_dmaflags,
5584 &next_scb->dmamap);
5585 if (error != 0) {
5586 free(next_scb, M_DEVBUF);
5587 free(pdata, M_DEVBUF);
5588 break;
5589 }
5590 next_scb->hscb->tag = ahd_htole16(scb_data->numscbs);
5591 col_tag = scb_data->numscbs ^ 0x100;
5592 next_scb->col_scb = ahd_find_scb_by_tag(ahd, col_tag);
5593 if (next_scb->col_scb != NULL)
5594 next_scb->col_scb->col_scb = next_scb;
5595 ahd_free_scb(ahd, next_scb);
5596 hscb++;
5597 hscb_busaddr += sizeof(*hscb);
5598 segs += ahd_sglist_size(ahd);
5599 sg_busaddr += ahd_sglist_size(ahd);
5600 sense_data += AHD_SENSE_BUFSIZE;
5601 sense_busaddr += AHD_SENSE_BUFSIZE;
5602 scb_data->numscbs++;
5603 }
5604 }
5605
5606 void
5607 ahd_controller_info(struct ahd_softc *ahd, char *buf)
5608 {
5609 const char *speed;
5610 const char *type;
5611 int len;
5612
5613 len = sprintf(buf, "%s: ", ahd_chip_names[ahd->chip & AHD_CHIPID_MASK]);
5614 buf += len;
5615
5616 speed = "Ultra320 ";
5617 if ((ahd->features & AHD_WIDE) != 0) {
5618 type = "Wide ";
5619 } else {
5620 type = "Single ";
5621 }
5622 len = sprintf(buf, "%s%sChannel %c, SCSI Id=%d, ",
5623 speed, type, ahd->channel, ahd->our_id);
5624 buf += len;
5625
5626 sprintf(buf, "%s, %d SCBs", ahd->bus_description,
5627 ahd->scb_data.maxhscbs);
5628 }
5629
5630 static const char *channel_strings[] = {
5631 "Primary Low",
5632 "Primary High",
5633 "Secondary Low",
5634 "Secondary High"
5635 };
5636
5637 static const char *termstat_strings[] = {
5638 "Terminated Correctly",
5639 "Over Terminated",
5640 "Under Terminated",
5641 "Not Configured"
5642 };
5643
5644 /*
5645 * Start the board, ready for normal operation
5646 */
5647 int
5648 ahd_init(struct ahd_softc *ahd)
5649 {
5650 uint8_t *next_vaddr;
5651 bus_addr_t next_baddr;
5652 size_t driver_data_size;
5653 int i;
5654 int error;
5655 u_int warn_user;
5656 uint8_t current_sensing;
5657 uint8_t fstat;
5658
5659 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
5660
5661 ahd->stack_size = ahd_probe_stack_size(ahd);
5662 ahd->saved_stack = malloc(ahd->stack_size * sizeof(uint16_t),
5663 M_DEVBUF, M_NOWAIT);
5664 if (ahd->saved_stack == NULL)
5665 return (ENOMEM);
5666 /* Zero the memory */
5667 memset(ahd->saved_stack, 0, ahd->stack_size * sizeof(uint16_t));
5668
5669 /*
5670 * Verify that the compiler hasn't over-agressively
5671 * padded important structures.
5672 */
5673 if (sizeof(struct hardware_scb) != 64)
5674 panic("Hardware SCB size is incorrect");
5675
5676 #ifdef AHD_DEBUG
5677 if ((ahd_debug & AHD_DEBUG_SEQUENCER) != 0)
5678 ahd->flags |= AHD_SEQUENCER_DEBUG;
5679 #endif
5680
5681 /*
5682 * Default to allowing initiator operations.
5683 */
5684 ahd->flags |= AHD_INITIATORROLE;
5685
5686 /*
5687 * Only allow target mode features if this unit has them enabled.
5688 */
5689 if ((AHD_TMODE_ENABLE & (0x1 << ahd->unit)) == 0)
5690 ahd->features &= ~AHD_TARGETMODE;
5691
5692 /*
5693 * DMA tag for our command fifos and other data in system memory
5694 * the card's sequencer must be able to access. For initiator
5695 * roles, we need to allocate space for the qoutfifo. When providing
5696 * for the target mode role, we must additionally provide space for
5697 * the incoming target command fifo.
5698 */
5699 driver_data_size = AHD_SCB_MAX * sizeof(uint16_t)
5700 + sizeof(struct hardware_scb);
5701 if ((ahd->features & AHD_TARGETMODE) != 0)
5702 driver_data_size += AHD_TMODE_CMDS * sizeof(struct target_cmd);
5703 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0)
5704 driver_data_size += PKT_OVERRUN_BUFSIZE;
5705 ahd->shared_data_size = driver_data_size;
5706
5707 memset(&ahd->shared_data_map, 0, sizeof(ahd->shared_data_map));
5708 ahd->sc_dmaflags = BUS_DMA_NOWAIT;
5709
5710 if (ahd_createdmamem(ahd->parent_dmat, ahd->shared_data_size,
5711 ahd->sc_dmaflags,
5712 &ahd->shared_data_map.dmamap, (caddr_t *)&ahd->shared_data_map.vaddr,
5713 &ahd->shared_data_map.physaddr, &ahd->shared_data_map.dmasegs,
5714 &ahd->shared_data_map.nseg, ahd_name(ahd), "shared data") < 0)
5715 return (ENOMEM);
5716 ahd->qoutfifo = (void *) ahd->shared_data_map.vaddr;
5717
5718 ahd->init_level++;
5719
5720 next_vaddr = (uint8_t *)&ahd->qoutfifo[AHD_QOUT_SIZE];
5721 next_baddr = ahd->shared_data_map.physaddr + AHD_QOUT_SIZE*sizeof(uint16_t);
5722 if ((ahd->features & AHD_TARGETMODE) != 0) {
5723 ahd->targetcmds = (struct target_cmd *)next_vaddr;
5724 next_vaddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
5725 next_baddr += AHD_TMODE_CMDS * sizeof(struct target_cmd);
5726 }
5727
5728 if ((ahd->bugs & AHD_PKT_BITBUCKET_BUG) != 0) {
5729 ahd->overrun_buf = next_vaddr;
5730 next_vaddr += PKT_OVERRUN_BUFSIZE;
5731 next_baddr += PKT_OVERRUN_BUFSIZE;
5732 }
5733
5734 /*
5735 * We need one SCB to serve as the "next SCB". Since the
5736 * tag identifier in this SCB will never be used, there is
5737 * no point in using a valid HSCB tag from an SCB pulled from
5738 * the standard free pool. So, we allocate this "sentinel"
5739 * specially from the DMA safe memory chunk used for the QOUTFIFO.
5740 */
5741 ahd->next_queued_hscb = (struct hardware_scb *)next_vaddr;
5742 ahd->next_queued_hscb_map = &ahd->shared_data_map;
5743 ahd->next_queued_hscb->hscb_busaddr = ahd_htole32(next_baddr);
5744
5745 memset(&ahd->scb_data, 0, sizeof(struct scb_data));
5746
5747 /* Allocate SCB data now that parent_dmat is initialized */
5748 if (ahd_init_scbdata(ahd) != 0)
5749 return (ENOMEM);
5750
5751 if ((ahd->flags & AHD_INITIATORROLE) == 0)
5752 ahd->flags &= ~AHD_RESET_BUS_A;
5753
5754 /*
5755 * Before committing these settings to the chip, give
5756 * the OSM one last chance to modify our configuration.
5757 */
5758 ahd_platform_init(ahd);
5759
5760 /* Bring up the chip. */
5761 ahd_chip_init(ahd);
5762
5763 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
5764
5765 if ((ahd->flags & AHD_CURRENT_SENSING) == 0)
5766 goto init_done;
5767
5768 /*
5769 * Verify termination based on current draw and
5770 * warn user if the bus is over/under terminated.
5771 */
5772 error = ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL,
5773 CURSENSE_ENB);
5774 if (error != 0) {
5775 printf("%s: current sensing timeout 1\n", ahd_name(ahd));
5776 goto init_done;
5777 }
5778 for (i = 20, fstat = FLX_FSTAT_BUSY;
5779 (fstat & FLX_FSTAT_BUSY) != 0 && i; i--) {
5780 error = ahd_read_flexport(ahd, FLXADDR_FLEXSTAT, &fstat);
5781 if (error != 0) {
5782 printf("%s: current sensing timeout 2\n",
5783 ahd_name(ahd));
5784 goto init_done;
5785 }
5786 }
5787 if (i == 0) {
5788 printf("%s: Timedout during current-sensing test\n",
5789 ahd_name(ahd));
5790 goto init_done;
5791 }
5792
5793 /* Latch Current Sensing status. */
5794 error = ahd_read_flexport(ahd, FLXADDR_CURRENT_STAT, ¤t_sensing);
5795 if (error != 0) {
5796 printf("%s: current sensing timeout 3\n", ahd_name(ahd));
5797 goto init_done;
5798 }
5799
5800 /* Diable current sensing. */
5801 ahd_write_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, 0);
5802
5803 #ifdef AHD_DEBUG
5804 if ((ahd_debug & AHD_SHOW_TERMCTL) != 0) {
5805 printf("%s: current_sensing == 0x%x\n",
5806 ahd_name(ahd), current_sensing);
5807 }
5808 #endif
5809 warn_user = 0;
5810 for (i = 0; i < 4; i++, current_sensing >>= FLX_CSTAT_SHIFT) {
5811 u_int term_stat;
5812
5813 term_stat = (current_sensing & FLX_CSTAT_MASK);
5814 switch (term_stat) {
5815 case FLX_CSTAT_OVER:
5816 case FLX_CSTAT_UNDER:
5817 warn_user++;
5818 case FLX_CSTAT_INVALID:
5819 case FLX_CSTAT_OKAY:
5820 if (warn_user == 0 && bootverbose == 0)
5821 break;
5822 printf("%s: %s Channel %s\n", ahd_name(ahd),
5823 channel_strings[i], termstat_strings[term_stat]);
5824 break;
5825 }
5826 }
5827 if (warn_user) {
5828 printf("%s: WARNING. Termination is not configured correctly.\n"
5829 "%s: WARNING. SCSI bus operations may FAIL.\n",
5830 ahd_name(ahd), ahd_name(ahd));
5831 }
5832 init_done:
5833 ahd_reset_current_bus(ahd);
5834 ahd_restart(ahd);
5835 ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
5836 ahd_stat_timer, ahd);
5837
5838 /* We have to wait until after any system dumps... */
5839 ahd->shutdown_hook = shutdownhook_establish(ahd_shutdown, ahd);
5840
5841 return (0);
5842 }
5843
5844 /*
5845 * (Re)initialize chip state after a chip reset.
5846 */
5847 static void
5848 ahd_chip_init(struct ahd_softc *ahd)
5849 {
5850 uint32_t busaddr;
5851 u_int sxfrctl1;
5852 u_int scsiseq_template;
5853 u_int wait;
5854 u_int i;
5855 u_int target;
5856
5857 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
5858 /*
5859 * Take the LED out of diagnostic mode
5860 */
5861 ahd_outb(ahd, SBLKCTL, ahd_inb(ahd, SBLKCTL) & ~(DIAGLEDEN|DIAGLEDON));
5862
5863 /*
5864 * Return HS_MAILBOX to its default value.
5865 */
5866 ahd->hs_mailbox = 0;
5867 ahd_outb(ahd, HS_MAILBOX, 0);
5868
5869 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1. */
5870 ahd_outb(ahd, IOWNID, ahd->our_id);
5871 ahd_outb(ahd, TOWNID, ahd->our_id);
5872 sxfrctl1 = (ahd->flags & AHD_TERM_ENB_A) != 0 ? STPWEN : 0;
5873 sxfrctl1 |= (ahd->flags & AHD_SPCHK_ENB_A) != 0 ? ENSPCHK : 0;
5874 if ((ahd->bugs & AHD_LONG_SETIMO_BUG)
5875 && (ahd->seltime != STIMESEL_MIN)) {
5876 /*
5877 * The selection timer duration is twice as long
5878 * as it should be. Halve it by adding "1" to
5879 * the user specified setting.
5880 */
5881 sxfrctl1 |= ahd->seltime + STIMESEL_BUG_ADJ;
5882 } else {
5883 sxfrctl1 |= ahd->seltime;
5884 }
5885
5886 ahd_outb(ahd, SXFRCTL0, DFON);
5887 ahd_outb(ahd, SXFRCTL1, sxfrctl1|ahd->seltime|ENSTIMER|ACTNEGEN);
5888 ahd_outb(ahd, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
5889
5890 /*
5891 * Now that termination is set, wait for up
5892 * to 500ms for our transceivers to settle. If
5893 * the adapter does not have a cable attached,
5894 * the transceivers may never settle, so don't
5895 * complain if we fail here.
5896 */
5897 for (wait = 10000;
5898 (ahd_inb(ahd, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
5899 wait--)
5900 ahd_delay(100);
5901
5902 /* Clear any false bus resets due to the transceivers settling */
5903 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
5904 ahd_outb(ahd, CLRINT, CLRSCSIINT);
5905
5906 /* Initialize mode specific S/G state. */
5907 for (i = 0; i < 2; i++) {
5908 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
5909 ahd_outb(ahd, LONGJMP_ADDR + 1, INVALID_ADDR);
5910 ahd_outb(ahd, SG_STATE, 0);
5911 ahd_outb(ahd, CLRSEQINTSRC, 0xFF);
5912 ahd_outb(ahd, SEQIMODE,
5913 ENSAVEPTRS|ENCFG4DATA|ENCFG4ISTAT
5914 |ENCFG4TSTAT|ENCFG4ICMD|ENCFG4TCMD);
5915 }
5916
5917 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
5918 ahd_outb(ahd, DSCOMMAND0, ahd_inb(ahd, DSCOMMAND0)|MPARCKEN|CACHETHEN);
5919 ahd_outb(ahd, DFF_THRSH, RD_DFTHRSH_75|WR_DFTHRSH_75);
5920 ahd_outb(ahd, SIMODE0, ENIOERR|ENOVERRUN);
5921 ahd_outb(ahd, SIMODE3, ENNTRAMPERR|ENOSRAMPERR);
5922 if ((ahd->bugs & AHD_BUSFREEREV_BUG) != 0) {
5923 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|AUTO_MSGOUT_DE);
5924 } else {
5925 ahd_outb(ahd, OPTIONMODE, AUTOACKEN|BUSFREEREV|AUTO_MSGOUT_DE);
5926 }
5927 ahd_outb(ahd, SCSCHKN, CURRFIFODEF|WIDERESEN|SHVALIDSTDIS);
5928 if ((ahd->chip & AHD_BUS_MASK) == AHD_PCIX)
5929 /*
5930 * Do not issue a target abort when a split completion
5931 * error occurs. Let our PCIX interrupt handler deal
5932 * with it instead. H2A4 Razor #625
5933 */
5934 ahd_outb(ahd, PCIXCTL, ahd_inb(ahd, PCIXCTL) | SPLTSTADIS);
5935
5936 if ((ahd->bugs & AHD_LQOOVERRUN_BUG) != 0)
5937 ahd_outb(ahd, LQOSCSCTL, LQONOCHKOVER);
5938
5939 /*
5940 * Tweak IOCELL settings.
5941 */
5942 if ((ahd->flags & AHD_HP_BOARD) != 0) {
5943 for (i = 0; i < NUMDSPS; i++) {
5944 ahd_outb(ahd, DSPSELECT, i);
5945 ahd_outb(ahd, WRTBIASCTL, WRTBIASCTL_HP_DEFAULT);
5946 }
5947 #ifdef AHD_DEBUG
5948 if ((ahd_debug & AHD_SHOW_MISC) != 0)
5949 printf("%s: WRTBIASCTL now 0x%x\n", ahd_name(ahd),
5950 WRTBIASCTL_HP_DEFAULT);
5951 #endif
5952 }
5953 ahd_setup_iocell_workaround(ahd);
5954
5955 /*
5956 * Enable LQI Manager interrupts.
5957 */
5958 ahd_outb(ahd, LQIMODE1, ENLQIPHASE_LQ|ENLQIPHASE_NLQ|ENLIQABORT
5959 | ENLQICRCI_LQ|ENLQICRCI_NLQ|ENLQIBADLQI
5960 | ENLQIOVERI_LQ|ENLQIOVERI_NLQ);
5961 ahd_outb(ahd, LQOMODE0, ENLQOATNLQ|ENLQOATNPKT|ENLQOTCRC);
5962 /*
5963 * An interrupt from LQOBUSFREE is made redundant by the
5964 * BUSFREE interrupt. We choose to have the sequencer catch
5965 * LQOPHCHGINPKT errors manually for the command phase at the
5966 * start of a packetized selection case.
5967 ahd_outb(ahd, LQOMODE1, ENLQOBUSFREE|ENLQOPHACHGINPKT);
5968 */
5969 ahd_outb(ahd, LQOMODE1, 0);
5970
5971 /*
5972 * Setup sequencer interrupt handlers.
5973 */
5974 ahd_outw(ahd, INTVEC1_ADDR, ahd_resolve_seqaddr(ahd, LABEL_seq_isr));
5975 ahd_outw(ahd, INTVEC2_ADDR, ahd_resolve_seqaddr(ahd, LABEL_timer_isr));
5976
5977 /*
5978 * Setup SCB Offset registers.
5979 */
5980 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
5981 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb,
5982 pkt_long_lun));
5983 } else {
5984 ahd_outb(ahd, LUNPTR, offsetof(struct hardware_scb, lun));
5985 }
5986 ahd_outb(ahd, CMDLENPTR, offsetof(struct hardware_scb, cdb_len));
5987 ahd_outb(ahd, ATTRPTR, offsetof(struct hardware_scb, task_attribute));
5988 ahd_outb(ahd, FLAGPTR, offsetof(struct hardware_scb, task_management));
5989 ahd_outb(ahd, CMDPTR, offsetof(struct hardware_scb,
5990 shared_data.idata.cdb));
5991 ahd_outb(ahd, QNEXTPTR,
5992 offsetof(struct hardware_scb, next_hscb_busaddr));
5993 ahd_outb(ahd, ABRTBITPTR, MK_MESSAGE_BIT_OFFSET);
5994 ahd_outb(ahd, ABRTBYTEPTR, offsetof(struct hardware_scb, control));
5995 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0) {
5996 ahd_outb(ahd, LUNLEN,
5997 sizeof(ahd->next_queued_hscb->pkt_long_lun) - 1);
5998 } else {
5999 ahd_outb(ahd, LUNLEN, sizeof(ahd->next_queued_hscb->lun) - 1);
6000 }
6001 ahd_outb(ahd, CDBLIMIT, SCB_CDB_LEN_PTR - 1);
6002 ahd_outb(ahd, MAXCMD, 0xFF);
6003 ahd_outb(ahd, SCBAUTOPTR,
6004 AUSCBPTR_EN | offsetof(struct hardware_scb, tag));
6005
6006 /* We haven't been enabled for target mode yet. */
6007 ahd_outb(ahd, MULTARGID, 0);
6008 ahd_outb(ahd, MULTARGID + 1, 0);
6009
6010 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6011 /* Initialize the negotiation table. */
6012 if ((ahd->features & AHD_NEW_IOCELL_OPTS) == 0) {
6013 /*
6014 * Clear the spare bytes in the neg table to avoid
6015 * spurious parity errors.
6016 */
6017 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6018 ahd_outb(ahd, NEGOADDR, target);
6019 ahd_outb(ahd, ANNEXCOL, AHD_ANNEXCOL_PER_DEV0);
6020 for (i = 0; i < AHD_NUM_PER_DEV_ANNEXCOLS; i++)
6021 ahd_outb(ahd, ANNEXDAT, 0);
6022 }
6023 }
6024
6025 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6026 struct ahd_devinfo devinfo;
6027 struct ahd_initiator_tinfo *tinfo;
6028 struct ahd_tmode_tstate *tstate;
6029
6030 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6031 target, &tstate);
6032 ahd_compile_devinfo(&devinfo, ahd->our_id,
6033 target, CAM_LUN_WILDCARD,
6034 'A', ROLE_INITIATOR);
6035 ahd_update_neg_table(ahd, &devinfo, &tinfo->curr);
6036 }
6037
6038 ahd_outb(ahd, CLRSINT3, NTRAMPERR|OSRAMPERR);
6039 ahd_outb(ahd, CLRINT, CLRSCSIINT);
6040
6041 /*
6042 * Always enable abort on incoming L_Qs if this feature is
6043 * supported. We use this to catch invalid SCB references.
6044 */
6045 if ((ahd->bugs & AHD_ABORT_LQI_BUG) == 0)
6046 ahd_outb(ahd, LQCTL1, ABORTPENDING);
6047 else
6048 ahd_outb(ahd, LQCTL1, 0);
6049
6050 /* All of our queues are empty */
6051 ahd->qoutfifonext = 0;
6052 ahd->qoutfifonext_valid_tag = QOUTFIFO_ENTRY_VALID_LE;
6053 ahd_outb(ahd, QOUTFIFO_ENTRY_VALID_TAG, QOUTFIFO_ENTRY_VALID >> 8);
6054 for (i = 0; i < AHD_QOUT_SIZE; i++)
6055 ahd->qoutfifo[i] = 0;
6056 ahd_sync_qoutfifo(ahd, BUS_DMASYNC_PREREAD);
6057
6058 ahd->qinfifonext = 0;
6059 for (i = 0; i < AHD_QIN_SIZE; i++)
6060 ahd->qinfifo[i] = SCB_LIST_NULL;
6061
6062 if ((ahd->features & AHD_TARGETMODE) != 0) {
6063 /* All target command blocks start out invalid. */
6064 for (i = 0; i < AHD_TMODE_CMDS; i++)
6065 ahd->targetcmds[i].cmd_valid = 0;
6066 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_PREREAD);
6067 ahd->tqinfifonext = 1;
6068 ahd_outb(ahd, KERNEL_TQINPOS, ahd->tqinfifonext - 1);
6069 ahd_outb(ahd, TQINPOS, ahd->tqinfifonext);
6070 }
6071
6072 /* Initialize Scratch Ram. */
6073 ahd_outb(ahd, SEQ_FLAGS, 0);
6074 ahd_outb(ahd, SEQ_FLAGS2, 0);
6075
6076 /* We don't have any waiting selections */
6077 ahd_outw(ahd, WAITING_TID_HEAD, SCB_LIST_NULL);
6078 ahd_outw(ahd, WAITING_TID_TAIL, SCB_LIST_NULL);
6079 for (i = 0; i < AHD_NUM_TARGETS; i++) {
6080 ahd_outw(ahd, WAITING_SCB_TAILS + (2 * i), SCB_LIST_NULL);
6081 }
6082
6083 /*
6084 * Nobody is waiting to be DMAed into the QOUTFIFO.
6085 */
6086 ahd_outw(ahd, COMPLETE_SCB_HEAD, SCB_LIST_NULL);
6087 ahd_outw(ahd, COMPLETE_SCB_DMAINPROG_HEAD, SCB_LIST_NULL);
6088 ahd_outw(ahd, COMPLETE_DMA_SCB_HEAD, SCB_LIST_NULL);
6089
6090 /*
6091 * The Freeze Count is 0.
6092 */
6093 ahd_outw(ahd, QFREEZE_COUNT, 0);
6094
6095 /*
6096 * Tell the sequencer where it can find our arrays in memory.
6097 */
6098 busaddr = ahd->shared_data_map.physaddr;
6099 ahd_outb(ahd, SHARED_DATA_ADDR, busaddr & 0xFF);
6100 ahd_outb(ahd, SHARED_DATA_ADDR + 1, (busaddr >> 8) & 0xFF);
6101 ahd_outb(ahd, SHARED_DATA_ADDR + 2, (busaddr >> 16) & 0xFF);
6102 ahd_outb(ahd, SHARED_DATA_ADDR + 3, (busaddr >> 24) & 0xFF);
6103 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR, busaddr & 0xFF);
6104 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 1, (busaddr >> 8) & 0xFF);
6105 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 2, (busaddr >> 16) & 0xFF);
6106 ahd_outb(ahd, QOUTFIFO_NEXT_ADDR + 3, (busaddr >> 24) & 0xFF);
6107 /*
6108 * Setup the allowed SCSI Sequences based on operational mode.
6109 * If we are a target, we'll enable select in operations once
6110 * we've had a lun enabled.
6111 */
6112 scsiseq_template = ENAUTOATNP;
6113 if ((ahd->flags & AHD_INITIATORROLE) != 0)
6114 scsiseq_template |= ENRSELI;
6115 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq_template);
6116
6117 /* There are no busy SCBs yet. */
6118 for (target = 0; target < AHD_NUM_TARGETS; target++) {
6119 int lun;
6120
6121 for (lun = 0; lun < AHD_NUM_LUNS_NONPKT; lun++)
6122 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(target, 'A', lun));
6123 }
6124
6125 /*
6126 * Initialize the group code to command length table.
6127 * Vendor Unique codes are set to 0 so we only capture
6128 * the first byte of the cdb. These can be overridden
6129 * when target mode is enabled.
6130 */
6131 ahd_outb(ahd, CMDSIZE_TABLE, 5);
6132 ahd_outb(ahd, CMDSIZE_TABLE + 1, 9);
6133 ahd_outb(ahd, CMDSIZE_TABLE + 2, 9);
6134 ahd_outb(ahd, CMDSIZE_TABLE + 3, 0);
6135 ahd_outb(ahd, CMDSIZE_TABLE + 4, 15);
6136 ahd_outb(ahd, CMDSIZE_TABLE + 5, 11);
6137 ahd_outb(ahd, CMDSIZE_TABLE + 6, 0);
6138 ahd_outb(ahd, CMDSIZE_TABLE + 7, 0);
6139
6140 /* Tell the sequencer of our initial queue positions */
6141 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6142 ahd_outb(ahd, QOFF_CTLSTA, SCB_QSIZE_512);
6143 ahd->qinfifonext = 0;
6144 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6145 ahd_set_hescb_qoff(ahd, 0);
6146 ahd_set_snscb_qoff(ahd, 0);
6147 ahd_set_sescb_qoff(ahd, 0);
6148 ahd_set_sdscb_qoff(ahd, 0);
6149
6150 /*
6151 * Tell the sequencer which SCB will be the next one it receives.
6152 */
6153 busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
6154 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
6155 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
6156 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
6157 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
6158
6159 /*
6160 * Default to coalescing disabled.
6161 */
6162 ahd_outw(ahd, INT_COALESCING_CMDCOUNT, 0);
6163 ahd_outw(ahd, CMDS_PENDING, 0);
6164 ahd_update_coalescing_values(ahd, ahd->int_coalescing_timer,
6165 ahd->int_coalescing_maxcmds,
6166 ahd->int_coalescing_mincmds);
6167 ahd_enable_coalescing(ahd, FALSE);
6168
6169 ahd_loadseq(ahd);
6170 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6171 }
6172
6173 /*
6174 * Setup default device and controller settings.
6175 * This should only be called if our probe has
6176 * determined that no configuration data is available.
6177 */
6178 int
6179 ahd_default_config(struct ahd_softc *ahd)
6180 {
6181 int targ;
6182
6183 ahd->our_id = 7;
6184
6185 /*
6186 * Allocate a tstate to house information for our
6187 * initiator presence on the bus as well as the user
6188 * data for any target mode initiator.
6189 */
6190 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
6191 printf("%s: unable to allocate ahd_tmode_tstate. "
6192 "Failing attach\n", ahd_name(ahd));
6193 return (ENOMEM);
6194 }
6195
6196 for (targ = 0; targ < AHD_NUM_TARGETS; targ++) {
6197 struct ahd_devinfo devinfo;
6198 struct ahd_initiator_tinfo *tinfo;
6199 struct ahd_tmode_tstate *tstate;
6200 uint16_t target_mask;
6201
6202 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6203 targ, &tstate);
6204 /*
6205 * We support SPC2 and SPI4.
6206 */
6207 tinfo->user.protocol_version = 4;
6208 tinfo->user.transport_version = 4;
6209
6210 target_mask = 0x01 << targ;
6211 ahd->user_discenable |= target_mask;
6212 tstate->discenable |= target_mask;
6213 ahd->user_tagenable |= target_mask;
6214 #ifdef AHD_FORCE_160
6215 tinfo->user.period = AHD_SYNCRATE_DT;
6216 #else
6217 tinfo->user.period = AHD_SYNCRATE_160;
6218 #endif
6219 tinfo->user.offset= MAX_OFFSET;
6220 tinfo->user.ppr_options = MSG_EXT_PPR_RDSTRM
6221 | MSG_EXT_PPR_WRFLOW
6222 | MSG_EXT_PPR_HOLDMCS
6223 | MSG_EXT_PPR_IU_REQ
6224 | MSG_EXT_PPR_QAS_REQ
6225 | MSG_EXT_PPR_DT_REQ;
6226 if ((ahd->features & AHD_RTI) != 0)
6227 tinfo->user.ppr_options |= MSG_EXT_PPR_RTI;
6228
6229 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
6230
6231 /*
6232 * Start out Async/Narrow/Untagged and with
6233 * conservative protocol support.
6234 */
6235 tinfo->goal.protocol_version = 2;
6236 tinfo->goal.transport_version = 2;
6237 tinfo->curr.protocol_version = 2;
6238 tinfo->curr.transport_version = 2;
6239 ahd_compile_devinfo(&devinfo, ahd->our_id,
6240 targ, CAM_LUN_WILDCARD,
6241 'A', ROLE_INITIATOR);
6242 tstate->tagenable &= ~target_mask;
6243 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6244 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
6245 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
6246 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
6247 /*paused*/TRUE);
6248 }
6249 return (0);
6250 }
6251
6252 /*
6253 * Parse device configuration information.
6254 */
6255 int
6256 ahd_parse_cfgdata(struct ahd_softc *ahd, struct seeprom_config *sc)
6257 {
6258 int targ;
6259 int max_targ;
6260
6261 max_targ = sc->max_targets & CFMAXTARG;
6262 ahd->our_id = sc->brtime_id & CFSCSIID;
6263
6264 /*
6265 * Allocate a tstate to house information for our
6266 * initiator presence on the bus as well as the user
6267 * data for any target mode initiator.
6268 */
6269 if (ahd_alloc_tstate(ahd, ahd->our_id, 'A') == NULL) {
6270 printf("%s: unable to allocate ahd_tmode_tstate. "
6271 "Failing attach\n", ahd_name(ahd));
6272 return (ENOMEM);
6273 }
6274
6275 for (targ = 0; targ < max_targ; targ++) {
6276 struct ahd_devinfo devinfo;
6277 struct ahd_initiator_tinfo *tinfo;
6278 struct ahd_transinfo *user_tinfo;
6279 struct ahd_tmode_tstate *tstate;
6280 uint16_t target_mask;
6281
6282 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
6283 targ, &tstate);
6284 user_tinfo = &tinfo->user;
6285
6286 /*
6287 * We support SPC2 and SPI4.
6288 */
6289 tinfo->user.protocol_version = 4;
6290 tinfo->user.transport_version = 4;
6291
6292 target_mask = 0x01 << targ;
6293 ahd->user_discenable &= ~target_mask;
6294 tstate->discenable &= ~target_mask;
6295 ahd->user_tagenable &= ~target_mask;
6296 if (sc->device_flags[targ] & CFDISC) {
6297 tstate->discenable |= target_mask;
6298 ahd->user_discenable |= target_mask;
6299 ahd->user_tagenable |= target_mask;
6300 } else {
6301 /*
6302 * Cannot be packetized without disconnection.
6303 */
6304 sc->device_flags[targ] &= ~CFPACKETIZED;
6305 }
6306
6307 user_tinfo->ppr_options = 0;
6308 user_tinfo->period = (sc->device_flags[targ] & CFXFER);
6309 if (user_tinfo->period < CFXFER_ASYNC) {
6310 if (user_tinfo->period <= AHD_PERIOD_10MHz)
6311 user_tinfo->ppr_options |= MSG_EXT_PPR_DT_REQ;
6312 user_tinfo->offset = MAX_OFFSET;
6313 } else {
6314 user_tinfo->offset = 0;
6315 user_tinfo->period = AHD_ASYNC_XFER_PERIOD;
6316 }
6317 #ifdef AHD_FORCE_160
6318 if (user_tinfo->period <= AHD_SYNCRATE_160)
6319 user_tinfo->period = AHD_SYNCRATE_DT;
6320 #endif
6321
6322 if ((sc->device_flags[targ] & CFPACKETIZED) != 0) {
6323 user_tinfo->ppr_options |= MSG_EXT_PPR_RDSTRM
6324 | MSG_EXT_PPR_WRFLOW
6325 | MSG_EXT_PPR_HOLDMCS
6326 | MSG_EXT_PPR_IU_REQ;
6327 if ((ahd->features & AHD_RTI) != 0)
6328 user_tinfo->ppr_options |= MSG_EXT_PPR_RTI;
6329 }
6330
6331 if ((sc->device_flags[targ] & CFQAS) != 0)
6332 user_tinfo->ppr_options |= MSG_EXT_PPR_QAS_REQ;
6333
6334 if ((sc->device_flags[targ] & CFWIDEB) != 0)
6335 user_tinfo->width = MSG_EXT_WDTR_BUS_16_BIT;
6336 else
6337 user_tinfo->width = MSG_EXT_WDTR_BUS_8_BIT;
6338 #ifdef AHD_DEBUG
6339 if ((ahd_debug & AHD_SHOW_MISC) != 0)
6340 printf("(%d): %x:%x:%x:%x\n", targ, user_tinfo->width,
6341 user_tinfo->period, user_tinfo->offset,
6342 user_tinfo->ppr_options);
6343 #endif
6344 /*
6345 * Start out Async/Narrow/Untagged and with
6346 * conservative protocol support.
6347 */
6348 tstate->tagenable &= ~target_mask;
6349 tinfo->goal.protocol_version = 2;
6350 tinfo->goal.transport_version = 2;
6351 tinfo->curr.protocol_version = 2;
6352 tinfo->curr.transport_version = 2;
6353 ahd_compile_devinfo(&devinfo, ahd->our_id,
6354 targ, CAM_LUN_WILDCARD,
6355 'A', ROLE_INITIATOR);
6356 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6357 AHD_TRANS_CUR|AHD_TRANS_GOAL, /*paused*/TRUE);
6358 ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
6359 /*ppr_options*/0, AHD_TRANS_CUR|AHD_TRANS_GOAL,
6360 /*paused*/TRUE);
6361 }
6362
6363 ahd->flags &= ~AHD_SPCHK_ENB_A;
6364 if (sc->bios_control & CFSPARITY)
6365 ahd->flags |= AHD_SPCHK_ENB_A;
6366
6367 ahd->flags &= ~AHD_RESET_BUS_A;
6368 if (sc->bios_control & CFRESETB)
6369 ahd->flags |= AHD_RESET_BUS_A;
6370
6371 ahd->flags &= ~AHD_EXTENDED_TRANS_A;
6372 if (sc->bios_control & CFEXTEND)
6373 ahd->flags |= AHD_EXTENDED_TRANS_A;
6374
6375 ahd->flags &= ~AHD_BIOS_ENABLED;
6376 if ((sc->bios_control & CFBIOSSTATE) == CFBS_ENABLED)
6377 ahd->flags |= AHD_BIOS_ENABLED;
6378
6379 ahd->flags &= ~AHD_STPWLEVEL_A;
6380 if ((sc->adapter_control & CFSTPWLEVEL) != 0)
6381 ahd->flags |= AHD_STPWLEVEL_A;
6382
6383 return (0);
6384 }
6385
6386 /*
6387 * Parse device configuration information.
6388 */
6389 int
6390 ahd_parse_vpddata(struct ahd_softc *ahd, struct vpd_config *vpd)
6391 {
6392 int error;
6393
6394 error = ahd_verify_vpd_cksum(vpd);
6395 if (error == 0)
6396 return (EINVAL);
6397 if ((vpd->bios_flags & VPDBOOTHOST) != 0)
6398 ahd->flags |= AHD_BOOT_CHANNEL;
6399 return (0);
6400 }
6401
6402 void
6403 ahd_intr_enable(struct ahd_softc *ahd, int enable)
6404 {
6405 u_int hcntrl;
6406
6407 hcntrl = ahd_inb(ahd, HCNTRL);
6408 hcntrl &= ~INTEN;
6409 ahd->pause &= ~INTEN;
6410 ahd->unpause &= ~INTEN;
6411 if (enable) {
6412 hcntrl |= INTEN;
6413 ahd->pause |= INTEN;
6414 ahd->unpause |= INTEN;
6415 }
6416 ahd_outb(ahd, HCNTRL, hcntrl);
6417 }
6418
6419 void
6420 ahd_update_coalescing_values(struct ahd_softc *ahd, u_int timer, u_int maxcmds,
6421 u_int mincmds)
6422 {
6423 if (timer > AHD_TIMER_MAX_US)
6424 timer = AHD_TIMER_MAX_US;
6425 ahd->int_coalescing_timer = timer;
6426
6427 if (maxcmds > AHD_INT_COALESCING_MAXCMDS_MAX)
6428 maxcmds = AHD_INT_COALESCING_MAXCMDS_MAX;
6429 if (mincmds > AHD_INT_COALESCING_MINCMDS_MAX)
6430 mincmds = AHD_INT_COALESCING_MINCMDS_MAX;
6431 ahd->int_coalescing_maxcmds = maxcmds;
6432 ahd_outw(ahd, INT_COALESCING_TIMER, timer / AHD_TIMER_US_PER_TICK);
6433 ahd_outb(ahd, INT_COALESCING_MAXCMDS, -maxcmds);
6434 ahd_outb(ahd, INT_COALESCING_MINCMDS, -mincmds);
6435 }
6436
6437 void
6438 ahd_enable_coalescing(struct ahd_softc *ahd, int enable)
6439 {
6440
6441 ahd->hs_mailbox &= ~ENINT_COALESCE;
6442 if (enable)
6443 ahd->hs_mailbox |= ENINT_COALESCE;
6444 ahd_outb(ahd, HS_MAILBOX, ahd->hs_mailbox);
6445 ahd_flush_device_writes(ahd);
6446 ahd_run_qoutfifo(ahd);
6447 }
6448
6449 /*
6450 * Ensure that the card is paused in a location
6451 * outside of all critical sections and that all
6452 * pending work is completed prior to returning.
6453 * This routine should only be called from outside
6454 * an interrupt context.
6455 */
6456 void
6457 ahd_pause_and_flushwork(struct ahd_softc *ahd)
6458 {
6459 u_int intstat;
6460 u_int maxloops;
6461 u_int qfreeze_cnt;
6462
6463 maxloops = 1000;
6464 ahd->flags |= AHD_ALL_INTERRUPTS;
6465 ahd_pause(ahd);
6466 /*
6467 * Increment the QFreeze Count so that the sequencer
6468 * will not start new selections. We do this only
6469 * until we are safely paused without further selections
6470 * pending.
6471 */
6472 ahd_outw(ahd, QFREEZE_COUNT, ahd_inw(ahd, QFREEZE_COUNT) + 1);
6473 ahd_outb(ahd, SEQ_FLAGS2, ahd_inb(ahd, SEQ_FLAGS2) | SELECTOUT_QFROZEN);
6474 do {
6475 struct scb *waiting_scb;
6476
6477 ahd_unpause(ahd);
6478 ahd_intr(ahd);
6479 ahd_pause(ahd);
6480 ahd_clear_critical_section(ahd);
6481 intstat = ahd_inb(ahd, INTSTAT);
6482 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
6483 if ((ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) == 0)
6484 ahd_outb(ahd, SCSISEQ0,
6485 ahd_inb(ahd, SCSISEQ0) & ~ENSELO);
6486 /*
6487 * In the non-packetized case, the sequencer (for Rev A),
6488 * relies on ENSELO remaining set after SELDO. The hardware
6489 * auto-clears ENSELO in the packetized case.
6490 */
6491 waiting_scb = ahd_lookup_scb(ahd,
6492 ahd_inw(ahd, WAITING_TID_HEAD));
6493 if (waiting_scb != NULL
6494 && (waiting_scb->flags & SCB_PACKETIZED) == 0
6495 && (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0)
6496 ahd_outb(ahd, SCSISEQ0,
6497 ahd_inb(ahd, SCSISEQ0) | ENSELO);
6498 } while (--maxloops
6499 && (intstat != 0xFF || (ahd->features & AHD_REMOVABLE) == 0)
6500 && ((intstat & INT_PEND) != 0
6501 || (ahd_inb(ahd, SCSISEQ0) & ENSELO) != 0
6502 || (ahd_inb(ahd, SSTAT0) & (SELDO|SELINGO)) != 0));
6503 if (maxloops == 0) {
6504 printf("Infinite interrupt loop, INTSTAT = %x",
6505 ahd_inb(ahd, INTSTAT));
6506 }
6507 qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
6508 if (qfreeze_cnt == 0) {
6509 printf("%s: ahd_pause_and_flushwork with 0 qfreeze count!\n",
6510 ahd_name(ahd));
6511 } else {
6512 qfreeze_cnt--;
6513 }
6514 ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
6515 if (qfreeze_cnt == 0)
6516 ahd_outb(ahd, SEQ_FLAGS2,
6517 ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
6518
6519 ahd_flush_qoutfifo(ahd);
6520
6521 ahd_platform_flushwork(ahd);
6522 ahd->flags &= ~AHD_ALL_INTERRUPTS;
6523 }
6524
6525 int
6526 ahd_suspend(struct ahd_softc *ahd)
6527 {
6528 #if 0
6529 uint8_t *ptr;
6530 int i;
6531
6532 ahd_pause_and_flushwork(ahd);
6533
6534 if (LIST_FIRST(&ahd->pending_scbs) != NULL)
6535 return (EBUSY);
6536
6537 #if AHD_TARGET_MODE
6538 /*
6539 * XXX What about ATIOs that have not yet been serviced?
6540 * Perhaps we should just refuse to be suspended if we
6541 * are acting in a target role.
6542 */
6543 if (ahd->pending_device != NULL)
6544 return (EBUSY);
6545 #endif
6546
6547 /* Save volatile registers */
6548 ahd->suspend_state.channel[0].scsiseq = ahd_inb(ahd, SCSISEQ0);
6549 ahd->suspend_state.channel[0].sxfrctl0 = ahd_inb(ahd, SXFRCTL0);
6550 ahd->suspend_state.channel[0].sxfrctl1 = ahd_inb(ahd, SXFRCTL1);
6551 ahd->suspend_state.channel[0].simode0 = ahd_inb(ahd, SIMODE0);
6552 ahd->suspend_state.channel[0].simode1 = ahd_inb(ahd, SIMODE1);
6553 ahd->suspend_state.channel[0].seltimer = ahd_inb(ahd, SELTIMER);
6554 ahd->suspend_state.channel[0].seqctl = ahd_inb(ahd, SEQCTL0);
6555 ahd->suspend_state.dscommand0 = ahd_inb(ahd, DSCOMMAND0);
6556 ahd->suspend_state.dspcistatus = ahd_inb(ahd, DSPCISTATUS);
6557
6558 if ((ahd->features & AHD_DT) != 0) {
6559 u_int sfunct;
6560
6561 sfunct = ahd_inb(ahd, SFUNCT) & ~ALT_MODE;
6562 ahd_outb(ahd, SFUNCT, sfunct | ALT_MODE);
6563 ahd->suspend_state.optionmode = ahd_inb(ahd, OPTIONMODE);
6564 ahd_outb(ahd, SFUNCT, sfunct);
6565 ahd->suspend_state.crccontrol1 = ahd_inb(ahd, CRCCONTROL1);
6566 }
6567
6568 if ((ahd->features & AHD_MULTI_FUNC) != 0)
6569 ahd->suspend_state.scbbaddr = ahd_inb(ahd, SCBBADDR);
6570
6571 if ((ahd->features & AHD_ULTRA2) != 0)
6572 ahd->suspend_state.dff_thrsh = ahd_inb(ahd, DFF_THRSH);
6573
6574 ptr = ahd->suspend_state.scratch_ram;
6575 for (i = 0; i < 64; i++)
6576 *ptr++ = ahd_inb(ahd, SRAM_BASE + i);
6577
6578 if ((ahd->features & AHD_MORE_SRAM) != 0) {
6579 for (i = 0; i < 16; i++)
6580 *ptr++ = ahd_inb(ahd, TARG_OFFSET + i);
6581 }
6582
6583 ptr = ahd->suspend_state.btt;
6584 for (i = 0;i < AHD_NUM_TARGETS; i++) {
6585 int j;
6586
6587 for (j = 0;j < AHD_NUM_LUNS_NONPKT; j++) {
6588 u_int tcl;
6589
6590 tcl = BUILD_TCL_RAW(i, 'A', j);
6591 *ptr = ahd_find_busy_tcl(ahd, tcl);
6592 }
6593 }
6594 ahd_shutdown(ahd);
6595 #endif
6596 return (0);
6597 }
6598
6599 int
6600 ahd_resume(struct ahd_softc *ahd)
6601 {
6602 #if 0
6603 uint8_t *ptr;
6604 int i;
6605
6606 ahd_reset(ahd);
6607
6608 ahd_build_free_scb_list(ahd);
6609
6610 /* Restore volatile registers */
6611 ahd_outb(ahd, SCSISEQ0, ahd->suspend_state.channel[0].scsiseq);
6612 ahd_outb(ahd, SXFRCTL0, ahd->suspend_state.channel[0].sxfrctl0);
6613 ahd_outb(ahd, SXFRCTL1, ahd->suspend_state.channel[0].sxfrctl1);
6614 ahd_outb(ahd, SIMODE0, ahd->suspend_state.channel[0].simode0);
6615 ahd_outb(ahd, SIMODE1, ahd->suspend_state.channel[0].simode1);
6616 ahd_outb(ahd, SELTIMER, ahd->suspend_state.channel[0].seltimer);
6617 ahd_outb(ahd, SEQCTL0, ahd->suspend_state.channel[0].seqctl);
6618 if ((ahd->features & AHD_ULTRA2) != 0)
6619 ahd_outb(ahd, SCSIID_ULTRA2, ahd->our_id);
6620 else
6621 ahd_outb(ahd, SCSIID, ahd->our_id);
6622
6623 ahd_outb(ahd, DSCOMMAND0, ahd->suspend_state.dscommand0);
6624 ahd_outb(ahd, DSPCISTATUS, ahd->suspend_state.dspcistatus);
6625
6626 if ((ahd->features & AHD_DT) != 0) {
6627 u_int sfunct;
6628
6629 sfunct = ahd_inb(ahd, SFUNCT) & ~ALT_MODE;
6630 ahd_outb(ahd, SFUNCT, sfunct | ALT_MODE);
6631 ahd_outb(ahd, OPTIONMODE, ahd->suspend_state.optionmode);
6632 ahd_outb(ahd, SFUNCT, sfunct);
6633 ahd_outb(ahd, CRCCONTROL1, ahd->suspend_state.crccontrol1);
6634 }
6635
6636 if ((ahd->features & AHD_MULTI_FUNC) != 0)
6637 ahd_outb(ahd, SCBBADDR, ahd->suspend_state.scbbaddr);
6638
6639 if ((ahd->features & AHD_ULTRA2) != 0)
6640 ahd_outb(ahd, DFF_THRSH, ahd->suspend_state.dff_thrsh);
6641
6642 ptr = ahd->suspend_state.scratch_ram;
6643 for (i = 0; i < 64; i++)
6644 ahd_outb(ahd, SRAM_BASE + i, *ptr++);
6645
6646 if ((ahd->features & AHD_MORE_SRAM) != 0) {
6647 for (i = 0; i < 16; i++)
6648 ahd_outb(ahd, TARG_OFFSET + i, *ptr++);
6649 }
6650
6651 ptr = ahd->suspend_state.btt;
6652 for (i = 0;i < AHD_NUM_TARGETS; i++) {
6653 int j;
6654
6655 for (j = 0;j < AHD_NUM_LUNS; j++) {
6656 u_int tcl;
6657
6658 tcl = BUILD_TCL(i << 4, j);
6659 ahd_busy_tcl(ahd, tcl, *ptr);
6660 }
6661 }
6662 #endif
6663 return (0);
6664 }
6665
6666 /************************** Busy Target Table *********************************/
6667 /*
6668 * Set SCBPTR to the SCB that contains the busy
6669 * table entry for TCL. Return the offset into
6670 * the SCB that contains the entry for TCL.
6671 * saved_scbid is dereferenced and set to the
6672 * scbid that should be restored once manipualtion
6673 * of the TCL entry is complete.
6674 */
6675 static __inline u_int
6676 ahd_index_busy_tcl(struct ahd_softc *ahd, u_int *saved_scbid, u_int tcl)
6677 {
6678 /*
6679 * Index to the SCB that contains the busy entry.
6680 */
6681 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
6682 *saved_scbid = ahd_get_scbptr(ahd);
6683 ahd_set_scbptr(ahd, TCL_LUN(tcl)
6684 | ((TCL_TARGET_OFFSET(tcl) & 0xC) << 4));
6685
6686 /*
6687 * And now calculate the SCB offset to the entry.
6688 * Each entry is 2 bytes wide, hence the
6689 * multiplication by 2.
6690 */
6691 return (((TCL_TARGET_OFFSET(tcl) & 0x3) << 1) + SCB_DISCONNECTED_LISTS);
6692 }
6693
6694 /*
6695 * Return the untagged transaction id for a given target/channel lun.
6696 */
6697 u_int
6698 ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl)
6699 {
6700 u_int scbid;
6701 u_int scb_offset;
6702 u_int saved_scbptr;
6703
6704 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
6705 scbid = ahd_inw_scbram(ahd, scb_offset);
6706 ahd_set_scbptr(ahd, saved_scbptr);
6707 return (scbid);
6708 }
6709
6710 void
6711 ahd_busy_tcl(struct ahd_softc *ahd, u_int tcl, u_int scbid)
6712 {
6713 u_int scb_offset;
6714 u_int saved_scbptr;
6715
6716 scb_offset = ahd_index_busy_tcl(ahd, &saved_scbptr, tcl);
6717 ahd_outw(ahd, scb_offset, scbid);
6718 ahd_set_scbptr(ahd, saved_scbptr);
6719 }
6720
6721 /************************** SCB and SCB queue management **********************/
6722 int
6723 ahd_match_scb(struct ahd_softc *ahd, struct scb *scb, int target,
6724 char channel, int lun, u_int tag, role_t role)
6725 {
6726 int targ = SCB_GET_TARGET(ahd, scb);
6727 char chan = SCB_GET_CHANNEL(ahd, scb);
6728 int slun = SCB_GET_LUN(scb);
6729 int match;
6730
6731 match = ((chan == channel) || (channel == ALL_CHANNELS));
6732 if (match != 0)
6733 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
6734 if (match != 0)
6735 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
6736 if (match != 0) {
6737 #if AHD_TARGET_MODE
6738 int group;
6739
6740 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
6741 if (role == ROLE_INITIATOR) {
6742 match = (group != XPT_FC_GROUP_TMODE)
6743 && ((tag == SCB_GET_TAG(scb))
6744 || (tag == SCB_LIST_NULL));
6745 } else if (role == ROLE_TARGET) {
6746 match = (group == XPT_FC_GROUP_TMODE)
6747 && ((tag == scb->io_ctx->csio.tag_id)
6748 || (tag == SCB_LIST_NULL));
6749 }
6750 #else /* !AHD_TARGET_MODE */
6751 match = ((tag == SCB_GET_TAG(scb)) || (tag == SCB_LIST_NULL));
6752 #endif /* AHD_TARGET_MODE */
6753 }
6754
6755 return match;
6756 }
6757
6758 void
6759 ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
6760 {
6761 int target;
6762 char channel;
6763 int lun;
6764
6765 target = SCB_GET_TARGET(ahd, scb);
6766 lun = SCB_GET_LUN(scb);
6767 channel = SCB_GET_CHANNEL(ahd, scb);
6768
6769 ahd_search_qinfifo(ahd, target, channel, lun,
6770 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
6771 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
6772
6773 ahd_platform_freeze_devq(ahd, scb);
6774 }
6775
6776 void
6777 ahd_qinfifo_requeue_tail(struct ahd_softc *ahd, struct scb *scb)
6778 {
6779 struct scb *prev_scb;
6780 ahd_mode_state saved_modes;
6781
6782 saved_modes = ahd_save_modes(ahd);
6783 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6784 prev_scb = NULL;
6785 if (ahd_qinfifo_count(ahd) != 0) {
6786 u_int prev_tag;
6787 u_int prev_pos;
6788
6789 prev_pos = AHD_QIN_WRAP(ahd->qinfifonext - 1);
6790 prev_tag = ahd->qinfifo[prev_pos];
6791 prev_scb = ahd_lookup_scb(ahd, prev_tag);
6792 }
6793 ahd_qinfifo_requeue(ahd, prev_scb, scb);
6794 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6795 ahd_restore_modes(ahd, saved_modes);
6796 }
6797
6798 static void
6799 ahd_qinfifo_requeue(struct ahd_softc *ahd, struct scb *prev_scb,
6800 struct scb *scb)
6801 {
6802 if (prev_scb == NULL) {
6803 uint32_t busaddr;
6804
6805 busaddr = ahd_le32toh(scb->hscb->hscb_busaddr);
6806 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
6807 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
6808 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
6809 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
6810 } else {
6811 prev_scb->hscb->next_hscb_busaddr = scb->hscb->hscb_busaddr;
6812 ahd_sync_scb(ahd, prev_scb,
6813 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
6814 }
6815 ahd->qinfifo[AHD_QIN_WRAP(ahd->qinfifonext)] = SCB_GET_TAG(scb);
6816 ahd->qinfifonext++;
6817 scb->hscb->next_hscb_busaddr = ahd->next_queued_hscb->hscb_busaddr;
6818 ahd_sync_scb(ahd, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
6819 }
6820
6821 static int
6822 ahd_qinfifo_count(struct ahd_softc *ahd)
6823 {
6824 u_int qinpos;
6825 u_int wrap_qinpos;
6826 u_int wrap_qinfifonext;
6827
6828 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
6829 qinpos = ahd_get_snscb_qoff(ahd);
6830 wrap_qinpos = AHD_QIN_WRAP(qinpos);
6831 wrap_qinfifonext = AHD_QIN_WRAP(ahd->qinfifonext);
6832 if (wrap_qinfifonext >= wrap_qinpos)
6833 return (wrap_qinfifonext - wrap_qinpos);
6834 else
6835 return (wrap_qinfifonext
6836 + NUM_ELEMENTS(ahd->qinfifo) - wrap_qinpos);
6837 }
6838
6839 void
6840 ahd_reset_cmds_pending(struct ahd_softc *ahd)
6841 {
6842 struct scb *scb;
6843 ahd_mode_state saved_modes;
6844 u_int pending_cmds;
6845
6846 saved_modes = ahd_save_modes(ahd);
6847 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6848
6849 /*
6850 * Don't count any commands as outstanding that the
6851 * sequencer has already marked for completion.
6852 */
6853 ahd_flush_qoutfifo(ahd);
6854
6855 pending_cmds = 0;
6856 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
6857 pending_cmds++;
6858 }
6859 ahd_outw(ahd, CMDS_PENDING, pending_cmds - ahd_qinfifo_count(ahd));
6860 ahd_restore_modes(ahd, saved_modes);
6861 ahd->flags &= ~AHD_UPDATE_PEND_CMDS;
6862 }
6863
6864 int
6865 ahd_search_qinfifo(struct ahd_softc *ahd, int target, char channel,
6866 int lun, u_int tag, role_t role, uint32_t status,
6867 ahd_search_action action)
6868 {
6869 struct scb *scb;
6870 struct scb *prev_scb;
6871 ahd_mode_state saved_modes;
6872 u_int qinstart;
6873 u_int qinpos;
6874 u_int qintail;
6875 u_int tid_next;
6876 u_int tid_prev;
6877 u_int scbid;
6878 u_int savedscbptr;
6879 uint32_t busaddr;
6880 int found;
6881 int targets;
6882 int pending_cmds;
6883 int qincount;
6884
6885 /* Must be in CCHAN mode */
6886 saved_modes = ahd_save_modes(ahd);
6887 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
6888
6889 /*
6890 * Halt any pending SCB DMA. The sequencer will reinitiate
6891 * this DMA if the qinfifo is not empty once we unpause.
6892 */
6893 if ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN|CCSCBDIR))
6894 == (CCARREN|CCSCBEN|CCSCBDIR)) {
6895 ahd_outb(ahd, CCSCBCTL,
6896 ahd_inb(ahd, CCSCBCTL) & ~(CCARREN|CCSCBEN));
6897 while ((ahd_inb(ahd, CCSCBCTL) & (CCARREN|CCSCBEN)) != 0)
6898 ;
6899 }
6900 /* Determine sequencer's position in the qinfifo. */
6901 qintail = AHD_QIN_WRAP(ahd->qinfifonext);
6902 qinstart = ahd_get_snscb_qoff(ahd);
6903 qinpos = AHD_QIN_WRAP(qinstart);
6904 found = 0;
6905 prev_scb = NULL;
6906
6907 pending_cmds = 0;
6908 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
6909 pending_cmds++;
6910 }
6911 qincount = ahd_qinfifo_count(ahd);
6912
6913 if (action == SEARCH_PRINT) {
6914 printf("qinstart = 0x%x qinfifonext = 0x%x\n",
6915 qinstart, ahd->qinfifonext);
6916 }
6917
6918 /*
6919 * Start with an empty queue. Entries that are not chosen
6920 * for removal will be re-added to the queue as we go.
6921 */
6922 ahd->qinfifonext = qinstart;
6923 busaddr = ahd_le32toh(ahd->next_queued_hscb->hscb_busaddr);
6924 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 0, busaddr & 0xFF);
6925 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 1, (busaddr >> 8) & 0xFF);
6926 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 2, (busaddr >> 16) & 0xFF);
6927 ahd_outb(ahd, NEXT_QUEUED_SCB_ADDR + 3, (busaddr >> 24) & 0xFF);
6928
6929 while (qinpos != qintail) {
6930 scb = ahd_lookup_scb(ahd, ahd->qinfifo[qinpos]);
6931 if (scb == NULL) {
6932 panic("Loop 1\n");
6933 }
6934
6935 if (ahd_match_scb(ahd, scb, target, channel, lun, tag, role)) {
6936 /*
6937 * We found an scb that needs to be acted on.
6938 */
6939 found++;
6940 switch (action) {
6941 case SEARCH_COMPLETE:
6942 {
6943 cam_status ostat;
6944 cam_status cstat;
6945
6946 ostat = ahd_get_scsi_status(scb);
6947 if (ostat == CAM_REQ_INPROG)
6948 ahd_set_scsi_status(scb, status);
6949 cstat = ahd_get_transaction_status(scb);
6950 if (cstat != CAM_REQ_CMP)
6951 ahd_freeze_scb(scb);
6952 if ((scb->flags & SCB_ACTIVE) == 0)
6953 printf("Inactive SCB in qinfifo\n");
6954 if (scb->xs->error != CAM_REQ_CMP)
6955 printf("SEARCH_COMPLETE(0x%x): ostat 0x%x, cstat 0x%x, xs_error 0x%x\n",
6956 SCB_GET_TAG(scb), ostat, cstat, scb->xs->error);
6957 ahd_done(ahd, scb);
6958
6959 /* FALLTHROUGH */
6960 }
6961 case SEARCH_REMOVE:
6962 break;
6963 case SEARCH_PRINT:
6964 printf(" 0x%x", ahd->qinfifo[qinpos]);
6965 /* FALLTHROUGH */
6966 case SEARCH_COUNT:
6967 ahd_qinfifo_requeue(ahd, prev_scb, scb);
6968 prev_scb = scb;
6969 break;
6970 }
6971 } else {
6972 ahd_qinfifo_requeue(ahd, prev_scb, scb);
6973 prev_scb = scb;
6974 }
6975 qinpos = AHD_QIN_WRAP(qinpos+1);
6976 }
6977
6978 ahd_set_hnscb_qoff(ahd, ahd->qinfifonext);
6979
6980 if (action == SEARCH_PRINT)
6981 printf("\nWAITING_TID_QUEUES:\n");
6982
6983 /*
6984 * Search waiting for selection lists. We traverse the
6985 * list of "their ids" waiting for selection and, if
6986 * appropriate, traverse the SCBs of each "their id"
6987 * looking for matches.
6988 */
6989 savedscbptr = ahd_get_scbptr(ahd);
6990 tid_next = ahd_inw(ahd, WAITING_TID_HEAD);
6991 tid_prev = SCB_LIST_NULL;
6992 targets = 0;
6993 for (scbid = tid_next; !SCBID_IS_NULL(scbid); scbid = tid_next) {
6994 u_int tid_head;
6995
6996 /*
6997 * We limit based on the number of SCBs since
6998 * MK_MESSAGE SCBs are not in the per-tid lists.
6999 */
7000 targets++;
7001 if (targets > AHD_SCB_MAX) {
7002 panic("TID LIST LOOP");
7003 }
7004 if (scbid >= ahd->scb_data.numscbs) {
7005 printf("%s: Waiting TID List inconsistency. "
7006 "SCB index == 0x%x, yet numscbs == 0x%x.",
7007 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
7008 ahd_dump_card_state(ahd);
7009 panic("for safety");
7010 }
7011 scb = ahd_lookup_scb(ahd, scbid);
7012 if (scb == NULL) {
7013 printf("%s: SCB = 0x%x Not Active!\n",
7014 ahd_name(ahd), scbid);
7015 panic("Waiting TID List traversal\n");
7016 break;
7017 }
7018 ahd_set_scbptr(ahd, scbid);
7019 tid_next = ahd_inw_scbram(ahd, SCB_NEXT2);
7020 if (ahd_match_scb(ahd, scb, target, channel, CAM_LUN_WILDCARD,
7021 SCB_LIST_NULL, ROLE_UNKNOWN) == 0) {
7022 tid_prev = scbid;
7023 continue;
7024 }
7025
7026 /*
7027 * We found a list of scbs that needs to be searched.
7028 */
7029 if (action == SEARCH_PRINT)
7030 printf(" %d ( ", SCB_GET_TARGET(ahd, scb));
7031 tid_head = scbid;
7032 found += ahd_search_scb_list(ahd, target, channel,
7033 lun, tag, role, status,
7034 action, &tid_head,
7035 SCB_GET_TARGET(ahd, scb));
7036 if (tid_head != scbid)
7037 ahd_stitch_tid_list(ahd, tid_prev, tid_head, tid_next);
7038 if (!SCBID_IS_NULL(tid_head))
7039 tid_prev = tid_head;
7040 if (action == SEARCH_PRINT)
7041 printf(")\n");
7042 }
7043 ahd_set_scbptr(ahd, savedscbptr);
7044 ahd_restore_modes(ahd, saved_modes);
7045 return (found);
7046 }
7047
7048 static int
7049 ahd_search_scb_list(struct ahd_softc *ahd, int target, char channel,
7050 int lun, u_int tag, role_t role, uint32_t status,
7051 ahd_search_action action, u_int *list_head, u_int tid)
7052 {
7053 struct scb *scb;
7054 u_int scbid;
7055 u_int next;
7056 u_int prev;
7057 int found;
7058
7059 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
7060 found = 0;
7061 prev = SCB_LIST_NULL;
7062 next = *list_head;
7063 for (scbid = next; !SCBID_IS_NULL(scbid); scbid = next) {
7064 if (scbid >= ahd->scb_data.numscbs) {
7065 printf("%s:SCB List inconsistency. "
7066 "SCB == 0x%x, yet numscbs == 0x%x.",
7067 ahd_name(ahd), scbid, ahd->scb_data.numscbs);
7068 ahd_dump_card_state(ahd);
7069 panic("for safety");
7070 }
7071 scb = ahd_lookup_scb(ahd, scbid);
7072 if (scb == NULL) {
7073 printf("%s: SCB = %d Not Active!\n",
7074 ahd_name(ahd), scbid);
7075 panic("Waiting List traversal\n");
7076 }
7077 ahd_set_scbptr(ahd, scbid);
7078 next = ahd_inw_scbram(ahd, SCB_NEXT);
7079 if (ahd_match_scb(ahd, scb, target, channel,
7080 lun, SCB_LIST_NULL, role) == 0) {
7081 prev = scbid;
7082 continue;
7083 }
7084 found++;
7085 switch (action) {
7086 case SEARCH_COMPLETE:
7087 {
7088 cam_status ostat;
7089 cam_status cstat;
7090
7091 ostat = ahd_get_scsi_status(scb);
7092 if (ostat == CAM_REQ_INPROG)
7093 ahd_set_scsi_status(scb, status);
7094 cstat = ahd_get_transaction_status(scb);
7095 if (cstat != CAM_REQ_CMP)
7096 ahd_freeze_scb(scb);
7097 if ((scb->flags & SCB_ACTIVE) == 0)
7098 printf("Inactive SCB in Waiting List\n");
7099 ahd_done(ahd, scb);
7100 /* FALLTHROUGH */
7101 }
7102 case SEARCH_REMOVE:
7103 ahd_rem_wscb(ahd, scbid, prev, next, tid);
7104 if (prev == SCB_LIST_NULL)
7105 *list_head = next;
7106 break;
7107 case SEARCH_PRINT:
7108 printf("0x%x ", scbid);
7109 case SEARCH_COUNT:
7110 prev = scbid;
7111 break;
7112 }
7113 if (found > AHD_SCB_MAX)
7114 panic("SCB LIST LOOP");
7115 }
7116 if (action == SEARCH_COMPLETE
7117 || action == SEARCH_REMOVE)
7118 ahd_outw(ahd, CMDS_PENDING, ahd_inw(ahd, CMDS_PENDING) - found);
7119 return (found);
7120 }
7121
7122 static void
7123 ahd_stitch_tid_list(struct ahd_softc *ahd, u_int tid_prev,
7124 u_int tid_cur, u_int tid_next)
7125 {
7126 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
7127
7128 if (SCBID_IS_NULL(tid_cur)) {
7129
7130 /* Bypass current TID list */
7131 if (SCBID_IS_NULL(tid_prev)) {
7132 ahd_outw(ahd, WAITING_TID_HEAD, tid_next);
7133 } else {
7134 ahd_set_scbptr(ahd, tid_prev);
7135 ahd_outw(ahd, SCB_NEXT2, tid_next);
7136 }
7137 if (SCBID_IS_NULL(tid_next))
7138 ahd_outw(ahd, WAITING_TID_TAIL, tid_prev);
7139 } else {
7140
7141 /* Stitch through tid_cur */
7142 if (SCBID_IS_NULL(tid_prev)) {
7143 ahd_outw(ahd, WAITING_TID_HEAD, tid_cur);
7144 } else {
7145 ahd_set_scbptr(ahd, tid_prev);
7146 ahd_outw(ahd, SCB_NEXT2, tid_cur);
7147 }
7148 ahd_set_scbptr(ahd, tid_cur);
7149 ahd_outw(ahd, SCB_NEXT2, tid_next);
7150
7151 if (SCBID_IS_NULL(tid_next))
7152 ahd_outw(ahd, WAITING_TID_TAIL, tid_cur);
7153 }
7154 }
7155
7156 /*
7157 * Manipulate the waiting for selection list and return the
7158 * scb that follows the one that we remove.
7159 */
7160 static u_int
7161 ahd_rem_wscb(struct ahd_softc *ahd, u_int scbid,
7162 u_int prev, u_int next, u_int tid)
7163 {
7164 u_int tail_offset;
7165
7166 AHD_ASSERT_MODES(ahd, AHD_MODE_CCHAN_MSK, AHD_MODE_CCHAN_MSK);
7167 if (!SCBID_IS_NULL(prev)) {
7168 ahd_set_scbptr(ahd, prev);
7169 ahd_outw(ahd, SCB_NEXT, next);
7170 }
7171
7172 /*
7173 * SCBs that had MK_MESSAGE set in them will not
7174 * be queued to the per-target lists, so don't
7175 * blindly clear the tail pointer.
7176 */
7177 tail_offset = WAITING_SCB_TAILS + (2 * tid);
7178 if (SCBID_IS_NULL(next)
7179 && ahd_inw(ahd, tail_offset) == scbid)
7180 ahd_outw(ahd, tail_offset, prev);
7181 ahd_add_scb_to_free_list(ahd, scbid);
7182 return (next);
7183 }
7184
7185 /*
7186 * Add the SCB as selected by SCBPTR onto the on chip list of
7187 * free hardware SCBs. This list is empty/unused if we are not
7188 * performing SCB paging.
7189 */
7190 static void
7191 ahd_add_scb_to_free_list(struct ahd_softc *ahd, u_int scbid)
7192 {
7193 /* XXX Need some other mechanism to designate "free". */
7194 /*
7195 * Invalidate the tag so that our abort
7196 * routines don't think it's active.
7197 ahd_outb(ahd, SCB_TAG, SCB_LIST_NULL);
7198 */
7199 }
7200
7201 /******************************** Error Handling ******************************/
7202 /*
7203 * Abort all SCBs that match the given description (target/channel/lun/tag),
7204 * setting their status to the passed in status if the status has not already
7205 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
7206 * is paused before it is called.
7207 */
7208 int
7209 ahd_abort_scbs(struct ahd_softc *ahd, int target, char channel,
7210 int lun, u_int tag, role_t role, uint32_t status)
7211 {
7212 struct scb *scbp;
7213 struct scb *scbp_next;
7214 u_int i, j;
7215 u_int maxtarget;
7216 u_int minlun;
7217 u_int maxlun;
7218 int found;
7219 ahd_mode_state saved_modes;
7220
7221 /* restore this when we're done */
7222 saved_modes = ahd_save_modes(ahd);
7223 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7224
7225 found = ahd_search_qinfifo(ahd, target, channel, lun, SCB_LIST_NULL,
7226 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
7227
7228 /*
7229 * Clean out the busy target table for any untagged commands.
7230 */
7231 i = 0;
7232 maxtarget = 16;
7233 if (target != CAM_TARGET_WILDCARD) {
7234 i = target;
7235 if (channel == 'B')
7236 i += 8;
7237 maxtarget = i + 1;
7238 }
7239
7240 if (lun == CAM_LUN_WILDCARD) {
7241 minlun = 0;
7242 maxlun = AHD_NUM_LUNS_NONPKT;
7243 } else if (lun >= AHD_NUM_LUNS_NONPKT) {
7244 minlun = maxlun = 0;
7245 } else {
7246 minlun = lun;
7247 maxlun = lun + 1;
7248 }
7249
7250 if (role != ROLE_TARGET) {
7251 for (;i < maxtarget; i++) {
7252 for (j = minlun;j < maxlun; j++) {
7253 u_int scbid;
7254 u_int tcl;
7255
7256 tcl = BUILD_TCL_RAW(i, 'A', j);
7257 scbid = ahd_find_busy_tcl(ahd, tcl);
7258 scbp = ahd_lookup_scb(ahd, scbid);
7259 if (scbp == NULL
7260 || ahd_match_scb(ahd, scbp, target, channel,
7261 lun, tag, role) == 0)
7262 continue;
7263 ahd_unbusy_tcl(ahd, BUILD_TCL_RAW(i, 'A', j));
7264 }
7265 }
7266 }
7267
7268 /*
7269 * Don't abort commands that have already completed,
7270 * but haven't quite made it up to the host yet.
7271 */
7272 ahd_flush_qoutfifo(ahd);
7273
7274 /*
7275 * Go through the pending CCB list and look for
7276 * commands for this target that are still active.
7277 * These are other tagged commands that were
7278 * disconnected when the reset occurred.
7279 */
7280 scbp_next = LIST_FIRST(&ahd->pending_scbs);
7281 while (scbp_next != NULL) {
7282 scbp = scbp_next;
7283 scbp_next = LIST_NEXT(scbp, pending_links);
7284 if (ahd_match_scb(ahd, scbp, target, channel, lun, tag, role)) {
7285 cam_status ostat;
7286
7287 ostat = ahd_get_scsi_status(scbp);
7288 if (ostat == CAM_REQ_INPROG)
7289 ahd_set_scsi_status(scbp, status);
7290 if (ahd_get_transaction_status(scbp) != CAM_REQ_CMP)
7291 ahd_freeze_scb(scbp);
7292 if ((scbp->flags & SCB_ACTIVE) == 0)
7293 printf("Inactive SCB on pending list\n");
7294 ahd_done(ahd, scbp);
7295 found++;
7296 }
7297 }
7298 ahd_restore_modes(ahd, saved_modes);
7299 ahd_platform_abort_scbs(ahd, target, channel, lun, tag, role, status);
7300 ahd->flags |= AHD_UPDATE_PEND_CMDS;
7301 return found;
7302 }
7303
7304 static void
7305 ahd_reset_current_bus(struct ahd_softc *ahd)
7306 {
7307 uint8_t scsiseq;
7308
7309 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7310 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) & ~ENSCSIRST);
7311 scsiseq = ahd_inb(ahd, SCSISEQ0) & ~(ENSELO|ENARBO|SCSIRSTO);
7312 ahd_outb(ahd, SCSISEQ0, scsiseq | SCSIRSTO);
7313 ahd_delay(AHD_BUSRESET_DELAY);
7314 /* Turn off the bus reset */
7315 ahd_outb(ahd, SCSISEQ0, scsiseq);
7316 if ((ahd->bugs & AHD_SCSIRST_BUG) != 0) {
7317 /*
7318 * 2A Razor #474
7319 * Certain chip state is not cleared for
7320 * SCSI bus resets that we initiate, so
7321 * we must reset the chip.
7322 */
7323 ahd_delay(AHD_BUSRESET_DELAY);
7324 ahd_reset(ahd);
7325 ahd_intr_enable(ahd, /*enable*/TRUE);
7326 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
7327 }
7328
7329 ahd_clear_intstat(ahd);
7330 }
7331
7332 int
7333 ahd_reset_channel(struct ahd_softc *ahd, char channel, int initiate_reset)
7334 {
7335 struct ahd_devinfo devinfo;
7336 u_int initiator;
7337 u_int target;
7338 u_int max_scsiid;
7339 int found;
7340 u_int fifo;
7341 u_int next_fifo;
7342
7343
7344 ahd->pending_device = NULL;
7345
7346 ahd_compile_devinfo(&devinfo,
7347 CAM_TARGET_WILDCARD,
7348 CAM_TARGET_WILDCARD,
7349 CAM_LUN_WILDCARD,
7350 channel, ROLE_UNKNOWN);
7351 ahd_pause(ahd);
7352
7353 /* Make sure the sequencer is in a safe location. */
7354 ahd_clear_critical_section(ahd);
7355
7356 #if AHD_TARGET_MODE
7357 if ((ahd->flags & AHD_TARGETROLE) != 0) {
7358 ahd_run_tqinfifo(ahd, /*paused*/TRUE);
7359 }
7360 #endif
7361 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7362
7363 /*
7364 * Disable selections so no automatic hardware
7365 * functions will modify chip state.
7366 */
7367 ahd_outb(ahd, SCSISEQ0, 0);
7368 ahd_outb(ahd, SCSISEQ1, 0);
7369
7370 /*
7371 * Safely shut down our DMA engines. Always start with
7372 * the FIFO that is not currently active (if any are
7373 * actively connected).
7374 */
7375 next_fifo = fifo = ahd_inb(ahd, DFFSTAT) & CURRFIFO;
7376 if (next_fifo > CURRFIFO_1)
7377 /* If disconneced, arbitrarily start with FIFO1. */
7378 next_fifo = fifo = 0;
7379 do {
7380 next_fifo ^= CURRFIFO_1;
7381 ahd_set_modes(ahd, next_fifo, next_fifo);
7382 ahd_outb(ahd, DFCNTRL,
7383 ahd_inb(ahd, DFCNTRL) & ~(SCSIEN|HDMAEN));
7384 while ((ahd_inb(ahd, DFCNTRL) & HDMAENACK) != 0)
7385 ahd_delay(10);
7386 /*
7387 * Set CURRFIFO to the now inactive channel.
7388 */
7389 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7390 ahd_outb(ahd, DFFSTAT, next_fifo);
7391 } while (next_fifo != fifo);
7392
7393 /*
7394 * Reset the bus if we are initiating this reset
7395 */
7396 ahd_clear_msg_state(ahd);
7397 ahd_outb(ahd, SIMODE1,
7398 ahd_inb(ahd, SIMODE1) & ~(ENBUSFREE|ENSCSIRST|ENBUSFREE));
7399
7400 if (initiate_reset)
7401 ahd_reset_current_bus(ahd);
7402
7403 ahd_clear_intstat(ahd);
7404
7405 /*
7406 * Clean up all the state information for the
7407 * pending transactions on this bus.
7408 */
7409 found = ahd_abort_scbs(ahd, CAM_TARGET_WILDCARD, channel,
7410 CAM_LUN_WILDCARD, SCB_LIST_NULL,
7411 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
7412
7413 /*
7414 * Cleanup anything left in the FIFOs.
7415 */
7416 ahd_clear_fifo(ahd, 0);
7417 ahd_clear_fifo(ahd, 1);
7418
7419 /*
7420 * Revert to async/narrow transfers until we renegotiate.
7421 */
7422 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
7423 for (target = 0; target <= max_scsiid; target++) {
7424
7425 if (ahd->enabled_targets[target] == NULL)
7426 continue;
7427 for (initiator = 0; initiator <= max_scsiid; initiator++) {
7428 struct ahd_devinfo devinfo;
7429
7430 ahd_compile_devinfo(&devinfo, target, initiator,
7431 CAM_LUN_WILDCARD,
7432 'A', ROLE_UNKNOWN);
7433 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
7434 AHD_TRANS_CUR, /*paused*/TRUE);
7435 ahd_set_syncrate(ahd, &devinfo, /*period*/0,
7436 /*offset*/0, /*ppr_options*/0,
7437 AHD_TRANS_CUR, /*paused*/TRUE);
7438 }
7439 }
7440
7441 #ifdef AHD_TARGET_MODE
7442 max_scsiid = (ahd->features & AHD_WIDE) ? 15 : 7;
7443
7444 /*
7445 * Send an immediate notify ccb to all target more peripheral
7446 * drivers affected by this action.
7447 */
7448 for (target = 0; target <= max_scsiid; target++) {
7449 struct ahd_tmode_tstate* tstate;
7450 u_int lun;
7451
7452 tstate = ahd->enabled_targets[target];
7453 if (tstate == NULL)
7454 continue;
7455 for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
7456 struct ahd_tmode_lstate* lstate;
7457
7458 lstate = tstate->enabled_luns[lun];
7459 if (lstate == NULL)
7460 continue;
7461
7462 ahd_queue_lstate_event(ahd, lstate, CAM_TARGET_WILDCARD,
7463 EVENT_TYPE_BUS_RESET, /*arg*/0);
7464 ahd_send_lstate_events(ahd, lstate);
7465 }
7466 }
7467 #endif
7468
7469 /* Notify the XPT that a bus reset occurred */
7470 ahd_send_async(ahd, devinfo.channel, CAM_TARGET_WILDCARD,
7471 CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
7472 ahd_restart(ahd);
7473
7474 /*
7475 * Freeze the SIMQ until our poller can determine that
7476 * the bus reset has really gone away. We set the initial
7477 * timer to 0 to have the check performed as soon as possible
7478 * from the timer context.
7479 */
7480 if ((ahd->flags & AHD_RESET_POLL_ACTIVE) == 0) {
7481 ahd->flags |= AHD_RESET_POLL_ACTIVE;
7482 ahd_freeze_simq(ahd);
7483 ahd_timer_reset(&ahd->reset_timer, 0, ahd_reset_poll, ahd);
7484 }
7485 return (found);
7486 }
7487
7488
7489 #define AHD_RESET_POLL_US 1000
7490 static void
7491 ahd_reset_poll(void *arg)
7492 {
7493 struct ahd_softc *ahd;
7494 u_int scsiseq1;
7495 u_long l;
7496 int s;
7497
7498 ahd_list_lock(&l);
7499 ahd = (void*)arg;
7500 if (ahd == NULL) {
7501 printf("ahd_reset_poll: Instance %p no longer exists\n", arg);
7502 ahd_list_unlock(&l);
7503 return;
7504 }
7505 ahd_lock(ahd, &s);
7506 ahd_pause(ahd);
7507 ahd_update_modes(ahd);
7508 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
7509 ahd_outb(ahd, CLRSINT1, CLRSCSIRSTI);
7510 if ((ahd_inb(ahd, SSTAT1) & SCSIRSTI) != 0) {
7511 ahd_timer_reset(&ahd->reset_timer, AHD_RESET_POLL_US,
7512 ahd_reset_poll, ahd);
7513 ahd_unpause(ahd);
7514 ahd_unlock(ahd, &s);
7515 ahd_list_unlock(&l);
7516 return;
7517 }
7518
7519 /* Reset is now low. Complete chip reinitialization. */
7520 ahd_outb(ahd, SIMODE1, ahd_inb(ahd, SIMODE1) | ENSCSIRST);
7521 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
7522 ahd_outb(ahd, SCSISEQ1, scsiseq1 & (ENSELI|ENRSELI|ENAUTOATNP));
7523 ahd_unpause(ahd);
7524 ahd->flags &= ~AHD_RESET_POLL_ACTIVE;
7525 ahd_unlock(ahd, &s);
7526 ahd_release_simq(ahd);
7527 ahd_list_unlock(&l);
7528 }
7529
7530 /**************************** Statistics Processing ***************************/
7531 static void
7532 ahd_stat_timer(void *arg)
7533 {
7534 struct ahd_softc *ahd;
7535 u_long l;
7536 int s;
7537 int enint_coal;
7538
7539 ahd_list_lock(&l);
7540 ahd = (void *)arg;
7541 if (ahd == NULL) {
7542 printf("ahd_stat_timer: Instance %p no longer exists\n", arg);
7543 ahd_list_unlock(&l);
7544 return;
7545 }
7546 ahd_lock(ahd, &s);
7547
7548 enint_coal = ahd->hs_mailbox & ENINT_COALESCE;
7549 if (ahd->cmdcmplt_total > ahd->int_coalescing_threshold)
7550 enint_coal |= ENINT_COALESCE;
7551 else if (ahd->cmdcmplt_total < ahd->int_coalescing_stop_threshold)
7552 enint_coal &= ~ENINT_COALESCE;
7553
7554 if (enint_coal != (ahd->hs_mailbox & ENINT_COALESCE)) {
7555 ahd_enable_coalescing(ahd, enint_coal);
7556 #ifdef AHD_DEBUG
7557 if ((ahd_debug & AHD_SHOW_INT_COALESCING) != 0)
7558 printf("%s: Interrupt coalescing "
7559 "now %sabled. Cmds %d\n",
7560 ahd_name(ahd),
7561 (enint_coal & ENINT_COALESCE) ? "en" : "dis",
7562 ahd->cmdcmplt_total);
7563 #endif
7564 }
7565
7566 ahd->cmdcmplt_bucket = (ahd->cmdcmplt_bucket+1) & (AHD_STAT_BUCKETS-1);
7567 ahd->cmdcmplt_total -= ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket];
7568 ahd->cmdcmplt_counts[ahd->cmdcmplt_bucket] = 0;
7569 ahd_timer_reset(&ahd->stat_timer, AHD_STAT_UPDATE_US,
7570 ahd_stat_timer, ahd);
7571 ahd_unlock(ahd, &s);
7572 ahd_list_unlock(&l);
7573 }
7574
7575 /****************************** Status Processing *****************************/
7576 void
7577 ahd_handle_scb_status(struct ahd_softc *ahd, struct scb *scb)
7578 {
7579 if (scb->hscb->shared_data.istatus.scsi_status != 0) {
7580 ahd_handle_scsi_status(ahd, scb);
7581 } else {
7582 ahd_calc_residual(ahd, scb);
7583 ahd_done(ahd, scb);
7584 }
7585 }
7586
7587 void
7588 ahd_handle_scsi_status(struct ahd_softc *ahd, struct scb *scb)
7589 {
7590 struct hardware_scb *hscb;
7591 u_int qfreeze_cnt;
7592
7593 /*
7594 * The sequencer freezes its select-out queue
7595 * anytime a SCSI status error occurs. We must
7596 * handle the error and decrement the QFREEZE count
7597 * to allow the sequencer to continue.
7598 */
7599 hscb = scb->hscb;
7600
7601 /* Freeze the queue until the client sees the error. */
7602 ahd_freeze_devq(ahd, scb);
7603 ahd_freeze_scb(scb);
7604 qfreeze_cnt = ahd_inw(ahd, QFREEZE_COUNT);
7605 if (qfreeze_cnt == 0) {
7606 printf("%s: Bad status with 0 qfreeze count!\n", ahd_name(ahd));
7607 } else {
7608 qfreeze_cnt--;
7609 ahd_outw(ahd, QFREEZE_COUNT, qfreeze_cnt);
7610 }
7611 if (qfreeze_cnt == 0)
7612 ahd_outb(ahd, SEQ_FLAGS2,
7613 ahd_inb(ahd, SEQ_FLAGS2) & ~SELECTOUT_QFROZEN);
7614
7615 /* Don't want to clobber the original sense code */
7616 if ((scb->flags & SCB_SENSE) != 0) {
7617 /*
7618 * Clear the SCB_SENSE Flag and perform
7619 * a normal command completion.
7620 */
7621 scb->flags &= ~SCB_SENSE;
7622 ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
7623 ahd_done(ahd, scb);
7624 return;
7625 }
7626 ahd_set_scsi_status(scb, hscb->shared_data.istatus.scsi_status);
7627 ahd_set_xfer_status(scb, hscb->shared_data.istatus.scsi_status);
7628 switch (hscb->shared_data.istatus.scsi_status) {
7629 case STATUS_PKT_SENSE:
7630 {
7631 struct scsi_status_iu_header *siu;
7632
7633 ahd_sync_sense(ahd, scb, BUS_DMASYNC_POSTREAD);
7634 siu = (struct scsi_status_iu_header *)scb->sense_data;
7635 ahd_set_scsi_status(scb, siu->status);
7636 #ifdef AHD_DEBUG
7637 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
7638 ahd_print_path(ahd, scb);
7639 printf("SCB 0x%x Received PKT Status of 0x%x\n",
7640 SCB_GET_TAG(scb), siu->status);
7641 }
7642 #endif
7643 if ((siu->flags & SIU_RSPVALID) != 0) {
7644 scsipi_printaddr(scb->xs->xs_periph);
7645 if (scsi_4btoul(siu->pkt_failures_length) < 4) {
7646 printf("Unable to parse pkt_failures\n");
7647 } else {
7648
7649 switch (SIU_PKTFAIL_CODE(siu)) {
7650 case SIU_PFC_NONE:
7651 printf("No packet failure found\n");
7652 break;
7653 case SIU_PFC_CIU_FIELDS_INVALID:
7654 printf("Invalid Command IU Field\n");
7655 break;
7656 case SIU_PFC_TMF_NOT_SUPPORTED:
7657 printf("TMF not supportd\n");
7658 break;
7659 case SIU_PFC_TMF_FAILED:
7660 printf("TMF failed\n");
7661 break;
7662 case SIU_PFC_INVALID_TYPE_CODE:
7663 printf("Invalid L_Q Type code\n");
7664 break;
7665 case SIU_PFC_ILLEGAL_REQUEST:
7666 printf("Illegal request\n");
7667 default:
7668 break;
7669 }
7670 }
7671 if (siu->status == SCSI_STATUS_OK)
7672 ahd_set_transaction_status(scb, CAM_REQ_CMP_ERR);
7673 }
7674 if ((siu->flags & SIU_SNSVALID) != 0) {
7675 scb->flags |= SCB_PKT_SENSE;
7676 #ifdef AHD_DEBUG
7677 if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
7678 printf("Sense data available (%d)\n", siu->sense_length[0]);
7679 printf("SK 0x%x ASC 0x%x ASCQ 0x%x\n",
7680 ((uint8_t)scb->sense_data[SIU_SENSE_OFFSET(siu)+2]) & 0x0F,
7681 ((uint8_t)scb->sense_data[SIU_SENSE_OFFSET(siu)+12]),
7682 ((uint8_t)scb->sense_data[SIU_SENSE_OFFSET(siu)+13]));
7683 }
7684 #endif
7685 }
7686 ahd_done(ahd, scb);
7687 break;
7688 }
7689 case SCSI_STATUS_CMD_TERMINATED:
7690 case SCSI_STATUS_CHECK_COND:
7691 {
7692 struct ahd_devinfo devinfo;
7693 struct ahd_dma_seg *sg;
7694 struct scsipi_sense *sc;
7695 struct ahd_initiator_tinfo *targ_info;
7696 struct ahd_tmode_tstate *tstate;
7697 struct ahd_transinfo *tinfo;
7698 #ifdef AHD_DEBUG
7699 if (ahd_debug & AHD_SHOW_SENSE) {
7700 ahd_print_path(ahd, scb);
7701 printf("SCB %d: requests Check Status\n",
7702 SCB_GET_TAG(scb));
7703 }
7704 #endif
7705
7706 if (ahd_perform_autosense(scb) == 0)
7707 break;
7708
7709 ahd_compile_devinfo(&devinfo, SCB_GET_OUR_ID(scb),
7710 SCB_GET_TARGET(ahd, scb),
7711 SCB_GET_LUN(scb),
7712 SCB_GET_CHANNEL(ahd, scb),
7713 ROLE_INITIATOR);
7714 targ_info = ahd_fetch_transinfo(ahd,
7715 devinfo.channel,
7716 devinfo.our_scsiid,
7717 devinfo.target,
7718 &tstate);
7719 tinfo = &targ_info->curr;
7720 sg = scb->sg_list;
7721 sc = (struct scsipi_sense *)hscb->shared_data.idata.cdb;
7722 /*
7723 * Save off the residual if there is one.
7724 */
7725 ahd_update_residual(ahd, scb);
7726 #ifdef AHD_DEBUG
7727 if (ahd_debug & AHD_SHOW_SENSE) {
7728 ahd_print_path(ahd, scb);
7729 printf("Sending Sense\n");
7730 }
7731 #endif
7732 scb->sg_count = 0;
7733 sg = ahd_sg_setup(ahd, scb, sg, ahd_get_sense_bufaddr(ahd, scb),
7734 ahd_get_sense_bufsize(ahd, scb),
7735 /*last*/TRUE);
7736 sc->opcode = REQUEST_SENSE;
7737 sc->byte2 = 0;
7738 sc->unused[0] = 0;
7739 sc->unused[1] = 0;
7740 sc->length = ahd_get_sense_bufsize(ahd, scb);
7741 sc->control = 0;
7742
7743 /*
7744 * We can't allow the target to disconnect.
7745 * This will be an untagged transaction and
7746 * having the target disconnect will make this
7747 * transaction indestinguishable from outstanding
7748 * tagged transactions.
7749 */
7750 hscb->control = 0;
7751
7752 /*
7753 * This request sense could be because the
7754 * the device lost power or in some other
7755 * way has lost our transfer negotiations.
7756 * Renegotiate if appropriate. Unit attention
7757 * errors will be reported before any data
7758 * phases occur.
7759 */
7760 if (ahd_get_residual(scb) == ahd_get_transfer_length(scb)) {
7761 ahd_update_neg_request(ahd, &devinfo,
7762 tstate, targ_info,
7763 AHD_NEG_IF_NON_ASYNC);
7764 }
7765 if (tstate->auto_negotiate & devinfo.target_mask) {
7766 hscb->control |= MK_MESSAGE;
7767 scb->flags &=
7768 ~(SCB_NEGOTIATE|SCB_ABORT|SCB_DEVICE_RESET);
7769 scb->flags |= SCB_AUTO_NEGOTIATE;
7770 }
7771 hscb->cdb_len = sizeof(*sc);
7772 ahd_setup_data_scb(ahd, scb);
7773 scb->flags |= SCB_SENSE;
7774 ahd_queue_scb(ahd, scb);
7775 /*
7776 * Ensure we have enough time to actually
7777 * retrieve the sense.
7778 */
7779 ahd_scb_timer_reset(scb, 5 * 1000000);
7780 break;
7781 }
7782 case SCSI_STATUS_OK:
7783 printf("%s: Interrupted for status of 0? (SCB 0x%x)\n",
7784 ahd_name(ahd), SCB_GET_TAG(scb));
7785 /* FALLTHROUGH */
7786 default:
7787 ahd_done(ahd, scb);
7788 break;
7789 }
7790 }
7791
7792 /*
7793 * Calculate the residual for a just completed SCB.
7794 */
7795 void
7796 ahd_calc_residual(struct ahd_softc *ahd, struct scb *scb)
7797 {
7798 struct hardware_scb *hscb;
7799 struct initiator_status *spkt;
7800 uint32_t sgptr;
7801 uint32_t resid_sgptr;
7802 uint32_t resid;
7803
7804 /*
7805 * 5 cases.
7806 * 1) No residual.
7807 * SG_STATUS_VALID clear in sgptr.
7808 * 2) Transferless command
7809 * 3) Never performed any transfers.
7810 * sgptr has SG_FULL_RESID set.
7811 * 4) No residual but target did not
7812 * save data pointers after the
7813 * last transfer, so sgptr was
7814 * never updated.
7815 * 5) We have a partial residual.
7816 * Use residual_sgptr to determine
7817 * where we are.
7818 */
7819
7820 hscb = scb->hscb;
7821 sgptr = ahd_le32toh(hscb->sgptr);
7822 if ((sgptr & SG_STATUS_VALID) == 0)
7823 /* Case 1 */
7824 return;
7825 sgptr &= ~SG_STATUS_VALID;
7826
7827 if ((sgptr & SG_LIST_NULL) != 0)
7828 /* Case 2 */
7829 return;
7830
7831 /*
7832 * Residual fields are the same in both
7833 * target and initiator status packets,
7834 * so we can always use the initiator fields
7835 * regardless of the role for this SCB.
7836 */
7837 spkt = &hscb->shared_data.istatus;
7838 resid_sgptr = ahd_le32toh(spkt->residual_sgptr);
7839 if ((sgptr & SG_FULL_RESID) != 0) {
7840 /* Case 3 */
7841 resid = ahd_get_transfer_length(scb);
7842 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
7843 /* Case 4 */
7844 return;
7845 } else if ((resid_sgptr & SG_OVERRUN_RESID) != 0) {
7846 ahd_print_path(ahd, scb);
7847 printf("data overrun detected Tag == 0x%x.\n",
7848 SCB_GET_TAG(scb));
7849 ahd_freeze_devq(ahd, scb);
7850 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
7851 ahd_freeze_scb(scb);
7852 return;
7853 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
7854 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
7855 /* NOTREACHED */
7856 } else {
7857 struct ahd_dma_seg *sg;
7858
7859 /*
7860 * Remainder of the SG where the transfer
7861 * stopped.
7862 */
7863 resid = ahd_le32toh(spkt->residual_datacnt) & AHD_SG_LEN_MASK;
7864 sg = ahd_sg_bus_to_virt(ahd, scb, resid_sgptr & SG_PTR_MASK);
7865
7866 /* The residual sg_ptr always points to the next sg */
7867 sg--;
7868
7869 /*
7870 * Add up the contents of all residual
7871 * SG segments that are after the SG where
7872 * the transfer stopped.
7873 */
7874 while ((ahd_le32toh(sg->len) & AHD_DMA_LAST_SEG) == 0) {
7875 sg++;
7876 resid += ahd_le32toh(sg->len) & AHD_SG_LEN_MASK;
7877 }
7878 }
7879
7880 if ((scb->flags & SCB_SENSE) == 0)
7881 ahd_set_residual(scb, resid);
7882 /*else
7883 ahd_set_sense_residual(scb, resid);*/
7884
7885 #ifdef AHD_DEBUG
7886 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
7887 ahd_print_path(ahd, scb);
7888 printf("Handled %sResidual of %d bytes\n",
7889 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
7890 }
7891 #endif
7892 }
7893
7894 /******************************* Target Mode **********************************/
7895 #ifdef AHD_TARGET_MODE
7896 /*
7897 * Add a target mode event to this lun's queue
7898 */
7899 static void
7900 ahd_queue_lstate_event(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate,
7901 u_int initiator_id, u_int event_type, u_int event_arg)
7902 {
7903 struct ahd_tmode_event *event;
7904 int pending;
7905
7906 xpt_freeze_devq(lstate->path, /*count*/1);
7907 if (lstate->event_w_idx >= lstate->event_r_idx)
7908 pending = lstate->event_w_idx - lstate->event_r_idx;
7909 else
7910 pending = AHD_TMODE_EVENT_BUFFER_SIZE + 1
7911 - (lstate->event_r_idx - lstate->event_w_idx);
7912
7913 if (event_type == EVENT_TYPE_BUS_RESET
7914 || event_type == MSG_BUS_DEV_RESET) {
7915 /*
7916 * Any earlier events are irrelevant, so reset our buffer.
7917 * This has the effect of allowing us to deal with reset
7918 * floods (an external device holding down the reset line)
7919 * without losing the event that is really interesting.
7920 */
7921 lstate->event_r_idx = 0;
7922 lstate->event_w_idx = 0;
7923 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
7924 }
7925
7926 if (pending == AHD_TMODE_EVENT_BUFFER_SIZE) {
7927 xpt_print_path(lstate->path);
7928 printf("immediate event %x:%x lost\n",
7929 lstate->event_buffer[lstate->event_r_idx].event_type,
7930 lstate->event_buffer[lstate->event_r_idx].event_arg);
7931 lstate->event_r_idx++;
7932 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
7933 lstate->event_r_idx = 0;
7934 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
7935 }
7936
7937 event = &lstate->event_buffer[lstate->event_w_idx];
7938 event->initiator_id = initiator_id;
7939 event->event_type = event_type;
7940 event->event_arg = event_arg;
7941 lstate->event_w_idx++;
7942 if (lstate->event_w_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
7943 lstate->event_w_idx = 0;
7944 }
7945
7946 /*
7947 * Send any target mode events queued up waiting
7948 * for immediate notify resources.
7949 */
7950 void
7951 ahd_send_lstate_events(struct ahd_softc *ahd, struct ahd_tmode_lstate *lstate)
7952 {
7953 struct ccb_hdr *ccbh;
7954 struct ccb_immed_notify *inot;
7955
7956 while (lstate->event_r_idx != lstate->event_w_idx
7957 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
7958 struct ahd_tmode_event *event;
7959
7960 event = &lstate->event_buffer[lstate->event_r_idx];
7961 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
7962 inot = (struct ccb_immed_notify *)ccbh;
7963 switch (event->event_type) {
7964 case EVENT_TYPE_BUS_RESET:
7965 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
7966 break;
7967 default:
7968 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
7969 inot->message_args[0] = event->event_type;
7970 inot->message_args[1] = event->event_arg;
7971 break;
7972 }
7973 inot->initiator_id = event->initiator_id;
7974 inot->sense_len = 0;
7975 xpt_done((union ccb *)inot);
7976 lstate->event_r_idx++;
7977 if (lstate->event_r_idx == AHD_TMODE_EVENT_BUFFER_SIZE)
7978 lstate->event_r_idx = 0;
7979 }
7980 }
7981 #endif
7982
7983 /******************** Sequencer Program Patching/Download *********************/
7984
7985 #ifdef AHD_DUMP_SEQ
7986 void
7987 ahd_dumpseq(struct ahd_softc* ahd)
7988 {
7989 int i;
7990 int max_prog;
7991
7992 max_prog = 2048;
7993
7994 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
7995 ahd_outb(ahd, PRGMCNT, 0);
7996 ahd_outb(ahd, PRGMCNT+1, 0);
7997 for (i = 0; i < max_prog; i++) {
7998 uint8_t ins_bytes[4];
7999
8000 ahd_insb(ahd, SEQRAM, ins_bytes, 4);
8001 printf("0x%08x\n", ins_bytes[0] << 24
8002 | ins_bytes[1] << 16
8003 | ins_bytes[2] << 8
8004 | ins_bytes[3]);
8005 }
8006 }
8007 #endif
8008
8009 static void
8010 ahd_loadseq(struct ahd_softc *ahd)
8011 {
8012 struct cs cs_table[num_critical_sections];
8013 u_int begin_set[num_critical_sections];
8014 u_int end_set[num_critical_sections];
8015 struct patch *cur_patch;
8016 u_int cs_count;
8017 u_int cur_cs;
8018 u_int i;
8019 int downloaded;
8020 u_int skip_addr;
8021 u_int sg_prefetch_cnt;
8022 u_int sg_prefetch_cnt_limit;
8023 u_int sg_prefetch_align;
8024 u_int sg_size;
8025 uint8_t download_consts[DOWNLOAD_CONST_COUNT];
8026
8027 if (bootverbose)
8028 printf("%s: Downloading Sequencer Program...",
8029 ahd_name(ahd));
8030
8031 #if DOWNLOAD_CONST_COUNT != 7
8032 #error "Download Const Mismatch"
8033 #endif
8034 /*
8035 * Start out with 0 critical sections
8036 * that apply to this firmware load.
8037 */
8038 cs_count = 0;
8039 cur_cs = 0;
8040 memset(begin_set, 0, sizeof(begin_set));
8041 memset(end_set, 0, sizeof(end_set));
8042
8043 /*
8044 * Setup downloadable constant table.
8045 *
8046 * The computation for the S/G prefetch variables is
8047 * a bit complicated. We would like to always fetch
8048 * in terms of cachelined sized increments. However,
8049 * if the cacheline is not an even multiple of the
8050 * SG element size or is larger than our SG RAM, using
8051 * just the cache size might leave us with only a portion
8052 * of an SG element at the tail of a prefetch. If the
8053 * cacheline is larger than our S/G prefetch buffer less
8054 * the size of an SG element, we may round down to a cacheline
8055 * that doesn't contain any or all of the S/G of interest
8056 * within the bounds of our S/G ram. Provide variables to
8057 * the sequencer that will allow it to handle these edge
8058 * cases.
8059 */
8060 /* Start by aligning to the nearest cacheline. */
8061 sg_prefetch_align = ahd->pci_cachesize;
8062 if (sg_prefetch_align == 0)
8063 sg_prefetch_align = 8;
8064 /* Round down to the nearest power of 2. */
8065 while (powerof2(sg_prefetch_align) == 0)
8066 sg_prefetch_align--;
8067 /*
8068 * If the cacheline boundary is greater than half our prefetch RAM
8069 * we risk not being able to fetch even a single complete S/G
8070 * segment if we align to that boundary.
8071 */
8072 if (sg_prefetch_align > CCSGADDR_MAX/2)
8073 sg_prefetch_align = CCSGADDR_MAX/2;
8074 /* Start by fetching a single cacheline. */
8075 sg_prefetch_cnt = sg_prefetch_align;
8076 /*
8077 * Increment the prefetch count by cachelines until
8078 * at least one S/G element will fit.
8079 */
8080 sg_size = sizeof(struct ahd_dma_seg);
8081 if ((ahd->flags & AHD_64BIT_ADDRESSING) != 0)
8082 sg_size = sizeof(struct ahd_dma64_seg);
8083 while (sg_prefetch_cnt < sg_size)
8084 sg_prefetch_cnt += sg_prefetch_align;
8085 /*
8086 * If the cacheline is not an even multiple of
8087 * the S/G size, we may only get a partial S/G when
8088 * we align. Add a cacheline if this is the case.
8089 */
8090 if ((sg_prefetch_align % sg_size) != 0
8091 && (sg_prefetch_cnt < CCSGADDR_MAX))
8092 sg_prefetch_cnt += sg_prefetch_align;
8093 /*
8094 * Lastly, compute a value that the sequencer can use
8095 * to determine if the remainder of the CCSGRAM buffer
8096 * has a full S/G element in it.
8097 */
8098 sg_prefetch_cnt_limit = -(sg_prefetch_cnt - sg_size + 1);
8099 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
8100 download_consts[SG_PREFETCH_CNT_LIMIT] = sg_prefetch_cnt_limit;
8101 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_align - 1);
8102 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_align - 1);
8103 download_consts[SG_SIZEOF] = sg_size;
8104 download_consts[PKT_OVERRUN_BUFOFFSET] =
8105 (ahd->overrun_buf - (uint8_t *)ahd->qoutfifo) / 256;
8106 download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_1BYTE_LUN;
8107 if ((ahd->bugs & AHD_PKT_LUN_BUG) != 0)
8108 download_consts[SCB_TRANSFER_SIZE] = SCB_TRANSFER_SIZE_FULL_LUN;
8109 cur_patch = patches;
8110 downloaded = 0;
8111 skip_addr = 0;
8112 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
8113 ahd_outb(ahd, PRGMCNT, 0);
8114 ahd_outb(ahd, PRGMCNT+1, 0);
8115
8116 for (i = 0; i < sizeof(seqprog)/4; i++) {
8117 if (ahd_check_patch(ahd, &cur_patch, i, &skip_addr) == 0) {
8118 /*
8119 * Don't download this instruction as it
8120 * is in a patch that was removed.
8121 */
8122 continue;
8123 }
8124 /*
8125 * Move through the CS table until we find a CS
8126 * that might apply to this instruction.
8127 */
8128 for (; cur_cs < num_critical_sections; cur_cs++) {
8129 if (critical_sections[cur_cs].end <= i) {
8130 if (begin_set[cs_count] == TRUE
8131 && end_set[cs_count] == FALSE) {
8132 cs_table[cs_count].end = downloaded;
8133 end_set[cs_count] = TRUE;
8134 cs_count++;
8135 }
8136 continue;
8137 }
8138 if (critical_sections[cur_cs].begin <= i
8139 && begin_set[cs_count] == FALSE) {
8140 cs_table[cs_count].begin = downloaded;
8141 begin_set[cs_count] = TRUE;
8142 }
8143 break;
8144 }
8145 ahd_download_instr(ahd, i, download_consts);
8146 downloaded++;
8147 }
8148
8149 ahd->num_critical_sections = cs_count;
8150 if (cs_count != 0) {
8151
8152 cs_count *= sizeof(struct cs);
8153 ahd->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
8154 if (ahd->critical_sections == NULL)
8155 panic("ahd_loadseq: Could not malloc");
8156 memcpy(ahd->critical_sections, cs_table, cs_count);
8157 }
8158 ahd_outb(ahd, SEQCTL0, PERRORDIS|FAILDIS|FASTMODE);
8159
8160 if (bootverbose) {
8161 printf(" %d instructions downloaded\n", downloaded);
8162 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
8163 ahd_name(ahd), ahd->features, ahd->bugs, ahd->flags);
8164 }
8165 }
8166
8167 static int
8168 ahd_check_patch(struct ahd_softc *ahd, struct patch **start_patch,
8169 u_int start_instr, u_int *skip_addr)
8170 {
8171 struct patch *cur_patch;
8172 struct patch *last_patch;
8173 u_int num_patches;
8174
8175 num_patches = sizeof(patches)/sizeof(struct patch);
8176 last_patch = &patches[num_patches];
8177 cur_patch = *start_patch;
8178
8179 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
8180
8181 if (cur_patch->patch_func(ahd) == 0) {
8182
8183 /* Start rejecting code */
8184 *skip_addr = start_instr + cur_patch->skip_instr;
8185 cur_patch += cur_patch->skip_patch;
8186 } else {
8187 /* Accepted this patch. Advance to the next
8188 * one and wait for our intruction pointer to
8189 * hit this point.
8190 */
8191 cur_patch++;
8192 }
8193 }
8194
8195 *start_patch = cur_patch;
8196 if (start_instr < *skip_addr)
8197 /* Still skipping */
8198 return (0);
8199
8200 return (1);
8201 }
8202
8203 static u_int
8204 ahd_resolve_seqaddr(struct ahd_softc *ahd, u_int address)
8205 {
8206 struct patch *cur_patch;
8207 int address_offset;
8208 u_int skip_addr;
8209 u_int i;
8210
8211 address_offset = 0;
8212 cur_patch = patches;
8213 skip_addr = 0;
8214
8215 for (i = 0; i < address;) {
8216
8217 ahd_check_patch(ahd, &cur_patch, i, &skip_addr);
8218
8219 if (skip_addr > i) {
8220 int end_addr;
8221
8222 end_addr = MIN(address, skip_addr);
8223 address_offset += end_addr - i;
8224 i = skip_addr;
8225 } else {
8226 i++;
8227 }
8228 }
8229 return (address - address_offset);
8230 }
8231
8232 static void
8233 ahd_download_instr(struct ahd_softc *ahd, u_int instrptr, uint8_t *dconsts)
8234 {
8235 union ins_formats instr;
8236 struct ins_format1 *fmt1_ins;
8237 struct ins_format3 *fmt3_ins;
8238 u_int opcode;
8239
8240 /*
8241 * The firmware is always compiled into a little endian format.
8242 */
8243 instr.integer = ahd_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
8244
8245 fmt1_ins = &instr.format1;
8246 fmt3_ins = NULL;
8247
8248 /* Pull the opcode */
8249 opcode = instr.format1.opcode;
8250 switch (opcode) {
8251 case AIC_OP_JMP:
8252 case AIC_OP_JC:
8253 case AIC_OP_JNC:
8254 case AIC_OP_CALL:
8255 case AIC_OP_JNE:
8256 case AIC_OP_JNZ:
8257 case AIC_OP_JE:
8258 case AIC_OP_JZ:
8259 {
8260 fmt3_ins = &instr.format3;
8261 fmt3_ins->address = ahd_resolve_seqaddr(ahd, fmt3_ins->address);
8262 /* FALLTHROUGH */
8263 }
8264 case AIC_OP_OR:
8265 case AIC_OP_AND:
8266 case AIC_OP_XOR:
8267 case AIC_OP_ADD:
8268 case AIC_OP_ADC:
8269 case AIC_OP_BMOV:
8270 if (fmt1_ins->parity != 0) {
8271 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
8272 }
8273 fmt1_ins->parity = 0;
8274 /* FALLTHROUGH */
8275 case AIC_OP_ROL:
8276 {
8277 int i, count;
8278
8279 /* Calculate odd parity for the instruction */
8280 for (i = 0, count = 0; i < 31; i++) {
8281 uint32_t mask;
8282
8283 mask = 0x01 << i;
8284 if ((instr.integer & mask) != 0)
8285 count++;
8286 }
8287 if ((count & 0x01) == 0)
8288 instr.format1.parity = 1;
8289
8290 /* The sequencer is a little endian cpu */
8291 instr.integer = ahd_htole32(instr.integer);
8292 ahd_outsb(ahd, SEQRAM, instr.bytes, 4);
8293 break;
8294 }
8295 default:
8296 panic("Unknown opcode encountered in seq program");
8297 break;
8298 }
8299 }
8300
8301 static int
8302 ahd_probe_stack_size(struct ahd_softc *ahd)
8303 {
8304 int last_probe;
8305
8306 last_probe = 0;
8307 while (1) {
8308 int i;
8309
8310 /*
8311 * We avoid using 0 as a pattern to avoid
8312 * confusion if the stack implementation
8313 * "back-fills" with zeros when "poping'
8314 * entries.
8315 */
8316 for (i = 1; i <= last_probe+1; i++) {
8317 ahd_outb(ahd, STACK, i & 0xFF);
8318 ahd_outb(ahd, STACK, (i >> 8) & 0xFF);
8319 }
8320
8321 /* Verify */
8322 for (i = last_probe+1; i > 0; i--) {
8323 u_int stack_entry;
8324
8325 stack_entry = ahd_inb(ahd, STACK)
8326 |(ahd_inb(ahd, STACK) << 8);
8327 if (stack_entry != i)
8328 goto sized;
8329 }
8330 last_probe++;
8331 }
8332 sized:
8333 return (last_probe);
8334 }
8335
8336 void
8337 ahd_dump_all_cards_state()
8338 {
8339 struct ahd_softc *list_ahd;
8340
8341 TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
8342 ahd_dump_card_state(list_ahd);
8343 }
8344 }
8345
8346 int
8347 ahd_print_register(ahd_reg_parse_entry_t *table, u_int num_entries,
8348 const char *name, u_int address, u_int value,
8349 u_int *cur_column, u_int wrap_point)
8350 {
8351 int printed;
8352 u_int printed_mask;
8353 char line[1024];
8354
8355 line[0] = 0;
8356
8357 if (cur_column != NULL && *cur_column >= wrap_point) {
8358 printf("\n");
8359 *cur_column = 0;
8360 }
8361 printed = snprintf(line, sizeof(line), "%s[0x%x]", name, value);
8362 if (table == NULL) {
8363 printed += snprintf(&line[printed], (sizeof line) - printed,
8364 " ");
8365 printf("%s", line);
8366 if (cur_column != NULL)
8367 *cur_column += printed;
8368 return (printed);
8369 }
8370 printed_mask = 0;
8371 while (printed_mask != 0xFF) {
8372 int entry;
8373
8374 for (entry = 0; entry < num_entries; entry++) {
8375 if (((value & table[entry].mask)
8376 != table[entry].value)
8377 || ((printed_mask & table[entry].mask)
8378 == table[entry].mask))
8379 continue;
8380 printed += snprintf(&line[printed],
8381 (sizeof line) - printed, "%s%s",
8382 printed_mask == 0 ? ":(" : "|",
8383 table[entry].name);
8384 printed_mask |= table[entry].mask;
8385
8386 break;
8387 }
8388 if (entry >= num_entries)
8389 break;
8390 }
8391 if (printed_mask != 0)
8392 printed += snprintf(&line[printed],
8393 (sizeof line) - printed, ") ");
8394 else
8395 printed += snprintf(&line[printed],
8396 (sizeof line) - printed, " ");
8397 if (cur_column != NULL)
8398 *cur_column += printed;
8399 printf("%s", line);
8400
8401 return (printed);
8402 }
8403
8404 void
8405 ahd_dump_card_state(struct ahd_softc *ahd)
8406 {
8407 struct scb *scb;
8408 ahd_mode_state saved_modes;
8409 u_int dffstat;
8410 int paused;
8411 u_int scb_index;
8412 u_int saved_scb_index;
8413 u_int cur_col;
8414 int i;
8415
8416 if (ahd_is_paused(ahd)) {
8417 paused = 1;
8418 } else {
8419 paused = 0;
8420 ahd_pause(ahd);
8421 }
8422 saved_modes = ahd_save_modes(ahd);
8423 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8424 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
8425 "%s: Dumping Card State at program address 0x%x Mode 0x%x\n",
8426 ahd_name(ahd),
8427 ahd_inb(ahd, CURADDR) | (ahd_inb(ahd, CURADDR+1) << 8),
8428 ahd_build_mode_state(ahd, ahd->saved_src_mode,
8429 ahd->saved_dst_mode));
8430 if (paused)
8431 printf("Card was paused\n");
8432 /*
8433 * Mode independent registers.
8434 */
8435 cur_col = 0;
8436 ahd_hs_mailbox_print(ahd_inb(ahd, LOCAL_HS_MAILBOX), &cur_col, 50);
8437 ahd_intctl_print(ahd_inb(ahd, INTCTL), &cur_col, 50);
8438 ahd_seqintstat_print(ahd_inb(ahd, SEQINTSTAT), &cur_col, 50);
8439 ahd_saved_mode_print(ahd_inb(ahd, SAVED_MODE), &cur_col, 50);
8440 ahd_dffstat_print(ahd_inb(ahd, DFFSTAT), &cur_col, 50);
8441 ahd_scsisigi_print(ahd_inb(ahd, SCSISIGI), &cur_col, 50);
8442 ahd_scsiphase_print(ahd_inb(ahd, SCSIPHASE), &cur_col, 50);
8443 ahd_scsibus_print(ahd_inb(ahd, SCSIBUS), &cur_col, 50);
8444 ahd_lastphase_print(ahd_inb(ahd, LASTPHASE), &cur_col, 50);
8445 ahd_scsiseq0_print(ahd_inb(ahd, SCSISEQ0), &cur_col, 50);
8446 ahd_scsiseq1_print(ahd_inb(ahd, SCSISEQ1), &cur_col, 50);
8447 ahd_seqctl0_print(ahd_inb(ahd, SEQCTL0), &cur_col, 50);
8448 ahd_seqintctl_print(ahd_inb(ahd, SEQINTCTL), &cur_col, 50);
8449 ahd_seq_flags_print(ahd_inb(ahd, SEQ_FLAGS), &cur_col, 50);
8450 ahd_seq_flags2_print(ahd_inb(ahd, SEQ_FLAGS2), &cur_col, 50);
8451 ahd_sstat0_print(ahd_inb(ahd, SSTAT0), &cur_col, 50);
8452 ahd_sstat1_print(ahd_inb(ahd, SSTAT1), &cur_col, 50);
8453 ahd_sstat2_print(ahd_inb(ahd, SSTAT2), &cur_col, 50);
8454 ahd_sstat3_print(ahd_inb(ahd, SSTAT3), &cur_col, 50);
8455 ahd_perrdiag_print(ahd_inb(ahd, PERRDIAG), &cur_col, 50);
8456 ahd_simode1_print(ahd_inb(ahd, SIMODE1), &cur_col, 50);
8457 ahd_lqistat0_print(ahd_inb(ahd, LQISTAT0), &cur_col, 50);
8458 ahd_lqistat1_print(ahd_inb(ahd, LQISTAT1), &cur_col, 50);
8459 ahd_lqistat2_print(ahd_inb(ahd, LQISTAT2), &cur_col, 50);
8460 ahd_lqostat0_print(ahd_inb(ahd, LQOSTAT0), &cur_col, 50);
8461 ahd_lqostat1_print(ahd_inb(ahd, LQOSTAT1), &cur_col, 50);
8462 ahd_lqostat2_print(ahd_inb(ahd, LQOSTAT2), &cur_col, 50);
8463 printf("\n");
8464 printf("\nSCB Count = %d CMDS_PENDING = %d LASTSCB 0x%x "
8465 "CURRSCB 0x%x NEXTSCB 0x%x\n",
8466 ahd->scb_data.numscbs, ahd_inw(ahd, CMDS_PENDING),
8467 ahd_inw(ahd, LASTSCB), ahd_inw(ahd, CURRSCB),
8468 ahd_inw(ahd, NEXTSCB));
8469 cur_col = 0;
8470 /* QINFIFO */
8471 ahd_search_qinfifo(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
8472 CAM_LUN_WILDCARD, SCB_LIST_NULL,
8473 ROLE_UNKNOWN, /*status*/0, SEARCH_PRINT);
8474 saved_scb_index = ahd_get_scbptr(ahd);
8475 printf("Pending list:");
8476 i = 0;
8477 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
8478 if (i++ > AHD_SCB_MAX)
8479 break;
8480 /*cur_col =*/ printf("\n%3d FIFO_USE[0x%x] ", SCB_GET_TAG(scb),
8481 ahd_inb(ahd, SCB_FIFO_USE_COUNT));
8482 ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
8483 ahd_scb_control_print(ahd_inb(ahd, SCB_CONTROL), &cur_col, 60);
8484 ahd_scb_scsiid_print(ahd_inb(ahd, SCB_SCSIID), &cur_col, 60);
8485 }
8486 printf("\nTotal %d\n", i);
8487
8488 printf("Kernel Free SCB list: ");
8489 i = 0;
8490 TAILQ_FOREACH(scb, &ahd->scb_data.free_scbs, links.tqe) {
8491 struct scb *list_scb;
8492
8493 list_scb = scb;
8494 do {
8495 printf("%d ", SCB_GET_TAG(list_scb));
8496 list_scb = LIST_NEXT(list_scb, collision_links);
8497 } while (list_scb && i++ < AHD_SCB_MAX);
8498 }
8499
8500 LIST_FOREACH(scb, &ahd->scb_data.any_dev_free_scb_list, links.le) {
8501 if (i++ > AHD_SCB_MAX)
8502 break;
8503 printf("%d ", SCB_GET_TAG(scb));
8504 }
8505 printf("\n");
8506
8507 printf("Sequencer Complete DMA-inprog list: ");
8508 scb_index = ahd_inw(ahd, COMPLETE_SCB_DMAINPROG_HEAD);
8509 i = 0;
8510 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8511 ahd_set_scbptr(ahd, scb_index);
8512 printf("%d ", scb_index);
8513 scb_index = ahd_inw(ahd, SCB_NEXT_COMPLETE);
8514 }
8515 printf("\n");
8516
8517 printf("Sequencer Complete list: ");
8518 scb_index = ahd_inw(ahd, COMPLETE_SCB_HEAD);
8519 i = 0;
8520 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8521 ahd_set_scbptr(ahd, scb_index);
8522 printf("%d ", scb_index);
8523 scb_index = ahd_inw(ahd, SCB_NEXT_COMPLETE);
8524 }
8525 printf("\n");
8526
8527
8528 printf("Sequencer DMA-Up and Complete list: ");
8529 scb_index = ahd_inw(ahd, COMPLETE_DMA_SCB_HEAD);
8530 i = 0;
8531 while (!SCBID_IS_NULL(scb_index) && i++ < AHD_SCB_MAX) {
8532 ahd_set_scbptr(ahd, scb_index);
8533 printf("%d ", scb_index);
8534 scb_index = ahd_inw(ahd, SCB_NEXT_COMPLETE);
8535 }
8536 printf("\n");
8537 ahd_set_scbptr(ahd, saved_scb_index);
8538 dffstat = ahd_inb(ahd, DFFSTAT);
8539 for (i = 0; i < 2; i++) {
8540 #ifdef AHD_DEBUG
8541 struct scb *fifo_scb;
8542 #endif
8543 u_int fifo_scbptr;
8544
8545 ahd_set_modes(ahd, AHD_MODE_DFF0 + i, AHD_MODE_DFF0 + i);
8546 fifo_scbptr = ahd_get_scbptr(ahd);
8547 printf("\n%s: FIFO%d %s, LONGJMP == 0x%x, SCB 0x%x\n",
8548 ahd_name(ahd), i,
8549 (dffstat & (FIFO0FREE << i)) ? "Free" : "Active",
8550 ahd_inw(ahd, LONGJMP_ADDR), fifo_scbptr);
8551 cur_col = 0;
8552 ahd_seqimode_print(ahd_inb(ahd, SEQIMODE), &cur_col, 50);
8553 ahd_seqintsrc_print(ahd_inb(ahd, SEQINTSRC), &cur_col, 50);
8554 ahd_dfcntrl_print(ahd_inb(ahd, DFCNTRL), &cur_col, 50);
8555 ahd_dfstatus_print(ahd_inb(ahd, DFSTATUS), &cur_col, 50);
8556 ahd_sg_cache_shadow_print(ahd_inb(ahd, SG_CACHE_SHADOW),
8557 &cur_col, 50);
8558 ahd_sg_state_print(ahd_inb(ahd, SG_STATE), &cur_col, 50);
8559 ahd_dffsxfrctl_print(ahd_inb(ahd, DFFSXFRCTL), &cur_col, 50);
8560 ahd_soffcnt_print(ahd_inb(ahd, SOFFCNT), &cur_col, 50);
8561 ahd_mdffstat_print(ahd_inb(ahd, MDFFSTAT), &cur_col, 50);
8562 if (cur_col > 50) {
8563 printf("\n");
8564 cur_col = 0;
8565 }
8566 printf("\nSHADDR = 0x%x%x, SHCNT = 0x%x ",
8567 ahd_inl(ahd, SHADDR+4),
8568 ahd_inl(ahd, SHADDR),
8569 (ahd_inb(ahd, SHCNT)
8570 | (ahd_inb(ahd, SHCNT + 1) << 8)
8571 | (ahd_inb(ahd, SHCNT + 2) << 16)));
8572 printf("HADDR = 0x%x%x, HCNT = 0x%x \n",
8573 ahd_inl(ahd, HADDR+4),
8574 ahd_inl(ahd, HADDR),
8575 (ahd_inb(ahd, HCNT)
8576 | (ahd_inb(ahd, HCNT + 1) << 8)
8577 | (ahd_inb(ahd, HCNT + 2) << 16)));
8578 ahd_ccsgctl_print(ahd_inb(ahd, CCSGCTL), &cur_col, 50);
8579 #ifdef AHD_DEBUG
8580 if ((ahd_debug & AHD_SHOW_SG) != 0) {
8581 fifo_scb = ahd_lookup_scb(ahd, fifo_scbptr);
8582 if (fifo_scb != NULL)
8583 ahd_dump_sglist(fifo_scb);
8584 }
8585 #endif
8586 }
8587 printf("\nLQIN: ");
8588 for (i = 0; i < 20; i++)
8589 printf("0x%x ", ahd_inb(ahd, LQIN + i));
8590 printf("\n");
8591 ahd_set_modes(ahd, AHD_MODE_CFG, AHD_MODE_CFG);
8592 printf("%s: LQISTATE = 0x%x, LQOSTATE = 0x%x, OPTIONMODE = 0x%x\n",
8593 ahd_name(ahd), ahd_inb(ahd, LQISTATE), ahd_inb(ahd, LQOSTATE),
8594 ahd_inb(ahd, OPTIONMODE));
8595 printf("%s: OS_SPACE_CNT = 0x%x MAXCMDCNT = 0x%x\n",
8596 ahd_name(ahd), ahd_inb(ahd, OS_SPACE_CNT),
8597 ahd_inb(ahd, MAXCMDCNT));
8598 ahd_simode0_print(ahd_inb(ahd, SIMODE0), &cur_col, 50);
8599 printf("\n");
8600 ahd_set_modes(ahd, AHD_MODE_CCHAN, AHD_MODE_CCHAN);
8601 cur_col = 0;
8602 ahd_set_modes(ahd, ahd->saved_src_mode, ahd->saved_dst_mode);
8603 printf("%s: REG0 == 0x%x, SINDEX = 0x%x, DINDEX = 0x%x\n",
8604 ahd_name(ahd), ahd_inw(ahd, REG0), ahd_inw(ahd, SINDEX),
8605 ahd_inw(ahd, DINDEX));
8606 printf("%s: SCBPTR == 0x%x, SCB_NEXT == 0x%x, SCB_NEXT2 == 0x%x\n",
8607 ahd_name(ahd), ahd_get_scbptr(ahd), ahd_inw(ahd, SCB_NEXT),
8608 ahd_inw(ahd, SCB_NEXT2));
8609 printf("CDB %x %x %x %x %x %x\n",
8610 ahd_inb(ahd, SCB_CDB_STORE),
8611 ahd_inb(ahd, SCB_CDB_STORE+1),
8612 ahd_inb(ahd, SCB_CDB_STORE+2),
8613 ahd_inb(ahd, SCB_CDB_STORE+3),
8614 ahd_inb(ahd, SCB_CDB_STORE+4),
8615 ahd_inb(ahd, SCB_CDB_STORE+5));
8616 printf("STACK:");
8617 for (i = 0; i < ahd->stack_size; i++) {
8618 ahd->saved_stack[i] =
8619 ahd_inb(ahd, STACK)|(ahd_inb(ahd, STACK) << 8);
8620 printf(" 0x%x", ahd->saved_stack[i]);
8621 }
8622 for (i = ahd->stack_size-1; i >= 0; i--) {
8623 ahd_outb(ahd, STACK, ahd->saved_stack[i] & 0xFF);
8624 ahd_outb(ahd, STACK, (ahd->saved_stack[i] >> 8) & 0xFF);
8625 }
8626 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
8627 ahd_platform_dump_card_state(ahd);
8628 ahd_restore_modes(ahd, saved_modes);
8629 if (paused == 0)
8630 ahd_unpause(ahd);
8631 }
8632
8633 void
8634 ahd_dump_scbs(struct ahd_softc *ahd)
8635 {
8636 ahd_mode_state saved_modes;
8637 u_int saved_scb_index;
8638 int i;
8639
8640 saved_modes = ahd_save_modes(ahd);
8641 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
8642 saved_scb_index = ahd_get_scbptr(ahd);
8643 for (i = 0; i < AHD_SCB_MAX; i++) {
8644 ahd_set_scbptr(ahd, i);
8645 printf("%3d", i);
8646 printf("(CTRL 0x%x ID 0x%x N 0x%x N2 0x%x SG 0x%x, RSG 0x%x)\n",
8647 ahd_inb(ahd, SCB_CONTROL),
8648 ahd_inb(ahd, SCB_SCSIID), ahd_inw(ahd, SCB_NEXT),
8649 ahd_inw(ahd, SCB_NEXT2), ahd_inl(ahd, SCB_SGPTR),
8650 ahd_inl(ahd, SCB_RESIDUAL_SGPTR));
8651 }
8652 printf("\n");
8653 ahd_set_scbptr(ahd, saved_scb_index);
8654 ahd_restore_modes(ahd, saved_modes);
8655 }
8656
8657 /**************************** Flexport Logic **********************************/
8658 /*
8659 * Read count 16bit words from 16bit word address start_addr from the
8660 * SEEPROM attached to the controller, into buf, using the controller's
8661 * SEEPROM reading state machine. Optionally treat the data as a byte
8662 * stream in terms of byte order.
8663 */
8664 int
8665 ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
8666 u_int start_addr, u_int count, int bytestream)
8667 {
8668 u_int cur_addr;
8669 u_int end_addr;
8670 int error;
8671
8672 /*
8673 * If we never make it through the loop even once,
8674 * we were passed invalid arguments.
8675 */
8676 error = EINVAL;
8677 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8678 end_addr = start_addr + count;
8679 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
8680
8681 ahd_outb(ahd, SEEADR, cur_addr);
8682 ahd_outb(ahd, SEECTL, SEEOP_READ | SEESTART);
8683
8684 error = ahd_wait_seeprom(ahd);
8685 if (error) {
8686 printf("%s: ahd_wait_seeprom timed out\n", ahd_name(ahd));
8687 break;
8688 }
8689 if (bytestream != 0) {
8690 uint8_t *bytestream_ptr;
8691
8692 bytestream_ptr = (uint8_t *)buf;
8693 *bytestream_ptr++ = ahd_inb(ahd, SEEDAT);
8694 *bytestream_ptr = ahd_inb(ahd, SEEDAT+1);
8695 } else {
8696 /*
8697 * ahd_inw() already handles machine byte order.
8698 */
8699 *buf = ahd_inw(ahd, SEEDAT);
8700 }
8701 buf++;
8702 }
8703 return (error);
8704 }
8705
8706 /*
8707 * Write count 16bit words from buf, into SEEPROM attache to the
8708 * controller starting at 16bit word address start_addr, using the
8709 * controller's SEEPROM writing state machine.
8710 */
8711 int
8712 ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
8713 u_int start_addr, u_int count)
8714 {
8715 u_int cur_addr;
8716 u_int end_addr;
8717 int error;
8718 int retval;
8719
8720 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8721 error = ENOENT;
8722
8723 /* Place the chip into write-enable mode */
8724 ahd_outb(ahd, SEEADR, SEEOP_EWEN_ADDR);
8725 ahd_outb(ahd, SEECTL, SEEOP_EWEN | SEESTART);
8726 error = ahd_wait_seeprom(ahd);
8727 if (error)
8728 return (error);
8729
8730 /*
8731 * Write the data. If we don't get throught the loop at
8732 * least once, the arguments were invalid.
8733 */
8734 retval = EINVAL;
8735 end_addr = start_addr + count;
8736 for (cur_addr = start_addr; cur_addr < end_addr; cur_addr++) {
8737 ahd_outw(ahd, SEEDAT, *buf++);
8738 ahd_outb(ahd, SEEADR, cur_addr);
8739 ahd_outb(ahd, SEECTL, SEEOP_WRITE | SEESTART);
8740
8741 retval = ahd_wait_seeprom(ahd);
8742 if (retval)
8743 break;
8744 }
8745
8746 /*
8747 * Disable writes.
8748 */
8749 ahd_outb(ahd, SEEADR, SEEOP_EWDS_ADDR);
8750 ahd_outb(ahd, SEECTL, SEEOP_EWDS | SEESTART);
8751 error = ahd_wait_seeprom(ahd);
8752 if (error)
8753 return (error);
8754 return (retval);
8755 }
8756
8757 /*
8758 * Wait ~100us for the serial eeprom to satisfy our request.
8759 */
8760 int
8761 ahd_wait_seeprom(struct ahd_softc *ahd)
8762 {
8763 int cnt;
8764
8765 cnt = 2000;
8766 while ((ahd_inb(ahd, SEESTAT) & (SEEARBACK|SEEBUSY)) != 0 && --cnt)
8767 ahd_delay(5);
8768
8769 if (cnt == 0)
8770 return (ETIMEDOUT);
8771 return (0);
8772 }
8773
8774 /*
8775 * Validate the two checksums in the per_channel
8776 * vital product data struct.
8777 */
8778 int
8779 ahd_verify_vpd_cksum(struct vpd_config *vpd)
8780 {
8781 int i;
8782 int maxaddr;
8783 uint32_t checksum;
8784 uint8_t *vpdarray;
8785
8786 vpdarray = (uint8_t *)vpd;
8787 maxaddr = offsetof(struct vpd_config, vpd_checksum);
8788 checksum = 0;
8789 for (i = offsetof(struct vpd_config, resource_type); i < maxaddr; i++)
8790 checksum = checksum + vpdarray[i];
8791 if (checksum == 0
8792 || (-checksum & 0xFF) != vpd->vpd_checksum)
8793 return (0);
8794
8795 checksum = 0;
8796 maxaddr = offsetof(struct vpd_config, checksum);
8797 for (i = offsetof(struct vpd_config, default_target_flags);
8798 i < maxaddr; i++)
8799 checksum = checksum + vpdarray[i];
8800 if (checksum == 0
8801 || (-checksum & 0xFF) != vpd->checksum)
8802 return (0);
8803 return (1);
8804 }
8805
8806 int
8807 ahd_verify_cksum(struct seeprom_config *sc)
8808 {
8809 int i;
8810 int maxaddr;
8811 uint32_t checksum;
8812 uint16_t *scarray;
8813
8814 maxaddr = (sizeof(*sc)/2) - 1;
8815 checksum = 0;
8816 scarray = (uint16_t *)sc;
8817
8818 for (i = 0; i < maxaddr; i++)
8819 checksum = checksum + scarray[i];
8820 if (checksum == 0
8821 || (checksum & 0xFFFF) != sc->checksum) {
8822 return (0);
8823 } else {
8824 return (1);
8825 }
8826 }
8827
8828 int
8829 ahd_acquire_seeprom(struct ahd_softc *ahd)
8830 {
8831 /*
8832 * We should be able to determine the SEEPROM type
8833 * from the flexport logic, but unfortunately not
8834 * all implementations have this logic and there is
8835 * no programatic method for determining if the logic
8836 * is present.
8837 */
8838
8839 return (1);
8840 #if 0
8841 uint8_t seetype;
8842 int error;
8843
8844 error = ahd_read_flexport(ahd, FLXADDR_ROMSTAT_CURSENSECTL, &seetype);
8845 if (error != 0
8846 || ((seetype & FLX_ROMSTAT_SEECFG) == FLX_ROMSTAT_SEE_NONE))
8847 return (0);
8848 return (1);
8849 #endif
8850 }
8851
8852 void
8853 ahd_release_seeprom(struct ahd_softc *ahd)
8854 {
8855 /* Currently a no-op */
8856 }
8857
8858 int
8859 ahd_write_flexport(struct ahd_softc *ahd, u_int addr, u_int value)
8860 {
8861 int error;
8862
8863 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8864 if (addr > 7)
8865 panic("ahd_write_flexport: address out of range");
8866 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
8867 error = ahd_wait_flexport(ahd);
8868 if (error != 0)
8869 return (error);
8870 ahd_outb(ahd, BRDDAT, value);
8871 ahd_flush_device_writes(ahd);
8872 ahd_outb(ahd, BRDCTL, BRDSTB|BRDEN|(addr << 3));
8873 ahd_flush_device_writes(ahd);
8874 ahd_outb(ahd, BRDCTL, BRDEN|(addr << 3));
8875 ahd_flush_device_writes(ahd);
8876 ahd_outb(ahd, BRDCTL, 0);
8877 ahd_flush_device_writes(ahd);
8878 return (0);
8879 }
8880
8881 int
8882 ahd_read_flexport(struct ahd_softc *ahd, u_int addr, uint8_t *value)
8883 {
8884 int error;
8885
8886 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8887 if (addr > 7)
8888 panic("ahd_read_flexport: address out of range");
8889 ahd_outb(ahd, BRDCTL, BRDRW|BRDEN|(addr << 3));
8890 error = ahd_wait_flexport(ahd);
8891 if (error != 0)
8892 return (error);
8893 *value = ahd_inb(ahd, BRDDAT);
8894 ahd_outb(ahd, BRDCTL, 0);
8895 ahd_flush_device_writes(ahd);
8896 return (0);
8897 }
8898
8899 /*
8900 * Wait at most 2 seconds for flexport arbitration to succeed.
8901 */
8902 int
8903 ahd_wait_flexport(struct ahd_softc *ahd)
8904 {
8905 int cnt;
8906
8907 AHD_ASSERT_MODES(ahd, AHD_MODE_SCSI_MSK, AHD_MODE_SCSI_MSK);
8908 cnt = 1000000 * 2 / 5;
8909 while ((ahd_inb(ahd, BRDCTL) & FLXARBACK) == 0 && --cnt)
8910 ahd_delay(5);
8911
8912 if (cnt == 0)
8913 return (ETIMEDOUT);
8914 return (0);
8915 }
8916
8917 /************************* Target Mode ****************************************/
8918 #ifdef AHD_TARGET_MODE
8919 cam_status
8920 ahd_find_tmode_devs(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb,
8921 struct ahd_tmode_tstate **tstate,
8922 struct ahd_tmode_lstate **lstate,
8923 int notfound_failure)
8924 {
8925
8926 if ((ahd->features & AHD_TARGETMODE) == 0)
8927 return (CAM_REQ_INVALID);
8928
8929 /*
8930 * Handle the 'black hole' device that sucks up
8931 * requests to unattached luns on enabled targets.
8932 */
8933 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
8934 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
8935 *tstate = NULL;
8936 *lstate = ahd->black_hole;
8937 } else {
8938 u_int max_id;
8939
8940 max_id = (ahd->features & AHD_WIDE) ? 15 : 7;
8941 if (ccb->ccb_h.target_id > max_id)
8942 return (CAM_TID_INVALID);
8943
8944 if (ccb->ccb_h.target_lun >= AHD_NUM_LUNS)
8945 return (CAM_LUN_INVALID);
8946
8947 *tstate = ahd->enabled_targets[ccb->ccb_h.target_id];
8948 *lstate = NULL;
8949 if (*tstate != NULL)
8950 *lstate =
8951 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
8952 }
8953
8954 if (notfound_failure != 0 && *lstate == NULL)
8955 return (CAM_PATH_INVALID);
8956
8957 return (CAM_REQ_CMP);
8958 }
8959
8960 void
8961 ahd_handle_en_lun(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
8962 {
8963 #if NOT_YET
8964 struct ahd_tmode_tstate *tstate;
8965 struct ahd_tmode_lstate *lstate;
8966 struct ccb_en_lun *cel;
8967 cam_status status;
8968 u_int target;
8969 u_int lun;
8970 u_int target_mask;
8971 u_long s;
8972 char channel;
8973
8974 status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate, &lstate,
8975 /*notfound_failure*/FALSE);
8976
8977 if (status != CAM_REQ_CMP) {
8978 ccb->ccb_h.status = status;
8979 return;
8980 }
8981
8982 if ((ahd->features & AHD_MULTIROLE) != 0) {
8983 u_int our_id;
8984
8985 our_id = ahd->our_id;
8986 if (ccb->ccb_h.target_id != our_id) {
8987 if ((ahd->features & AHD_MULTI_TID) != 0
8988 && (ahd->flags & AHD_INITIATORROLE) != 0) {
8989 /*
8990 * Only allow additional targets if
8991 * the initiator role is disabled.
8992 * The hardware cannot handle a re-select-in
8993 * on the initiator id during a re-select-out
8994 * on a different target id.
8995 */
8996 status = CAM_TID_INVALID;
8997 } else if ((ahd->flags & AHD_INITIATORROLE) != 0
8998 || ahd->enabled_luns > 0) {
8999 /*
9000 * Only allow our target id to change
9001 * if the initiator role is not configured
9002 * and there are no enabled luns which
9003 * are attached to the currently registered
9004 * scsi id.
9005 */
9006 status = CAM_TID_INVALID;
9007 }
9008 }
9009 }
9010
9011 if (status != CAM_REQ_CMP) {
9012 ccb->ccb_h.status = status;
9013 return;
9014 }
9015
9016 /*
9017 * We now have an id that is valid.
9018 * If we aren't in target mode, switch modes.
9019 */
9020 if ((ahd->flags & AHD_TARGETROLE) == 0
9021 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
9022 u_long s;
9023
9024 printf("Configuring Target Mode\n");
9025 ahd_lock(ahd, &s);
9026 if (LIST_FIRST(&ahd->pending_scbs) != NULL) {
9027 ccb->ccb_h.status = CAM_BUSY;
9028 ahd_unlock(ahd, &s);
9029 return;
9030 }
9031 ahd->flags |= AHD_TARGETROLE;
9032 if ((ahd->features & AHD_MULTIROLE) == 0)
9033 ahd->flags &= ~AHD_INITIATORROLE;
9034 ahd_pause(ahd);
9035 ahd_loadseq(ahd);
9036 ahd_unlock(ahd, &s);
9037 }
9038 cel = &ccb->cel;
9039 target = ccb->ccb_h.target_id;
9040 lun = ccb->ccb_h.target_lun;
9041 channel = SIM_CHANNEL(ahd, sim);
9042 target_mask = 0x01 << target;
9043 if (channel == 'B')
9044 target_mask <<= 8;
9045
9046 if (cel->enable != 0) {
9047 u_int scsiseq1;
9048
9049 /* Are we already enabled?? */
9050 if (lstate != NULL) {
9051 xpt_print_path(ccb->ccb_h.path);
9052 printf("Lun already enabled\n");
9053 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
9054 return;
9055 }
9056
9057 if (cel->grp6_len != 0
9058 || cel->grp7_len != 0) {
9059 /*
9060 * Don't (yet?) support vendor
9061 * specific commands.
9062 */
9063 ccb->ccb_h.status = CAM_REQ_INVALID;
9064 printf("Non-zero Group Codes\n");
9065 return;
9066 }
9067
9068 /*
9069 * Seems to be okay.
9070 * Setup our data structures.
9071 */
9072 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
9073 tstate = ahd_alloc_tstate(ahd, target, channel);
9074 if (tstate == NULL) {
9075 xpt_print_path(ccb->ccb_h.path);
9076 printf("Couldn't allocate tstate\n");
9077 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
9078 return;
9079 }
9080 }
9081 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
9082 if (lstate == NULL) {
9083 xpt_print_path(ccb->ccb_h.path);
9084 printf("Couldn't allocate lstate\n");
9085 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
9086 return;
9087 }
9088 memset(lstate, 0, sizeof(*lstate));
9089 status = xpt_create_path(&lstate->path, /*periph*/NULL,
9090 xpt_path_path_id(ccb->ccb_h.path),
9091 xpt_path_target_id(ccb->ccb_h.path),
9092 xpt_path_lun_id(ccb->ccb_h.path));
9093 if (status != CAM_REQ_CMP) {
9094 free(lstate, M_DEVBUF);
9095 xpt_print_path(ccb->ccb_h.path);
9096 printf("Couldn't allocate path\n");
9097 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
9098 return;
9099 }
9100 SLIST_INIT(&lstate->accept_tios);
9101 SLIST_INIT(&lstate->immed_notifies);
9102 ahd_lock(ahd, &s);
9103 ahd_pause(ahd);
9104 if (target != CAM_TARGET_WILDCARD) {
9105 tstate->enabled_luns[lun] = lstate;
9106 ahd->enabled_luns++;
9107
9108 if ((ahd->features & AHD_MULTI_TID) != 0) {
9109 u_int targid_mask;
9110
9111 targid_mask = ahd_inb(ahd, TARGID)
9112 | (ahd_inb(ahd, TARGID + 1) << 8);
9113
9114 targid_mask |= target_mask;
9115 ahd_outb(ahd, TARGID, targid_mask);
9116 ahd_outb(ahd, TARGID+1, (targid_mask >> 8));
9117
9118 ahd_update_scsiid(ahd, targid_mask);
9119 } else {
9120 u_int our_id;
9121 char channel;
9122
9123 channel = SIM_CHANNEL(ahd, sim);
9124 our_id = SIM_SCSI_ID(ahd, sim);
9125
9126 /*
9127 * This can only happen if selections
9128 * are not enabled
9129 */
9130 if (target != our_id) {
9131 u_int sblkctl;
9132 char cur_channel;
9133 int swap;
9134
9135 sblkctl = ahd_inb(ahd, SBLKCTL);
9136 cur_channel = (sblkctl & SELBUSB)
9137 ? 'B' : 'A';
9138 if ((ahd->features & AHD_TWIN) == 0)
9139 cur_channel = 'A';
9140 swap = cur_channel != channel;
9141 ahd->our_id = target;
9142
9143 if (swap)
9144 ahd_outb(ahd, SBLKCTL,
9145 sblkctl ^ SELBUSB);
9146
9147 ahd_outb(ahd, SCSIID, target);
9148
9149 if (swap)
9150 ahd_outb(ahd, SBLKCTL, sblkctl);
9151 }
9152 }
9153 } else
9154 ahd->black_hole = lstate;
9155 /* Allow select-in operations */
9156 if (ahd->black_hole != NULL && ahd->enabled_luns > 0) {
9157 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
9158 scsiseq1 |= ENSELI;
9159 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
9160 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
9161 scsiseq1 |= ENSELI;
9162 ahd_outb(ahd, SCSISEQ1, scsiseq1);
9163 }
9164 ahd_unpause(ahd);
9165 ahd_unlock(ahd, &s);
9166 ccb->ccb_h.status = CAM_REQ_CMP;
9167 xpt_print_path(ccb->ccb_h.path);
9168 printf("Lun now enabled for target mode\n");
9169 } else {
9170 struct scb *scb;
9171 int i, empty;
9172
9173 if (lstate == NULL) {
9174 ccb->ccb_h.status = CAM_LUN_INVALID;
9175 return;
9176 }
9177
9178 ahd_lock(ahd, &s);
9179
9180 ccb->ccb_h.status = CAM_REQ_CMP;
9181 LIST_FOREACH(scb, &ahd->pending_scbs, pending_links) {
9182 struct ccb_hdr *ccbh;
9183
9184 ccbh = &scb->io_ctx->ccb_h;
9185 if (ccbh->func_code == XPT_CONT_TARGET_IO
9186 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
9187 printf("CTIO pending\n");
9188 ccb->ccb_h.status = CAM_REQ_INVALID;
9189 ahd_unlock(ahd, &s);
9190 return;
9191 }
9192 }
9193
9194 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
9195 printf("ATIOs pending\n");
9196 ccb->ccb_h.status = CAM_REQ_INVALID;
9197 }
9198
9199 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
9200 printf("INOTs pending\n");
9201 ccb->ccb_h.status = CAM_REQ_INVALID;
9202 }
9203
9204 if (ccb->ccb_h.status != CAM_REQ_CMP) {
9205 ahd_unlock(ahd, &s);
9206 return;
9207 }
9208
9209 xpt_print_path(ccb->ccb_h.path);
9210 printf("Target mode disabled\n");
9211 xpt_free_path(lstate->path);
9212 free(lstate, M_DEVBUF);
9213
9214 ahd_pause(ahd);
9215 /* Can we clean up the target too? */
9216 if (target != CAM_TARGET_WILDCARD) {
9217 tstate->enabled_luns[lun] = NULL;
9218 ahd->enabled_luns--;
9219 for (empty = 1, i = 0; i < 8; i++)
9220 if (tstate->enabled_luns[i] != NULL) {
9221 empty = 0;
9222 break;
9223 }
9224
9225 if (empty) {
9226 ahd_free_tstate(ahd, target, channel,
9227 /*force*/FALSE);
9228 if (ahd->features & AHD_MULTI_TID) {
9229 u_int targid_mask;
9230
9231 targid_mask = ahd_inb(ahd, TARGID)
9232 | (ahd_inb(ahd, TARGID + 1)
9233 << 8);
9234
9235 targid_mask &= ~target_mask;
9236 ahd_outb(ahd, TARGID, targid_mask);
9237 ahd_outb(ahd, TARGID+1,
9238 (targid_mask >> 8));
9239 ahd_update_scsiid(ahd, targid_mask);
9240 }
9241 }
9242 } else {
9243
9244 ahd->black_hole = NULL;
9245
9246 /*
9247 * We can't allow selections without
9248 * our black hole device.
9249 */
9250 empty = TRUE;
9251 }
9252 if (ahd->enabled_luns == 0) {
9253 /* Disallow select-in */
9254 u_int scsiseq1;
9255
9256 scsiseq1 = ahd_inb(ahd, SCSISEQ_TEMPLATE);
9257 scsiseq1 &= ~ENSELI;
9258 ahd_outb(ahd, SCSISEQ_TEMPLATE, scsiseq1);
9259 scsiseq1 = ahd_inb(ahd, SCSISEQ1);
9260 scsiseq1 &= ~ENSELI;
9261 ahd_outb(ahd, SCSISEQ1, scsiseq1);
9262
9263 if ((ahd->features & AHD_MULTIROLE) == 0) {
9264 printf("Configuring Initiator Mode\n");
9265 ahd->flags &= ~AHD_TARGETROLE;
9266 ahd->flags |= AHD_INITIATORROLE;
9267 ahd_pause(ahd);
9268 ahd_loadseq(ahd);
9269 }
9270 }
9271 ahd_unpause(ahd);
9272 ahd_unlock(ahd, &s);
9273 }
9274 #endif
9275 }
9276
9277 static void
9278 ahd_update_scsiid(struct ahd_softc *ahd, u_int targid_mask)
9279 {
9280 #if NOT_YET
9281 u_int scsiid_mask;
9282 u_int scsiid;
9283
9284 if ((ahd->features & AHD_MULTI_TID) == 0)
9285 panic("ahd_update_scsiid called on non-multitid unit\n");
9286
9287 /*
9288 * Since we will rely on the TARGID mask
9289 * for selection enables, ensure that OID
9290 * in SCSIID is not set to some other ID
9291 * that we don't want to allow selections on.
9292 */
9293 if ((ahd->features & AHD_ULTRA2) != 0)
9294 scsiid = ahd_inb(ahd, SCSIID_ULTRA2);
9295 else
9296 scsiid = ahd_inb(ahd, SCSIID);
9297 scsiid_mask = 0x1 << (scsiid & OID);
9298 if ((targid_mask & scsiid_mask) == 0) {
9299 u_int our_id;
9300
9301 /* ffs counts from 1 */
9302 our_id = ffs(targid_mask);
9303 if (our_id == 0)
9304 our_id = ahd->our_id;
9305 else
9306 our_id--;
9307 scsiid &= TID;
9308 scsiid |= our_id;
9309 }
9310 if ((ahd->features & AHD_ULTRA2) != 0)
9311 ahd_outb(ahd, SCSIID_ULTRA2, scsiid);
9312 else
9313 ahd_outb(ahd, SCSIID, scsiid);
9314 #endif
9315 }
9316
9317 #ifdef AHD_TARGET_MODE
9318 void
9319 ahd_run_tqinfifo(struct ahd_softc *ahd, int paused)
9320 {
9321 struct target_cmd *cmd;
9322
9323 ahd_sync_tqinfifo(ahd, BUS_DMASYNC_POSTREAD);
9324 while ((cmd = &ahd->targetcmds[ahd->tqinfifonext])->cmd_valid != 0) {
9325
9326 /*
9327 * Only advance through the queue if we
9328 * have the resources to process the command.
9329 */
9330 if (ahd_handle_target_cmd(ahd, cmd) != 0)
9331 break;
9332
9333 cmd->cmd_valid = 0;
9334 ahd_dmamap_sync(ahd, ahd->parent_dmat /*shared_data_dmat*/,
9335 ahd->shared_data_map.dmamap,
9336 ahd_targetcmd_offset(ahd, ahd->tqinfifonext),
9337 sizeof(struct target_cmd),
9338 BUS_DMASYNC_PREREAD);
9339 ahd->tqinfifonext++;
9340
9341 /*
9342 * Lazily update our position in the target mode incoming
9343 * command queue as seen by the sequencer.
9344 */
9345 if ((ahd->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
9346 u_int hs_mailbox;
9347
9348 hs_mailbox = ahd_inb(ahd, HS_MAILBOX);
9349 hs_mailbox &= ~HOST_TQINPOS;
9350 hs_mailbox |= ahd->tqinfifonext & HOST_TQINPOS;
9351 ahd_outb(ahd, HS_MAILBOX, hs_mailbox);
9352 }
9353 }
9354 }
9355 #endif
9356
9357 static int
9358 ahd_handle_target_cmd(struct ahd_softc *ahd, struct target_cmd *cmd)
9359 {
9360 struct ahd_tmode_tstate *tstate;
9361 struct ahd_tmode_lstate *lstate;
9362 struct ccb_accept_tio *atio;
9363 uint8_t *byte;
9364 int initiator;
9365 int target;
9366 int lun;
9367
9368 initiator = SCSIID_TARGET(ahd, cmd->scsiid);
9369 target = SCSIID_OUR_ID(cmd->scsiid);
9370 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
9371
9372 byte = cmd->bytes;
9373 tstate = ahd->enabled_targets[target];
9374 lstate = NULL;
9375 if (tstate != NULL)
9376 lstate = tstate->enabled_luns[lun];
9377
9378 /*
9379 * Commands for disabled luns go to the black hole driver.
9380 */
9381 if (lstate == NULL)
9382 lstate = ahd->black_hole;
9383
9384 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
9385 if (atio == NULL) {
9386 ahd->flags |= AHD_TQINFIFO_BLOCKED;
9387 /*
9388 * Wait for more ATIOs from the peripheral driver for this lun.
9389 */
9390 return (1);
9391 } else
9392 ahd->flags &= ~AHD_TQINFIFO_BLOCKED;
9393 #ifdef AHD_DEBUG
9394 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
9395 printf("%s: incoming command from %d for %d:%d%s\n",
9396 ahd_name(ahd),
9397 initiator, target, lun,
9398 lstate == ahd->black_hole ? "(Black Holed)" : "");
9399 #endif
9400 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
9401
9402 if (lstate == ahd->black_hole) {
9403 /* Fill in the wildcards */
9404 atio->ccb_h.target_id = target;
9405 atio->ccb_h.target_lun = lun;
9406 }
9407
9408 /*
9409 * Package it up and send it off to
9410 * whomever has this lun enabled.
9411 */
9412 atio->sense_len = 0;
9413 atio->init_id = initiator;
9414 if (byte[0] != 0xFF) {
9415 /* Tag was included */
9416 atio->tag_action = *byte++;
9417 atio->tag_id = *byte++;
9418 atio->ccb_h.flags = CAM_TAG_ACTION_VALID;
9419 } else {
9420 atio->ccb_h.flags = 0;
9421 }
9422 byte++;
9423
9424 /* Okay. Now determine the cdb size based on the command code */
9425 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
9426 case 0:
9427 atio->cdb_len = 6;
9428 break;
9429 case 1:
9430 case 2:
9431 atio->cdb_len = 10;
9432 break;
9433 case 4:
9434 atio->cdb_len = 16;
9435 break;
9436 case 5:
9437 atio->cdb_len = 12;
9438 break;
9439 case 3:
9440 default:
9441 /* Only copy the opcode. */
9442 atio->cdb_len = 1;
9443 printf("Reserved or VU command code type encountered\n");
9444 break;
9445 }
9446
9447 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
9448
9449 atio->ccb_h.status |= CAM_CDB_RECVD;
9450
9451 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
9452 /*
9453 * We weren't allowed to disconnect.
9454 * We're hanging on the bus until a
9455 * continue target I/O comes in response
9456 * to this accept tio.
9457 */
9458 #ifdef AHD_DEBUG
9459 if ((ahd_debug & AHD_SHOW_TQIN) != 0)
9460 printf("Received Immediate Command %d:%d:%d - %p\n",
9461 initiator, target, lun, ahd->pending_device);
9462 #endif
9463 ahd->pending_device = lstate;
9464 ahd_freeze_ccb((union ccb *)atio);
9465 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
9466 }
9467 xpt_done((union ccb*)atio);
9468 return (0);
9469 }
9470
9471 #endif
9472
9473 static int
9474 ahd_createdmamem(tag, size, flags, mapp, vaddr, baddr, seg, nseg, myname, what)
9475 bus_dma_tag_t tag;
9476 int size;
9477 int flags;
9478 bus_dmamap_t *mapp;
9479 caddr_t *vaddr;
9480 bus_addr_t *baddr;
9481 bus_dma_segment_t *seg;
9482 int *nseg;
9483 const char *myname, *what;
9484 {
9485 int error, level = 0;
9486
9487 if ((error = bus_dmamem_alloc(tag, size, PAGE_SIZE, 0,
9488 seg, 1, nseg, BUS_DMA_NOWAIT)) != 0) {
9489 printf("%s: failed to allocate DMA mem for %s, error = %d\n",
9490 myname, what, error);
9491 goto out;
9492 }
9493 level++;
9494
9495 if ((error = bus_dmamem_map(tag, seg, *nseg, size, vaddr,
9496 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
9497 printf("%s: failed to map DMA mem for %s, error = %d\n",
9498 myname, what, error);
9499 goto out;
9500 }
9501 level++;
9502
9503 if ((error = bus_dmamap_create(tag, size, 1, size, 0,
9504 BUS_DMA_NOWAIT | flags, mapp)) != 0) {
9505 printf("%s: failed to create DMA map for %s, error = %d\n",
9506 myname, what, error);
9507 goto out;
9508 }
9509 level++;
9510
9511
9512 if ((error = bus_dmamap_load(tag, *mapp, *vaddr, size, NULL,
9513 BUS_DMA_NOWAIT)) != 0) {
9514 printf("%s: failed to load DMA map for %s, error = %d\n",
9515 myname, what, error);
9516 goto out;
9517 }
9518
9519 *baddr = (*mapp)->dm_segs[0].ds_addr;
9520
9521 return 0;
9522 out:
9523 printf("ahd_createdmamem error (%d)\n", level);
9524 switch (level) {
9525 case 3:
9526 bus_dmamap_destroy(tag, *mapp);
9527 /* FALLTHROUGH */
9528 case 2:
9529 bus_dmamem_unmap(tag, *vaddr, size);
9530 /* FALLTHROUGH */
9531 case 1:
9532 bus_dmamem_free(tag, seg, *nseg);
9533 break;
9534 default:
9535 break;
9536 }
9537
9538 return error;
9539 }
9540
9541 static void
9542 ahd_freedmamem(tag, size, map, vaddr, seg, nseg)
9543 bus_dma_tag_t tag;
9544 int size;
9545 bus_dmamap_t map;
9546 caddr_t vaddr;
9547 bus_dma_segment_t *seg;
9548 int nseg;
9549 {
9550
9551 bus_dmamap_unload(tag, map);
9552 bus_dmamap_destroy(tag, map);
9553 bus_dmamem_unmap(tag, vaddr, size);
9554 bus_dmamem_free(tag, seg, nseg);
9555 }
9556
9557 static void
9558 ahd_update_xfer_mode(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
9559 {
9560 struct scsipi_xfer_mode xm;
9561 struct ahd_initiator_tinfo *tinfo;
9562 struct ahd_tmode_tstate *tstate;
9563
9564 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel, devinfo->our_scsiid,
9565 devinfo->target, &tstate);
9566
9567 xm.xm_target = devinfo->target;
9568 xm.xm_mode = 0;
9569 xm.xm_period = tinfo->curr.period;
9570 xm.xm_offset = tinfo->curr.offset;
9571 if (tinfo->curr.width == 1)
9572 xm.xm_mode |= PERIPH_CAP_WIDE16;
9573 if (tinfo->curr.period)
9574 xm.xm_mode |= PERIPH_CAP_SYNC;
9575 if (tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ)
9576 xm.xm_mode |= PERIPH_CAP_DT;
9577 if (tstate->tagenable & devinfo->target_mask)
9578 xm.xm_mode |= PERIPH_CAP_TQING;
9579
9580 tinfo->goal.width = tinfo->curr.width;
9581 tinfo->goal.period = tinfo->curr.period;
9582 tinfo->goal.offset = tinfo->curr.offset;
9583 tinfo->goal.ppr_options = tinfo->curr.ppr_options;
9584
9585 ahd_update_neg_request(ahd, devinfo, tstate,
9586 tinfo, AHD_NEG_TO_GOAL);
9587
9588 scsipi_async_event(&ahd->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
9589 }
9590