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