1/* $Id: os_bsd.h,v 1.20 2010/05/11 03:12:11 lcn Exp $ */
2/*-
3 * HighPoint RAID Driver for FreeBSD
4 * Copyright (C) 2005-2011 HighPoint Technologies, Inc. All Rights Reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31#include <dev/hptnr/hptnr_config.h>
32
33#ifndef _OS_BSD_H
34#define _OS_BSD_H
35
36#ifndef DBG
37#define  DBG	0
38#endif
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/cons.h>
43#if (__FreeBSD_version >= 500000)
44#include <sys/time.h>
45#include <sys/systm.h>
46#else
47#include <machine/clock.h>	/*to support DELAY function under 4.x BSD versions*/
48#endif
49
50#include <sys/stat.h>
51#include <sys/malloc.h>
52#include <sys/conf.h>
53#include <sys/libkern.h>
54#include <sys/kernel.h>
55
56#if (__FreeBSD_version >= 500000)
57#include <sys/kthread.h>
58#include <sys/mutex.h>
59#include <sys/module.h>
60#endif
61
62#include <sys/eventhandler.h>
63#include <sys/bus.h>
64#include <sys/taskqueue.h>
65#include <sys/ioccom.h>
66
67#include <machine/resource.h>
68#if __FreeBSD_version >= 500043
69#include <machine/pci_cfgreg.h>
70#endif
71#include <machine/bus.h>
72#include <machine/stdarg.h>
73#include <sys/rman.h>
74
75#include <vm/vm.h>
76#include <vm/pmap.h>
77
78#if (__FreeBSD_version >= 500000)
79#include <dev/pci/pcireg.h>
80#include <dev/pci/pcivar.h>
81#else
82#include <pci/pcivar.h>
83#include <pci/pcireg.h>
84#endif
85
86#if (__FreeBSD_version <= 500043)
87#include <sys/devicestat.h>
88#endif
89
90#include <cam/cam.h>
91#include <cam/cam_ccb.h>
92#include <cam/cam_sim.h>
93#include <cam/cam_xpt_sim.h>
94#include <cam/cam_debug.h>
95#include <cam/cam_xpt_periph.h>
96#include <cam/cam_periph.h>
97#include <cam/scsi/scsi_all.h>
98#include <cam/scsi/scsi_message.h>
99
100#if (__FreeBSD_version < 500043)
101#include <sys/bus_private.h>
102#endif
103
104
105typedef struct _INQUIRYDATA {
106	u_char DeviceType : 5;
107	u_char DeviceTypeQualifier : 3;
108	u_char DeviceTypeModifier : 7;
109	u_char RemovableMedia : 1;
110	u_char Versions;
111	u_char ResponseDataFormat;
112	u_char AdditionalLength;
113	u_char Reserved[2];
114	u_char SoftReset : 1;
115	u_char CommandQueue : 1;
116	u_char Reserved2 : 1;
117	u_char LinkedCommands : 1;
118	u_char Synchronous : 1;
119	u_char Wide16Bit : 1;
120	u_char Wide32Bit : 1;
121	u_char RelativeAddressing : 1;
122	u_char VendorId[8];
123	u_char ProductId[16];
124	u_char ProductRevisionLevel[4];
125	u_char VendorSpecific[20];
126	u_char Reserved3[40];
127}
128__attribute__((packed))
129INQUIRYDATA, *PINQUIRYDATA;
130
131#endif
132
133/* private headers */
134
135#include <dev/hptnr/osm.h>
136#include <dev/hptnr/him.h>
137#include <dev/hptnr/ldm.h>
138
139/* driver parameters */
140extern const char driver_name[];
141extern const char driver_name_long[];
142extern const char driver_ver[];
143extern int  osm_max_targets;
144
145/*
146 * adapter/vbus extensions:
147 * each physical controller has an adapter_ext, passed to him.create_adapter()
148 * each vbus has a vbus_ext passed to ldm_create_vbus().
149 */
150#define EXT_TYPE_HBA  1
151#define EXT_TYPE_VBUS 2
152
153typedef struct _hba {
154	int               ext_type;
155	LDM_ADAPTER       ldm_adapter;
156	device_t          pcidev;
157	PCI_ADDRESS       pciaddr;
158	struct _vbus_ext *vbus_ext;
159	struct _hba      *next;
160
161	struct {
162		struct resource *res;
163		int type;
164		int rid;
165		void *base;
166	}
167	pcibar[6];
168
169	struct resource  *irq_res;
170	void             *irq_handle;
171}
172HBA, *PHBA;
173
174typedef struct _os_cmdext {
175	struct _vbus_ext  *vbus_ext;
176	struct _os_cmdext *next;
177	union ccb         *ccb;
178	bus_dmamap_t       dma_map;
179	SG                 psg[os_max_sg_descriptors];
180}
181OS_CMDEXT, *POS_CMDEXT;
182
183typedef struct _vbus_ext {
184	int               ext_type;
185	struct _vbus_ext *next;
186	PHBA              hba_list;
187	struct freelist  *freelist_head;
188	struct freelist  *freelist_dma_head;
189
190	struct cam_sim   *sim;    /* sim for this vbus */
191	struct cam_path  *path;   /* peripheral, path, tgt, lun with this vbus */
192#if (__FreeBSD_version >= 500000)
193	struct mtx        lock; /* general purpose lock */
194#else
195	int  			hpt_splx;
196#endif
197	bus_dma_tag_t     io_dmat; /* I/O buffer DMA tag */
198
199	POS_CMDEXT        cmdext_list;
200
201	OSM_TASK         *tasks;
202	struct task       worker;
203
204	struct callout_handle timer;
205
206	eventhandler_tag  shutdown_eh;
207
208	/* the LDM vbus instance continues */
209	unsigned long vbus[0] __attribute__((aligned(sizeof(unsigned long))));
210}
211VBUS_EXT, *PVBUS_EXT;
212
213#if __FreeBSD_version >= 500000
214#define hpt_lock_vbus(vbus_ext)   mtx_lock(&(vbus_ext)->lock)
215#define hpt_unlock_vbus(vbus_ext) mtx_unlock(&(vbus_ext)->lock)
216#else
217static __inline	void	hpt_lock_vbus(PVBUS_EXT	vbus_ext)
218{
219	vbus_ext->hpt_splx = splcam();
220}
221static __inline	void	hpt_unlock_vbus(PVBUS_EXT vbus_ext)
222{
223	splx(vbus_ext->hpt_splx);
224}
225#endif
226
227
228#define HPT_OSM_TIMEOUT (20*hz)  /* timeout value for OS commands */
229
230#define HPT_DO_IOCONTROL	_IOW('H', 0, HPT_IOCTL_PARAM)
231
232#define HPT_SCAN_BUS		_IO('H', 1)
233
234#if __FreeBSD_version >= 501000
235#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi_giant,(task));
236#else
237#define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi,(task));
238#endif
239
240#if __FreeBSD_version >= 500000
241static	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
242{
243	return	msleep(ident, &vbus_ext->lock, priority, wmesg, timo);
244}
245#else
246static	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
247{
248	int retval = 0;
249
250	asleep(ident, priority, wmesg, timo);
251	hpt_unlock_vbus(vbus_ext);
252	retval = await(priority, timo);
253	hpt_lock_vbus(vbus_ext);
254
255	return retval;
256}
257#endif
258
259#if __FreeBSD_version < 501000
260#define	READ_16				0x88
261#define WRITE_16			0x8a
262#define SERVICE_ACTION_IN	0x9e
263#endif
264
265#define	HPT_DEV_MAJOR	200
266
267