aic79xx_openbsd.h revision 1.1
1/*	$OpenBSD: aic79xx_openbsd.h,v 1.1 2004/05/20 03:11:45 marco Exp $	*/
2/*
3 * FreeBSD platform specific driver option settings, data structures,
4 * function declarations and includes.
5 *
6 * Copyright (c) 1994-2001 Justin T. Gibbs.
7 * Copyright (c) 2001-2002 Adaptec Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions, and the following disclaimer,
15 *    without modification.
16 * 2. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * Alternatively, this software may be distributed under the terms of the
20 * GNU Public License ("GPL").
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $Id: aic79xx_openbsd.h,v 1.1 2004/05/20 03:11:45 marco Exp $
35 *
36 * $FreeBSD: src/sys/dev/aic7xxx/aic79xx_osm.h,v 1.13 2003/12/17 00:02:09 gibbs Exp $
37 *
38 * Additional copyrights by:
39 * Milos Urbanek
40 * Kenneth R. Westerback
41 * Marco Peereboom
42 *
43 */
44
45#ifndef _AIC79XX_OPENBSD_H_
46#define _AIC79XX_OPENBSD_H_
47
48#include "pci.h"		/* for config options */
49
50#include <sys/param.h>
51#include <sys/kernel.h>
52#include <sys/systm.h>
53#include <sys/device.h>
54#include <sys/malloc.h>
55#include <sys/buf.h>
56#include <sys/proc.h>
57#include <sys/queue.h>
58
59#define AIC_PCI_CONFIG 1
60#include <dev/pci/pcireg.h>
61#include <dev/pci/pcivar.h>
62
63#include <machine/bus.h>
64#include <machine/intr.h>
65
66#include <scsi/scsi_all.h>
67#include <scsi/scsi_message.h>
68#include <scsi/scsi_debug.h>
69#include <scsi/scsiconf.h>
70
71#include <uvm/uvm_extern.h>
72
73#ifdef DEBUG
74#define bootverbose     1
75#else
76#define bootverbose     0
77#endif
78/****************************** Platform Macros *******************************/
79#define	SCSI_IS_SCSIBUS_B(ahd, sc_link)	\
80	(0)
81#define	SCSI_CHANNEL(ahd, sc_link)	\
82	('A')
83#define	SCSI_SCSI_ID(ahd, sc_link)	\
84	(ahd->our_id)
85#define BUILD_SCSIID(ahd, sc_link, target_id, our_id) \
86        ((((target_id) << TID_SHIFT) & TID) | (our_id))
87
88#ifndef offsetof
89#define offsetof(type, member)  ((size_t)(&((type *)0)->member))
90#endif
91
92/************************* Forward Declarations *******************************/typedef struct pci_attach_args * ahd_dev_softc_t;
93
94/***************************** Bus Space/DMA **********************************/
95
96/* XXX Need to update Bus DMA for partial map syncs */
97#define ahd_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)		\
98	bus_dmamap_sync(dma_tag, dmamap, offset, len, op)
99
100/************************ Tunable Driver Parameters  **************************/
101/*
102 * The number of dma segments supported.  The sequencer can handle any number
103 * of physically contiguous S/G entrys.  To reduce the driver's memory
104 * consumption, we limit the number supported to be sufficient to handle
105 * the largest mapping supported by the kernel, MAXPHYS.  Assuming the
106 * transfer is as fragmented as possible and unaligned, this turns out to
107 * be the number of paged sized transfers in MAXPHYS plus an extra element
108 * to handle any unaligned residual.  The sequencer fetches SG elements
109 * in cacheline sized chucks, so make the number per-transaction an even
110 * multiple of 16 which should align us on even the largest of cacheline
111 * boundaries.
112 */
113#define AHD_NSEG (roundup(btoc(MAXPHYS) + 1, 16))
114
115/* This driver supports target mode */
116// #define AHD_TARGET_MODE 1
117
118/************************** Softc/SCB Platform Data ***************************/
119struct ahd_platform_data {
120};
121
122struct scb_platform_data {
123};
124
125/********************************* Byte Order *********************************/
126#define ahd_htobe16(x) htobe16(x)
127#define ahd_htobe32(x) htobe32(x)
128#define ahd_htobe64(x) htobe64(x)
129#define ahd_htole16(x) htole16(x)
130#define ahd_htole32(x) htole32(x)
131#define ahd_htole64(x) htole64(x)
132
133#define ahd_be16toh(x) be16toh(x)
134#define ahd_be32toh(x) be32toh(x)
135#define ahd_be64toh(x) be64toh(x)
136#define ahd_le16toh(x) letoh16(x)
137#define ahd_le32toh(x) letoh32(x)
138#define ahd_le64toh(x) letoh64(x)
139
140/************************** Timer DataStructures ******************************/
141typedef struct timeout ahd_timer_t;
142
143/***************************** Core Includes **********************************/
144
145#if AHD_REG_PRETTY_PRINT
146#define AIC_DEBUG_REGISTERS 1
147#else
148#define AIC_DEBUG_REGISTERS 0
149#endif
150#include <dev/ic/aic79xx.h>
151
152/***************************** Timer Facilities *******************************/
153void ahd_timeout(void*);
154
155ahd_callback_t  ahd_reset_poll;
156ahd_callback_t  ahd_stat_timer;
157
158#define ahd_timer_init callout_init
159#define ahd_timer_stop callout_stop
160
161static __inline void
162ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg)
163{
164	callout_reset(timer, (usec * hz)/1000000, func, arg);
165}
166
167static __inline void
168ahd_scb_timer_reset(struct scb *scb, u_int usec)
169{
170	if (!(scb->xs->xs_control & XS_CTL_POLL)) {
171		callout_reset(&scb->xs->xs_callout,
172		    (usec * hz)/1000000, ahd_timeout, scb);
173	}
174}
175
176/*************************** Device Access ************************************/
177#define ahd_inb(ahd, port)					\
178	bus_space_read_1((ahd)->tags[(port) >> 8],		\
179			 (ahd)->bshs[(port) >> 8], (port) & 0xFF)
180
181#define ahd_outb(ahd, port, value)				\
182	bus_space_write_1((ahd)->tags[(port) >> 8],		\
183			  (ahd)->bshs[(port) >> 8], (port) & 0xFF, value)
184
185#define ahd_inw_atomic(ahd, port)				\
186	ahd_le16toh(bus_space_read_2((ahd)->tags[(port) >> 8],	\
187				     (ahd)->bshs[(port) >> 8], (port) & 0xFF))
188
189#define ahd_outw_atomic(ahd, port, value)			\
190	bus_space_write_2((ahd)->tags[(port) >> 8],		\
191			  (ahd)->bshs[(port) >> 8],		\
192			  (port & 0xFF), ahd_htole16(value))
193
194#define ahd_outsb(ahd, port, valp, count)			\
195	bus_space_write_multi_1((ahd)->tags[(port) >> 8],	\
196				(ahd)->bshs[(port) >> 8],	\
197				(port & 0xFF), valp, count)
198
199#define ahd_insb(ahd, port, valp, count)			\
200	bus_space_read_multi_1((ahd)->tags[(port) >> 8],	\
201			       (ahd)->bshs[(port) >> 8],	\
202			       (port & 0xFF), valp, count)
203
204static __inline void ahd_flush_device_writes(struct ahd_softc *);
205
206static __inline void
207ahd_flush_device_writes(struct ahd_softc *ahd)
208{
209	/* XXX Is this sufficient for all architectures??? */
210	ahd_inb(ahd, INTSTAT);
211}
212
213/**************************** Locking Primitives ******************************/
214/* Lock protecting internal data structures */
215static __inline void ahd_lockinit(struct ahd_softc *);
216static __inline void ahd_lock(struct ahd_softc *, int *flags);
217static __inline void ahd_unlock(struct ahd_softc *, int *flags);
218
219/* Lock held during command compeletion to the upper layer */
220static __inline void ahd_done_lockinit(struct ahd_softc *);
221static __inline void ahd_done_lock(struct ahd_softc *, int *flags);
222static __inline void ahd_done_unlock(struct ahd_softc *, int *flags);
223
224/* Lock held during ahd_list manipulation and ahd softc frees */
225static __inline void ahd_list_lockinit(void);
226static __inline void ahd_list_lock(int *flags);
227static __inline void ahd_list_unlock(int *flags);
228
229static __inline void
230ahd_lockinit(struct ahd_softc *ahd)
231{
232}
233
234static __inline void
235ahd_lock(struct ahd_softc *ahd, int *flags)
236{
237	*flags = splbio();
238}
239
240static __inline void
241ahd_unlock(struct ahd_softc *ahd, int *flags)
242{
243	splx(*flags);
244}
245
246/* Lock held during command compeletion to the upper layer */
247static __inline void
248ahd_done_lockinit(struct ahd_softc *ahd)
249{
250}
251
252static __inline void
253ahd_done_lock(struct ahd_softc *ahd, int *flags)
254{
255}
256
257static __inline void
258ahd_done_unlock(struct ahd_softc *ahd, int *flags)
259{
260}
261
262/* Lock held during ahd_list manipulation and ahd softc frees */
263static __inline void
264ahd_list_lockinit(void)
265{
266}
267
268static __inline void
269ahd_list_lock(int *flags)
270{
271}
272
273static __inline void
274ahd_list_unlock(int *flags)
275{
276}
277
278/****************************** OS Primitives *********************************/
279#define ahd_delay DELAY
280
281/************************** Transaction Operations ****************************/
282static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
283static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
284static __inline uint32_t ahd_get_transaction_status(struct scb *);
285static __inline uint32_t ahd_get_scsi_status(struct scb *);
286static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
287static __inline u_long ahd_get_transfer_length(struct scb *);
288static __inline int ahd_get_transfer_dir(struct scb *);
289static __inline void ahd_set_residual(struct scb *, u_long);
290static __inline void ahd_set_sense_residual(struct scb *, u_long);
291static __inline u_long ahd_get_residual(struct scb *);
292static __inline int ahd_perform_autosense(struct scb *);
293static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc*, struct scb*);
294static __inline void ahd_freeze_simq(struct ahd_softc *);
295static __inline void ahd_release_simq(struct ahd_softc *);
296static __inline void ahd_freeze_scb(struct scb *);
297static __inline void ahd_platform_freeze_devq(struct ahd_softc *, struct scb *);
298static __inline int  ahd_platform_abort_scbs(struct ahd_softc *, int,
299		    char, int, u_int, role_t, uint32_t);
300
301static __inline
302void ahd_set_transaction_status(struct scb *scb, uint32_t status)
303{
304	scb->xs->error = status;
305}
306
307static __inline
308void ahd_set_scsi_status(struct scb *scb, uint32_t status)
309{
310	scb->xs->xs_status = status;
311}
312
313static __inline
314uint32_t ahd_get_transaction_status(struct scb *scb)
315{
316	if (scb->xs->flags & ITSDONE)
317		return CAM_REQ_CMP;
318	else
319		return scb->xs->error;
320}
321
322static __inline
323uint32_t ahd_get_scsi_status(struct scb *scb)
324{
325	return (scb->xs->status);
326}
327
328static __inline
329void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
330{
331}
332
333static __inline
334u_long ahd_get_transfer_length(struct scb *scb)
335{
336	return (scb->xs->datalen);
337}
338
339static __inline
340int ahd_get_transfer_dir(struct scb *scb)
341{
342	return (scb->xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT));
343}
344
345static __inline
346void ahd_set_residual(struct scb *scb, u_long resid)
347{
348	scb->xs->resid = resid;
349}
350
351static __inline
352void ahd_set_sense_residual(struct scb *scb, u_long resid)
353{
354	scb->xs->resid = resid;
355}
356
357static __inline
358u_long ahd_get_residual(struct scb *scb)
359{
360	return (scb->xs->resid);
361}
362
363static __inline
364int ahd_perform_autosense(struct scb *scb)
365{
366	/* Return true for OpenBSD */
367	return (1);
368}
369
370static __inline uint32_t
371ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
372{
373	return (sizeof(struct scsi_sense_data));
374}
375
376static __inline void
377ahd_freeze_simq(struct ahd_softc *ahd)
378{
379        /* do nothing for now */
380}
381
382static __inline void
383ahd_release_simq(struct ahd_softc *ahd)
384{
385        /* do nothing for now */
386}
387
388static __inline void
389ahd_freeze_scb(struct scb *scb)
390{
391	struct scsi_xfer *xs = scb->xs;
392	int target;
393
394	target = xs->sc_link->target;
395	if (!(scb->flags & SCB_FREEZE_QUEUE)) {
396		scb->flags |= SCB_FREEZE_QUEUE;
397	}
398}
399
400static __inline void
401ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
402{
403}
404
405static __inline int
406ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
407			char channel, int lun, u_int tag,
408			role_t role, uint32_t status)
409{
410	return (0);
411}
412
413static __inline void
414ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
415{
416	int s;
417
418	ahd_lock(ahd, &s);
419
420	if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0 ||
421	    (scb->flags & SCB_RECOVERY_SCB) != 0) {
422		ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
423	}
424
425	timeout_del(&scb->xs->stimeout);
426
427	ahd_unlock(ahd, &s);
428}
429
430/********************************** PCI ***************************************/
431#if AHD_PCI_CONFIG > 0
432static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t, int, int);
433static __inline void	ahd_pci_write_config(ahd_dev_softc_t, int,
434		uint32_t, int);
435static __inline int	ahd_get_pci_function(ahd_dev_softc_t);
436static __inline int	ahd_get_pci_slot(ahd_dev_softc_t);
437static __inline int	ahd_get_pci_bus(ahd_dev_softc_t);
438
439int			ahd_pci_map_registers(struct ahd_softc *);
440int			ahd_pci_map_int(struct ahd_softc *);
441
442static __inline uint32_t
443ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
444{
445	return (pci_conf_read(pci->pa_pc, pci->pa_tag, reg));
446}
447
448static __inline void
449ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
450{
451	pci_conf_write(pci->pa_pc, pci->pa_tag, reg, value);
452}
453
454static __inline int
455ahd_get_pci_function(ahd_dev_softc_t pci)
456{
457	return (pci->pa_function);
458}
459
460static __inline int
461ahd_get_pci_slot(ahd_dev_softc_t pci)
462{
463	return (pci->pa_device);
464}
465
466
467static __inline int
468ahd_get_pci_bus(ahd_dev_softc_t pci)
469{
470	return (pci->pa_bus);
471}
472#endif
473
474typedef enum
475{
476	AHD_POWER_STATE_D0,
477	AHD_POWER_STATE_D1,
478	AHD_POWER_STATE_D2,
479	AHD_POWER_STATE_D3
480} ahd_power_state;
481
482void ahd_power_state_change(struct ahd_softc *, ahd_power_state);
483
484/******************************** VL/EISA *************************************/
485int aic7770_map_registers(struct ahd_softc *);
486int aic7770_map_int(struct ahd_softc *, int);
487
488/********************************* Debug **************************************/
489static __inline void	ahd_print_path(struct ahd_softc *, struct scb *);
490static __inline void	ahd_platform_dump_card_state(struct ahd_softc *ahd);
491
492static __inline void
493ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
494{
495	sc_print_addr(scb->xs->sc_link);
496}
497
498static __inline void
499ahd_platform_dump_card_state(struct ahd_softc *ahd)
500{
501	/* Nothing to do here for OpenBSD */
502	printf("FEATURES = 0x%x, FLAGS = 0x%x, CHIP = 0x%x BUGS =0x%x\n",
503		ahd->features, ahd->flags, ahd->chip, ahd->bugs);
504}
505/**************************** Transfer Settings *******************************/
506void	  ahd_notify_xfer_settings_change(struct ahd_softc *,
507					  struct ahd_devinfo *);
508void	  ahd_platform_set_tags(struct ahd_softc *, struct ahd_devinfo *,
509				ahd_queue_alg);
510
511/************************* Initialization/Teardown ****************************/
512int	  ahd_platform_alloc(struct ahd_softc *, void *);
513void	  ahd_platform_free(struct ahd_softc *);
514int	  ahd_attach(struct ahd_softc *);
515int	  ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd);
516int	  ahd_detach(struct device *, int);
517#define	ahd_platform_init
518
519/****************************** Interrupts ************************************/
520int			ahd_platform_intr(void *);
521static __inline void	ahd_platform_flushwork(struct ahd_softc *ahd);
522static __inline void
523ahd_platform_flushwork(struct ahd_softc *ahd)
524{
525}
526
527/************************ Misc Function Declarations **************************/
528void	  ahd_done(struct ahd_softc *, struct scb *);
529void	  ahd_send_async(struct ahd_softc *, char /*channel*/,
530			 u_int /*target*/, u_int /*lun*/, ac_code, void *arg);
531/************************ SCSI task management **************************/
532#define SIU_TASKMGMT_NONE               0x00
533#define SIU_TASKMGMT_ABORT_TASK         0x01
534#define SIU_TASKMGMT_ABORT_TASK_SET     0x02
535#define SIU_TASKMGMT_CLEAR_TASK_SET     0x04
536#define SIU_TASKMGMT_LUN_RESET          0x08
537#define SIU_TASKMGMT_TARGET_RESET       0x20
538#define SIU_TASKMGMT_CLEAR_ACA          0x40
539#endif  /* _AIC79XX_OPENBSD_H_ */
540