xref: /freebsd/sys/dev/hpt27xx/os_bsd.h (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2011 HighPoint Technologies, Inc.
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/hpt27xx/hpt27xx_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 #include <sys/time.h>
44 #include <sys/systm.h>
45 
46 #include <sys/stat.h>
47 #include <sys/malloc.h>
48 #include <sys/conf.h>
49 #include <sys/libkern.h>
50 #include <sys/kernel.h>
51 
52 #include <sys/kthread.h>
53 #include <sys/mutex.h>
54 #include <sys/module.h>
55 
56 #include <sys/eventhandler.h>
57 #include <sys/bus.h>
58 #include <sys/taskqueue.h>
59 #include <sys/ioccom.h>
60 
61 #include <machine/resource.h>
62 #include <machine/pci_cfgreg.h>
63 #include <machine/bus.h>
64 #include <machine/stdarg.h>
65 #include <sys/rman.h>
66 
67 #include <vm/vm.h>
68 #include <vm/pmap.h>
69 
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 
73 #include <cam/cam.h>
74 #include <cam/cam_ccb.h>
75 #include <cam/cam_sim.h>
76 #include <cam/cam_xpt_sim.h>
77 #include <cam/cam_debug.h>
78 #include <cam/cam_xpt_periph.h>
79 #include <cam/cam_periph.h>
80 #include <cam/scsi/scsi_all.h>
81 #include <cam/scsi/scsi_message.h>
82 
83 
84 typedef struct _INQUIRYDATA {
85 	u_char DeviceType : 5;
86 	u_char DeviceTypeQualifier : 3;
87 	u_char DeviceTypeModifier : 7;
88 	u_char RemovableMedia : 1;
89 	u_char Versions;
90 	u_char ResponseDataFormat;
91 	u_char AdditionalLength;
92 	u_char Reserved[2];
93 	u_char SoftReset : 1;
94 	u_char CommandQueue : 1;
95 	u_char Reserved2 : 1;
96 	u_char LinkedCommands : 1;
97 	u_char Synchronous : 1;
98 	u_char Wide16Bit : 1;
99 	u_char Wide32Bit : 1;
100 	u_char RelativeAddressing : 1;
101 	u_char VendorId[8];
102 	u_char ProductId[16];
103 	u_char ProductRevisionLevel[4];
104 	u_char VendorSpecific[20];
105 	u_char Reserved3[40];
106 }
107 __attribute__((packed))
108 INQUIRYDATA, *PINQUIRYDATA;
109 
110 #endif
111 
112 /* private headers */
113 
114 #include <dev/hpt27xx/osm.h>
115 #include <dev/hpt27xx/him.h>
116 #include <dev/hpt27xx/ldm.h>
117 
118 /* driver parameters */
119 extern const char driver_name[];
120 extern const char driver_name_long[];
121 extern const char driver_ver[];
122 extern int  osm_max_targets;
123 
124 /*
125  * adapter/vbus extensions:
126  * each physical controller has an adapter_ext, passed to him.create_adapter()
127  * each vbus has a vbus_ext passed to ldm_create_vbus().
128  */
129 #define EXT_TYPE_HBA  1
130 #define EXT_TYPE_VBUS 2
131 
132 typedef struct _hba {
133 	int               ext_type;
134 	LDM_ADAPTER       ldm_adapter;
135 	device_t          pcidev;
136 	PCI_ADDRESS       pciaddr;
137 	struct _vbus_ext *vbus_ext;
138 	struct _hba      *next;
139 
140 	struct {
141 		struct resource *res;
142 		int type;
143 		int rid;
144 		void *base;
145 	}
146 	pcibar[6];
147 
148 	struct resource  *irq_res;
149 	void             *irq_handle;
150 }
151 HBA, *PHBA;
152 
153 typedef struct _os_cmdext {
154 	struct _vbus_ext  *vbus_ext;
155 	struct _os_cmdext *next;
156 	union ccb         *ccb;
157 	bus_dmamap_t       dma_map;
158 #if (__FreeBSD_version >= 1000510)
159 	struct callout     timeout;
160 #endif
161 	SG                 psg[os_max_sg_descriptors];
162 }
163 OS_CMDEXT, *POS_CMDEXT;
164 
165 typedef struct _vbus_ext {
166 	int               ext_type;
167 	struct _vbus_ext *next;
168 	PHBA              hba_list;
169 	struct freelist  *freelist_head;
170 	struct freelist  *freelist_dma_head;
171 
172 	struct cam_sim   *sim;    /* sim for this vbus */
173 	struct cam_path  *path;   /* peripheral, path, tgt, lun with this vbus */
174 	struct mtx        lock; /* general purpose lock */
175 	bus_dma_tag_t     io_dmat; /* I/O buffer DMA tag */
176 
177 	POS_CMDEXT        cmdext_list;
178 
179 	OSM_TASK         *tasks;
180 	struct task       worker;
181 #if (__FreeBSD_version >= 1000510)
182 	struct callout    timer;
183 #else
184 	struct callout_handle timer;
185 #endif
186 	eventhandler_tag  shutdown_eh;
187 
188 	/* the LDM vbus instance continues */
189 	unsigned long vbus[0] __attribute__((aligned(sizeof(unsigned long))));
190 }
191 VBUS_EXT, *PVBUS_EXT;
192 
193 #define hpt_lock_vbus(vbus_ext)   mtx_lock(&(vbus_ext)->lock)
194 #define hpt_unlock_vbus(vbus_ext) mtx_unlock(&(vbus_ext)->lock)
195 #define	hpt_assert_vbus_locked(vbus_ext) mtx_assert(&(vbus_ext)->lock, MA_OWNED)
196 
197 
198 #define HPT_OSM_TIMEOUT (20*hz)  /* timeout value for OS commands */
199 
200 #define HPT_DO_IOCONTROL	_IOW('H', 0, HPT_IOCTL_PARAM)
201 
202 #define HPT_SCAN_BUS		_IO('H', 1)
203 
204 #if __FreeBSD_version < 1000510
205 #define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi_giant,(task));
206 #else
207 #define TASK_ENQUEUE(task)	taskqueue_enqueue(taskqueue_swi,(task));
208 #endif
209 
210 static	__inline	int hpt_sleep(PVBUS_EXT vbus_ext, void *ident, int priority, const char *wmesg, int timo)
211 {
212 	return	msleep(ident, &vbus_ext->lock, priority, wmesg, timo);
213 }
214 
215