Deleted Added
full compact
1/*
2 * Generic driver for the BusLogic MultiMaster SCSI host adapters
3 * Product specific probe and attach routines can be found in:
4 * i386/isa/bt_isa.c BT-54X, BT-445 cards
5 * i386/eisa/bt_eisa.c BT-74x, BT-75x cards
6 * pci/bt_pci.c BT-946, BT-948, BT-956, BT-958 cards
7 *
8 * Copyright (c) 1998, 1999 Justin T. Gibbs.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification, immediately at the beginning of the file.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $Id: bt.c,v 1.21 1999/05/08 21:59:00 dfr Exp $
32 * $Id: bt.c,v 1.22 1999/05/23 18:54:34 gibbs Exp $
33 */
34
35 /*
36 * Special thanks to Leonard N. Zubkoff for writing such a complete and
37 * well documented Mylex/BusLogic MultiMaster driver for Linux. Support
38 * in this driver for the wide range of MultiMaster controllers and
39 * firmware revisions, with their otherwise undocumented quirks, would not
40 * have been possible without his efforts.
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/malloc.h>
46#include <sys/buf.h>
47#include <sys/kernel.h>
48#include <sys/sysctl.h>
49#include <sys/bus.h>
50
51/*
52 * XXX It appears that BusLogic PCI adapters go out to lunch if you
53 * attempt to perform memory mapped I/O.
54 */
55#if 0
56#include "pci.h"
57#if NPCI > 0
58#include <machine/bus_memio.h>
59#endif
60#endif
61#include <machine/bus_pio.h>
62#include <machine/bus.h>
63#include <machine/clock.h>
64#include <sys/rman.h>
65
66#include <cam/cam.h>
67#include <cam/cam_ccb.h>
68#include <cam/cam_sim.h>
69#include <cam/cam_xpt_sim.h>
70#include <cam/cam_debug.h>
71
72#include <cam/scsi/scsi_message.h>
73
74#include <vm/vm.h>
75#include <vm/pmap.h>
76
77#include <dev/buslogic/btreg.h>
78
79#ifndef MAX
80#define MAX(a, b) ((a) > (b) ? (a) : (b))
81#endif
82
83/* MailBox Management functions */
84static __inline void btnextinbox(struct bt_softc *bt);
85static __inline void btnextoutbox(struct bt_softc *bt);
86
87static __inline void
88btnextinbox(struct bt_softc *bt)
89{
90 if (bt->cur_inbox == bt->last_inbox)
91 bt->cur_inbox = bt->in_boxes;
92 else
93 bt->cur_inbox++;
94}
95
96static __inline void
97btnextoutbox(struct bt_softc *bt)
98{
99 if (bt->cur_outbox == bt->last_outbox)
100 bt->cur_outbox = bt->out_boxes;
101 else
102 bt->cur_outbox++;
103}
104
105/* CCB Mangement functions */
106static __inline u_int32_t btccbvtop(struct bt_softc *bt,
107 struct bt_ccb *bccb);
108static __inline struct bt_ccb* btccbptov(struct bt_softc *bt,
109 u_int32_t ccb_addr);
110static __inline u_int32_t btsensepaddr(struct bt_softc *bt,
111 struct bt_ccb *bccb);
112static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
113 struct bt_ccb *bccb);
114
115static __inline u_int32_t
116btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
117{
118 return (bt->bt_ccb_physbase
119 + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
120}
121
122static __inline struct bt_ccb *
123btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
124{
125 return (bt->bt_ccb_array +
126 ((struct bt_ccb*)ccb_addr-(struct bt_ccb*)bt->bt_ccb_physbase));
127}
128
129static __inline u_int32_t
130btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
131{
132 u_int index;
133
134 index = (u_int)(bccb - bt->bt_ccb_array);
135 return (bt->sense_buffers_physbase
136 + (index * sizeof(struct scsi_sense_data)));
137}
138
139static __inline struct scsi_sense_data *
140btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
141{
142 u_int index;
143
144 index = (u_int)(bccb - bt->bt_ccb_array);
145 return (bt->sense_buffers + index);
146}
147
148static __inline struct bt_ccb* btgetccb(struct bt_softc *bt);
149static __inline void btfreeccb(struct bt_softc *bt,
150 struct bt_ccb *bccb);
151static void btallocccbs(struct bt_softc *bt);
152static bus_dmamap_callback_t btexecuteccb;
153static void btdone(struct bt_softc *bt, struct bt_ccb *bccb,
154 bt_mbi_comp_code_t comp_code);
155
156/* Host adapter command functions */
157static int btreset(struct bt_softc* bt, int hard_reset);
158
159/* Initialization functions */
160static int btinitmboxes(struct bt_softc *bt);
161static bus_dmamap_callback_t btmapmboxes;
162static bus_dmamap_callback_t btmapccbs;
163static bus_dmamap_callback_t btmapsgs;
164
165/* Transfer Negotiation Functions */
166static void btfetchtransinfo(struct bt_softc *bt,
167 struct ccb_trans_settings *cts);
168
169/* CAM SIM entry points */
170#define ccb_bccb_ptr spriv_ptr0
171#define ccb_bt_ptr spriv_ptr1
172static void btaction(struct cam_sim *sim, union ccb *ccb);
173static void btpoll(struct cam_sim *sim);
174
175/* Our timeout handler */
176timeout_t bttimeout;
177
178u_long bt_unit = 0;
179
180/*
181 * XXX
182 * Do our own re-probe protection until a configuration
183 * manager can do it for us. This ensures that we don't
184 * reprobe a card already found by the EISA or PCI probes.
185 */
186struct bt_isa_port bt_isa_ports[] =
187{
188 { 0x130, 0, 4 },
189 { 0x134, 0, 5 },
190 { 0x230, 0, 2 },
191 { 0x234, 0, 3 },
192 { 0x330, 0, 0 },
193 { 0x334, 0, 1 }
194};
195
196/*
197 * I/O ports listed in the order enumerated by the
198 * card for certain op codes.
199 */
200u_int16_t bt_board_ports[] =
201{
202 0x330,
203 0x334,
204 0x230,
205 0x234,
206 0x130,
207 0x134
208};
209
210/* Exported functions */
211void
212bt_init_softc(device_t dev, struct resource *port,
213 struct resource *irq, struct resource *drq)
214{
215 struct bt_softc *bt = device_get_softc(dev);
216
217 SLIST_INIT(&bt->free_bt_ccbs);
218 LIST_INIT(&bt->pending_ccbs);
219 SLIST_INIT(&bt->sg_maps);
220 bt->dev = dev;
221 bt->unit = device_get_unit(dev);
222 bt->port = port;
223 bt->irq = irq;
224 bt->drq = drq;
225 bt->tag = rman_get_bustag(port);
226 bt->bsh = rman_get_bushandle(port);
227}
228
229void
230bt_free_softc(device_t dev)
231{
232 struct bt_softc *bt = device_get_softc(dev);
233
234 switch (bt->init_level) {
235 default:
236 case 11:
237 bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
238 case 10:
239 bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
240 bt->sense_dmamap);
241 case 9:
242 bus_dma_tag_destroy(bt->sense_dmat);
243 case 8:
244 {
245 struct sg_map_node *sg_map;
246
247 while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
248 SLIST_REMOVE_HEAD(&bt->sg_maps, links);
249 bus_dmamap_unload(bt->sg_dmat,
250 sg_map->sg_dmamap);
251 bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
252 sg_map->sg_dmamap);
253 free(sg_map, M_DEVBUF);
254 }
255 bus_dma_tag_destroy(bt->sg_dmat);
256 }
257 case 7:
258 bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
259 case 6:
260 bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
261 bt->ccb_dmamap);
262 bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
263 case 5:
264 bus_dma_tag_destroy(bt->ccb_dmat);
265 case 4:
266 bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
267 case 3:
268 bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
269 bt->mailbox_dmamap);
270 bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
271 case 2:
272 bus_dma_tag_destroy(bt->buffer_dmat);
273 case 1:
274 bus_dma_tag_destroy(bt->mailbox_dmat);
275 case 0:
276 break;
277 }
278}
279
280int
281bt_port_probe(device_t dev, struct bt_probe_info *info)
282{
283 struct bt_softc *bt = device_get_softc(dev);
284 config_data_t config_data;
285 int error;
286
287 /* See if there is really a card present */
288 if (bt_probe(dev) || bt_fetch_adapter_info(dev))
289 return(1);
290
291 /*
292 * Determine our IRQ, and DMA settings and
293 * export them to the configuration system.
294 */
295 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
296 (u_int8_t*)&config_data, sizeof(config_data),
297 DEFAULT_CMD_TIMEOUT);
298 if (error != 0) {
299 printf("bt_port_probe: Could not determine IRQ or DMA "
300 "settings for adapter.\n");
301 return (1);
302 }
303
304 if (bt->model[0] == '5') {
305 /* DMA settings only make sense for ISA cards */
306 switch (config_data.dma_chan) {
307 case DMA_CHAN_5:
308 info->drq = 5;
309 break;
310 case DMA_CHAN_6:
311 info->drq = 6;
312 break;
313 case DMA_CHAN_7:
314 info->drq = 7;
315 break;
316 default:
317 printf("bt_port_probe: Invalid DMA setting "
318 "detected for adapter.\n");
319 return (1);
320 }
321 } else {
322 /* VL/EISA/PCI DMA */
323 info->drq = -1;
324 }
325 switch (config_data.irq) {
326 case IRQ_9:
327 case IRQ_10:
328 case IRQ_11:
329 case IRQ_12:
330 case IRQ_14:
331 case IRQ_15:
332 info->irq = ffs(config_data.irq) + 8;
333 break;
334 default:
335 printf("bt_port_probe: Invalid IRQ setting %x"
336 "detected for adapter.\n", config_data.irq);
337 return (1);
338 }
339 return (0);
340}
341
342/*
343 * Probe the adapter and verify that the card is a BusLogic.
344 */
345int
346bt_probe(device_t dev)
347{
348 struct bt_softc *bt = device_get_softc(dev);
349 esetup_info_data_t esetup_info;
350 u_int status;
351 u_int intstat;
352 u_int geometry;
353 int error;
354 u_int8_t param;
355
356 /*
357 * See if the three I/O ports look reasonable.
358 * Touch the minimal number of registers in the
359 * failure case.
360 */
361 status = bt_inb(bt, STATUS_REG);
362 if ((status == 0)
363 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
364 STATUS_REG_RSVD|CMD_INVALID)) != 0) {
365 if (bootverbose)
366 device_printf(dev, "Failed Status Reg Test - %x\n",
367 status);
368 return (ENXIO);
369 }
370
371 intstat = bt_inb(bt, INTSTAT_REG);
372 if ((intstat & INTSTAT_REG_RSVD) != 0) {
373 device_printf(dev, "Failed Intstat Reg Test\n");
374 return (ENXIO);
375 }
376
377 geometry = bt_inb(bt, GEOMETRY_REG);
378 if (geometry == 0xFF) {
379 if (bootverbose)
380 device_printf(dev, "Failed Geometry Reg Test\n");
381 return (ENXIO);
382 }
383
384 /*
385 * Looking good so far. Final test is to reset the
386 * adapter and attempt to fetch the extended setup
387 * information. This should filter out all 1542 cards.
388 */
389 if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
390 if (bootverbose)
391 device_printf(dev, "Failed Reset\n");
392 return (ENXIO);
393 }
394
395 param = sizeof(esetup_info);
396 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &param, /*parmlen*/1,
397 (u_int8_t*)&esetup_info, sizeof(esetup_info),
398 DEFAULT_CMD_TIMEOUT);
399 if (error != 0) {
400 return (ENXIO);
401 }
402
403 return (0);
404}
405
406/*
407 * Pull the boards setup information and record it in our softc.
408 */
409int
410bt_fetch_adapter_info(device_t dev)
411{
412 struct bt_softc *bt = device_get_softc(dev);
413 board_id_data_t board_id;
414 esetup_info_data_t esetup_info;
415 config_data_t config_data;
416 int error;
417 u_int8_t length_param;
418
419 /* First record the firmware version */
420 error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
421 (u_int8_t*)&board_id, sizeof(board_id),
422 DEFAULT_CMD_TIMEOUT);
423 if (error != 0) {
424 device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
425 return (error);
426 }
427 bt->firmware_ver[0] = board_id.firmware_rev_major;
428 bt->firmware_ver[1] = '.';
429 bt->firmware_ver[2] = board_id.firmware_rev_minor;
430 bt->firmware_ver[3] = '\0';
431
432 /*
433 * Depending on the firmware major and minor version,
434 * we may be able to fetch additional minor version info.
435 */
436 if (bt->firmware_ver[0] > '0') {
437
438 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
439 (u_int8_t*)&bt->firmware_ver[3], 1,
440 DEFAULT_CMD_TIMEOUT);
441 if (error != 0) {
442 device_printf(dev,
443 "bt_fetch_adapter_info - Failed Get "
444 "Firmware 3rd Digit\n");
445 return (error);
446 }
447 if (bt->firmware_ver[3] == ' ')
448 bt->firmware_ver[3] = '\0';
449 bt->firmware_ver[4] = '\0';
450 }
451
452 if (strcmp(bt->firmware_ver, "3.3") >= 0) {
453
454 error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
455 (u_int8_t*)&bt->firmware_ver[4], 1,
456 DEFAULT_CMD_TIMEOUT);
457 if (error != 0) {
458 device_printf(dev,
459 "bt_fetch_adapter_info - Failed Get "
460 "Firmware 4th Digit\n");
461 return (error);
462 }
463 if (bt->firmware_ver[4] == ' ')
464 bt->firmware_ver[4] = '\0';
465 bt->firmware_ver[5] = '\0';
466 }
467
468 /*
469 * Some boards do not handle the "recently documented"
470 * Inquire Board Model Number command correctly or do not give
471 * exact information. Use the Firmware and Extended Setup
472 * information in these cases to come up with the right answer.
473 * The major firmware revision number indicates:
474 *
475 * 5.xx BusLogic "W" Series Host Adapters:
476 * BT-948/958/958D
477 * 4.xx BusLogic "C" Series Host Adapters:
478 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
479 * 3.xx BusLogic "S" Series Host Adapters:
480 * BT-747S/747D/757S/757D/445S/545S/542D
481 * BT-542B/742A (revision H)
482 * 2.xx BusLogic "A" Series Host Adapters:
483 * BT-542B/742A (revision G and below)
484 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
485 */
486 length_param = sizeof(esetup_info);
487 error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
488 (u_int8_t*)&esetup_info, sizeof(esetup_info),
489 DEFAULT_CMD_TIMEOUT);
490 if (error != 0) {
491 return (error);
492 }
493
494 bt->bios_addr = esetup_info.bios_addr << 12;
495
496 if (esetup_info.bus_type == 'A'
497 && bt->firmware_ver[0] == '2') {
498 snprintf(bt->model, sizeof(bt->model), "542B");
499 } else if (esetup_info.bus_type == 'E'
500 && (strncmp(bt->firmware_ver, "2.1", 3) == 0
501 || strncmp(bt->firmware_ver, "2.20", 4) == 0)) {
502 snprintf(bt->model, sizeof(bt->model), "742A");
503 } else if (esetup_info.bus_type == 'E'
504 && bt->firmware_ver[0] == '0') {
505 /* AMI FastDisk EISA Series 441 0.x */
506 snprintf(bt->model, sizeof(bt->model), "747A");
507 } else {
508 ha_model_data_t model_data;
509 int i;
510
511 length_param = sizeof(model_data);
512 error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
513 (u_int8_t*)&model_data, sizeof(model_data),
514 DEFAULT_CMD_TIMEOUT);
515 if (error != 0) {
516 device_printf(dev,
517 "bt_fetch_adapter_info - Failed Inquire "
518 "Model Number\n");
519 return (error);
520 }
521 for (i = 0; i < sizeof(model_data.ascii_model); i++) {
522 bt->model[i] = model_data.ascii_model[i];
523 if (bt->model[i] == ' ')
524 break;
525 }
526 bt->model[i] = '\0';
527 }
528
529 bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
530
531 /* SG element limits */
532 bt->max_sg = esetup_info.max_sg;
533
534 /* Set feature flags */
535 bt->wide_bus = esetup_info.wide_bus;
536 bt->diff_bus = esetup_info.diff_bus;
537 bt->ultra_scsi = esetup_info.ultra_scsi;
538
539 if ((bt->firmware_ver[0] == '5')
540 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
541 bt->extended_lun = TRUE;
542
543 bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
544
545 bt->extended_trans =
546 ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
547
548 /*
549 * Determine max CCB count and whether tagged queuing is
550 * available based on controller type. Tagged queuing
551 * only works on 'W' series adapters, 'C' series adapters
552 * with firmware of rev 4.42 and higher, and 'S' series
553 * adapters with firmware of rev 3.35 and higher. The
554 * maximum CCB counts are as follows:
555 *
556 * 192 BT-948/958/958D
557 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
558 * 50 BT-545C/540CF
559 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
560 */
561 if (bt->firmware_ver[0] == '5') {
562 bt->max_ccbs = 192;
563 bt->tag_capable = TRUE;
564 } else if (bt->firmware_ver[0] == '4') {
565 if (bt->model[0] == '5')
566 bt->max_ccbs = 50;
567 else
568 bt->max_ccbs = 100;
569 bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
570 } else {
571 bt->max_ccbs = 30;
572 if (bt->firmware_ver[0] == '3'
573 && (strcmp(bt->firmware_ver, "3.35") >= 0))
574 bt->tag_capable = TRUE;
575 else
576 bt->tag_capable = FALSE;
577 }
578
579 if (bt->tag_capable != FALSE)
580 bt->tags_permitted = ALL_TARGETS;
581
582 /* Determine Sync/Wide/Disc settings */
583 if (bt->firmware_ver[0] >= '4') {
584 auto_scsi_data_t auto_scsi_data;
585 fetch_lram_params_t fetch_lram_params;
586 int error;
587
588 /*
589 * These settings are stored in the
590 * AutoSCSI data in LRAM of 'W' and 'C'
591 * adapters.
592 */
593 fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
594 fetch_lram_params.response_len = sizeof(auto_scsi_data);
595 error = bt_cmd(bt, BOP_FETCH_LRAM,
596 (u_int8_t*)&fetch_lram_params,
597 sizeof(fetch_lram_params),
598 (u_int8_t*)&auto_scsi_data,
599 sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
600
601 if (error != 0) {
602 device_printf(dev,
603 "bt_fetch_adapter_info - Failed "
604 "Get Auto SCSI Info\n");
605 return (error);
606 }
607
608 bt->disc_permitted = auto_scsi_data.low_disc_permitted
609 | (auto_scsi_data.high_disc_permitted << 8);
610 bt->sync_permitted = auto_scsi_data.low_sync_permitted
611 | (auto_scsi_data.high_sync_permitted << 8);
612 bt->fast_permitted = auto_scsi_data.low_fast_permitted
613 | (auto_scsi_data.high_fast_permitted << 8);
614 bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
615 | (auto_scsi_data.high_ultra_permitted << 8);
616 bt->wide_permitted = auto_scsi_data.low_wide_permitted
617 | (auto_scsi_data.high_wide_permitted << 8);
618
619 if (bt->ultra_scsi == FALSE)
620 bt->ultra_permitted = 0;
621
622 if (bt->wide_bus == FALSE)
623 bt->wide_permitted = 0;
624 } else {
625 /*
626 * 'S' and 'A' series have this information in the setup
627 * information structure.
628 */
629 setup_data_t setup_info;
630
631 length_param = sizeof(setup_info);
632 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
633 /*paramlen*/1, (u_int8_t*)&setup_info,
634 sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
635
636 if (error != 0) {
637 device_printf(dev,
638 "bt_fetch_adapter_info - Failed "
639 "Get Setup Info\n");
640 return (error);
641 }
642
643 if (setup_info.initiate_sync != 0) {
644 bt->sync_permitted = ALL_TARGETS;
645
646 if (bt->model[0] == '7') {
647 if (esetup_info.sync_neg10MB != 0)
648 bt->fast_permitted = ALL_TARGETS;
649 if (strcmp(bt->model, "757") == 0)
650 bt->wide_permitted = ALL_TARGETS;
651 }
652 }
653 bt->disc_permitted = ALL_TARGETS;
654 }
655
656 /* We need as many mailboxes as we can have ccbs */
657 bt->num_boxes = bt->max_ccbs;
658
659 /* Determine our SCSI ID */
660
661 error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
662 (u_int8_t*)&config_data, sizeof(config_data),
663 DEFAULT_CMD_TIMEOUT);
664 if (error != 0) {
665 device_printf(dev,
666 "bt_fetch_adapter_info - Failed Get Config\n");
667 return (error);
668 }
669 bt->scsi_id = config_data.scsi_id;
670
671 return (0);
672}
673
674/*
675 * Start the board, ready for normal operation
676 */
677int
678bt_init(device_t dev)
679{
680 struct bt_softc *bt = device_get_softc(dev);
681
682 /* Announce the Adapter */
683 device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
684
685 if (bt->ultra_scsi != 0)
686 printf("Ultra ");
687
688 if (bt->wide_bus != 0)
689 printf("Wide ");
690 else
691 printf("Narrow ");
692
693 if (bt->diff_bus != 0)
694 printf("Diff ");
695
696 printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
697 bt->max_ccbs);
698
699 /*
700 * Create our DMA tags. These tags define the kinds of device
701 * accessable memory allocations and memory mappings we will
702 * need to perform during normal operation.
703 *
704 * Unless we need to further restrict the allocation, we rely
705 * on the restrictions of the parent dmat, hence the common
706 * use of MAXADDR and MAXSIZE.
707 */
708
709 /* DMA tag for mapping buffers into device visible space. */
710 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/0, /*boundary*/0,
710 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
711 /*lowaddr*/BUS_SPACE_MAXADDR,
712 /*highaddr*/BUS_SPACE_MAXADDR,
713 /*filter*/NULL, /*filterarg*/NULL,
714 /*maxsize*/MAXBSIZE, /*nsegments*/BT_NSEG,
715 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
716 /*flags*/BUS_DMA_ALLOCNOW,
717 &bt->buffer_dmat) != 0) {
718 goto error_exit;
719 }
720
721 bt->init_level++;
722 /* DMA tag for our mailboxes */
723 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/0, /*boundary*/0,
723 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
724 /*lowaddr*/BUS_SPACE_MAXADDR,
725 /*highaddr*/BUS_SPACE_MAXADDR,
726 /*filter*/NULL, /*filterarg*/NULL,
727 bt->num_boxes * (sizeof(bt_mbox_in_t)
728 + sizeof(bt_mbox_out_t)),
729 /*nsegments*/1,
730 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
731 /*flags*/0, &bt->mailbox_dmat) != 0) {
732 goto error_exit;
733 }
734
735 bt->init_level++;
736
737 /* Allocation for our mailboxes */
738 if (bus_dmamem_alloc(bt->mailbox_dmat, (void **)&bt->out_boxes,
739 BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
740 goto error_exit;
741 }
742
743 bt->init_level++;
744
745 /* And permanently map them */
746 bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
747 bt->out_boxes,
748 bt->num_boxes * (sizeof(bt_mbox_in_t)
749 + sizeof(bt_mbox_out_t)),
750 btmapmboxes, bt, /*flags*/0);
751
752 bt->init_level++;
753
754 bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
755
756 btinitmboxes(bt);
757
758 /* DMA tag for our ccb structures */
759 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/0, /*boundary*/0,
759 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
760 /*lowaddr*/BUS_SPACE_MAXADDR,
761 /*highaddr*/BUS_SPACE_MAXADDR,
762 /*filter*/NULL, /*filterarg*/NULL,
763 bt->max_ccbs * sizeof(struct bt_ccb),
764 /*nsegments*/1,
765 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
766 /*flags*/0, &bt->ccb_dmat) != 0) {
767 goto error_exit;
768 }
769
770 bt->init_level++;
771
772 /* Allocation for our ccbs */
773 if (bus_dmamem_alloc(bt->ccb_dmat, (void **)&bt->bt_ccb_array,
774 BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
775 goto error_exit;
776 }
777
778 bt->init_level++;
779
780 /* And permanently map them */
781 bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
782 bt->bt_ccb_array,
783 bt->max_ccbs * sizeof(struct bt_ccb),
784 btmapccbs, bt, /*flags*/0);
785
786 bt->init_level++;
787
788 /* DMA tag for our S/G structures. We allocate in page sized chunks */
789 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/0, /*boundary*/0,
789 if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
790 /*lowaddr*/BUS_SPACE_MAXADDR,
791 /*highaddr*/BUS_SPACE_MAXADDR,
792 /*filter*/NULL, /*filterarg*/NULL,
793 PAGE_SIZE, /*nsegments*/1,
794 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
795 /*flags*/0, &bt->sg_dmat) != 0) {
796 goto error_exit;
797 }
798
799 bt->init_level++;
800
801 /* Perform initial CCB allocation */
802 bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
803 btallocccbs(bt);
804
805 if (bt->num_ccbs == 0) {
806 device_printf(dev,
807 "bt_init - Unable to allocate initial ccbs\n");
808 goto error_exit;
809 }
810
811 /*
812 * Note that we are going and return (to probe)
813 */
814 return 0;
815
816error_exit:
817
818 return (ENXIO);
819}
820
821int
822bt_attach(device_t dev)
823{
824 struct bt_softc *bt = device_get_softc(dev);
825 int tagged_dev_openings;
826 struct cam_devq *devq;
827 int error;
828
829 /*
830 * We reserve 1 ccb for error recovery, so don't
831 * tell the XPT about it.
832 */
833 if (bt->tag_capable != 0)
834 tagged_dev_openings = bt->max_ccbs - 1;
835 else
836 tagged_dev_openings = 0;
837
838 /*
839 * Create the device queue for our SIM.
840 */
841 devq = cam_simq_alloc(bt->max_ccbs - 1);
842 if (devq == NULL)
843 return (ENOMEM);
844
845 /*
846 * Construct our SIM entry
847 */
848 bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
849 2, tagged_dev_openings, devq);
850 if (bt->sim == NULL) {
851 cam_simq_free(devq);
852 return (ENOMEM);
853 }
854
855 if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
856 cam_sim_free(bt->sim, /*free_devq*/TRUE);
857 return (ENXIO);
858 }
859
860 if (xpt_create_path(&bt->path, /*periph*/NULL,
861 cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
862 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
863 xpt_bus_deregister(cam_sim_path(bt->sim));
864 cam_sim_free(bt->sim, /*free_devq*/TRUE);
865 return (ENXIO);
866 }
867
868 /*
869 * Setup interrupt.
870 */
871 error = bus_setup_intr(dev, bt->irq, INTR_TYPE_CAM,
872 bt_intr, bt, &bt->ih);
873 if (error) {
874 device_printf(dev, "bus_setup_intr() failed: %d\n", error);
875 return (error);
876 }
877
878 return (0);
879}
880
881int
882bt_check_probed_iop(u_int ioport)
883{
884 u_int i;
885
886 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
887 if (bt_isa_ports[i].addr == ioport) {
888 if (bt_isa_ports[i].probed != 0)
889 return (1);
890 else {
891 return (0);
892 }
893 }
894 }
895 return (1);
896}
897
898void
899bt_mark_probed_bio(isa_compat_io_t port)
900{
901 if (port < BIO_DISABLED)
902 bt_mark_probed_iop(bt_board_ports[port]);
903}
904
905void
906bt_mark_probed_iop(u_int ioport)
907{
908 u_int i;
909
910 for (i = 0; i < BT_NUM_ISAPORTS; i++) {
911 if (ioport == bt_isa_ports[i].addr) {
912 bt_isa_ports[i].probed = 1;
913 break;
914 }
915 }
916}
917
918void
919bt_find_probe_range(int ioport, int *port_index, int *max_port_index)
920{
921 if (ioport > 0) {
922 int i;
923
924 for (i = 0;i < BT_NUM_ISAPORTS; i++)
925 if (ioport <= bt_isa_ports[i].addr)
926 break;
927 if ((i >= BT_NUM_ISAPORTS)
928 || (ioport != bt_isa_ports[i].addr)) {
929 printf("
930bt_isa_probe: Invalid baseport of 0x%x specified.
931bt_isa_probe: Nearest valid baseport is 0x%x.
932bt_isa_probe: Failing probe.\n",
933 ioport,
934 (i < BT_NUM_ISAPORTS)
935 ? bt_isa_ports[i].addr
936 : bt_isa_ports[BT_NUM_ISAPORTS - 1].addr);
937 *port_index = *max_port_index = -1;
938 return;
939 }
940 *port_index = *max_port_index = bt_isa_ports[i].bio;
941 } else {
942 *port_index = 0;
943 *max_port_index = BT_NUM_ISAPORTS - 1;
944 }
945}
946
947int
948bt_iop_from_bio(isa_compat_io_t bio_index)
949{
950 if (bio_index >= 0 && bio_index < BT_NUM_ISAPORTS)
951 return (bt_board_ports[bio_index]);
952 return (-1);
953}
954
955
956static void
957btallocccbs(struct bt_softc *bt)
958{
959 struct bt_ccb *next_ccb;
960 struct sg_map_node *sg_map;
961 bus_addr_t physaddr;
962 bt_sg_t *segs;
963 int newcount;
964 int i;
965
966 if (bt->num_ccbs >= bt->max_ccbs)
967 /* Can't allocate any more */
968 return;
969
970 next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
971
972 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
973
974 if (sg_map == NULL)
975 goto error_exit;
976
977 /* Allocate S/G space for the next batch of CCBS */
978 if (bus_dmamem_alloc(bt->sg_dmat, (void **)&sg_map->sg_vaddr,
979 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
980 free(sg_map, M_DEVBUF);
981 goto error_exit;
982 }
983
984 SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
985
986 bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
987 PAGE_SIZE, btmapsgs, bt, /*flags*/0);
988
989 segs = sg_map->sg_vaddr;
990 physaddr = sg_map->sg_physaddr;
991
992 newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
993 for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
994 int error;
995
996 next_ccb->sg_list = segs;
997 next_ccb->sg_list_phys = physaddr;
998 next_ccb->flags = BCCB_FREE;
999 error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
1000 &next_ccb->dmamap);
1001 if (error != 0)
1002 break;
1003 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
1004 segs += BT_NSEG;
1005 physaddr += (BT_NSEG * sizeof(bt_sg_t));
1006 next_ccb++;
1007 bt->num_ccbs++;
1008 }
1009
1010 /* Reserve a CCB for error recovery */
1011 if (bt->recovery_bccb == NULL) {
1012 bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1013 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1014 }
1015
1016 if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
1017 return;
1018
1019error_exit:
1020 device_printf(bt->dev, "Can't malloc BCCBs\n");
1021}
1022
1023static __inline void
1024btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
1025{
1026 int s;
1027
1028 s = splcam();
1029 if ((bccb->flags & BCCB_ACTIVE) != 0)
1030 LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
1031 if (bt->resource_shortage != 0
1032 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
1033 bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1034 bt->resource_shortage = FALSE;
1035 }
1036 bccb->flags = BCCB_FREE;
1037 SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
1038 bt->active_ccbs--;
1039 splx(s);
1040}
1041
1042static __inline struct bt_ccb*
1043btgetccb(struct bt_softc *bt)
1044{
1045 struct bt_ccb* bccb;
1046 int s;
1047
1048 s = splcam();
1049 if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
1050 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1051 bt->active_ccbs++;
1052 } else {
1053 btallocccbs(bt);
1054 bccb = SLIST_FIRST(&bt->free_bt_ccbs);
1055 if (bccb != NULL) {
1056 SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
1057 bt->active_ccbs++;
1058 }
1059 }
1060 splx(s);
1061
1062 return (bccb);
1063}
1064
1065static void
1066btaction(struct cam_sim *sim, union ccb *ccb)
1067{
1068 struct bt_softc *bt;
1069
1070 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
1071
1072 bt = (struct bt_softc *)cam_sim_softc(sim);
1073
1074 switch (ccb->ccb_h.func_code) {
1075 /* Common cases first */
1076 case XPT_SCSI_IO: /* Execute the requested I/O operation */
1077 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1078 {
1079 struct bt_ccb *bccb;
1080 struct bt_hccb *hccb;
1081
1082 /*
1083 * get a bccb to use.
1084 */
1085 if ((bccb = btgetccb(bt)) == NULL) {
1086 int s;
1087
1088 s = splcam();
1089 bt->resource_shortage = TRUE;
1090 splx(s);
1091 xpt_freeze_simq(bt->sim, /*count*/1);
1092 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1093 xpt_done(ccb);
1094 return;
1095 }
1096
1097 hccb = &bccb->hccb;
1098
1099 /*
1100 * So we can find the BCCB when an abort is requested
1101 */
1102 bccb->ccb = ccb;
1103 ccb->ccb_h.ccb_bccb_ptr = bccb;
1104 ccb->ccb_h.ccb_bt_ptr = bt;
1105
1106 /*
1107 * Put all the arguments for the xfer in the bccb
1108 */
1109 hccb->target_id = ccb->ccb_h.target_id;
1110 hccb->target_lun = ccb->ccb_h.target_lun;
1111 hccb->btstat = 0;
1112 hccb->sdstat = 0;
1113
1114 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1115 struct ccb_scsiio *csio;
1116 struct ccb_hdr *ccbh;
1117
1118 csio = &ccb->csio;
1119 ccbh = &csio->ccb_h;
1120 hccb->opcode = INITIATOR_CCB_WRESID;
1121 hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
1122 hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
1123 hccb->cmd_len = csio->cdb_len;
1124 if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
1125 ccb->ccb_h.status = CAM_REQ_INVALID;
1126 btfreeccb(bt, bccb);
1127 xpt_done(ccb);
1128 return;
1129 }
1130 hccb->sense_len = csio->sense_len;
1131 if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
1132 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
1133 hccb->tag_enable = TRUE;
1134 hccb->tag_type = (ccb->csio.tag_action & 0x3);
1135 } else {
1136 hccb->tag_enable = FALSE;
1137 hccb->tag_type = 0;
1138 }
1139 if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
1140 if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
1141 bcopy(csio->cdb_io.cdb_ptr,
1142 hccb->scsi_cdb, hccb->cmd_len);
1143 } else {
1144 /* I guess I could map it in... */
1145 ccbh->status = CAM_REQ_INVALID;
1146 btfreeccb(bt, bccb);
1147 xpt_done(ccb);
1148 return;
1149 }
1150 } else {
1151 bcopy(csio->cdb_io.cdb_bytes,
1152 hccb->scsi_cdb, hccb->cmd_len);
1153 }
1154 /* If need be, bounce our sense buffer */
1155 if (bt->sense_buffers != NULL) {
1156 hccb->sense_addr = btsensepaddr(bt, bccb);
1157 } else {
1158 hccb->sense_addr = vtophys(&csio->sense_data);
1159 }
1160 /*
1161 * If we have any data to send with this command,
1162 * map it into bus space.
1163 */
1164 /* Only use S/G if there is a transfer */
1165 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1166 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
1167 /*
1168 * We've been given a pointer
1169 * to a single buffer.
1170 */
1171 if ((ccbh->flags & CAM_DATA_PHYS)==0) {
1172 int s;
1173 int error;
1174
1175 s = splsoftvm();
1176 error = bus_dmamap_load(
1177 bt->buffer_dmat,
1178 bccb->dmamap,
1179 csio->data_ptr,
1180 csio->dxfer_len,
1181 btexecuteccb,
1182 bccb,
1183 /*flags*/0);
1184 if (error == EINPROGRESS) {
1185 /*
1186 * So as to maintain
1187 * ordering, freeze the
1188 * controller queue
1189 * until our mapping is
1190 * returned.
1191 */
1192 xpt_freeze_simq(bt->sim,
1193 1);
1194 csio->ccb_h.status |=
1195 CAM_RELEASE_SIMQ;
1196 }
1197 splx(s);
1198 } else {
1199 struct bus_dma_segment seg;
1200
1201 /* Pointer to physical buffer */
1202 seg.ds_addr =
1203 (bus_addr_t)csio->data_ptr;
1204 seg.ds_len = csio->dxfer_len;
1205 btexecuteccb(bccb, &seg, 1, 0);
1206 }
1207 } else {
1208 struct bus_dma_segment *segs;
1209
1210 if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1211 panic("btaction - Physical "
1212 "segment pointers "
1213 "unsupported");
1214
1215 if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1216 panic("btaction - Virtual "
1217 "segment addresses "
1218 "unsupported");
1219
1220 /* Just use the segments provided */
1221 segs = (struct bus_dma_segment *)
1222 csio->data_ptr;
1223 btexecuteccb(bccb, segs,
1224 csio->sglist_cnt, 0);
1225 }
1226 } else {
1227 btexecuteccb(bccb, NULL, 0, 0);
1228 }
1229 } else {
1230 hccb->opcode = INITIATOR_BUS_DEV_RESET;
1231 /* No data transfer */
1232 hccb->datain = TRUE;
1233 hccb->dataout = TRUE;
1234 hccb->cmd_len = 0;
1235 hccb->sense_len = 0;
1236 hccb->tag_enable = FALSE;
1237 hccb->tag_type = 0;
1238 btexecuteccb(bccb, NULL, 0, 0);
1239 }
1240 break;
1241 }
1242 case XPT_EN_LUN: /* Enable LUN as a target */
1243 case XPT_TARGET_IO: /* Execute target I/O request */
1244 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1245 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1246 case XPT_ABORT: /* Abort the specified CCB */
1247 /* XXX Implement */
1248 ccb->ccb_h.status = CAM_REQ_INVALID;
1249 xpt_done(ccb);
1250 break;
1251 case XPT_SET_TRAN_SETTINGS:
1252 {
1253 /* XXX Implement */
1254 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1255 xpt_done(ccb);
1256 break;
1257 }
1258 case XPT_GET_TRAN_SETTINGS:
1259 /* Get default/user set transfer settings for the target */
1260 {
1261 struct ccb_trans_settings *cts;
1262 u_int target_mask;
1263
1264 cts = &ccb->cts;
1265 target_mask = 0x01 << ccb->ccb_h.target_id;
1266 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
1267 cts->flags = 0;
1268 if ((bt->disc_permitted & target_mask) != 0)
1269 cts->flags |= CCB_TRANS_DISC_ENB;
1270 if ((bt->tags_permitted & target_mask) != 0)
1271 cts->flags |= CCB_TRANS_TAG_ENB;
1272 if ((bt->wide_permitted & target_mask) != 0)
1273 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1274 else
1275 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1276 if ((bt->ultra_permitted & target_mask) != 0)
1277 cts->sync_period = 12;
1278 else if ((bt->fast_permitted & target_mask) != 0)
1279 cts->sync_period = 25;
1280 else if ((bt->sync_permitted & target_mask) != 0)
1281 cts->sync_period = 50;
1282 else
1283 cts->sync_period = 0;
1284
1285 if (cts->sync_period != 0)
1286 cts->sync_offset = 15;
1287
1288 cts->valid = CCB_TRANS_SYNC_RATE_VALID
1289 | CCB_TRANS_SYNC_OFFSET_VALID
1290 | CCB_TRANS_BUS_WIDTH_VALID
1291 | CCB_TRANS_DISC_VALID
1292 | CCB_TRANS_TQ_VALID;
1293 } else {
1294 btfetchtransinfo(bt, cts);
1295 }
1296
1297 ccb->ccb_h.status = CAM_REQ_CMP;
1298 xpt_done(ccb);
1299 break;
1300 }
1301 case XPT_CALC_GEOMETRY:
1302 {
1303 struct ccb_calc_geometry *ccg;
1304 u_int32_t size_mb;
1305 u_int32_t secs_per_cylinder;
1306
1307 ccg = &ccb->ccg;
1308 size_mb = ccg->volume_size
1309 / ((1024L * 1024L) / ccg->block_size);
1310
1311 if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1312 if (size_mb >= 2048) {
1313 ccg->heads = 255;
1314 ccg->secs_per_track = 63;
1315 } else {
1316 ccg->heads = 128;
1317 ccg->secs_per_track = 32;
1318 }
1319 } else {
1320 ccg->heads = 64;
1321 ccg->secs_per_track = 32;
1322 }
1323 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1324 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1325 ccb->ccb_h.status = CAM_REQ_CMP;
1326 xpt_done(ccb);
1327 break;
1328 }
1329 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1330 {
1331 btreset(bt, /*hardreset*/TRUE);
1332 ccb->ccb_h.status = CAM_REQ_CMP;
1333 xpt_done(ccb);
1334 break;
1335 }
1336 case XPT_TERM_IO: /* Terminate the I/O process */
1337 /* XXX Implement */
1338 ccb->ccb_h.status = CAM_REQ_INVALID;
1339 xpt_done(ccb);
1340 break;
1341 case XPT_PATH_INQ: /* Path routing inquiry */
1342 {
1343 struct ccb_pathinq *cpi = &ccb->cpi;
1344
1345 cpi->version_num = 1; /* XXX??? */
1346 cpi->hba_inquiry = PI_SDTR_ABLE;
1347 if (bt->tag_capable != 0)
1348 cpi->hba_inquiry |= PI_TAG_ABLE;
1349 if (bt->wide_bus != 0)
1350 cpi->hba_inquiry |= PI_WIDE_16;
1351 cpi->target_sprt = 0;
1352 cpi->hba_misc = 0;
1353 cpi->hba_eng_cnt = 0;
1354 cpi->max_target = bt->wide_bus ? 15 : 7;
1355 cpi->max_lun = 7;
1356 cpi->initiator_id = bt->scsi_id;
1357 cpi->bus_id = cam_sim_bus(sim);
1358 cpi->base_transfer_speed = 3300;
1359 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1360 strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1361 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1362 cpi->unit_number = cam_sim_unit(sim);
1363 cpi->ccb_h.status = CAM_REQ_CMP;
1364 xpt_done(ccb);
1365 break;
1366 }
1367 default:
1368 ccb->ccb_h.status = CAM_REQ_INVALID;
1369 xpt_done(ccb);
1370 break;
1371 }
1372}
1373
1374static void
1375btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1376{
1377 struct bt_ccb *bccb;
1378 union ccb *ccb;
1379 struct bt_softc *bt;
1380 int s;
1381
1382 bccb = (struct bt_ccb *)arg;
1383 ccb = bccb->ccb;
1384 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1385
1386 if (error != 0) {
1387 if (error != EFBIG)
1388 device_printf(bt->dev,
1389 "Unexepected error 0x%x returned from "
1390 "bus_dmamap_load\n", error);
1391 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1392 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1393 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1394 }
1395 btfreeccb(bt, bccb);
1396 xpt_done(ccb);
1397 return;
1398 }
1399
1400 if (nseg != 0) {
1401 bt_sg_t *sg;
1402 bus_dma_segment_t *end_seg;
1403 bus_dmasync_op_t op;
1404
1405 end_seg = dm_segs + nseg;
1406
1407 /* Copy the segments into our SG list */
1408 sg = bccb->sg_list;
1409 while (dm_segs < end_seg) {
1410 sg->len = dm_segs->ds_len;
1411 sg->addr = dm_segs->ds_addr;
1412 sg++;
1413 dm_segs++;
1414 }
1415
1416 if (nseg > 1) {
1417 bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1418 bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1419 bccb->hccb.data_addr = bccb->sg_list_phys;
1420 } else {
1421 bccb->hccb.data_len = bccb->sg_list->len;
1422 bccb->hccb.data_addr = bccb->sg_list->addr;
1423 }
1424
1425 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1426 op = BUS_DMASYNC_PREREAD;
1427 else
1428 op = BUS_DMASYNC_PREWRITE;
1429
1430 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1431
1432 } else {
1433 bccb->hccb.opcode = INITIATOR_CCB;
1434 bccb->hccb.data_len = 0;
1435 bccb->hccb.data_addr = 0;
1436 }
1437
1438 s = splcam();
1439
1440 /*
1441 * Last time we need to check if this CCB needs to
1442 * be aborted.
1443 */
1444 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1445 if (nseg != 0)
1446 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1447 btfreeccb(bt, bccb);
1448 xpt_done(ccb);
1449 splx(s);
1450 return;
1451 }
1452
1453 bccb->flags = BCCB_ACTIVE;
1454 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1455 LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1456
1457 ccb->ccb_h.timeout_ch =
1458 timeout(bttimeout, (caddr_t)bccb,
1459 (ccb->ccb_h.timeout * hz) / 1000);
1460
1461 /* Tell the adapter about this command */
1462 bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1463 if (bt->cur_outbox->action_code != BMBO_FREE) {
1464 /*
1465 * We should never encounter a busy mailbox.
1466 * If we do, warn the user, and treat it as
1467 * a resource shortage. If the controller is
1468 * hung, one of the pending transactions will
1469 * timeout causing us to start recovery operations.
1470 */
1471 device_printf(bt->dev,
1472 "Encountered busy mailbox with %d out of %d "
1473 "commands active!!!\n", bt->active_ccbs,
1474 bt->max_ccbs);
1475 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1476 if (nseg != 0)
1477 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1478 btfreeccb(bt, bccb);
1479 bt->resource_shortage = TRUE;
1480 xpt_freeze_simq(bt->sim, /*count*/1);
1481 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1482 xpt_done(ccb);
1483 return;
1484 }
1485 bt->cur_outbox->action_code = BMBO_START;
1486 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1487 btnextoutbox(bt);
1488 splx(s);
1489}
1490
1491void
1492bt_intr(void *arg)
1493{
1494 struct bt_softc *bt;
1495 u_int intstat;
1496
1497 bt = (struct bt_softc *)arg;
1498 while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1499
1500 if ((intstat & CMD_COMPLETE) != 0) {
1501 bt->latched_status = bt_inb(bt, STATUS_REG);
1502 bt->command_cmp = TRUE;
1503 }
1504
1505 bt_outb(bt, CONTROL_REG, RESET_INTR);
1506
1507 if ((intstat & IMB_LOADED) != 0) {
1508 while (bt->cur_inbox->comp_code != BMBI_FREE) {
1509 btdone(bt,
1510 btccbptov(bt, bt->cur_inbox->ccb_addr),
1511 bt->cur_inbox->comp_code);
1512 bt->cur_inbox->comp_code = BMBI_FREE;
1513 btnextinbox(bt);
1514 }
1515 }
1516
1517 if ((intstat & SCSI_BUS_RESET) != 0) {
1518 btreset(bt, /*hardreset*/FALSE);
1519 }
1520 }
1521}
1522
1523static void
1524btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1525{
1526 union ccb *ccb;
1527 struct ccb_scsiio *csio;
1528
1529 ccb = bccb->ccb;
1530 csio = &bccb->ccb->csio;
1531
1532 if ((bccb->flags & BCCB_ACTIVE) == 0) {
1533 device_printf(bt->dev,
1534 "btdone - Attempt to free non-active BCCB %p\n",
1535 (void *)bccb);
1536 return;
1537 }
1538
1539 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1540 bus_dmasync_op_t op;
1541
1542 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1543 op = BUS_DMASYNC_POSTREAD;
1544 else
1545 op = BUS_DMASYNC_POSTWRITE;
1546 bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1547 bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1548 }
1549
1550 if (bccb == bt->recovery_bccb) {
1551 /*
1552 * The recovery BCCB does not have a CCB associated
1553 * with it, so short circuit the normal error handling.
1554 * We now traverse our list of pending CCBs and process
1555 * any that were terminated by the recovery CCBs action.
1556 * We also reinstate timeouts for all remaining, pending,
1557 * CCBs.
1558 */
1559 struct cam_path *path;
1560 struct ccb_hdr *ccb_h;
1561 cam_status error;
1562
1563 /* Notify all clients that a BDR occured */
1564 error = xpt_create_path(&path, /*periph*/NULL,
1565 cam_sim_path(bt->sim),
1566 bccb->hccb.target_id,
1567 CAM_LUN_WILDCARD);
1568
1569 if (error == CAM_REQ_CMP)
1570 xpt_async(AC_SENT_BDR, path, NULL);
1571
1572 ccb_h = LIST_FIRST(&bt->pending_ccbs);
1573 while (ccb_h != NULL) {
1574 struct bt_ccb *pending_bccb;
1575
1576 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1577 if (pending_bccb->hccb.target_id
1578 == bccb->hccb.target_id) {
1579 pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1580 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1581 btdone(bt, pending_bccb, BMBI_ERROR);
1582 } else {
1583 ccb_h->timeout_ch =
1584 timeout(bttimeout, (caddr_t)pending_bccb,
1585 (ccb_h->timeout * hz) / 1000);
1586 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1587 }
1588 }
1589 device_printf(bt->dev, "No longer in timeout\n");
1590 return;
1591 }
1592
1593 untimeout(bttimeout, bccb, ccb->ccb_h.timeout_ch);
1594
1595 switch (comp_code) {
1596 case BMBI_FREE:
1597 device_printf(bt->dev,
1598 "btdone - CCB completed with free status!\n");
1599 break;
1600 case BMBI_NOT_FOUND:
1601 device_printf(bt->dev,
1602 "btdone - CCB Abort failed to find CCB\n");
1603 break;
1604 case BMBI_ABORT:
1605 case BMBI_ERROR:
1606 if (bootverbose) {
1607 printf("bt: ccb %p - error %x occured. "
1608 "btstat = %x, sdstat = %x\n",
1609 (void *)bccb, comp_code, bccb->hccb.btstat,
1610 bccb->hccb.sdstat);
1611 }
1612 /* An error occured */
1613 switch(bccb->hccb.btstat) {
1614 case BTSTAT_DATARUN_ERROR:
1615 if (bccb->hccb.data_len == 0) {
1616 /*
1617 * At least firmware 4.22, does this
1618 * for a QUEUE FULL condition.
1619 */
1620 bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1621 } else if (bccb->hccb.data_len < 0) {
1622 csio->ccb_h.status = CAM_DATA_RUN_ERR;
1623 break;
1624 }
1625 /* FALLTHROUGH */
1626 case BTSTAT_NOERROR:
1627 case BTSTAT_LINKED_CMD_COMPLETE:
1628 case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1629 case BTSTAT_DATAUNDERUN_ERROR:
1630
1631 csio->scsi_status = bccb->hccb.sdstat;
1632 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1633 switch(csio->scsi_status) {
1634 case SCSI_STATUS_CHECK_COND:
1635 case SCSI_STATUS_CMD_TERMINATED:
1636 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1637 /* Bounce sense back if necessary */
1638 if (bt->sense_buffers != NULL) {
1639 csio->sense_data =
1640 *btsensevaddr(bt, bccb);
1641 }
1642 break;
1643 default:
1644 break;
1645 case SCSI_STATUS_OK:
1646 csio->ccb_h.status = CAM_REQ_CMP;
1647 break;
1648 }
1649 csio->resid = bccb->hccb.data_len;
1650 break;
1651 case BTSTAT_SELTIMEOUT:
1652 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1653 break;
1654 case BTSTAT_UNEXPECTED_BUSFREE:
1655 csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1656 break;
1657 case BTSTAT_INVALID_PHASE:
1658 csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1659 break;
1660 case BTSTAT_INVALID_ACTION_CODE:
1661 panic("%s: Inavlid Action code", bt_name(bt));
1662 break;
1663 case BTSTAT_INVALID_OPCODE:
1664 panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1665 break;
1666 case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1667 /* We don't even support linked commands... */
1668 panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1669 break;
1670 case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1671 panic("%s: Invalid CCB or SG list", bt_name(bt));
1672 break;
1673 case BTSTAT_AUTOSENSE_FAILED:
1674 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1675 break;
1676 case BTSTAT_TAGGED_MSG_REJECTED:
1677 {
1678 struct ccb_trans_settings neg;
1679
1680 xpt_print_path(csio->ccb_h.path);
1681 printf("refuses tagged commands. Performing "
1682 "non-tagged I/O\n");
1683 neg.flags = 0;
1684 neg.valid = CCB_TRANS_TQ_VALID;
1685 xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1686 /*priority*/1);
1687 xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1688 bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1689 csio->ccb_h.status = CAM_MSG_REJECT_REC;
1690 break;
1691 }
1692 case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1693 /*
1694 * XXX You would think that this is
1695 * a recoverable error... Hmmm.
1696 */
1697 csio->ccb_h.status = CAM_REQ_CMP_ERR;
1698 break;
1699 case BTSTAT_HA_SOFTWARE_ERROR:
1700 case BTSTAT_HA_WATCHDOG_ERROR:
1701 case BTSTAT_HARDWARE_FAILURE:
1702 /* Hardware reset ??? Can we recover ??? */
1703 csio->ccb_h.status = CAM_NO_HBA;
1704 break;
1705 case BTSTAT_TARGET_IGNORED_ATN:
1706 case BTSTAT_OTHER_SCSI_BUS_RESET:
1707 case BTSTAT_HA_SCSI_BUS_RESET:
1708 if ((csio->ccb_h.status & CAM_STATUS_MASK)
1709 != CAM_CMD_TIMEOUT)
1710 csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1711 break;
1712 case BTSTAT_HA_BDR:
1713 if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1714 csio->ccb_h.status = CAM_BDR_SENT;
1715 else
1716 csio->ccb_h.status = CAM_CMD_TIMEOUT;
1717 break;
1718 case BTSTAT_INVALID_RECONNECT:
1719 case BTSTAT_ABORT_QUEUE_GENERATED:
1720 csio->ccb_h.status = CAM_REQ_TERMIO;
1721 break;
1722 case BTSTAT_SCSI_PERROR_DETECTED:
1723 csio->ccb_h.status = CAM_UNCOR_PARITY;
1724 break;
1725 }
1726 if (csio->ccb_h.status != CAM_REQ_CMP) {
1727 xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1728 csio->ccb_h.status |= CAM_DEV_QFRZN;
1729 }
1730 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1731 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1732 btfreeccb(bt, bccb);
1733 xpt_done(ccb);
1734 break;
1735 case BMBI_OK:
1736 /* All completed without incident */
1737 ccb->ccb_h.status |= CAM_REQ_CMP;
1738 if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1739 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1740 btfreeccb(bt, bccb);
1741 xpt_done(ccb);
1742 break;
1743 }
1744}
1745
1746static int
1747btreset(struct bt_softc* bt, int hard_reset)
1748{
1749 struct ccb_hdr *ccb_h;
1750 u_int status;
1751 u_int timeout;
1752 u_int8_t reset_type;
1753
1754 if (hard_reset != 0)
1755 reset_type = HARD_RESET;
1756 else
1757 reset_type = SOFT_RESET;
1758 bt_outb(bt, CONTROL_REG, reset_type);
1759
1760 /* Wait 5sec. for Diagnostic start */
1761 timeout = 5 * 10000;
1762 while (--timeout) {
1763 status = bt_inb(bt, STATUS_REG);
1764 if ((status & DIAG_ACTIVE) != 0)
1765 break;
1766 DELAY(100);
1767 }
1768 if (timeout == 0) {
1769 if (bootverbose)
1770 printf("%s: btreset - Diagnostic Active failed to "
1771 "assert. status = 0x%x\n", bt_name(bt), status);
1772 return (ETIMEDOUT);
1773 }
1774
1775 /* Wait 10sec. for Diagnostic end */
1776 timeout = 10 * 10000;
1777 while (--timeout) {
1778 status = bt_inb(bt, STATUS_REG);
1779 if ((status & DIAG_ACTIVE) == 0)
1780 break;
1781 DELAY(100);
1782 }
1783 if (timeout == 0) {
1784 panic("%s: btreset - Diagnostic Active failed to drop. "
1785 "status = 0x%x\n", bt_name(bt), status);
1786 return (ETIMEDOUT);
1787 }
1788
1789 /* Wait for the host adapter to become ready or report a failure */
1790 timeout = 10000;
1791 while (--timeout) {
1792 status = bt_inb(bt, STATUS_REG);
1793 if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1794 break;
1795 DELAY(100);
1796 }
1797 if (timeout == 0) {
1798 printf("%s: btreset - Host adapter failed to come ready. "
1799 "status = 0x%x\n", bt_name(bt), status);
1800 return (ETIMEDOUT);
1801 }
1802
1803 /* If the diagnostics failed, tell the user */
1804 if ((status & DIAG_FAIL) != 0
1805 || (status & HA_READY) == 0) {
1806 printf("%s: btreset - Adapter failed diagnostics\n",
1807 bt_name(bt));
1808
1809 if ((status & DATAIN_REG_READY) != 0)
1810 printf("%s: btreset - Host Adapter Error code = 0x%x\n",
1811 bt_name(bt), bt_inb(bt, DATAIN_REG));
1812 return (ENXIO);
1813 }
1814
1815 /* If we've allocated mailboxes, initialize them */
1816 if (bt->init_level > 4)
1817 btinitmboxes(bt);
1818
1819 /* If we've attached to the XPT, tell it about the event */
1820 if (bt->path != NULL)
1821 xpt_async(AC_BUS_RESET, bt->path, NULL);
1822
1823 /*
1824 * Perform completion processing for all outstanding CCBs.
1825 */
1826 while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1827 struct bt_ccb *pending_bccb;
1828
1829 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1830 pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1831 btdone(bt, pending_bccb, BMBI_ERROR);
1832 }
1833
1834 return (0);
1835}
1836
1837/*
1838 * Send a command to the adapter.
1839 */
1840int
1841bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1842 u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1843{
1844 u_int timeout;
1845 u_int status;
1846 u_int saved_status;
1847 u_int intstat;
1848 u_int reply_buf_size;
1849 int s;
1850 int cmd_complete;
1851 int error;
1852
1853 /* No data returned to start */
1854 reply_buf_size = reply_len;
1855 reply_len = 0;
1856 intstat = 0;
1857 cmd_complete = 0;
1858 saved_status = 0;
1859 error = 0;
1860
1861 bt->command_cmp = 0;
1862 /*
1863 * Wait up to 10 sec. for the adapter to become
1864 * ready to accept commands.
1865 */
1866 timeout = 100000;
1867 while (--timeout) {
1868 status = bt_inb(bt, STATUS_REG);
1869 if ((status & HA_READY) != 0
1870 && (status & CMD_REG_BUSY) == 0)
1871 break;
1872 /*
1873 * Throw away any pending data which may be
1874 * left over from earlier commands that we
1875 * timedout on.
1876 */
1877 if ((status & DATAIN_REG_READY) != 0)
1878 (void)bt_inb(bt, DATAIN_REG);
1879 DELAY(100);
1880 }
1881 if (timeout == 0) {
1882 printf("%s: bt_cmd: Timeout waiting for adapter ready, "
1883 "status = 0x%x\n", bt_name(bt), status);
1884 return (ETIMEDOUT);
1885 }
1886
1887 /*
1888 * Send the opcode followed by any necessary parameter bytes.
1889 */
1890 bt_outb(bt, COMMAND_REG, opcode);
1891
1892 /*
1893 * Wait for up to 1sec for each byte of the the
1894 * parameter list sent to be sent.
1895 */
1896 timeout = 10000;
1897 while (param_len && --timeout) {
1898 DELAY(100);
1899 s = splcam();
1900 status = bt_inb(bt, STATUS_REG);
1901 intstat = bt_inb(bt, INTSTAT_REG);
1902 splx(s);
1903
1904 if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1905 == (INTR_PENDING|CMD_COMPLETE)) {
1906 saved_status = status;
1907 cmd_complete = 1;
1908 break;
1909 }
1910 if (bt->command_cmp != 0) {
1911 saved_status = bt->latched_status;
1912 cmd_complete = 1;
1913 break;
1914 }
1915 if ((status & DATAIN_REG_READY) != 0)
1916 break;
1917 if ((status & CMD_REG_BUSY) == 0) {
1918 bt_outb(bt, COMMAND_REG, *params++);
1919 param_len--;
1920 timeout = 10000;
1921 }
1922 }
1923 if (timeout == 0) {
1924 printf("%s: bt_cmd: Timeout sending parameters, "
1925 "status = 0x%x\n", bt_name(bt), status);
1926 cmd_complete = 1;
1927 saved_status = status;
1928 error = ETIMEDOUT;
1929 }
1930
1931 /*
1932 * Wait for the command to complete.
1933 */
1934 while (cmd_complete == 0 && --cmd_timeout) {
1935
1936 s = splcam();
1937 status = bt_inb(bt, STATUS_REG);
1938 intstat = bt_inb(bt, INTSTAT_REG);
1939 /*
1940 * It may be that this command was issued with
1941 * controller interrupts disabled. We'll never
1942 * get to our command if an incoming mailbox
1943 * interrupt is pending, so take care of completed
1944 * mailbox commands by calling our interrupt handler.
1945 */
1946 if ((intstat & (INTR_PENDING|IMB_LOADED))
1947 == (INTR_PENDING|IMB_LOADED))
1948 bt_intr(bt);
1949 splx(s);
1950
1951 if (bt->command_cmp != 0) {
1952 /*
1953 * Our interrupt handler saw CMD_COMPLETE
1954 * status before we did.
1955 */
1956 cmd_complete = 1;
1957 saved_status = bt->latched_status;
1958 } else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1959 == (INTR_PENDING|CMD_COMPLETE)) {
1960 /*
1961 * Our poll (in case interrupts are blocked)
1962 * saw the CMD_COMPLETE interrupt.
1963 */
1964 cmd_complete = 1;
1965 saved_status = status;
1966 } else if (opcode == BOP_MODIFY_IO_ADDR
1967 && (status & CMD_REG_BUSY) == 0) {
1968 /*
1969 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1970 * but it should update the status register. So, we
1971 * consider this command complete when the CMD_REG_BUSY
1972 * status clears.
1973 */
1974 saved_status = status;
1975 cmd_complete = 1;
1976 } else if ((status & DATAIN_REG_READY) != 0) {
1977 u_int8_t data;
1978
1979 data = bt_inb(bt, DATAIN_REG);
1980 if (reply_len < reply_buf_size) {
1981 *reply_data++ = data;
1982 } else {
1983 printf("%s: bt_cmd - Discarded reply data byte "
1984 "for opcode 0x%x\n", bt_name(bt),
1985 opcode);
1986 }
1987 /*
1988 * Reset timeout to ensure at least a second
1989 * between response bytes.
1990 */
1991 cmd_timeout = MAX(cmd_timeout, 10000);
1992 reply_len++;
1993
1994 } else if ((opcode == BOP_FETCH_LRAM)
1995 && (status & HA_READY) != 0) {
1996 saved_status = status;
1997 cmd_complete = 1;
1998 }
1999 DELAY(100);
2000 }
2001 if (cmd_timeout == 0) {
2002 printf("%s: bt_cmd: Timeout waiting for command (%x) "
2003 "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
2004 "rlen %d\n", bt_name(bt), opcode,
2005 bt_name(bt), status, intstat, reply_len);
2006 error = (ETIMEDOUT);
2007 }
2008
2009 /*
2010 * Clear any pending interrupts. Block interrupts so our
2011 * interrupt handler is not re-entered.
2012 */
2013 s = splcam();
2014 bt_intr(bt);
2015 splx(s);
2016
2017 if (error != 0)
2018 return (error);
2019
2020 /*
2021 * If the command was rejected by the controller, tell the caller.
2022 */
2023 if ((saved_status & CMD_INVALID) != 0) {
2024 /*
2025 * Some early adapters may not recover properly from
2026 * an invalid command. If it appears that the controller
2027 * has wedged (i.e. status was not cleared by our interrupt
2028 * reset above), perform a soft reset.
2029 */
2030 if (bootverbose)
2031 printf("%s: Invalid Command 0x%x\n", bt_name(bt),
2032 opcode);
2033 DELAY(1000);
2034 status = bt_inb(bt, STATUS_REG);
2035 if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
2036 CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
2037 || (status & (HA_READY|INIT_REQUIRED))
2038 != (HA_READY|INIT_REQUIRED)) {
2039 btreset(bt, /*hard_reset*/FALSE);
2040 }
2041 return (EINVAL);
2042 }
2043
2044 if (param_len > 0) {
2045 /* The controller did not accept the full argument list */
2046 return (E2BIG);
2047 }
2048
2049 if (reply_len != reply_buf_size) {
2050 /* Too much or too little data received */
2051 return (EMSGSIZE);
2052 }
2053
2054 /* We were successful */
2055 return (0);
2056}
2057
2058static int
2059btinitmboxes(struct bt_softc *bt) {
2060 init_32b_mbox_params_t init_mbox;
2061 int error;
2062
2063 bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
2064 bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
2065 bt->cur_inbox = bt->in_boxes;
2066 bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
2067 bt->cur_outbox = bt->out_boxes;
2068 bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
2069
2070 /* Tell the adapter about them */
2071 init_mbox.num_boxes = bt->num_boxes;
2072 init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
2073 init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
2074 init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
2075 init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
2076 error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
2077 /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
2078 /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
2079
2080 if (error != 0)
2081 printf("btinitmboxes: Initialization command failed\n");
2082 else if (bt->strict_rr != 0) {
2083 /*
2084 * If the controller supports
2085 * strict round robin mode,
2086 * enable it
2087 */
2088 u_int8_t param;
2089
2090 param = 0;
2091 error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, &param, 1,
2092 /*reply_buf*/NULL, /*reply_len*/0,
2093 DEFAULT_CMD_TIMEOUT);
2094
2095 if (error != 0) {
2096 printf("btinitmboxes: Unable to enable strict RR\n");
2097 error = 0;
2098 } else if (bootverbose) {
2099 printf("%s: Using Strict Round Robin Mailbox Mode\n",
2100 bt_name(bt));
2101 }
2102 }
2103
2104 return (error);
2105}
2106
2107/*
2108 * Update the XPT's idea of the negotiated transfer
2109 * parameters for a particular target.
2110 */
2111static void
2112btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings* cts)
2113{
2114 setup_data_t setup_info;
2115 u_int target;
2116 u_int targ_offset;
2117 u_int targ_mask;
2118 u_int sync_period;
2119 int error;
2120 u_int8_t param;
2121 targ_syncinfo_t sync_info;
2122
2123 target = cts->ccb_h.target_id;
2124 targ_offset = (target & 0x7);
2125 targ_mask = (0x01 << targ_offset);
2126
2127 /*
2128 * Inquire Setup Information. This command retreives the
2129 * Wide negotiation status for recent adapters as well as
2130 * the sync info for older models.
2131 */
2132 param = sizeof(setup_info);
2133 error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
2134 (u_int8_t*)&setup_info, sizeof(setup_info),
2135 DEFAULT_CMD_TIMEOUT);
2136
2137 if (error != 0) {
2138 printf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
2139 bt_name(bt), error);
2140 cts->valid = 0;
2141 return;
2142 }
2143
2144 sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
2145 : setup_info.high_syncinfo[targ_offset];
2146
2147 if (sync_info.sync == 0)
2148 cts->sync_offset = 0;
2149 else
2150 cts->sync_offset = sync_info.offset;
2151
2152 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2153 if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
2154 u_int wide_active;
2155
2156 wide_active =
2157 (target < 8) ? (setup_info.low_wide_active & targ_mask)
2158 : (setup_info.high_wide_active & targ_mask);
2159
2160 if (wide_active)
2161 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2162 } else if ((bt->wide_permitted & targ_mask) != 0) {
2163 struct ccb_getdev cgd;
2164
2165 /*
2166 * Prior to rev 5.06L, wide status isn't provided,
2167 * so we "guess" that wide transfers are in effect
2168 * if the user settings allow for wide and the inquiry
2169 * data for the device indicates that it can handle
2170 * wide transfers.
2171 */
2172 xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2173 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2174 xpt_action((union ccb *)&cgd);
2175 if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2176 && (cgd.inq_data.flags & SID_WBus16) != 0)
2177 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2178 }
2179
2180 if (bt->firmware_ver[0] >= '3') {
2181 /*
2182 * For adapters that can do fast or ultra speeds,
2183 * use the more exact Target Sync Information command.
2184 */
2185 target_sync_info_data_t sync_info;
2186
2187 param = sizeof(sync_info);
2188 error = bt_cmd(bt, BOP_TARG_SYNC_INFO, &param, /*paramlen*/1,
2189 (u_int8_t*)&sync_info, sizeof(sync_info),
2190 DEFAULT_CMD_TIMEOUT);
2191
2192 if (error != 0) {
2193 printf("%s: btfetchtransinfo - Inquire Sync "
2194 "Info Failed 0x%x\n", bt_name(bt), error);
2195 cts->valid = 0;
2196 return;
2197 }
2198 sync_period = sync_info.sync_rate[target] * 100;
2199 } else {
2200 sync_period = 2000 + (500 * sync_info.period);
2201 }
2202
2203 /* Convert ns value to standard SCSI sync rate */
2204 if (cts->sync_offset != 0)
2205 cts->sync_period = scsi_calc_syncparam(sync_period);
2206 else
2207 cts->sync_period = 0;
2208
2209 cts->valid = CCB_TRANS_SYNC_RATE_VALID
2210 | CCB_TRANS_SYNC_OFFSET_VALID
2211 | CCB_TRANS_BUS_WIDTH_VALID;
2212 xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2213}
2214
2215static void
2216btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2217{
2218 struct bt_softc* bt;
2219
2220 bt = (struct bt_softc*)arg;
2221 bt->mailbox_physbase = segs->ds_addr;
2222}
2223
2224static void
2225btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2226{
2227 struct bt_softc* bt;
2228
2229 bt = (struct bt_softc*)arg;
2230 bt->bt_ccb_physbase = segs->ds_addr;
2231}
2232
2233static void
2234btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2235{
2236
2237 struct bt_softc* bt;
2238
2239 bt = (struct bt_softc*)arg;
2240 SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2241}
2242
2243static void
2244btpoll(struct cam_sim *sim)
2245{
2246 bt_intr(cam_sim_softc(sim));
2247}
2248
2249void
2250bttimeout(void *arg)
2251{
2252 struct bt_ccb *bccb;
2253 union ccb *ccb;
2254 struct bt_softc *bt;
2255 int s;
2256
2257 bccb = (struct bt_ccb *)arg;
2258 ccb = bccb->ccb;
2259 bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2260 xpt_print_path(ccb->ccb_h.path);
2261 printf("CCB %p - timed out\n", (void *)bccb);
2262
2263 s = splcam();
2264
2265 if ((bccb->flags & BCCB_ACTIVE) == 0) {
2266 xpt_print_path(ccb->ccb_h.path);
2267 printf("CCB %p - timed out CCB already completed\n",
2268 (void *)bccb);
2269 splx(s);
2270 return;
2271 }
2272
2273 /*
2274 * In order to simplify the recovery process, we ask the XPT
2275 * layer to halt the queue of new transactions and we traverse
2276 * the list of pending CCBs and remove their timeouts. This
2277 * means that the driver attempts to clear only one error
2278 * condition at a time. In general, timeouts that occur
2279 * close together are related anyway, so there is no benefit
2280 * in attempting to handle errors in parrallel. Timeouts will
2281 * be reinstated when the recovery process ends.
2282 */
2283 if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2284 struct ccb_hdr *ccb_h;
2285
2286 if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2287 xpt_freeze_simq(bt->sim, /*count*/1);
2288 bccb->flags |= BCCB_RELEASE_SIMQ;
2289 }
2290
2291 ccb_h = LIST_FIRST(&bt->pending_ccbs);
2292 while (ccb_h != NULL) {
2293 struct bt_ccb *pending_bccb;
2294
2295 pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2296 untimeout(bttimeout, pending_bccb, ccb_h->timeout_ch);
2297 ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2298 }
2299 }
2300
2301 if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2302 || bt->cur_outbox->action_code != BMBO_FREE
2303 || ((bccb->hccb.tag_enable == TRUE)
2304 && (bt->firmware_ver[0] < '5'))) {
2305 /*
2306 * Try a full host adapter/SCSI bus reset.
2307 * We do this only if we have already attempted
2308 * to clear the condition with a BDR, or we cannot
2309 * attempt a BDR for lack of mailbox resources
2310 * or because of faulty firmware. It turns out
2311 * that firmware versions prior to 5.xx treat BDRs
2312 * as untagged commands that cannot be sent until
2313 * all outstanding tagged commands have been processed.
2314 * This makes it somewhat difficult to use a BDR to
2315 * clear up a problem with an uncompleted tagged command.
2316 */
2317 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2318 btreset(bt, /*hardreset*/TRUE);
2319 printf("%s: No longer in timeout\n", bt_name(bt));
2320 } else {
2321 /*
2322 * Send a Bus Device Reset message:
2323 * The target that is holding up the bus may not
2324 * be the same as the one that triggered this timeout
2325 * (different commands have different timeout lengths),
2326 * but we have no way of determining this from our
2327 * timeout handler. Our strategy here is to queue a
2328 * BDR message to the target of the timed out command.
2329 * If this fails, we'll get another timeout 2 seconds
2330 * later which will attempt a bus reset.
2331 */
2332 bccb->flags |= BCCB_DEVICE_RESET;
2333 ccb->ccb_h.timeout_ch =
2334 timeout(bttimeout, (caddr_t)bccb, 2 * hz);
2335
2336 bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2337
2338 /* No Data Transfer */
2339 bt->recovery_bccb->hccb.datain = TRUE;
2340 bt->recovery_bccb->hccb.dataout = TRUE;
2341 bt->recovery_bccb->hccb.btstat = 0;
2342 bt->recovery_bccb->hccb.sdstat = 0;
2343 bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2344
2345 /* Tell the adapter about this command */
2346 bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2347 bt->cur_outbox->action_code = BMBO_START;
2348 bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
2349 btnextoutbox(bt);
2350 }
2351
2352 splx(s);
2353}
2354