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