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