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