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