aic7xxx_osm.c revision 1.19 1 /* $NetBSD: aic7xxx_osm.c,v 1.19 2005/11/28 21:03:20 bouyer Exp $ */
2
3 /*
4 * Bus independent FreeBSD shim for the aic7xxx based adaptec SCSI controllers
5 *
6 * Copyright (c) 1994-2001 Justin T. Gibbs.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU Public License ("GPL").
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.c#12 $
34 *
35 * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx_osm.c,v 1.31 2002/11/30 19:08:58 scottl Exp $
36 */
37 /*
38 * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: aic7xxx_osm.c,v 1.19 2005/11/28 21:03:20 bouyer Exp $");
43
44 #include <dev/ic/aic7xxx_osm.h>
45 #include <dev/ic/aic7xxx_inline.h>
46
47 #ifndef AHC_TMODE_ENABLE
48 #define AHC_TMODE_ENABLE 0
49 #endif
50
51
52 static void ahc_action(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg);
53 static void ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments);
54 static int ahc_poll(struct ahc_softc *ahc, int wait);
55 static void ahc_setup_data(struct ahc_softc *ahc,
56 struct scsipi_xfer *xs, struct scb *scb);
57 static void ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb);
58 static int ahc_ioctl(struct scsipi_channel *channel, u_long cmd, caddr_t addr, int flag,
59 struct proc *p);
60
61
62
63 /*
64 * Attach all the sub-devices we can find
65 */
66 int
67 ahc_attach(struct ahc_softc *ahc)
68 {
69 u_long s;
70 int i;
71 char ahc_info[256];
72
73 LIST_INIT(&ahc->pending_scbs);
74 for (i = 0; i < AHC_NUM_TARGETS; i++)
75 TAILQ_INIT(&ahc->untagged_queues[i]);
76
77 ahc_lock(ahc, &s);
78
79 ahc->sc_adapter.adapt_dev = &ahc->sc_dev;
80 ahc->sc_adapter.adapt_nchannels = (ahc->features & AHC_TWIN) ? 2 : 1;
81
82 ahc->sc_adapter.adapt_openings = ahc->scb_data->numscbs - 1;
83 ahc->sc_adapter.adapt_max_periph = 16;
84
85 ahc->sc_adapter.adapt_ioctl = ahc_ioctl;
86 ahc->sc_adapter.adapt_minphys = ahc_minphys;
87 ahc->sc_adapter.adapt_request = ahc_action;
88
89 ahc->sc_channel.chan_adapter = &ahc->sc_adapter;
90 ahc->sc_channel.chan_bustype = &scsi_bustype;
91 ahc->sc_channel.chan_channel = 0;
92 ahc->sc_channel.chan_ntargets = (ahc->features & AHC_WIDE) ? 16 : 8;
93 ahc->sc_channel.chan_nluns = 8 /*AHC_NUM_LUNS*/;
94 ahc->sc_channel.chan_id = ahc->our_id;
95 ahc->sc_channel.chan_flags |= SCSIPI_CHAN_CANGROW;
96
97 if (ahc->features & AHC_TWIN) {
98 ahc->sc_channel_b = ahc->sc_channel;
99 ahc->sc_channel_b.chan_id = ahc->our_id_b;
100 ahc->sc_channel_b.chan_channel = 1;
101 }
102
103 ahc_controller_info(ahc, ahc_info, sizeof(ahc_info));
104 printf("%s: %s\n", ahc->sc_dev.dv_xname, ahc_info);
105
106 if ((ahc->flags & AHC_PRIMARY_CHANNEL) == 0) {
107 ahc->sc_child = config_found((void *)&ahc->sc_dev,
108 &ahc->sc_channel, scsiprint);
109 if (ahc->features & AHC_TWIN)
110 ahc->sc_child_b = config_found((void *)&ahc->sc_dev,
111 &ahc->sc_channel_b, scsiprint);
112 } else {
113 if (ahc->features & AHC_TWIN)
114 ahc->sc_child = config_found((void *)&ahc->sc_dev,
115 &ahc->sc_channel_b, scsiprint);
116 ahc->sc_child_b = config_found((void *)&ahc->sc_dev,
117 &ahc->sc_channel, scsiprint);
118 }
119
120 ahc_intr_enable(ahc, TRUE);
121
122 if (ahc->flags & AHC_RESET_BUS_A)
123 ahc_reset_channel(ahc, 'A', TRUE);
124 if ((ahc->features & AHC_TWIN) && ahc->flags & AHC_RESET_BUS_B)
125 ahc_reset_channel(ahc, 'B', TRUE);
126
127 ahc_unlock(ahc, &s);
128 return (1);
129 }
130
131 /*
132 * Catch an interrupt from the adapter
133 */
134 void
135 ahc_platform_intr(void *arg)
136 {
137 struct ahc_softc *ahc;
138
139 ahc = (struct ahc_softc *)arg;
140 ahc_intr(ahc);
141 }
142
143 /*
144 * We have an scb which has been processed by the
145 * adaptor, now we look to see how the operation
146 * went.
147 */
148 void
149 ahc_done(struct ahc_softc *ahc, struct scb *scb)
150 {
151 struct scsipi_xfer *xs;
152 struct scsipi_periph *periph;
153 u_long s;
154
155 xs = scb->xs;
156 periph = xs->xs_periph;
157 LIST_REMOVE(scb, pending_links);
158 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
159 struct scb_tailq *untagged_q;
160 int target_offset;
161
162 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
163 untagged_q = &ahc->untagged_queues[target_offset];
164 TAILQ_REMOVE(untagged_q, scb, links.tqe);
165 scb->flags &= ~SCB_UNTAGGEDQ;
166 ahc_run_untagged_queue(ahc, untagged_q);
167 }
168
169 callout_stop(&scb->xs->xs_callout);
170
171 if (xs->datalen) {
172 int op;
173
174 if (xs->xs_control & XS_CTL_DATA_IN)
175 op = BUS_DMASYNC_POSTREAD;
176 else
177 op = BUS_DMASYNC_POSTWRITE;
178 bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
179 scb->dmamap->dm_mapsize, op);
180 bus_dmamap_unload(ahc->parent_dmat, scb->dmamap);
181 }
182
183 /*
184 * If the recovery SCB completes, we have to be
185 * out of our timeout.
186 */
187 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
188 struct scb *list_scb;
189
190 /*
191 * We were able to complete the command successfully,
192 * so reinstate the timeouts for all other pending
193 * commands.
194 */
195 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
196 if (!(list_scb->xs->xs_control & XS_CTL_POLL)) {
197 callout_reset(&list_scb->xs->xs_callout,
198 (list_scb->xs->timeout > 1000000) ?
199 (list_scb->xs->timeout / 1000) * hz :
200 (list_scb->xs->timeout * hz) / 1000,
201 ahc_timeout, list_scb);
202 }
203 }
204
205 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
206 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
207 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
208 scsipi_printaddr(xs->xs_periph);
209 printf("%s: no longer in timeout, status = %x\n",
210 ahc_name(ahc), xs->status);
211 }
212
213 /* Don't clobber any existing error state */
214 if (xs->error != XS_NOERROR) {
215 /* Don't clobber any existing error state */
216 } else if ((scb->flags & SCB_SENSE) != 0) {
217 /*
218 * We performed autosense retrieval.
219 *
220 * Zero any sense not transferred by the
221 * device. The SCSI spec mandates that any
222 * untransferred data should be assumed to be
223 * zero. Complete the 'bounce' of sense information
224 * through buffers accessible via bus-space by
225 * copying it into the clients csio.
226 */
227 memset(&xs->sense.scsi_sense, 0, sizeof(xs->sense.scsi_sense));
228 memcpy(&xs->sense.scsi_sense,
229 ahc_get_sense_buf(ahc, scb),
230 sizeof(xs->sense.scsi_sense));
231 xs->error = XS_SENSE;
232 }
233 if (scb->flags & SCB_FREEZE_QUEUE) {
234 scsipi_periph_thaw(periph, 1);
235 scb->flags &= ~SCB_FREEZE_QUEUE;
236 }
237
238 ahc_lock(ahc, &s);
239 ahc_free_scb(ahc, scb);
240 ahc_unlock(ahc, &s);
241
242 scsipi_done(xs);
243 }
244
245 static int
246 ahc_ioctl(struct scsipi_channel *channel, u_long cmd, caddr_t addr, int flag,
247 struct proc *p)
248 {
249 struct ahc_softc *ahc = (void *)channel->chan_adapter->adapt_dev;
250 int s, ret = ENOTTY;
251
252 switch (cmd) {
253 case SCBUSIORESET:
254 s = splbio();
255 ahc_reset_channel(ahc, channel->chan_channel == 1 ? 'B' : 'A',
256 TRUE);
257 splx(s);
258 ret = 0;
259 break;
260 default:
261 break;
262 }
263
264 return ret;
265 }
266
267 static void
268 ahc_action(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
269 {
270 struct ahc_softc *ahc;
271 int s;
272 struct ahc_initiator_tinfo *tinfo;
273 struct ahc_tmode_tstate *tstate;
274
275 ahc = (void *)chan->chan_adapter->adapt_dev;
276
277 switch (req) {
278
279 case ADAPTER_REQ_RUN_XFER:
280 {
281 struct scsipi_xfer *xs;
282 struct scsipi_periph *periph;
283 struct scb *scb;
284 struct hardware_scb *hscb;
285 u_int target_id;
286 u_int our_id;
287 u_long ss;
288
289 xs = arg;
290 periph = xs->xs_periph;
291
292 target_id = periph->periph_target;
293 our_id = ahc->our_id;
294
295 SC_DEBUG(xs->xs_periph, SCSIPI_DB3, ("ahc_action\n"));
296
297 /*
298 * get an scb to use.
299 */
300 ahc_lock(ahc, &ss);
301 if ((scb = ahc_get_scb(ahc)) == NULL) {
302 xs->error = XS_RESOURCE_SHORTAGE;
303 ahc_unlock(ahc, &ss);
304 scsipi_done(xs);
305 return;
306 }
307 ahc_unlock(ahc, &ss);
308
309 hscb = scb->hscb;
310
311 SC_DEBUG(periph, SCSIPI_DB3, ("start scb(%p)\n", scb));
312 scb->xs = xs;
313
314 /*
315 * Put all the arguments for the xfer in the scb
316 */
317 hscb->control = 0;
318 hscb->scsiid = BUILD_SCSIID(ahc, 0, target_id, our_id);
319 hscb->lun = periph->periph_lun;
320 if (xs->xs_control & XS_CTL_RESET) {
321 hscb->cdb_len = 0;
322 scb->flags |= SCB_DEVICE_RESET;
323 hscb->control |= MK_MESSAGE;
324 ahc_execute_scb(scb, NULL, 0);
325 }
326
327 ahc_setup_data(ahc, xs, scb);
328
329 break;
330 }
331 case ADAPTER_REQ_GROW_RESOURCES:
332 printf("%s: ADAPTER_REQ_GROW_RESOURCES\n", ahc_name(ahc));
333 chan->chan_adapter->adapt_openings += ahc_alloc_scbs(ahc);
334 if (ahc->scb_data->numscbs >= AHC_SCB_MAX_ALLOC)
335 chan->chan_flags &= ~SCSIPI_CHAN_CANGROW;
336 return;
337
338 case ADAPTER_REQ_SET_XFER_MODE:
339 {
340 struct scsipi_xfer_mode *xm = arg;
341 struct ahc_devinfo devinfo;
342 int target_id, our_id, first;
343 u_int width;
344 char channel;
345 u_int ppr_options, period, offset;
346 struct ahc_syncrate *syncrate;
347 uint16_t old_autoneg;
348
349 target_id = xm->xm_target;
350 our_id = chan->chan_id;
351 channel = (chan->chan_channel == 1) ? 'B' : 'A';
352 s = splbio();
353 tinfo = ahc_fetch_transinfo(ahc, channel, our_id, target_id,
354 &tstate);
355 ahc_compile_devinfo(&devinfo, our_id, target_id,
356 0, channel, ROLE_INITIATOR);
357
358 old_autoneg = tstate->auto_negotiate;
359
360 /*
361 * XXX since the period and offset are not provided here,
362 * fake things by forcing a renegotiation using the user
363 * settings if this is called for the first time (i.e.
364 * during probe). Also, cap various values at the user
365 * values, assuming that the user set it up that way.
366 */
367 if (ahc->inited_target[target_id] == 0) {
368 period = tinfo->user.period;
369 offset = tinfo->user.offset;
370 ppr_options = tinfo->user.ppr_options;
371 width = tinfo->user.width;
372 tstate->tagenable |=
373 (ahc->user_tagenable & devinfo.target_mask);
374 tstate->discenable |=
375 (ahc->user_discenable & devinfo.target_mask);
376 ahc->inited_target[target_id] = 1;
377 first = 1;
378 } else
379 first = 0;
380
381 if (xm->xm_mode & (PERIPH_CAP_WIDE16 | PERIPH_CAP_DT))
382 width = MSG_EXT_WDTR_BUS_16_BIT;
383 else
384 width = MSG_EXT_WDTR_BUS_8_BIT;
385
386 ahc_validate_width(ahc, NULL, &width, ROLE_UNKNOWN);
387 if (width > tinfo->user.width)
388 width = tinfo->user.width;
389 ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
390
391 if (!(xm->xm_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_DT))) {
392 period = 0;
393 offset = 0;
394 ppr_options = 0;
395 }
396
397 if ((xm->xm_mode & PERIPH_CAP_DT) &&
398 (ppr_options & MSG_EXT_PPR_DT_REQ))
399 ppr_options |= MSG_EXT_PPR_DT_REQ;
400 else
401 ppr_options &= ~MSG_EXT_PPR_DT_REQ;
402 if ((tstate->discenable & devinfo.target_mask) == 0 ||
403 (tstate->tagenable & devinfo.target_mask) == 0)
404 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
405
406 if ((xm->xm_mode & PERIPH_CAP_TQING) &&
407 (ahc->user_tagenable & devinfo.target_mask))
408 tstate->tagenable |= devinfo.target_mask;
409 else
410 tstate->tagenable &= ~devinfo.target_mask;
411
412 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
413 AHC_SYNCRATE_MAX);
414 ahc_validate_offset(ahc, NULL, syncrate, &offset,
415 width, ROLE_UNKNOWN);
416
417 if (offset == 0) {
418 period = 0;
419 ppr_options = 0;
420 }
421
422 if (ppr_options != 0
423 && tinfo->user.transport_version >= 3) {
424 tinfo->goal.transport_version =
425 tinfo->user.transport_version;
426 tinfo->curr.transport_version =
427 tinfo->user.transport_version;
428 }
429
430 ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
431 ppr_options, AHC_TRANS_GOAL, FALSE);
432
433 /*
434 * If this is the first request, and no negotiation is
435 * needed, just confirm the state to the scsipi layer,
436 * so that it can print a message.
437 */
438 if (old_autoneg == tstate->auto_negotiate && first) {
439 xm->xm_mode = 0;
440 xm->xm_period = tinfo->curr.period;
441 xm->xm_offset = tinfo->curr.offset;
442 if (tinfo->curr.width == MSG_EXT_WDTR_BUS_16_BIT)
443 xm->xm_mode |= PERIPH_CAP_WIDE16;
444 if (tinfo->curr.period)
445 xm->xm_mode |= PERIPH_CAP_SYNC;
446 if (tstate->tagenable & devinfo.target_mask)
447 xm->xm_mode |= PERIPH_CAP_TQING;
448 if (tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ)
449 xm->xm_mode |= PERIPH_CAP_DT;
450 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
451 }
452 splx(s);
453 }
454 }
455
456 return;
457 }
458
459 static void
460 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments)
461 {
462 struct scb *scb;
463 struct scsipi_xfer *xs;
464 struct ahc_softc *ahc;
465 struct ahc_initiator_tinfo *tinfo;
466 struct ahc_tmode_tstate *tstate;
467
468 u_int mask;
469 long s;
470
471 scb = (struct scb *)arg;
472 xs = scb->xs;
473 xs->error = 0;
474 xs->status = 0;
475 xs->xs_status = 0;
476 ahc = (void *)xs->xs_periph->periph_channel->chan_adapter->adapt_dev;
477
478 if (nsegments != 0) {
479 struct ahc_dma_seg *sg;
480 bus_dma_segment_t *end_seg;
481 int op;
482
483 end_seg = dm_segs + nsegments;
484
485 /* Copy the segments into our SG list */
486 sg = scb->sg_list;
487 while (dm_segs < end_seg) {
488 uint32_t len;
489
490 sg->addr = ahc_htole32(dm_segs->ds_addr);
491 len = dm_segs->ds_len
492 | ((dm_segs->ds_addr >> 8) & AHC_SG_HIGH_ADDR_MASK);
493 sg->len = ahc_htole32(len);
494 sg++;
495 dm_segs++;
496 }
497
498 /*
499 * Note where to find the SG entries in bus space.
500 * We also set the full residual flag which the
501 * sequencer will clear as soon as a data transfer
502 * occurs.
503 */
504 scb->hscb->sgptr = ahc_htole32(scb->sg_list_phys|SG_FULL_RESID);
505
506 if (xs->xs_control & XS_CTL_DATA_IN)
507 op = BUS_DMASYNC_PREREAD;
508 else
509 op = BUS_DMASYNC_PREWRITE;
510
511 bus_dmamap_sync(ahc->parent_dmat, scb->dmamap, 0,
512 scb->dmamap->dm_mapsize, op);
513
514 sg--;
515 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
516
517 /* Copy the first SG into the "current" data pointer area */
518 scb->hscb->dataptr = scb->sg_list->addr;
519 scb->hscb->datacnt = scb->sg_list->len;
520 } else {
521 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
522 scb->hscb->dataptr = 0;
523 scb->hscb->datacnt = 0;
524 }
525
526 scb->sg_count = nsegments;
527
528 ahc_lock(ahc, &s);
529
530 /*
531 * Last time we need to check if this SCB needs to
532 * be aborted.
533 */
534 if (xs->xs_status & XS_STS_DONE) {
535 if (nsegments != 0)
536 bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
537 ahc_free_scb(ahc, scb);
538 ahc_unlock(ahc, &s);
539 scsipi_done(xs);
540 return;
541 }
542
543 tinfo = ahc_fetch_transinfo(ahc, ahc->channel,
544 SCSIID_OUR_ID(scb->hscb->scsiid),
545 SCSIID_TARGET(ahc, scb->hscb->scsiid),
546 &tstate);
547
548 mask = SCB_GET_TARGET_MASK(ahc, scb);
549 scb->hscb->scsirate = tinfo->scsirate;
550 scb->hscb->scsioffset = tinfo->curr.offset;
551
552 if ((tstate->ultraenb & mask) != 0)
553 scb->hscb->control |= ULTRAENB;
554
555 if ((tstate->discenable & mask) != 0)
556 scb->hscb->control |= DISCENB;
557
558 if (xs->xs_tag_type)
559 scb->hscb->control |= xs->xs_tag_type;
560
561 #if 1 /* This looks like it makes sense at first, but it can loop */
562 if ((xs->xs_control & XS_CTL_DISCOVERY) && (tinfo->goal.width == 0
563 && tinfo->goal.offset == 0
564 && tinfo->goal.ppr_options == 0)) {
565 scb->flags |= SCB_NEGOTIATE;
566 scb->hscb->control |= MK_MESSAGE;
567 } else
568 #endif
569 if ((tstate->auto_negotiate & mask) != 0) {
570 scb->flags |= SCB_AUTO_NEGOTIATE;
571 scb->hscb->control |= MK_MESSAGE;
572 }
573
574 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
575
576 if (!(xs->xs_control & XS_CTL_POLL)) {
577 callout_reset(&scb->xs->xs_callout, xs->timeout > 1000000 ?
578 (xs->timeout / 1000) * hz : (xs->timeout * hz) / 1000,
579 ahc_timeout, scb);
580 }
581
582 /*
583 * We only allow one untagged transaction
584 * per target in the initiator role unless
585 * we are storing a full busy target *lun*
586 * table in SCB space.
587 */
588 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
589 && (ahc->flags & AHC_SCB_BTT) == 0) {
590 struct scb_tailq *untagged_q;
591 int target_offset;
592
593 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
594 untagged_q = &(ahc->untagged_queues[target_offset]);
595 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
596 scb->flags |= SCB_UNTAGGEDQ;
597 if (TAILQ_FIRST(untagged_q) != scb) {
598 ahc_unlock(ahc, &s);
599 return;
600 }
601 }
602 scb->flags |= SCB_ACTIVE;
603
604 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
605 /* Define a mapping from our tag to the SCB. */
606 ahc->scb_data->scbindex[scb->hscb->tag] = scb;
607 ahc_pause(ahc);
608 if ((ahc->flags & AHC_PAGESCBS) == 0)
609 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
610 ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
611 ahc_unpause(ahc);
612 } else {
613 ahc_queue_scb(ahc, scb);
614 }
615
616 if (!(xs->xs_control & XS_CTL_POLL)) {
617 ahc_unlock(ahc, &s);
618 return;
619 }
620
621 /*
622 * If we can't use interrupts, poll for completion
623 */
624 SC_DEBUG(xs->xs_periph, SCSIPI_DB3, ("cmd_poll\n"));
625 do {
626 if (ahc_poll(ahc, xs->timeout)) {
627 if (!(xs->xs_control & XS_CTL_SILENT))
628 printf("cmd fail\n");
629 ahc_timeout(scb);
630 break;
631 }
632 } while (!(xs->xs_status & XS_STS_DONE));
633 ahc_unlock(ahc, &s);
634
635 return;
636 }
637
638 static int
639 ahc_poll(struct ahc_softc *ahc, int wait)
640 {
641 while (--wait) {
642 DELAY(1000);
643 if (ahc_inb(ahc, INTSTAT) & INT_PEND)
644 break;
645 }
646
647 if (wait == 0) {
648 printf("%s: board is not responding\n", ahc_name(ahc));
649 return (EIO);
650 }
651
652 ahc_intr((void *)ahc);
653 return (0);
654 }
655
656 static void
657 ahc_setup_data(struct ahc_softc *ahc, struct scsipi_xfer *xs,
658 struct scb *scb)
659 {
660 struct hardware_scb *hscb;
661
662 hscb = scb->hscb;
663 xs->resid = xs->status = 0;
664
665 hscb->cdb_len = xs->cmdlen;
666 if (hscb->cdb_len > sizeof(hscb->cdb32)) {
667 u_long s;
668
669 ahc_set_transaction_status(scb, CAM_REQ_INVALID);
670 ahc_lock(ahc, &s);
671 ahc_free_scb(ahc, scb);
672 ahc_unlock(ahc, &s);
673 scsipi_done(xs);
674 return;
675 }
676
677 if (hscb->cdb_len > 12) {
678 memcpy(hscb->cdb32, xs->cmd, hscb->cdb_len);
679 scb->flags |= SCB_CDB32_PTR;
680 } else {
681 memcpy(hscb->shared_data.cdb, xs->cmd, hscb->cdb_len);
682 }
683
684 /* Only use S/G if there is a transfer */
685 if (xs->datalen) {
686 int error;
687
688 error = bus_dmamap_load(ahc->parent_dmat,
689 scb->dmamap, xs->data,
690 xs->datalen, NULL,
691 ((xs->xs_control & XS_CTL_NOSLEEP) ?
692 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
693 BUS_DMA_STREAMING |
694 ((xs->xs_control & XS_CTL_DATA_IN) ?
695 BUS_DMA_READ : BUS_DMA_WRITE));
696 if (error) {
697 #ifdef AHC_DEBUG
698 printf("%s: in ahc_setup_data(): bus_dmamap_load() "
699 "= %d\n",
700 ahc_name(ahc), error);
701 #endif
702 xs->error = XS_RESOURCE_SHORTAGE;
703 scsipi_done(xs);
704 return;
705 }
706 ahc_execute_scb(scb,
707 scb->dmamap->dm_segs,
708 scb->dmamap->dm_nsegs);
709 } else {
710 ahc_execute_scb(scb, NULL, 0);
711 }
712 }
713
714 static void
715 ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) {
716
717 if ((scb->flags & SCB_RECOVERY_SCB) == 0) {
718 struct scb *list_scb;
719
720 scb->flags |= SCB_RECOVERY_SCB;
721
722 /*
723 * Take all queued, but not sent SCBs out of the equation.
724 * Also ensure that no new CCBs are queued to us while we
725 * try to fix this problem.
726 */
727 scsipi_channel_freeze(&ahc->sc_channel, 1);
728 if (ahc->features & AHC_TWIN)
729 scsipi_channel_freeze(&ahc->sc_channel_b, 1);
730
731 /*
732 * Go through all of our pending SCBs and remove
733 * any scheduled timeouts for them. We will reschedule
734 * them after we've successfully fixed this problem.
735 */
736 LIST_FOREACH(list_scb, &ahc->pending_scbs, pending_links) {
737 callout_stop(&list_scb->xs->xs_callout);
738 }
739 }
740 }
741
742 void
743 ahc_timeout(void *arg)
744 {
745 struct scb *scb;
746 struct ahc_softc *ahc;
747 long s;
748 int found;
749 u_int last_phase;
750 int target;
751 int lun;
752 int i;
753 char channel;
754
755 scb = (struct scb *)arg;
756 ahc = (struct ahc_softc *)scb->ahc_softc;
757
758 ahc_lock(ahc, &s);
759
760 ahc_pause_and_flushwork(ahc);
761
762 if ((scb->flags & SCB_ACTIVE) == 0) {
763 /* Previous timeout took care of me already */
764 printf("%s: Timedout SCB already complete. "
765 "Interrupts may not be functioning.\n", ahc_name(ahc));
766 ahc_unpause(ahc);
767 ahc_unlock(ahc, &s);
768 return;
769 }
770
771 target = SCB_GET_TARGET(ahc, scb);
772 channel = SCB_GET_CHANNEL(ahc, scb);
773 lun = SCB_GET_LUN(scb);
774
775 ahc_print_path(ahc, scb);
776 printf("SCB 0x%x - timed out\n", scb->hscb->tag);
777 ahc_dump_card_state(ahc);
778 last_phase = ahc_inb(ahc, LASTPHASE);
779 if (scb->sg_count > 0) {
780 for (i = 0; i < scb->sg_count; i++) {
781 printf("sg[%d] - Addr 0x%x : Length %d\n",
782 i,
783 scb->sg_list[i].addr,
784 scb->sg_list[i].len & AHC_SG_LEN_MASK);
785 }
786 }
787 if (scb->flags & (SCB_DEVICE_RESET|SCB_ABORT)) {
788 /*
789 * Been down this road before.
790 * Do a full bus reset.
791 */
792 bus_reset:
793 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
794 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE);
795 printf("%s: Issued Channel %c Bus Reset. "
796 "%d SCBs aborted\n", ahc_name(ahc), channel, found);
797 } else {
798 /*
799 * If we are a target, transition to bus free and report
800 * the timeout.
801 *
802 * The target/initiator that is holding up the bus may not
803 * be the same as the one that triggered this timeout
804 * (different commands have different timeout lengths).
805 * If the bus is idle and we are acting as the initiator
806 * for this request, queue a BDR message to the timed out
807 * target. Otherwise, if the timed out transaction is
808 * active:
809 * Initiator transaction:
810 * Stuff the message buffer with a BDR message and assert
811 * ATN in the hopes that the target will let go of the bus
812 * and go to the mesgout phase. If this fails, we'll
813 * get another timeout 2 seconds later which will attempt
814 * a bus reset.
815 *
816 * Target transaction:
817 * Transition to BUS FREE and report the error.
818 * It's good to be the target!
819 */
820 u_int active_scb_index;
821 u_int saved_scbptr;
822
823 saved_scbptr = ahc_inb(ahc, SCBPTR);
824 active_scb_index = ahc_inb(ahc, SCB_TAG);
825
826 if ((ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0
827 && (active_scb_index < ahc->scb_data->numscbs)) {
828 struct scb *active_scb;
829
830 /*
831 * If the active SCB is not us, assume that
832 * the active SCB has a longer timeout than
833 * the timedout SCB, and wait for the active
834 * SCB to timeout.
835 */
836 active_scb = ahc_lookup_scb(ahc, active_scb_index);
837 if (active_scb != scb) {
838 uint64_t newtimeout;
839
840 ahc_print_path(ahc, scb);
841 printf("Other SCB Timeout%s",
842 (scb->flags & SCB_OTHERTCL_TIMEOUT) != 0
843 ? " again\n" : "\n");
844 scb->flags |= SCB_OTHERTCL_TIMEOUT;
845 newtimeout = MAX(active_scb->xs->timeout,
846 scb->xs->timeout);
847 callout_reset(&scb->xs->xs_callout,
848 newtimeout > 1000000 ?
849 (newtimeout / 1000) * hz :
850 (newtimeout * hz) / 1000,
851 ahc_timeout, scb);
852 ahc_unpause(ahc);
853 ahc_unlock(ahc, &s);
854 return;
855 }
856
857 /* It's us */
858 if ((scb->flags & SCB_TARGET_SCB) != 0) {
859
860 /*
861 * Send back any queued up transactions
862 * and properly record the error condition.
863 */
864 ahc_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
865 SCB_GET_CHANNEL(ahc, scb),
866 SCB_GET_LUN(scb),
867 scb->hscb->tag,
868 ROLE_TARGET,
869 CAM_CMD_TIMEOUT);
870
871 /* Will clear us from the bus */
872 ahc_restart(ahc);
873 ahc_unlock(ahc, &s);
874 return;
875 }
876
877 ahc_set_recoveryscb(ahc, active_scb);
878 ahc_outb(ahc, MSG_OUT, HOST_MSG);
879 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
880 ahc_print_path(ahc, active_scb);
881 printf("BDR message in message buffer\n");
882 active_scb->flags |= SCB_DEVICE_RESET;
883 callout_reset(&active_scb->xs->xs_callout,
884 2 * hz, ahc_timeout, active_scb);
885 ahc_unpause(ahc);
886 } else {
887 int disconnected;
888
889 /* XXX Shouldn't panic. Just punt instead? */
890 if ((scb->flags & SCB_TARGET_SCB) != 0)
891 panic("Timed-out target SCB but bus idle");
892
893 if (last_phase != P_BUSFREE
894 && (ahc_inb(ahc, SSTAT0) & TARGET) != 0) {
895 /* XXX What happened to the SCB? */
896 /* Hung target selection. Goto busfree */
897 printf("%s: Hung target selection\n",
898 ahc_name(ahc));
899 ahc_restart(ahc);
900 ahc_unlock(ahc, &s);
901 return;
902 }
903
904 if (ahc_search_qinfifo(ahc, target, channel, lun,
905 scb->hscb->tag, ROLE_INITIATOR,
906 /*status*/0, SEARCH_COUNT) > 0) {
907 disconnected = FALSE;
908 } else {
909 disconnected = TRUE;
910 }
911
912 if (disconnected) {
913
914 ahc_set_recoveryscb(ahc, scb);
915 /*
916 * Actually re-queue this SCB in an attempt
917 * to select the device before it reconnects.
918 * In either case (selection or reselection),
919 * we will now issue a target reset to the
920 * timed-out device.
921 *
922 * Set the MK_MESSAGE control bit indicating
923 * that we desire to send a message. We
924 * also set the disconnected flag since
925 * in the paging case there is no guarantee
926 * that our SCB control byte matches the
927 * version on the card. We don't want the
928 * sequencer to abort the command thinking
929 * an unsolicited reselection occurred.
930 */
931 scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
932 scb->flags |= SCB_DEVICE_RESET;
933
934 /*
935 * Remove any cached copy of this SCB in the
936 * disconnected list in preparation for the
937 * queuing of our abort SCB. We use the
938 * same element in the SCB, SCB_NEXT, for
939 * both the qinfifo and the disconnected list.
940 */
941 ahc_search_disc_list(ahc, target, channel,
942 lun, scb->hscb->tag,
943 /*stop_on_first*/TRUE,
944 /*remove*/TRUE,
945 /*save_state*/FALSE);
946
947 /*
948 * In the non-paging case, the sequencer will
949 * never re-reference the in-core SCB.
950 * To make sure we are notified during
951 * reslection, set the MK_MESSAGE flag in
952 * the card's copy of the SCB.
953 */
954 if ((ahc->flags & AHC_PAGESCBS) == 0) {
955 ahc_outb(ahc, SCBPTR, scb->hscb->tag);
956 ahc_outb(ahc, SCB_CONTROL,
957 ahc_inb(ahc, SCB_CONTROL)
958 | MK_MESSAGE);
959 }
960
961 /*
962 * Clear out any entries in the QINFIFO first
963 * so we are the next SCB for this target
964 * to run.
965 */
966 ahc_search_qinfifo(ahc,
967 SCB_GET_TARGET(ahc, scb),
968 channel, SCB_GET_LUN(scb),
969 SCB_LIST_NULL,
970 ROLE_INITIATOR,
971 CAM_REQUEUE_REQ,
972 SEARCH_COMPLETE);
973 ahc_print_path(ahc, scb);
974 printf("Queuing a BDR SCB\n");
975 ahc_qinfifo_requeue_tail(ahc, scb);
976 ahc_outb(ahc, SCBPTR, saved_scbptr);
977 callout_reset(&scb->xs->xs_callout, 2 * hz,
978 ahc_timeout, scb);
979 ahc_unpause(ahc);
980 } else {
981 /* Go "immediatly" to the bus reset */
982 /* This shouldn't happen */
983 ahc_set_recoveryscb(ahc, scb);
984 ahc_print_path(ahc, scb);
985 printf("SCB %d: Immediate reset. "
986 "Flags = 0x%x\n", scb->hscb->tag,
987 scb->flags);
988 goto bus_reset;
989 }
990 }
991 }
992 ahc_unlock(ahc, &s);
993 }
994
995 void
996 ahc_platform_set_tags(struct ahc_softc *ahc,
997 struct ahc_devinfo *devinfo, int enable)
998 {
999 struct ahc_tmode_tstate *tstate;
1000
1001 ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
1002 devinfo->target, &tstate);
1003
1004 if (enable)
1005 tstate->tagenable |= devinfo->target_mask;
1006 else
1007 tstate->tagenable &= ~devinfo->target_mask;
1008 }
1009
1010 int
1011 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1012 {
1013 if (sizeof(struct ahc_platform_data) == 0)
1014 return 0;
1015 ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1016 M_NOWAIT);
1017 if (ahc->platform_data == NULL)
1018 return (ENOMEM);
1019 return (0);
1020 }
1021
1022 void
1023 ahc_platform_free(struct ahc_softc *ahc)
1024 {
1025 if (sizeof(struct ahc_platform_data) == 0)
1026 return;
1027 free(ahc->platform_data, M_DEVBUF);
1028 }
1029
1030 int
1031 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1032 {
1033 return (0);
1034 }
1035
1036 int
1037 ahc_detach(struct device *self, int flags)
1038 {
1039 int rv = 0;
1040
1041 struct ahc_softc *ahc = (struct ahc_softc*)self;
1042
1043 ahc_intr_enable(ahc, FALSE);
1044 if (ahc->sc_child != NULL)
1045 rv = config_detach(ahc->sc_child, flags);
1046 if (rv == 0 && ahc->sc_child_b != NULL)
1047 rv = config_detach(ahc->sc_child_b, flags);
1048
1049 shutdownhook_disestablish(ahc->shutdown_hook);
1050
1051 ahc_free(ahc);
1052
1053 return (rv);
1054 }
1055
1056
1057 void
1058 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target, u_int lun,
1059 ac_code code, void *opt_arg)
1060 {
1061 struct ahc_tmode_tstate *tstate;
1062 struct ahc_initiator_tinfo *tinfo;
1063 struct ahc_devinfo devinfo;
1064 struct scsipi_channel *chan;
1065 struct scsipi_xfer_mode xm;
1066
1067 chan = channel == 'B' ? &ahc->sc_channel_b : &ahc->sc_channel;
1068 switch (code) {
1069 case AC_TRANSFER_NEG:
1070 tinfo = ahc_fetch_transinfo(ahc, channel, ahc->our_id, target,
1071 &tstate);
1072 ahc_compile_devinfo(&devinfo, ahc->our_id, target, lun,
1073 channel, ROLE_UNKNOWN);
1074 /*
1075 * Don't bother if negotiating. XXX?
1076 */
1077 if (tinfo->curr.period != tinfo->goal.period
1078 || tinfo->curr.width != tinfo->goal.width
1079 || tinfo->curr.offset != tinfo->goal.offset
1080 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
1081 break;
1082 xm.xm_target = target;
1083 xm.xm_mode = 0;
1084 xm.xm_period = tinfo->curr.period;
1085 xm.xm_offset = tinfo->curr.offset;
1086 if (tinfo->curr.width == MSG_EXT_WDTR_BUS_16_BIT)
1087 xm.xm_mode |= PERIPH_CAP_WIDE16;
1088 if (tinfo->curr.period)
1089 xm.xm_mode |= PERIPH_CAP_SYNC;
1090 if (tstate->tagenable & devinfo.target_mask)
1091 xm.xm_mode |= PERIPH_CAP_TQING;
1092 if (tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ)
1093 xm.xm_mode |= PERIPH_CAP_DT;
1094 scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, &xm);
1095 break;
1096 case AC_BUS_RESET:
1097 scsipi_async_event(chan, ASYNC_EVENT_RESET, NULL);
1098 case AC_SENT_BDR:
1099 default:
1100 break;
1101 }
1102 }
1103