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