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