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