Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*-
 * FreeBSD platform specific, shared driver option settings, data structures,
 * function declarations and includes.
 *
 * Copyright (c) 1994-2001 Justin T. Gibbs.
 * Copyright (c) 2001-2003 Adaptec Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU Public License ("GPL").
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic_osm_lib.h#5 $
 *
 * $FreeBSD$
 */

/******************************** OS Includes *********************************/
#include <sys/mutex.h>

/*************************** Library Symbol Mapping ***************************/
#define	AIC_LIB_ENTRY_CONCAT(x, prefix)	prefix ## x
#define	AIC_LIB_ENTRY_EXPAND(x, prefix)	AIC_LIB_ENTRY_CONCAT(x, prefix)
#define	AIC_LIB_ENTRY(x)		AIC_LIB_ENTRY_EXPAND(x, AIC_LIB_PREFIX)
#define	AIC_CONST_ENTRY(x)		AIC_LIB_ENTRY_EXPAND(x,AIC_CONST_PREFIX)

#define	aic_softc			AIC_LIB_ENTRY(_softc)
#define	aic_tailq			AIC_LIB_ENTRY(_tailq)
#define	aic_transinfo			AIC_LIB_ENTRY(_transinfo)
#define	aic_platform_data		AIC_LIB_ENTRY(_platform_data)
#define	aic_devinfo			AIC_LIB_ENTRY(_devinfo)
#define	aic_lock			AIC_LIB_ENTRY(_lock)
#define	aic_unlock			AIC_LIB_ENTRY(_unlock)
#define	aic_callback_t			AIC_LIB_ENTRY(_callback_t)
#define	aic_platform_freeze_devq	AIC_LIB_ENTRY(_platform_freeze_devq)
#define	aic_platform_abort_scbs		AIC_LIB_ENTRY(_platform_abort_scbs)
#define	aic_platform_timeout		AIC_LIB_ENTRY(_platform_timeout)
#define	aic_timeout			AIC_LIB_ENTRY(_timeout)
#define	aic_set_recoveryscb		AIC_LIB_ENTRY(_set_recoveryscb)
#define	aic_spawn_recovery_thread	AIC_LIB_ENTRY(_spawn_recovery_thread)
#define	aic_wakeup_recovery_thread	AIC_LIB_ENTRY(_wakeup_recovery_thread)
#define	aic_terminate_recovery_thread \
	AIC_LIB_ENTRY(_terminate_recovery_thread)
#define	aic_recovery_thread		AIC_LIB_ENTRY(_recovery_thread)
#define	aic_recover_commands		AIC_LIB_ENTRY(_recover_commands)
#define	aic_calc_geometry		AIC_LIB_ENTRY(_calc_geometry)

#define	AIC_RESOURCE_SHORTAGE		AIC_CONST_ENTRY(_RESOURCE_SHORTAGE)
#define	AIC_SHUTDOWN_RECOVERY		AIC_CONST_ENTRY(_SHUTDOWN_RECOVERY)

/********************************* Byte Order *********************************/
#define aic_htobe16(x) htobe16(x)
#define aic_htobe32(x) htobe32(x)
#define aic_htobe64(x) htobe64(x)
#define aic_htole16(x) htole16(x)
#define aic_htole32(x) htole32(x)
#define aic_htole64(x) htole64(x)

#define aic_be16toh(x) be16toh(x)
#define aic_be32toh(x) be32toh(x)
#define aic_be64toh(x) be64toh(x)
#define aic_le16toh(x) le16toh(x)
#define aic_le32toh(x) le32toh(x)
#define aic_le64toh(x) le64toh(x)

/************************* Forward Declarations *******************************/
typedef device_t aic_dev_softc_t;
typedef union ccb *aic_io_ctx_t;
struct aic_softc;
struct scb;

/*************************** Timer DataStructures *****************************/
typedef struct callout aic_timer_t;

/****************************** Error Recovery ********************************/
void		aic_set_recoveryscb(struct aic_softc *aic, struct scb *scb);
callout_func_t	aic_platform_timeout;
int		aic_spawn_recovery_thread(struct aic_softc *aic);
void		aic_terminate_recovery_thread(struct aic_softc *aic);

static __inline void	aic_wakeup_recovery_thread(struct aic_softc *aic);

static __inline void
aic_wakeup_recovery_thread(struct aic_softc *aic)
{
	wakeup(aic);
}

/****************************** Kernel Threads ********************************/
#define	aic_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)

/******************************* Bus Space/DMA ********************************/

