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