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