1228940Sdelphij/*-
2228940Sdelphij * Copyright (c) 2011 HighPoint Technologies, Inc.
3228940Sdelphij * All rights reserved.
4228940Sdelphij *
5228940Sdelphij * Redistribution and use in source and binary forms, with or without
6228940Sdelphij * modification, are permitted provided that the following conditions
7228940Sdelphij * are met:
8228940Sdelphij * 1. Redistributions of source code must retain the above copyright
9228940Sdelphij *    notice, this list of conditions and the following disclaimer.
10228940Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11228940Sdelphij *    notice, this list of conditions and the following disclaimer in the
12228940Sdelphij *    documentation and/or other materials provided with the distribution.
13228940Sdelphij *
14228940Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15228940Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16228940Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17228940Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18228940Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19228940Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20228940Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21228940Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22228940Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23228940Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24228940Sdelphij * SUCH DAMAGE.
25228940Sdelphij *
26228940Sdelphij * $FreeBSD$
27228940Sdelphij */
28228940Sdelphij
29228940Sdelphij#include <dev/hpt27xx/hpt27xx_config.h>
30228940Sdelphij
31228940Sdelphij#ifndef _OS_BSD_H
32228940Sdelphij#define _OS_BSD_H
33228940Sdelphij
34228940Sdelphij#ifndef DBG
35228940Sdelphij#define  DBG	0
36228940Sdelphij#endif
37228940Sdelphij
38228940Sdelphij#include <sys/param.h>
39228940Sdelphij#include <sys/types.h>
40228940Sdelphij#include <sys/cons.h>
41228940Sdelphij#if (__FreeBSD_version >= 500000)
42228940Sdelphij#include <sys/time.h>
43228940Sdelphij#include <sys/systm.h>
44228940Sdelphij#else
45228940Sdelphij#include <machine/clock.h>	/*to support DELAY function under 4.x BSD versions*/
46228940Sdelphij#endif
47228940Sdelphij
48228940Sdelphij#include <sys/stat.h>
49228940Sdelphij#include <sys/malloc.h>
50228940Sdelphij#include <sys/conf.h>
51228940Sdelphij#include <sys/libkern.h>
52228940Sdelphij#include <sys/kernel.h>
53228940Sdelphij
54228940Sdelphij#if (__FreeBSD_version >= 500000)
55228940Sdelphij#include <sys/kthread.h>
56228940Sdelphij#include <sys/mutex.h>
57228940Sdelphij#include <sys/module.h>
58228940Sdelphij#endif
59228940Sdelphij
60228940Sdelphij#include <sys/eventhandler.h>
61228940Sdelphij#include <sys/bus.h>
62228940Sdelphij#include <sys/taskqueue.h>
63228940Sdelphij#include <sys/ioccom.h>
64228940Sdelphij
65228940Sdelphij#include <machine/resource.h>
66228940Sdelphij#if __FreeBSD_version >= 500043
67228940Sdelphij#include <machine/pci_cfgreg.h>
68228940Sdelphij#endif
69228940Sdelphij#include <machine/bus.h>
70228940Sdelphij#include <machine/stdarg.h>
71228940Sdelphij#include <sys/rman.h>
72228940Sdelphij
73228940Sdelphij#include <vm/vm.h>
74228940Sdelphij#include <vm/pmap.h>
75228940Sdelphij
76228940Sdelphij#if (__FreeBSD_version >= 500000)
77228940Sdelphij#include <dev/pci/pcireg.h>
78228940Sdelphij#include <dev/pci/pcivar.h>
79228940Sdelphij#else
80228940Sdelphij#include <pci/pcivar.h>
81228940Sdelphij#include <pci/pcireg.h>
82228940Sdelphij#endif
83228940Sdelphij
84228940Sdelphij#if (__FreeBSD_version <= 500043)
85228940Sdelphij#include <sys/devicestat.h>
86228940Sdelphij#endif
87228940Sdelphij
88228940Sdelphij#include <cam/cam.h>
89228940Sdelphij#include <cam/cam_ccb.h>
90228940Sdelphij#include <cam/cam_sim.h>
91228940Sdelphij#include <cam/cam_xpt_sim.h>
92228940Sdelphij#include <cam/cam_debug.h>
93228940Sdelphij#include <cam/cam_xpt_periph.h>
94228940Sdelphij#include <cam/cam_periph.h>
95228940Sdelphij#include <cam/scsi/scsi_all.h>
96228940Sdelphij#include <cam/scsi/scsi_message.h>
97228940Sdelphij
98228940Sdelphij#if (__FreeBSD_version < 500043)
99228940Sdelphij#include <sys/bus_private.h>
100228940Sdelphij#endif
101228940Sdelphij
102228940Sdelphij
103228940Sdelphijtypedef struct _INQUIRYDATA {
104228940Sdelphij	u_char DeviceType : 5;
105228940Sdelphij	u_char DeviceTypeQualifier : 3;
106228940Sdelphij	u_char DeviceTypeModifier : 7;
107228940Sdelphij	u_char RemovableMedia : 1;
108228940Sdelphij	u_char Versions;
109228940Sdelphij	u_char ResponseDataFormat;
110228940Sdelphij	u_char AdditionalLength;
111228940Sdelphij	u_char Reserved[2];
112228940Sdelphij	u_char SoftReset : 1;
113228940Sdelphij	u_char CommandQueue : 1;
114228940Sdelphij	u_char Reserved2 : 1;
115228940Sdelphij	u_char LinkedCommands : 1;
116228940Sdelphij	u_char Synchronous : 1;
117228940Sdelphij	u_char Wide16Bit : 1;
118228940Sdelphij	u_char Wide32Bit : 1;
119228940Sdelphij	u_char RelativeAddressing : 1;
120228940Sdelphij	u_char VendorId[8];
121228940Sdelphij	u_char ProductId[16];
122228940Sdelphij	u_char ProductRevisionLevel[4];
123228940Sdelphij	u_char VendorSpecific[20];
124228940Sdelphij	u_char Reserved3[40];
125228940Sdelphij}
126228940Sdelphij__attribute__((packed))
127228940SdelphijINQUIRYDATA, *PINQUIRYDATA;
128228940Sdelphij
129228940Sdelphij#endif
130228940Sdelphij
131228940Sdelphij/* private headers */
132228940Sdelphij
133228940Sdelphij#include <dev/hpt27xx/osm.h>
134228940Sdelphij#include <dev/hpt27xx/him.h>
135228940Sdelphij#include <dev/hpt27xx/ldm.h>
136228940Sdelphij
137228940Sdelphij/* driver parameters */
138245938Sdelphijextern const char driver_name[];
139245938Sdelphijextern const char driver_name_long[];
140245938Sdelphijextern const char driver_ver[];
141228940Sdelphijextern int  osm_max_targets;
142228940Sdelphij
143228940Sdelphij/*
144228940Sdelphij * adapter/vbus extensions:
145228940Sdelphij * each physical controller has an adapter_ext, passed to him.create_adapter()
146228940Sdelphij * each vbus has a vbus_ext passed to ldm_create_vbus().
147228940Sdelphij */
148228940Sdelphij#define EXT_TYPE_HBA  1
149228940Sdelphij#define EXT_TYPE_VBUS 2
150228940Sdelphij
151228940Sdelphijtypedef struct _hba {
152228940Sdelphij	int               ext_type;
153228940Sdelphij	LDM_ADAPTER       ldm_adapter;
154228940Sdelphij	device_t          pcidev;
155228940Sdelphij	PCI_ADDRESS       pciaddr;
156228940Sdelphij	struct _vbus_ext *vbus_ext;
157228940Sdelphij	struct _hba      *next;
158228940Sdelphij
159228940Sdelphij	struct {
160228940Sdelphij		struct resource *res;
161228940Sdelphij		int type;
162228940Sdelphij		int rid;
163228940Sdelphij		void *base;
164228940Sdelphij	}
165228940Sdelphij	pcibar[6];
166228940Sdelphij
167228940Sdelphij	struct resource  *irq_res;
168228940Sdelphij	void             *irq_handle;
169228940Sdelphij}
170228940SdelphijHBA, *PHBA;
171228940Sdelphij
172228940Sdelphijtypedef struct _os_cmdext {
173228940Sdelphij	struct _vbus_ext  *vbus_ext;
174228940Sdelphij	struct _os_cmdext *next;
175228940Sdelphij	union ccb         *ccb;
176228940Sdelphij	bus_dmamap_t       dma_map;
177228940Sdelphij	SG                 psg[os_max_sg_descriptors];
178228940Sdelphij}
179228940SdelphijOS_CMDEXT, *POS_CMDEXT;
180228940Sdelphij
181228940Sdelphijtypedef struct _vbus_ext {
182228940Sdelphij	int               ext_type;
183228940Sdelphij	struct _vbus_ext *next;
184228940Sdelphij	PHBA              hba_list;
185228940Sdelphij	struct freelist  *freelist_head;
186228940Sdelphij	struct freelist  *freelist_dma_head;
187228940Sdelphij
188228940Sdelphij	struct cam_sim   *sim;    /* sim for this vbus */
189228940Sdelphij	struct cam_path  *path;   /* peripheral, path, tgt, lun with this vbus */
190228940Sdelphij#if (__FreeBSD_version >= 500000)
191228940Sdelphij	struct mtx        lock; /* general purpose lock */
192228940Sdelphij#else
193228940Sdelphij	int  			hpt_splx;
194228940Sdelphij#endif
195228940Sdelphij	bus_dma_tag_t     io_dmat; /* I/O buffer DMA tag */
196228940Sdelphij
197228940Sdelphij	POS_CMDEXT        cmdext_list;
198228940Sdelphij
199228940Sdelphij	OSM_TASK         *tasks;
200228940Sdelphij	struct task       worker;
201228940Sdelphij
202228940Sdelphij	struct callout_handle timer;
203228940Sdelphij
204228940Sdelphij	eventhandler_tag  shutdown_eh;
205228940Sdelphij
206228940Sdelphij	/* the LDM vbus instance continues */
207228940Sdelphij	unsigned long vbus[0] __attribute__((aligned(sizeof(unsigned long))));
208228940Sdelphij}
209228940SdelphijVBUS_EXT, *PVBUS_EXT;
210228940Sdelphij
211228940Sdelphij#if __FreeBSD_version >= 500000
212228940Sdelphij#define hpt_lock_vbus(vbus_ext)   mtx_lock(&(vbus_ext)->lock)
213228940Sdelphij#define hpt_unlock_vbus(vbus_ext) mtx_unlock(&(vbus_ext)->lock)
214228940Sdelphij#else
215228940Sdelphijstatic __inline	void	hpt_lock_vbus(PVBUS_EXT	vbus_ext)
216228940Sdelphij{
217228940Sdelphij	vbus_ext->hpt_splx = splcam();
218228940Sdelphij}
219228940Sdelphijstatic __inline	void	hpt_unlock_vbus(PVBUS_EXT vbus_ext)
220228940Sdelphij{
221228940Sdelphij	splx(vbus_ext->hpt_splx);
222228940Sdelphij}
223228940Sdelphij#endif
224228940Sdelphij
225228940Sdelphij
226228940Sdelphij#define HPT_OSM_TIMEOUT (20*hz)  /* timeout value for OS commands */
227228940Sdelphij
228228940Sdelphij#define HPT_DO_IOCONTROL	_IOW('H', 0, HPT_IOCTL_PARAM)
229228940Sdelphij
230228940Sdelphij#define HPT_SCAN_BUS		_IO('H', 1)
231228940Sdelphij
232228940Sdelphij#if __FreeBSD_version >= 501000
233228940Sdelphij#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi_giant,(task));
234228940Sdelphij#else
235228940Sdelphij#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi,(task));
236228940Sdelphij#endif
237228940Sdelphij
238228940Sdelphij#if __FreeBSD_version >= 500000
239228940Sdelphijstatic	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
240228940Sdelphij{
241228940Sdelphij	return	msleep(ident, &vbus_ext->lock, priority, wmesg, timo);
242228940Sdelphij}
243228940Sdelphij#else
244228940Sdelphijstatic	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
245228940Sdelphij{
246228940Sdelphij	int retval = 0;
247228940Sdelphij
248228940Sdelphij	asleep(ident, priority, wmesg, timo);
249228940Sdelphij	hpt_unlock_vbus(vbus_ext);
250228940Sdelphij	retval = await(priority, timo);
251228940Sdelphij	hpt_lock_vbus(vbus_ext);
252228940Sdelphij
253228940Sdelphij	return retval;
254228940Sdelphij}
255228940Sdelphij#endif
256228940Sdelphij
257228940Sdelphij#if __FreeBSD_version < 501000
258228940Sdelphij#define	READ_16				0x88
259228940Sdelphij#define WRITE_16			0x8a
260228940Sdelphij#define SERVICE_ACTION_IN	0x9e
261228940Sdelphij#endif
262228940Sdelphij
263228940Sdelphij#define	HPT_DEV_MAJOR	200
264