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