#define aic_dma_tag_create(aic, parent_tag, alignment, boundary,	\
			   lowaddr, highaddr, filter, filterarg,	\
			   maxsize, nsegments, maxsegsz, flags,		\
			   dma_tagp)					\
	bus_dma_tag_create(parent_tag, alignment, boundary,		\
			   lowaddr, highaddr, filter, filterarg,	\
			   maxsize, nsegments, maxsegsz, flags,		\
			   busdma_lock_mutex, &aic->platform_data->mtx,			\
			   dma_tagp)

#define aic_dma_tag_destroy(aic, tag)					\
	bus_dma_tag_destroy(tag)

#define aic_dmamem_alloc(aic, dmat, vaddr, flags, mapp)			\
	bus_dmamem_alloc(dmat, vaddr, flags, mapp)

#define aic_dmamem_free(aic, dmat, vaddr, map)				\
	bus_dmamem_free(dmat, vaddr, map)

#define aic_dmamap_create(aic, tag, flags, mapp)			\
	bus_dmamap_create(tag, flags, mapp)

#define aic_dmamap_destroy(aic, tag, map)				\
	bus_dmamap_destroy(tag, map)

#define aic_dmamap_load(aic, dmat, map, addr, buflen, callback,		\
			callback_arg, flags)				\
	bus_dmamap_load(dmat, map, addr, buflen, callback, callback_arg, flags)

#define aic_dmamap_unload(aic, tag, map)				\
	bus_dmamap_unload(tag, map)

/* XXX Need to update Bus DMA for partial map syncs */
#define aic_dmamap_sync(aic, dma_tag, dmamap, offset, len, op)		\
	bus_dmamap_sync(dma_tag, dmamap, op)

/***************************** Core Includes **********************************/
#include AIC_CORE_INCLUDE

/***************************** Timer Facilities *******************************/
#define aic_timer_init(timer) callout_init(timer, /*mpsafe*/1)
#define aic_timer_stop callout_stop

static __inline void aic_timer_reset(aic_timer_t *, u_int,
				     aic_callback_t *, void *);
static __inline u_int aic_get_timeout(struct scb *);
static __inline void aic_scb_timer_reset(struct scb *, u_int);

static __inline void
aic_timer_reset(aic_timer_t *timer, u_int msec, aic_callback_t *func, void *arg)
{
	uint64_t time;

	time = msec;
	time *= hz;
	time /= 1000;
	callout_reset(timer, time, func, arg);
}

static __inline u_int
aic_get_timeout(struct scb *scb)
{
	return (scb->io_ctx->ccb_h.timeout);
}

static __inline void
aic_scb_timer_reset(struct scb *scb, u_int msec)
{
	uint64_t time;

	time = msec;
	time *= hz;
	time /= 1000;
	callout_reset(&scb->io_timer, time, aic_platform_timeout, scb);
}

static __inline void
aic_scb_timer_start(struct scb *scb)
{

	if (AIC_SCB_DATA(scb->aic_softc)->recovery_scbs == 0
	 && scb->io_ctx->ccb_h.timeout != CAM_TIME_INFINITY) {
		aic_scb_timer_reset(scb, scb->io_ctx->ccb_h.timeout);
	}
}

/************************** Transaction Operations ****************************/
static __inline void aic_set_transaction_status(struct scb *, uint32_t);
static __inline void aic_set_scsi_status(struct scb *, uint32_t);
static __inline uint32_t aic_get_transaction_status(struct scb *);
static __inline uint32_t aic_get_scsi_status(struct scb *);
static __inline void aic_set_transaction_tag(struct scb *, int, u_int);
static __inline u_long aic_get_transfer_length(struct scb *);
static __inline int aic_get_transfer_dir(struct scb *);
static __inline void aic_set_residual(struct scb *, u_long);
static __inline void aic_set_sense_residual(struct scb *, u_long);
static __inline u_long aic_get_residual(struct scb *);
static __inline int aic_perform_autosense(struct scb *);
static __inline uint32_t aic_get_sense_bufsize(struct aic_softc*, struct scb*);
static __inline void aic_freeze_ccb(union ccb *ccb);
static __inline void aic_freeze_scb(struct scb *scb);
static __inline void aic_platform_freeze_devq(struct aic_softc *, struct scb *);
static __inline int  aic_platform_abort_scbs(struct aic_softc *aic, int target,
					     char channel, int lun, u_int tag,
					     role_t role, uint32_t status);

static __inline
void aic_set_transaction_status(struct scb *scb, uint32_t status)
{
	scb->io_ctx->ccb_h.status &= ~CAM_STATUS_MASK;
	scb->io_ctx->ccb_h.status |= status;
}

static __inline
void aic_set_scsi_status(struct scb *scb, uint32_t status)
{
	scb->io_ctx->csio.scsi_status = status;
}

static __inline
uint32_t aic_get_transaction_status(struct scb *scb)
{
	return (scb->io_ctx->ccb_h.status & CAM_STATUS_MASK);
}

