1174604Sscottl/*
2174604Sscottl * Copyright (c) HighPoint Technologies, Inc.
3174604Sscottl * All rights reserved.
4174604Sscottl *
5174604Sscottl * Redistribution and use in source and binary forms, with or without
6174604Sscottl * modification, are permitted provided that the following conditions
7174604Sscottl * are met:
8174604Sscottl * 1. Redistributions of source code must retain the above copyright
9174604Sscottl *    notice, this list of conditions and the following disclaimer.
10174604Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11174604Sscottl *    notice, this list of conditions and the following disclaimer in the
12174604Sscottl *    documentation and/or other materials provided with the distribution.
13174604Sscottl *
14174604Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15174604Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16174604Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17174604Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18174604Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19174604Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20174604Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21174604Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22174604Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23174604Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24174604Sscottl * SUCH DAMAGE.
25174604Sscottl *
26174604Sscottl * $FreeBSD$
27174604Sscottl */
28174604Sscottl#include <dev/hptrr/hptrr_config.h>
29174604Sscottl/* $Id: os_bsd.h,v 1.18 2006/04/11 08:19:02 gmm Exp $
30174604Sscottl *
31174604Sscottl * HighPoint RAID Driver for FreeBSD
32174604Sscottl * Copyright (C) 2005 HighPoint Technologies, Inc. All Rights Reserved.
33174604Sscottl */
34174604Sscottl
35174604Sscottl#ifndef _OS_BSD_H
36174604Sscottl#define _OS_BSD_H
37174604Sscottl
38176018Sscottl#ifndef DBG
39176018Sscottl#define  DBG	0
40176018Sscottl#endif
41176018Sscottl
42174604Sscottl#include <sys/param.h>
43174604Sscottl#include <sys/types.h>
44174604Sscottl#include <sys/cons.h>
45174604Sscottl#if (__FreeBSD_version >= 500000)
46174604Sscottl#include <sys/time.h>
47174604Sscottl#include <sys/systm.h>
48174604Sscottl#else
49174604Sscottl#include <machine/clock.h>	/*to support DELAY function under 4.x BSD versions*/
50174604Sscottl#endif
51174604Sscottl
52174604Sscottl#include <sys/stat.h>
53174604Sscottl#include <sys/malloc.h>
54174604Sscottl#include <sys/conf.h>
55174604Sscottl#include <sys/libkern.h>
56174604Sscottl#include <sys/kernel.h>
57174604Sscottl
58174604Sscottl#if (__FreeBSD_version >= 500000)
59174604Sscottl#include <sys/kthread.h>
60174604Sscottl#include <sys/mutex.h>
61174604Sscottl#include <sys/module.h>
62174604Sscottl#endif
63174604Sscottl
64174604Sscottl#include <sys/eventhandler.h>
65174604Sscottl#include <sys/bus.h>
66174604Sscottl#include <sys/taskqueue.h>
67174604Sscottl#include <sys/ioccom.h>
68174604Sscottl
69174604Sscottl#include <machine/resource.h>
70174604Sscottl#include <machine/bus.h>
71174604Sscottl#include <machine/stdarg.h>
72174604Sscottl#include <sys/rman.h>
73174604Sscottl
74174604Sscottl#include <vm/vm.h>
75174604Sscottl#include <vm/pmap.h>
76174604Sscottl
77174604Sscottl#if (__FreeBSD_version >= 500000)
78174604Sscottl#include <dev/pci/pcireg.h>
79174604Sscottl#include <dev/pci/pcivar.h>
80174604Sscottl#else
81174604Sscottl#include <pci/pcivar.h>
82174604Sscottl#include <pci/pcireg.h>
83174604Sscottl#endif
84174604Sscottl
85174604Sscottl#if (__FreeBSD_version <= 500043)
86174604Sscottl#include <sys/devicestat.h>
87174604Sscottl#endif
88174604Sscottl
89174604Sscottl#include <cam/cam.h>
90174604Sscottl#include <cam/cam_ccb.h>
91174604Sscottl#include <cam/cam_sim.h>
92174604Sscottl#include <cam/cam_xpt_sim.h>
93174604Sscottl#include <cam/cam_debug.h>
94174604Sscottl#include <cam/cam_periph.h>
95174604Sscottl#include <cam/scsi/scsi_all.h>
96174604Sscottl#include <cam/scsi/scsi_message.h>
97174604Sscottl
98174604Sscottl#if (__FreeBSD_version < 500043)
99174604Sscottl#include <sys/bus_private.h>
100174604Sscottl#endif
101174604Sscottl
102174604Sscottl
103174604Sscottltypedef struct _INQUIRYDATA {
104174604Sscottl	u_char DeviceType : 5;
105174604Sscottl	u_char DeviceTypeQualifier : 3;
106174604Sscottl	u_char DeviceTypeModifier : 7;
107174604Sscottl	u_char RemovableMedia : 1;
108174604Sscottl	u_char Versions;
109174604Sscottl	u_char ResponseDataFormat;
110174604Sscottl	u_char AdditionalLength;
111174604Sscottl	u_char Reserved[2];
112174604Sscottl	u_char SoftReset : 1;
113174604Sscottl	u_char CommandQueue : 1;
114174604Sscottl	u_char Reserved2 : 1;
115174604Sscottl	u_char LinkedCommands : 1;
116174604Sscottl	u_char Synchronous : 1;
117174604Sscottl	u_char Wide16Bit : 1;
118174604Sscottl	u_char Wide32Bit : 1;
119174604Sscottl	u_char RelativeAddressing : 1;
120174604Sscottl	u_char VendorId[8];
121174604Sscottl	u_char ProductId[16];
122174604Sscottl	u_char ProductRevisionLevel[4];
123174604Sscottl	u_char VendorSpecific[20];
124174604Sscottl	u_char Reserved3[40];
125174604Sscottl}
126174604Sscottl__attribute__((packed))
127174604SscottlINQUIRYDATA, *PINQUIRYDATA;
128174604Sscottl
129174604Sscottl#endif
130174604Sscottl
131174604Sscottl/* private headers */
132174604Sscottl
133174604Sscottl#include <dev/hptrr/osm.h>
134174604Sscottl#include <dev/hptrr/him.h>
135174604Sscottl#include <dev/hptrr/ldm.h>
136174604Sscottl
137174604Sscottl/* driver parameters */
138174604Sscottlextern char driver_name[];
139174604Sscottlextern char driver_name_long[];
140174604Sscottlextern char driver_ver[];
141174604Sscottlextern int  osm_max_targets;
142174604Sscottl
143174604Sscottl/*
144174604Sscottl * adapter/vbus extensions:
145174604Sscottl * each physical controller has an adapter_ext, passed to him.create_adapter()
146174604Sscottl * each vbus has a vbus_ext passed to ldm_create_vbus().
147174604Sscottl */
148174604Sscottl#define EXT_TYPE_HBA  1
149174604Sscottl#define EXT_TYPE_VBUS 2
150174604Sscottl
151174604Sscottltypedef struct _hba {
152174604Sscottl	int               ext_type;
153174604Sscottl	LDM_ADAPTER       ldm_adapter;
154174604Sscottl	device_t          pcidev;
155174604Sscottl	PCI_ADDRESS       pciaddr;
156174604Sscottl	struct _vbus_ext *vbus_ext;
157174604Sscottl	struct _hba      *next;
158174604Sscottl
159174604Sscottl	struct {
160174604Sscottl		struct resource *res;
161174604Sscottl		int type;
162174604Sscottl		int rid;
163174604Sscottl		void *base;
164174604Sscottl	}
165174604Sscottl	pcibar[6];
166174604Sscottl
167174604Sscottl	struct resource  *irq_res;
168174604Sscottl	void             *irq_handle;
169174604Sscottl}
170174604SscottlHBA, *PHBA;
171174604Sscottl
172174604Sscottltypedef struct _os_cmdext {
173174604Sscottl	struct _vbus_ext  *vbus_ext;
174174604Sscottl	struct _os_cmdext *next;
175174604Sscottl	union ccb         *ccb;
176174604Sscottl	bus_dmamap_t       dma_map;
177174604Sscottl	SG                 psg[os_max_sg_descriptors];
178174604Sscottl}
179174604SscottlOS_CMDEXT, *POS_CMDEXT;
180174604Sscottl
181174604Sscottltypedef struct _vbus_ext {
182174604Sscottl	int               ext_type;
183174604Sscottl	struct _vbus_ext *next;
184174604Sscottl	PHBA              hba_list;
185174604Sscottl	struct freelist  *freelist_head;
186174604Sscottl	struct freelist  *freelist_dma_head;
187174604Sscottl
188174604Sscottl	struct cam_sim   *sim;    /* sim for this vbus */
189174604Sscottl	struct cam_path  *path;   /* peripheral, path, tgt, lun with this vbus */
190174604Sscottl#if (__FreeBSD_version >= 500000)
191174604Sscottl	struct mtx        lock; /* general purpose lock */
192174604Sscottl#else
193174604Sscottl	int  			hpt_splx;
194174604Sscottl#endif
195174604Sscottl	bus_dma_tag_t     io_dmat; /* I/O buffer DMA tag */
196174604Sscottl
197174604Sscottl	POS_CMDEXT        cmdext_list;
198174604Sscottl
199174604Sscottl	OSM_TASK         *tasks;
200174604Sscottl	struct task       worker;
201174604Sscottl
202174604Sscottl	struct callout_handle timer;
203174604Sscottl
204174604Sscottl	eventhandler_tag  shutdown_eh;
205174604Sscottl
206174604Sscottl	/* the LDM vbus instance continues */
207174604Sscottl	unsigned long vbus[0] __attribute__((aligned(sizeof(unsigned long))));
208174604Sscottl}
209174604SscottlVBUS_EXT, *PVBUS_EXT;
210174604Sscottl
211174604Sscottl#if __FreeBSD_version >= 500000
212174604Sscottl#define hpt_lock_vbus(vbus_ext)   mtx_lock(&(vbus_ext)->lock)
213174604Sscottl#define hpt_unlock_vbus(vbus_ext) mtx_unlock(&(vbus_ext)->lock)
214174604Sscottl#else
215174604Sscottlstatic __inline	void	hpt_lock_vbus(PVBUS_EXT	vbus_ext)
216174604Sscottl{
217174604Sscottl	vbus_ext->hpt_splx = splcam();
218174604Sscottl}
219174604Sscottlstatic __inline	void	hpt_unlock_vbus(PVBUS_EXT vbus_ext)
220174604Sscottl{
221174604Sscottl	splx(vbus_ext->hpt_splx);
222174604Sscottl}
223174604Sscottl#endif
224174604Sscottl
225174604Sscottl
226174604Sscottl#define HPT_OSM_TIMEOUT (20*hz)  /* timeout value for OS commands */
227174604Sscottl
228174604Sscottl#define HPT_DO_IOCONTROL	_IOW('H', 0, HPT_IOCTL_PARAM)
229174604Sscottl
230174604Sscottl#define HPT_SCAN_BUS		_IO('H', 1)
231174604Sscottl
232174604Sscottl#if __FreeBSD_version >= 501000
233174604Sscottl#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi_giant,(task));
234174604Sscottl#else
235174604Sscottl#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi,(task));
236174604Sscottl#endif
237174604Sscottl
238174604Sscottl#if __FreeBSD_version >= 500000
239174604Sscottlstatic	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
240174604Sscottl{
241174604Sscottl	return	msleep(ident, &vbus_ext->lock, priority, wmesg, timo);
242174604Sscottl}
243174604Sscottl#else
244174604Sscottlstatic	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
245174604Sscottl{
246174604Sscottl	int retval = 0;
247174604Sscottl
248174604Sscottl	asleep(ident, priority, wmesg, timo);
249174604Sscottl	hpt_unlock_vbus(vbus_ext);
250174604Sscottl	retval = await(priority, timo);
251174604Sscottl	hpt_lock_vbus(vbus_ext);
252174604Sscottl
253174604Sscottl	return retval;
254174604Sscottl}
255174604Sscottl#endif
256174604Sscottl
257174604Sscottl#if __FreeBSD_version < 501000
258174604Sscottl#define	READ_16				0x88
259174604Sscottl#define WRITE_16			0x8a
260174604Sscottl#define SERVICE_ACTION_IN	0x9e
261174604Sscottl#endif
262174604Sscottl
263174604Sscottl#define	HPT_DEV_MAJOR	200
264