1 /* $NetBSD: aic79xx_osm.h,v 1.20 2009/09/26 14:44:10 tsutsui Exp $ */ 2 3 /* 4 * NetBSD platform specific driver option settings, data structures, 5 * function declarations and includes. 6 * 7 * Copyright (c) 1994-2001 Justin T. Gibbs. 8 * Copyright (c) 2001-2002 Adaptec Inc. 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. 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 * Alternatively, this software may be distributed under the terms of the 21 * GNU Public License ("GPL"). 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $NetBSD: aic79xx_osm.h,v 1.20 2009/09/26 14:44:10 tsutsui Exp $ 36 * 37 * //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.h#19 $$NetBSD: aic79xx_osm.h,v 1.20 2009/09/26 14:44:10 tsutsui Exp $ 38 * 39 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.h,v 1.9 2003/05/26 21:43:29 gibbs Exp $ 40 */ 41 /* 42 * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. 43 * - April 2003 44 */ 45 46 #ifndef _AIC79XX_NETBSD_H_ 47 #define _AIC79XX_NETBSD_H_ 48 49 #include "opt_ahd.h" /* for config options */ 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/systm.h> 54 #include <sys/device.h> 55 #include <sys/malloc.h> 56 #include <sys/buf.h> 57 #include <sys/proc.h> 58 #include <sys/scsiio.h> 59 #include <sys/reboot.h> 60 #include <sys/kthread.h> 61 62 #include <dev/pci/pcireg.h> 63 #include <dev/pci/pcivar.h> 64 65 #include <sys/bus.h> 66 #include <sys/intr.h> 67 68 #include <dev/scsipi/scsi_all.h> 69 #include <dev/scsipi/scsipi_all.h> 70 #include <dev/scsipi/scsi_message.h> 71 #include <dev/scsipi/scsipi_debug.h> 72 #include <dev/scsipi/scsiconf.h> 73 #include <dev/scsipi/scsi_iu.h> 74 75 #include <uvm/uvm_extern.h> 76 77 #include <dev/ic/aic7xxx_cam.h> 78 79 /****************************** Platform Macros *******************************/ 80 #define SIM_IS_SCSIBUS_B(ahd, sim) \ 81 (0) 82 #define SIM_CHANNEL(ahd, sim) \ 83 ('A') 84 #define SIM_SCSI_ID(ahd, sim) \ 85 (ahd->our_id) 86 #define SIM_PATH(ahd, sim) \ 87 (ahd->platform_data->path) 88 #define BUILD_SCSIID(ahd, sim, target_id, our_id) \ 89 ((((target_id) << TID_SHIFT) & TID) | (our_id)) 90 91 92 #define SCB_GET_SIM(ahd, scb) \ 93 ((ahd)->platform_data->sim) 94 95 #ifndef offsetof 96 #define offsetof(type, member) ((size_t)(&((type *)0)->member)) 97 #endif 98 99 /************************* Forward Declarations *******************************/ 100 typedef pcireg_t ahd_dev_softc_t; 101 /***************************** Bus Space/DMA **********************************/ 102 #define ahd_dma_tag_create(ahd, parent_tag, alignment, boundary, \ 103 lowaddr, highaddr, filter, filterarg, \ 104 maxsize, nsegments, maxsegsz, flags, \ 105 dma_tagp) \ 106 bus_dma_tag_create(parent_tag, alignment, boundary, \ 107 lowaddr, highaddr, filter, filterarg, \ 108 maxsize, nsegments, maxsegsz, flags, \ 109 dma_tagp) 110 111 #define ahd_dma_tag_destroy(ahd, tag) \ 112 bus_dma_tag_destroy(tag) 113 114 #define ahd_dmamem_alloc(ahd, dmat, vaddr, flags, mapp) \ 115 bus_dmamem_alloc(dmat, vaddr, flags, mapp) 116 117 #define ahd_dmamem_free(ahd, dmat, vaddr, map) \ 118 bus_dmamem_free(dmat, vaddr, map) 119 120 #define ahd_dmamap_create(ahd, tag, flags, mapp) \ 121 bus_dmamap_create(tag, flags, mapp) 122 123 #define ahd_dmamap_destroy(ahd, tag, map) \ 124 bus_dmamap_destroy(tag, map) 125 126 #define ahd_dmamap_load(ahd, dmat, map, addr, buflen, callback, \ 127 callback_arg, flags) \ 128 bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags) 129 130 #define ahd_dmamap_unload(ahd, tag, map) \ 131 bus_dmamap_unload(tag, map) 132 133 /* XXX Need to update Bus DMA for partial map syncs */ 134 #define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op) \ 135 bus_dmamap_sync(dma_tag, dmamap, offset, len, op) 136 137 /************************ Tunable Driver Parameters **************************/ 138 /* 139 * The number of DMA segments supported. The sequencer can handle any number 140 * of physically contiguous S/G entrys. To reduce the driver's memory 141 * consumption, we limit the number supported to be sufficient to handle 142 * the largest mapping supported by the kernel, MAXPHYS. Assuming the 143 * transfer is as fragmented as possible and unaligned, this turns out to 144 * be the number of paged sized transfers in MAXPHYS plus an extra element 145 * to handle any unaligned residual. The sequencer fetches SG elements 146 * in cacheline sized chucks, so make the number per-transaction an even 147 * multiple of 16 which should align us on even the largest of cacheline 148 * boundaries. 149 */ 150 #define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16)) 151 152 /* This driver supports target mode */ 153 #if NOT_YET 154 #define AHD_TARGET_MODE 1 155 #endif 156 157 /************************** Softc/SCB Platform Data ***************************/ 158 struct ahd_platform_data { 159 }; 160 161 struct scb_platform_data { 162 }; 163 164 /********************************* Byte Order *********************************/ 165 #define ahd_htobe16(x) htobe16(x) 166 #define ahd_htobe32(x) htobe32(x) 167 #define ahd_htobe64(x) htobe64(x) 168 #define ahd_htole16(x) htole16(x) 169 #define ahd_htole32(x) htole32(x) 170 #define ahd_htole64(x) htole64(x) 171 172 #define ahd_be16toh(x) be16toh(x) 173 #define ahd_be32toh(x) be32toh(x) 174 #define ahd_be64toh(x) be64toh(x) 175 #define ahd_le16toh(x) le16toh(x) 176 #define ahd_le32toh(x) le32toh(x) 177 #define ahd_le64toh(x) le64toh(x) 178 179 /************************** Timer DataStructures ******************************/ 180 typedef struct callout ahd_timer_t; 181 182 /***************************** Core Includes **********************************/ 183 #if AHD_REG_PRETTY_PRINT 184 #define AIC_DEBUG_REGISTERS 1 185 #else 186 #define AIC_DEBUG_REGISTERS 0 187 #endif 188 #include <dev/ic/aic79xxvar.h> 189 190 /***************************** Timer Facilities *******************************/ 191 void ahd_timeout(void*); 192 #define ahd_timer_init(timer) callout_init(timer, 0) 193 #define ahd_timer_stop callout_stop 194 195 static __inline void 196 ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg) 197 { 198 callout_reset(timer, (usec * hz)/1000000, func, arg); 199 } 200 201 static __inline void 202 ahd_scb_timer_reset(struct scb *scb, u_int usec) 203 { 204 if (!(scb->xs->xs_control & XS_CTL_POLL)) { 205 callout_reset(&scb->xs->xs_callout, 206 (usec * hz)/1000000, ahd_timeout, scb); 207 } 208 } 209 210 /*************************** Device Access ************************************/ 211 #define ahd_inb(ahd, port) \ 212 bus_space_read_1((ahd)->tags[(port) >> 8], \ 213 (ahd)->bshs[(port) >> 8], (port) & 0xFF) 214 215 #define ahd_outb(ahd, port, value) \ 216 bus_space_write_1((ahd)->tags[(port) >> 8], \ 217 (ahd)->bshs[(port) >> 8], (port) & 0xFF, value) 218 219 #define ahd_inw_atomic(ahd, port) \ 220 ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8], \ 221 (ahd)->bshs[(port) >> 8], (port) & 0xFF)) 222 223 #define ahd_outw_atomic(ahd, port, value) \ 224 bus_space_write_2((ahd)->tags[(port) >> 8], \ 225 (ahd)->bshs[(port) >> 8], \ 226 (port & 0xFF), ahd_htole16(value)) 227 228 #define ahd_outsb(ahd, port, valp, count) \ 229 bus_space_write_multi_1((ahd)->tags[(port) >> 8], \ 230 (ahd)->bshs[(port) >> 8], \ 231 (port & 0xFF), valp, count) 232 233 #define ahd_insb(ahd, port, valp, count) \ 234 bus_space_read_multi_1((ahd)->tags[(port) >> 8], \ 235 (ahd)->bshs[(port) >> 8], \ 236 (port & 0xFF), valp, count) 237 238 static __inline void ahd_flush_device_writes(struct ahd_softc *); 239 240 static __inline void 241 ahd_flush_device_writes(struct ahd_softc *ahd) 242 { 243 /* XXX Is this sufficient for all architectures??? */ 244 ahd_inb(ahd, INTSTAT); 245 } 246 247 /**************************** Locking Primitives ******************************/ 248 /* Lock protecting internal data structures */ 249 static __inline void ahd_lockinit(struct ahd_softc *); 250 static __inline void ahd_lock(struct ahd_softc *, int *); 251 static __inline void ahd_unlock(struct ahd_softc *, int *); 252 253 /* Lock held during command completion to the upper layer */ 254 static __inline void ahd_done_lockinit(struct ahd_softc *); 255 static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *); 256 static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *); 257 258 /* Lock held during ahd_list manipulation and ahd softc frees */ 259 static __inline void ahd_list_lockinit(void); 260 static __inline void ahd_list_lock(unsigned long *); 261 static __inline void ahd_list_unlock(unsigned long *); 262 263 static __inline void 264 ahd_lockinit(struct ahd_softc *ahd) 265 { 266 } 267 268 static __inline void 269 ahd_lock(struct ahd_softc *ahd, int *flags) 270 { 271 *flags = splbio(); 272 } 273 274 static __inline void 275 ahd_unlock(struct ahd_softc *ahd, int *flags) 276 { 277 splx(*flags); 278 } 279 280 /* Lock held during command completion to the upper layer */ 281 static __inline void 282 ahd_done_lockinit(struct ahd_softc *ahd) 283 { 284 } 285 286 static __inline void 287 ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags) 288 { 289 } 290 291 static __inline void 292 ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags) 293 { 294 } 295 296 /* Lock held during ahd_list manipulation and ahd softc frees */ 297 static __inline void 298 ahd_list_lockinit(void) 299 { 300 } 301 302 static __inline void 303 ahd_list_lock(unsigned long *flags) 304 { 305 } 306 307 static __inline void 308 ahd_list_unlock(unsigned long *flags) 309 { 310 } 311 /****************************** OS Primitives *********************************/ 312 #define ahd_delay DELAY 313 314 /************************** Transaction Operations ****************************/ 315 static __inline void ahd_set_transaction_status(struct scb *, uint32_t); 316 static __inline void ahd_set_scsi_status(struct scb *, uint32_t); 317 static __inline void ahd_set_xfer_status(struct scb *, uint32_t); 318 static __inline uint32_t ahd_get_transaction_status(struct scb *); 319 static __inline uint32_t ahd_get_scsi_status(struct scb *); 320 static __inline void ahd_set_transaction_tag(struct scb *, int, u_int); 321 static __inline u_long ahd_get_transfer_length(struct scb *); 322 static __inline int ahd_get_transfer_dir(struct scb *); 323 static __inline void ahd_set_residual(struct scb *, u_long); 324 static __inline void ahd_set_sense_residual(struct scb *, u_long); 325 static __inline u_long ahd_get_residual(struct scb *); 326 static __inline int ahd_perform_autosense(struct scb *); 327 static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*); 328 static __inline void ahd_freeze_simq(struct ahd_softc *); 329 static __inline void ahd_release_simq(struct ahd_softc *); 330 static __inline void ahd_freeze_scb(struct scb *); 331 static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *); 332 static __inline int ahd_platform_abort_scbs(struct ahd_softc *, int, 333 char, int, u_int, role_t, uint32_t); 334 335 static __inline 336 void ahd_set_transaction_status(struct scb *scb, uint32_t status) 337 { 338 scb->xs->error = status; 339 } 340 341 static __inline 342 void ahd_set_scsi_status(struct scb *scb, uint32_t status) 343 { 344 scb->xs->xs_status = status; 345 } 346 347 static __inline 348 void ahd_set_xfer_status(struct scb *scb, uint32_t status) 349 { 350 scb->xs->status = status; 351 } 352 353 static __inline 354 uint32_t ahd_get_transaction_status(struct scb *scb) 355 { 356 return (scb->xs->error); 357 } 358 359 static __inline 360 uint32_t ahd_get_scsi_status(struct scb *scb) 361 { 362 return (scb->xs->xs_status); 363 } 364 365 static __inline 366 void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type) 367 { 368 scb->xs->xs_tag_type = type; 369 } 370 371 static __inline 372 u_long ahd_get_transfer_length(struct scb *scb) 373 { 374 return (scb->xs->datalen); 375 } 376 377 static __inline 378 int ahd_get_transfer_dir(struct scb *scb) 379 { 380 return (scb->xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT)); 381 } 382 383 static __inline 384 void ahd_set_residual(struct scb *scb, u_long resid) 385 { 386 scb->xs->resid = resid; 387 } 388 389 390 static __inline 391 void ahd_set_sense_residual(struct scb *scb, u_long resid) 392 { 393 //scb->xs->sense.scsi_sense.extra_len = resid; /* ??? */ 394 } 395 396 397 static __inline 398 u_long ahd_get_residual(struct scb *scb) 399 { 400 return (scb->xs->resid); 401 } 402 403 static __inline 404 int ahd_perform_autosense(struct scb *scb) 405 { 406 return (!(scb->flags & SCB_SENSE)); 407 } 408 409 static __inline uint32_t 410 ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb) 411 { 412 return (sizeof(struct scsi_sense_data)); 413 } 414 415 static __inline void 416 ahd_freeze_simq(struct ahd_softc *ahd) 417 { 418 /* do nothing for now */ 419 } 420 421 static __inline void 422 ahd_release_simq(struct ahd_softc *ahd) 423 { 424 /* do nothing for now */ 425 } 426 427 static __inline void 428 ahd_freeze_scb(struct scb *scb) 429 { 430 struct scsipi_xfer *xs = scb->xs; 431 432 if (!(scb->flags & SCB_FREEZE_QUEUE)) { 433 scsipi_periph_freeze(xs->xs_periph, 1); 434 scb->flags |= SCB_FREEZE_QUEUE; 435 } 436 } 437 438 static __inline void 439 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb) 440 { 441 /* Nothing to do here for NetBSD */ 442 } 443 444 static __inline int 445 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, 446 char channel, int lun, u_int tag, 447 role_t role, uint32_t status) 448 { 449 /* Nothing to do here for NetBSD */ 450 return (0); 451 } 452 453 static __inline void 454 ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb) 455 { 456 #ifdef __FreeBSD__ 457 /* What do we do to generically handle driver resource shortages??? */ 458 if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0 459 && scb->io_ctx != NULL 460 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 461 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ; 462 ahd->flags &= ~AHD_RESOURCE_SHORTAGE; 463 } 464 scb->io_ctx = NULL; 465 #endif /* end of modification */ 466 } 467 468 /********************************** PCI ***************************************/ 469 #if AHD_PCI_CONFIG > 0 470 static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t, int, int); 471 static __inline void ahd_pci_write_config(ahd_dev_softc_t, int, 472 uint32_t, int); 473 static __inline int ahd_get_pci_function(ahd_dev_softc_t); 474 static __inline int ahd_get_pci_slot(ahd_dev_softc_t); 475 static __inline int ahd_get_pci_bus(ahd_dev_softc_t); 476 477 int ahd_pci_map_registers(struct ahd_softc *); 478 int ahd_pci_map_int(struct ahd_softc *); 479 480 static __inline uint32_t 481 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width) 482 { 483 return (pci_read_config(pci, reg, width)); 484 } 485 486 static __inline void 487 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width) 488 { 489 pci_write_config(pci, reg, value, width); 490 } 491 492 static __inline int 493 ahd_get_pci_function(ahd_dev_softc_t pci) 494 { 495 return (pci_get_function(pci)); 496 } 497 498 static __inline int 499 ahd_get_pci_slot(ahd_dev_softc_t pci) 500 { 501 return (pci_get_slot(pci)); 502 } 503 504 505 static __inline int 506 ahd_get_pci_bus(ahd_dev_softc_t pci) 507 { 508 return (pci_get_bus(pci)); 509 } 510 #endif 511 512 typedef enum 513 { 514 AHD_POWER_STATE_D0, 515 AHD_POWER_STATE_D1, 516 AHD_POWER_STATE_D2, 517 AHD_POWER_STATE_D3 518 } ahd_power_state; 519 520 void ahd_power_state_change(struct ahd_softc *, ahd_power_state); 521 522 /******************************** VL/EISA *************************************/ 523 int aic7770_map_registers(struct ahd_softc *); 524 int aic7770_map_int(struct ahd_softc *, int); 525 526 /********************************* Debug **************************************/ 527 static __inline void ahd_print_path(struct ahd_softc *, struct scb *); 528 static __inline void ahd_platform_dump_card_state(struct ahd_softc *); 529 static __inline void 530 ahd_print_path(struct ahd_softc *ahd, struct scb *scb) 531 { 532 printf("%s:", device_xname(ahd->sc_dev)); 533 } 534 535 static __inline void 536 ahd_platform_dump_card_state(struct ahd_softc *ahd) 537 { 538 /* Nothing to do here for NetBSD */ 539 } 540 /**************************** Transfer Settings *******************************/ 541 void ahd_notify_xfer_settings_change(struct ahd_softc *, 542 struct ahd_devinfo *); 543 void ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *, 544 ahd_queue_alg); 545 546 /************************* Initialization/Teardown ****************************/ 547 int ahd_platform_alloc(struct ahd_softc *, void *); 548 void ahd_platform_free(struct ahd_softc *); 549 int ahd_map_int(struct ahd_softc *); 550 int ahd_attach(struct ahd_softc *); 551 int ahd_softc_comp(struct ahd_softc *, struct ahd_softc *); 552 int ahd_detach(struct ahd_softc *, int); 553 #define ahd_platform_init(arg) 554 555 556 /****************************** Interrupts ************************************/ 557 void ahd_platform_intr(void *); 558 static __inline void ahd_platform_flushwork(struct ahd_softc *); 559 static __inline void 560 ahd_platform_flushwork(struct ahd_softc *ahd) 561 { 562 /* Nothing to do here for NetBSD */ 563 } 564 565 /************************ Misc Function Declarations **************************/ 566 void ahd_done(struct ahd_softc *, struct scb *); 567 void ahd_send_async(struct ahd_softc *, char, u_int, u_int, ac_code, void *); 568 569 #endif /* _AIC79XX_NETBSD_H_ */ 570