static __inline
uint32_t aic_get_scsi_status(struct scb *scb)
{
	return (scb->io_ctx->csio.scsi_status);
}

static __inline
void aic_set_transaction_tag(struct scb *scb, int enabled, u_int type)
{
	scb->io_ctx->csio.tag_action = type;
	if (enabled)
		scb->io_ctx->ccb_h.flags |= CAM_TAG_ACTION_VALID;
	else
		scb->io_ctx->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
}

static __inline
u_long aic_get_transfer_length(struct scb *scb)
{
	return (scb->io_ctx->csio.dxfer_len);
}

static __inline
int aic_get_transfer_dir(struct scb *scb)
{
	return (scb->io_ctx->ccb_h.flags & CAM_DIR_MASK);
}

static __inline
void aic_set_residual(struct scb *scb, u_long resid)
{
	scb->io_ctx->csio.resid = resid;
}

static __inline
void aic_set_sense_residual(struct scb *scb, u_long resid)
{
	scb->io_ctx->csio.sense_resid = resid;
}

static __inline
u_long aic_get_residual(struct scb *scb)
{
	return (scb->io_ctx->csio.resid);
}

static __inline
int aic_perform_autosense(struct scb *scb)
{
	return (!(scb->io_ctx->ccb_h.flags & CAM_DIS_AUTOSENSE));
}

static __inline uint32_t
aic_get_sense_bufsize(struct aic_softc *aic, struct scb *scb)
{
	return (sizeof(struct scsi_sense_data));
}

static __inline void
aic_freeze_ccb(union ccb *ccb)
{
	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
		ccb->ccb_h.status |= CAM_DEV_QFRZN;
		xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
	}
}

static __inline void
aic_freeze_scb(struct scb *scb)
{
	aic_freeze_ccb(scb->io_ctx);
}

static __inline void
aic_platform_freeze_devq(struct aic_softc *aic, struct scb *scb)
{
	/* Nothing to do here for FreeBSD */
}

static __inline int
aic_platform_abort_scbs(struct aic_softc *aic, int target,
			char channel, int lun, u_int tag,
			role_t role, uint32_t status)
{
	/* Nothing to do here for FreeBSD */
	return (0);
}

static __inline void
aic_platform_scb_free(struct aic_softc *aic, struct scb *scb)
{
	/* What do we do to generically handle driver resource shortages??? */
	if ((aic->flags & AIC_RESOURCE_SHORTAGE) != 0
	 && scb->io_ctx != NULL
	 && (scb->io_ctx->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
		aic->flags &= ~AIC_RESOURCE_SHORTAGE;
	}
	scb->io_ctx = NULL;
}

/*************************** CAM CCB Operations *******************************/
void aic_calc_geometry(struct ccb_calc_geometry *ccg, int extended);

/****************************** OS Primitives *********************************/
#define aic_delay DELAY

/********************************** PCI ***************************************/
#ifdef AIC_PCI_CONFIG
static __inline uint32_t aic_pci_read_config(aic_dev_softc_t pci,
					     int reg, int width);
static __inline void	 aic_pci_write_config(aic_dev_softc_t pci,
					      int reg, uint32_t value,
					      int width);
static __inline int	 aic_get_pci_function(aic_dev_softc_t);
static __inline int	 aic_get_pci_slot(aic_dev_softc_t);
static __inline int	 aic_get_pci_bus(aic_dev_softc_t);

static __inline uint32_t
aic_pci_read_config(aic_dev_softc_t pci, int reg, int width)
{
	return (pci_read_config(pci, reg, width));
}

static __inline void
aic_pci_write_config(aic_dev_softc_t pci, int reg, uint32_t value, int width)
{
	pci_write_config(pci, reg, value, width);
}

static __inline int
aic_get_pci_function(aic_dev_softc_t pci)
{
	return (pci_get_function(pci));
}

static __inline int
aic_get_pci_slot(aic_dev_softc_t pci)
{
	return (pci_get_slot(pci));
}

static __inline int
aic_get_pci_bus(aic_dev_softc_t pci)
{
	return (pci_get_bus(pci));
}

typedef enum
{
	AIC_POWER_STATE_D0 = PCI_POWERSTATE_D0,
	AIC_POWER_STATE_D1 = PCI_POWERSTATE_D1,
	AIC_POWER_STATE_D2 = PCI_POWERSTATE_D2,
	AIC_POWER_STATE_D3 = PCI_POWERSTATE_D3
} aic_power_state;

static __inline int aic_power_state_change(struct aic_softc *aic,
					   aic_power_state new_state);

static __inline int
aic_power_state_change(struct aic_softc *aic, aic_power_state new_state)
{
	return (pci_set_powerstate(aic->dev_softc, new_state));
}
#endif