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