Merge from vendor branch BINUTILS:
[dragonfly.git] / sys / dev / disk / aic7xxx / aic7xxx_osm.h
1 /*
2  * FreeBSD platform specific driver option settings, data structures,
3  * function declarations and includes.
4  *
5  * Copyright (c) 1994-2001 Justin T. Gibbs.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
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  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.h#15 $
33  *
34  * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx_osm.h,v 1.14.2.6 2003/06/10 03:26:09 gibbs Exp $
35  * $DragonFly: src/sys/dev/disk/aic7xxx/aic7xxx_osm.h,v 1.7 2005/08/25 23:37:35 dillon Exp $
36  */
37
38 #ifndef _AIC7XXX_FREEBSD_H_
39 #define _AIC7XXX_FREEBSD_H_
40
41 #include <opt_aic7xxx.h>        /* for config options */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>            /* For device_t */
46 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
47 #include <sys/endian.h>
48 #endif
49 #include <sys/eventhandler.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/queue.h>
53 #include <sys/thread2.h>
54
55 #if defined(__DragonFly__) || __FreeBSD_version < 500000
56 #include <use_pci.h>
57 #else
58 #define NPCI 1
59 #endif
60
61 #if NPCI > 0
62 #define AHC_PCI_CONFIG 1
63 #include <machine/bus_memio.h>
64 #endif
65 #include <machine/bus_pio.h>
66 #include <machine/bus.h>
67 #include <machine/endian.h>
68 #include <machine/clock.h>
69 #include <machine/resource.h>
70
71 #include <sys/rman.h>
72
73 #if NPCI > 0
74 #include <bus/pci/pcireg.h>
75 #include <bus/pci/pcivar.h>
76 #endif
77
78 #include <bus/cam/cam.h>
79 #include <bus/cam/cam_ccb.h>
80 #include <bus/cam/cam_debug.h>
81 #include <bus/cam/cam_sim.h>
82 #include <bus/cam/cam_xpt_sim.h>
83
84 #include <bus/cam/scsi/scsi_all.h>
85 #include <bus/cam/scsi/scsi_message.h>
86
87 #ifdef CAM_NEW_TRAN_CODE
88 #define AHC_NEW_TRAN_SETTINGS
89 #endif /* CAM_NEW_TRAN_CODE */
90
91 /*************************** Attachment Bookkeeping ***************************/
92 extern devclass_t ahc_devclass;
93
94 /****************************** Platform Macros *******************************/
95 #define SIM_IS_SCSIBUS_B(ahc, sim)      \
96         ((sim) == ahc->platform_data->sim_b)
97 #define SIM_CHANNEL(ahc, sim)   \
98         (((sim) == ahc->platform_data->sim_b) ? 'B' : 'A')
99 #define SIM_SCSI_ID(ahc, sim)   \
100         (((sim) == ahc->platform_data->sim_b) ? ahc->our_id_b : ahc->our_id)
101 #define SIM_PATH(ahc, sim)      \
102         (((sim) == ahc->platform_data->sim_b) ? ahc->platform_data->path_b \
103                                               : ahc->platform_data->path)
104 #define BUILD_SCSIID(ahc, sim, target_id, our_id) \
105         ((((target_id) << TID_SHIFT) & TID) | (our_id) \
106         | (SIM_IS_SCSIBUS_B(ahc, sim) ? TWIN_CHNLB : 0))
107
108 #define SCB_GET_SIM(ahc, scb) \
109         (SCB_GET_CHANNEL(ahc, scb) == 'A' ? (ahc)->platform_data->sim \
110                                           : (ahc)->platform_data->sim_b)
111
112 #ifndef offsetof
113 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))
114 #endif
115 /************************* Forward Declarations *******************************/
116 typedef device_t ahc_dev_softc_t;
117 typedef union ccb *ahc_io_ctx_t;
118
119 /***************************** Bus Space/DMA **********************************/
120 #define ahc_dma_tag_create(ahc, parent_tag, alignment, boundary,        \
121                            lowaddr, highaddr, filter, filterarg,        \
122                            maxsize, nsegments, maxsegsz, flags,         \
123                            dma_tagp)                                    \
124         bus_dma_tag_create(parent_tag, alignment, boundary,             \
125                            lowaddr, highaddr, filter, filterarg,        \
126                            maxsize, nsegments, maxsegsz, flags,         \
127                            dma_tagp)
128
129 #define ahc_dma_tag_destroy(ahc, tag)                                   \
130         bus_dma_tag_destroy(tag)
131
132 #define ahc_dmamem_alloc(ahc, dmat, vaddr, flags, mapp)                 \
133         bus_dmamem_alloc(dmat, vaddr, flags, mapp)
134
135 #define ahc_dmamem_free(ahc, dmat, vaddr, map)                          \
136         bus_dmamem_free(dmat, vaddr, map)
137
138 #define ahc_dmamap_create(ahc, tag, flags, mapp)                        \
139         bus_dmamap_create(tag, flags, mapp)
140
141 #define ahc_dmamap_destroy(ahc, tag, map)                               \
142         bus_dmamap_destroy(tag, map)
143
144 #define ahc_dmamap_load(ahc, dmat, map, addr, buflen, callback,         \
145                         callback_arg, flags)                            \
146         bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)
147
148 #define ahc_dmamap_unload(ahc, tag, map)                                \
149         bus_dmamap_unload(tag, map)
150
151 /* XXX Need to update Bus DMA for partial map syncs */
152 #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)          \
153         bus_dmamap_sync(dma_tag, dmamap, op)
154
155 /************************ Tunable Driver Parameters  **************************/
156 /*
157  * The number of dma segments supported.  The sequencer can handle any number
158  * of physically contiguous S/G entrys.  To reduce the driver's memory
159  * consumption, we limit the number supported to be sufficient to handle
160  * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
161  * transfer is as fragmented as possible and unaligned, this turns out to
162  * be the number of paged sized transfers in MAXPHYS plus an extra element
163  * to handle any unaligned residual.  The sequencer fetches SG elements
164  * in cacheline sized chucks, so make the number per-transaction an even
165  * multiple of 16 which should align us on even the largest of cacheline
166  * boundaries. 
167  */
168 #define AHC_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
169
170 /* This driver supports target mode */
171 #define AHC_TARGET_MODE 1
172
173 /************************** Softc/SCB Platform Data ***************************/
174 struct ahc_platform_data {
175         /*
176          * Hooks into the XPT.
177          */
178         struct  cam_sim         *sim;
179         struct  cam_sim         *sim_b;
180         struct  cam_path        *path;
181         struct  cam_path        *path_b;
182
183         int                      regs_res_type;
184         int                      regs_res_id;
185         int                      irq_res_type;
186         struct resource         *regs;
187         struct resource         *irq;
188         void                    *ih;
189         eventhandler_tag         eh;
190 };
191
192 struct scb_platform_data {
193 };
194
195 /********************************* Byte Order *********************************/
196 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
197 #define ahc_htobe16(x) htobe16(x)
198 #define ahc_htobe32(x) htobe32(x)
199 #define ahc_htobe64(x) htobe64(x)
200 #define ahc_htole16(x) htole16(x)
201 #define ahc_htole32(x) htole32(x)
202 #define ahc_htole64(x) htole64(x)
203
204 #define ahc_be16toh(x) be16toh(x)
205 #define ahc_be32toh(x) be32toh(x)
206 #define ahc_be64toh(x) be64toh(x)
207 #define ahc_le16toh(x) le16toh(x)
208 #define ahc_le32toh(x) le32toh(x)
209 #define ahc_le64toh(x) le64toh(x)
210 #else
211 #define ahc_htobe16(x) (x)
212 #define ahc_htobe32(x) (x)
213 #define ahc_htobe64(x) (x)
214 #define ahc_htole16(x) (x)
215 #define ahc_htole32(x) (x)
216 #define ahc_htole64(x) (x)
217
218 #define ahc_be16toh(x) (x)
219 #define ahc_be32toh(x) (x)
220 #define ahc_be64toh(x) (x)
221 #define ahc_le16toh(x) (x)
222 #define ahc_le32toh(x) (x)
223 #define ahc_le64toh(x) (x)
224 #endif
225
226 /************************** Timer DataStructures ******************************/
227 typedef struct callout ahc_timer_t;
228
229 /***************************** Core Includes **********************************/
230 #if AHC_REG_PRETTY_PRINT
231 #define AIC_DEBUG_REGISTERS 1
232 #else
233 #define AIC_DEBUG_REGISTERS 0
234 #endif
235 #include "aic7xxx.h"
236
237 /***************************** Timer Facilities *******************************/
238 timeout_t ahc_timeout;
239
240 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
241 #define ahc_timer_init(timer) callout_init(timer, /*mpsafe*/0)
242 #else
243 #define ahc_timer_init callout_init
244 #endif
245 #define ahc_timer_stop callout_stop
246
247 static __inline void
248 ahc_timer_reset(ahc_timer_t *timer, u_int usec, ahc_callback_t *func, void *arg)
249 {
250         callout_reset(timer, (usec * hz)/1000000, func, arg);
251 }
252
253 static __inline void
254 ahc_scb_timer_reset(struct scb *scb, u_int usec)
255 {
256         callout_reset(&scb->io_ctx->ccb_h.timeout_ch, (usec * hz)/1000000,
257                       ahc_timeout, scb);
258 }
259
260 /*************************** Device Access ************************************/
261 #define ahc_inb(ahc, port)                              \
262         bus_space_read_1((ahc)->tag, (ahc)->bsh, port)
263
264 #define ahc_outb(ahc, port, value)                      \
265         bus_space_write_1((ahc)->tag, (ahc)->bsh, port, value)
266
267 #define ahc_outsb(ahc, port, valp, count)               \
268         bus_space_write_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
269
270 #define ahc_insb(ahc, port, valp, count)                \
271         bus_space_read_multi_1((ahc)->tag, (ahc)->bsh, port, valp, count)
272
273 static __inline void ahc_flush_device_writes(struct ahc_softc *);
274
275 static __inline void
276 ahc_flush_device_writes(struct ahc_softc *ahc)
277 {
278         /* XXX Is this sufficient for all architectures??? */
279         ahc_inb(ahc, INTSTAT);
280 }
281
282 /**************************** Locking Primitives ******************************/
283 /* Lock protecting internal data structures */
284 static __inline void ahc_lockinit(struct ahc_softc *);
285 static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
286 static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
287
288 /* Lock held during command compeletion to the upper layer */
289 static __inline void ahc_done_lockinit(struct ahc_softc *);
290 static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
291 static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
292
293 /* Lock held during ahc_list manipulation and ahc softc frees */
294 static __inline void ahc_list_lockinit(void);
295 static __inline void ahc_list_lock(unsigned long *flags);
296 static __inline void ahc_list_unlock(unsigned long *flags);
297
298 static __inline void
299 ahc_lockinit(struct ahc_softc *ahc)
300 {
301 }
302
303 static __inline void
304 ahc_lock(struct ahc_softc *ahc, unsigned long *flags __unused)
305 {
306         crit_enter_id("ahc");
307 }
308
309 static __inline void
310 ahc_unlock(struct ahc_softc *ahc, unsigned long *flags __unused)
311 {
312         crit_exit_id("ahc");
313 }
314
315 /* Lock held during command compeletion to the upper layer */
316 static __inline void
317 ahc_done_lockinit(struct ahc_softc *ahc)
318 {
319 }
320
321 static __inline void
322 ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
323 {
324 }
325
326 static __inline void
327 ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
328 {
329 }
330
331 /* Lock held during ahc_list manipulation and ahc softc frees */
332 static __inline void
333 ahc_list_lockinit(void)
334 {
335 }
336
337 static __inline void
338 ahc_list_lock(unsigned long *flags)
339 {
340 }
341
342 static __inline void
343 ahc_list_unlock(unsigned long *flags)
344 {
345 }
346 /****************************** OS Primitives *********************************/
347 #define ahc_delay DELAY
348
349 /************************** Transaction Operations ****************************/
350 static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
351 static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
352 static __inline uint32_t ahc_get_transaction_status(struct scb *);
353 static __inline uint32_t ahc_get_scsi_status(struct scb *);
354 static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
355 static __inline u_long ahc_get_transfer_length(struct scb *);
356 static __inline int ahc_get_transfer_dir(struct scb *);
357 static __inline void ahc_set_residual(struct scb *, u_long);
358 static __inline void ahc_set_sense_residual(struct scb *, u_long);
359 static __inline u_long ahc_get_residual(struct scb *);
360 static __inline int ahc_perform_autosense(struct scb *);
361 static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc*, struct scb*);
362 static __inline void ahc_freeze_ccb(union ccb *ccb);
363 static __inline void ahc_freeze_scb(struct scb *scb);
364 static __inline void ahc_platform_freeze_devq(struct ahc_softc *, struct scb *);
365 static __inline int  ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
366                                              char channel, int lun, u_int tag,
367                                              role_t role, uint32_t status);
368
369 static __inline
370 void ahc_set_transaction_status(struct scb *scb, uint32_t status)
371 {
372         scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
373         scb->io_ctx->ccb_h.status |= status;
374 }
375
376 static __inline
377 void ahc_set_scsi_status(struct scb *scb, uint32_t status)
378 {
379         scb->io_ctx->csio.scsi_status = status;
380 }
381
382 static __inline
383 uint32_t ahc_get_transaction_status(struct scb *scb)
384 {
385         return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
386 }
387
388 static __inline
389 uint32_t ahc_get_scsi_status(struct scb *scb)
390 {
391         return (scb->io_ctx->csio.scsi_status);
392 }
393
394 static __inline
395 void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
396 {
397         scb->io_ctx->csio.tag_action = type;
398         if (enabled)
399                 scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
400         else
401                 scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
402 }
403
404 static __inline
405 u_long ahc_get_transfer_length(struct scb *scb)
406 {
407         return (scb->io_ctx->csio.dxfer_len);
408 }
409
410 static __inline
411 int ahc_get_transfer_dir(struct scb *scb)
412 {
413         return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
414 }
415
416 static __inline
417 void ahc_set_residual(struct scb *scb, u_long resid)
418 {
419         scb->io_ctx->csio.resid = resid;
420 }
421
422 static __inline
423 void ahc_set_sense_residual(struct scb *scb, u_long resid)
424 {
425         scb->io_ctx->csio.sense_resid = resid;
426 }
427
428 static __inline
429 u_long ahc_get_residual(struct scb *scb)
430 {
431         return (scb->io_ctx->csio.resid);
432 }
433
434 static __inline
435 int ahc_perform_autosense(struct scb *scb)
436 {
437         return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
438 }
439
440 static __inline uint32_t
441 ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
442 {
443         return (sizeof(struct scsi_sense_data));
444 }
445
446 static __inline void
447 ahc_freeze_ccb(union ccb *ccb)
448 {
449         if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
450                 ccb->ccb_h.status |= CAM_DEV_QFRZN;
451                 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
452         }
453 }
454
455 static __inline void
456 ahc_freeze_scb(struct scb *scb)
457 {
458         ahc_freeze_ccb(scb->io_ctx);
459 }
460
461 static __inline void
462 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
463 {
464         /* Nothing to do here for FreeBSD */
465 }
466
467 static __inline int
468 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
469                         char channel, int lun, u_int tag,
470                         role_t role, uint32_t status)
471 {
472         /* Nothing to do here for FreeBSD */
473         return (0);
474 }
475
476 static __inline void
477 ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
478 {
479         /* What do we do to generically handle driver resource shortages??? */
480         if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
481          && scb->io_ctx != NULL
482          && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
483                 scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
484                 ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
485         }
486         scb->io_ctx = NULL;
487 }
488
489 /********************************** PCI ***************************************/
490 #ifdef AHC_PCI_CONFIG
491 static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
492                                              int reg, int width);
493 static __inline void     ahc_pci_write_config(ahc_dev_softc_t pci,
494                                               int reg, uint32_t value,
495                                               int width);
496 static __inline int      ahc_get_pci_function(ahc_dev_softc_t);
497 static __inline int      ahc_get_pci_slot(ahc_dev_softc_t);
498 static __inline int      ahc_get_pci_bus(ahc_dev_softc_t);
499
500 int                      ahc_pci_map_registers(struct ahc_softc *ahc);
501 int                      ahc_pci_map_int(struct ahc_softc *ahc);
502
503 static __inline uint32_t
504 ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
505 {
506         return (pci_read_config(pci, reg, width));
507 }
508
509 static __inline void
510 ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
511 {
512         pci_write_config(pci, reg, value, width);
513 }
514
515 static __inline int
516 ahc_get_pci_function(ahc_dev_softc_t pci)
517 {
518         return (pci_get_function(pci));
519 }
520
521 static __inline int
522 ahc_get_pci_slot(ahc_dev_softc_t pci)
523 {
524         return (pci_get_slot(pci));
525 }
526
527 static __inline int
528 ahc_get_pci_bus(ahc_dev_softc_t pci)
529 {
530         return (pci_get_bus(pci));
531 }
532
533 typedef enum
534 {
535         AHC_POWER_STATE_D0,
536         AHC_POWER_STATE_D1,
537         AHC_POWER_STATE_D2,
538         AHC_POWER_STATE_D3
539 } ahc_power_state;
540
541 void ahc_power_state_change(struct ahc_softc *ahc,
542                             ahc_power_state new_state);
543 #endif
544 /******************************** VL/EISA *************************************/
545 int aic7770_map_registers(struct ahc_softc *ahc, u_int port);
546 int aic7770_map_int(struct ahc_softc *ahc, int irq);
547
548 /********************************* Debug **************************************/
549 static __inline void    ahc_print_path(struct ahc_softc *, struct scb *);
550 static __inline void    ahc_platform_dump_card_state(struct ahc_softc *ahc);
551
552 static __inline void
553 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
554 {
555         xpt_print_path(scb->io_ctx->ccb_h.path);
556 }
557
558 static __inline void
559 ahc_platform_dump_card_state(struct ahc_softc *ahc)
560 {
561         /* Nothing to do here for FreeBSD */
562 }
563 /**************************** Transfer Settings *******************************/
564 void      ahc_notify_xfer_settings_change(struct ahc_softc *,
565                                           struct ahc_devinfo *);
566 void      ahc_platform_set_tags(struct ahc_softc *, struct ahc_devinfo *,
567                                 int /*enable*/);
568
569 /************************* Initialization/Teardown ****************************/
570 int       ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
571 void      ahc_platform_free(struct ahc_softc *ahc);
572 int       ahc_map_int(struct ahc_softc *ahc);
573 int       ahc_attach(struct ahc_softc *);
574 int       ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc);
575 int       ahc_detach(device_t);
576
577 /****************************** Interrupts ************************************/
578 void                    ahc_platform_intr(void *);
579 static __inline void    ahc_platform_flushwork(struct ahc_softc *ahc);
580 static __inline void
581 ahc_platform_flushwork(struct ahc_softc *ahc)
582 {
583 }
584
585 /************************ Misc Function Declarations **************************/
586 void      ahc_done(struct ahc_softc *ahc, struct scb *scb);
587 void      ahc_send_async(struct ahc_softc *, char /*channel*/,
588                          u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
589 #endif  /* _AIC7XXX_FREEBSD_H_ */