1252867Sdelphij/* $Id: os_bsd.h,v 1.20 2010/05/11 03:12:11 lcn Exp $ */
2252867Sdelphij/*-
3252867Sdelphij * HighPoint RAID Driver for FreeBSD
4252867Sdelphij * Copyright (C) 2005-2011 HighPoint Technologies, Inc. All Rights Reserved.
5252867Sdelphij * All rights reserved.
6252867Sdelphij *
7252867Sdelphij * Redistribution and use in source and binary forms, with or without
8252867Sdelphij * modification, are permitted provided that the following conditions
9252867Sdelphij * are met:
10252867Sdelphij * 1. Redistributions of source code must retain the above copyright
11252867Sdelphij *    notice, this list of conditions and the following disclaimer.
12252867Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
13252867Sdelphij *    notice, this list of conditions and the following disclaimer in the
14252867Sdelphij *    documentation and/or other materials provided with the distribution.
15252867Sdelphij *
16252867Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17252867Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18252867Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19252867Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20252867Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21252867Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22252867Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23252867Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24252867Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25252867Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26252867Sdelphij * SUCH DAMAGE.
27252867Sdelphij *
28252867Sdelphij * $FreeBSD$
29252867Sdelphij */
30252867Sdelphij
31252867Sdelphij#include <dev/hptnr/hptnr_config.h>
32252867Sdelphij
33252867Sdelphij#ifndef _OS_BSD_H
34252867Sdelphij#define _OS_BSD_H
35252867Sdelphij
36252867Sdelphij#ifndef DBG
37252867Sdelphij#define  DBG	0
38252867Sdelphij#endif
39252867Sdelphij
40252867Sdelphij#include <sys/param.h>
41252867Sdelphij#include <sys/types.h>
42252867Sdelphij#include <sys/cons.h>
43252867Sdelphij#if (__FreeBSD_version >= 500000)
44252867Sdelphij#include <sys/time.h>
45252867Sdelphij#include <sys/systm.h>
46252867Sdelphij#else
47252867Sdelphij#include <machine/clock.h>	/*to support DELAY function under 4.x BSD versions*/
48252867Sdelphij#endif
49252867Sdelphij
50252867Sdelphij#include <sys/stat.h>
51252867Sdelphij#include <sys/malloc.h>
52252867Sdelphij#include <sys/conf.h>
53252867Sdelphij#include <sys/libkern.h>
54252867Sdelphij#include <sys/kernel.h>
55252867Sdelphij
56252867Sdelphij#if (__FreeBSD_version >= 500000)
57252867Sdelphij#include <sys/kthread.h>
58252867Sdelphij#include <sys/mutex.h>
59252867Sdelphij#include <sys/module.h>
60252867Sdelphij#endif
61252867Sdelphij
62252867Sdelphij#include <sys/eventhandler.h>
63252867Sdelphij#include <sys/bus.h>
64252867Sdelphij#include <sys/taskqueue.h>
65252867Sdelphij#include <sys/ioccom.h>
66252867Sdelphij
67252867Sdelphij#include <machine/resource.h>
68252867Sdelphij#if __FreeBSD_version >= 500043
69252867Sdelphij#include <machine/pci_cfgreg.h>
70252867Sdelphij#endif
71252867Sdelphij#include <machine/bus.h>
72252867Sdelphij#include <machine/stdarg.h>
73252867Sdelphij#include <sys/rman.h>
74252867Sdelphij
75252867Sdelphij#include <vm/vm.h>
76252867Sdelphij#include <vm/pmap.h>
77252867Sdelphij
78252867Sdelphij#if (__FreeBSD_version >= 500000)
79252867Sdelphij#include <dev/pci/pcireg.h>
80252867Sdelphij#include <dev/pci/pcivar.h>
81252867Sdelphij#else
82252867Sdelphij#include <pci/pcivar.h>
83252867Sdelphij#include <pci/pcireg.h>
84252867Sdelphij#endif
85252867Sdelphij
86252867Sdelphij#if (__FreeBSD_version <= 500043)
87252867Sdelphij#include <sys/devicestat.h>
88252867Sdelphij#endif
89252867Sdelphij
90252867Sdelphij#include <cam/cam.h>
91252867Sdelphij#include <cam/cam_ccb.h>
92252867Sdelphij#include <cam/cam_sim.h>
93252867Sdelphij#include <cam/cam_xpt_sim.h>
94252867Sdelphij#include <cam/cam_debug.h>
95252867Sdelphij#include <cam/cam_xpt_periph.h>
96252867Sdelphij#include <cam/cam_periph.h>
97252867Sdelphij#include <cam/scsi/scsi_all.h>
98252867Sdelphij#include <cam/scsi/scsi_message.h>
99252867Sdelphij
100252867Sdelphij#if (__FreeBSD_version < 500043)
101252867Sdelphij#include <sys/bus_private.h>
102252867Sdelphij#endif
103252867Sdelphij
104252867Sdelphij
105252867Sdelphijtypedef struct _INQUIRYDATA {
106252867Sdelphij	u_char DeviceType : 5;
107252867Sdelphij	u_char DeviceTypeQualifier : 3;
108252867Sdelphij	u_char DeviceTypeModifier : 7;
109252867Sdelphij	u_char RemovableMedia : 1;
110252867Sdelphij	u_char Versions;
111252867Sdelphij	u_char ResponseDataFormat;
112252867Sdelphij	u_char AdditionalLength;
113252867Sdelphij	u_char Reserved[2];
114252867Sdelphij	u_char SoftReset : 1;
115252867Sdelphij	u_char CommandQueue : 1;
116252867Sdelphij	u_char Reserved2 : 1;
117252867Sdelphij	u_char LinkedCommands : 1;
118252867Sdelphij	u_char Synchronous : 1;
119252867Sdelphij	u_char Wide16Bit : 1;
120252867Sdelphij	u_char Wide32Bit : 1;
121252867Sdelphij	u_char RelativeAddressing : 1;
122252867Sdelphij	u_char VendorId[8];
123252867Sdelphij	u_char ProductId[16];
124252867Sdelphij	u_char ProductRevisionLevel[4];
125252867Sdelphij	u_char VendorSpecific[20];
126252867Sdelphij	u_char Reserved3[40];
127252867Sdelphij}
128252867Sdelphij__attribute__((packed))
129252867SdelphijINQUIRYDATA, *PINQUIRYDATA;
130252867Sdelphij
131252867Sdelphij#endif
132252867Sdelphij
133252867Sdelphij/* private headers */
134252867Sdelphij
135252867Sdelphij#include <dev/hptnr/osm.h>
136252867Sdelphij#include <dev/hptnr/him.h>
137252867Sdelphij#include <dev/hptnr/ldm.h>
138252867Sdelphij
139252867Sdelphij/* driver parameters */
140252867Sdelphijextern const char driver_name[];
141252867Sdelphijextern const char driver_name_long[];
142252867Sdelphijextern const char driver_ver[];
143252867Sdelphijextern int  osm_max_targets;
144252867Sdelphij
145252867Sdelphij/*
146252867Sdelphij * adapter/vbus extensions:
147252867Sdelphij * each physical controller has an adapter_ext, passed to him.create_adapter()
148252867Sdelphij * each vbus has a vbus_ext passed to ldm_create_vbus().
149252867Sdelphij */
150252867Sdelphij#define EXT_TYPE_HBA  1
151252867Sdelphij#define EXT_TYPE_VBUS 2
152252867Sdelphij
153252867Sdelphijtypedef struct _hba {
154252867Sdelphij	int               ext_type;
155252867Sdelphij	LDM_ADAPTER       ldm_adapter;
156252867Sdelphij	device_t          pcidev;
157252867Sdelphij	PCI_ADDRESS       pciaddr;
158252867Sdelphij	struct _vbus_ext *vbus_ext;
159252867Sdelphij	struct _hba      *next;
160252867Sdelphij
161252867Sdelphij	struct {
162252867Sdelphij		struct resource *res;
163252867Sdelphij		int type;
164252867Sdelphij		int rid;
165252867Sdelphij		void *base;
166252867Sdelphij	}
167252867Sdelphij	pcibar[6];
168252867Sdelphij
169252867Sdelphij	struct resource  *irq_res;
170252867Sdelphij	void             *irq_handle;
171252867Sdelphij}
172252867SdelphijHBA, *PHBA;
173252867Sdelphij
174252867Sdelphijtypedef struct _os_cmdext {
175252867Sdelphij	struct _vbus_ext  *vbus_ext;
176252867Sdelphij	struct _os_cmdext *next;
177252867Sdelphij	union ccb         *ccb;
178252867Sdelphij	bus_dmamap_t       dma_map;
179252867Sdelphij	SG                 psg[os_max_sg_descriptors];
180252867Sdelphij}
181252867SdelphijOS_CMDEXT, *POS_CMDEXT;
182252867Sdelphij
183252867Sdelphijtypedef struct _vbus_ext {
184252867Sdelphij	int               ext_type;
185252867Sdelphij	struct _vbus_ext *next;
186252867Sdelphij	PHBA              hba_list;
187252867Sdelphij	struct freelist  *freelist_head;
188252867Sdelphij	struct freelist  *freelist_dma_head;
189252867Sdelphij
190252867Sdelphij	struct cam_sim   *sim;    /* sim for this vbus */
191252867Sdelphij	struct cam_path  *path;   /* peripheral, path, tgt, lun with this vbus */
192252867Sdelphij#if (__FreeBSD_version >= 500000)
193252867Sdelphij	struct mtx        lock; /* general purpose lock */
194252867Sdelphij#else
195252867Sdelphij	int  			hpt_splx;
196252867Sdelphij#endif
197252867Sdelphij	bus_dma_tag_t     io_dmat; /* I/O buffer DMA tag */
198252867Sdelphij
199252867Sdelphij	POS_CMDEXT        cmdext_list;
200252867Sdelphij
201252867Sdelphij	OSM_TASK         *tasks;
202252867Sdelphij	struct task       worker;
203252867Sdelphij
204252867Sdelphij	struct callout_handle timer;
205252867Sdelphij
206252867Sdelphij	eventhandler_tag  shutdown_eh;
207252867Sdelphij
208252867Sdelphij	/* the LDM vbus instance continues */
209252867Sdelphij	unsigned long vbus[0] __attribute__((aligned(sizeof(unsigned long))));
210252867Sdelphij}
211252867SdelphijVBUS_EXT, *PVBUS_EXT;
212252867Sdelphij
213252867Sdelphij#if __FreeBSD_version >= 500000
214252867Sdelphij#define hpt_lock_vbus(vbus_ext)   mtx_lock(&(vbus_ext)->lock)
215252867Sdelphij#define hpt_unlock_vbus(vbus_ext) mtx_unlock(&(vbus_ext)->lock)
216252867Sdelphij#else
217252867Sdelphijstatic __inline	void	hpt_lock_vbus(PVBUS_EXT	vbus_ext)
218252867Sdelphij{
219252867Sdelphij	vbus_ext->hpt_splx = splcam();
220252867Sdelphij}
221252867Sdelphijstatic __inline	void	hpt_unlock_vbus(PVBUS_EXT vbus_ext)
222252867Sdelphij{
223252867Sdelphij	splx(vbus_ext->hpt_splx);
224252867Sdelphij}
225252867Sdelphij#endif
226252867Sdelphij
227252867Sdelphij
228252867Sdelphij#define HPT_OSM_TIMEOUT (20*hz)  /* timeout value for OS commands */
229252867Sdelphij
230252867Sdelphij#define HPT_DO_IOCONTROL	_IOW('H', 0, HPT_IOCTL_PARAM)
231252867Sdelphij
232252867Sdelphij#define HPT_SCAN_BUS		_IO('H', 1)
233252867Sdelphij
234252867Sdelphij#if __FreeBSD_version >= 501000
235252867Sdelphij#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi_giant,(task));
236252867Sdelphij#else
237252867Sdelphij#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi,(task));
238252867Sdelphij#endif
239252867Sdelphij
240252867Sdelphij#if __FreeBSD_version >= 500000
241252867Sdelphijstatic	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
242252867Sdelphij{
243252867Sdelphij	return	msleep(ident, &vbus_ext->lock, priority, wmesg, timo);
244252867Sdelphij}
245252867Sdelphij#else
246252867Sdelphijstatic	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
247252867Sdelphij{
248252867Sdelphij	int retval = 0;
249252867Sdelphij
250252867Sdelphij	asleep(ident, priority, wmesg, timo);
251252867Sdelphij	hpt_unlock_vbus(vbus_ext);
252252867Sdelphij	retval = await(priority, timo);
253252867Sdelphij	hpt_lock_vbus(vbus_ext);
254252867Sdelphij
255252867Sdelphij	return retval;
256252867Sdelphij}
257252867Sdelphij#endif
258252867Sdelphij
259252867Sdelphij#if __FreeBSD_version < 501000
260252867Sdelphij#define	READ_16				0x88
261252867Sdelphij#define WRITE_16			0x8a
262252867Sdelphij#define SERVICE_ACTION_IN	0x9e
263252867Sdelphij#endif
264252867Sdelphij
265252867Sdelphij#define	HPT_DEV_MAJOR	200
266252867Sdelphij
267