1 /*-
2 * Core routines and tables shareable across OS platforms.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 1994-2002 Justin T. Gibbs.
7 * Copyright (c) 2000-2002 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/aic7xxx.c#155 $
43 */
44
45 #ifdef __linux__
46 #include "aic7xxx_osm.h"
47 #include "aic7xxx_inline.h"
48 #include "aicasm/aicasm_insformat.h"
49 #else
50 #include <sys/cdefs.h>
51 #include <dev/aic7xxx/aic7xxx_osm.h>
52 #include <dev/aic7xxx/aic7xxx_inline.h>
53 #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
54 #endif
55
56 /****************************** Softc Data ************************************/
57 struct ahc_softc_tailq ahc_tailq = TAILQ_HEAD_INITIALIZER(ahc_tailq);
58
59 /***************************** Lookup Tables **********************************/
60 char *ahc_chip_names[] =
61 {
62 "NONE",
63 "aic7770",
64 "aic7850",
65 "aic7855",
66 "aic7859",
67 "aic7860",
68 "aic7870",
69 "aic7880",
70 "aic7895",
71 "aic7895C",
72 "aic7890/91",
73 "aic7896/97",
74 "aic7892",
75 "aic7899"
76 };
77
78 /*
79 * Hardware error codes.
80 */
81 struct ahc_hard_error_entry {
82 uint8_t errno;
83 char *errmesg;
84 };
85
86 static struct ahc_hard_error_entry ahc_hard_errors[] = {
87 { ILLHADDR, "Illegal Host Access" },
88 { ILLSADDR, "Illegal Sequencer Address referrenced" },
89 { ILLOPCODE, "Illegal Opcode in sequencer program" },
90 { SQPARERR, "Sequencer Parity Error" },
91 { DPARERR, "Data-path Parity Error" },
92 { MPARERR, "Scratch or SCB Memory Parity Error" },
93 { PCIERRSTAT, "PCI Error detected" },
94 { CIOPARERR, "CIOBUS Parity Error" },
95 };
96 static const u_int num_errors = NUM_ELEMENTS(ahc_hard_errors);
97
98 static struct ahc_phase_table_entry ahc_phase_table[] =
99 {
100 { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
101 { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
102 { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
103 { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
104 { P_COMMAND, MSG_NOOP, "in Command phase" },
105 { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
106 { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
107 { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
108 { P_BUSFREE, MSG_NOOP, "while idle" },
109 { 0, MSG_NOOP, "in unknown phase" }
110 };
111
112 /*
113 * In most cases we only wish to itterate over real phases, so
114 * exclude the last element from the count.
115 */
116 static const u_int num_phases = NUM_ELEMENTS(ahc_phase_table) - 1;
117
118 /*
119 * Valid SCSIRATE values. (p. 3-17)
120 * Provides a mapping of transfer periods in ns to the proper value to
121 * stick in the scsixfer reg.
122 */
123 static struct ahc_syncrate ahc_syncrates[] =
124 {
125 /* ultra2 fast/ultra period rate */
126 { 0x42, 0x000, 9, "80.0" },
127 { 0x03, 0x000, 10, "40.0" },
128 { 0x04, 0x000, 11, "33.0" },
129 { 0x05, 0x100, 12, "20.0" },
130 { 0x06, 0x110, 15, "16.0" },
131 { 0x07, 0x120, 18, "13.4" },
132 { 0x08, 0x000, 25, "10.0" },
133 { 0x19, 0x010, 31, "8.0" },
134 { 0x1a, 0x020, 37, "6.67" },
135 { 0x1b, 0x030, 43, "5.7" },
136 { 0x1c, 0x040, 50, "5.0" },
137 { 0x00, 0x050, 56, "4.4" },
138 { 0x00, 0x060, 62, "4.0" },
139 { 0x00, 0x070, 68, "3.6" },
140 { 0x00, 0x000, 0, NULL }
141 };
142
143 /* Our Sequencer Program */
144 #include "aic7xxx_seq.h"
145
146 /**************************** Function Declarations ***************************/
147 static void ahc_force_renegotiation(struct ahc_softc *ahc,
148 struct ahc_devinfo *devinfo);
149 static struct ahc_tmode_tstate*
150 ahc_alloc_tstate(struct ahc_softc *ahc,
151 u_int scsi_id, char channel);
152 #ifdef AHC_TARGET_MODE
153 static void ahc_free_tstate(struct ahc_softc *ahc,
154 u_int scsi_id, char channel, int force);
155 #endif
156 static struct ahc_syncrate*
157 ahc_devlimited_syncrate(struct ahc_softc *ahc,
158 struct ahc_initiator_tinfo *,
159 u_int *period,
160 u_int *ppr_options,
161 role_t role);
162 static void ahc_update_pending_scbs(struct ahc_softc *ahc);
163 static void ahc_fetch_devinfo(struct ahc_softc *ahc,
164 struct ahc_devinfo *devinfo);
165 static void ahc_scb_devinfo(struct ahc_softc *ahc,
166 struct ahc_devinfo *devinfo,
167 struct scb *scb);
168 static void ahc_assert_atn(struct ahc_softc *ahc);
169 static void ahc_setup_initiator_msgout(struct ahc_softc *ahc,
170 struct ahc_devinfo *devinfo,
171 struct scb *scb);
172 static void ahc_build_transfer_msg(struct ahc_softc *ahc,
173 struct ahc_devinfo *devinfo);
174 static void ahc_construct_sdtr(struct ahc_softc *ahc,
175 struct ahc_devinfo *devinfo,
176 u_int period, u_int offset);
177 static void ahc_construct_wdtr(struct ahc_softc *ahc,
178 struct ahc_devinfo *devinfo,
179 u_int bus_width);
180 static void ahc_construct_ppr(struct ahc_softc *ahc,
181 struct ahc_devinfo *devinfo,
182 u_int period, u_int offset,
183 u_int bus_width, u_int ppr_options);
184 static void ahc_clear_msg_state(struct ahc_softc *ahc);
185 static void ahc_handle_proto_violation(struct ahc_softc *ahc);
186 static void ahc_handle_message_phase(struct ahc_softc *ahc);
187 typedef enum {
188 AHCMSG_1B,
189 AHCMSG_2B,
190 AHCMSG_EXT
191 } ahc_msgtype;
192 static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type,
193 u_int msgval, int full);
194 static int ahc_parse_msg(struct ahc_softc *ahc,
195 struct ahc_devinfo *devinfo);
196 static int ahc_handle_msg_reject(struct ahc_softc *ahc,
197 struct ahc_devinfo *devinfo);
198 static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc,
199 struct ahc_devinfo *devinfo);
200 static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc);
201 static void ahc_handle_devreset(struct ahc_softc *ahc,
202 struct ahc_devinfo *devinfo,
203 cam_status status, char *message,
204 int verbose_level);
205 #ifdef AHC_TARGET_MODE
206 static void ahc_setup_target_msgin(struct ahc_softc *ahc,
207 struct ahc_devinfo *devinfo,
208 struct scb *scb);
209 #endif
210
211 static bus_dmamap_callback_t ahc_dmamap_cb;
212 static void ahc_build_free_scb_list(struct ahc_softc *ahc);
213 static int ahc_init_scbdata(struct ahc_softc *ahc);
214 static void ahc_fini_scbdata(struct ahc_softc *ahc);
215 static void ahc_qinfifo_requeue(struct ahc_softc *ahc,
216 struct scb *prev_scb,
217 struct scb *scb);
218 static int ahc_qinfifo_count(struct ahc_softc *ahc);
219 static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc,
220 u_int prev, u_int scbptr);
221 static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc);
222 static u_int ahc_rem_wscb(struct ahc_softc *ahc,
223 u_int scbpos, u_int prev);
224 static void ahc_reset_current_bus(struct ahc_softc *ahc);
225 #ifdef AHC_DUMP_SEQ
226 static void ahc_dumpseq(struct ahc_softc *ahc);
227 #endif
228 static int ahc_loadseq(struct ahc_softc *ahc);
229 static int ahc_check_patch(struct ahc_softc *ahc,
230 struct patch **start_patch,
231 u_int start_instr, u_int *skip_addr);
232 static void ahc_download_instr(struct ahc_softc *ahc,
233 u_int instrptr, uint8_t *dconsts);
234 static int ahc_other_scb_timeout(struct ahc_softc *ahc,
235 struct scb *scb,
236 struct scb *other_scb);
237 #ifdef AHC_TARGET_MODE
238 static void ahc_queue_lstate_event(struct ahc_softc *ahc,
239 struct ahc_tmode_lstate *lstate,
240 u_int initiator_id,
241 u_int event_type,
242 u_int event_arg);
243 static void ahc_update_scsiid(struct ahc_softc *ahc,
244 u_int targid_mask);
245 static int ahc_handle_target_cmd(struct ahc_softc *ahc,
246 struct target_cmd *cmd);
247 #endif
248 /************************* Sequencer Execution Control ************************/
249 /*
250 * Restart the sequencer program from address zero
251 */
252 void
ahc_restart(struct ahc_softc * ahc)253 ahc_restart(struct ahc_softc *ahc)
254 {
255
256 ahc_pause(ahc);
257
258 /* No more pending messages. */
259 ahc_clear_msg_state(ahc);
260
261 ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */
262 ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */
263 ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
264 ahc_outb(ahc, LASTPHASE, P_BUSFREE);
265 ahc_outb(ahc, SAVED_SCSIID, 0xFF);
266 ahc_outb(ahc, SAVED_LUN, 0xFF);
267
268 /*
269 * Ensure that the sequencer's idea of TQINPOS
270 * matches our own. The sequencer increments TQINPOS
271 * only after it sees a DMA complete and a reset could
272 * occur before the increment leaving the kernel to believe
273 * the command arrived but the sequencer to not.
274 */
275 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
276
277 /* Always allow reselection */
278 ahc_outb(ahc, SCSISEQ,
279 ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
280 if ((ahc->features & AHC_CMD_CHAN) != 0) {
281 /* Ensure that no DMA operations are in progress */
282 ahc_outb(ahc, CCSCBCNT, 0);
283 ahc_outb(ahc, CCSGCTL, 0);
284 ahc_outb(ahc, CCSCBCTL, 0);
285 }
286 /*
287 * If we were in the process of DMA'ing SCB data into
288 * an SCB, replace that SCB on the free list. This prevents
289 * an SCB leak.
290 */
291 if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) {
292 ahc_add_curscb_to_free_list(ahc);
293 ahc_outb(ahc, SEQ_FLAGS2,
294 ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA);
295 }
296
297 /*
298 * Clear any pending sequencer interrupt. It is no
299 * longer relevant since we're resetting the Program
300 * Counter.
301 */
302 ahc_outb(ahc, CLRINT, CLRSEQINT);
303
304 ahc_outb(ahc, MWI_RESIDUAL, 0);
305 ahc_outb(ahc, SEQCTL, ahc->seqctl);
306 ahc_outb(ahc, SEQADDR0, 0);
307 ahc_outb(ahc, SEQADDR1, 0);
308
309 ahc_unpause(ahc);
310 }
311
312 /************************* Input/Output Queues ********************************/
313 void
ahc_run_qoutfifo(struct ahc_softc * ahc)314 ahc_run_qoutfifo(struct ahc_softc *ahc)
315 {
316 struct scb *scb;
317 u_int scb_index;
318
319 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
320 while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
321 scb_index = ahc->qoutfifo[ahc->qoutfifonext];
322 if ((ahc->qoutfifonext & 0x03) == 0x03) {
323 u_int modnext;
324
325 /*
326 * Clear 32bits of QOUTFIFO at a time
327 * so that we don't clobber an incoming
328 * byte DMA to the array on architectures
329 * that only support 32bit load and store
330 * operations.
331 */
332 modnext = ahc->qoutfifonext & ~0x3;
333 *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL;
334 aic_dmamap_sync(ahc, ahc->shared_data_dmat,
335 ahc->shared_data_dmamap,
336 /*offset*/modnext, /*len*/4,
337 BUS_DMASYNC_PREREAD);
338 }
339 ahc->qoutfifonext++;
340
341 scb = ahc_lookup_scb(ahc, scb_index);
342 if (scb == NULL) {
343 printf("%s: WARNING no command for scb %d "
344 "(cmdcmplt)\nQOUTPOS = %d\n",
345 ahc_name(ahc), scb_index,
346 (ahc->qoutfifonext - 1) & 0xFF);
347 continue;
348 }
349
350 /*
351 * Save off the residual
352 * if there is one.
353 */
354 ahc_update_residual(ahc, scb);
355 ahc_done(ahc, scb);
356 }
357 }
358
359 void
ahc_run_untagged_queues(struct ahc_softc * ahc)360 ahc_run_untagged_queues(struct ahc_softc *ahc)
361 {
362 int i;
363
364 for (i = 0; i < 16; i++)
365 ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]);
366 }
367
368 void
ahc_run_untagged_queue(struct ahc_softc * ahc,struct scb_tailq * queue)369 ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue)
370 {
371 struct scb *scb;
372
373 if (ahc->untagged_queue_lock != 0)
374 return;
375
376 if ((scb = TAILQ_FIRST(queue)) != NULL
377 && (scb->flags & SCB_ACTIVE) == 0) {
378 scb->flags |= SCB_ACTIVE;
379 /*
380 * Timers are disabled while recovery is in progress.
381 */
382 aic_scb_timer_start(scb);
383 ahc_queue_scb(ahc, scb);
384 }
385 }
386
387 /************************* Interrupt Handling *********************************/
388 void
ahc_handle_brkadrint(struct ahc_softc * ahc)389 ahc_handle_brkadrint(struct ahc_softc *ahc)
390 {
391 /*
392 * We upset the sequencer :-(
393 * Lookup the error message
394 */
395 int i;
396 int error;
397
398 error = ahc_inb(ahc, ERROR);
399 for (i = 0; error != 1 && i < num_errors; i++)
400 error >>= 1;
401 printf("%s: brkadrint, %s at seqaddr = 0x%x\n",
402 ahc_name(ahc), ahc_hard_errors[i].errmesg,
403 ahc_inb(ahc, SEQADDR0) |
404 (ahc_inb(ahc, SEQADDR1) << 8));
405
406 ahc_dump_card_state(ahc);
407
408 /* Tell everyone that this HBA is no longer available */
409 ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
410 CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
411 CAM_NO_HBA);
412
413 /* Disable all interrupt sources by resetting the controller */
414 ahc_shutdown(ahc);
415 }
416
417 void
ahc_handle_seqint(struct ahc_softc * ahc,u_int intstat)418 ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
419 {
420 struct scb *scb;
421 struct ahc_devinfo devinfo;
422
423 ahc_fetch_devinfo(ahc, &devinfo);
424
425 /*
426 * Clear the upper byte that holds SEQINT status
427 * codes and clear the SEQINT bit. We will unpause
428 * the sequencer, if appropriate, after servicing
429 * the request.
430 */
431 ahc_outb(ahc, CLRINT, CLRSEQINT);
432 switch (intstat & SEQINT_MASK) {
433 case BAD_STATUS:
434 {
435 u_int scb_index;
436 struct hardware_scb *hscb;
437
438 /*
439 * Set the default return value to 0 (don't
440 * send sense). The sense code will change
441 * this if needed.
442 */
443 ahc_outb(ahc, RETURN_1, 0);
444
445 /*
446 * The sequencer will notify us when a command
447 * has an error that would be of interest to
448 * the kernel. This allows us to leave the sequencer
449 * running in the common case of command completes
450 * without error. The sequencer will already have
451 * dma'd the SCB back up to us, so we can reference
452 * the in kernel copy directly.
453 */
454 scb_index = ahc_inb(ahc, SCB_TAG);
455 scb = ahc_lookup_scb(ahc, scb_index);
456 if (scb == NULL) {
457 ahc_print_devinfo(ahc, &devinfo);
458 printf("ahc_intr - referenced scb "
459 "not valid during seqint 0x%x scb(%d)\n",
460 intstat, scb_index);
461 ahc_dump_card_state(ahc);
462 panic("for safety");
463 goto unpause;
464 }
465
466 hscb = scb->hscb;
467
468 /* Don't want to clobber the original sense code */
469 if ((scb->flags & SCB_SENSE) != 0) {
470 /*
471 * Clear the SCB_SENSE Flag and have
472 * the sequencer do a normal command
473 * complete.
474 */
475 scb->flags &= ~SCB_SENSE;
476 aic_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
477 break;
478 }
479 aic_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
480 /* Freeze the queue until the client sees the error. */
481 ahc_freeze_devq(ahc, scb);
482 aic_freeze_scb(scb);
483 aic_set_scsi_status(scb, hscb->shared_data.status.scsi_status);
484 switch (hscb->shared_data.status.scsi_status) {
485 case SCSI_STATUS_OK:
486 printf("%s: Interrupted for staus of 0???\n",
487 ahc_name(ahc));
488 break;
489 case SCSI_STATUS_CMD_TERMINATED:
490 case SCSI_STATUS_CHECK_COND:
491 {
492 struct ahc_dma_seg *sg;
493 struct scsi_sense *sc;
494 struct ahc_initiator_tinfo *targ_info;
495 struct ahc_tmode_tstate *tstate;
496 struct ahc_transinfo *tinfo;
497 #ifdef AHC_DEBUG
498 if (ahc_debug & AHC_SHOW_SENSE) {
499 ahc_print_path(ahc, scb);
500 printf("SCB %d: requests Check Status\n",
501 scb->hscb->tag);
502 }
503 #endif
504
505 if (aic_perform_autosense(scb) == 0)
506 break;
507
508 targ_info = ahc_fetch_transinfo(ahc,
509 devinfo.channel,
510 devinfo.our_scsiid,
511 devinfo.target,
512 &tstate);
513 tinfo = &targ_info->curr;
514 sg = scb->sg_list;
515 sc = (struct scsi_sense *)(&hscb->shared_data.cdb);
516 /*
517 * Save off the residual if there is one.
518 */
519 ahc_update_residual(ahc, scb);
520 #ifdef AHC_DEBUG
521 if (ahc_debug & AHC_SHOW_SENSE) {
522 ahc_print_path(ahc, scb);
523 printf("Sending Sense\n");
524 }
525 #endif
526 sg->addr = ahc_get_sense_bufaddr(ahc, scb);
527 sg->len = aic_get_sense_bufsize(ahc, scb);
528 sg->len |= AHC_DMA_LAST_SEG;
529
530 /* Fixup byte order */
531 sg->addr = aic_htole32(sg->addr);
532 sg->len = aic_htole32(sg->len);
533
534 sc->opcode = REQUEST_SENSE;
535 sc->byte2 = 0;
536 if (tinfo->protocol_version <= SCSI_REV_2
537 && SCB_GET_LUN(scb) < 8)
538 sc->byte2 = SCB_GET_LUN(scb) << 5;
539 sc->unused[0] = 0;
540 sc->unused[1] = 0;
541 sc->length = sg->len;
542 sc->control = 0;
543
544 /*
545 * We can't allow the target to disconnect.
546 * This will be an untagged transaction and
547 * having the target disconnect will make this
548 * transaction indestinguishable from outstanding
549 * tagged transactions.
550 */
551 hscb->control = 0;
552
553 /*
554 * This request sense could be because the
555 * the device lost power or in some other
556 * way has lost our transfer negotiations.
557 * Renegotiate if appropriate. Unit attention
558 * errors will be reported before any data
559 * phases occur.
560 */
561 if (aic_get_residual(scb)
562 == aic_get_transfer_length(scb)) {
563 ahc_update_neg_request(ahc, &devinfo,
564 tstate, targ_info,
565 AHC_NEG_IF_NON_ASYNC);
566 }
567 if (tstate->auto_negotiate & devinfo.target_mask) {
568 hscb->control |= MK_MESSAGE;
569 scb->flags &= ~SCB_NEGOTIATE;
570 scb->flags |= SCB_AUTO_NEGOTIATE;
571 }
572 hscb->cdb_len = sizeof(*sc);
573 hscb->dataptr = sg->addr;
574 hscb->datacnt = sg->len;
575 hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
576 hscb->sgptr = aic_htole32(hscb->sgptr);
577 scb->sg_count = 1;
578 scb->flags |= SCB_SENSE;
579 ahc_qinfifo_requeue_tail(ahc, scb);
580 ahc_outb(ahc, RETURN_1, SEND_SENSE);
581 /*
582 * Ensure we have enough time to actually
583 * retrieve the sense, but only schedule
584 * the timer if we are not in recovery or
585 * this is a recovery SCB that is allowed
586 * to have an active timer.
587 */
588 if (ahc->scb_data->recovery_scbs == 0
589 || (scb->flags & SCB_RECOVERY_SCB) != 0)
590 aic_scb_timer_reset(scb, 5 * 1000);
591 break;
592 }
593 default:
594 break;
595 }
596 break;
597 }
598 case NO_MATCH:
599 {
600 /* Ensure we don't leave the selection hardware on */
601 ahc_outb(ahc, SCSISEQ,
602 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
603
604 printf("%s:%c:%d: no active SCB for reconnecting "
605 "target - issuing BUS DEVICE RESET\n",
606 ahc_name(ahc), devinfo.channel, devinfo.target);
607 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
608 "ARG_1 == 0x%x ACCUM = 0x%x\n",
609 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
610 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
611 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
612 "SINDEX == 0x%x\n",
613 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
614 ahc_index_busy_tcl(ahc,
615 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
616 ahc_inb(ahc, SAVED_LUN))),
617 ahc_inb(ahc, SINDEX));
618 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
619 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
620 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
621 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
622 ahc_inb(ahc, SCB_CONTROL));
623 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
624 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
625 printf("SXFRCTL0 == 0x%x\n", ahc_inb(ahc, SXFRCTL0));
626 printf("SEQCTL == 0x%x\n", ahc_inb(ahc, SEQCTL));
627 ahc_dump_card_state(ahc);
628 ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
629 ahc->msgout_len = 1;
630 ahc->msgout_index = 0;
631 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
632 ahc_outb(ahc, MSG_OUT, HOST_MSG);
633 ahc_assert_atn(ahc);
634 break;
635 }
636 case SEND_REJECT:
637 {
638 u_int rejbyte = ahc_inb(ahc, ACCUM);
639 printf("%s:%c:%d: Warning - unknown message received from "
640 "target (0x%x). Rejecting\n",
641 ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
642 break;
643 }
644 case PROTO_VIOLATION:
645 {
646 ahc_handle_proto_violation(ahc);
647 break;
648 }
649 case IGN_WIDE_RES:
650 ahc_handle_ign_wide_residue(ahc, &devinfo);
651 break;
652 case PDATA_REINIT:
653 ahc_reinitialize_dataptrs(ahc);
654 break;
655 case BAD_PHASE:
656 {
657 u_int lastphase;
658
659 lastphase = ahc_inb(ahc, LASTPHASE);
660 printf("%s:%c:%d: unknown scsi bus phase %x, "
661 "lastphase = 0x%x. Attempting to continue\n",
662 ahc_name(ahc), devinfo.channel, devinfo.target,
663 lastphase, ahc_inb(ahc, SCSISIGI));
664 break;
665 }
666 case MISSED_BUSFREE:
667 {
668 u_int lastphase;
669
670 lastphase = ahc_inb(ahc, LASTPHASE);
671 printf("%s:%c:%d: Missed busfree. "
672 "Lastphase = 0x%x, Curphase = 0x%x\n",
673 ahc_name(ahc), devinfo.channel, devinfo.target,
674 lastphase, ahc_inb(ahc, SCSISIGI));
675 ahc_restart(ahc);
676 return;
677 }
678 case HOST_MSG_LOOP:
679 {
680 /*
681 * The sequencer has encountered a message phase
682 * that requires host assistance for completion.
683 * While handling the message phase(s), we will be
684 * notified by the sequencer after each byte is
685 * transferred so we can track bus phase changes.
686 *
687 * If this is the first time we've seen a HOST_MSG_LOOP
688 * interrupt, initialize the state of the host message
689 * loop.
690 */
691 if (ahc->msg_type == MSG_TYPE_NONE) {
692 struct scb *scb;
693 u_int scb_index;
694 u_int bus_phase;
695
696 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
697 if (bus_phase != P_MESGIN
698 && bus_phase != P_MESGOUT) {
699 printf("ahc_intr: HOST_MSG_LOOP bad "
700 "phase 0x%x\n",
701 bus_phase);
702 /*
703 * Probably transitioned to bus free before
704 * we got here. Just punt the message.
705 */
706 ahc_clear_intstat(ahc);
707 ahc_restart(ahc);
708 return;
709 }
710
711 scb_index = ahc_inb(ahc, SCB_TAG);
712 scb = ahc_lookup_scb(ahc, scb_index);
713 if (devinfo.role == ROLE_INITIATOR) {
714 if (scb == NULL)
715 panic("HOST_MSG_LOOP with "
716 "invalid SCB %x\n", scb_index);
717
718 if (bus_phase == P_MESGOUT)
719 ahc_setup_initiator_msgout(ahc,
720 &devinfo,
721 scb);
722 else {
723 ahc->msg_type =
724 MSG_TYPE_INITIATOR_MSGIN;
725 ahc->msgin_index = 0;
726 }
727 }
728 #ifdef AHC_TARGET_MODE
729 else {
730 if (bus_phase == P_MESGOUT) {
731 ahc->msg_type =
732 MSG_TYPE_TARGET_MSGOUT;
733 ahc->msgin_index = 0;
734 }
735 else
736 ahc_setup_target_msgin(ahc,
737 &devinfo,
738 scb);
739 }
740 #endif
741 }
742
743 ahc_handle_message_phase(ahc);
744 break;
745 }
746 case PERR_DETECTED:
747 {
748 /*
749 * If we've cleared the parity error interrupt
750 * but the sequencer still believes that SCSIPERR
751 * is true, it must be that the parity error is
752 * for the currently presented byte on the bus,
753 * and we are not in a phase (data-in) where we will
754 * eventually ack this byte. Ack the byte and
755 * throw it away in the hope that the target will
756 * take us to message out to deliver the appropriate
757 * error message.
758 */
759 if ((intstat & SCSIINT) == 0
760 && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
761 if ((ahc->features & AHC_DT) == 0) {
762 u_int curphase;
763
764 /*
765 * The hardware will only let you ack bytes
766 * if the expected phase in SCSISIGO matches
767 * the current phase. Make sure this is
768 * currently the case.
769 */
770 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
771 ahc_outb(ahc, LASTPHASE, curphase);
772 ahc_outb(ahc, SCSISIGO, curphase);
773 }
774 if ((ahc_inb(ahc, SCSISIGI) & (CDI|MSGI)) == 0) {
775 int wait;
776
777 /*
778 * In a data phase. Faster to bitbucket
779 * the data than to individually ack each
780 * byte. This is also the only strategy
781 * that will work with AUTOACK enabled.
782 */
783 ahc_outb(ahc, SXFRCTL1,
784 ahc_inb(ahc, SXFRCTL1) | BITBUCKET);
785 wait = 5000;
786 while (--wait != 0) {
787 if ((ahc_inb(ahc, SCSISIGI)
788 & (CDI|MSGI)) != 0)
789 break;
790 aic_delay(100);
791 }
792 ahc_outb(ahc, SXFRCTL1,
793 ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
794 if (wait == 0) {
795 struct scb *scb;
796 u_int scb_index;
797
798 ahc_print_devinfo(ahc, &devinfo);
799 printf("Unable to clear parity error. "
800 "Resetting bus.\n");
801 scb_index = ahc_inb(ahc, SCB_TAG);
802 scb = ahc_lookup_scb(ahc, scb_index);
803 if (scb != NULL)
804 aic_set_transaction_status(scb,
805 CAM_UNCOR_PARITY);
806 ahc_reset_channel(ahc, devinfo.channel,
807 /*init reset*/TRUE);
808 }
809 } else {
810 ahc_inb(ahc, SCSIDATL);
811 }
812 }
813 break;
814 }
815 case DATA_OVERRUN:
816 {
817 /*
818 * When the sequencer detects an overrun, it
819 * places the controller in "BITBUCKET" mode
820 * and allows the target to complete its transfer.
821 * Unfortunately, none of the counters get updated
822 * when the controller is in this mode, so we have
823 * no way of knowing how large the overrun was.
824 */
825 u_int scbindex = ahc_inb(ahc, SCB_TAG);
826 u_int lastphase = ahc_inb(ahc, LASTPHASE);
827 u_int i;
828
829 scb = ahc_lookup_scb(ahc, scbindex);
830 for (i = 0; i < num_phases; i++) {
831 if (lastphase == ahc_phase_table[i].phase)
832 break;
833 }
834 ahc_print_path(ahc, scb);
835 printf("data overrun detected %s."
836 " Tag == 0x%x.\n",
837 ahc_phase_table[i].phasemsg,
838 scb->hscb->tag);
839 ahc_print_path(ahc, scb);
840 printf("%s seen Data Phase. Length = %ld. NumSGs = %d.\n",
841 ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
842 aic_get_transfer_length(scb), scb->sg_count);
843 if (scb->sg_count > 0) {
844 for (i = 0; i < scb->sg_count; i++) {
845 printf("sg[%d] - Addr 0x%x%x : Length %d\n",
846 i,
847 (aic_le32toh(scb->sg_list[i].len) >> 24
848 & SG_HIGH_ADDR_BITS),
849 aic_le32toh(scb->sg_list[i].addr),
850 aic_le32toh(scb->sg_list[i].len)
851 & AHC_SG_LEN_MASK);
852 }
853 }
854 /*
855 * Set this and it will take effect when the
856 * target does a command complete.
857 */
858 ahc_freeze_devq(ahc, scb);
859 if ((scb->flags & SCB_SENSE) == 0) {
860 aic_set_transaction_status(scb, CAM_DATA_RUN_ERR);
861 } else {
862 scb->flags &= ~SCB_SENSE;
863 aic_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
864 }
865 aic_freeze_scb(scb);
866
867 if ((ahc->features & AHC_ULTRA2) != 0) {
868 /*
869 * Clear the channel in case we return
870 * to data phase later.
871 */
872 ahc_outb(ahc, SXFRCTL0,
873 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
874 ahc_outb(ahc, SXFRCTL0,
875 ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
876 }
877 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
878 u_int dscommand1;
879
880 /* Ensure HHADDR is 0 for future DMA operations. */
881 dscommand1 = ahc_inb(ahc, DSCOMMAND1);
882 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
883 ahc_outb(ahc, HADDR, 0);
884 ahc_outb(ahc, DSCOMMAND1, dscommand1);
885 }
886 break;
887 }
888 case MKMSG_FAILED:
889 {
890 u_int scbindex;
891
892 printf("%s:%c:%d:%d: Attempt to issue message failed\n",
893 ahc_name(ahc), devinfo.channel, devinfo.target,
894 devinfo.lun);
895 scbindex = ahc_inb(ahc, SCB_TAG);
896 scb = ahc_lookup_scb(ahc, scbindex);
897 if (scb != NULL
898 && (scb->flags & SCB_RECOVERY_SCB) != 0)
899 /*
900 * Ensure that we didn't put a second instance of this
901 * SCB into the QINFIFO.
902 */
903 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
904 SCB_GET_CHANNEL(ahc, scb),
905 SCB_GET_LUN(scb), scb->hscb->tag,
906 ROLE_INITIATOR, /*status*/0,
907 SEARCH_REMOVE);
908 break;
909 }
910 case NO_FREE_SCB:
911 {
912 printf("%s: No free or disconnected SCBs\n", ahc_name(ahc));
913 ahc_dump_card_state(ahc);
914 panic("for safety");
915 break;
916 }
917 case SCB_MISMATCH:
918 {
919 u_int scbptr;
920
921 scbptr = ahc_inb(ahc, SCBPTR);
922 printf("Bogus TAG after DMA. SCBPTR %d, tag %d, our tag %d\n",
923 scbptr, ahc_inb(ahc, ARG_1),
924 ahc->scb_data->hscbs[scbptr].tag);
925 ahc_dump_card_state(ahc);
926 panic("for safety");
927 break;
928 }
929 case OUT_OF_RANGE:
930 {
931 printf("%s: BTT calculation out of range\n", ahc_name(ahc));
932 printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
933 "ARG_1 == 0x%x ACCUM = 0x%x\n",
934 ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
935 ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
936 printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
937 "SINDEX == 0x%x\n, A == 0x%x\n",
938 ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
939 ahc_index_busy_tcl(ahc,
940 BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
941 ahc_inb(ahc, SAVED_LUN))),
942 ahc_inb(ahc, SINDEX),
943 ahc_inb(ahc, ACCUM));
944 printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
945 "SCB_TAG == 0x%x, SCB_CONTROL == 0x%x\n",
946 ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
947 ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
948 ahc_inb(ahc, SCB_CONTROL));
949 printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%x\n",
950 ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
951 ahc_dump_card_state(ahc);
952 panic("for safety");
953 break;
954 }
955 default:
956 printf("ahc_intr: seqint, "
957 "intstat == 0x%x, scsisigi = 0x%x\n",
958 intstat, ahc_inb(ahc, SCSISIGI));
959 break;
960 }
961 unpause:
962 /*
963 * The sequencer is paused immediately on
964 * a SEQINT, so we should restart it when
965 * we're done.
966 */
967 ahc_unpause(ahc);
968 }
969
970 void
ahc_handle_scsiint(struct ahc_softc * ahc,u_int intstat)971 ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
972 {
973 u_int scb_index;
974 u_int status0;
975 u_int status;
976 struct scb *scb;
977 char cur_channel;
978 char intr_channel;
979
980 if ((ahc->features & AHC_TWIN) != 0
981 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
982 cur_channel = 'B';
983 else
984 cur_channel = 'A';
985 intr_channel = cur_channel;
986
987 if ((ahc->features & AHC_ULTRA2) != 0)
988 status0 = ahc_inb(ahc, SSTAT0) & IOERR;
989 else
990 status0 = 0;
991 status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
992 if (status == 0 && status0 == 0) {
993 if ((ahc->features & AHC_TWIN) != 0) {
994 /* Try the other channel */
995 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
996 status = ahc_inb(ahc, SSTAT1)
997 & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
998 intr_channel = (cur_channel == 'A') ? 'B' : 'A';
999 }
1000 if (status == 0) {
1001 printf("%s: Spurious SCSI interrupt\n", ahc_name(ahc));
1002 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1003 ahc_unpause(ahc);
1004 return;
1005 }
1006 }
1007
1008 /* Make sure the sequencer is in a safe location. */
1009 ahc_clear_critical_section(ahc);
1010
1011 scb_index = ahc_inb(ahc, SCB_TAG);
1012 scb = ahc_lookup_scb(ahc, scb_index);
1013 if (scb != NULL
1014 && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) != 0)
1015 scb = NULL;
1016
1017 if ((ahc->features & AHC_ULTRA2) != 0
1018 && (status0 & IOERR) != 0) {
1019 int now_lvd;
1020
1021 now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40;
1022 printf("%s: Transceiver State Has Changed to %s mode\n",
1023 ahc_name(ahc), now_lvd ? "LVD" : "SE");
1024 ahc_outb(ahc, CLRSINT0, CLRIOERR);
1025 /*
1026 * When transitioning to SE mode, the reset line
1027 * glitches, triggering an arbitration bug in some
1028 * Ultra2 controllers. This bug is cleared when we
1029 * assert the reset line. Since a reset glitch has
1030 * already occurred with this transition and a
1031 * transceiver state change is handled just like
1032 * a bus reset anyway, asserting the reset line
1033 * ourselves is safe.
1034 */
1035 ahc_reset_channel(ahc, intr_channel,
1036 /*Initiate Reset*/now_lvd == 0);
1037 } else if ((status & SCSIRSTI) != 0) {
1038 printf("%s: Someone reset channel %c\n",
1039 ahc_name(ahc), intr_channel);
1040 if (intr_channel != cur_channel)
1041 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
1042 ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE);
1043 } else if ((status & SCSIPERR) != 0) {
1044 /*
1045 * Determine the bus phase and queue an appropriate message.
1046 * SCSIPERR is latched true as soon as a parity error
1047 * occurs. If the sequencer acked the transfer that
1048 * caused the parity error and the currently presented
1049 * transfer on the bus has correct parity, SCSIPERR will
1050 * be cleared by CLRSCSIPERR. Use this to determine if
1051 * we should look at the last phase the sequencer recorded,
1052 * or the current phase presented on the bus.
1053 */
1054 struct ahc_devinfo devinfo;
1055 u_int mesg_out;
1056 u_int curphase;
1057 u_int errorphase;
1058 u_int lastphase;
1059 u_int scsirate;
1060 u_int i;
1061 u_int sstat2;
1062 int silent;
1063
1064 lastphase = ahc_inb(ahc, LASTPHASE);
1065 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
1066 sstat2 = ahc_inb(ahc, SSTAT2);
1067 ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
1068 /*
1069 * For all phases save DATA, the sequencer won't
1070 * automatically ack a byte that has a parity error
1071 * in it. So the only way that the current phase
1072 * could be 'data-in' is if the parity error is for
1073 * an already acked byte in the data phase. During
1074 * synchronous data-in transfers, we may actually
1075 * ack bytes before latching the current phase in
1076 * LASTPHASE, leading to the discrepancy between
1077 * curphase and lastphase.
1078 */
1079 if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
1080 || curphase == P_DATAIN || curphase == P_DATAIN_DT)
1081 errorphase = curphase;
1082 else
1083 errorphase = lastphase;
1084
1085 for (i = 0; i < num_phases; i++) {
1086 if (errorphase == ahc_phase_table[i].phase)
1087 break;
1088 }
1089 mesg_out = ahc_phase_table[i].mesg_out;
1090 silent = FALSE;
1091 if (scb != NULL) {
1092 if (SCB_IS_SILENT(scb))
1093 silent = TRUE;
1094 else
1095 ahc_print_path(ahc, scb);
1096 scb->flags |= SCB_TRANSMISSION_ERROR;
1097 } else
1098 printf("%s:%c:%d: ", ahc_name(ahc), intr_channel,
1099 SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)));
1100 scsirate = ahc_inb(ahc, SCSIRATE);
1101 if (silent == FALSE) {
1102 printf("parity error detected %s. "
1103 "SEQADDR(0x%x) SCSIRATE(0x%x)\n",
1104 ahc_phase_table[i].phasemsg,
1105 ahc_inw(ahc, SEQADDR0),
1106 scsirate);
1107 if ((ahc->features & AHC_DT) != 0) {
1108 if ((sstat2 & CRCVALERR) != 0)
1109 printf("\tCRC Value Mismatch\n");
1110 if ((sstat2 & CRCENDERR) != 0)
1111 printf("\tNo terminal CRC packet "
1112 "received\n");
1113 if ((sstat2 & CRCREQERR) != 0)
1114 printf("\tIllegal CRC packet "
1115 "request\n");
1116 if ((sstat2 & DUAL_EDGE_ERR) != 0)
1117 printf("\tUnexpected %sDT Data Phase\n",
1118 (scsirate & SINGLE_EDGE)
1119 ? "" : "non-");
1120 }
1121 }
1122
1123 if ((ahc->features & AHC_DT) != 0
1124 && (sstat2 & DUAL_EDGE_ERR) != 0) {
1125 /*
1126 * This error applies regardless of
1127 * data direction, so ignore the value
1128 * in the phase table.
1129 */
1130 mesg_out = MSG_INITIATOR_DET_ERR;
1131 }
1132
1133 /*
1134 * We've set the hardware to assert ATN if we
1135 * get a parity error on "in" phases, so all we
1136 * need to do is stuff the message buffer with
1137 * the appropriate message. "In" phases have set
1138 * mesg_out to something other than MSG_NOP.
1139 */
1140 if (mesg_out != MSG_NOOP) {
1141 if (ahc->msg_type != MSG_TYPE_NONE)
1142 ahc->send_msg_perror = TRUE;
1143 else
1144 ahc_outb(ahc, MSG_OUT, mesg_out);
1145 }
1146 /*
1147 * Force a renegotiation with this target just in
1148 * case we are out of sync for some external reason
1149 * unknown (or unreported) by the target.
1150 */
1151 ahc_fetch_devinfo(ahc, &devinfo);
1152 ahc_force_renegotiation(ahc, &devinfo);
1153
1154 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1155 ahc_unpause(ahc);
1156 } else if ((status & SELTO) != 0) {
1157 u_int scbptr;
1158
1159 /* Stop the selection */
1160 ahc_outb(ahc, SCSISEQ, 0);
1161
1162 /* No more pending messages */
1163 ahc_clear_msg_state(ahc);
1164
1165 /* Clear interrupt state */
1166 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
1167 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
1168
1169 /*
1170 * Although the driver does not care about the
1171 * 'Selection in Progress' status bit, the busy
1172 * LED does. SELINGO is only cleared by a successful
1173 * selection, so we must manually clear it to insure
1174 * the LED turns off just incase no future successful
1175 * selections occur (e.g. no devices on the bus).
1176 */
1177 ahc_outb(ahc, CLRSINT0, CLRSELINGO);
1178
1179 scbptr = ahc_inb(ahc, WAITING_SCBH);
1180 ahc_outb(ahc, SCBPTR, scbptr);
1181 scb_index = ahc_inb(ahc, SCB_TAG);
1182
1183 scb = ahc_lookup_scb(ahc, scb_index);
1184 if (scb == NULL) {
1185 printf("%s: ahc_intr - referenced scb not "
1186 "valid during SELTO scb(%d, %d)\n",
1187 ahc_name(ahc), scbptr, scb_index);
1188 ahc_dump_card_state(ahc);
1189 } else {
1190 struct ahc_devinfo devinfo;
1191 #ifdef AHC_DEBUG
1192 if ((ahc_debug & AHC_SHOW_SELTO) != 0) {
1193 ahc_print_path(ahc, scb);
1194 printf("Saw Selection Timeout for SCB 0x%x\n",
1195 scb_index);
1196 }
1197 #endif
1198 ahc_scb_devinfo(ahc, &devinfo, scb);
1199 aic_set_transaction_status(scb, CAM_SEL_TIMEOUT);
1200 ahc_freeze_devq(ahc, scb);
1201
1202 /*
1203 * Cancel any pending transactions on the device
1204 * now that it seems to be missing. This will
1205 * also revert us to async/narrow transfers until
1206 * we can renegotiate with the device.
1207 */
1208 ahc_handle_devreset(ahc, &devinfo,
1209 CAM_SEL_TIMEOUT,
1210 "Selection Timeout",
1211 /*verbose_level*/1);
1212 }
1213 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1214 ahc_restart(ahc);
1215 } else if ((status & BUSFREE) != 0
1216 && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
1217 struct ahc_devinfo devinfo;
1218 u_int lastphase;
1219 u_int saved_scsiid;
1220 u_int saved_lun;
1221 u_int target;
1222 u_int initiator_role_id;
1223 char channel;
1224 int printerror;
1225
1226 /*
1227 * Clear our selection hardware as soon as possible.
1228 * We may have an entry in the waiting Q for this target,
1229 * that is affected by this busfree and we don't want to
1230 * go about selecting the target while we handle the event.
1231 */
1232 ahc_outb(ahc, SCSISEQ,
1233 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
1234
1235 /*
1236 * Disable busfree interrupts and clear the busfree
1237 * interrupt status. We do this here so that several
1238 * bus transactions occur prior to clearing the SCSIINT
1239 * latch. It can take a bit for the clearing to take effect.
1240 */
1241 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
1242 ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
1243
1244 /*
1245 * Look at what phase we were last in.
1246 * If its message out, chances are pretty good
1247 * that the busfree was in response to one of
1248 * our abort requests.
1249 */
1250 lastphase = ahc_inb(ahc, LASTPHASE);
1251 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
1252 saved_lun = ahc_inb(ahc, SAVED_LUN);
1253 target = SCSIID_TARGET(ahc, saved_scsiid);
1254 initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
1255 channel = SCSIID_CHANNEL(ahc, saved_scsiid);
1256 ahc_compile_devinfo(&devinfo, initiator_role_id,
1257 target, saved_lun, channel, ROLE_INITIATOR);
1258 printerror = 1;
1259
1260 if (lastphase == P_MESGOUT) {
1261 u_int tag;
1262
1263 tag = SCB_LIST_NULL;
1264 if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE)
1265 || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) {
1266 if (ahc->msgout_buf[ahc->msgout_index - 1]
1267 == MSG_ABORT_TAG)
1268 tag = scb->hscb->tag;
1269 ahc_print_path(ahc, scb);
1270 printf("SCB %d - Abort%s Completed.\n",
1271 scb->hscb->tag, tag == SCB_LIST_NULL ?
1272 "" : " Tag");
1273 ahc_abort_scbs(ahc, target, channel,
1274 saved_lun, tag,
1275 ROLE_INITIATOR,
1276 CAM_REQ_ABORTED);
1277 printerror = 0;
1278 } else if (ahc_sent_msg(ahc, AHCMSG_1B,
1279 MSG_BUS_DEV_RESET, TRUE)) {
1280 #ifdef __FreeBSD__
1281 /*
1282 * Don't mark the user's request for this BDR
1283 * as completing with CAM_BDR_SENT. CAM3
1284 * specifies CAM_REQ_CMP.
1285 */
1286 if (scb != NULL
1287 && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
1288 && ahc_match_scb(ahc, scb, target, channel,
1289 CAM_LUN_WILDCARD,
1290 SCB_LIST_NULL,
1291 ROLE_INITIATOR)) {
1292 aic_set_transaction_status(scb, CAM_REQ_CMP);
1293 }
1294 #endif
1295 ahc_compile_devinfo(&devinfo,
1296 initiator_role_id,
1297 target,
1298 CAM_LUN_WILDCARD,
1299 channel,
1300 ROLE_INITIATOR);
1301 ahc_handle_devreset(ahc, &devinfo,
1302 CAM_BDR_SENT,
1303 "Bus Device Reset",
1304 /*verbose_level*/0);
1305 printerror = 0;
1306 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1307 MSG_EXT_PPR, FALSE)) {
1308 struct ahc_initiator_tinfo *tinfo;
1309 struct ahc_tmode_tstate *tstate;
1310
1311 /*
1312 * PPR Rejected. Try non-ppr negotiation
1313 * and retry command.
1314 */
1315 tinfo = ahc_fetch_transinfo(ahc,
1316 devinfo.channel,
1317 devinfo.our_scsiid,
1318 devinfo.target,
1319 &tstate);
1320 tinfo->curr.transport_version = 2;
1321 tinfo->goal.transport_version = 2;
1322 tinfo->goal.ppr_options = 0;
1323 ahc_qinfifo_requeue_tail(ahc, scb);
1324 printerror = 0;
1325 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1326 MSG_EXT_WDTR, FALSE)) {
1327 /*
1328 * Negotiation Rejected. Go-narrow and
1329 * retry command.
1330 */
1331 ahc_set_width(ahc, &devinfo,
1332 MSG_EXT_WDTR_BUS_8_BIT,
1333 AHC_TRANS_CUR|AHC_TRANS_GOAL,
1334 /*paused*/TRUE);
1335 ahc_qinfifo_requeue_tail(ahc, scb);
1336 printerror = 0;
1337 } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
1338 MSG_EXT_SDTR, FALSE)) {
1339 /*
1340 * Negotiation Rejected. Go-async and
1341 * retry command.
1342 */
1343 ahc_set_syncrate(ahc, &devinfo,
1344 /*syncrate*/NULL,
1345 /*period*/0, /*offset*/0,
1346 /*ppr_options*/0,
1347 AHC_TRANS_CUR|AHC_TRANS_GOAL,
1348 /*paused*/TRUE);
1349 ahc_qinfifo_requeue_tail(ahc, scb);
1350 printerror = 0;
1351 }
1352 }
1353 if (printerror != 0) {
1354 u_int i;
1355
1356 if (scb != NULL) {
1357 u_int tag;
1358
1359 if ((scb->hscb->control & TAG_ENB) != 0)
1360 tag = scb->hscb->tag;
1361 else
1362 tag = SCB_LIST_NULL;
1363 ahc_print_path(ahc, scb);
1364 ahc_abort_scbs(ahc, target, channel,
1365 SCB_GET_LUN(scb), tag,
1366 ROLE_INITIATOR,
1367 CAM_UNEXP_BUSFREE);
1368 } else {
1369 /*
1370 * We had not fully identified this connection,
1371 * so we cannot abort anything.
1372 */
1373 printf("%s: ", ahc_name(ahc));
1374 }
1375 for (i = 0; i < num_phases; i++) {
1376 if (lastphase == ahc_phase_table[i].phase)
1377 break;
1378 }
1379 if (lastphase != P_BUSFREE) {
1380 /*
1381 * Renegotiate with this device at the
1382 * next opportunity just in case this busfree
1383 * is due to a negotiation mismatch with the
1384 * device.
1385 */
1386 ahc_force_renegotiation(ahc, &devinfo);
1387 }
1388 printf("Unexpected busfree %s\n"
1389 "SEQADDR == 0x%x\n",
1390 ahc_phase_table[i].phasemsg,
1391 ahc_inb(ahc, SEQADDR0)
1392 | (ahc_inb(ahc, SEQADDR1) << 8));
1393 }
1394 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1395 ahc_restart(ahc);
1396 } else {
1397 printf("%s: Missing case in ahc_handle_scsiint. status = %x\n",
1398 ahc_name(ahc), status);
1399 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1400 }
1401 }
1402
1403 /*
1404 * Force renegotiation to occur the next time we initiate
1405 * a command to the current device.
1406 */
1407 static void
ahc_force_renegotiation(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)1408 ahc_force_renegotiation(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1409 {
1410 struct ahc_initiator_tinfo *targ_info;
1411 struct ahc_tmode_tstate *tstate;
1412
1413 targ_info = ahc_fetch_transinfo(ahc,
1414 devinfo->channel,
1415 devinfo->our_scsiid,
1416 devinfo->target,
1417 &tstate);
1418 ahc_update_neg_request(ahc, devinfo, tstate,
1419 targ_info, AHC_NEG_IF_NON_ASYNC);
1420 }
1421
1422 #define AHC_MAX_STEPS 2000
1423 void
ahc_clear_critical_section(struct ahc_softc * ahc)1424 ahc_clear_critical_section(struct ahc_softc *ahc)
1425 {
1426 int stepping;
1427 int steps;
1428 u_int simode0;
1429 u_int simode1;
1430
1431 if (ahc->num_critical_sections == 0)
1432 return;
1433
1434 stepping = FALSE;
1435 steps = 0;
1436 simode0 = 0;
1437 simode1 = 0;
1438 for (;;) {
1439 struct cs *cs;
1440 u_int seqaddr;
1441 u_int i;
1442
1443 seqaddr = ahc_inb(ahc, SEQADDR0)
1444 | (ahc_inb(ahc, SEQADDR1) << 8);
1445
1446 /*
1447 * Seqaddr represents the next instruction to execute,
1448 * so we are really executing the instruction just
1449 * before it.
1450 */
1451 cs = ahc->critical_sections;
1452 for (i = 0; i < ahc->num_critical_sections; i++, cs++) {
1453
1454 if (cs->begin < seqaddr && cs->end >= seqaddr)
1455 break;
1456 }
1457
1458 if (i == ahc->num_critical_sections)
1459 break;
1460
1461 if (steps > AHC_MAX_STEPS) {
1462 printf("%s: Infinite loop in critical section\n",
1463 ahc_name(ahc));
1464 ahc_dump_card_state(ahc);
1465 panic("critical section loop");
1466 }
1467
1468 steps++;
1469 if (stepping == FALSE) {
1470 /*
1471 * Disable all interrupt sources so that the
1472 * sequencer will not be stuck by a pausing
1473 * interrupt condition while we attempt to
1474 * leave a critical section.
1475 */
1476 simode0 = ahc_inb(ahc, SIMODE0);
1477 ahc_outb(ahc, SIMODE0, 0);
1478 simode1 = ahc_inb(ahc, SIMODE1);
1479 if ((ahc->features & AHC_DT) != 0)
1480 /*
1481 * On DT class controllers, we
1482 * use the enhanced busfree logic.
1483 * Unfortunately we cannot re-enable
1484 * busfree detection within the
1485 * current connection, so we must
1486 * leave it on while single stepping.
1487 */
1488 ahc_outb(ahc, SIMODE1, simode1 & ENBUSFREE);
1489 else
1490 ahc_outb(ahc, SIMODE1, 0);
1491 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1492 ahc_outb(ahc, SEQCTL, ahc->seqctl | STEP);
1493 stepping = TRUE;
1494 }
1495 if ((ahc->features & AHC_DT) != 0) {
1496 ahc_outb(ahc, CLRSINT1, CLRBUSFREE);
1497 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1498 }
1499 ahc_outb(ahc, HCNTRL, ahc->unpause);
1500 while (!ahc_is_paused(ahc))
1501 aic_delay(200);
1502 }
1503 if (stepping) {
1504 ahc_outb(ahc, SIMODE0, simode0);
1505 ahc_outb(ahc, SIMODE1, simode1);
1506 ahc_outb(ahc, SEQCTL, ahc->seqctl);
1507 }
1508 }
1509
1510 /*
1511 * Clear any pending interrupt status.
1512 */
1513 void
ahc_clear_intstat(struct ahc_softc * ahc)1514 ahc_clear_intstat(struct ahc_softc *ahc)
1515 {
1516 /* Clear any interrupt conditions this may have caused */
1517 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
1518 |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
1519 CLRREQINIT);
1520 ahc_flush_device_writes(ahc);
1521 ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
1522 ahc_flush_device_writes(ahc);
1523 ahc_outb(ahc, CLRINT, CLRSCSIINT);
1524 ahc_flush_device_writes(ahc);
1525 }
1526
1527 /**************************** Debugging Routines ******************************/
1528 #ifdef AHC_DEBUG
1529 uint32_t ahc_debug = AHC_DEBUG_OPTS;
1530 #endif
1531
1532 void
ahc_print_scb(struct scb * scb)1533 ahc_print_scb(struct scb *scb)
1534 {
1535 int i;
1536
1537 struct hardware_scb *hscb = scb->hscb;
1538
1539 printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%d\n",
1540 (void *)scb,
1541 hscb->control,
1542 hscb->scsiid,
1543 hscb->lun,
1544 hscb->cdb_len);
1545 printf("Shared Data: ");
1546 for (i = 0; i < sizeof(hscb->shared_data.cdb); i++)
1547 printf("%#02x", hscb->shared_data.cdb[i]);
1548 printf(" dataptr:%#x datacnt:%#x sgptr:%#x tag:%#x\n",
1549 aic_le32toh(hscb->dataptr),
1550 aic_le32toh(hscb->datacnt),
1551 aic_le32toh(hscb->sgptr),
1552 hscb->tag);
1553 if (scb->sg_count > 0) {
1554 for (i = 0; i < scb->sg_count; i++) {
1555 printf("sg[%d] - Addr 0x%x%x : Length %d\n",
1556 i,
1557 (aic_le32toh(scb->sg_list[i].len) >> 24
1558 & SG_HIGH_ADDR_BITS),
1559 aic_le32toh(scb->sg_list[i].addr),
1560 aic_le32toh(scb->sg_list[i].len));
1561 }
1562 }
1563 }
1564
1565 /************************* Transfer Negotiation *******************************/
1566 /*
1567 * Allocate per target mode instance (ID we respond to as a target)
1568 * transfer negotiation data structures.
1569 */
1570 static struct ahc_tmode_tstate *
ahc_alloc_tstate(struct ahc_softc * ahc,u_int scsi_id,char channel)1571 ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
1572 {
1573 struct ahc_tmode_tstate *master_tstate;
1574 struct ahc_tmode_tstate *tstate;
1575 int i;
1576
1577 master_tstate = ahc->enabled_targets[ahc->our_id];
1578 if (channel == 'B') {
1579 scsi_id += 8;
1580 master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
1581 }
1582 if (ahc->enabled_targets[scsi_id] != NULL
1583 && ahc->enabled_targets[scsi_id] != master_tstate)
1584 panic("%s: ahc_alloc_tstate - Target already allocated",
1585 ahc_name(ahc));
1586 tstate = (struct ahc_tmode_tstate*)malloc(sizeof(*tstate),
1587 M_DEVBUF, M_NOWAIT);
1588 if (tstate == NULL)
1589 return (NULL);
1590
1591 /*
1592 * If we have allocated a master tstate, copy user settings from
1593 * the master tstate (taken from SRAM or the EEPROM) for this
1594 * channel, but reset our current and goal settings to async/narrow
1595 * until an initiator talks to us.
1596 */
1597 if (master_tstate != NULL) {
1598 memcpy(tstate, master_tstate, sizeof(*tstate));
1599 memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
1600 tstate->ultraenb = 0;
1601 for (i = 0; i < AHC_NUM_TARGETS; i++) {
1602 memset(&tstate->transinfo[i].curr, 0,
1603 sizeof(tstate->transinfo[i].curr));
1604 memset(&tstate->transinfo[i].goal, 0,
1605 sizeof(tstate->transinfo[i].goal));
1606 }
1607 } else
1608 memset(tstate, 0, sizeof(*tstate));
1609 ahc->enabled_targets[scsi_id] = tstate;
1610 return (tstate);
1611 }
1612
1613 #ifdef AHC_TARGET_MODE
1614 /*
1615 * Free per target mode instance (ID we respond to as a target)
1616 * transfer negotiation data structures.
1617 */
1618 static void
ahc_free_tstate(struct ahc_softc * ahc,u_int scsi_id,char channel,int force)1619 ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
1620 {
1621 struct ahc_tmode_tstate *tstate;
1622
1623 /*
1624 * Don't clean up our "master" tstate.
1625 * It has our default user settings.
1626 */
1627 if (((channel == 'B' && scsi_id == ahc->our_id_b)
1628 || (channel == 'A' && scsi_id == ahc->our_id))
1629 && force == FALSE)
1630 return;
1631
1632 if (channel == 'B')
1633 scsi_id += 8;
1634 tstate = ahc->enabled_targets[scsi_id];
1635 if (tstate != NULL)
1636 free(tstate, M_DEVBUF);
1637 ahc->enabled_targets[scsi_id] = NULL;
1638 }
1639 #endif
1640
1641 /*
1642 * Called when we have an active connection to a target on the bus,
1643 * this function finds the nearest syncrate to the input period limited
1644 * by the capabilities of the bus connectivity of and sync settings for
1645 * the target.
1646 */
1647 struct ahc_syncrate *
ahc_devlimited_syncrate(struct ahc_softc * ahc,struct ahc_initiator_tinfo * tinfo,u_int * period,u_int * ppr_options,role_t role)1648 ahc_devlimited_syncrate(struct ahc_softc *ahc,
1649 struct ahc_initiator_tinfo *tinfo,
1650 u_int *period, u_int *ppr_options, role_t role)
1651 {
1652 struct ahc_transinfo *transinfo;
1653 u_int maxsync;
1654
1655 if ((ahc->features & AHC_ULTRA2) != 0) {
1656 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
1657 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
1658 maxsync = AHC_SYNCRATE_DT;
1659 } else {
1660 maxsync = AHC_SYNCRATE_ULTRA;
1661 /* Can't do DT on an SE bus */
1662 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1663 }
1664 } else if ((ahc->features & AHC_ULTRA) != 0) {
1665 maxsync = AHC_SYNCRATE_ULTRA;
1666 } else {
1667 maxsync = AHC_SYNCRATE_FAST;
1668 }
1669 /*
1670 * Never allow a value higher than our current goal
1671 * period otherwise we may allow a target initiated
1672 * negotiation to go above the limit as set by the
1673 * user. In the case of an initiator initiated
1674 * sync negotiation, we limit based on the user
1675 * setting. This allows the system to still accept
1676 * incoming negotiations even if target initiated
1677 * negotiation is not performed.
1678 */
1679 if (role == ROLE_TARGET)
1680 transinfo = &tinfo->user;
1681 else
1682 transinfo = &tinfo->goal;
1683 *ppr_options &= transinfo->ppr_options;
1684 if (transinfo->width == MSG_EXT_WDTR_BUS_8_BIT) {
1685 maxsync = MAX(maxsync, AHC_SYNCRATE_ULTRA2);
1686 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1687 }
1688 if (transinfo->period == 0) {
1689 *period = 0;
1690 *ppr_options = 0;
1691 return (NULL);
1692 }
1693 *period = MAX(*period, transinfo->period);
1694 return (ahc_find_syncrate(ahc, period, ppr_options, maxsync));
1695 }
1696
1697 /*
1698 * Look up the valid period to SCSIRATE conversion in our table.
1699 * Return the period and offset that should be sent to the target
1700 * if this was the beginning of an SDTR.
1701 */
1702 struct ahc_syncrate *
ahc_find_syncrate(struct ahc_softc * ahc,u_int * period,u_int * ppr_options,u_int maxsync)1703 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
1704 u_int *ppr_options, u_int maxsync)
1705 {
1706 struct ahc_syncrate *syncrate;
1707
1708 if ((ahc->features & AHC_DT) == 0)
1709 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1710
1711 /* Skip all DT only entries if DT is not available */
1712 if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
1713 && maxsync < AHC_SYNCRATE_ULTRA2)
1714 maxsync = AHC_SYNCRATE_ULTRA2;
1715
1716 for (syncrate = &ahc_syncrates[maxsync];
1717 syncrate->rate != NULL;
1718 syncrate++) {
1719 /*
1720 * The Ultra2 table doesn't go as low
1721 * as for the Fast/Ultra cards.
1722 */
1723 if ((ahc->features & AHC_ULTRA2) != 0
1724 && (syncrate->sxfr_u2 == 0))
1725 break;
1726
1727 if (*period <= syncrate->period) {
1728 /*
1729 * When responding to a target that requests
1730 * sync, the requested rate may fall between
1731 * two rates that we can output, but still be
1732 * a rate that we can receive. Because of this,
1733 * we want to respond to the target with
1734 * the same rate that it sent to us even
1735 * if the period we use to send data to it
1736 * is lower. Only lower the response period
1737 * if we must.
1738 */
1739 if (syncrate == &ahc_syncrates[maxsync])
1740 *period = syncrate->period;
1741
1742 /*
1743 * At some speeds, we only support
1744 * ST transfers.
1745 */
1746 if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
1747 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1748 break;
1749 }
1750 }
1751
1752 if ((*period == 0)
1753 || (syncrate->rate == NULL)
1754 || ((ahc->features & AHC_ULTRA2) != 0
1755 && (syncrate->sxfr_u2 == 0))) {
1756 /* Use asynchronous transfers. */
1757 *period = 0;
1758 syncrate = NULL;
1759 *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
1760 }
1761 return (syncrate);
1762 }
1763
1764 /*
1765 * Convert from an entry in our syncrate table to the SCSI equivalent
1766 * sync "period" factor.
1767 */
1768 u_int
ahc_find_period(struct ahc_softc * ahc,u_int scsirate,u_int maxsync)1769 ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
1770 {
1771 struct ahc_syncrate *syncrate;
1772
1773 if ((ahc->features & AHC_ULTRA2) != 0)
1774 scsirate &= SXFR_ULTRA2;
1775 else
1776 scsirate &= SXFR;
1777
1778 syncrate = &ahc_syncrates[maxsync];
1779 while (syncrate->rate != NULL) {
1780 if ((ahc->features & AHC_ULTRA2) != 0) {
1781 if (syncrate->sxfr_u2 == 0)
1782 break;
1783 else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
1784 return (syncrate->period);
1785 } else if (scsirate == (syncrate->sxfr & SXFR)) {
1786 return (syncrate->period);
1787 }
1788 syncrate++;
1789 }
1790 return (0); /* async */
1791 }
1792
1793 /*
1794 * Truncate the given synchronous offset to a value the
1795 * current adapter type and syncrate are capable of.
1796 */
1797 void
ahc_validate_offset(struct ahc_softc * ahc,struct ahc_initiator_tinfo * tinfo,struct ahc_syncrate * syncrate,u_int * offset,int wide,role_t role)1798 ahc_validate_offset(struct ahc_softc *ahc,
1799 struct ahc_initiator_tinfo *tinfo,
1800 struct ahc_syncrate *syncrate,
1801 u_int *offset, int wide, role_t role)
1802 {
1803 u_int maxoffset;
1804
1805 /* Limit offset to what we can do */
1806 if (syncrate == NULL) {
1807 maxoffset = 0;
1808 } else if ((ahc->features & AHC_ULTRA2) != 0) {
1809 maxoffset = MAX_OFFSET_ULTRA2;
1810 } else {
1811 if (wide)
1812 maxoffset = MAX_OFFSET_16BIT;
1813 else
1814 maxoffset = MAX_OFFSET_8BIT;
1815 }
1816 *offset = MIN(*offset, maxoffset);
1817 if (tinfo != NULL) {
1818 if (role == ROLE_TARGET)
1819 *offset = MIN(*offset, tinfo->user.offset);
1820 else
1821 *offset = MIN(*offset, tinfo->goal.offset);
1822 }
1823 }
1824
1825 /*
1826 * Truncate the given transfer width parameter to a value the
1827 * current adapter type is capable of.
1828 */
1829 void
ahc_validate_width(struct ahc_softc * ahc,struct ahc_initiator_tinfo * tinfo,u_int * bus_width,role_t role)1830 ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
1831 u_int *bus_width, role_t role)
1832 {
1833 switch (*bus_width) {
1834 default:
1835 if (ahc->features & AHC_WIDE) {
1836 /* Respond Wide */
1837 *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1838 break;
1839 }
1840 /* FALLTHROUGH */
1841 case MSG_EXT_WDTR_BUS_8_BIT:
1842 *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1843 break;
1844 }
1845 if (tinfo != NULL) {
1846 if (role == ROLE_TARGET)
1847 *bus_width = MIN(tinfo->user.width, *bus_width);
1848 else
1849 *bus_width = MIN(tinfo->goal.width, *bus_width);
1850 }
1851 }
1852
1853 /*
1854 * Update the bitmask of targets for which the controller should
1855 * negotiate with at the next convenient opportunity. This currently
1856 * means the next time we send the initial identify messages for
1857 * a new transaction.
1858 */
1859 int
ahc_update_neg_request(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,struct ahc_tmode_tstate * tstate,struct ahc_initiator_tinfo * tinfo,ahc_neg_type neg_type)1860 ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1861 struct ahc_tmode_tstate *tstate,
1862 struct ahc_initiator_tinfo *tinfo, ahc_neg_type neg_type)
1863 {
1864 u_int auto_negotiate_orig;
1865
1866 auto_negotiate_orig = tstate->auto_negotiate;
1867 if (neg_type == AHC_NEG_ALWAYS) {
1868 /*
1869 * Force our "current" settings to be
1870 * unknown so that unless a bus reset
1871 * occurs the need to renegotiate is
1872 * recorded persistently.
1873 */
1874 if ((ahc->features & AHC_WIDE) != 0)
1875 tinfo->curr.width = AHC_WIDTH_UNKNOWN;
1876 tinfo->curr.period = AHC_PERIOD_UNKNOWN;
1877 tinfo->curr.offset = AHC_OFFSET_UNKNOWN;
1878 }
1879 if (tinfo->curr.period != tinfo->goal.period
1880 || tinfo->curr.width != tinfo->goal.width
1881 || tinfo->curr.offset != tinfo->goal.offset
1882 || tinfo->curr.ppr_options != tinfo->goal.ppr_options
1883 || (neg_type == AHC_NEG_IF_NON_ASYNC
1884 && (tinfo->goal.offset != 0
1885 || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
1886 || tinfo->goal.ppr_options != 0)))
1887 tstate->auto_negotiate |= devinfo->target_mask;
1888 else
1889 tstate->auto_negotiate &= ~devinfo->target_mask;
1890
1891 return (auto_negotiate_orig != tstate->auto_negotiate);
1892 }
1893
1894 /*
1895 * Update the user/goal/curr tables of synchronous negotiation
1896 * parameters as well as, in the case of a current or active update,
1897 * any data structures on the host controller. In the case of an
1898 * active update, the specified target is currently talking to us on
1899 * the bus, so the transfer parameter update must take effect
1900 * immediately.
1901 */
1902 void
ahc_set_syncrate(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,struct ahc_syncrate * syncrate,u_int period,u_int offset,u_int ppr_options,u_int type,int paused)1903 ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1904 struct ahc_syncrate *syncrate, u_int period,
1905 u_int offset, u_int ppr_options, u_int type, int paused)
1906 {
1907 struct ahc_initiator_tinfo *tinfo;
1908 struct ahc_tmode_tstate *tstate;
1909 u_int old_period;
1910 u_int old_offset;
1911 u_int old_ppr;
1912 int active;
1913 int update_needed;
1914
1915 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
1916 update_needed = 0;
1917
1918 if (syncrate == NULL) {
1919 period = 0;
1920 offset = 0;
1921 }
1922
1923 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1924 devinfo->target, &tstate);
1925
1926 if ((type & AHC_TRANS_USER) != 0) {
1927 tinfo->user.period = period;
1928 tinfo->user.offset = offset;
1929 tinfo->user.ppr_options = ppr_options;
1930 }
1931
1932 if ((type & AHC_TRANS_GOAL) != 0) {
1933 tinfo->goal.period = period;
1934 tinfo->goal.offset = offset;
1935 tinfo->goal.ppr_options = ppr_options;
1936 }
1937
1938 old_period = tinfo->curr.period;
1939 old_offset = tinfo->curr.offset;
1940 old_ppr = tinfo->curr.ppr_options;
1941
1942 if ((type & AHC_TRANS_CUR) != 0
1943 && (old_period != period
1944 || old_offset != offset
1945 || old_ppr != ppr_options)) {
1946 u_int scsirate;
1947
1948 update_needed++;
1949 scsirate = tinfo->scsirate;
1950 if ((ahc->features & AHC_ULTRA2) != 0) {
1951 scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
1952 if (syncrate != NULL) {
1953 scsirate |= syncrate->sxfr_u2;
1954 if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
1955 scsirate |= ENABLE_CRC;
1956 else
1957 scsirate |= SINGLE_EDGE;
1958 }
1959 } else {
1960 scsirate &= ~(SXFR|SOFS);
1961 /*
1962 * Ensure Ultra mode is set properly for
1963 * this target.
1964 */
1965 tstate->ultraenb &= ~devinfo->target_mask;
1966 if (syncrate != NULL) {
1967 if (syncrate->sxfr & ULTRA_SXFR) {
1968 tstate->ultraenb |=
1969 devinfo->target_mask;
1970 }
1971 scsirate |= syncrate->sxfr & SXFR;
1972 scsirate |= offset & SOFS;
1973 }
1974 if (active) {
1975 u_int sxfrctl0;
1976
1977 sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
1978 sxfrctl0 &= ~FAST20;
1979 if (tstate->ultraenb & devinfo->target_mask)
1980 sxfrctl0 |= FAST20;
1981 ahc_outb(ahc, SXFRCTL0, sxfrctl0);
1982 }
1983 }
1984 if (active) {
1985 ahc_outb(ahc, SCSIRATE, scsirate);
1986 if ((ahc->features & AHC_ULTRA2) != 0)
1987 ahc_outb(ahc, SCSIOFFSET, offset);
1988 }
1989
1990 tinfo->scsirate = scsirate;
1991 tinfo->curr.period = period;
1992 tinfo->curr.offset = offset;
1993 tinfo->curr.ppr_options = ppr_options;
1994
1995 ahc_send_async(ahc, devinfo->channel, devinfo->target,
1996 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
1997 if (bootverbose) {
1998 if (offset != 0) {
1999 printf("%s: target %d synchronous at %sMHz%s, "
2000 "offset = 0x%x\n", ahc_name(ahc),
2001 devinfo->target, syncrate->rate,
2002 (ppr_options & MSG_EXT_PPR_DT_REQ)
2003 ? " DT" : "", offset);
2004 } else {
2005 printf("%s: target %d using "
2006 "asynchronous transfers\n",
2007 ahc_name(ahc), devinfo->target);
2008 }
2009 }
2010 }
2011
2012 update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
2013 tinfo, AHC_NEG_TO_GOAL);
2014
2015 if (update_needed)
2016 ahc_update_pending_scbs(ahc);
2017 }
2018
2019 /*
2020 * Update the user/goal/curr tables of wide negotiation
2021 * parameters as well as, in the case of a current or active update,
2022 * any data structures on the host controller. In the case of an
2023 * active update, the specified target is currently talking to us on
2024 * the bus, so the transfer parameter update must take effect
2025 * immediately.
2026 */
2027 void
ahc_set_width(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,u_int width,u_int type,int paused)2028 ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2029 u_int width, u_int type, int paused)
2030 {
2031 struct ahc_initiator_tinfo *tinfo;
2032 struct ahc_tmode_tstate *tstate;
2033 u_int oldwidth;
2034 int active;
2035 int update_needed;
2036
2037 active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
2038 update_needed = 0;
2039 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2040 devinfo->target, &tstate);
2041
2042 if ((type & AHC_TRANS_USER) != 0)
2043 tinfo->user.width = width;
2044
2045 if ((type & AHC_TRANS_GOAL) != 0)
2046 tinfo->goal.width = width;
2047
2048 oldwidth = tinfo->curr.width;
2049 if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
2050 u_int scsirate;
2051
2052 update_needed++;
2053 scsirate = tinfo->scsirate;
2054 scsirate &= ~WIDEXFER;
2055 if (width == MSG_EXT_WDTR_BUS_16_BIT)
2056 scsirate |= WIDEXFER;
2057
2058 tinfo->scsirate = scsirate;
2059
2060 if (active)
2061 ahc_outb(ahc, SCSIRATE, scsirate);
2062
2063 tinfo->curr.width = width;
2064
2065 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2066 CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
2067 if (bootverbose) {
2068 printf("%s: target %d using %dbit transfers\n",
2069 ahc_name(ahc), devinfo->target,
2070 8 * (0x01 << width));
2071 }
2072 }
2073
2074 update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
2075 tinfo, AHC_NEG_TO_GOAL);
2076 if (update_needed)
2077 ahc_update_pending_scbs(ahc);
2078 }
2079
2080 /*
2081 * Update the current state of tagged queuing for a given target.
2082 */
2083 void
ahc_set_tags(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,ahc_queue_alg alg)2084 ahc_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2085 ahc_queue_alg alg)
2086 {
2087 ahc_platform_set_tags(ahc, devinfo, alg);
2088 ahc_send_async(ahc, devinfo->channel, devinfo->target,
2089 devinfo->lun, AC_TRANSFER_NEG, &alg);
2090 }
2091
2092 /*
2093 * When the transfer settings for a connection change, update any
2094 * in-transit SCBs to contain the new data so the hardware will
2095 * be set correctly during future (re)selections.
2096 */
2097 static void
ahc_update_pending_scbs(struct ahc_softc * ahc)2098 ahc_update_pending_scbs(struct ahc_softc *ahc)
2099 {
2100 struct scb *pending_scb;
2101 int pending_scb_count;
2102 int i;
2103 int paused;
2104 u_int saved_scbptr;
2105
2106 /*
2107 * Traverse the pending SCB list and ensure that all of the
2108 * SCBs there have the proper settings.
2109 */
2110 pending_scb_count = 0;
2111 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2112 struct ahc_devinfo devinfo;
2113 struct hardware_scb *pending_hscb;
2114 struct ahc_initiator_tinfo *tinfo;
2115 struct ahc_tmode_tstate *tstate;
2116
2117 ahc_scb_devinfo(ahc, &devinfo, pending_scb);
2118 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
2119 devinfo.our_scsiid,
2120 devinfo.target, &tstate);
2121 pending_hscb = pending_scb->hscb;
2122 pending_hscb->control &= ~ULTRAENB;
2123 if ((tstate->ultraenb & devinfo.target_mask) != 0)
2124 pending_hscb->control |= ULTRAENB;
2125 pending_hscb->scsirate = tinfo->scsirate;
2126 pending_hscb->scsioffset = tinfo->curr.offset;
2127 if ((tstate->auto_negotiate & devinfo.target_mask) == 0
2128 && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
2129 pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
2130 pending_hscb->control &= ~MK_MESSAGE;
2131 }
2132 ahc_sync_scb(ahc, pending_scb,
2133 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2134 pending_scb_count++;
2135 }
2136
2137 if (pending_scb_count == 0)
2138 return;
2139
2140 if (ahc_is_paused(ahc)) {
2141 paused = 1;
2142 } else {
2143 paused = 0;
2144 ahc_pause(ahc);
2145 }
2146
2147 saved_scbptr = ahc_inb(ahc, SCBPTR);
2148 /* Ensure that the hscbs down on the card match the new information */
2149 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
2150 struct hardware_scb *pending_hscb;
2151 u_int control;
2152 u_int scb_tag;
2153
2154 ahc_outb(ahc, SCBPTR, i);
2155 scb_tag = ahc_inb(ahc, SCB_TAG);
2156 pending_scb = ahc_lookup_scb(ahc, scb_tag);
2157 if (pending_scb == NULL)
2158 continue;
2159
2160 pending_hscb = pending_scb->hscb;
2161 control = ahc_inb(ahc, SCB_CONTROL);
2162 control &= ~(ULTRAENB|MK_MESSAGE);
2163 control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
2164 ahc_outb(ahc, SCB_CONTROL, control);
2165 ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
2166 ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
2167 }
2168 ahc_outb(ahc, SCBPTR, saved_scbptr);
2169
2170 if (paused == 0)
2171 ahc_unpause(ahc);
2172 }
2173
2174 /**************************** Pathing Information *****************************/
2175 static void
ahc_fetch_devinfo(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)2176 ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2177 {
2178 u_int saved_scsiid;
2179 role_t role;
2180 int our_id;
2181
2182 if (ahc_inb(ahc, SSTAT0) & TARGET)
2183 role = ROLE_TARGET;
2184 else
2185 role = ROLE_INITIATOR;
2186
2187 if (role == ROLE_TARGET
2188 && (ahc->features & AHC_MULTI_TID) != 0
2189 && (ahc_inb(ahc, SEQ_FLAGS)
2190 & (CMDPHASE_PENDING|TARG_CMD_PENDING|NO_DISCONNECT)) != 0) {
2191 /* We were selected, so pull our id from TARGIDIN */
2192 our_id = ahc_inb(ahc, TARGIDIN) & OID;
2193 } else if ((ahc->features & AHC_ULTRA2) != 0)
2194 our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
2195 else
2196 our_id = ahc_inb(ahc, SCSIID) & OID;
2197
2198 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
2199 ahc_compile_devinfo(devinfo,
2200 our_id,
2201 SCSIID_TARGET(ahc, saved_scsiid),
2202 ahc_inb(ahc, SAVED_LUN),
2203 SCSIID_CHANNEL(ahc, saved_scsiid),
2204 role);
2205 }
2206
2207 struct ahc_phase_table_entry*
ahc_lookup_phase_entry(int phase)2208 ahc_lookup_phase_entry(int phase)
2209 {
2210 struct ahc_phase_table_entry *entry;
2211 struct ahc_phase_table_entry *last_entry;
2212
2213 /*
2214 * num_phases doesn't include the default entry which
2215 * will be returned if the phase doesn't match.
2216 */
2217 last_entry = &ahc_phase_table[num_phases];
2218 for (entry = ahc_phase_table; entry < last_entry; entry++) {
2219 if (phase == entry->phase)
2220 break;
2221 }
2222 return (entry);
2223 }
2224
2225 void
ahc_compile_devinfo(struct ahc_devinfo * devinfo,u_int our_id,u_int target,u_int lun,char channel,role_t role)2226 ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
2227 u_int lun, char channel, role_t role)
2228 {
2229 devinfo->our_scsiid = our_id;
2230 devinfo->target = target;
2231 devinfo->lun = lun;
2232 devinfo->target_offset = target;
2233 devinfo->channel = channel;
2234 devinfo->role = role;
2235 if (channel == 'B')
2236 devinfo->target_offset += 8;
2237 devinfo->target_mask = (0x01 << devinfo->target_offset);
2238 }
2239
2240 void
ahc_print_devinfo(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)2241 ahc_print_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2242 {
2243 printf("%s:%c:%d:%d: ", ahc_name(ahc), devinfo->channel,
2244 devinfo->target, devinfo->lun);
2245 }
2246
2247 static void
ahc_scb_devinfo(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,struct scb * scb)2248 ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2249 struct scb *scb)
2250 {
2251 role_t role;
2252 int our_id;
2253
2254 our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
2255 role = ROLE_INITIATOR;
2256 if ((scb->flags & SCB_TARGET_SCB) != 0)
2257 role = ROLE_TARGET;
2258 ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
2259 SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
2260 }
2261
2262 /************************ Message Phase Processing ****************************/
2263 static void
ahc_assert_atn(struct ahc_softc * ahc)2264 ahc_assert_atn(struct ahc_softc *ahc)
2265 {
2266 u_int scsisigo;
2267
2268 scsisigo = ATNO;
2269 if ((ahc->features & AHC_DT) == 0)
2270 scsisigo |= ahc_inb(ahc, SCSISIGI);
2271 ahc_outb(ahc, SCSISIGO, scsisigo);
2272 }
2273
2274 /*
2275 * When an initiator transaction with the MK_MESSAGE flag either reconnects
2276 * or enters the initial message out phase, we are interrupted. Fill our
2277 * outgoing message buffer with the appropriate message and beging handing
2278 * the message phase(s) manually.
2279 */
2280 static void
ahc_setup_initiator_msgout(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,struct scb * scb)2281 ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2282 struct scb *scb)
2283 {
2284 /*
2285 * To facilitate adding multiple messages together,
2286 * each routine should increment the index and len
2287 * variables instead of setting them explicitly.
2288 */
2289 ahc->msgout_index = 0;
2290 ahc->msgout_len = 0;
2291
2292 if ((scb->flags & SCB_DEVICE_RESET) == 0
2293 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
2294 u_int identify_msg;
2295
2296 identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
2297 if ((scb->hscb->control & DISCENB) != 0)
2298 identify_msg |= MSG_IDENTIFY_DISCFLAG;
2299 ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
2300 ahc->msgout_len++;
2301
2302 if ((scb->hscb->control & TAG_ENB) != 0) {
2303 ahc->msgout_buf[ahc->msgout_index++] =
2304 scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
2305 ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
2306 ahc->msgout_len += 2;
2307 }
2308 }
2309
2310 if (scb->flags & SCB_DEVICE_RESET) {
2311 ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
2312 ahc->msgout_len++;
2313 ahc_print_path(ahc, scb);
2314 printf("Bus Device Reset Message Sent\n");
2315 /*
2316 * Clear our selection hardware in advance of
2317 * the busfree. We may have an entry in the waiting
2318 * Q for this target, and we don't want to go about
2319 * selecting while we handle the busfree and blow it
2320 * away.
2321 */
2322 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
2323 } else if ((scb->flags & SCB_ABORT) != 0) {
2324 if ((scb->hscb->control & TAG_ENB) != 0)
2325 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
2326 else
2327 ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
2328 ahc->msgout_len++;
2329 ahc_print_path(ahc, scb);
2330 printf("Abort%s Message Sent\n",
2331 (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
2332 /*
2333 * Clear our selection hardware in advance of
2334 * the busfree. We may have an entry in the waiting
2335 * Q for this target, and we don't want to go about
2336 * selecting while we handle the busfree and blow it
2337 * away.
2338 */
2339 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
2340 } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
2341 ahc_build_transfer_msg(ahc, devinfo);
2342 } else {
2343 printf("ahc_intr: AWAITING_MSG for an SCB that "
2344 "does not have a waiting message\n");
2345 printf("SCSIID = %x, target_mask = %x\n", scb->hscb->scsiid,
2346 devinfo->target_mask);
2347 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
2348 "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
2349 ahc_inb(ahc, MSG_OUT), scb->flags);
2350 }
2351
2352 /*
2353 * Clear the MK_MESSAGE flag from the SCB so we aren't
2354 * asked to send this message again.
2355 */
2356 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
2357 scb->hscb->control &= ~MK_MESSAGE;
2358 ahc->msgout_index = 0;
2359 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2360 }
2361
2362 /*
2363 * Build an appropriate transfer negotiation message for the
2364 * currently active target.
2365 */
2366 static void
ahc_build_transfer_msg(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)2367 ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
2368 {
2369 /*
2370 * We need to initiate transfer negotiations.
2371 * If our current and goal settings are identical,
2372 * we want to renegotiate due to a check condition.
2373 */
2374 struct ahc_initiator_tinfo *tinfo;
2375 struct ahc_tmode_tstate *tstate;
2376 struct ahc_syncrate *rate;
2377 int dowide;
2378 int dosync;
2379 int doppr;
2380 u_int period;
2381 u_int ppr_options;
2382 u_int offset;
2383
2384 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
2385 devinfo->target, &tstate);
2386 /*
2387 * Filter our period based on the current connection.
2388 * If we can't perform DT transfers on this segment (not in LVD
2389 * mode for instance), then our decision to issue a PPR message
2390 * may change.
2391 */
2392 period = tinfo->goal.period;
2393 offset = tinfo->goal.offset;
2394 ppr_options = tinfo->goal.ppr_options;
2395 /* Target initiated PPR is not allowed in the SCSI spec */
2396 if (devinfo->role == ROLE_TARGET)
2397 ppr_options = 0;
2398 rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
2399 &ppr_options, devinfo->role);
2400 dowide = tinfo->curr.width != tinfo->goal.width;
2401 dosync = tinfo->curr.offset != offset || tinfo->curr.period != period;
2402 /*
2403 * Only use PPR if we have options that need it, even if the device
2404 * claims to support it. There might be an expander in the way
2405 * that doesn't.
2406 */
2407 doppr = ppr_options != 0;
2408
2409 if (!dowide && !dosync && !doppr) {
2410 dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
2411 dosync = tinfo->goal.offset != 0;
2412 }
2413
2414 if (!dowide && !dosync && !doppr) {
2415 /*
2416 * Force async with a WDTR message if we have a wide bus,
2417 * or just issue an SDTR with a 0 offset.
2418 */
2419 if ((ahc->features & AHC_WIDE) != 0)
2420 dowide = 1;
2421 else
2422 dosync = 1;
2423
2424 if (bootverbose) {
2425 ahc_print_devinfo(ahc, devinfo);
2426 printf("Ensuring async\n");
2427 }
2428 }
2429
2430 /* Target initiated PPR is not allowed in the SCSI spec */
2431 if (devinfo->role == ROLE_TARGET)
2432 doppr = 0;
2433
2434 /*
2435 * Both the PPR message and SDTR message require the
2436 * goal syncrate to be limited to what the target device
2437 * is capable of handling (based on whether an LVD->SE
2438 * expander is on the bus), so combine these two cases.
2439 * Regardless, guarantee that if we are using WDTR and SDTR
2440 * messages that WDTR comes first.
2441 */
2442 if (doppr || (dosync && !dowide)) {
2443 offset = tinfo->goal.offset;
2444 ahc_validate_offset(ahc, tinfo, rate, &offset,
2445 doppr ? tinfo->goal.width
2446 : tinfo->curr.width,
2447 devinfo->role);
2448 if (doppr) {
2449 ahc_construct_ppr(ahc, devinfo, period, offset,
2450 tinfo->goal.width, ppr_options);
2451 } else {
2452 ahc_construct_sdtr(ahc, devinfo, period, offset);
2453 }
2454 } else {
2455 ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
2456 }
2457 }
2458
2459 /*
2460 * Build a synchronous negotiation message in our message
2461 * buffer based on the input parameters.
2462 */
2463 static void
ahc_construct_sdtr(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,u_int period,u_int offset)2464 ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2465 u_int period, u_int offset)
2466 {
2467 if (offset == 0)
2468 period = AHC_ASYNC_XFER_PERIOD;
2469 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
2470 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR_LEN;
2471 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR;
2472 ahc->msgout_buf[ahc->msgout_index++] = period;
2473 ahc->msgout_buf[ahc->msgout_index++] = offset;
2474 ahc->msgout_len += 5;
2475 if (bootverbose) {
2476 printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %x\n",
2477 ahc_name(ahc), devinfo->channel, devinfo->target,
2478 devinfo->lun, period, offset);
2479 }
2480 }
2481
2482 /*
2483 * Build a wide negotiation message in our message
2484 * buffer based on the input parameters.
2485 */
2486 static void
ahc_construct_wdtr(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,u_int bus_width)2487 ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2488 u_int bus_width)
2489 {
2490 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
2491 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR_LEN;
2492 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR;
2493 ahc->msgout_buf[ahc->msgout_index++] = bus_width;
2494 ahc->msgout_len += 4;
2495 if (bootverbose) {
2496 printf("(%s:%c:%d:%d): Sending WDTR %x\n",
2497 ahc_name(ahc), devinfo->channel, devinfo->target,
2498 devinfo->lun, bus_width);
2499 }
2500 }
2501
2502 /*
2503 * Build a parallel protocol request message in our message
2504 * buffer based on the input parameters.
2505 */
2506 static void
ahc_construct_ppr(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,u_int period,u_int offset,u_int bus_width,u_int ppr_options)2507 ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2508 u_int period, u_int offset, u_int bus_width,
2509 u_int ppr_options)
2510 {
2511 if (offset == 0)
2512 period = AHC_ASYNC_XFER_PERIOD;
2513 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
2514 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR_LEN;
2515 ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR;
2516 ahc->msgout_buf[ahc->msgout_index++] = period;
2517 ahc->msgout_buf[ahc->msgout_index++] = 0;
2518 ahc->msgout_buf[ahc->msgout_index++] = offset;
2519 ahc->msgout_buf[ahc->msgout_index++] = bus_width;
2520 ahc->msgout_buf[ahc->msgout_index++] = ppr_options;
2521 ahc->msgout_len += 8;
2522 if (bootverbose) {
2523 printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
2524 "offset %x, ppr_options %x\n", ahc_name(ahc),
2525 devinfo->channel, devinfo->target, devinfo->lun,
2526 bus_width, period, offset, ppr_options);
2527 }
2528 }
2529
2530 /*
2531 * Clear any active message state.
2532 */
2533 static void
ahc_clear_msg_state(struct ahc_softc * ahc)2534 ahc_clear_msg_state(struct ahc_softc *ahc)
2535 {
2536 ahc->msgout_len = 0;
2537 ahc->msgin_index = 0;
2538 ahc->msg_type = MSG_TYPE_NONE;
2539 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
2540 /*
2541 * The target didn't care to respond to our
2542 * message request, so clear ATN.
2543 */
2544 ahc_outb(ahc, CLRSINT1, CLRATNO);
2545 }
2546 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
2547 ahc_outb(ahc, SEQ_FLAGS2,
2548 ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
2549 }
2550
2551 static void
ahc_handle_proto_violation(struct ahc_softc * ahc)2552 ahc_handle_proto_violation(struct ahc_softc *ahc)
2553 {
2554 struct ahc_devinfo devinfo;
2555 struct scb *scb;
2556 u_int scbid;
2557 u_int seq_flags;
2558 u_int curphase;
2559 u_int lastphase;
2560 int found;
2561
2562 ahc_fetch_devinfo(ahc, &devinfo);
2563 scbid = ahc_inb(ahc, SCB_TAG);
2564 scb = ahc_lookup_scb(ahc, scbid);
2565 seq_flags = ahc_inb(ahc, SEQ_FLAGS);
2566 curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
2567 lastphase = ahc_inb(ahc, LASTPHASE);
2568 if ((seq_flags & NOT_IDENTIFIED) != 0) {
2569 /*
2570 * The reconnecting target either did not send an
2571 * identify message, or did, but we didn't find an SCB
2572 * to match.
2573 */
2574 ahc_print_devinfo(ahc, &devinfo);
2575 printf("Target did not send an IDENTIFY message. "
2576 "LASTPHASE = 0x%x.\n", lastphase);
2577 scb = NULL;
2578 } else if (scb == NULL) {
2579 /*
2580 * We don't seem to have an SCB active for this
2581 * transaction. Print an error and reset the bus.
2582 */
2583 ahc_print_devinfo(ahc, &devinfo);
2584 printf("No SCB found during protocol violation\n");
2585 goto proto_violation_reset;
2586 } else {
2587 aic_set_transaction_status(scb, CAM_SEQUENCE_FAIL);
2588 if ((seq_flags & NO_CDB_SENT) != 0) {
2589 ahc_print_path(ahc, scb);
2590 printf("No or incomplete CDB sent to device.\n");
2591 } else if ((ahc_inb(ahc, SCB_CONTROL) & STATUS_RCVD) == 0) {
2592 /*
2593 * The target never bothered to provide status to
2594 * us prior to completing the command. Since we don't
2595 * know the disposition of this command, we must attempt
2596 * to abort it. Assert ATN and prepare to send an abort
2597 * message.
2598 */
2599 ahc_print_path(ahc, scb);
2600 printf("Completed command without status.\n");
2601 } else {
2602 ahc_print_path(ahc, scb);
2603 printf("Unknown protocol violation.\n");
2604 ahc_dump_card_state(ahc);
2605 }
2606 }
2607 if ((lastphase & ~P_DATAIN_DT) == 0
2608 || lastphase == P_COMMAND) {
2609 proto_violation_reset:
2610 /*
2611 * Target either went directly to data/command
2612 * phase or didn't respond to our ATN.
2613 * The only safe thing to do is to blow
2614 * it away with a bus reset.
2615 */
2616 found = ahc_reset_channel(ahc, 'A', TRUE);
2617 printf("%s: Issued Channel %c Bus Reset. "
2618 "%d SCBs aborted\n", ahc_name(ahc), 'A', found);
2619 } else {
2620 /*
2621 * Leave the selection hardware off in case
2622 * this abort attempt will affect yet to
2623 * be sent commands.
2624 */
2625 ahc_outb(ahc, SCSISEQ,
2626 ahc_inb(ahc, SCSISEQ) & ~ENSELO);
2627 ahc_assert_atn(ahc);
2628 ahc_outb(ahc, MSG_OUT, HOST_MSG);
2629 if (scb == NULL) {
2630 ahc_print_devinfo(ahc, &devinfo);
2631 ahc->msgout_buf[0] = MSG_ABORT_TASK;
2632 ahc->msgout_len = 1;
2633 ahc->msgout_index = 0;
2634 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2635 } else {
2636 ahc_print_path(ahc, scb);
2637 scb->flags |= SCB_ABORT;
2638 }
2639 printf("Protocol violation %s. Attempting to abort.\n",
2640 ahc_lookup_phase_entry(curphase)->phasemsg);
2641 }
2642 }
2643
2644 /*
2645 * Manual message loop handler.
2646 */
2647 static void
ahc_handle_message_phase(struct ahc_softc * ahc)2648 ahc_handle_message_phase(struct ahc_softc *ahc)
2649 {
2650 struct ahc_devinfo devinfo;
2651 u_int bus_phase;
2652 int end_session;
2653
2654 ahc_fetch_devinfo(ahc, &devinfo);
2655 end_session = FALSE;
2656 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
2657
2658 reswitch:
2659 switch (ahc->msg_type) {
2660 case MSG_TYPE_INITIATOR_MSGOUT:
2661 {
2662 int lastbyte;
2663 int phasemis;
2664 int msgdone;
2665
2666 if (ahc->msgout_len == 0)
2667 panic("HOST_MSG_LOOP interrupt with no active message");
2668
2669 #ifdef AHC_DEBUG
2670 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2671 ahc_print_devinfo(ahc, &devinfo);
2672 printf("INITIATOR_MSG_OUT");
2673 }
2674 #endif
2675 phasemis = bus_phase != P_MESGOUT;
2676 if (phasemis) {
2677 #ifdef AHC_DEBUG
2678 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2679 printf(" PHASEMIS %s\n",
2680 ahc_lookup_phase_entry(bus_phase)
2681 ->phasemsg);
2682 }
2683 #endif
2684 if (bus_phase == P_MESGIN) {
2685 /*
2686 * Change gears and see if
2687 * this messages is of interest to
2688 * us or should be passed back to
2689 * the sequencer.
2690 */
2691 ahc_outb(ahc, CLRSINT1, CLRATNO);
2692 ahc->send_msg_perror = FALSE;
2693 ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
2694 ahc->msgin_index = 0;
2695 goto reswitch;
2696 }
2697 end_session = TRUE;
2698 break;
2699 }
2700
2701 if (ahc->send_msg_perror) {
2702 ahc_outb(ahc, CLRSINT1, CLRATNO);
2703 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2704 #ifdef AHC_DEBUG
2705 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2706 printf(" byte 0x%x\n", ahc->send_msg_perror);
2707 #endif
2708 ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
2709 break;
2710 }
2711
2712 msgdone = ahc->msgout_index == ahc->msgout_len;
2713 if (msgdone) {
2714 /*
2715 * The target has requested a retry.
2716 * Re-assert ATN, reset our message index to
2717 * 0, and try again.
2718 */
2719 ahc->msgout_index = 0;
2720 ahc_assert_atn(ahc);
2721 }
2722
2723 lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
2724 if (lastbyte) {
2725 /* Last byte is signified by dropping ATN */
2726 ahc_outb(ahc, CLRSINT1, CLRATNO);
2727 }
2728
2729 /*
2730 * Clear our interrupt status and present
2731 * the next byte on the bus.
2732 */
2733 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2734 #ifdef AHC_DEBUG
2735 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2736 printf(" byte 0x%x\n",
2737 ahc->msgout_buf[ahc->msgout_index]);
2738 #endif
2739 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
2740 break;
2741 }
2742 case MSG_TYPE_INITIATOR_MSGIN:
2743 {
2744 int phasemis;
2745 int message_done;
2746
2747 #ifdef AHC_DEBUG
2748 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2749 ahc_print_devinfo(ahc, &devinfo);
2750 printf("INITIATOR_MSG_IN");
2751 }
2752 #endif
2753 phasemis = bus_phase != P_MESGIN;
2754 if (phasemis) {
2755 #ifdef AHC_DEBUG
2756 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2757 printf(" PHASEMIS %s\n",
2758 ahc_lookup_phase_entry(bus_phase)
2759 ->phasemsg);
2760 }
2761 #endif
2762 ahc->msgin_index = 0;
2763 if (bus_phase == P_MESGOUT
2764 && (ahc->send_msg_perror == TRUE
2765 || (ahc->msgout_len != 0
2766 && ahc->msgout_index == 0))) {
2767 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
2768 goto reswitch;
2769 }
2770 end_session = TRUE;
2771 break;
2772 }
2773
2774 /* Pull the byte in without acking it */
2775 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
2776 #ifdef AHC_DEBUG
2777 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2778 printf(" byte 0x%x\n",
2779 ahc->msgin_buf[ahc->msgin_index]);
2780 #endif
2781
2782 message_done = ahc_parse_msg(ahc, &devinfo);
2783
2784 if (message_done) {
2785 /*
2786 * Clear our incoming message buffer in case there
2787 * is another message following this one.
2788 */
2789 ahc->msgin_index = 0;
2790
2791 /*
2792 * If this message illicited a response,
2793 * assert ATN so the target takes us to the
2794 * message out phase.
2795 */
2796 if (ahc->msgout_len != 0) {
2797 #ifdef AHC_DEBUG
2798 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2799 ahc_print_devinfo(ahc, &devinfo);
2800 printf("Asserting ATN for response\n");
2801 }
2802 #endif
2803 ahc_assert_atn(ahc);
2804 }
2805 } else
2806 ahc->msgin_index++;
2807
2808 if (message_done == MSGLOOP_TERMINATED) {
2809 end_session = TRUE;
2810 } else {
2811 /* Ack the byte */
2812 ahc_outb(ahc, CLRSINT1, CLRREQINIT);
2813 ahc_inb(ahc, SCSIDATL);
2814 }
2815 break;
2816 }
2817 case MSG_TYPE_TARGET_MSGIN:
2818 {
2819 int msgdone;
2820
2821 if (ahc->msgout_len == 0)
2822 panic("Target MSGIN with no active message");
2823
2824 #ifdef AHC_DEBUG
2825 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2826 ahc_print_devinfo(ahc, &devinfo);
2827 printf("TARGET_MSG_IN");
2828 }
2829 #endif
2830
2831 /*
2832 * If we interrupted a mesgout session, the initiator
2833 * will not know this until our first REQ. So, we
2834 * only honor mesgout requests after we've sent our
2835 * first byte.
2836 */
2837 if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
2838 && ahc->msgout_index > 0) {
2839 /*
2840 * Change gears and see if this messages is
2841 * of interest to us or should be passed back
2842 * to the sequencer.
2843 */
2844 #ifdef AHC_DEBUG
2845 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2846 printf(" Honoring ATN Request.\n");
2847 #endif
2848 ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
2849
2850 /*
2851 * Disable SCSI Programmed I/O during the
2852 * phase change so as to avoid phantom REQs.
2853 */
2854 ahc_outb(ahc, SXFRCTL0,
2855 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2856
2857 /*
2858 * Since SPIORDY asserts when ACK is asserted
2859 * for P_MSGOUT, and SPIORDY's assertion triggered
2860 * our entry into this routine, wait for ACK to
2861 * *de-assert* before changing phases.
2862 */
2863 while ((ahc_inb(ahc, SCSISIGI) & ACKI) != 0)
2864 ;
2865
2866 ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
2867
2868 /*
2869 * All phase line changes require a bus
2870 * settle delay before REQ is asserted.
2871 * [SCSI SPI4 10.7.1]
2872 */
2873 ahc_flush_device_writes(ahc);
2874 aic_delay(AHC_BUSSETTLE_DELAY);
2875
2876 ahc->msgin_index = 0;
2877 /* Enable SCSI Programmed I/O to REQ for first byte */
2878 ahc_outb(ahc, SXFRCTL0,
2879 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2880 break;
2881 }
2882
2883 msgdone = ahc->msgout_index == ahc->msgout_len;
2884 if (msgdone) {
2885 ahc_outb(ahc, SXFRCTL0,
2886 ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2887 end_session = TRUE;
2888 break;
2889 }
2890
2891 /*
2892 * Present the next byte on the bus.
2893 */
2894 #ifdef AHC_DEBUG
2895 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2896 printf(" byte 0x%x\n",
2897 ahc->msgout_buf[ahc->msgout_index]);
2898 #endif
2899 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2900 ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
2901 break;
2902 }
2903 case MSG_TYPE_TARGET_MSGOUT:
2904 {
2905 int lastbyte;
2906 int msgdone;
2907
2908 #ifdef AHC_DEBUG
2909 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2910 ahc_print_devinfo(ahc, &devinfo);
2911 printf("TARGET_MSG_OUT");
2912 }
2913 #endif
2914 /*
2915 * The initiator signals that this is
2916 * the last byte by dropping ATN.
2917 */
2918 lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
2919
2920 /*
2921 * Read the latched byte, but turn off SPIOEN first
2922 * so that we don't inadvertently cause a REQ for the
2923 * next byte.
2924 */
2925 ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
2926 ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
2927
2928 #ifdef AHC_DEBUG
2929 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2930 printf(" byte 0x%x\n",
2931 ahc->msgin_buf[ahc->msgin_index]);
2932 #endif
2933
2934 msgdone = ahc_parse_msg(ahc, &devinfo);
2935 if (msgdone == MSGLOOP_TERMINATED) {
2936 /*
2937 * The message is *really* done in that it caused
2938 * us to go to bus free. The sequencer has already
2939 * been reset at this point, so pull the ejection
2940 * handle.
2941 */
2942 return;
2943 }
2944
2945 ahc->msgin_index++;
2946
2947 /*
2948 * XXX Read spec about initiator dropping ATN too soon
2949 * and use msgdone to detect it.
2950 */
2951 if (msgdone == MSGLOOP_MSGCOMPLETE) {
2952 ahc->msgin_index = 0;
2953
2954 /*
2955 * If this message illicited a response, transition
2956 * to the Message in phase and send it.
2957 */
2958 if (ahc->msgout_len != 0) {
2959 #ifdef AHC_DEBUG
2960 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0) {
2961 ahc_print_devinfo(ahc, &devinfo);
2962 printf(" preparing response.\n");
2963 }
2964 #endif
2965 ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
2966
2967 /*
2968 * All phase line changes require a bus
2969 * settle delay before REQ is asserted.
2970 * [SCSI SPI4 10.7.1] When transitioning
2971 * from an OUT to an IN phase, we must
2972 * also wait a data release delay to allow
2973 * the initiator time to release the data
2974 * lines. [SCSI SPI4 10.12]
2975 */
2976 ahc_flush_device_writes(ahc);
2977 aic_delay(AHC_BUSSETTLE_DELAY
2978 + AHC_DATARELEASE_DELAY);
2979
2980 /*
2981 * Enable SCSI Programmed I/O. This will
2982 * immediately cause SPIORDY to assert,
2983 * and the sequencer will call our message
2984 * loop again.
2985 */
2986 ahc_outb(ahc, SXFRCTL0,
2987 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
2988 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
2989 ahc->msgin_index = 0;
2990 break;
2991 }
2992 }
2993
2994 if (lastbyte)
2995 end_session = TRUE;
2996 else {
2997 /* Ask for the next byte. */
2998 ahc_outb(ahc, SXFRCTL0,
2999 ahc_inb(ahc, SXFRCTL0) | SPIOEN);
3000 }
3001
3002 break;
3003 }
3004 default:
3005 panic("Unknown REQINIT message type");
3006 }
3007
3008 if (end_session) {
3009 ahc_clear_msg_state(ahc);
3010 ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
3011 } else
3012 ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
3013 }
3014
3015 /*
3016 * See if we sent a particular extended message to the target.
3017 * If "full" is true, return true only if the target saw the full
3018 * message. If "full" is false, return true if the target saw at
3019 * least the first byte of the message.
3020 */
3021 static int
ahc_sent_msg(struct ahc_softc * ahc,ahc_msgtype type,u_int msgval,int full)3022 ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
3023 {
3024 int found;
3025 u_int index;
3026
3027 found = FALSE;
3028 index = 0;
3029
3030 while (index < ahc->msgout_len) {
3031 if (ahc->msgout_buf[index] == MSG_EXTENDED) {
3032 u_int end_index;
3033
3034 end_index = index + 1 + ahc->msgout_buf[index + 1];
3035 if (ahc->msgout_buf[index+2] == msgval
3036 && type == AHCMSG_EXT) {
3037 if (full) {
3038 if (ahc->msgout_index > end_index)
3039 found = TRUE;
3040 } else if (ahc->msgout_index > index)
3041 found = TRUE;
3042 }
3043 index = end_index;
3044 } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
3045 && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
3046 /* Skip tag type and tag id or residue param*/
3047 index += 2;
3048 } else {
3049 /* Single byte message */
3050 if (type == AHCMSG_1B
3051 && ahc->msgout_buf[index] == msgval
3052 && ahc->msgout_index > index)
3053 found = TRUE;
3054 index++;
3055 }
3056
3057 if (found)
3058 break;
3059 }
3060 return (found);
3061 }
3062
3063 /*
3064 * Wait for a complete incoming message, parse it, and respond accordingly.
3065 */
3066 static int
ahc_parse_msg(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)3067 ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3068 {
3069 struct ahc_initiator_tinfo *tinfo;
3070 struct ahc_tmode_tstate *tstate;
3071 int reject;
3072 int done;
3073 int response;
3074 u_int targ_scsirate;
3075
3076 done = MSGLOOP_IN_PROG;
3077 response = FALSE;
3078 reject = FALSE;
3079 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
3080 devinfo->target, &tstate);
3081 targ_scsirate = tinfo->scsirate;
3082
3083 /*
3084 * Parse as much of the message as is available,
3085 * rejecting it if we don't support it. When
3086 * the entire message is available and has been
3087 * handled, return MSGLOOP_MSGCOMPLETE, indicating
3088 * that we have parsed an entire message.
3089 *
3090 * In the case of extended messages, we accept the length
3091 * byte outright and perform more checking once we know the
3092 * extended message type.
3093 */
3094 switch (ahc->msgin_buf[0]) {
3095 case MSG_DISCONNECT:
3096 case MSG_SAVEDATAPOINTER:
3097 case MSG_CMDCOMPLETE:
3098 case MSG_RESTOREPOINTERS:
3099 case MSG_IGN_WIDE_RESIDUE:
3100 /*
3101 * End our message loop as these are messages
3102 * the sequencer handles on its own.
3103 */
3104 done = MSGLOOP_TERMINATED;
3105 break;
3106 case MSG_MESSAGE_REJECT:
3107 response = ahc_handle_msg_reject(ahc, devinfo);
3108 /* FALLTHROUGH */
3109 case MSG_NOOP:
3110 done = MSGLOOP_MSGCOMPLETE;
3111 break;
3112 case MSG_EXTENDED:
3113 {
3114 /* Wait for enough of the message to begin validation */
3115 if (ahc->msgin_index < 2)
3116 break;
3117 switch (ahc->msgin_buf[2]) {
3118 case MSG_EXT_SDTR:
3119 {
3120 struct ahc_syncrate *syncrate;
3121 u_int period;
3122 u_int ppr_options;
3123 u_int offset;
3124 u_int saved_offset;
3125
3126 if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
3127 reject = TRUE;
3128 break;
3129 }
3130
3131 /*
3132 * Wait until we have both args before validating
3133 * and acting on this message.
3134 *
3135 * Add one to MSG_EXT_SDTR_LEN to account for
3136 * the extended message preamble.
3137 */
3138 if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
3139 break;
3140
3141 period = ahc->msgin_buf[3];
3142 ppr_options = 0;
3143 saved_offset = offset = ahc->msgin_buf[4];
3144 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
3145 &ppr_options,
3146 devinfo->role);
3147 ahc_validate_offset(ahc, tinfo, syncrate, &offset,
3148 targ_scsirate & WIDEXFER,
3149 devinfo->role);
3150 if (bootverbose) {
3151 printf("(%s:%c:%d:%d): Received "
3152 "SDTR period %x, offset %x\n\t"
3153 "Filtered to period %x, offset %x\n",
3154 ahc_name(ahc), devinfo->channel,
3155 devinfo->target, devinfo->lun,
3156 ahc->msgin_buf[3], saved_offset,
3157 period, offset);
3158 }
3159 ahc_set_syncrate(ahc, devinfo,
3160 syncrate, period,
3161 offset, ppr_options,
3162 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3163 /*paused*/TRUE);
3164
3165 /*
3166 * See if we initiated Sync Negotiation
3167 * and didn't have to fall down to async
3168 * transfers.
3169 */
3170 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
3171 /* We started it */
3172 if (saved_offset != offset) {
3173 /* Went too low - force async */
3174 reject = TRUE;
3175 }
3176 } else {
3177 /*
3178 * Send our own SDTR in reply
3179 */
3180 if (bootverbose
3181 && devinfo->role == ROLE_INITIATOR) {
3182 printf("(%s:%c:%d:%d): Target "
3183 "Initiated SDTR\n",
3184 ahc_name(ahc), devinfo->channel,
3185 devinfo->target, devinfo->lun);
3186 }
3187 ahc->msgout_index = 0;
3188 ahc->msgout_len = 0;
3189 ahc_construct_sdtr(ahc, devinfo,
3190 period, offset);
3191 ahc->msgout_index = 0;
3192 response = TRUE;
3193 }
3194 done = MSGLOOP_MSGCOMPLETE;
3195 break;
3196 }
3197 case MSG_EXT_WDTR:
3198 {
3199 u_int bus_width;
3200 u_int saved_width;
3201 u_int sending_reply;
3202
3203 sending_reply = FALSE;
3204 if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
3205 reject = TRUE;
3206 break;
3207 }
3208
3209 /*
3210 * Wait until we have our arg before validating
3211 * and acting on this message.
3212 *
3213 * Add one to MSG_EXT_WDTR_LEN to account for
3214 * the extended message preamble.
3215 */
3216 if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
3217 break;
3218
3219 bus_width = ahc->msgin_buf[3];
3220 saved_width = bus_width;
3221 ahc_validate_width(ahc, tinfo, &bus_width,
3222 devinfo->role);
3223 if (bootverbose) {
3224 printf("(%s:%c:%d:%d): Received WDTR "
3225 "%x filtered to %x\n",
3226 ahc_name(ahc), devinfo->channel,
3227 devinfo->target, devinfo->lun,
3228 saved_width, bus_width);
3229 }
3230
3231 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
3232 /*
3233 * Don't send a WDTR back to the
3234 * target, since we asked first.
3235 * If the width went higher than our
3236 * request, reject it.
3237 */
3238 if (saved_width > bus_width) {
3239 reject = TRUE;
3240 printf("(%s:%c:%d:%d): requested %dBit "
3241 "transfers. Rejecting...\n",
3242 ahc_name(ahc), devinfo->channel,
3243 devinfo->target, devinfo->lun,
3244 8 * (0x01 << bus_width));
3245 bus_width = 0;
3246 }
3247 } else {
3248 /*
3249 * Send our own WDTR in reply
3250 */
3251 if (bootverbose
3252 && devinfo->role == ROLE_INITIATOR) {
3253 printf("(%s:%c:%d:%d): Target "
3254 "Initiated WDTR\n",
3255 ahc_name(ahc), devinfo->channel,
3256 devinfo->target, devinfo->lun);
3257 }
3258 ahc->msgout_index = 0;
3259 ahc->msgout_len = 0;
3260 ahc_construct_wdtr(ahc, devinfo, bus_width);
3261 ahc->msgout_index = 0;
3262 response = TRUE;
3263 sending_reply = TRUE;
3264 }
3265 /*
3266 * After a wide message, we are async, but
3267 * some devices don't seem to honor this portion
3268 * of the spec. Force a renegotiation of the
3269 * sync component of our transfer agreement even
3270 * if our goal is async. By updating our width
3271 * after forcing the negotiation, we avoid
3272 * renegotiating for width.
3273 */
3274 ahc_update_neg_request(ahc, devinfo, tstate,
3275 tinfo, AHC_NEG_ALWAYS);
3276 ahc_set_width(ahc, devinfo, bus_width,
3277 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3278 /*paused*/TRUE);
3279 if (sending_reply == FALSE && reject == FALSE) {
3280 /*
3281 * We will always have an SDTR to send.
3282 */
3283 ahc->msgout_index = 0;
3284 ahc->msgout_len = 0;
3285 ahc_build_transfer_msg(ahc, devinfo);
3286 ahc->msgout_index = 0;
3287 response = TRUE;
3288 }
3289 done = MSGLOOP_MSGCOMPLETE;
3290 break;
3291 }
3292 case MSG_EXT_PPR:
3293 {
3294 struct ahc_syncrate *syncrate;
3295 u_int period;
3296 u_int offset;
3297 u_int bus_width;
3298 u_int ppr_options;
3299 u_int saved_width;
3300 u_int saved_offset;
3301 u_int saved_ppr_options;
3302
3303 if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
3304 reject = TRUE;
3305 break;
3306 }
3307
3308 /*
3309 * Wait until we have all args before validating
3310 * and acting on this message.
3311 *
3312 * Add one to MSG_EXT_PPR_LEN to account for
3313 * the extended message preamble.
3314 */
3315 if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
3316 break;
3317
3318 period = ahc->msgin_buf[3];
3319 offset = ahc->msgin_buf[5];
3320 bus_width = ahc->msgin_buf[6];
3321 saved_width = bus_width;
3322 ppr_options = ahc->msgin_buf[7];
3323 /*
3324 * According to the spec, a DT only
3325 * period factor with no DT option
3326 * set implies async.
3327 */
3328 if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
3329 && period == 9)
3330 offset = 0;
3331 saved_ppr_options = ppr_options;
3332 saved_offset = offset;
3333
3334 /*
3335 * Mask out any options we don't support
3336 * on any controller. Transfer options are
3337 * only available if we are negotiating wide.
3338 */
3339 ppr_options &= MSG_EXT_PPR_DT_REQ;
3340 if (bus_width == 0)
3341 ppr_options = 0;
3342
3343 ahc_validate_width(ahc, tinfo, &bus_width,
3344 devinfo->role);
3345 syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
3346 &ppr_options,
3347 devinfo->role);
3348 ahc_validate_offset(ahc, tinfo, syncrate,
3349 &offset, bus_width,
3350 devinfo->role);
3351
3352 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
3353 /*
3354 * If we are unable to do any of the
3355 * requested options (we went too low),
3356 * then we'll have to reject the message.
3357 */
3358 if (saved_width > bus_width
3359 || saved_offset != offset
3360 || saved_ppr_options != ppr_options) {
3361 reject = TRUE;
3362 period = 0;
3363 offset = 0;
3364 bus_width = 0;
3365 ppr_options = 0;
3366 syncrate = NULL;
3367 }
3368 } else {
3369 if (devinfo->role != ROLE_TARGET)
3370 printf("(%s:%c:%d:%d): Target "
3371 "Initiated PPR\n",
3372 ahc_name(ahc), devinfo->channel,
3373 devinfo->target, devinfo->lun);
3374 else
3375 printf("(%s:%c:%d:%d): Initiator "
3376 "Initiated PPR\n",
3377 ahc_name(ahc), devinfo->channel,
3378 devinfo->target, devinfo->lun);
3379 ahc->msgout_index = 0;
3380 ahc->msgout_len = 0;
3381 ahc_construct_ppr(ahc, devinfo, period, offset,
3382 bus_width, ppr_options);
3383 ahc->msgout_index = 0;
3384 response = TRUE;
3385 }
3386 if (bootverbose) {
3387 printf("(%s:%c:%d:%d): Received PPR width %x, "
3388 "period %x, offset %x,options %x\n"
3389 "\tFiltered to width %x, period %x, "
3390 "offset %x, options %x\n",
3391 ahc_name(ahc), devinfo->channel,
3392 devinfo->target, devinfo->lun,
3393 saved_width, ahc->msgin_buf[3],
3394 saved_offset, saved_ppr_options,
3395 bus_width, period, offset, ppr_options);
3396 }
3397 ahc_set_width(ahc, devinfo, bus_width,
3398 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3399 /*paused*/TRUE);
3400 ahc_set_syncrate(ahc, devinfo,
3401 syncrate, period,
3402 offset, ppr_options,
3403 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3404 /*paused*/TRUE);
3405 done = MSGLOOP_MSGCOMPLETE;
3406 break;
3407 }
3408 default:
3409 /* Unknown extended message. Reject it. */
3410 reject = TRUE;
3411 break;
3412 }
3413 break;
3414 }
3415 #ifdef AHC_TARGET_MODE
3416 case MSG_BUS_DEV_RESET:
3417 ahc_handle_devreset(ahc, devinfo,
3418 CAM_BDR_SENT,
3419 "Bus Device Reset Received",
3420 /*verbose_level*/0);
3421 ahc_restart(ahc);
3422 done = MSGLOOP_TERMINATED;
3423 break;
3424 case MSG_ABORT_TAG:
3425 case MSG_ABORT:
3426 case MSG_CLEAR_QUEUE:
3427 {
3428 int tag;
3429
3430 /* Target mode messages */
3431 if (devinfo->role != ROLE_TARGET) {
3432 reject = TRUE;
3433 break;
3434 }
3435 tag = SCB_LIST_NULL;
3436 if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
3437 tag = ahc_inb(ahc, INITIATOR_TAG);
3438 ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3439 devinfo->lun, tag, ROLE_TARGET,
3440 CAM_REQ_ABORTED);
3441
3442 tstate = ahc->enabled_targets[devinfo->our_scsiid];
3443 if (tstate != NULL) {
3444 struct ahc_tmode_lstate* lstate;
3445
3446 lstate = tstate->enabled_luns[devinfo->lun];
3447 if (lstate != NULL) {
3448 ahc_queue_lstate_event(ahc, lstate,
3449 devinfo->our_scsiid,
3450 ahc->msgin_buf[0],
3451 /*arg*/tag);
3452 ahc_send_lstate_events(ahc, lstate);
3453 }
3454 }
3455 ahc_restart(ahc);
3456 done = MSGLOOP_TERMINATED;
3457 break;
3458 }
3459 #endif
3460 case MSG_TERM_IO_PROC:
3461 default:
3462 reject = TRUE;
3463 break;
3464 }
3465
3466 if (reject) {
3467 /*
3468 * Setup to reject the message.
3469 */
3470 ahc->msgout_index = 0;
3471 ahc->msgout_len = 1;
3472 ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
3473 done = MSGLOOP_MSGCOMPLETE;
3474 response = TRUE;
3475 }
3476
3477 if (done != MSGLOOP_IN_PROG && !response)
3478 /* Clear the outgoing message buffer */
3479 ahc->msgout_len = 0;
3480
3481 return (done);
3482 }
3483
3484 /*
3485 * Process a message reject message.
3486 */
3487 static int
ahc_handle_msg_reject(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)3488 ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3489 {
3490 /*
3491 * What we care about here is if we had an
3492 * outstanding SDTR or WDTR message for this
3493 * target. If we did, this is a signal that
3494 * the target is refusing negotiation.
3495 */
3496 struct scb *scb;
3497 struct ahc_initiator_tinfo *tinfo;
3498 struct ahc_tmode_tstate *tstate;
3499 u_int scb_index;
3500 u_int last_msg;
3501 int response = 0;
3502
3503 scb_index = ahc_inb(ahc, SCB_TAG);
3504 scb = ahc_lookup_scb(ahc, scb_index);
3505 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
3506 devinfo->our_scsiid,
3507 devinfo->target, &tstate);
3508 /* Might be necessary */
3509 last_msg = ahc_inb(ahc, LAST_MSG);
3510
3511 if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
3512 /*
3513 * Target does not support the PPR message.
3514 * Attempt to negotiate SPI-2 style.
3515 */
3516 if (bootverbose) {
3517 printf("(%s:%c:%d:%d): PPR Rejected. "
3518 "Trying WDTR/SDTR\n",
3519 ahc_name(ahc), devinfo->channel,
3520 devinfo->target, devinfo->lun);
3521 }
3522 tinfo->goal.ppr_options = 0;
3523 tinfo->curr.transport_version = 2;
3524 tinfo->goal.transport_version = 2;
3525 ahc->msgout_index = 0;
3526 ahc->msgout_len = 0;
3527 ahc_build_transfer_msg(ahc, devinfo);
3528 ahc->msgout_index = 0;
3529 response = 1;
3530 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
3531 /* note 8bit xfers */
3532 printf("(%s:%c:%d:%d): refuses WIDE negotiation. Using "
3533 "8bit transfers\n", ahc_name(ahc),
3534 devinfo->channel, devinfo->target, devinfo->lun);
3535 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3536 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3537 /*paused*/TRUE);
3538 /*
3539 * No need to clear the sync rate. If the target
3540 * did not accept the command, our syncrate is
3541 * unaffected. If the target started the negotiation,
3542 * but rejected our response, we already cleared the
3543 * sync rate before sending our WDTR.
3544 */
3545 if (tinfo->goal.offset != tinfo->curr.offset) {
3546 /* Start the sync negotiation */
3547 ahc->msgout_index = 0;
3548 ahc->msgout_len = 0;
3549 ahc_build_transfer_msg(ahc, devinfo);
3550 ahc->msgout_index = 0;
3551 response = 1;
3552 }
3553 } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
3554 /* note asynch xfers and clear flag */
3555 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
3556 /*offset*/0, /*ppr_options*/0,
3557 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
3558 /*paused*/TRUE);
3559 printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
3560 "Using asynchronous transfers\n",
3561 ahc_name(ahc), devinfo->channel,
3562 devinfo->target, devinfo->lun);
3563 } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
3564 int tag_type;
3565 int mask;
3566
3567 tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
3568
3569 if (tag_type == MSG_SIMPLE_TASK) {
3570 printf("(%s:%c:%d:%d): refuses tagged commands. "
3571 "Performing non-tagged I/O\n", ahc_name(ahc),
3572 devinfo->channel, devinfo->target, devinfo->lun);
3573 ahc_set_tags(ahc, devinfo, AHC_QUEUE_NONE);
3574 mask = ~0x23;
3575 } else {
3576 printf("(%s:%c:%d:%d): refuses %s tagged commands. "
3577 "Performing simple queue tagged I/O only\n",
3578 ahc_name(ahc), devinfo->channel, devinfo->target,
3579 devinfo->lun, tag_type == MSG_ORDERED_TASK
3580 ? "ordered" : "head of queue");
3581 ahc_set_tags(ahc, devinfo, AHC_QUEUE_BASIC);
3582 mask = ~0x03;
3583 }
3584
3585 /*
3586 * Resend the identify for this CCB as the target
3587 * may believe that the selection is invalid otherwise.
3588 */
3589 ahc_outb(ahc, SCB_CONTROL,
3590 ahc_inb(ahc, SCB_CONTROL) & mask);
3591 scb->hscb->control &= mask;
3592 aic_set_transaction_tag(scb, /*enabled*/FALSE,
3593 /*type*/MSG_SIMPLE_TASK);
3594 ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
3595 ahc_assert_atn(ahc);
3596
3597 /*
3598 * This transaction is now at the head of
3599 * the untagged queue for this target.
3600 */
3601 if ((ahc->flags & AHC_SCB_BTT) == 0) {
3602 struct scb_tailq *untagged_q;
3603
3604 untagged_q =
3605 &(ahc->untagged_queues[devinfo->target_offset]);
3606 TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
3607 scb->flags |= SCB_UNTAGGEDQ;
3608 }
3609 ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
3610 scb->hscb->tag);
3611
3612 /*
3613 * Requeue all tagged commands for this target
3614 * currently in our possession so they can be
3615 * converted to untagged commands.
3616 */
3617 ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
3618 SCB_GET_CHANNEL(ahc, scb),
3619 SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
3620 ROLE_INITIATOR, CAM_REQUEUE_REQ,
3621 SEARCH_COMPLETE);
3622 } else {
3623 /*
3624 * Otherwise, we ignore it.
3625 */
3626 printf("%s:%c:%d: Message reject for %x -- ignored\n",
3627 ahc_name(ahc), devinfo->channel, devinfo->target,
3628 last_msg);
3629 }
3630 return (response);
3631 }
3632
3633 /*
3634 * Process an ignore wide residue message.
3635 */
3636 static void
ahc_handle_ign_wide_residue(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)3637 ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3638 {
3639 u_int scb_index;
3640 struct scb *scb;
3641
3642 scb_index = ahc_inb(ahc, SCB_TAG);
3643 scb = ahc_lookup_scb(ahc, scb_index);
3644 /*
3645 * XXX Actually check data direction in the sequencer?
3646 * Perhaps add datadir to some spare bits in the hscb?
3647 */
3648 if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
3649 || aic_get_transfer_dir(scb) != CAM_DIR_IN) {
3650 /*
3651 * Ignore the message if we haven't
3652 * seen an appropriate data phase yet.
3653 */
3654 } else {
3655 /*
3656 * If the residual occurred on the last
3657 * transfer and the transfer request was
3658 * expected to end on an odd count, do
3659 * nothing. Otherwise, subtract a byte
3660 * and update the residual count accordingly.
3661 */
3662 uint32_t sgptr;
3663
3664 sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
3665 if ((sgptr & SG_LIST_NULL) != 0
3666 && (ahc_inb(ahc, SCB_LUN) & SCB_XFERLEN_ODD) != 0) {
3667 /*
3668 * If the residual occurred on the last
3669 * transfer and the transfer request was
3670 * expected to end on an odd count, do
3671 * nothing.
3672 */
3673 } else {
3674 struct ahc_dma_seg *sg;
3675 uint32_t data_cnt;
3676 uint32_t data_addr;
3677 uint32_t sglen;
3678
3679 /* Pull in all of the sgptr */
3680 sgptr = ahc_inl(ahc, SCB_RESIDUAL_SGPTR);
3681 data_cnt = ahc_inl(ahc, SCB_RESIDUAL_DATACNT);
3682
3683 if ((sgptr & SG_LIST_NULL) != 0) {
3684 /*
3685 * The residual data count is not updated
3686 * for the command run to completion case.
3687 * Explicitly zero the count.
3688 */
3689 data_cnt &= ~AHC_SG_LEN_MASK;
3690 }
3691
3692 data_addr = ahc_inl(ahc, SHADDR);
3693
3694 data_cnt += 1;
3695 data_addr -= 1;
3696 sgptr &= SG_PTR_MASK;
3697
3698 sg = ahc_sg_bus_to_virt(scb, sgptr);
3699
3700 /*
3701 * The residual sg ptr points to the next S/G
3702 * to load so we must go back one.
3703 */
3704 sg--;
3705 sglen = aic_le32toh(sg->len) & AHC_SG_LEN_MASK;
3706 if (sg != scb->sg_list
3707 && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
3708 sg--;
3709 sglen = aic_le32toh(sg->len);
3710 /*
3711 * Preserve High Address and SG_LIST bits
3712 * while setting the count to 1.
3713 */
3714 data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
3715 data_addr = aic_le32toh(sg->addr)
3716 + (sglen & AHC_SG_LEN_MASK) - 1;
3717
3718 /*
3719 * Increment sg so it points to the
3720 * "next" sg.
3721 */
3722 sg++;
3723 sgptr = ahc_sg_virt_to_bus(scb, sg);
3724 }
3725 ahc_outl(ahc, SCB_RESIDUAL_SGPTR, sgptr);
3726 ahc_outl(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
3727 /*
3728 * Toggle the "oddness" of the transfer length
3729 * to handle this mid-transfer ignore wide
3730 * residue. This ensures that the oddness is
3731 * correct for subsequent data transfers.
3732 */
3733 ahc_outb(ahc, SCB_LUN,
3734 ahc_inb(ahc, SCB_LUN) ^ SCB_XFERLEN_ODD);
3735 }
3736 }
3737 }
3738
3739 /*
3740 * Reinitialize the data pointers for the active transfer
3741 * based on its current residual.
3742 */
3743 static void
ahc_reinitialize_dataptrs(struct ahc_softc * ahc)3744 ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
3745 {
3746 struct scb *scb;
3747 struct ahc_dma_seg *sg;
3748 u_int scb_index;
3749 uint32_t sgptr;
3750 uint32_t resid;
3751 uint32_t dataptr;
3752
3753 scb_index = ahc_inb(ahc, SCB_TAG);
3754 scb = ahc_lookup_scb(ahc, scb_index);
3755 sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
3756 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
3757 | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
3758 | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
3759
3760 sgptr &= SG_PTR_MASK;
3761 sg = ahc_sg_bus_to_virt(scb, sgptr);
3762
3763 /* The residual sg_ptr always points to the next sg */
3764 sg--;
3765
3766 resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
3767 | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
3768 | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
3769
3770 dataptr = aic_le32toh(sg->addr)
3771 + (aic_le32toh(sg->len) & AHC_SG_LEN_MASK)
3772 - resid;
3773 if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
3774 u_int dscommand1;
3775
3776 dscommand1 = ahc_inb(ahc, DSCOMMAND1);
3777 ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
3778 ahc_outb(ahc, HADDR,
3779 (aic_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
3780 ahc_outb(ahc, DSCOMMAND1, dscommand1);
3781 }
3782 ahc_outb(ahc, HADDR + 3, dataptr >> 24);
3783 ahc_outb(ahc, HADDR + 2, dataptr >> 16);
3784 ahc_outb(ahc, HADDR + 1, dataptr >> 8);
3785 ahc_outb(ahc, HADDR, dataptr);
3786 ahc_outb(ahc, HCNT + 2, resid >> 16);
3787 ahc_outb(ahc, HCNT + 1, resid >> 8);
3788 ahc_outb(ahc, HCNT, resid);
3789 if ((ahc->features & AHC_ULTRA2) == 0) {
3790 ahc_outb(ahc, STCNT + 2, resid >> 16);
3791 ahc_outb(ahc, STCNT + 1, resid >> 8);
3792 ahc_outb(ahc, STCNT, resid);
3793 }
3794 }
3795
3796 /*
3797 * Handle the effects of issuing a bus device reset message.
3798 */
3799 static void
ahc_handle_devreset(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,cam_status status,char * message,int verbose_level)3800 ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3801 cam_status status, char *message, int verbose_level)
3802 {
3803 #ifdef AHC_TARGET_MODE
3804 struct ahc_tmode_tstate* tstate;
3805 u_int lun;
3806 #endif
3807 int found;
3808
3809 found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
3810 CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
3811 status);
3812
3813 #ifdef AHC_TARGET_MODE
3814 /*
3815 * Send an immediate notify ccb to all target mord peripheral
3816 * drivers affected by this action.
3817 */
3818 tstate = ahc->enabled_targets[devinfo->our_scsiid];
3819 if (tstate != NULL) {
3820 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
3821 struct ahc_tmode_lstate* lstate;
3822
3823 lstate = tstate->enabled_luns[lun];
3824 if (lstate == NULL)
3825 continue;
3826
3827 ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
3828 MSG_BUS_DEV_RESET, /*arg*/0);
3829 ahc_send_lstate_events(ahc, lstate);
3830 }
3831 }
3832 #endif
3833
3834 /*
3835 * Go back to async/narrow transfers and renegotiate.
3836 */
3837 ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3838 AHC_TRANS_CUR, /*paused*/TRUE);
3839 ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
3840 /*period*/0, /*offset*/0, /*ppr_options*/0,
3841 AHC_TRANS_CUR, /*paused*/TRUE);
3842
3843 if (status != CAM_SEL_TIMEOUT)
3844 ahc_send_async(ahc, devinfo->channel, devinfo->target,
3845 CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
3846
3847 if (message != NULL
3848 && (verbose_level <= bootverbose))
3849 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc),
3850 message, devinfo->channel, devinfo->target, found);
3851 }
3852
3853 #ifdef AHC_TARGET_MODE
3854 static void
ahc_setup_target_msgin(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,struct scb * scb)3855 ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
3856 struct scb *scb)
3857 {
3858
3859 /*
3860 * To facilitate adding multiple messages together,
3861 * each routine should increment the index and len
3862 * variables instead of setting them explicitly.
3863 */
3864 ahc->msgout_index = 0;
3865 ahc->msgout_len = 0;
3866
3867 if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
3868 ahc_build_transfer_msg(ahc, devinfo);
3869 else
3870 panic("ahc_intr: AWAITING target message with no message");
3871
3872 ahc->msgout_index = 0;
3873 ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
3874 }
3875 #endif
3876 /**************************** Initialization **********************************/
3877 /*
3878 * Allocate a controller structure for a new device
3879 * and perform initial initializion.
3880 */
3881 struct ahc_softc *
ahc_alloc(void * platform_arg,char * name)3882 ahc_alloc(void *platform_arg, char *name)
3883 {
3884 struct ahc_softc *ahc;
3885 int i;
3886
3887 #ifndef __FreeBSD__
3888 ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
3889 if (!ahc) {
3890 printf("aic7xxx: cannot malloc softc!\n");
3891 free(name, M_DEVBUF);
3892 return NULL;
3893 }
3894 #else
3895 ahc = device_get_softc((device_t)platform_arg);
3896 #endif
3897 memset(ahc, 0, sizeof(*ahc));
3898 ahc->seep_config = malloc(sizeof(*ahc->seep_config),
3899 M_DEVBUF, M_NOWAIT);
3900 if (ahc->seep_config == NULL) {
3901 #ifndef __FreeBSD__
3902 free(ahc, M_DEVBUF);
3903 #endif
3904 free(name, M_DEVBUF);
3905 return (NULL);
3906 }
3907 LIST_INIT(&ahc->pending_scbs);
3908 LIST_INIT(&ahc->timedout_scbs);
3909 /* We don't know our unit number until the OSM sets it */
3910 ahc->name = name;
3911 ahc->unit = -1;
3912 ahc->description = NULL;
3913 ahc->channel = 'A';
3914 ahc->channel_b = 'B';
3915 ahc->chip = AHC_NONE;
3916 ahc->features = AHC_FENONE;
3917 ahc->bugs = AHC_BUGNONE;
3918 ahc->flags = AHC_FNONE;
3919 /*
3920 * Default to all error reporting enabled with the
3921 * sequencer operating at its fastest speed.
3922 * The bus attach code may modify this.
3923 */
3924 ahc->seqctl = FASTMODE;
3925
3926 for (i = 0; i < AHC_NUM_TARGETS; i++)
3927 TAILQ_INIT(&ahc->untagged_queues[i]);
3928 if (ahc_platform_alloc(ahc, platform_arg) != 0) {
3929 ahc_free(ahc);
3930 ahc = NULL;
3931 }
3932 ahc_lockinit(ahc);
3933 return (ahc);
3934 }
3935
3936 int
ahc_softc_init(struct ahc_softc * ahc)3937 ahc_softc_init(struct ahc_softc *ahc)
3938 {
3939
3940 /* The IRQMS bit is only valid on VL and EISA chips */
3941 if ((ahc->chip & AHC_PCI) == 0)
3942 ahc->unpause = ahc_inb(ahc, HCNTRL) & IRQMS;
3943 else
3944 ahc->unpause = 0;
3945 ahc->pause = ahc->unpause | PAUSE;
3946 /* XXX The shared scb data stuff should be deprecated */
3947 if (ahc->scb_data == NULL) {
3948 ahc->scb_data = malloc(sizeof(*ahc->scb_data),
3949 M_DEVBUF, M_NOWAIT);
3950 if (ahc->scb_data == NULL)
3951 return (ENOMEM);
3952 memset(ahc->scb_data, 0, sizeof(*ahc->scb_data));
3953 }
3954
3955 return (0);
3956 }
3957
3958 void
ahc_softc_insert(struct ahc_softc * ahc)3959 ahc_softc_insert(struct ahc_softc *ahc)
3960 {
3961 struct ahc_softc *list_ahc;
3962
3963 #if AIC_PCI_CONFIG > 0
3964 /*
3965 * Second Function PCI devices need to inherit some
3966 * settings from function 0.
3967 */
3968 if ((ahc->chip & AHC_BUS_MASK) == AHC_PCI
3969 && (ahc->features & AHC_MULTI_FUNC) != 0) {
3970 TAILQ_FOREACH(list_ahc, &ahc_tailq, links) {
3971 aic_dev_softc_t list_pci;
3972 aic_dev_softc_t pci;
3973
3974 list_pci = list_ahc->dev_softc;
3975 pci = ahc->dev_softc;
3976 if (aic_get_pci_slot(list_pci) == aic_get_pci_slot(pci)
3977 && aic_get_pci_bus(list_pci) == aic_get_pci_bus(pci)) {
3978 struct ahc_softc *master;
3979 struct ahc_softc *slave;
3980
3981 if (aic_get_pci_function(list_pci) == 0) {
3982 master = list_ahc;
3983 slave = ahc;
3984 } else {
3985 master = ahc;
3986 slave = list_ahc;
3987 }
3988 slave->flags &= ~AHC_BIOS_ENABLED;
3989 slave->flags |=
3990 master->flags & AHC_BIOS_ENABLED;
3991 slave->flags &= ~AHC_PRIMARY_CHANNEL;
3992 slave->flags |=
3993 master->flags & AHC_PRIMARY_CHANNEL;
3994 break;
3995 }
3996 }
3997 }
3998 #endif
3999
4000 /*
4001 * Insertion sort into our list of softcs.
4002 */
4003 list_ahc = TAILQ_FIRST(&ahc_tailq);
4004 while (list_ahc != NULL
4005 && ahc_softc_comp(ahc, list_ahc) <= 0)
4006 list_ahc = TAILQ_NEXT(list_ahc, links);
4007 if (list_ahc != NULL)
4008 TAILQ_INSERT_BEFORE(list_ahc, ahc, links);
4009 else
4010 TAILQ_INSERT_TAIL(&ahc_tailq, ahc, links);
4011 ahc->init_level++;
4012 }
4013
4014 void
ahc_set_unit(struct ahc_softc * ahc,int unit)4015 ahc_set_unit(struct ahc_softc *ahc, int unit)
4016 {
4017 ahc->unit = unit;
4018 }
4019
4020 void
ahc_set_name(struct ahc_softc * ahc,char * name)4021 ahc_set_name(struct ahc_softc *ahc, char *name)
4022 {
4023 if (ahc->name != NULL)
4024 free(ahc->name, M_DEVBUF);
4025 ahc->name = name;
4026 }
4027
4028 void
ahc_free(struct ahc_softc * ahc)4029 ahc_free(struct ahc_softc *ahc)
4030 {
4031 int i;
4032
4033 ahc_terminate_recovery_thread(ahc);
4034 switch (ahc->init_level) {
4035 default:
4036 case 5:
4037 ahc_shutdown(ahc);
4038 /* FALLTHROUGH */
4039 case 4:
4040 aic_dmamap_unload(ahc, ahc->shared_data_dmat,
4041 ahc->shared_data_dmamap);
4042 /* FALLTHROUGH */
4043 case 3:
4044 aic_dmamem_free(ahc, ahc->shared_data_dmat, ahc->qoutfifo,
4045 ahc->shared_data_dmamap);
4046 /* FALLTHROUGH */
4047 case 2:
4048 aic_dma_tag_destroy(ahc, ahc->shared_data_dmat);
4049 case 1:
4050 #ifndef __linux__
4051 aic_dma_tag_destroy(ahc, ahc->buffer_dmat);
4052 #endif
4053 break;
4054 case 0:
4055 break;
4056 }
4057
4058 #ifndef __linux__
4059 aic_dma_tag_destroy(ahc, ahc->parent_dmat);
4060 #endif
4061 ahc_platform_free(ahc);
4062 ahc_fini_scbdata(ahc);
4063 for (i = 0; i < AHC_NUM_TARGETS; i++) {
4064 struct ahc_tmode_tstate *tstate;
4065
4066 tstate = ahc->enabled_targets[i];
4067 if (tstate != NULL) {
4068 #ifdef AHC_TARGET_MODE
4069 int j;
4070
4071 for (j = 0; j < AHC_NUM_LUNS; j++) {
4072 struct ahc_tmode_lstate *lstate;
4073
4074 lstate = tstate->enabled_luns[j];
4075 if (lstate != NULL) {
4076 xpt_free_path(lstate->path);
4077 free(lstate, M_DEVBUF);
4078 }
4079 }
4080 #endif
4081 free(tstate, M_DEVBUF);
4082 }
4083 }
4084 #ifdef AHC_TARGET_MODE
4085 if (ahc->black_hole != NULL) {
4086 xpt_free_path(ahc->black_hole->path);
4087 free(ahc->black_hole, M_DEVBUF);
4088 }
4089 #endif
4090 if (ahc->name != NULL)
4091 free(ahc->name, M_DEVBUF);
4092 if (ahc->seep_config != NULL)
4093 free(ahc->seep_config, M_DEVBUF);
4094 #ifndef __FreeBSD__
4095 free(ahc, M_DEVBUF);
4096 #endif
4097 return;
4098 }
4099
4100 void
ahc_shutdown(void * arg)4101 ahc_shutdown(void *arg)
4102 {
4103 struct ahc_softc *ahc;
4104 int i;
4105
4106 ahc = (struct ahc_softc *)arg;
4107
4108 /* This will reset most registers to 0, but not all */
4109 ahc_reset(ahc, /*reinit*/FALSE);
4110 ahc_outb(ahc, SCSISEQ, 0);
4111 ahc_outb(ahc, SXFRCTL0, 0);
4112 ahc_outb(ahc, DSPCISTATUS, 0);
4113
4114 for (i = TARG_SCSIRATE; i < SCSICONF; i++)
4115 ahc_outb(ahc, i, 0);
4116 }
4117
4118 /*
4119 * Reset the controller and record some information about it
4120 * that is only available just after a reset. If "reinit" is
4121 * non-zero, this reset occurred after initial configuration
4122 * and the caller requests that the chip be fully reinitialized
4123 * to a runable state. Chip interrupts are *not* enabled after
4124 * a reinitialization. The caller must enable interrupts via
4125 * ahc_intr_enable().
4126 */
4127 int
ahc_reset(struct ahc_softc * ahc,int reinit)4128 ahc_reset(struct ahc_softc *ahc, int reinit)
4129 {
4130 u_int sblkctl;
4131 u_int sxfrctl1_a, sxfrctl1_b;
4132 int error;
4133 int wait;
4134
4135 /*
4136 * Preserve the value of the SXFRCTL1 register for all channels.
4137 * It contains settings that affect termination and we don't want
4138 * to disturb the integrity of the bus.
4139 */
4140 ahc_pause(ahc);
4141 sxfrctl1_b = 0;
4142 if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7770) {
4143 u_int sblkctl;
4144
4145 /*
4146 * Save channel B's settings in case this chip
4147 * is setup for TWIN channel operation.
4148 */
4149 sblkctl = ahc_inb(ahc, SBLKCTL);
4150 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
4151 sxfrctl1_b = ahc_inb(ahc, SXFRCTL1);
4152 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
4153 }
4154 sxfrctl1_a = ahc_inb(ahc, SXFRCTL1);
4155
4156 ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause);
4157
4158 /*
4159 * Ensure that the reset has finished. We delay 1000us
4160 * prior to reading the register to make sure the chip
4161 * has sufficiently completed its reset to handle register
4162 * accesses.
4163 */
4164 wait = 1000;
4165 do {
4166 aic_delay(1000);
4167 } while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK));
4168
4169 if (wait == 0) {
4170 printf("%s: WARNING - Failed chip reset! "
4171 "Trying to initialize anyway.\n", ahc_name(ahc));
4172 }
4173 ahc_outb(ahc, HCNTRL, ahc->pause);
4174
4175 /* Determine channel configuration */
4176 sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE);
4177 /* No Twin Channel PCI cards */
4178 if ((ahc->chip & AHC_PCI) != 0)
4179 sblkctl &= ~SELBUSB;
4180 switch (sblkctl) {
4181 case 0:
4182 /* Single Narrow Channel */
4183 break;
4184 case 2:
4185 /* Wide Channel */
4186 ahc->features |= AHC_WIDE;
4187 break;
4188 case 8:
4189 /* Twin Channel */
4190 ahc->features |= AHC_TWIN;
4191 break;
4192 default:
4193 printf(" Unsupported adapter type. Ignoring\n");
4194 return(-1);
4195 }
4196
4197 /*
4198 * Reload sxfrctl1.
4199 *
4200 * We must always initialize STPWEN to 1 before we
4201 * restore the saved values. STPWEN is initialized
4202 * to a tri-state condition which can only be cleared
4203 * by turning it on.
4204 */
4205 if ((ahc->features & AHC_TWIN) != 0) {
4206 u_int sblkctl;
4207
4208 sblkctl = ahc_inb(ahc, SBLKCTL);
4209 ahc_outb(ahc, SBLKCTL, sblkctl | SELBUSB);
4210 ahc_outb(ahc, SXFRCTL1, sxfrctl1_b);
4211 ahc_outb(ahc, SBLKCTL, sblkctl & ~SELBUSB);
4212 }
4213 ahc_outb(ahc, SXFRCTL1, sxfrctl1_a);
4214
4215 error = 0;
4216 if (reinit != 0)
4217 /*
4218 * If a recovery action has forced a chip reset,
4219 * re-initialize the chip to our liking.
4220 */
4221 error = ahc->bus_chip_init(ahc);
4222 #ifdef AHC_DUMP_SEQ
4223 else
4224 ahc_dumpseq(ahc);
4225 #endif
4226
4227 return (error);
4228 }
4229
4230 /*
4231 * Determine the number of SCBs available on the controller
4232 */
4233 int
ahc_probe_scbs(struct ahc_softc * ahc)4234 ahc_probe_scbs(struct ahc_softc *ahc) {
4235 int i;
4236
4237 for (i = 0; i < AHC_SCB_MAX; i++) {
4238 ahc_outb(ahc, SCBPTR, i);
4239 ahc_outb(ahc, SCB_BASE, i);
4240 if (ahc_inb(ahc, SCB_BASE) != i)
4241 break;
4242 ahc_outb(ahc, SCBPTR, 0);
4243 if (ahc_inb(ahc, SCB_BASE) != 0)
4244 break;
4245 }
4246 return (i);
4247 }
4248
4249 static void
ahc_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)4250 ahc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
4251 {
4252 bus_addr_t *baddr;
4253
4254 baddr = (bus_addr_t *)arg;
4255 *baddr = segs->ds_addr;
4256 }
4257
4258 static void
ahc_build_free_scb_list(struct ahc_softc * ahc)4259 ahc_build_free_scb_list(struct ahc_softc *ahc)
4260 {
4261 int scbsize;
4262 int i;
4263
4264 scbsize = 32;
4265 if ((ahc->flags & AHC_LSCBS_ENABLED) != 0)
4266 scbsize = 64;
4267
4268 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
4269 int j;
4270
4271 ahc_outb(ahc, SCBPTR, i);
4272
4273 /*
4274 * Touch all SCB bytes to avoid parity errors
4275 * should one of our debugging routines read
4276 * an otherwise uninitiatlized byte.
4277 */
4278 for (j = 0; j < scbsize; j++)
4279 ahc_outb(ahc, SCB_BASE+j, 0xFF);
4280
4281 /* Clear the control byte. */
4282 ahc_outb(ahc, SCB_CONTROL, 0);
4283
4284 /* Set the next pointer */
4285 if ((ahc->flags & AHC_PAGESCBS) != 0)
4286 ahc_outb(ahc, SCB_NEXT, i+1);
4287 else
4288 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
4289
4290 /* Make the tag number, SCSIID, and lun invalid */
4291 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
4292 ahc_outb(ahc, SCB_SCSIID, 0xFF);
4293 ahc_outb(ahc, SCB_LUN, 0xFF);
4294 }
4295
4296 if ((ahc->flags & AHC_PAGESCBS) != 0) {
4297 /* SCB 0 heads the free list. */
4298 ahc_outb(ahc, FREE_SCBH, 0);
4299 } else {
4300 /* No free list. */
4301 ahc_outb(ahc, FREE_SCBH, SCB_LIST_NULL);
4302 }
4303
4304 /* Make sure that the last SCB terminates the free list */
4305 ahc_outb(ahc, SCBPTR, i-1);
4306 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL);
4307 }
4308
4309 static int
ahc_init_scbdata(struct ahc_softc * ahc)4310 ahc_init_scbdata(struct ahc_softc *ahc)
4311 {
4312 struct scb_data *scb_data;
4313
4314 scb_data = ahc->scb_data;
4315 SLIST_INIT(&scb_data->free_scbs);
4316 SLIST_INIT(&scb_data->sg_maps);
4317
4318 /* Allocate SCB resources */
4319 scb_data->scbarray =
4320 (struct scb *)malloc(sizeof(struct scb) * AHC_SCB_MAX_ALLOC,
4321 M_DEVBUF, M_NOWAIT);
4322 if (scb_data->scbarray == NULL)
4323 return (ENOMEM);
4324 memset(scb_data->scbarray, 0, sizeof(struct scb) * AHC_SCB_MAX_ALLOC);
4325
4326 /* Determine the number of hardware SCBs and initialize them */
4327
4328 scb_data->maxhscbs = ahc_probe_scbs(ahc);
4329 if (ahc->scb_data->maxhscbs == 0) {
4330 printf("%s: No SCB space found\n", ahc_name(ahc));
4331 return (ENXIO);
4332 }
4333
4334 /*
4335 * Create our DMA tags. These tags define the kinds of device
4336 * accessible memory allocations and memory mappings we will
4337 * need to perform during normal operation.
4338 *
4339 * Unless we need to further restrict the allocation, we rely
4340 * on the restrictions of the parent dmat, hence the common
4341 * use of MAXADDR and MAXSIZE.
4342 */
4343
4344 /* DMA tag for our hardware scb structures */
4345 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4346 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4347 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4348 /*highaddr*/BUS_SPACE_MAXADDR,
4349 /*filter*/NULL, /*filterarg*/NULL,
4350 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
4351 /*nsegments*/1,
4352 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4353 /*flags*/0, &scb_data->hscb_dmat) != 0) {
4354 goto error_exit;
4355 }
4356
4357 scb_data->init_level++;
4358
4359 /* Allocation for our hscbs */
4360 if (aic_dmamem_alloc(ahc, scb_data->hscb_dmat,
4361 (void **)&scb_data->hscbs,
4362 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
4363 &scb_data->hscb_dmamap) != 0) {
4364 goto error_exit;
4365 }
4366
4367 scb_data->init_level++;
4368
4369 /* And permanently map them */
4370 aic_dmamap_load(ahc, scb_data->hscb_dmat, scb_data->hscb_dmamap,
4371 scb_data->hscbs,
4372 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb),
4373 ahc_dmamap_cb, &scb_data->hscb_busaddr, /*flags*/0);
4374
4375 scb_data->init_level++;
4376
4377 /* DMA tag for our sense buffers */
4378 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4379 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4380 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4381 /*highaddr*/BUS_SPACE_MAXADDR,
4382 /*filter*/NULL, /*filterarg*/NULL,
4383 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
4384 /*nsegments*/1,
4385 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4386 /*flags*/0, &scb_data->sense_dmat) != 0) {
4387 goto error_exit;
4388 }
4389
4390 scb_data->init_level++;
4391
4392 /* Allocate them */
4393 if (aic_dmamem_alloc(ahc, scb_data->sense_dmat,
4394 (void **)&scb_data->sense,
4395 BUS_DMA_NOWAIT, &scb_data->sense_dmamap) != 0) {
4396 goto error_exit;
4397 }
4398
4399 scb_data->init_level++;
4400
4401 /* And permanently map them */
4402 aic_dmamap_load(ahc, scb_data->sense_dmat, scb_data->sense_dmamap,
4403 scb_data->sense,
4404 AHC_SCB_MAX_ALLOC * sizeof(struct scsi_sense_data),
4405 ahc_dmamap_cb, &scb_data->sense_busaddr, /*flags*/0);
4406
4407 scb_data->init_level++;
4408
4409 /* DMA tag for our S/G structures. We allocate in page sized chunks */
4410 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/8,
4411 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4412 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4413 /*highaddr*/BUS_SPACE_MAXADDR,
4414 /*filter*/NULL, /*filterarg*/NULL,
4415 PAGE_SIZE, /*nsegments*/1,
4416 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4417 /*flags*/0, &scb_data->sg_dmat) != 0) {
4418 goto error_exit;
4419 }
4420
4421 scb_data->init_level++;
4422
4423 /* Perform initial CCB allocation */
4424 memset(scb_data->hscbs, 0,
4425 AHC_SCB_MAX_ALLOC * sizeof(struct hardware_scb));
4426 while (ahc_alloc_scbs(ahc) != 0)
4427 ;
4428
4429 if (scb_data->numscbs == 0) {
4430 printf("%s: ahc_init_scbdata - "
4431 "Unable to allocate initial scbs\n",
4432 ahc_name(ahc));
4433 goto error_exit;
4434 }
4435
4436 /*
4437 * Reserve the next queued SCB.
4438 */
4439 ahc->next_queued_scb = ahc_get_scb(ahc);
4440
4441 /*
4442 * Note that we were successful
4443 */
4444 return (0);
4445
4446 error_exit:
4447
4448 return (ENOMEM);
4449 }
4450
4451 static void
ahc_fini_scbdata(struct ahc_softc * ahc)4452 ahc_fini_scbdata(struct ahc_softc *ahc)
4453 {
4454 struct scb_data *scb_data;
4455
4456 scb_data = ahc->scb_data;
4457 if (scb_data == NULL)
4458 return;
4459
4460 switch (scb_data->init_level) {
4461 default:
4462 case 7:
4463 {
4464 struct sg_map_node *sg_map;
4465
4466 while ((sg_map = SLIST_FIRST(&scb_data->sg_maps))!= NULL) {
4467 SLIST_REMOVE_HEAD(&scb_data->sg_maps, links);
4468 aic_dmamap_unload(ahc, scb_data->sg_dmat,
4469 sg_map->sg_dmamap);
4470 aic_dmamem_free(ahc, scb_data->sg_dmat,
4471 sg_map->sg_vaddr,
4472 sg_map->sg_dmamap);
4473 free(sg_map, M_DEVBUF);
4474 }
4475 aic_dma_tag_destroy(ahc, scb_data->sg_dmat);
4476 }
4477 case 6:
4478 aic_dmamap_unload(ahc, scb_data->sense_dmat,
4479 scb_data->sense_dmamap);
4480 case 5:
4481 aic_dmamem_free(ahc, scb_data->sense_dmat, scb_data->sense,
4482 scb_data->sense_dmamap);
4483 case 4:
4484 aic_dma_tag_destroy(ahc, scb_data->sense_dmat);
4485 case 3:
4486 aic_dmamap_unload(ahc, scb_data->hscb_dmat,
4487 scb_data->hscb_dmamap);
4488 case 2:
4489 aic_dmamem_free(ahc, scb_data->hscb_dmat, scb_data->hscbs,
4490 scb_data->hscb_dmamap);
4491 case 1:
4492 aic_dma_tag_destroy(ahc, scb_data->hscb_dmat);
4493 break;
4494 case 0:
4495 break;
4496 }
4497 if (scb_data->scbarray != NULL)
4498 free(scb_data->scbarray, M_DEVBUF);
4499 }
4500
4501 int
ahc_alloc_scbs(struct ahc_softc * ahc)4502 ahc_alloc_scbs(struct ahc_softc *ahc)
4503 {
4504 struct scb_data *scb_data;
4505 struct scb *next_scb;
4506 struct sg_map_node *sg_map;
4507 bus_addr_t physaddr;
4508 struct ahc_dma_seg *segs;
4509 int newcount;
4510 int i;
4511
4512 scb_data = ahc->scb_data;
4513 if (scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
4514 /* Can't allocate any more */
4515 return (0);
4516
4517 next_scb = &scb_data->scbarray[scb_data->numscbs];
4518
4519 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
4520
4521 if (sg_map == NULL)
4522 return (0);
4523
4524 /* Allocate S/G space for the next batch of SCBS */
4525 if (aic_dmamem_alloc(ahc, scb_data->sg_dmat,
4526 (void **)&sg_map->sg_vaddr,
4527 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
4528 &sg_map->sg_dmamap) != 0) {
4529 free(sg_map, M_DEVBUF);
4530 return (0);
4531 }
4532
4533 SLIST_INSERT_HEAD(&scb_data->sg_maps, sg_map, links);
4534
4535 aic_dmamap_load(ahc, scb_data->sg_dmat, sg_map->sg_dmamap,
4536 sg_map->sg_vaddr, PAGE_SIZE, ahc_dmamap_cb,
4537 &sg_map->sg_physaddr, /*flags*/0);
4538
4539 segs = sg_map->sg_vaddr;
4540 physaddr = sg_map->sg_physaddr;
4541
4542 newcount = (PAGE_SIZE / (AHC_NSEG * sizeof(struct ahc_dma_seg)));
4543 newcount = MIN(newcount, (AHC_SCB_MAX_ALLOC - scb_data->numscbs));
4544 for (i = 0; i < newcount; i++) {
4545 struct scb_platform_data *pdata;
4546 #ifndef __linux__
4547 int error;
4548 #endif
4549 pdata = (struct scb_platform_data *)malloc(sizeof(*pdata),
4550 M_DEVBUF, M_NOWAIT);
4551 if (pdata == NULL)
4552 break;
4553 next_scb->platform_data = pdata;
4554 next_scb->sg_map = sg_map;
4555 next_scb->sg_list = segs;
4556 /*
4557 * The sequencer always starts with the second entry.
4558 * The first entry is embedded in the scb.
4559 */
4560 next_scb->sg_list_phys = physaddr + sizeof(struct ahc_dma_seg);
4561 next_scb->ahc_softc = ahc;
4562 next_scb->flags = SCB_FLAG_NONE;
4563 #ifndef __linux__
4564 error = aic_dmamap_create(ahc, ahc->buffer_dmat, /*flags*/0,
4565 &next_scb->dmamap);
4566 if (error != 0)
4567 break;
4568 #endif
4569 next_scb->hscb = &scb_data->hscbs[scb_data->numscbs];
4570 next_scb->hscb->tag = ahc->scb_data->numscbs;
4571 aic_timer_init(&next_scb->io_timer);
4572 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs,
4573 next_scb, links.sle);
4574 segs += AHC_NSEG;
4575 physaddr += (AHC_NSEG * sizeof(struct ahc_dma_seg));
4576 next_scb++;
4577 ahc->scb_data->numscbs++;
4578 }
4579 return (i);
4580 }
4581
4582 void
ahc_controller_info(struct ahc_softc * ahc,char * buf)4583 ahc_controller_info(struct ahc_softc *ahc, char *buf)
4584 {
4585 int len;
4586
4587 len = sprintf(buf, "%s: ", ahc_chip_names[ahc->chip & AHC_CHIPID_MASK]);
4588 buf += len;
4589 if ((ahc->features & AHC_TWIN) != 0)
4590 len = sprintf(buf, "Twin Channel, A SCSI Id=%d, "
4591 "B SCSI Id=%d, primary %c, ",
4592 ahc->our_id, ahc->our_id_b,
4593 (ahc->flags & AHC_PRIMARY_CHANNEL) + 'A');
4594 else {
4595 const char *speed;
4596 const char *type;
4597
4598 speed = "";
4599 if ((ahc->features & AHC_ULTRA) != 0) {
4600 speed = "Ultra ";
4601 } else if ((ahc->features & AHC_DT) != 0) {
4602 speed = "Ultra160 ";
4603 } else if ((ahc->features & AHC_ULTRA2) != 0) {
4604 speed = "Ultra2 ";
4605 }
4606 if ((ahc->features & AHC_WIDE) != 0) {
4607 type = "Wide";
4608 } else {
4609 type = "Single";
4610 }
4611 len = sprintf(buf, "%s%s Channel %c, SCSI Id=%d, ",
4612 speed, type, ahc->channel, ahc->our_id);
4613 }
4614 buf += len;
4615
4616 if ((ahc->flags & AHC_PAGESCBS) != 0)
4617 sprintf(buf, "%d/%d SCBs",
4618 ahc->scb_data->maxhscbs, AHC_MAX_QUEUE);
4619 else
4620 sprintf(buf, "%d SCBs", ahc->scb_data->maxhscbs);
4621 }
4622
4623 int
ahc_chip_init(struct ahc_softc * ahc)4624 ahc_chip_init(struct ahc_softc *ahc)
4625 {
4626 int term;
4627 int error;
4628 u_int i;
4629 u_int scsi_conf;
4630 u_int scsiseq_template;
4631 uint32_t physaddr;
4632
4633 ahc_outb(ahc, SEQ_FLAGS, 0);
4634 ahc_outb(ahc, SEQ_FLAGS2, 0);
4635
4636 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/
4637 if (ahc->features & AHC_TWIN) {
4638 /*
4639 * Setup Channel B first.
4640 */
4641 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) | SELBUSB);
4642 term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0;
4643 ahc_outb(ahc, SCSIID, ahc->our_id_b);
4644 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
4645 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
4646 |term|ahc->seltime_b|ENSTIMER|ACTNEGEN);
4647 if ((ahc->features & AHC_ULTRA2) != 0)
4648 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
4649 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
4650 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
4651
4652 /* Select Channel A */
4653 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB);
4654 }
4655 term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0;
4656 if ((ahc->features & AHC_ULTRA2) != 0)
4657 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id);
4658 else
4659 ahc_outb(ahc, SCSIID, ahc->our_id);
4660 scsi_conf = ahc_inb(ahc, SCSICONF);
4661 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL))
4662 |term|ahc->seltime
4663 |ENSTIMER|ACTNEGEN);
4664 if ((ahc->features & AHC_ULTRA2) != 0)
4665 ahc_outb(ahc, SIMODE0, ahc_inb(ahc, SIMODE0)|ENIOERR);
4666 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR);
4667 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN);
4668
4669 /* There are no untagged SCBs active yet. */
4670 for (i = 0; i < 16; i++) {
4671 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, 0));
4672 if ((ahc->flags & AHC_SCB_BTT) != 0) {
4673 int lun;
4674
4675 /*
4676 * The SCB based BTT allows an entry per
4677 * target and lun pair.
4678 */
4679 for (lun = 1; lun < AHC_NUM_LUNS; lun++)
4680 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, lun));
4681 }
4682 }
4683
4684 /* All of our queues are empty */
4685 for (i = 0; i < 256; i++)
4686 ahc->qoutfifo[i] = SCB_LIST_NULL;
4687 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_PREREAD);
4688
4689 for (i = 0; i < 256; i++)
4690 ahc->qinfifo[i] = SCB_LIST_NULL;
4691
4692 if ((ahc->features & AHC_MULTI_TID) != 0) {
4693 ahc_outb(ahc, TARGID, 0);
4694 ahc_outb(ahc, TARGID + 1, 0);
4695 }
4696
4697 /*
4698 * Tell the sequencer where it can find our arrays in memory.
4699 */
4700 physaddr = ahc->scb_data->hscb_busaddr;
4701 ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF);
4702 ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF);
4703 ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF);
4704 ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF);
4705
4706 physaddr = ahc->shared_data_busaddr;
4707 ahc_outb(ahc, SHARED_DATA_ADDR, physaddr & 0xFF);
4708 ahc_outb(ahc, SHARED_DATA_ADDR + 1, (physaddr >> 8) & 0xFF);
4709 ahc_outb(ahc, SHARED_DATA_ADDR + 2, (physaddr >> 16) & 0xFF);
4710 ahc_outb(ahc, SHARED_DATA_ADDR + 3, (physaddr >> 24) & 0xFF);
4711
4712 /*
4713 * Initialize the group code to command length table.
4714 * This overrides the values in TARG_SCSIRATE, so only
4715 * setup the table after we have processed that information.
4716 */
4717 ahc_outb(ahc, CMDSIZE_TABLE, 5);
4718 ahc_outb(ahc, CMDSIZE_TABLE + 1, 9);
4719 ahc_outb(ahc, CMDSIZE_TABLE + 2, 9);
4720 ahc_outb(ahc, CMDSIZE_TABLE + 3, 0);
4721 ahc_outb(ahc, CMDSIZE_TABLE + 4, 15);
4722 ahc_outb(ahc, CMDSIZE_TABLE + 5, 11);
4723 ahc_outb(ahc, CMDSIZE_TABLE + 6, 0);
4724 ahc_outb(ahc, CMDSIZE_TABLE + 7, 0);
4725
4726 if ((ahc->features & AHC_HS_MAILBOX) != 0)
4727 ahc_outb(ahc, HS_MAILBOX, 0);
4728
4729 /* Tell the sequencer of our initial queue positions */
4730 if ((ahc->features & AHC_TARGETMODE) != 0) {
4731 ahc->tqinfifonext = 1;
4732 ahc_outb(ahc, KERNEL_TQINPOS, ahc->tqinfifonext - 1);
4733 ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
4734 }
4735 ahc->qinfifonext = 0;
4736 ahc->qoutfifonext = 0;
4737 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
4738 ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256);
4739 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
4740 ahc_outb(ahc, SNSCB_QOFF, ahc->qinfifonext);
4741 ahc_outb(ahc, SDSCB_QOFF, 0);
4742 } else {
4743 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
4744 ahc_outb(ahc, QINPOS, ahc->qinfifonext);
4745 ahc_outb(ahc, QOUTPOS, ahc->qoutfifonext);
4746 }
4747
4748 /* We don't have any waiting selections */
4749 ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL);
4750
4751 /* Our disconnection list is empty too */
4752 ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL);
4753
4754 /* Message out buffer starts empty */
4755 ahc_outb(ahc, MSG_OUT, MSG_NOOP);
4756
4757 /*
4758 * Setup the allowed SCSI Sequences based on operational mode.
4759 * If we are a target, we'll enalbe select in operations once
4760 * we've had a lun enabled.
4761 */
4762 scsiseq_template = ENSELO|ENAUTOATNO|ENAUTOATNP;
4763 if ((ahc->flags & AHC_INITIATORROLE) != 0)
4764 scsiseq_template |= ENRSELI;
4765 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq_template);
4766
4767 /* Initialize our list of free SCBs. */
4768 ahc_build_free_scb_list(ahc);
4769
4770 /*
4771 * Tell the sequencer which SCB will be the next one it receives.
4772 */
4773 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
4774
4775 /*
4776 * Load the Sequencer program and Enable the adapter
4777 * in "fast" mode.
4778 */
4779 if (bootverbose)
4780 printf("%s: Downloading Sequencer Program...",
4781 ahc_name(ahc));
4782
4783 error = ahc_loadseq(ahc);
4784 if (error != 0)
4785 return (error);
4786
4787 if ((ahc->features & AHC_ULTRA2) != 0) {
4788 int wait;
4789
4790 /*
4791 * Wait for up to 500ms for our transceivers
4792 * to settle. If the adapter does not have
4793 * a cable attached, the transceivers may
4794 * never settle, so don't complain if we
4795 * fail here.
4796 */
4797 for (wait = 5000;
4798 (ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0 && wait;
4799 wait--)
4800 aic_delay(100);
4801 }
4802 ahc_restart(ahc);
4803 return (0);
4804 }
4805
4806 /*
4807 * Start the board, ready for normal operation
4808 */
4809 int
ahc_init(struct ahc_softc * ahc)4810 ahc_init(struct ahc_softc *ahc)
4811 {
4812 int max_targ;
4813 int error;
4814 u_int i;
4815 u_int scsi_conf;
4816 u_int ultraenb;
4817 u_int discenable;
4818 u_int tagenable;
4819 size_t driver_data_size;
4820
4821 #ifdef AHC_DEBUG
4822 if ((ahc_debug & AHC_DEBUG_SEQUENCER) != 0)
4823 ahc->flags |= AHC_SEQUENCER_DEBUG;
4824 #endif
4825
4826 #ifdef AHC_PRINT_SRAM
4827 printf("Scratch Ram:");
4828 for (i = 0x20; i < 0x5f; i++) {
4829 if (((i % 8) == 0) && (i != 0)) {
4830 printf ("\n ");
4831 }
4832 printf (" 0x%x", ahc_inb(ahc, i));
4833 }
4834 if ((ahc->features & AHC_MORE_SRAM) != 0) {
4835 for (i = 0x70; i < 0x7f; i++) {
4836 if (((i % 8) == 0) && (i != 0)) {
4837 printf ("\n ");
4838 }
4839 printf (" 0x%x", ahc_inb(ahc, i));
4840 }
4841 }
4842 printf ("\n");
4843 /*
4844 * Reading uninitialized scratch ram may
4845 * generate parity errors.
4846 */
4847 ahc_outb(ahc, CLRINT, CLRPARERR);
4848 ahc_outb(ahc, CLRINT, CLRBRKADRINT);
4849 #endif
4850 max_targ = 15;
4851
4852 /*
4853 * Assume we have a board at this stage and it has been reset.
4854 */
4855 if ((ahc->flags & AHC_USEDEFAULTS) != 0)
4856 ahc->our_id = ahc->our_id_b = 7;
4857
4858 /*
4859 * Default to allowing initiator operations.
4860 */
4861 ahc->flags |= AHC_INITIATORROLE;
4862
4863 /*
4864 * Only allow target mode features if this unit has them enabled.
4865 */
4866 if ((AHC_TMODE_ENABLE & (0x1 << ahc->unit)) == 0)
4867 ahc->features &= ~AHC_TARGETMODE;
4868
4869 #ifndef __linux__
4870 /* DMA tag for mapping buffers into device visible space. */
4871 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4872 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4873 /*lowaddr*/ahc->flags & AHC_39BIT_ADDRESSING
4874 ? (bus_addr_t)0x7FFFFFFFFFULL
4875 : BUS_SPACE_MAXADDR_32BIT,
4876 /*highaddr*/BUS_SPACE_MAXADDR,
4877 /*filter*/NULL, /*filterarg*/NULL,
4878 /*maxsize*/(AHC_NSEG - 1) * PAGE_SIZE,
4879 /*nsegments*/AHC_NSEG,
4880 /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
4881 /*flags*/BUS_DMA_ALLOCNOW,
4882 &ahc->buffer_dmat) != 0) {
4883 return (ENOMEM);
4884 }
4885 #endif
4886
4887 ahc->init_level++;
4888
4889 /*
4890 * DMA tag for our command fifos and other data in system memory
4891 * the card's sequencer must be able to access. For initiator
4892 * roles, we need to allocate space for the qinfifo and qoutfifo.
4893 * The qinfifo and qoutfifo are composed of 256 1 byte elements.
4894 * When providing for the target mode role, we must additionally
4895 * provide space for the incoming target command fifo and an extra
4896 * byte to deal with a dma bug in some chip versions.
4897 */
4898 driver_data_size = 2 * 256 * sizeof(uint8_t);
4899 if ((ahc->features & AHC_TARGETMODE) != 0)
4900 driver_data_size += AHC_TMODE_CMDS * sizeof(struct target_cmd)
4901 + /*DMA WideOdd Bug Buffer*/1;
4902 if (aic_dma_tag_create(ahc, ahc->parent_dmat, /*alignment*/1,
4903 /*boundary*/BUS_SPACE_MAXADDR_32BIT + 1,
4904 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
4905 /*highaddr*/BUS_SPACE_MAXADDR,
4906 /*filter*/NULL, /*filterarg*/NULL,
4907 driver_data_size,
4908 /*nsegments*/1,
4909 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
4910 /*flags*/0, &ahc->shared_data_dmat) != 0) {
4911 return (ENOMEM);
4912 }
4913
4914 ahc->init_level++;
4915
4916 /* Allocation of driver data */
4917 if (aic_dmamem_alloc(ahc, ahc->shared_data_dmat,
4918 (void **)&ahc->qoutfifo,
4919 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
4920 &ahc->shared_data_dmamap) != 0) {
4921 return (ENOMEM);
4922 }
4923
4924 ahc->init_level++;
4925
4926 /* And permanently map it in */
4927 aic_dmamap_load(ahc, ahc->shared_data_dmat, ahc->shared_data_dmamap,
4928 ahc->qoutfifo, driver_data_size, ahc_dmamap_cb,
4929 &ahc->shared_data_busaddr, /*flags*/0);
4930
4931 if ((ahc->features & AHC_TARGETMODE) != 0) {
4932 ahc->targetcmds = (struct target_cmd *)ahc->qoutfifo;
4933 ahc->qoutfifo = (uint8_t *)&ahc->targetcmds[AHC_TMODE_CMDS];
4934 ahc->dma_bug_buf = ahc->shared_data_busaddr
4935 + driver_data_size - 1;
4936 /* All target command blocks start out invalid. */
4937 for (i = 0; i < AHC_TMODE_CMDS; i++)
4938 ahc->targetcmds[i].cmd_valid = 0;
4939 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_PREREAD);
4940 }
4941 ahc->qinfifo = &ahc->qoutfifo[256];
4942
4943 ahc->init_level++;
4944
4945 /* Allocate SCB data now that buffer_dmat is initialized */
4946 if (ahc->scb_data->maxhscbs == 0)
4947 if (ahc_init_scbdata(ahc) != 0)
4948 return (ENOMEM);
4949
4950 /*
4951 * Allocate a tstate to house information for our
4952 * initiator presence on the bus as well as the user
4953 * data for any target mode initiator.
4954 */
4955 if (ahc_alloc_tstate(ahc, ahc->our_id, 'A') == NULL) {
4956 printf("%s: unable to allocate ahc_tmode_tstate. "
4957 "Failing attach\n", ahc_name(ahc));
4958 return (ENOMEM);
4959 }
4960
4961 if ((ahc->features & AHC_TWIN) != 0) {
4962 if (ahc_alloc_tstate(ahc, ahc->our_id_b, 'B') == NULL) {
4963 printf("%s: unable to allocate ahc_tmode_tstate. "
4964 "Failing attach\n", ahc_name(ahc));
4965 return (ENOMEM);
4966 }
4967 }
4968
4969 /*
4970 * Fire up a recovery thread for this controller.
4971 */
4972 error = ahc_spawn_recovery_thread(ahc);
4973 if (error != 0)
4974 return (error);
4975
4976 if (ahc->scb_data->maxhscbs < AHC_SCB_MAX_ALLOC) {
4977 ahc->flags |= AHC_PAGESCBS;
4978 } else {
4979 ahc->flags &= ~AHC_PAGESCBS;
4980 }
4981
4982 #ifdef AHC_DEBUG
4983 if (ahc_debug & AHC_SHOW_MISC) {
4984 printf("%s: hardware scb %u bytes; kernel scb %u bytes; "
4985 "ahc_dma %u bytes\n",
4986 ahc_name(ahc),
4987 (u_int)sizeof(struct hardware_scb),
4988 (u_int)sizeof(struct scb),
4989 (u_int)sizeof(struct ahc_dma_seg));
4990 }
4991 #endif /* AHC_DEBUG */
4992
4993 /*
4994 * Look at the information that board initialization or
4995 * the board bios has left us.
4996 */
4997 if (ahc->features & AHC_TWIN) {
4998 scsi_conf = ahc_inb(ahc, SCSICONF + 1);
4999 if ((scsi_conf & RESET_SCSI) != 0
5000 && (ahc->flags & AHC_INITIATORROLE) != 0)
5001 ahc->flags |= AHC_RESET_BUS_B;
5002 }
5003
5004 scsi_conf = ahc_inb(ahc, SCSICONF);
5005 if ((scsi_conf & RESET_SCSI) != 0
5006 && (ahc->flags & AHC_INITIATORROLE) != 0)
5007 ahc->flags |= AHC_RESET_BUS_A;
5008
5009 ultraenb = 0;
5010 tagenable = ALL_TARGETS_MASK;
5011
5012 /* Grab the disconnection disable table and invert it for our needs */
5013 if ((ahc->flags & AHC_USEDEFAULTS) != 0) {
5014 printf("%s: Host Adapter Bios disabled. Using default SCSI "
5015 "device parameters\n", ahc_name(ahc));
5016 ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B|
5017 AHC_TERM_ENB_A|AHC_TERM_ENB_B;
5018 discenable = ALL_TARGETS_MASK;
5019 if ((ahc->features & AHC_ULTRA) != 0)
5020 ultraenb = ALL_TARGETS_MASK;
5021 } else {
5022 discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8)
5023 | ahc_inb(ahc, DISC_DSB));
5024 if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0)
5025 ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8)
5026 | ahc_inb(ahc, ULTRA_ENB);
5027 }
5028
5029 if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0)
5030 max_targ = 7;
5031
5032 for (i = 0; i <= max_targ; i++) {
5033 struct ahc_initiator_tinfo *tinfo;
5034 struct ahc_tmode_tstate *tstate;
5035 u_int our_id;
5036 u_int target_id;
5037 char channel;
5038
5039 channel = 'A';
5040 our_id = ahc->our_id;
5041 target_id = i;
5042 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
5043 channel = 'B';
5044 our_id = ahc->our_id_b;
5045 target_id = i % 8;
5046 }
5047 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
5048 target_id, &tstate);
5049 /* Default to async narrow across the board */
5050 memset(tinfo, 0, sizeof(*tinfo));
5051 if (ahc->flags & AHC_USEDEFAULTS) {
5052 if ((ahc->features & AHC_WIDE) != 0)
5053 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
5054
5055 /*
5056 * These will be truncated when we determine the
5057 * connection type we have with the target.
5058 */
5059 tinfo->user.period = ahc_syncrates->period;
5060 tinfo->user.offset = MAX_OFFSET;
5061 } else {
5062 u_int scsirate;
5063 uint16_t mask;
5064
5065 /* Take the settings leftover in scratch RAM. */
5066 scsirate = ahc_inb(ahc, TARG_SCSIRATE + i);
5067 mask = (0x01 << i);
5068 if ((ahc->features & AHC_ULTRA2) != 0) {
5069 u_int offset;
5070 u_int maxsync;
5071
5072 if ((scsirate & SOFS) == 0x0F) {
5073 /*
5074 * Haven't negotiated yet,
5075 * so the format is different.
5076 */
5077 scsirate = (scsirate & SXFR) >> 4
5078 | (ultraenb & mask)
5079 ? 0x08 : 0x0
5080 | (scsirate & WIDEXFER);
5081 offset = MAX_OFFSET_ULTRA2;
5082 } else
5083 offset = ahc_inb(ahc, TARG_OFFSET + i);
5084 if ((scsirate & ~WIDEXFER) == 0 && offset != 0)
5085 /* Set to the lowest sync rate, 5MHz */
5086 scsirate |= 0x1c;
5087 maxsync = AHC_SYNCRATE_ULTRA2;
5088 if ((ahc->features & AHC_DT) != 0)
5089 maxsync = AHC_SYNCRATE_DT;
5090 tinfo->user.period =
5091 ahc_find_period(ahc, scsirate, maxsync);
5092 if (offset == 0)
5093 tinfo->user.period = 0;
5094 else
5095 tinfo->user.offset = MAX_OFFSET;
5096 if ((scsirate & SXFR_ULTRA2) <= 8/*10MHz*/
5097 && (ahc->features & AHC_DT) != 0)
5098 tinfo->user.ppr_options =
5099 MSG_EXT_PPR_DT_REQ;
5100 } else if ((scsirate & SOFS) != 0) {
5101 if ((scsirate & SXFR) == 0x40
5102 && (ultraenb & mask) != 0) {
5103 /* Treat 10MHz as a non-ultra speed */
5104 scsirate &= ~SXFR;
5105 ultraenb &= ~mask;
5106 }
5107 tinfo->user.period =
5108 ahc_find_period(ahc, scsirate,
5109 (ultraenb & mask)
5110 ? AHC_SYNCRATE_ULTRA
5111 : AHC_SYNCRATE_FAST);
5112 if (tinfo->user.period != 0)
5113 tinfo->user.offset = MAX_OFFSET;
5114 }
5115 if (tinfo->user.period == 0)
5116 tinfo->user.offset = 0;
5117 if ((scsirate & WIDEXFER) != 0
5118 && (ahc->features & AHC_WIDE) != 0)
5119 tinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT;
5120 tinfo->user.protocol_version = 4;
5121 if ((ahc->features & AHC_DT) != 0)
5122 tinfo->user.transport_version = 3;
5123 else
5124 tinfo->user.transport_version = 2;
5125 tinfo->goal.protocol_version = 2;
5126 tinfo->goal.transport_version = 2;
5127 tinfo->curr.protocol_version = 2;
5128 tinfo->curr.transport_version = 2;
5129 }
5130 tstate->ultraenb = 0;
5131 }
5132 ahc->user_discenable = discenable;
5133 ahc->user_tagenable = tagenable;
5134
5135 return (ahc->bus_chip_init(ahc));
5136 }
5137
5138 void
ahc_intr_enable(struct ahc_softc * ahc,int enable)5139 ahc_intr_enable(struct ahc_softc *ahc, int enable)
5140 {
5141 u_int hcntrl;
5142
5143 hcntrl = ahc_inb(ahc, HCNTRL);
5144 hcntrl &= ~INTEN;
5145 ahc->pause &= ~INTEN;
5146 ahc->unpause &= ~INTEN;
5147 if (enable) {
5148 hcntrl |= INTEN;
5149 ahc->pause |= INTEN;
5150 ahc->unpause |= INTEN;
5151 }
5152 ahc_outb(ahc, HCNTRL, hcntrl);
5153 }
5154
5155 /*
5156 * Ensure that the card is paused in a location
5157 * outside of all critical sections and that all
5158 * pending work is completed prior to returning.
5159 * This routine should only be called from outside
5160 * an interrupt context.
5161 */
5162 void
ahc_pause_and_flushwork(struct ahc_softc * ahc)5163 ahc_pause_and_flushwork(struct ahc_softc *ahc)
5164 {
5165 int intstat;
5166 int maxloops;
5167 int paused;
5168
5169 maxloops = 1000;
5170 ahc->flags |= AHC_ALL_INTERRUPTS;
5171 paused = FALSE;
5172 do {
5173 if (paused) {
5174 ahc_unpause(ahc);
5175 /*
5176 * Give the sequencer some time to service
5177 * any active selections.
5178 */
5179 aic_delay(500);
5180 }
5181 ahc_intr(ahc);
5182 ahc_pause(ahc);
5183 paused = TRUE;
5184 ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
5185 intstat = ahc_inb(ahc, INTSTAT);
5186 if ((intstat & INT_PEND) == 0) {
5187 ahc_clear_critical_section(ahc);
5188 intstat = ahc_inb(ahc, INTSTAT);
5189 }
5190 } while (--maxloops
5191 && (intstat != 0xFF || (ahc->features & AHC_REMOVABLE) == 0)
5192 && ((intstat & INT_PEND) != 0
5193 || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)) != 0));
5194 if (maxloops == 0) {
5195 printf("Infinite interrupt loop, INTSTAT = %x",
5196 ahc_inb(ahc, INTSTAT));
5197 }
5198 ahc_platform_flushwork(ahc);
5199 ahc->flags &= ~AHC_ALL_INTERRUPTS;
5200 }
5201
5202 int
ahc_suspend(struct ahc_softc * ahc)5203 ahc_suspend(struct ahc_softc *ahc)
5204 {
5205
5206 ahc_pause_and_flushwork(ahc);
5207
5208 if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
5209 ahc_unpause(ahc);
5210 return (EBUSY);
5211 }
5212
5213 #ifdef AHC_TARGET_MODE
5214 /*
5215 * XXX What about ATIOs that have not yet been serviced?
5216 * Perhaps we should just refuse to be suspended if we
5217 * are acting in a target role.
5218 */
5219 if (ahc->pending_device != NULL) {
5220 ahc_unpause(ahc);
5221 return (EBUSY);
5222 }
5223 #endif
5224 ahc_shutdown(ahc);
5225 return (0);
5226 }
5227
5228 int
ahc_resume(struct ahc_softc * ahc)5229 ahc_resume(struct ahc_softc *ahc)
5230 {
5231
5232 ahc_reset(ahc, /*reinit*/TRUE);
5233 ahc_intr_enable(ahc, TRUE);
5234 ahc_restart(ahc);
5235 return (0);
5236 }
5237
5238 /************************** Busy Target Table *********************************/
5239 /*
5240 * Return the untagged transaction id for a given target/channel lun.
5241 * Optionally, clear the entry.
5242 */
5243 u_int
ahc_index_busy_tcl(struct ahc_softc * ahc,u_int tcl)5244 ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl)
5245 {
5246 u_int scbid;
5247 u_int target_offset;
5248
5249 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5250 u_int saved_scbptr;
5251
5252 saved_scbptr = ahc_inb(ahc, SCBPTR);
5253 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5254 scbid = ahc_inb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl));
5255 ahc_outb(ahc, SCBPTR, saved_scbptr);
5256 } else {
5257 target_offset = TCL_TARGET_OFFSET(tcl);
5258 scbid = ahc_inb(ahc, BUSY_TARGETS + target_offset);
5259 }
5260
5261 return (scbid);
5262 }
5263
5264 void
ahc_unbusy_tcl(struct ahc_softc * ahc,u_int tcl)5265 ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl)
5266 {
5267 u_int target_offset;
5268
5269 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5270 u_int saved_scbptr;
5271
5272 saved_scbptr = ahc_inb(ahc, SCBPTR);
5273 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5274 ahc_outb(ahc, SCB_64_BTT+TCL_TARGET_OFFSET(tcl), SCB_LIST_NULL);
5275 ahc_outb(ahc, SCBPTR, saved_scbptr);
5276 } else {
5277 target_offset = TCL_TARGET_OFFSET(tcl);
5278 ahc_outb(ahc, BUSY_TARGETS + target_offset, SCB_LIST_NULL);
5279 }
5280 }
5281
5282 void
ahc_busy_tcl(struct ahc_softc * ahc,u_int tcl,u_int scbid)5283 ahc_busy_tcl(struct ahc_softc *ahc, u_int tcl, u_int scbid)
5284 {
5285 u_int target_offset;
5286
5287 if ((ahc->flags & AHC_SCB_BTT) != 0) {
5288 u_int saved_scbptr;
5289
5290 saved_scbptr = ahc_inb(ahc, SCBPTR);
5291 ahc_outb(ahc, SCBPTR, TCL_LUN(tcl));
5292 ahc_outb(ahc, SCB_64_BTT + TCL_TARGET_OFFSET(tcl), scbid);
5293 ahc_outb(ahc, SCBPTR, saved_scbptr);
5294 } else {
5295 target_offset = TCL_TARGET_OFFSET(tcl);
5296 ahc_outb(ahc, BUSY_TARGETS + target_offset, scbid);
5297 }
5298 }
5299
5300 /************************** SCB and SCB queue management **********************/
5301 int
ahc_match_scb(struct ahc_softc * ahc,struct scb * scb,int target,char channel,int lun,u_int tag,role_t role)5302 ahc_match_scb(struct ahc_softc *ahc, struct scb *scb, int target,
5303 char channel, int lun, u_int tag, role_t role)
5304 {
5305 int targ = SCB_GET_TARGET(ahc, scb);
5306 char chan = SCB_GET_CHANNEL(ahc, scb);
5307 int slun = SCB_GET_LUN(scb);
5308 int match;
5309
5310 match = ((chan == channel) || (channel == ALL_CHANNELS));
5311 if (match != 0)
5312 match = ((targ == target) || (target == CAM_TARGET_WILDCARD));
5313 if (match != 0)
5314 match = ((lun == slun) || (lun == CAM_LUN_WILDCARD));
5315 if (match != 0) {
5316 #ifdef AHC_TARGET_MODE
5317 int group;
5318
5319 group = XPT_FC_GROUP(scb->io_ctx->ccb_h.func_code);
5320 if (role == ROLE_INITIATOR) {
5321 match = (group != XPT_FC_GROUP_TMODE)
5322 && ((tag == scb->hscb->tag)
5323 || (tag == SCB_LIST_NULL));
5324 } else if (role == ROLE_TARGET) {
5325 match = (group == XPT_FC_GROUP_TMODE)
5326 && ((tag == scb->io_ctx->csio.tag_id)
5327 || (tag == SCB_LIST_NULL));
5328 }
5329 #else /* !AHC_TARGET_MODE */
5330 match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
5331 #endif /* AHC_TARGET_MODE */
5332 }
5333
5334 return match;
5335 }
5336
5337 void
ahc_freeze_devq(struct ahc_softc * ahc,struct scb * scb)5338 ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
5339 {
5340 int target;
5341 char channel;
5342 int lun;
5343
5344 target = SCB_GET_TARGET(ahc, scb);
5345 lun = SCB_GET_LUN(scb);
5346 channel = SCB_GET_CHANNEL(ahc, scb);
5347
5348 ahc_search_qinfifo(ahc, target, channel, lun,
5349 /*tag*/SCB_LIST_NULL, ROLE_UNKNOWN,
5350 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
5351
5352 ahc_platform_freeze_devq(ahc, scb);
5353 }
5354
5355 void
ahc_qinfifo_requeue_tail(struct ahc_softc * ahc,struct scb * scb)5356 ahc_qinfifo_requeue_tail(struct ahc_softc *ahc, struct scb *scb)
5357 {
5358 struct scb *prev_scb;
5359
5360 prev_scb = NULL;
5361 if (ahc_qinfifo_count(ahc) != 0) {
5362 u_int prev_tag;
5363 uint8_t prev_pos;
5364
5365 prev_pos = ahc->qinfifonext - 1;
5366 prev_tag = ahc->qinfifo[prev_pos];
5367 prev_scb = ahc_lookup_scb(ahc, prev_tag);
5368 }
5369 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5370 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5371 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5372 } else {
5373 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5374 }
5375 }
5376
5377 static void
ahc_qinfifo_requeue(struct ahc_softc * ahc,struct scb * prev_scb,struct scb * scb)5378 ahc_qinfifo_requeue(struct ahc_softc *ahc, struct scb *prev_scb,
5379 struct scb *scb)
5380 {
5381 if (prev_scb == NULL) {
5382 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
5383 } else {
5384 prev_scb->hscb->next = scb->hscb->tag;
5385 ahc_sync_scb(ahc, prev_scb,
5386 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
5387 }
5388 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag;
5389 scb->hscb->next = ahc->next_queued_scb->hscb->tag;
5390 ahc_sync_scb(ahc, scb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
5391 }
5392
5393 static int
ahc_qinfifo_count(struct ahc_softc * ahc)5394 ahc_qinfifo_count(struct ahc_softc *ahc)
5395 {
5396 uint8_t qinpos;
5397 uint8_t diff;
5398
5399 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5400 qinpos = ahc_inb(ahc, SNSCB_QOFF);
5401 ahc_outb(ahc, SNSCB_QOFF, qinpos);
5402 } else
5403 qinpos = ahc_inb(ahc, QINPOS);
5404 diff = ahc->qinfifonext - qinpos;
5405 return (diff);
5406 }
5407
5408 int
ahc_search_qinfifo(struct ahc_softc * ahc,int target,char channel,int lun,u_int tag,role_t role,uint32_t status,ahc_search_action action)5409 ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel,
5410 int lun, u_int tag, role_t role, uint32_t status,
5411 ahc_search_action action)
5412 {
5413 struct scb *scb;
5414 struct scb *prev_scb;
5415 uint8_t qinstart;
5416 uint8_t qinpos;
5417 uint8_t qintail;
5418 uint8_t next;
5419 uint8_t prev;
5420 uint8_t curscbptr;
5421 int found;
5422 int have_qregs;
5423
5424 qintail = ahc->qinfifonext;
5425 have_qregs = (ahc->features & AHC_QUEUE_REGS) != 0;
5426 if (have_qregs) {
5427 qinstart = ahc_inb(ahc, SNSCB_QOFF);
5428 ahc_outb(ahc, SNSCB_QOFF, qinstart);
5429 } else
5430 qinstart = ahc_inb(ahc, QINPOS);
5431 qinpos = qinstart;
5432 found = 0;
5433 prev_scb = NULL;
5434
5435 if (action == SEARCH_COMPLETE) {
5436 /*
5437 * Don't attempt to run any queued untagged transactions
5438 * until we are done with the abort process.
5439 */
5440 ahc_freeze_untagged_queues(ahc);
5441 }
5442
5443 /*
5444 * Start with an empty queue. Entries that are not chosen
5445 * for removal will be re-added to the queue as we go.
5446 */
5447 ahc->qinfifonext = qinpos;
5448 ahc_outb(ahc, NEXT_QUEUED_SCB, ahc->next_queued_scb->hscb->tag);
5449
5450 while (qinpos != qintail) {
5451 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinpos]);
5452 if (scb == NULL) {
5453 printf("qinpos = %d, SCB index = %d\n",
5454 qinpos, ahc->qinfifo[qinpos]);
5455 panic("Loop 1\n");
5456 }
5457
5458 if (ahc_match_scb(ahc, scb, target, channel, lun, tag, role)) {
5459 /*
5460 * We found an scb that needs to be acted on.
5461 */
5462 found++;
5463 switch (action) {
5464 case SEARCH_COMPLETE:
5465 {
5466 cam_status ostat;
5467 cam_status cstat;
5468
5469 ostat = aic_get_transaction_status(scb);
5470 if (ostat == CAM_REQ_INPROG)
5471 aic_set_transaction_status(scb, status);
5472 cstat = aic_get_transaction_status(scb);
5473 if (cstat != CAM_REQ_CMP)
5474 aic_freeze_scb(scb);
5475 if ((scb->flags & SCB_ACTIVE) == 0)
5476 printf("Inactive SCB in qinfifo\n");
5477 ahc_done(ahc, scb);
5478
5479 /* FALLTHROUGH */
5480 }
5481 case SEARCH_REMOVE:
5482 break;
5483 case SEARCH_COUNT:
5484 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5485 prev_scb = scb;
5486 break;
5487 }
5488 } else {
5489 ahc_qinfifo_requeue(ahc, prev_scb, scb);
5490 prev_scb = scb;
5491 }
5492 qinpos++;
5493 }
5494
5495 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
5496 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext);
5497 } else {
5498 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext);
5499 }
5500
5501 if (action != SEARCH_COUNT
5502 && (found != 0)
5503 && (qinstart != ahc->qinfifonext)) {
5504 /*
5505 * The sequencer may be in the process of dmaing
5506 * down the SCB at the beginning of the queue.
5507 * This could be problematic if either the first,
5508 * or the second SCB is removed from the queue
5509 * (the first SCB includes a pointer to the "next"
5510 * SCB to dma). If we have removed any entries, swap
5511 * the first element in the queue with the next HSCB
5512 * so the sequencer will notice that NEXT_QUEUED_SCB
5513 * has changed during its dma attempt and will retry
5514 * the DMA.
5515 */
5516 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qinstart]);
5517
5518 if (scb == NULL) {
5519 printf("found = %d, qinstart = %d, qinfifionext = %d\n",
5520 found, qinstart, ahc->qinfifonext);
5521 panic("First/Second Qinfifo fixup\n");
5522 }
5523 /*
5524 * ahc_swap_with_next_hscb forces our next pointer to
5525 * point to the reserved SCB for future commands. Save
5526 * and restore our original next pointer to maintain
5527 * queue integrity.
5528 */
5529 next = scb->hscb->next;
5530 ahc->scb_data->scbindex[scb->hscb->tag] = NULL;
5531 ahc_swap_with_next_hscb(ahc, scb);
5532 scb->hscb->next = next;
5533 ahc->qinfifo[qinstart] = scb->hscb->tag;
5534
5535 /* Tell the card about the new head of the qinfifo. */
5536 ahc_outb(ahc, NEXT_QUEUED_SCB, scb->hscb->tag);
5537
5538 /* Fixup the tail "next" pointer. */
5539 qintail = ahc->qinfifonext - 1;
5540 scb = ahc_lookup_scb(ahc, ahc->qinfifo[qintail]);
5541 scb->hscb->next = ahc->next_queued_scb->hscb->tag;
5542 }
5543
5544 /*
5545 * Search waiting for selection list.
5546 */
5547 curscbptr = ahc_inb(ahc, SCBPTR);
5548 next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */
5549 prev = SCB_LIST_NULL;
5550
5551 while (next != SCB_LIST_NULL) {
5552 uint8_t scb_index;
5553
5554 ahc_outb(ahc, SCBPTR, next);
5555 scb_index = ahc_inb(ahc, SCB_TAG);
5556 if (scb_index >= ahc->scb_data->numscbs) {
5557 printf("Waiting List inconsistency. "
5558 "SCB index == %d, yet numscbs == %d.",
5559 scb_index, ahc->scb_data->numscbs);
5560 ahc_dump_card_state(ahc);
5561 panic("for safety");
5562 }
5563 scb = ahc_lookup_scb(ahc, scb_index);
5564 if (scb == NULL) {
5565 printf("scb_index = %d, next = %d\n",
5566 scb_index, next);
5567 panic("Waiting List traversal\n");
5568 }
5569 if (ahc_match_scb(ahc, scb, target, channel,
5570 lun, SCB_LIST_NULL, role)) {
5571 /*
5572 * We found an scb that needs to be acted on.
5573 */
5574 found++;
5575 switch (action) {
5576 case SEARCH_COMPLETE:
5577 {
5578 cam_status ostat;
5579 cam_status cstat;
5580
5581 ostat = aic_get_transaction_status(scb);
5582 if (ostat == CAM_REQ_INPROG)
5583 aic_set_transaction_status(scb,
5584 status);
5585 cstat = aic_get_transaction_status(scb);
5586 if (cstat != CAM_REQ_CMP)
5587 aic_freeze_scb(scb);
5588 if ((scb->flags & SCB_ACTIVE) == 0)
5589 printf("Inactive SCB in Wait List\n");
5590 ahc_done(ahc, scb);
5591 /* FALLTHROUGH */
5592 }
5593 case SEARCH_REMOVE:
5594 next = ahc_rem_wscb(ahc, next, prev);
5595 break;
5596 case SEARCH_COUNT:
5597 prev = next;
5598 next = ahc_inb(ahc, SCB_NEXT);
5599 break;
5600 }
5601 } else {
5602
5603 prev = next;
5604 next = ahc_inb(ahc, SCB_NEXT);
5605 }
5606 }
5607 ahc_outb(ahc, SCBPTR, curscbptr);
5608
5609 found += ahc_search_untagged_queues(ahc, /*aic_io_ctx_t*/NULL, target,
5610 channel, lun, status, action);
5611
5612 if (action == SEARCH_COMPLETE)
5613 ahc_release_untagged_queues(ahc);
5614 return (found);
5615 }
5616
5617 int
ahc_search_untagged_queues(struct ahc_softc * ahc,aic_io_ctx_t ctx,int target,char channel,int lun,uint32_t status,ahc_search_action action)5618 ahc_search_untagged_queues(struct ahc_softc *ahc, aic_io_ctx_t ctx,
5619 int target, char channel, int lun, uint32_t status,
5620 ahc_search_action action)
5621 {
5622 struct scb *scb;
5623 int maxtarget;
5624 int found;
5625 int i;
5626
5627 if (action == SEARCH_COMPLETE) {
5628 /*
5629 * Don't attempt to run any queued untagged transactions
5630 * until we are done with the abort process.
5631 */
5632 ahc_freeze_untagged_queues(ahc);
5633 }
5634
5635 found = 0;
5636 i = 0;
5637 if ((ahc->flags & AHC_SCB_BTT) == 0) {
5638 maxtarget = 16;
5639 if (target != CAM_TARGET_WILDCARD) {
5640 i = target;
5641 if (channel == 'B')
5642 i += 8;
5643 maxtarget = i + 1;
5644 }
5645 } else {
5646 maxtarget = 0;
5647 }
5648
5649 for (; i < maxtarget; i++) {
5650 struct scb_tailq *untagged_q;
5651 struct scb *next_scb;
5652
5653 untagged_q = &(ahc->untagged_queues[i]);
5654 next_scb = TAILQ_FIRST(untagged_q);
5655 while (next_scb != NULL) {
5656 scb = next_scb;
5657 next_scb = TAILQ_NEXT(scb, links.tqe);
5658
5659 /*
5660 * The head of the list may be the currently
5661 * active untagged command for a device.
5662 * We're only searching for commands that
5663 * have not been started. A transaction
5664 * marked active but still in the qinfifo
5665 * is removed by the qinfifo scanning code
5666 * above.
5667 */
5668 if ((scb->flags & SCB_ACTIVE) != 0)
5669 continue;
5670
5671 if (ahc_match_scb(ahc, scb, target, channel, lun,
5672 SCB_LIST_NULL, ROLE_INITIATOR) == 0
5673 || (ctx != NULL && ctx != scb->io_ctx))
5674 continue;
5675
5676 /*
5677 * We found an scb that needs to be acted on.
5678 */
5679 found++;
5680 switch (action) {
5681 case SEARCH_COMPLETE:
5682 {
5683 cam_status ostat;
5684 cam_status cstat;
5685
5686 ostat = aic_get_transaction_status(scb);
5687 if (ostat == CAM_REQ_INPROG)
5688 aic_set_transaction_status(scb, status);
5689 cstat = aic_get_transaction_status(scb);
5690 if (cstat != CAM_REQ_CMP)
5691 aic_freeze_scb(scb);
5692 ahc_done(ahc, scb);
5693 break;
5694 }
5695 case SEARCH_REMOVE:
5696 scb->flags &= ~SCB_UNTAGGEDQ;
5697 TAILQ_REMOVE(untagged_q, scb, links.tqe);
5698 break;
5699 case SEARCH_COUNT:
5700 break;
5701 }
5702 }
5703 }
5704
5705 if (action == SEARCH_COMPLETE)
5706 ahc_release_untagged_queues(ahc);
5707 return (found);
5708 }
5709
5710 int
ahc_search_disc_list(struct ahc_softc * ahc,int target,char channel,int lun,u_int tag,int stop_on_first,int remove,int save_state)5711 ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel,
5712 int lun, u_int tag, int stop_on_first, int remove,
5713 int save_state)
5714 {
5715 struct scb *scbp;
5716 u_int next;
5717 u_int prev;
5718 u_int count;
5719 u_int active_scb;
5720
5721 count = 0;
5722 next = ahc_inb(ahc, DISCONNECTED_SCBH);
5723 prev = SCB_LIST_NULL;
5724
5725 if (save_state) {
5726 /* restore this when we're done */
5727 active_scb = ahc_inb(ahc, SCBPTR);
5728 } else
5729 /* Silence compiler */
5730 active_scb = SCB_LIST_NULL;
5731
5732 while (next != SCB_LIST_NULL) {
5733 u_int scb_index;
5734
5735 ahc_outb(ahc, SCBPTR, next);
5736 scb_index = ahc_inb(ahc, SCB_TAG);
5737 if (scb_index >= ahc->scb_data->numscbs) {
5738 printf("Disconnected List inconsistency. "
5739 "SCB index == %d, yet numscbs == %d.",
5740 scb_index, ahc->scb_data->numscbs);
5741 ahc_dump_card_state(ahc);
5742 panic("for safety");
5743 }
5744
5745 if (next == prev) {
5746 panic("Disconnected List Loop. "
5747 "cur SCBPTR == %x, prev SCBPTR == %x.",
5748 next, prev);
5749 }
5750 scbp = ahc_lookup_scb(ahc, scb_index);
5751 if (ahc_match_scb(ahc, scbp, target, channel, lun,
5752 tag, ROLE_INITIATOR)) {
5753 count++;
5754 if (remove) {
5755 next =
5756 ahc_rem_scb_from_disc_list(ahc, prev, next);
5757 } else {
5758 prev = next;
5759 next = ahc_inb(ahc, SCB_NEXT);
5760 }
5761 if (stop_on_first)
5762 break;
5763 } else {
5764 prev = next;
5765 next = ahc_inb(ahc, SCB_NEXT);
5766 }
5767 }
5768 if (save_state)
5769 ahc_outb(ahc, SCBPTR, active_scb);
5770 return (count);
5771 }
5772
5773 /*
5774 * Remove an SCB from the on chip list of disconnected transactions.
5775 * This is empty/unused if we are not performing SCB paging.
5776 */
5777 static u_int
ahc_rem_scb_from_disc_list(struct ahc_softc * ahc,u_int prev,u_int scbptr)5778 ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr)
5779 {
5780 u_int next;
5781
5782 ahc_outb(ahc, SCBPTR, scbptr);
5783 next = ahc_inb(ahc, SCB_NEXT);
5784
5785 ahc_outb(ahc, SCB_CONTROL, 0);
5786
5787 ahc_add_curscb_to_free_list(ahc);
5788
5789 if (prev != SCB_LIST_NULL) {
5790 ahc_outb(ahc, SCBPTR, prev);
5791 ahc_outb(ahc, SCB_NEXT, next);
5792 } else
5793 ahc_outb(ahc, DISCONNECTED_SCBH, next);
5794
5795 return (next);
5796 }
5797
5798 /*
5799 * Add the SCB as selected by SCBPTR onto the on chip list of
5800 * free hardware SCBs. This list is empty/unused if we are not
5801 * performing SCB paging.
5802 */
5803 static void
ahc_add_curscb_to_free_list(struct ahc_softc * ahc)5804 ahc_add_curscb_to_free_list(struct ahc_softc *ahc)
5805 {
5806 /*
5807 * Invalidate the tag so that our abort
5808 * routines don't think it's active.
5809 */
5810 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL);
5811
5812 if ((ahc->flags & AHC_PAGESCBS) != 0) {
5813 ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH));
5814 ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR));
5815 }
5816 }
5817
5818 /*
5819 * Manipulate the waiting for selection list and return the
5820 * scb that follows the one that we remove.
5821 */
5822 static u_int
ahc_rem_wscb(struct ahc_softc * ahc,u_int scbpos,u_int prev)5823 ahc_rem_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev)
5824 {
5825 u_int curscb, next;
5826
5827 /*
5828 * Select the SCB we want to abort and
5829 * pull the next pointer out of it.
5830 */
5831 curscb = ahc_inb(ahc, SCBPTR);
5832 ahc_outb(ahc, SCBPTR, scbpos);
5833 next = ahc_inb(ahc, SCB_NEXT);
5834
5835 /* Clear the necessary fields */
5836 ahc_outb(ahc, SCB_CONTROL, 0);
5837
5838 ahc_add_curscb_to_free_list(ahc);
5839
5840 /* update the waiting list */
5841 if (prev == SCB_LIST_NULL) {
5842 /* First in the list */
5843 ahc_outb(ahc, WAITING_SCBH, next);
5844
5845 /*
5846 * Ensure we aren't attempting to perform
5847 * selection for this entry.
5848 */
5849 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
5850 } else {
5851 /*
5852 * Select the scb that pointed to us
5853 * and update its next pointer.
5854 */
5855 ahc_outb(ahc, SCBPTR, prev);
5856 ahc_outb(ahc, SCB_NEXT, next);
5857 }
5858
5859 /*
5860 * Point us back at the original scb position.
5861 */
5862 ahc_outb(ahc, SCBPTR, curscb);
5863 return next;
5864 }
5865
5866 /******************************** Error Handling ******************************/
5867 /*
5868 * Abort all SCBs that match the given description (target/channel/lun/tag),
5869 * setting their status to the passed in status if the status has not already
5870 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer
5871 * is paused before it is called.
5872 */
5873 int
ahc_abort_scbs(struct ahc_softc * ahc,int target,char channel,int lun,u_int tag,role_t role,uint32_t status)5874 ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel,
5875 int lun, u_int tag, role_t role, uint32_t status)
5876 {
5877 struct scb *scbp;
5878 struct scb *scbp_next;
5879 u_int active_scb;
5880 int i, j;
5881 int maxtarget;
5882 int minlun;
5883 int maxlun;
5884
5885 int found;
5886
5887 /*
5888 * Don't attempt to run any queued untagged transactions
5889 * until we are done with the abort process.
5890 */
5891 ahc_freeze_untagged_queues(ahc);
5892
5893 /* restore this when we're done */
5894 active_scb = ahc_inb(ahc, SCBPTR);
5895
5896 found = ahc_search_qinfifo(ahc, target, channel, lun, SCB_LIST_NULL,
5897 role, CAM_REQUEUE_REQ, SEARCH_COMPLETE);
5898
5899 /*
5900 * Clean out the busy target table for any untagged commands.
5901 */
5902 i = 0;
5903 maxtarget = 16;
5904 if (target != CAM_TARGET_WILDCARD) {
5905 i = target;
5906 if (channel == 'B')
5907 i += 8;
5908 maxtarget = i + 1;
5909 }
5910
5911 if (lun == CAM_LUN_WILDCARD) {
5912 /*
5913 * Unless we are using an SCB based
5914 * busy targets table, there is only
5915 * one table entry for all luns of
5916 * a target.
5917 */
5918 minlun = 0;
5919 maxlun = 1;
5920 if ((ahc->flags & AHC_SCB_BTT) != 0)
5921 maxlun = AHC_NUM_LUNS;
5922 } else {
5923 minlun = lun;
5924 maxlun = lun + 1;
5925 }
5926
5927 if (role != ROLE_TARGET) {
5928 for (;i < maxtarget; i++) {
5929 for (j = minlun;j < maxlun; j++) {
5930 u_int scbid;
5931 u_int tcl;
5932
5933 tcl = BUILD_TCL(i << 4, j);
5934 scbid = ahc_index_busy_tcl(ahc, tcl);
5935 scbp = ahc_lookup_scb(ahc, scbid);
5936 if (scbp == NULL
5937 || ahc_match_scb(ahc, scbp, target, channel,
5938 lun, tag, role) == 0)
5939 continue;
5940 ahc_unbusy_tcl(ahc, BUILD_TCL(i << 4, j));
5941 }
5942 }
5943
5944 /*
5945 * Go through the disconnected list and remove any entries we
5946 * have queued for completion, 0'ing their control byte too.
5947 * We save the active SCB and restore it ourselves, so there
5948 * is no reason for this search to restore it too.
5949 */
5950 ahc_search_disc_list(ahc, target, channel, lun, tag,
5951 /*stop_on_first*/FALSE, /*remove*/TRUE,
5952 /*save_state*/FALSE);
5953 }
5954
5955 /*
5956 * Go through the hardware SCB array looking for commands that
5957 * were active but not on any list. In some cases, these remnants
5958 * might not still have mappings in the scbindex array (e.g. unexpected
5959 * bus free with the same scb queued for an abort). Don't hold this
5960 * against them.
5961 */
5962 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
5963 u_int scbid;
5964
5965 ahc_outb(ahc, SCBPTR, i);
5966 scbid = ahc_inb(ahc, SCB_TAG);
5967 scbp = ahc_lookup_scb(ahc, scbid);
5968 if ((scbp == NULL && scbid != SCB_LIST_NULL)
5969 || (scbp != NULL
5970 && ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)))
5971 ahc_add_curscb_to_free_list(ahc);
5972 }
5973
5974 /*
5975 * Go through the pending CCB list and look for
5976 * commands for this target that are still active.
5977 * These are other tagged commands that were
5978 * disconnected when the reset occurred.
5979 */
5980 scbp_next = LIST_FIRST(&ahc->pending_scbs);
5981 while (scbp_next != NULL) {
5982 scbp = scbp_next;
5983 scbp_next = LIST_NEXT(scbp, pending_links);
5984 if (ahc_match_scb(ahc, scbp, target, channel, lun, tag, role)) {
5985 cam_status ostat;
5986
5987 ostat = aic_get_transaction_status(scbp);
5988 if (ostat == CAM_REQ_INPROG)
5989 aic_set_transaction_status(scbp, status);
5990 if (aic_get_transaction_status(scbp) != CAM_REQ_CMP)
5991 aic_freeze_scb(scbp);
5992 if ((scbp->flags & SCB_ACTIVE) == 0)
5993 printf("Inactive SCB on pending list\n");
5994 ahc_done(ahc, scbp);
5995 found++;
5996 }
5997 }
5998 ahc_outb(ahc, SCBPTR, active_scb);
5999 ahc_platform_abort_scbs(ahc, target, channel, lun, tag, role, status);
6000 ahc_release_untagged_queues(ahc);
6001 return found;
6002 }
6003
6004 static void
ahc_reset_current_bus(struct ahc_softc * ahc)6005 ahc_reset_current_bus(struct ahc_softc *ahc)
6006 {
6007 uint8_t scsiseq;
6008
6009 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST);
6010 scsiseq = ahc_inb(ahc, SCSISEQ);
6011 ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO);
6012 ahc_flush_device_writes(ahc);
6013 aic_delay(AHC_BUSRESET_DELAY);
6014 /* Turn off the bus reset */
6015 ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO);
6016
6017 ahc_clear_intstat(ahc);
6018
6019 /* Re-enable reset interrupts */
6020 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST);
6021 }
6022
6023 int
ahc_reset_channel(struct ahc_softc * ahc,char channel,int initiate_reset)6024 ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset)
6025 {
6026 struct ahc_devinfo devinfo;
6027 u_int initiator, target, max_scsiid;
6028 u_int sblkctl;
6029 u_int scsiseq;
6030 u_int simode1;
6031 int found;
6032 int restart_needed;
6033 char cur_channel;
6034
6035 ahc->pending_device = NULL;
6036
6037 ahc_compile_devinfo(&devinfo,
6038 CAM_TARGET_WILDCARD,
6039 CAM_TARGET_WILDCARD,
6040 CAM_LUN_WILDCARD,
6041 channel, ROLE_UNKNOWN);
6042 ahc_pause(ahc);
6043
6044 /* Make sure the sequencer is in a safe location. */
6045 ahc_clear_critical_section(ahc);
6046
6047 /*
6048 * Run our command complete fifos to ensure that we perform
6049 * completion processing on any commands that 'completed'
6050 * before the reset occurred.
6051 */
6052 ahc_run_qoutfifo(ahc);
6053 #ifdef AHC_TARGET_MODE
6054 /*
6055 * XXX - In Twin mode, the tqinfifo may have commands
6056 * for an unaffected channel in it. However, if
6057 * we have run out of ATIO resources to drain that
6058 * queue, we may not get them all out here. Further,
6059 * the blocked transactions for the reset channel
6060 * should just be killed off, irrespective of whether
6061 * we are blocked on ATIO resources. Write a routine
6062 * to compact the tqinfifo appropriately.
6063 */
6064 if ((ahc->flags & AHC_TARGETROLE) != 0) {
6065 ahc_run_tqinfifo(ahc, /*paused*/TRUE);
6066 }
6067 #endif
6068
6069 /*
6070 * Reset the bus if we are initiating this reset
6071 */
6072 sblkctl = ahc_inb(ahc, SBLKCTL);
6073 cur_channel = 'A';
6074 if ((ahc->features & AHC_TWIN) != 0
6075 && ((sblkctl & SELBUSB) != 0))
6076 cur_channel = 'B';
6077 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
6078 if (cur_channel != channel) {
6079 /* Case 1: Command for another bus is active
6080 * Stealthily reset the other bus without
6081 * upsetting the current bus.
6082 */
6083 ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB);
6084 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
6085 #ifdef AHC_TARGET_MODE
6086 /*
6087 * Bus resets clear ENSELI, so we cannot
6088 * defer re-enabling bus reset interrupts
6089 * if we are in target mode.
6090 */
6091 if ((ahc->flags & AHC_TARGETROLE) != 0)
6092 simode1 |= ENSCSIRST;
6093 #endif
6094 ahc_outb(ahc, SIMODE1, simode1);
6095 if (initiate_reset)
6096 ahc_reset_current_bus(ahc);
6097 ahc_clear_intstat(ahc);
6098 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
6099 ahc_outb(ahc, SBLKCTL, sblkctl);
6100 restart_needed = FALSE;
6101 } else {
6102 /* Case 2: A command from this bus is active or we're idle */
6103 simode1 = ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENSCSIRST);
6104 #ifdef AHC_TARGET_MODE
6105 /*
6106 * Bus resets clear ENSELI, so we cannot
6107 * defer re-enabling bus reset interrupts
6108 * if we are in target mode.
6109 */
6110 if ((ahc->flags & AHC_TARGETROLE) != 0)
6111 simode1 |= ENSCSIRST;
6112 #endif
6113 ahc_outb(ahc, SIMODE1, simode1);
6114 if (initiate_reset)
6115 ahc_reset_current_bus(ahc);
6116 ahc_clear_intstat(ahc);
6117 ahc_outb(ahc, SCSISEQ, scsiseq & (ENSELI|ENRSELI|ENAUTOATNP));
6118 restart_needed = TRUE;
6119 }
6120
6121 /*
6122 * Clean up all the state information for the
6123 * pending transactions on this bus.
6124 */
6125 found = ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, channel,
6126 CAM_LUN_WILDCARD, SCB_LIST_NULL,
6127 ROLE_UNKNOWN, CAM_SCSI_BUS_RESET);
6128
6129 max_scsiid = (ahc->features & AHC_WIDE) ? 15 : 7;
6130
6131 #ifdef AHC_TARGET_MODE
6132 /*
6133 * Send an immediate notify ccb to all target more peripheral
6134 * drivers affected by this action.
6135 */
6136 for (target = 0; target <= max_scsiid; target++) {
6137 struct ahc_tmode_tstate* tstate;
6138 u_int lun;
6139
6140 tstate = ahc->enabled_targets[target];
6141 if (tstate == NULL)
6142 continue;
6143 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
6144 struct ahc_tmode_lstate* lstate;
6145
6146 lstate = tstate->enabled_luns[lun];
6147 if (lstate == NULL)
6148 continue;
6149
6150 ahc_queue_lstate_event(ahc, lstate, CAM_TARGET_WILDCARD,
6151 EVENT_TYPE_BUS_RESET, /*arg*/0);
6152 ahc_send_lstate_events(ahc, lstate);
6153 }
6154 }
6155 #endif
6156 /* Notify the XPT that a bus reset occurred */
6157 ahc_send_async(ahc, devinfo.channel, CAM_TARGET_WILDCARD,
6158 CAM_LUN_WILDCARD, AC_BUS_RESET, NULL);
6159
6160 /*
6161 * Revert to async/narrow transfers until we renegotiate.
6162 */
6163 for (target = 0; target <= max_scsiid; target++) {
6164 if (ahc->enabled_targets[target] == NULL)
6165 continue;
6166 for (initiator = 0; initiator <= max_scsiid; initiator++) {
6167 struct ahc_devinfo devinfo;
6168
6169 ahc_compile_devinfo(&devinfo, target, initiator,
6170 CAM_LUN_WILDCARD,
6171 channel, ROLE_UNKNOWN);
6172 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
6173 AHC_TRANS_CUR, /*paused*/TRUE);
6174 ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
6175 /*period*/0, /*offset*/0,
6176 /*ppr_options*/0, AHC_TRANS_CUR,
6177 /*paused*/TRUE);
6178 }
6179 }
6180
6181 if (restart_needed)
6182 ahc_restart(ahc);
6183 else
6184 ahc_unpause(ahc);
6185 return found;
6186 }
6187
6188 /***************************** Residual Processing ****************************/
6189 /*
6190 * Calculate the residual for a just completed SCB.
6191 */
6192 void
ahc_calc_residual(struct ahc_softc * ahc,struct scb * scb)6193 ahc_calc_residual(struct ahc_softc *ahc, struct scb *scb)
6194 {
6195 struct hardware_scb *hscb;
6196 struct status_pkt *spkt;
6197 uint32_t sgptr;
6198 uint32_t resid_sgptr;
6199 uint32_t resid;
6200
6201 /*
6202 * 5 cases.
6203 * 1) No residual.
6204 * SG_RESID_VALID clear in sgptr.
6205 * 2) Transferless command
6206 * 3) Never performed any transfers.
6207 * sgptr has SG_FULL_RESID set.
6208 * 4) No residual but target did not
6209 * save data pointers after the
6210 * last transfer, so sgptr was
6211 * never updated.
6212 * 5) We have a partial residual.
6213 * Use residual_sgptr to determine
6214 * where we are.
6215 */
6216
6217 hscb = scb->hscb;
6218 sgptr = aic_le32toh(hscb->sgptr);
6219 if ((sgptr & SG_RESID_VALID) == 0)
6220 /* Case 1 */
6221 return;
6222 sgptr &= ~SG_RESID_VALID;
6223
6224 if ((sgptr & SG_LIST_NULL) != 0)
6225 /* Case 2 */
6226 return;
6227
6228 spkt = &hscb->shared_data.status;
6229 resid_sgptr = aic_le32toh(spkt->residual_sg_ptr);
6230 if ((sgptr & SG_FULL_RESID) != 0) {
6231 /* Case 3 */
6232 resid = aic_get_transfer_length(scb);
6233 } else if ((resid_sgptr & SG_LIST_NULL) != 0) {
6234 /* Case 4 */
6235 return;
6236 } else if ((resid_sgptr & ~SG_PTR_MASK) != 0) {
6237 panic("Bogus resid sgptr value 0x%x\n", resid_sgptr);
6238 /* NOTREACHED */
6239 return;
6240 } else {
6241 struct ahc_dma_seg *sg;
6242
6243 /*
6244 * Remainder of the SG where the transfer
6245 * stopped.
6246 */
6247 resid = aic_le32toh(spkt->residual_datacnt) & AHC_SG_LEN_MASK;
6248 sg = ahc_sg_bus_to_virt(scb, resid_sgptr & SG_PTR_MASK);
6249
6250 /* The residual sg_ptr always points to the next sg */
6251 sg--;
6252
6253 /*
6254 * Add up the contents of all residual
6255 * SG segments that are after the SG where
6256 * the transfer stopped.
6257 */
6258 while ((aic_le32toh(sg->len) & AHC_DMA_LAST_SEG) == 0) {
6259 sg++;
6260 resid += aic_le32toh(sg->len) & AHC_SG_LEN_MASK;
6261 }
6262 }
6263 if ((scb->flags & SCB_SENSE) == 0)
6264 aic_set_residual(scb, resid);
6265 else
6266 aic_set_sense_residual(scb, resid);
6267
6268 #ifdef AHC_DEBUG
6269 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
6270 ahc_print_path(ahc, scb);
6271 printf("Handled %sResidual of %d bytes\n",
6272 (scb->flags & SCB_SENSE) ? "Sense " : "", resid);
6273 }
6274 #endif
6275 }
6276
6277 /******************************* Target Mode **********************************/
6278 #ifdef AHC_TARGET_MODE
6279 /*
6280 * Add a target mode event to this lun's queue
6281 */
6282 static void
ahc_queue_lstate_event(struct ahc_softc * ahc,struct ahc_tmode_lstate * lstate,u_int initiator_id,u_int event_type,u_int event_arg)6283 ahc_queue_lstate_event(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate,
6284 u_int initiator_id, u_int event_type, u_int event_arg)
6285 {
6286 struct ahc_tmode_event *event;
6287 int pending;
6288
6289 xpt_freeze_devq(lstate->path, /*count*/1);
6290 if (lstate->event_w_idx >= lstate->event_r_idx)
6291 pending = lstate->event_w_idx - lstate->event_r_idx;
6292 else
6293 pending = AHC_TMODE_EVENT_BUFFER_SIZE + 1
6294 - (lstate->event_r_idx - lstate->event_w_idx);
6295
6296 if (event_type == EVENT_TYPE_BUS_RESET
6297 || event_type == MSG_BUS_DEV_RESET) {
6298 /*
6299 * Any earlier events are irrelevant, so reset our buffer.
6300 * This has the effect of allowing us to deal with reset
6301 * floods (an external device holding down the reset line)
6302 * without losing the event that is really interesting.
6303 */
6304 lstate->event_r_idx = 0;
6305 lstate->event_w_idx = 0;
6306 xpt_release_devq(lstate->path, pending, /*runqueue*/FALSE);
6307 }
6308
6309 if (pending == AHC_TMODE_EVENT_BUFFER_SIZE) {
6310 xpt_print_path(lstate->path);
6311 printf("immediate event %x:%x lost\n",
6312 lstate->event_buffer[lstate->event_r_idx].event_type,
6313 lstate->event_buffer[lstate->event_r_idx].event_arg);
6314 lstate->event_r_idx++;
6315 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6316 lstate->event_r_idx = 0;
6317 xpt_release_devq(lstate->path, /*count*/1, /*runqueue*/FALSE);
6318 }
6319
6320 event = &lstate->event_buffer[lstate->event_w_idx];
6321 event->initiator_id = initiator_id;
6322 event->event_type = event_type;
6323 event->event_arg = event_arg;
6324 lstate->event_w_idx++;
6325 if (lstate->event_w_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6326 lstate->event_w_idx = 0;
6327 }
6328
6329 /*
6330 * Send any target mode events queued up waiting
6331 * for immediate notify resources.
6332 */
6333 void
ahc_send_lstate_events(struct ahc_softc * ahc,struct ahc_tmode_lstate * lstate)6334 ahc_send_lstate_events(struct ahc_softc *ahc, struct ahc_tmode_lstate *lstate)
6335 {
6336 struct ccb_hdr *ccbh;
6337 struct ccb_immediate_notify *inot;
6338
6339 while (lstate->event_r_idx != lstate->event_w_idx
6340 && (ccbh = SLIST_FIRST(&lstate->immed_notifies)) != NULL) {
6341 struct ahc_tmode_event *event;
6342
6343 event = &lstate->event_buffer[lstate->event_r_idx];
6344 SLIST_REMOVE_HEAD(&lstate->immed_notifies, sim_links.sle);
6345 inot = (struct ccb_immediate_notify *)ccbh;
6346 switch (event->event_type) {
6347 case EVENT_TYPE_BUS_RESET:
6348 ccbh->status = CAM_SCSI_BUS_RESET|CAM_DEV_QFRZN;
6349 break;
6350 default:
6351 ccbh->status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
6352 inot->arg = event->event_type;
6353 inot->seq_id = event->event_arg;
6354 break;
6355 }
6356 inot->initiator_id = event->initiator_id;
6357 xpt_done((union ccb *)inot);
6358 lstate->event_r_idx++;
6359 if (lstate->event_r_idx == AHC_TMODE_EVENT_BUFFER_SIZE)
6360 lstate->event_r_idx = 0;
6361 }
6362 }
6363 #endif
6364
6365 /******************** Sequencer Program Patching/Download *********************/
6366
6367 #ifdef AHC_DUMP_SEQ
6368 void
ahc_dumpseq(struct ahc_softc * ahc)6369 ahc_dumpseq(struct ahc_softc* ahc)
6370 {
6371 int i;
6372
6373 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
6374 ahc_outb(ahc, SEQADDR0, 0);
6375 ahc_outb(ahc, SEQADDR1, 0);
6376 for (i = 0; i < ahc->instruction_ram_size; i++) {
6377 uint8_t ins_bytes[4];
6378
6379 ahc_insb(ahc, SEQRAM, ins_bytes, 4);
6380 printf("0x%08x\n", ins_bytes[0] << 24
6381 | ins_bytes[1] << 16
6382 | ins_bytes[2] << 8
6383 | ins_bytes[3]);
6384 }
6385 }
6386 #endif
6387
6388 static int
ahc_loadseq(struct ahc_softc * ahc)6389 ahc_loadseq(struct ahc_softc *ahc)
6390 {
6391 struct cs cs_table[num_critical_sections];
6392 u_int begin_set[num_critical_sections];
6393 u_int end_set[num_critical_sections];
6394 struct patch *cur_patch;
6395 u_int cs_count;
6396 u_int cur_cs;
6397 u_int i;
6398 u_int skip_addr;
6399 u_int sg_prefetch_cnt;
6400 int downloaded;
6401 uint8_t download_consts[7];
6402
6403 /*
6404 * Start out with 0 critical sections
6405 * that apply to this firmware load.
6406 */
6407 cs_count = 0;
6408 cur_cs = 0;
6409 memset(begin_set, 0, sizeof(begin_set));
6410 memset(end_set, 0, sizeof(end_set));
6411
6412 /* Setup downloadable constant table */
6413 download_consts[QOUTFIFO_OFFSET] = 0;
6414 if (ahc->targetcmds != NULL)
6415 download_consts[QOUTFIFO_OFFSET] += 32;
6416 download_consts[QINFIFO_OFFSET] = download_consts[QOUTFIFO_OFFSET] + 1;
6417 download_consts[CACHESIZE_MASK] = ahc->pci_cachesize - 1;
6418 download_consts[INVERTED_CACHESIZE_MASK] = ~(ahc->pci_cachesize - 1);
6419 sg_prefetch_cnt = ahc->pci_cachesize;
6420 if (sg_prefetch_cnt < (2 * sizeof(struct ahc_dma_seg)))
6421 sg_prefetch_cnt = 2 * sizeof(struct ahc_dma_seg);
6422 download_consts[SG_PREFETCH_CNT] = sg_prefetch_cnt;
6423 download_consts[SG_PREFETCH_ALIGN_MASK] = ~(sg_prefetch_cnt - 1);
6424 download_consts[SG_PREFETCH_ADDR_MASK] = (sg_prefetch_cnt - 1);
6425
6426 cur_patch = patches;
6427 downloaded = 0;
6428 skip_addr = 0;
6429 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM);
6430 ahc_outb(ahc, SEQADDR0, 0);
6431 ahc_outb(ahc, SEQADDR1, 0);
6432
6433 for (i = 0; i < sizeof(seqprog)/4; i++) {
6434 if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) {
6435 /*
6436 * Don't download this instruction as it
6437 * is in a patch that was removed.
6438 */
6439 continue;
6440 }
6441
6442 if (downloaded == ahc->instruction_ram_size) {
6443 /*
6444 * We're about to exceed the instruction
6445 * storage capacity for this chip. Fail
6446 * the load.
6447 */
6448 printf("\n%s: Program too large for instruction memory "
6449 "size of %d!\n", ahc_name(ahc),
6450 ahc->instruction_ram_size);
6451 return (ENOMEM);
6452 }
6453
6454 /*
6455 * Move through the CS table until we find a CS
6456 * that might apply to this instruction.
6457 */
6458 for (; cur_cs < num_critical_sections; cur_cs++) {
6459 if (critical_sections[cur_cs].end <= i) {
6460 if (begin_set[cs_count] == TRUE
6461 && end_set[cs_count] == FALSE) {
6462 cs_table[cs_count].end = downloaded;
6463 end_set[cs_count] = TRUE;
6464 cs_count++;
6465 }
6466 continue;
6467 }
6468 if (critical_sections[cur_cs].begin <= i
6469 && begin_set[cs_count] == FALSE) {
6470 cs_table[cs_count].begin = downloaded;
6471 begin_set[cs_count] = TRUE;
6472 }
6473 break;
6474 }
6475 ahc_download_instr(ahc, i, download_consts);
6476 downloaded++;
6477 }
6478
6479 ahc->num_critical_sections = cs_count;
6480 if (cs_count != 0) {
6481 cs_count *= sizeof(struct cs);
6482 ahc->critical_sections = malloc(cs_count, M_DEVBUF, M_NOWAIT);
6483 if (ahc->critical_sections == NULL)
6484 panic("ahc_loadseq: Could not malloc");
6485 memcpy(ahc->critical_sections, cs_table, cs_count);
6486 }
6487 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE);
6488
6489 if (bootverbose) {
6490 printf(" %d instructions downloaded\n", downloaded);
6491 printf("%s: Features 0x%x, Bugs 0x%x, Flags 0x%x\n",
6492 ahc_name(ahc), ahc->features, ahc->bugs, ahc->flags);
6493 }
6494 return (0);
6495 }
6496
6497 static int
ahc_check_patch(struct ahc_softc * ahc,struct patch ** start_patch,u_int start_instr,u_int * skip_addr)6498 ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch,
6499 u_int start_instr, u_int *skip_addr)
6500 {
6501 struct patch *cur_patch;
6502 struct patch *last_patch;
6503 u_int num_patches;
6504
6505 num_patches = sizeof(patches)/sizeof(struct patch);
6506 last_patch = &patches[num_patches];
6507 cur_patch = *start_patch;
6508
6509 while (cur_patch < last_patch && start_instr == cur_patch->begin) {
6510 if (cur_patch->patch_func(ahc) == 0) {
6511 /* Start rejecting code */
6512 *skip_addr = start_instr + cur_patch->skip_instr;
6513 cur_patch += cur_patch->skip_patch;
6514 } else {
6515 /* Accepted this patch. Advance to the next
6516 * one and wait for our instruction pointer to
6517 * hit this point.
6518 */
6519 cur_patch++;
6520 }
6521 }
6522
6523 *start_patch = cur_patch;
6524 if (start_instr < *skip_addr)
6525 /* Still skipping */
6526 return (0);
6527
6528 return (1);
6529 }
6530
6531 static void
ahc_download_instr(struct ahc_softc * ahc,u_int instrptr,uint8_t * dconsts)6532 ahc_download_instr(struct ahc_softc *ahc, u_int instrptr, uint8_t *dconsts)
6533 {
6534 union ins_formats instr;
6535 struct ins_format1 *fmt1_ins;
6536 struct ins_format3 *fmt3_ins;
6537 u_int opcode;
6538
6539 /*
6540 * The firmware is always compiled into a little endian format.
6541 */
6542 instr.integer = aic_le32toh(*(uint32_t*)&seqprog[instrptr * 4]);
6543
6544 fmt1_ins = &instr.format1;
6545 fmt3_ins = NULL;
6546
6547 /* Pull the opcode */
6548 opcode = instr.format1.opcode;
6549 switch (opcode) {
6550 case AIC_OP_JMP:
6551 case AIC_OP_JC:
6552 case AIC_OP_JNC:
6553 case AIC_OP_CALL:
6554 case AIC_OP_JNE:
6555 case AIC_OP_JNZ:
6556 case AIC_OP_JE:
6557 case AIC_OP_JZ:
6558 {
6559 struct patch *cur_patch;
6560 int address_offset;
6561 u_int address;
6562 u_int skip_addr;
6563 u_int i;
6564
6565 fmt3_ins = &instr.format3;
6566 address_offset = 0;
6567 address = fmt3_ins->address;
6568 cur_patch = patches;
6569 skip_addr = 0;
6570
6571 for (i = 0; i < address;) {
6572 ahc_check_patch(ahc, &cur_patch, i, &skip_addr);
6573
6574 if (skip_addr > i) {
6575 int end_addr;
6576
6577 end_addr = MIN(address, skip_addr);
6578 address_offset += end_addr - i;
6579 i = skip_addr;
6580 } else {
6581 i++;
6582 }
6583 }
6584 address -= address_offset;
6585 fmt3_ins->address = address;
6586 /* FALLTHROUGH */
6587 }
6588 case AIC_OP_OR:
6589 case AIC_OP_AND:
6590 case AIC_OP_XOR:
6591 case AIC_OP_ADD:
6592 case AIC_OP_ADC:
6593 case AIC_OP_BMOV:
6594 if (fmt1_ins->parity != 0) {
6595 fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
6596 }
6597 fmt1_ins->parity = 0;
6598 if ((ahc->features & AHC_CMD_CHAN) == 0
6599 && opcode == AIC_OP_BMOV) {
6600 /*
6601 * Block move was added at the same time
6602 * as the command channel. Verify that
6603 * this is only a move of a single element
6604 * and convert the BMOV to a MOV
6605 * (AND with an immediate of FF).
6606 */
6607 if (fmt1_ins->immediate != 1)
6608 panic("%s: BMOV not supported\n",
6609 ahc_name(ahc));
6610 fmt1_ins->opcode = AIC_OP_AND;
6611 fmt1_ins->immediate = 0xff;
6612 }
6613 /* FALLTHROUGH */
6614 case AIC_OP_ROL:
6615 if ((ahc->features & AHC_ULTRA2) != 0) {
6616 int i, count;
6617
6618 /* Calculate odd parity for the instruction */
6619 for (i = 0, count = 0; i < 31; i++) {
6620 uint32_t mask;
6621
6622 mask = 0x01 << i;
6623 if ((instr.integer & mask) != 0)
6624 count++;
6625 }
6626 if ((count & 0x01) == 0)
6627 instr.format1.parity = 1;
6628 } else {
6629 /* Compress the instruction for older sequencers */
6630 if (fmt3_ins != NULL) {
6631 instr.integer =
6632 fmt3_ins->immediate
6633 | (fmt3_ins->source << 8)
6634 | (fmt3_ins->address << 16)
6635 | (fmt3_ins->opcode << 25);
6636 } else {
6637 instr.integer =
6638 fmt1_ins->immediate
6639 | (fmt1_ins->source << 8)
6640 | (fmt1_ins->destination << 16)
6641 | (fmt1_ins->ret << 24)
6642 | (fmt1_ins->opcode << 25);
6643 }
6644 }
6645 /* The sequencer is a little endian cpu */
6646 instr.integer = aic_htole32(instr.integer);
6647 ahc_outsb(ahc, SEQRAM, instr.bytes, 4);
6648 break;
6649 default:
6650 panic("Unknown opcode encountered in seq program");
6651 break;
6652 }
6653 }
6654
6655 int
ahc_print_register(ahc_reg_parse_entry_t * table,u_int num_entries,const char * name,u_int address,u_int value,u_int * cur_column,u_int wrap_point)6656 ahc_print_register(ahc_reg_parse_entry_t *table, u_int num_entries,
6657 const char *name, u_int address, u_int value,
6658 u_int *cur_column, u_int wrap_point)
6659 {
6660 int printed;
6661 u_int printed_mask;
6662 u_int dummy_column;
6663
6664 if (cur_column == NULL) {
6665 dummy_column = 0;
6666 cur_column = &dummy_column;
6667 }
6668
6669 if (*cur_column >= wrap_point) {
6670 printf("\n");
6671 *cur_column = 0;
6672 }
6673 printed = printf("%s[0x%x]", name, value);
6674 if (table == NULL) {
6675 printed += printf(" ");
6676 *cur_column += printed;
6677 return (printed);
6678 }
6679 printed_mask = 0;
6680 while (printed_mask != 0xFF) {
6681 int entry;
6682
6683 for (entry = 0; entry < num_entries; entry++) {
6684 if (((value & table[entry].mask)
6685 != table[entry].value)
6686 || ((printed_mask & table[entry].mask)
6687 == table[entry].mask))
6688 continue;
6689
6690 printed += printf("%s%s",
6691 printed_mask == 0 ? ":(" : "|",
6692 table[entry].name);
6693 printed_mask |= table[entry].mask;
6694
6695 break;
6696 }
6697 if (entry >= num_entries)
6698 break;
6699 }
6700 if (printed_mask != 0)
6701 printed += printf(") ");
6702 else
6703 printed += printf(" ");
6704 if (cur_column != NULL)
6705 *cur_column += printed;
6706 return (printed);
6707 }
6708
6709 void
ahc_dump_card_state(struct ahc_softc * ahc)6710 ahc_dump_card_state(struct ahc_softc *ahc)
6711 {
6712 struct scb *scb;
6713 struct scb_tailq *untagged_q;
6714 u_int cur_col;
6715 int paused;
6716 int target;
6717 int maxtarget;
6718 int i;
6719 uint8_t last_phase;
6720 uint8_t qinpos;
6721 uint8_t qintail;
6722 uint8_t qoutpos;
6723 uint8_t scb_index;
6724 uint8_t saved_scbptr;
6725
6726 if (ahc_is_paused(ahc)) {
6727 paused = 1;
6728 } else {
6729 paused = 0;
6730 ahc_pause(ahc);
6731 }
6732
6733 saved_scbptr = ahc_inb(ahc, SCBPTR);
6734 last_phase = ahc_inb(ahc, LASTPHASE);
6735 printf(">>>>>>>>>>>>>>>>>> Dump Card State Begins <<<<<<<<<<<<<<<<<\n"
6736 "%s: Dumping Card State %s, at SEQADDR 0x%x\n",
6737 ahc_name(ahc), ahc_lookup_phase_entry(last_phase)->phasemsg,
6738 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8));
6739 if (paused)
6740 printf("Card was paused\n");
6741 printf("ACCUM = 0x%x, SINDEX = 0x%x, DINDEX = 0x%x, ARG_2 = 0x%x\n",
6742 ahc_inb(ahc, ACCUM), ahc_inb(ahc, SINDEX), ahc_inb(ahc, DINDEX),
6743 ahc_inb(ahc, ARG_2));
6744 printf("HCNT = 0x%x SCBPTR = 0x%x\n", ahc_inb(ahc, HCNT),
6745 ahc_inb(ahc, SCBPTR));
6746 cur_col = 0;
6747 if ((ahc->features & AHC_DT) != 0)
6748 ahc_scsiphase_print(ahc_inb(ahc, SCSIPHASE), &cur_col, 50);
6749 ahc_scsisigi_print(ahc_inb(ahc, SCSISIGI), &cur_col, 50);
6750 ahc_error_print(ahc_inb(ahc, ERROR), &cur_col, 50);
6751 ahc_scsibusl_print(ahc_inb(ahc, SCSIBUSL), &cur_col, 50);
6752 ahc_lastphase_print(ahc_inb(ahc, LASTPHASE), &cur_col, 50);
6753 ahc_scsiseq_print(ahc_inb(ahc, SCSISEQ), &cur_col, 50);
6754 ahc_sblkctl_print(ahc_inb(ahc, SBLKCTL), &cur_col, 50);
6755 ahc_scsirate_print(ahc_inb(ahc, SCSIRATE), &cur_col, 50);
6756 ahc_seqctl_print(ahc_inb(ahc, SEQCTL), &cur_col, 50);
6757 ahc_seq_flags_print(ahc_inb(ahc, SEQ_FLAGS), &cur_col, 50);
6758 ahc_sstat0_print(ahc_inb(ahc, SSTAT0), &cur_col, 50);
6759 ahc_sstat1_print(ahc_inb(ahc, SSTAT1), &cur_col, 50);
6760 ahc_sstat2_print(ahc_inb(ahc, SSTAT2), &cur_col, 50);
6761 ahc_sstat3_print(ahc_inb(ahc, SSTAT3), &cur_col, 50);
6762 ahc_simode0_print(ahc_inb(ahc, SIMODE0), &cur_col, 50);
6763 ahc_simode1_print(ahc_inb(ahc, SIMODE1), &cur_col, 50);
6764 ahc_sxfrctl0_print(ahc_inb(ahc, SXFRCTL0), &cur_col, 50);
6765 ahc_dfcntrl_print(ahc_inb(ahc, DFCNTRL), &cur_col, 50);
6766 ahc_dfstatus_print(ahc_inb(ahc, DFSTATUS), &cur_col, 50);
6767 if (cur_col != 0)
6768 printf("\n");
6769 printf("STACK:");
6770 for (i = 0; i < STACK_SIZE; i++)
6771 printf(" 0x%x", ahc_inb(ahc, STACK)|(ahc_inb(ahc, STACK) << 8));
6772 printf("\nSCB count = %d\n", ahc->scb_data->numscbs);
6773 printf("Kernel NEXTQSCB = %d\n", ahc->next_queued_scb->hscb->tag);
6774 printf("Card NEXTQSCB = %d\n", ahc_inb(ahc, NEXT_QUEUED_SCB));
6775 /* QINFIFO */
6776 printf("QINFIFO entries: ");
6777 if ((ahc->features & AHC_QUEUE_REGS) != 0) {
6778 qinpos = ahc_inb(ahc, SNSCB_QOFF);
6779 ahc_outb(ahc, SNSCB_QOFF, qinpos);
6780 } else
6781 qinpos = ahc_inb(ahc, QINPOS);
6782 qintail = ahc->qinfifonext;
6783 while (qinpos != qintail) {
6784 printf("%d ", ahc->qinfifo[qinpos]);
6785 qinpos++;
6786 }
6787 printf("\n");
6788
6789 printf("Waiting Queue entries: ");
6790 scb_index = ahc_inb(ahc, WAITING_SCBH);
6791 i = 0;
6792 while (scb_index != SCB_LIST_NULL && i++ < 256) {
6793 ahc_outb(ahc, SCBPTR, scb_index);
6794 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
6795 scb_index = ahc_inb(ahc, SCB_NEXT);
6796 }
6797 printf("\n");
6798
6799 printf("Disconnected Queue entries: ");
6800 scb_index = ahc_inb(ahc, DISCONNECTED_SCBH);
6801 i = 0;
6802 while (scb_index != SCB_LIST_NULL && i++ < 256) {
6803 ahc_outb(ahc, SCBPTR, scb_index);
6804 printf("%d:%d ", scb_index, ahc_inb(ahc, SCB_TAG));
6805 scb_index = ahc_inb(ahc, SCB_NEXT);
6806 }
6807 printf("\n");
6808
6809 ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
6810 printf("QOUTFIFO entries: ");
6811 qoutpos = ahc->qoutfifonext;
6812 i = 0;
6813 while (ahc->qoutfifo[qoutpos] != SCB_LIST_NULL && i++ < 256) {
6814 printf("%d ", ahc->qoutfifo[qoutpos]);
6815 qoutpos++;
6816 }
6817 printf("\n");
6818
6819 printf("Sequencer Free SCB List: ");
6820 scb_index = ahc_inb(ahc, FREE_SCBH);
6821 i = 0;
6822 while (scb_index != SCB_LIST_NULL && i++ < 256) {
6823 ahc_outb(ahc, SCBPTR, scb_index);
6824 printf("%d ", scb_index);
6825 scb_index = ahc_inb(ahc, SCB_NEXT);
6826 }
6827 printf("\n");
6828
6829 printf("Sequencer SCB Info: ");
6830 for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
6831 ahc_outb(ahc, SCBPTR, i);
6832 cur_col = printf("\n%3d ", i);
6833
6834 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL), &cur_col, 60);
6835 ahc_scb_scsiid_print(ahc_inb(ahc, SCB_SCSIID), &cur_col, 60);
6836 ahc_scb_lun_print(ahc_inb(ahc, SCB_LUN), &cur_col, 60);
6837 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
6838 }
6839 printf("\n");
6840
6841 printf("Pending list: ");
6842 i = 0;
6843 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
6844 if (i++ > 256)
6845 break;
6846 cur_col = printf("\n%3d ", scb->hscb->tag);
6847 ahc_scb_control_print(scb->hscb->control, &cur_col, 60);
6848 ahc_scb_scsiid_print(scb->hscb->scsiid, &cur_col, 60);
6849 ahc_scb_lun_print(scb->hscb->lun, &cur_col, 60);
6850 if ((ahc->flags & AHC_PAGESCBS) == 0) {
6851 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
6852 printf("(");
6853 ahc_scb_control_print(ahc_inb(ahc, SCB_CONTROL),
6854 &cur_col, 60);
6855 ahc_scb_tag_print(ahc_inb(ahc, SCB_TAG), &cur_col, 60);
6856 printf(")");
6857 }
6858 }
6859 printf("\n");
6860
6861 printf("Kernel Free SCB list: ");
6862 i = 0;
6863 SLIST_FOREACH(scb, &ahc->scb_data->free_scbs, links.sle) {
6864 if (i++ > 256)
6865 break;
6866 printf("%d ", scb->hscb->tag);
6867 }
6868 printf("\n");
6869
6870 maxtarget = (ahc->features & (AHC_WIDE|AHC_TWIN)) ? 15 : 7;
6871 for (target = 0; target <= maxtarget; target++) {
6872 untagged_q = &ahc->untagged_queues[target];
6873 if (TAILQ_FIRST(untagged_q) == NULL)
6874 continue;
6875 printf("Untagged Q(%d): ", target);
6876 i = 0;
6877 TAILQ_FOREACH(scb, untagged_q, links.tqe) {
6878 if (i++ > 256)
6879 break;
6880 printf("%d ", scb->hscb->tag);
6881 }
6882 printf("\n");
6883 }
6884
6885 ahc_platform_dump_card_state(ahc);
6886 printf("\n<<<<<<<<<<<<<<<<< Dump Card State Ends >>>>>>>>>>>>>>>>>>\n");
6887 ahc_outb(ahc, SCBPTR, saved_scbptr);
6888 if (paused == 0)
6889 ahc_unpause(ahc);
6890 }
6891
6892 /*************************** Timeout Handling *********************************/
6893 void
ahc_timeout(struct scb * scb)6894 ahc_timeout(struct scb *scb)
6895 {
6896 struct ahc_softc *ahc;
6897
6898 ahc = scb->ahc_softc;
6899 if ((scb->flags & SCB_ACTIVE) != 0) {
6900 if ((scb->flags & SCB_TIMEDOUT) == 0) {
6901 LIST_INSERT_HEAD(&ahc->timedout_scbs, scb,
6902 timedout_links);
6903 scb->flags |= SCB_TIMEDOUT;
6904 }
6905 ahc_wakeup_recovery_thread(ahc);
6906 }
6907 }
6908
6909 /*
6910 * Re-schedule a timeout for the passed in SCB if we determine that some
6911 * other SCB is in the process of recovery or an SCB with a longer
6912 * timeout is still pending. Limit our search to just "other_scb"
6913 * if it is non-NULL.
6914 */
6915 static int
ahc_other_scb_timeout(struct ahc_softc * ahc,struct scb * scb,struct scb * other_scb)6916 ahc_other_scb_timeout(struct ahc_softc *ahc, struct scb *scb,
6917 struct scb *other_scb)
6918 {
6919 u_int newtimeout;
6920 int found;
6921
6922 ahc_print_path(ahc, scb);
6923 printf("Other SCB Timeout%s",
6924 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
6925 ? " again\n" : "\n");
6926
6927 newtimeout = aic_get_timeout(scb);
6928 scb->flags |= SCB_OTHERTCL_TIMEOUT;
6929 found = 0;
6930 if (other_scb != NULL) {
6931 if ((other_scb->flags
6932 & (SCB_OTHERTCL_TIMEOUT|SCB_TIMEDOUT)) == 0
6933 || (other_scb->flags & SCB_RECOVERY_SCB) != 0) {
6934 found++;
6935 newtimeout = MAX(aic_get_timeout(other_scb),
6936 newtimeout);
6937 }
6938 } else {
6939 LIST_FOREACH(other_scb, &ahc->pending_scbs, pending_links) {
6940 if ((other_scb->flags
6941 & (SCB_OTHERTCL_TIMEOUT|SCB_TIMEDOUT)) == 0
6942 || (other_scb->flags & SCB_RECOVERY_SCB) != 0) {
6943 found++;
6944 newtimeout =
6945 MAX(aic_get_timeout(other_scb),
6946 newtimeout);
6947 }
6948 }
6949 }
6950
6951 if (found != 0)
6952 aic_scb_timer_reset(scb, newtimeout);
6953 else {
6954 ahc_print_path(ahc, scb);
6955 printf("No other SCB worth waiting for...\n");
6956 }
6957
6958 return (found != 0);
6959 }
6960
6961 /*
6962 * ahc_recover_commands determines if any of the commands that have currently
6963 * timedout are the root cause for this timeout. Innocent commands are given
6964 * a new timeout while we wait for the command executing on the bus to timeout.
6965 * This routine is invoked from a thread context so we are allowed to sleep.
6966 * Our lock is not held on entry.
6967 */
6968 void
ahc_recover_commands(struct ahc_softc * ahc)6969 ahc_recover_commands(struct ahc_softc *ahc)
6970 {
6971 struct scb *scb;
6972 int found;
6973 int restart_needed;
6974 u_int last_phase;
6975
6976 /*
6977 * Pause the controller and manually flush any
6978 * commands that have just completed but that our
6979 * interrupt handler has yet to see.
6980 */
6981 ahc_pause_and_flushwork(ahc);
6982
6983 if (LIST_EMPTY(&ahc->timedout_scbs) != 0) {
6984 /*
6985 * The timedout commands have already
6986 * completed. This typically means
6987 * that either the timeout value was on
6988 * the hairy edge of what the device
6989 * requires or - more likely - interrupts
6990 * are not happening.
6991 */
6992 printf("%s: Timedout SCBs already complete. "
6993 "Interrupts may not be functioning.\n", ahc_name(ahc));
6994 ahc_unpause(ahc);
6995 return;
6996 }
6997
6998 restart_needed = 0;
6999 printf("%s: Recovery Initiated\n", ahc_name(ahc));
7000 ahc_dump_card_state(ahc);
7001
7002 last_phase = ahc_inb(ahc, LASTPHASE);
7003 while ((scb = LIST_FIRST(&ahc->timedout_scbs)) != NULL) {
7004 u_int active_scb_index;
7005 u_int saved_scbptr;
7006 int target;
7007 int lun;
7008 int i;
7009 char channel;
7010
7011 target = SCB_GET_TARGET(ahc, scb);
7012 channel = SCB_GET_CHANNEL(ahc, scb);
7013 lun = SCB_GET_LUN(scb);
7014
7015 ahc_print_path(ahc, scb);
7016 printf("SCB 0x%x - timed out\n", scb->hscb->tag);
7017 if (scb->sg_count > 0) {
7018 for (i = 0; i < scb->sg_count; i++) {
7019 printf("sg[%d] - Addr 0x%x : Length %d\n",
7020 i,
7021 scb->sg_list[i].addr,
7022 scb->sg_list[i].len & AHC_SG_LEN_MASK);
7023 }
7024 }
7025 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
7026 /*
7027 * Been down this road before.
7028 * Do a full bus reset.
7029 */
7030 aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
7031 bus_reset:
7032 found = ahc_reset_channel(ahc, channel,
7033 /*Initiate Reset*/TRUE);
7034 printf("%s: Issued Channel %c Bus Reset. "
7035 "%d SCBs aborted\n", ahc_name(ahc), channel,
7036 found);
7037 continue;
7038 }
7039
7040 /*
7041 * Remove the command from the timedout list in
7042 * preparation for requeing it.
7043 */
7044 LIST_REMOVE(scb, timedout_links);
7045 scb->flags &= ~SCB_TIMEDOUT;
7046
7047 /*
7048 * If we are a target, transition to bus free and report
7049 * the timeout.
7050 *
7051 * The target/initiator that is holding up the bus may not
7052 * be the same as the one that triggered this timeout
7053 * (different commands have different timeout lengths).
7054 * If the bus is idle and we are actiing as the initiator
7055 * for this request, queue a BDR message to the timed out
7056 * target. Otherwise, if the timed out transaction is
7057 * active:
7058 * Initiator transaction:
7059 * Stuff the message buffer with a BDR message and assert
7060 * ATN in the hopes that the target will let go of the bus
7061 * and go to the mesgout phase. If this fails, we'll
7062 * get another timeout 2 seconds later which will attempt
7063 * a bus reset.
7064 *
7065 * Target transaction:
7066 * Transition to BUS FREE and report the error.
7067 * It's good to be the target!
7068 */
7069 saved_scbptr = ahc_inb(ahc, SCBPTR);
7070 active_scb_index = ahc_inb(ahc, SCB_TAG);
7071
7072 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
7073 && (active_scb_index < ahc->scb_data->numscbs)) {
7074 struct scb *active_scb;
7075
7076 /*
7077 * If the active SCB is not us, assume that
7078 * the active SCB has a longer timeout than
7079 * the timedout SCB, and wait for the active
7080 * SCB to timeout.
7081 */
7082 active_scb = ahc_lookup_scb(ahc, active_scb_index);
7083 if (active_scb != scb) {
7084 if (ahc_other_scb_timeout(ahc, scb,
7085 active_scb) == 0)
7086 goto bus_reset;
7087 continue;
7088 }
7089
7090 /* It's us */
7091 if ((scb->flags & SCB_TARGET_SCB) != 0) {
7092 /*
7093 * Send back any queued up transactions
7094 * and properly record the error condition.
7095 */
7096 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
7097 SCB_GET_CHANNEL(ahc, scb),
7098 SCB_GET_LUN(scb),
7099 scb->hscb->tag,
7100 ROLE_TARGET,
7101 CAM_CMD_TIMEOUT);
7102
7103 /* Will clear us from the bus */
7104 restart_needed = 1;
7105 break;
7106 }
7107
7108 ahc_set_recoveryscb(ahc, active_scb);
7109 ahc_outb(ahc, MSG_OUT, HOST_MSG);
7110 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
7111 ahc_print_path(ahc, active_scb);
7112 printf("BDR message in message buffer\n");
7113 active_scb->flags |= SCB_DEVICE_RESET;
7114 aic_scb_timer_reset(scb, 2 * 1000);
7115 } else if (last_phase != P_BUSFREE
7116 && (ahc_inb(ahc, SSTAT1) & REQINIT) == 0) {
7117 /*
7118 * SCB is not identified, there
7119 * is no pending REQ, and the sequencer
7120 * has not seen a busfree. Looks like
7121 * a stuck connection waiting to
7122 * go busfree. Reset the bus.
7123 */
7124 printf("%s: Connection stuck awaiting busfree or "
7125 "Identify Msg.\n", ahc_name(ahc));
7126 goto bus_reset;
7127 } else {
7128 int disconnected;
7129
7130 if (last_phase != P_BUSFREE
7131 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
7132 /* Hung target selection. Goto busfree */
7133 printf("%s: Hung target selection\n",
7134 ahc_name(ahc));
7135 restart_needed = 1;
7136 break;
7137 }
7138
7139 /* XXX Shouldn't panic. Just punt instead? */
7140 if ((scb->flags & SCB_TARGET_SCB) != 0)
7141 panic("Timed-out target SCB but bus idle");
7142
7143 if (ahc_search_qinfifo(ahc, target, channel, lun,
7144 scb->hscb->tag, ROLE_INITIATOR,
7145 /*status*/0, SEARCH_COUNT) > 0) {
7146 disconnected = FALSE;
7147 } else {
7148 disconnected = TRUE;
7149 }
7150
7151 if (disconnected) {
7152 ahc_set_recoveryscb(ahc, scb);
7153 /*
7154 * Actually re-queue this SCB in an attempt
7155 * to select the device before it reconnects.
7156 * In either case (selection or reselection),
7157 * we will now issue a target reset to the
7158 * timed-out device.
7159 *
7160 * Set the MK_MESSAGE control bit indicating
7161 * that we desire to send a message. We
7162 * also set the disconnected flag since
7163 * in the paging case there is no guarantee
7164 * that our SCB control byte matches the
7165 * version on the card. We don't want the
7166 * sequencer to abort the command thinking
7167 * an unsolicited reselection occurred.
7168 */
7169 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
7170 scb->flags |= SCB_DEVICE_RESET;
7171
7172 /*
7173 * Remove any cached copy of this SCB in the
7174 * disconnected list in preparation for the
7175 * queuing of our abort SCB. We use the
7176 * same element in the SCB, SCB_NEXT, for
7177 * both the qinfifo and the disconnected list.
7178 */
7179 ahc_search_disc_list(ahc, target, channel,
7180 lun, scb->hscb->tag,
7181 /*stop_on_first*/TRUE,
7182 /*remove*/TRUE,
7183 /*save_state*/FALSE);
7184
7185 /*
7186 * In the non-paging case, the sequencer will
7187 * never re-reference the in-core SCB.
7188 * To make sure we are notified during
7189 * reselection, set the MK_MESSAGE flag in
7190 * the card's copy of the SCB.
7191 */
7192 if ((ahc->flags & AHC_PAGESCBS) == 0) {
7193 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
7194 ahc_outb(ahc, SCB_CONTROL,
7195 ahc_inb(ahc, SCB_CONTROL)
7196 | MK_MESSAGE);
7197 }
7198
7199 /*
7200 * Clear out any entries in the QINFIFO first
7201 * so we are the next SCB for this target
7202 * to run.
7203 */
7204 ahc_search_qinfifo(ahc,
7205 SCB_GET_TARGET(ahc, scb),
7206 channel, SCB_GET_LUN(scb),
7207 SCB_LIST_NULL,
7208 ROLE_INITIATOR,
7209 CAM_REQUEUE_REQ,
7210 SEARCH_COMPLETE);
7211 ahc_print_path(ahc, scb);
7212 printf("Queuing a BDR SCB\n");
7213 ahc_qinfifo_requeue_tail(ahc, scb);
7214 ahc_outb(ahc, SCBPTR, saved_scbptr);
7215 aic_scb_timer_reset(scb, 2 * 1000);
7216 } else {
7217 /* Go "immediately" to the bus reset */
7218 /* This shouldn't happen */
7219 ahc_set_recoveryscb(ahc, scb);
7220 ahc_print_path(ahc, scb);
7221 printf("SCB %d: Immediate reset. "
7222 "Flags = 0x%x\n", scb->hscb->tag,
7223 scb->flags);
7224 goto bus_reset;
7225 }
7226 }
7227 break;
7228 }
7229
7230 /*
7231 * Any remaining SCBs were not the "culprit", so remove
7232 * them from the timeout list. The timer for these commands
7233 * will be reset once the recovery SCB completes.
7234 */
7235 while ((scb = LIST_FIRST(&ahc->timedout_scbs)) != NULL) {
7236 LIST_REMOVE(scb, timedout_links);
7237 scb->flags &= ~SCB_TIMEDOUT;
7238 }
7239
7240 if (restart_needed)
7241 ahc_restart(ahc);
7242 else
7243 ahc_unpause(ahc);
7244 }
7245
7246 /************************* Target Mode ****************************************/
7247 #ifdef AHC_TARGET_MODE
7248 cam_status
ahc_find_tmode_devs(struct ahc_softc * ahc,struct cam_sim * sim,union ccb * ccb,struct ahc_tmode_tstate ** tstate,struct ahc_tmode_lstate ** lstate,int notfound_failure)7249 ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb,
7250 struct ahc_tmode_tstate **tstate,
7251 struct ahc_tmode_lstate **lstate,
7252 int notfound_failure)
7253 {
7254
7255 if ((ahc->features & AHC_TARGETMODE) == 0)
7256 return (CAM_REQ_INVALID);
7257
7258 /*
7259 * Handle the 'black hole' device that sucks up
7260 * requests to unattached luns on enabled targets.
7261 */
7262 if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD
7263 && ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
7264 *tstate = NULL;
7265 *lstate = ahc->black_hole;
7266 } else {
7267 u_int max_id;
7268
7269 max_id = (ahc->features & AHC_WIDE) ? 15 : 7;
7270 if (ccb->ccb_h.target_id > max_id)
7271 return (CAM_TID_INVALID);
7272
7273 if (ccb->ccb_h.target_lun >= AHC_NUM_LUNS)
7274 return (CAM_LUN_INVALID);
7275
7276 *tstate = ahc->enabled_targets[ccb->ccb_h.target_id];
7277 *lstate = NULL;
7278 if (*tstate != NULL)
7279 *lstate =
7280 (*tstate)->enabled_luns[ccb->ccb_h.target_lun];
7281 }
7282
7283 if (notfound_failure != 0 && *lstate == NULL)
7284 return (CAM_PATH_INVALID);
7285
7286 return (CAM_REQ_CMP);
7287 }
7288
7289 void
ahc_handle_en_lun(struct ahc_softc * ahc,struct cam_sim * sim,union ccb * ccb)7290 ahc_handle_en_lun(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
7291 {
7292 struct ahc_tmode_tstate *tstate;
7293 struct ahc_tmode_lstate *lstate;
7294 struct ccb_en_lun *cel;
7295 cam_status status;
7296 u_int target;
7297 u_int lun;
7298 u_int target_mask;
7299 u_int our_id;
7300 int error;
7301 char channel;
7302
7303 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate,
7304 /*notfound_failure*/FALSE);
7305
7306 if (status != CAM_REQ_CMP) {
7307 ccb->ccb_h.status = status;
7308 return;
7309 }
7310
7311 if (cam_sim_bus(sim) == 0)
7312 our_id = ahc->our_id;
7313 else
7314 our_id = ahc->our_id_b;
7315
7316 if (ccb->ccb_h.target_id != our_id
7317 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
7318 /*
7319 * our_id represents our initiator ID, or
7320 * the ID of the first target to have an
7321 * enabled lun in target mode. There are
7322 * two cases that may preclude enabling a
7323 * target id other than our_id.
7324 *
7325 * o our_id is for an active initiator role.
7326 * Since the hardware does not support
7327 * reselections to the initiator role at
7328 * anything other than our_id, and our_id
7329 * is used by the hardware to indicate the
7330 * ID to use for both select-out and
7331 * reselect-out operations, the only target
7332 * ID we can support in this mode is our_id.
7333 *
7334 * o The MULTARGID feature is not available and
7335 * a previous target mode ID has been enabled.
7336 */
7337 if ((ahc->features & AHC_MULTIROLE) != 0) {
7338 if ((ahc->features & AHC_MULTI_TID) != 0
7339 && (ahc->flags & AHC_INITIATORROLE) != 0) {
7340 /*
7341 * Only allow additional targets if
7342 * the initiator role is disabled.
7343 * The hardware cannot handle a re-select-in
7344 * on the initiator id during a re-select-out
7345 * on a different target id.
7346 */
7347 status = CAM_TID_INVALID;
7348 } else if ((ahc->flags & AHC_INITIATORROLE) != 0
7349 || ahc->enabled_luns > 0) {
7350 /*
7351 * Only allow our target id to change
7352 * if the initiator role is not configured
7353 * and there are no enabled luns which
7354 * are attached to the currently registered
7355 * scsi id.
7356 */
7357 status = CAM_TID_INVALID;
7358 }
7359 } else if ((ahc->features & AHC_MULTI_TID) == 0
7360 && ahc->enabled_luns > 0) {
7361 status = CAM_TID_INVALID;
7362 }
7363 }
7364
7365 if (status != CAM_REQ_CMP) {
7366 ccb->ccb_h.status = status;
7367 return;
7368 }
7369
7370 /*
7371 * We now have an id that is valid.
7372 * If we aren't in target mode, switch modes.
7373 */
7374 if ((ahc->flags & AHC_TARGETROLE) == 0
7375 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
7376 ahc_flag saved_flags;
7377
7378 printf("Configuring Target Mode\n");
7379 if (LIST_FIRST(&ahc->pending_scbs) != NULL) {
7380 ccb->ccb_h.status = CAM_BUSY;
7381 return;
7382 }
7383 saved_flags = ahc->flags;
7384 ahc->flags |= AHC_TARGETROLE;
7385 if ((ahc->features & AHC_MULTIROLE) == 0)
7386 ahc->flags &= ~AHC_INITIATORROLE;
7387 ahc_pause(ahc);
7388 error = ahc_loadseq(ahc);
7389 if (error != 0) {
7390 /*
7391 * Restore original configuration and notify
7392 * the caller that we cannot support target mode.
7393 * Since the adapter started out in this
7394 * configuration, the firmware load will succeed,
7395 * so there is no point in checking ahc_loadseq's
7396 * return value.
7397 */
7398 ahc->flags = saved_flags;
7399 (void)ahc_loadseq(ahc);
7400 ahc_restart(ahc);
7401 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
7402 return;
7403 }
7404 ahc_restart(ahc);
7405 }
7406 cel = &ccb->cel;
7407 target = ccb->ccb_h.target_id;
7408 lun = ccb->ccb_h.target_lun;
7409 channel = SIM_CHANNEL(ahc, sim);
7410 target_mask = 0x01 << target;
7411 if (channel == 'B')
7412 target_mask <<= 8;
7413
7414 if (cel->enable != 0) {
7415 u_int scsiseq;
7416
7417 /* Are we already enabled?? */
7418 if (lstate != NULL) {
7419 xpt_print_path(ccb->ccb_h.path);
7420 printf("Lun already enabled\n");
7421 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
7422 return;
7423 }
7424
7425 if (cel->grp6_len != 0
7426 || cel->grp7_len != 0) {
7427 /*
7428 * Don't (yet?) support vendor
7429 * specific commands.
7430 */
7431 ccb->ccb_h.status = CAM_REQ_INVALID;
7432 printf("Non-zero Group Codes\n");
7433 return;
7434 }
7435
7436 /*
7437 * Seems to be okay.
7438 * Setup our data structures.
7439 */
7440 if (target != CAM_TARGET_WILDCARD && tstate == NULL) {
7441 tstate = ahc_alloc_tstate(ahc, target, channel);
7442 if (tstate == NULL) {
7443 xpt_print_path(ccb->ccb_h.path);
7444 printf("Couldn't allocate tstate\n");
7445 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7446 return;
7447 }
7448 }
7449 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT);
7450 if (lstate == NULL) {
7451 xpt_print_path(ccb->ccb_h.path);
7452 printf("Couldn't allocate lstate\n");
7453 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7454 return;
7455 }
7456 memset(lstate, 0, sizeof(*lstate));
7457 status = xpt_create_path(&lstate->path, /*periph*/NULL,
7458 xpt_path_path_id(ccb->ccb_h.path),
7459 xpt_path_target_id(ccb->ccb_h.path),
7460 xpt_path_lun_id(ccb->ccb_h.path));
7461 if (status != CAM_REQ_CMP) {
7462 free(lstate, M_DEVBUF);
7463 xpt_print_path(ccb->ccb_h.path);
7464 printf("Couldn't allocate path\n");
7465 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
7466 return;
7467 }
7468 SLIST_INIT(&lstate->accept_tios);
7469 SLIST_INIT(&lstate->immed_notifies);
7470 ahc_pause(ahc);
7471 if (target != CAM_TARGET_WILDCARD) {
7472 tstate->enabled_luns[lun] = lstate;
7473 ahc->enabled_luns++;
7474
7475 if ((ahc->features & AHC_MULTI_TID) != 0) {
7476 u_int targid_mask;
7477
7478 targid_mask = ahc_inb(ahc, TARGID)
7479 | (ahc_inb(ahc, TARGID + 1) << 8);
7480
7481 targid_mask |= target_mask;
7482 ahc_outb(ahc, TARGID, targid_mask);
7483 ahc_outb(ahc, TARGID+1, (targid_mask >> 8));
7484
7485 ahc_update_scsiid(ahc, targid_mask);
7486 } else {
7487 u_int our_id;
7488 char channel;
7489
7490 channel = SIM_CHANNEL(ahc, sim);
7491 our_id = SIM_SCSI_ID(ahc, sim);
7492
7493 /*
7494 * This can only happen if selections
7495 * are not enabled
7496 */
7497 if (target != our_id) {
7498 u_int sblkctl;
7499 char cur_channel;
7500 int swap;
7501
7502 sblkctl = ahc_inb(ahc, SBLKCTL);
7503 cur_channel = (sblkctl & SELBUSB)
7504 ? 'B' : 'A';
7505 if ((ahc->features & AHC_TWIN) == 0)
7506 cur_channel = 'A';
7507 swap = cur_channel != channel;
7508 if (channel == 'A')
7509 ahc->our_id = target;
7510 else
7511 ahc->our_id_b = target;
7512
7513 if (swap)
7514 ahc_outb(ahc, SBLKCTL,
7515 sblkctl ^ SELBUSB);
7516
7517 ahc_outb(ahc, SCSIID, target);
7518
7519 if (swap)
7520 ahc_outb(ahc, SBLKCTL, sblkctl);
7521 }
7522 }
7523 } else
7524 ahc->black_hole = lstate;
7525 /* Allow select-in operations */
7526 if (ahc->black_hole != NULL && ahc->enabled_luns > 0) {
7527 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
7528 scsiseq |= ENSELI;
7529 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
7530 scsiseq = ahc_inb(ahc, SCSISEQ);
7531 scsiseq |= ENSELI;
7532 ahc_outb(ahc, SCSISEQ, scsiseq);
7533 }
7534 ahc_unpause(ahc);
7535 ccb->ccb_h.status = CAM_REQ_CMP;
7536 xpt_print_path(ccb->ccb_h.path);
7537 printf("Lun now enabled for target mode\n");
7538 } else {
7539 struct scb *scb;
7540 int i, empty;
7541
7542 if (lstate == NULL) {
7543 ccb->ccb_h.status = CAM_LUN_INVALID;
7544 return;
7545 }
7546
7547 ccb->ccb_h.status = CAM_REQ_CMP;
7548 LIST_FOREACH(scb, &ahc->pending_scbs, pending_links) {
7549 struct ccb_hdr *ccbh;
7550
7551 ccbh = &scb->io_ctx->ccb_h;
7552 if (ccbh->func_code == XPT_CONT_TARGET_IO
7553 && !xpt_path_comp(ccbh->path, ccb->ccb_h.path)){
7554 printf("CTIO pending\n");
7555 ccb->ccb_h.status = CAM_REQ_INVALID;
7556 return;
7557 }
7558 }
7559
7560 if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
7561 printf("ATIOs pending\n");
7562 ccb->ccb_h.status = CAM_REQ_INVALID;
7563 }
7564
7565 if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
7566 printf("INOTs pending\n");
7567 ccb->ccb_h.status = CAM_REQ_INVALID;
7568 }
7569
7570 if (ccb->ccb_h.status != CAM_REQ_CMP) {
7571 return;
7572 }
7573
7574 xpt_print_path(ccb->ccb_h.path);
7575 printf("Target mode disabled\n");
7576 xpt_free_path(lstate->path);
7577 free(lstate, M_DEVBUF);
7578
7579 ahc_pause(ahc);
7580 /* Can we clean up the target too? */
7581 if (target != CAM_TARGET_WILDCARD) {
7582 tstate->enabled_luns[lun] = NULL;
7583 ahc->enabled_luns--;
7584 for (empty = 1, i = 0; i < 8; i++)
7585 if (tstate->enabled_luns[i] != NULL) {
7586 empty = 0;
7587 break;
7588 }
7589
7590 if (empty) {
7591 ahc_free_tstate(ahc, target, channel,
7592 /*force*/FALSE);
7593 if (ahc->features & AHC_MULTI_TID) {
7594 u_int targid_mask;
7595
7596 targid_mask = ahc_inb(ahc, TARGID)
7597 | (ahc_inb(ahc, TARGID + 1)
7598 << 8);
7599
7600 targid_mask &= ~target_mask;
7601 ahc_outb(ahc, TARGID, targid_mask);
7602 ahc_outb(ahc, TARGID+1,
7603 (targid_mask >> 8));
7604 ahc_update_scsiid(ahc, targid_mask);
7605 }
7606 }
7607 } else {
7608 ahc->black_hole = NULL;
7609
7610 /*
7611 * We can't allow selections without
7612 * our black hole device.
7613 */
7614 empty = TRUE;
7615 }
7616 if (ahc->enabled_luns == 0) {
7617 /* Disallow select-in */
7618 u_int scsiseq;
7619
7620 scsiseq = ahc_inb(ahc, SCSISEQ_TEMPLATE);
7621 scsiseq &= ~ENSELI;
7622 ahc_outb(ahc, SCSISEQ_TEMPLATE, scsiseq);
7623 scsiseq = ahc_inb(ahc, SCSISEQ);
7624 scsiseq &= ~ENSELI;
7625 ahc_outb(ahc, SCSISEQ, scsiseq);
7626
7627 if ((ahc->features & AHC_MULTIROLE) == 0) {
7628 printf("Configuring Initiator Mode\n");
7629 ahc->flags &= ~AHC_TARGETROLE;
7630 ahc->flags |= AHC_INITIATORROLE;
7631 /*
7632 * Returning to a configuration that
7633 * fit previously will always succeed.
7634 */
7635 (void)ahc_loadseq(ahc);
7636 ahc_restart(ahc);
7637 /*
7638 * Unpaused. The extra unpause
7639 * that follows is harmless.
7640 */
7641 }
7642 }
7643 ahc_unpause(ahc);
7644 }
7645 }
7646
7647 static void
ahc_update_scsiid(struct ahc_softc * ahc,u_int targid_mask)7648 ahc_update_scsiid(struct ahc_softc *ahc, u_int targid_mask)
7649 {
7650 u_int scsiid_mask;
7651 u_int scsiid;
7652
7653 if ((ahc->features & AHC_MULTI_TID) == 0)
7654 panic("ahc_update_scsiid called on non-multitid unit\n");
7655
7656 /*
7657 * Since we will rely on the TARGID mask
7658 * for selection enables, ensure that OID
7659 * in SCSIID is not set to some other ID
7660 * that we don't want to allow selections on.
7661 */
7662 if ((ahc->features & AHC_ULTRA2) != 0)
7663 scsiid = ahc_inb(ahc, SCSIID_ULTRA2);
7664 else
7665 scsiid = ahc_inb(ahc, SCSIID);
7666 scsiid_mask = 0x1 << (scsiid & OID);
7667 if ((targid_mask & scsiid_mask) == 0) {
7668 u_int our_id;
7669
7670 /* ffs counts from 1 */
7671 our_id = ffs(targid_mask);
7672 if (our_id == 0)
7673 our_id = ahc->our_id;
7674 else
7675 our_id--;
7676 scsiid &= TID;
7677 scsiid |= our_id;
7678 }
7679 if ((ahc->features & AHC_ULTRA2) != 0)
7680 ahc_outb(ahc, SCSIID_ULTRA2, scsiid);
7681 else
7682 ahc_outb(ahc, SCSIID, scsiid);
7683 }
7684
7685 void
ahc_run_tqinfifo(struct ahc_softc * ahc,int paused)7686 ahc_run_tqinfifo(struct ahc_softc *ahc, int paused)
7687 {
7688 struct target_cmd *cmd;
7689
7690 /*
7691 * If the card supports auto-access pause,
7692 * we can access the card directly regardless
7693 * of whether it is paused or not.
7694 */
7695 if ((ahc->features & AHC_AUTOPAUSE) != 0)
7696 paused = TRUE;
7697
7698 ahc_sync_tqinfifo(ahc, BUS_DMASYNC_POSTREAD);
7699 while ((cmd = &ahc->targetcmds[ahc->tqinfifonext])->cmd_valid != 0) {
7700 /*
7701 * Only advance through the queue if we
7702 * have the resources to process the command.
7703 */
7704 if (ahc_handle_target_cmd(ahc, cmd) != 0)
7705 break;
7706
7707 cmd->cmd_valid = 0;
7708 aic_dmamap_sync(ahc, ahc->shared_data_dmat,
7709 ahc->shared_data_dmamap,
7710 ahc_targetcmd_offset(ahc, ahc->tqinfifonext),
7711 sizeof(struct target_cmd),
7712 BUS_DMASYNC_PREREAD);
7713 ahc->tqinfifonext++;
7714
7715 /*
7716 * Lazily update our position in the target mode incoming
7717 * command queue as seen by the sequencer.
7718 */
7719 if ((ahc->tqinfifonext & (HOST_TQINPOS - 1)) == 1) {
7720 if ((ahc->features & AHC_HS_MAILBOX) != 0) {
7721 u_int hs_mailbox;
7722
7723 hs_mailbox = ahc_inb(ahc, HS_MAILBOX);
7724 hs_mailbox &= ~HOST_TQINPOS;
7725 hs_mailbox |= ahc->tqinfifonext & HOST_TQINPOS;
7726 ahc_outb(ahc, HS_MAILBOX, hs_mailbox);
7727 } else {
7728 if (!paused)
7729 ahc_pause(ahc);
7730 ahc_outb(ahc, KERNEL_TQINPOS,
7731 ahc->tqinfifonext & HOST_TQINPOS);
7732 if (!paused)
7733 ahc_unpause(ahc);
7734 }
7735 }
7736 }
7737 }
7738
7739 static int
ahc_handle_target_cmd(struct ahc_softc * ahc,struct target_cmd * cmd)7740 ahc_handle_target_cmd(struct ahc_softc *ahc, struct target_cmd *cmd)
7741 {
7742 struct ahc_tmode_tstate *tstate;
7743 struct ahc_tmode_lstate *lstate;
7744 struct ccb_accept_tio *atio;
7745 uint8_t *byte;
7746 int initiator;
7747 int target;
7748 int lun;
7749
7750 initiator = SCSIID_TARGET(ahc, cmd->scsiid);
7751 target = SCSIID_OUR_ID(cmd->scsiid);
7752 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK);
7753
7754 byte = cmd->bytes;
7755 tstate = ahc->enabled_targets[target];
7756 lstate = NULL;
7757 if (tstate != NULL)
7758 lstate = tstate->enabled_luns[lun];
7759
7760 /*
7761 * Commands for disabled luns go to the black hole driver.
7762 */
7763 if (lstate == NULL)
7764 lstate = ahc->black_hole;
7765
7766 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios);
7767 if (atio == NULL) {
7768 ahc->flags |= AHC_TQINFIFO_BLOCKED;
7769 /*
7770 * Wait for more ATIOs from the peripheral driver for this lun.
7771 */
7772 if (bootverbose)
7773 printf("%s: ATIOs exhausted\n", ahc_name(ahc));
7774 return (1);
7775 } else
7776 ahc->flags &= ~AHC_TQINFIFO_BLOCKED;
7777 #ifdef AHC_DEBUG
7778 if (ahc_debug & AHC_SHOW_TQIN) {
7779 printf("Incoming command from %d for %d:%d%s\n",
7780 initiator, target, lun,
7781 lstate == ahc->black_hole ? "(Black Holed)" : "");
7782 }
7783 #endif
7784 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle);
7785
7786 if (lstate == ahc->black_hole) {
7787 /* Fill in the wildcards */
7788 atio->ccb_h.target_id = target;
7789 atio->ccb_h.target_lun = lun;
7790 }
7791
7792 /*
7793 * Package it up and send it off to
7794 * whomever has this lun enabled.
7795 */
7796 atio->sense_len = 0;
7797 atio->init_id = initiator;
7798 if (byte[0] != 0xFF) {
7799 /* Tag was included */
7800 atio->tag_action = *byte++;
7801 atio->tag_id = *byte++;
7802 atio->ccb_h.flags |= CAM_TAG_ACTION_VALID;
7803 } else {
7804 atio->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
7805 }
7806 byte++;
7807
7808 /* Okay. Now determine the cdb size based on the command code */
7809 switch (*byte >> CMD_GROUP_CODE_SHIFT) {
7810 case 0:
7811 atio->cdb_len = 6;
7812 break;
7813 case 1:
7814 case 2:
7815 atio->cdb_len = 10;
7816 break;
7817 case 4:
7818 atio->cdb_len = 16;
7819 break;
7820 case 5:
7821 atio->cdb_len = 12;
7822 break;
7823 case 3:
7824 default:
7825 /* Only copy the opcode. */
7826 atio->cdb_len = 1;
7827 printf("Reserved or VU command code type encountered\n");
7828 break;
7829 }
7830
7831 memcpy(atio->cdb_io.cdb_bytes, byte, atio->cdb_len);
7832
7833 atio->ccb_h.status |= CAM_CDB_RECVD;
7834
7835 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) {
7836 /*
7837 * We weren't allowed to disconnect.
7838 * We're hanging on the bus until a
7839 * continue target I/O comes in response
7840 * to this accept tio.
7841 */
7842 #ifdef AHC_DEBUG
7843 if (ahc_debug & AHC_SHOW_TQIN) {
7844 printf("Received Immediate Command %d:%d:%d - %p\n",
7845 initiator, target, lun, ahc->pending_device);
7846 }
7847 #endif
7848 ahc->pending_device = lstate;
7849 aic_freeze_ccb((union ccb *)atio);
7850 atio->ccb_h.flags |= CAM_DIS_DISCONNECT;
7851 } else {
7852 atio->ccb_h.flags &= ~CAM_DIS_DISCONNECT;
7853 }
7854
7855 xpt_done((union ccb*)atio);
7856 return (0);
7857 }
7858
7859 #endif
7860