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