xref: /freebsd/sys/dev/mps/mpsvar.h (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * Copyright (c) 2011, 2012 LSI Corp.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * LSI MPT-Fusion Host Adapter FreeBSD
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _MPSVAR_H
33 #define _MPSVAR_H
34 
35 #define MPS_DRIVER_VERSION	"14.00.00.02-fbsd"
36 
37 #define MPS_DB_MAX_WAIT		2500
38 
39 #define MPS_REQ_FRAMES		1024
40 #define MPS_EVT_REPLY_FRAMES	32
41 #define MPS_REPLY_FRAMES	MPS_REQ_FRAMES
42 #define MPS_CHAIN_FRAMES	2048
43 #define MPS_SENSE_LEN		SSD_FULL_SIZE
44 #define MPS_MSI_COUNT		1
45 #define MPS_SGE64_SIZE		12
46 #define MPS_SGE32_SIZE		8
47 #define MPS_SGC_SIZE		8
48 
49 #define	 CAN_SLEEP			1
50 #define  NO_SLEEP			0
51 
52 #define MPS_PERIODIC_DELAY	1	/* 1 second heartbeat/watchdog check */
53 
54 #define MPS_SCSI_RI_INVALID_FRAME	(0x00000002)
55 #define MPS_STRING_LENGTH               64
56 
57 #include <sys/endian.h>
58 
59 /*
60  * host mapping related macro definitions
61  */
62 #define MPS_MAPTABLE_BAD_IDX	0xFFFFFFFF
63 #define MPS_DPM_BAD_IDX		0xFFFF
64 #define MPS_ENCTABLE_BAD_IDX	0xFF
65 #define MPS_MAX_MISSING_COUNT	0x0F
66 #define MPS_DEV_RESERVED	0x20000000
67 #define MPS_MAP_IN_USE		0x10000000
68 #define MPS_RAID_CHANNEL	1
69 #define MPS_MAP_BAD_ID		0xFFFFFFFF
70 
71 /*
72  * WarpDrive controller
73  */
74 #define	MPS_CHIP_WD_DEVICE_ID	0x007E
75 #define	MPS_WD_LSI_OEM		0x80
76 #define	MPS_WD_HIDE_EXPOSE_MASK	0x03
77 #define	MPS_WD_HIDE_ALWAYS	0x00
78 #define	MPS_WD_EXPOSE_ALWAYS	0x01
79 #define	MPS_WD_HIDE_IF_VOLUME	0x02
80 #define	MPS_WD_RETRY		0x01
81 #define	MPS_MAN_PAGE10_SIZE	0x5C	/* Hardcode for now */
82 #define MPS_MAX_DISKS_IN_VOL	10
83 
84 /*
85  * WarpDrive Event Logging
86  */
87 #define	MPI2_WD_LOG_ENTRY	0x8002
88 #define	MPI2_WD_SSD_THROTTLING	0x0041
89 #define	MPI2_WD_DRIVE_LIFE_WARN	0x0043
90 #define	MPI2_WD_DRIVE_LIFE_DEAD	0x0044
91 #define	MPI2_WD_RAIL_MON_FAIL	0x004D
92 
93 typedef uint8_t u8;
94 typedef uint16_t u16;
95 typedef uint32_t u32;
96 typedef uint64_t u64;
97 
98 /**
99  * struct dev_mapping_table - device mapping information
100  * @physical_id: SAS address for drives or WWID for RAID volumes
101  * @device_info: bitfield provides detailed info about the device
102  * @phy_bits: bitfields indicating controller phys
103  * @dpm_entry_num: index of this device in device persistent map table
104  * @dev_handle: device handle for the device pointed by this entry
105  * @channel: target channel
106  * @id: target id
107  * @missing_count: number of times the device not detected by driver
108  * @hide_flag: Hide this physical disk/not (foreign configuration)
109  * @init_complete: Whether the start of the day checks completed or not
110  */
111 struct dev_mapping_table {
112 	u64	physical_id;
113 	u32	device_info;
114 	u32	phy_bits;
115 	u16	dpm_entry_num;
116 	u16	dev_handle;
117 	u8	reserved1;
118 	u8	channel;
119 	u16	id;
120 	u8	missing_count;
121 	u8	init_complete;
122 	u8	TLR_bits;
123 	u8	reserved2;
124 };
125 
126 /**
127  * struct enc_mapping_table -  mapping information about an enclosure
128  * @enclosure_id: Logical ID of this enclosure
129  * @start_index: index to the entry in dev_mapping_table
130  * @phy_bits: bitfields indicating controller phys
131  * @dpm_entry_num: index of this enclosure in device persistent map table
132  * @enc_handle: device handle for the enclosure pointed by this entry
133  * @num_slots: number of slots in the enclosure
134  * @start_slot: Starting slot id
135  * @missing_count: number of times the device not detected by driver
136  * @removal_flag: used to mark the device for removal
137  * @skip_search: used as a flag to include/exclude enclosure for search
138  * @init_complete: Whether the start of the day checks completed or not
139  */
140 struct enc_mapping_table {
141 	u64	enclosure_id;
142 	u32	start_index;
143 	u32	phy_bits;
144 	u16	dpm_entry_num;
145 	u16	enc_handle;
146 	u16	num_slots;
147 	u16	start_slot;
148 	u8	missing_count;
149 	u8	removal_flag;
150 	u8	skip_search;
151 	u8	init_complete;
152 };
153 
154 /**
155  * struct map_removal_table - entries to be removed from mapping table
156  * @dpm_entry_num: index of this device in device persistent map table
157  * @dev_handle: device handle for the device pointed by this entry
158  */
159 struct map_removal_table{
160 	u16	dpm_entry_num;
161 	u16	dev_handle;
162 };
163 
164 typedef struct mps_fw_diagnostic_buffer {
165 	size_t		size;
166 	uint8_t		extended_type;
167 	uint8_t		buffer_type;
168 	uint8_t		force_release;
169 	uint32_t	product_specific[23];
170 	uint8_t		immediate;
171 	uint8_t		enabled;
172 	uint8_t		valid_data;
173 	uint8_t		owned_by_firmware;
174 	uint32_t	unique_id;
175 } mps_fw_diagnostic_buffer_t;
176 
177 struct mps_softc;
178 struct mps_command;
179 struct mpssas_softc;
180 union ccb;
181 struct mpssas_target;
182 struct mps_column_map;
183 
184 MALLOC_DECLARE(M_MPT2);
185 
186 typedef void mps_evt_callback_t(struct mps_softc *, uintptr_t,
187     MPI2_EVENT_NOTIFICATION_REPLY *reply);
188 typedef void mps_command_callback_t(struct mps_softc *, struct mps_command *cm);
189 
190 struct mps_chain {
191 	TAILQ_ENTRY(mps_chain)		chain_link;
192 	MPI2_SGE_IO_UNION		*chain;
193 	uint32_t			chain_busaddr;
194 };
195 
196 /*
197  * This needs to be at least 2 to support SMP passthrough.
198  */
199 #define       MPS_IOVEC_COUNT 2
200 
201 struct mps_command {
202 	TAILQ_ENTRY(mps_command)	cm_link;
203 	TAILQ_ENTRY(mps_command)	cm_recovery;
204 	struct mps_softc		*cm_sc;
205 	union ccb			*cm_ccb;
206 	void				*cm_data;
207 	u_int				cm_length;
208 	u_int				cm_out_len;
209 	struct uio			cm_uio;
210 	struct iovec			cm_iovec[MPS_IOVEC_COUNT];
211 	u_int				cm_max_segs;
212 	u_int				cm_sglsize;
213 	MPI2_SGE_IO_UNION		*cm_sge;
214 	uint8_t				*cm_req;
215 	uint8_t				*cm_reply;
216 	uint32_t			cm_reply_data;
217 	mps_command_callback_t		*cm_complete;
218 	void				*cm_complete_data;
219 	struct mpssas_target		*cm_targ;
220 	MPI2_REQUEST_DESCRIPTOR_UNION	cm_desc;
221 	u_int	                	cm_lun;
222 	u_int				cm_flags;
223 #define MPS_CM_FLAGS_POLLED		(1 << 0)
224 #define MPS_CM_FLAGS_COMPLETE		(1 << 1)
225 #define MPS_CM_FLAGS_SGE_SIMPLE		(1 << 2)
226 #define MPS_CM_FLAGS_DATAOUT		(1 << 3)
227 #define MPS_CM_FLAGS_DATAIN		(1 << 4)
228 #define MPS_CM_FLAGS_WAKEUP		(1 << 5)
229 #define MPS_CM_FLAGS_DD_IO		(1 << 6)
230 #define MPS_CM_FLAGS_USE_UIO		(1 << 7)
231 #define MPS_CM_FLAGS_SMP_PASS		(1 << 8)
232 #define	MPS_CM_FLAGS_CHAIN_FAILED	(1 << 9)
233 #define	MPS_CM_FLAGS_ERROR_MASK		MPS_CM_FLAGS_CHAIN_FAILED
234 #define	MPS_CM_FLAGS_USE_CCB		(1 << 10)
235 	u_int				cm_state;
236 #define MPS_CM_STATE_FREE		0
237 #define MPS_CM_STATE_BUSY		1
238 #define MPS_CM_STATE_TIMEDOUT		2
239 	bus_dmamap_t			cm_dmamap;
240 	struct scsi_sense_data		*cm_sense;
241 	TAILQ_HEAD(, mps_chain)		cm_chain_list;
242 	uint32_t			cm_req_busaddr;
243 	uint32_t			cm_sense_busaddr;
244 	struct callout			cm_callout;
245 };
246 
247 struct mps_column_map {
248 	uint16_t			dev_handle;
249 	uint8_t				phys_disk_num;
250 };
251 
252 struct mps_event_handle {
253 	TAILQ_ENTRY(mps_event_handle)	eh_list;
254 	mps_evt_callback_t		*callback;
255 	void				*data;
256 	u32				mask[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
257 };
258 
259 struct mps_softc {
260 	device_t			mps_dev;
261 	struct cdev			*mps_cdev;
262 	u_int				mps_flags;
263 #define MPS_FLAGS_INTX		(1 << 0)
264 #define MPS_FLAGS_MSI		(1 << 1)
265 #define MPS_FLAGS_BUSY		(1 << 2)
266 #define MPS_FLAGS_SHUTDOWN	(1 << 3)
267 #define MPS_FLAGS_DIAGRESET	(1 << 4)
268 #define	MPS_FLAGS_ATTACH_DONE	(1 << 5)
269 #define	MPS_FLAGS_WD_AVAILABLE	(1 << 6)
270 	u_int				mps_debug;
271 	u_int				disable_msix;
272 	u_int				disable_msi;
273 	int				tm_cmds_active;
274 	int				io_cmds_active;
275 	int				io_cmds_highwater;
276 	int				chain_free;
277 	int				max_chains;
278 	int				chain_free_lowwater;
279 #if __FreeBSD_version >= 900030
280 	uint64_t			chain_alloc_fail;
281 #endif
282 	struct sysctl_ctx_list		sysctl_ctx;
283 	struct sysctl_oid		*sysctl_tree;
284 	char                            fw_version[16];
285 	struct mps_command		*commands;
286 	struct mps_chain		*chains;
287 	struct callout			periodic;
288 
289 	struct mpssas_softc		*sassc;
290 	char            tmp_string[MPS_STRING_LENGTH];
291 	TAILQ_HEAD(, mps_command)	req_list;
292 	TAILQ_HEAD(, mps_command)	high_priority_req_list;
293 	TAILQ_HEAD(, mps_chain)		chain_list;
294 	TAILQ_HEAD(, mps_command)	tm_list;
295 	int				replypostindex;
296 	int				replyfreeindex;
297 
298 	struct resource			*mps_regs_resource;
299 	bus_space_handle_t		mps_bhandle;
300 	bus_space_tag_t			mps_btag;
301 	int				mps_regs_rid;
302 
303 	bus_dma_tag_t			mps_parent_dmat;
304 	bus_dma_tag_t			buffer_dmat;
305 
306 	MPI2_IOC_FACTS_REPLY		*facts;
307 	MPI2_PORT_FACTS_REPLY		*pfacts;
308 	int				num_reqs;
309 	int				num_replies;
310 	int				fqdepth;	/* Free queue */
311 	int				pqdepth;	/* Post queue */
312 
313 	u32             event_mask[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
314 	TAILQ_HEAD(, mps_event_handle)	event_list;
315 	struct mps_event_handle		*mps_log_eh;
316 
317 	struct mtx			mps_mtx;
318 	struct intr_config_hook		mps_ich;
319 	struct resource			*mps_irq[MPS_MSI_COUNT];
320 	void				*mps_intrhand[MPS_MSI_COUNT];
321 	int				mps_irq_rid[MPS_MSI_COUNT];
322 
323 	uint8_t				*req_frames;
324 	bus_addr_t			req_busaddr;
325 	bus_dma_tag_t			req_dmat;
326 	bus_dmamap_t			req_map;
327 
328 	uint8_t				*reply_frames;
329 	bus_addr_t			reply_busaddr;
330 	bus_dma_tag_t			reply_dmat;
331 	bus_dmamap_t			reply_map;
332 
333 	struct scsi_sense_data		*sense_frames;
334 	bus_addr_t			sense_busaddr;
335 	bus_dma_tag_t			sense_dmat;
336 	bus_dmamap_t			sense_map;
337 
338 	uint8_t				*chain_frames;
339 	bus_addr_t			chain_busaddr;
340 	bus_dma_tag_t			chain_dmat;
341 	bus_dmamap_t			chain_map;
342 
343 	MPI2_REPLY_DESCRIPTORS_UNION	*post_queue;
344 	bus_addr_t			post_busaddr;
345 	uint32_t			*free_queue;
346 	bus_addr_t			free_busaddr;
347 	bus_dma_tag_t			queues_dmat;
348 	bus_dmamap_t			queues_map;
349 
350 	uint8_t				*fw_diag_buffer;
351 	bus_addr_t			fw_diag_busaddr;
352 	bus_dma_tag_t			fw_diag_dmat;
353 	bus_dmamap_t			fw_diag_map;
354 
355 	uint8_t				ir_firmware;
356 
357 	/* static config pages */
358 	Mpi2IOCPage8_t			ioc_pg8;
359 
360 	/* host mapping support */
361 	struct dev_mapping_table	*mapping_table;
362 	struct enc_mapping_table	*enclosure_table;
363 	struct map_removal_table	*removal_table;
364 	uint8_t				*dpm_entry_used;
365 	uint8_t				*dpm_flush_entry;
366 	Mpi2DriverMappingPage0_t	*dpm_pg0;
367 	uint16_t			max_devices;
368 	uint16_t			max_enclosures;
369 	uint16_t			max_expanders;
370 	uint8_t				max_volumes;
371 	uint8_t				num_enc_table_entries;
372 	uint8_t				num_rsvd_entries;
373 	uint8_t				num_channels;
374 	uint16_t			max_dpm_entries;
375 	uint8_t				is_dpm_enable;
376 	uint8_t				track_mapping_events;
377 	uint32_t			pending_map_events;
378 	uint8_t				mt_full_retry;
379 	uint8_t				mt_add_device_failed;
380 
381 	/* FW diag Buffer List */
382 	mps_fw_diagnostic_buffer_t
383 				fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_COUNT];
384 
385 	/* Event Recording IOCTL support */
386 	uint32_t			events_to_record[4];
387 	mps_event_entry_t		recorded_events[MPS_EVENT_QUEUE_SIZE];
388 	uint8_t				event_index;
389 	uint32_t			event_number;
390 
391 	/* EEDP and TLR support */
392 	uint8_t				eedp_enabled;
393 	uint8_t				control_TLR;
394 
395 	/* Shutdown Event Handler */
396 	eventhandler_tag		shutdown_eh;
397 
398 	/* To track topo events during reset */
399 #define	MPS_DIAG_RESET_TIMEOUT	300000
400 	uint8_t				wait_for_port_enable;
401 	uint8_t				port_enable_complete;
402 	uint8_t				msleep_fake_chan;
403 
404 	/* WD controller */
405 	uint8_t             WD_available;
406 	uint8_t				WD_valid_config;
407 	uint8_t				WD_hide_expose;
408 
409 	/* Direct Drive for WarpDrive */
410 	uint8_t				DD_num_phys_disks;
411 	uint16_t			DD_dev_handle;
412 	uint32_t			DD_stripe_size;
413 	uint32_t			DD_stripe_exponent;
414 	uint32_t			DD_block_size;
415 	uint16_t			DD_block_exponent;
416 	uint64_t			DD_max_lba;
417 	struct mps_column_map		DD_column_map[MPS_MAX_DISKS_IN_VOL];
418 };
419 
420 struct mps_config_params {
421 	MPI2_CONFIG_EXT_PAGE_HEADER_UNION	hdr;
422 	u_int		action;
423 	u_int		page_address;	/* Attributes, not a phys address */
424 	u_int		status;
425 	void		*buffer;
426 	u_int		length;
427 	int		timeout;
428 	void		(*callback)(struct mps_softc *, struct mps_config_params *);
429 	void		*cbdata;
430 };
431 
432 struct scsi_read_capacity_eedp
433 {
434 	uint8_t addr[8];
435 	uint8_t length[4];
436 	uint8_t protect;
437 };
438 
439 static __inline uint32_t
440 mps_regread(struct mps_softc *sc, uint32_t offset)
441 {
442 	return (bus_space_read_4(sc->mps_btag, sc->mps_bhandle, offset));
443 }
444 
445 static __inline void
446 mps_regwrite(struct mps_softc *sc, uint32_t offset, uint32_t val)
447 {
448 	bus_space_write_4(sc->mps_btag, sc->mps_bhandle, offset, val);
449 }
450 
451 /* free_queue must have Little Endian address
452  * TODO- cm_reply_data is unwanted. We can remove it.
453  * */
454 static __inline void
455 mps_free_reply(struct mps_softc *sc, uint32_t busaddr)
456 {
457 	if (++sc->replyfreeindex >= sc->fqdepth)
458 		sc->replyfreeindex = 0;
459 	sc->free_queue[sc->replyfreeindex] = htole32(busaddr);
460 	mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
461 }
462 
463 static __inline struct mps_chain *
464 mps_alloc_chain(struct mps_softc *sc)
465 {
466 	struct mps_chain *chain;
467 
468 	if ((chain = TAILQ_FIRST(&sc->chain_list)) != NULL) {
469 		TAILQ_REMOVE(&sc->chain_list, chain, chain_link);
470 		sc->chain_free--;
471 		if (sc->chain_free < sc->chain_free_lowwater)
472 			sc->chain_free_lowwater = sc->chain_free;
473 	}
474 #if __FreeBSD_version >= 900030
475 	else
476 		sc->chain_alloc_fail++;
477 #endif
478 	return (chain);
479 }
480 
481 static __inline void
482 mps_free_chain(struct mps_softc *sc, struct mps_chain *chain)
483 {
484 #if 0
485 	bzero(chain->chain, 128);
486 #endif
487 	sc->chain_free++;
488 	TAILQ_INSERT_TAIL(&sc->chain_list, chain, chain_link);
489 }
490 
491 static __inline void
492 mps_free_command(struct mps_softc *sc, struct mps_command *cm)
493 {
494 	struct mps_chain *chain, *chain_temp;
495 
496 	if (cm->cm_reply != NULL)
497 		mps_free_reply(sc, cm->cm_reply_data);
498 	cm->cm_reply = NULL;
499 	cm->cm_flags = 0;
500 	cm->cm_complete = NULL;
501 	cm->cm_complete_data = NULL;
502 	cm->cm_ccb = NULL;
503 	cm->cm_targ = NULL;
504 	cm->cm_max_segs = 0;
505 	cm->cm_lun = 0;
506 	cm->cm_state = MPS_CM_STATE_FREE;
507 	cm->cm_data = NULL;
508 	cm->cm_length = 0;
509 	cm->cm_out_len = 0;
510 	cm->cm_sglsize = 0;
511 	cm->cm_sge = NULL;
512 
513 	TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
514 		TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
515 		mps_free_chain(sc, chain);
516 	}
517 	TAILQ_INSERT_TAIL(&sc->req_list, cm, cm_link);
518 }
519 
520 static __inline struct mps_command *
521 mps_alloc_command(struct mps_softc *sc)
522 {
523 	struct mps_command *cm;
524 
525 	cm = TAILQ_FIRST(&sc->req_list);
526 	if (cm == NULL)
527 		return (NULL);
528 
529 	TAILQ_REMOVE(&sc->req_list, cm, cm_link);
530 	KASSERT(cm->cm_state == MPS_CM_STATE_FREE, ("mps: Allocating busy command\n"));
531 	cm->cm_state = MPS_CM_STATE_BUSY;
532 	return (cm);
533 }
534 
535 static __inline void
536 mps_free_high_priority_command(struct mps_softc *sc, struct mps_command *cm)
537 {
538 	struct mps_chain *chain, *chain_temp;
539 
540 	if (cm->cm_reply != NULL)
541 		mps_free_reply(sc, cm->cm_reply_data);
542 	cm->cm_reply = NULL;
543 	cm->cm_flags = 0;
544 	cm->cm_complete = NULL;
545 	cm->cm_complete_data = NULL;
546 	cm->cm_ccb = NULL;
547 	cm->cm_targ = NULL;
548 	cm->cm_lun = 0;
549 	cm->cm_state = MPS_CM_STATE_FREE;
550 	TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
551 		TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
552 		mps_free_chain(sc, chain);
553 	}
554 	TAILQ_INSERT_TAIL(&sc->high_priority_req_list, cm, cm_link);
555 }
556 
557 static __inline struct mps_command *
558 mps_alloc_high_priority_command(struct mps_softc *sc)
559 {
560 	struct mps_command *cm;
561 
562 	cm = TAILQ_FIRST(&sc->high_priority_req_list);
563 	if (cm == NULL)
564 		return (NULL);
565 
566 	TAILQ_REMOVE(&sc->high_priority_req_list, cm, cm_link);
567 	KASSERT(cm->cm_state == MPS_CM_STATE_FREE, ("mps: Allocating busy command\n"));
568 	cm->cm_state = MPS_CM_STATE_BUSY;
569 	return (cm);
570 }
571 
572 static __inline void
573 mps_lock(struct mps_softc *sc)
574 {
575 	mtx_lock(&sc->mps_mtx);
576 }
577 
578 static __inline void
579 mps_unlock(struct mps_softc *sc)
580 {
581 	mtx_unlock(&sc->mps_mtx);
582 }
583 
584 #define MPS_INFO	(1 << 0)
585 #define MPS_TRACE	(1 << 1)
586 #define MPS_FAULT	(1 << 2)
587 #define MPS_EVENT	(1 << 3)
588 #define MPS_LOG		(1 << 4)
589 
590 #define mps_printf(sc, args...)				\
591 	device_printf((sc)->mps_dev, ##args)
592 
593 #define mps_vprintf(sc, args...)			\
594 do {							\
595 	if (bootverbose)				\
596 		mps_printf(sc, ##args);			\
597 } while (0)
598 
599 #define mps_dprint(sc, level, msg, args...)		\
600 do {							\
601 	if (sc->mps_debug & level)			\
602 		device_printf(sc->mps_dev, msg, ##args);	\
603 } while (0)
604 
605 #define mps_dprint_field(sc, level, msg, args...)		\
606 do {								\
607 	if (sc->mps_debug & level)				\
608 		printf("\t" msg, ##args);			\
609 } while (0)
610 
611 #define MPS_PRINTFIELD_START(sc, tag...)	\
612 	mps_dprint((sc), MPS_INFO, ##tag);	\
613 	mps_dprint_field((sc), MPS_INFO, ":\n")
614 #define MPS_PRINTFIELD_END(sc, tag)		\
615 	mps_dprint((sc), MPS_INFO, tag "\n")
616 #define MPS_PRINTFIELD(sc, facts, attr, fmt)	\
617 	mps_dprint_field((sc), MPS_INFO, #attr ": " #fmt "\n", (facts)->attr)
618 
619 #define MPS_EVENTFIELD_START(sc, tag...)	\
620 	mps_dprint((sc), MPS_EVENT, ##tag);	\
621 	mps_dprint_field((sc), MPS_EVENT, ":\n")
622 #define MPS_EVENTFIELD(sc, facts, attr, fmt)	\
623 	mps_dprint_field((sc), MPS_EVENT, #attr ": " #fmt "\n", (facts)->attr)
624 
625 #define  CAN_SLEEP                      1
626 #define  NO_SLEEP                       0
627 
628 static __inline void
629 mps_from_u64(uint64_t data, U64 *mps)
630 {
631 	(mps)->High = htole32((uint32_t)((data) >> 32));
632 	(mps)->Low = htole32((uint32_t)((data) & 0xffffffff));
633 }
634 
635 static __inline uint64_t
636 mps_to_u64(U64 *data)
637 {
638 
639 	return (((uint64_t)le32toh(data->High) << 32) | le32toh(data->Low));
640 }
641 
642 static __inline void
643 mps_mask_intr(struct mps_softc *sc)
644 {
645 	uint32_t mask;
646 
647 	mask = mps_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
648 	mask |= MPI2_HIM_REPLY_INT_MASK;
649 	mps_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
650 }
651 
652 static __inline void
653 mps_unmask_intr(struct mps_softc *sc)
654 {
655 	uint32_t mask;
656 
657 	mask = mps_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
658 	mask &= ~MPI2_HIM_REPLY_INT_MASK;
659 	mps_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
660 }
661 
662 int mps_pci_setup_interrupts(struct mps_softc *sc);
663 int mps_pci_restore(struct mps_softc *sc);
664 
665 int mps_attach(struct mps_softc *sc);
666 int mps_free(struct mps_softc *sc);
667 void mps_intr(void *);
668 void mps_intr_msi(void *);
669 void mps_intr_locked(void *);
670 int mps_register_events(struct mps_softc *, u32 *, mps_evt_callback_t *,
671     void *, struct mps_event_handle **);
672 int mps_restart(struct mps_softc *);
673 int mps_update_events(struct mps_softc *, struct mps_event_handle *, u32 *);
674 void mps_deregister_events(struct mps_softc *, struct mps_event_handle *);
675 int mps_push_sge(struct mps_command *, void *, size_t, int);
676 int mps_add_dmaseg(struct mps_command *, vm_paddr_t, size_t, u_int, int);
677 int mps_attach_sas(struct mps_softc *sc);
678 int mps_detach_sas(struct mps_softc *sc);
679 int mps_read_config_page(struct mps_softc *, struct mps_config_params *);
680 int mps_write_config_page(struct mps_softc *, struct mps_config_params *);
681 void mps_memaddr_cb(void *, bus_dma_segment_t *, int , int );
682 void mpi_init_sge(struct mps_command *cm, void *req, void *sge);
683 int mps_attach_user(struct mps_softc *);
684 void mps_detach_user(struct mps_softc *);
685 void mpssas_record_event(struct mps_softc *sc,
686     MPI2_EVENT_NOTIFICATION_REPLY *event_reply);
687 
688 int mps_map_command(struct mps_softc *sc, struct mps_command *cm);
689 int mps_wait_command(struct mps_softc *sc, struct mps_command *cm, int timeout);
690 int mps_request_polled(struct mps_softc *sc, struct mps_command *cm);
691 
692 int mps_config_get_bios_pg3(struct mps_softc *sc, Mpi2ConfigReply_t
693     *mpi_reply, Mpi2BiosPage3_t *config_page);
694 int mps_config_get_raid_volume_pg0(struct mps_softc *sc, Mpi2ConfigReply_t
695     *mpi_reply, Mpi2RaidVolPage0_t *config_page, u32 page_address);
696 int mps_config_get_ioc_pg8(struct mps_softc *sc, Mpi2ConfigReply_t *,
697     Mpi2IOCPage8_t *);
698 int mps_config_get_man_pg10(struct mps_softc *sc, Mpi2ConfigReply_t *mpi_reply);
699 int mps_config_get_sas_device_pg0(struct mps_softc *, Mpi2ConfigReply_t *,
700     Mpi2SasDevicePage0_t *, u32 , u16 );
701 int mps_config_get_dpm_pg0(struct mps_softc *, Mpi2ConfigReply_t *,
702     Mpi2DriverMappingPage0_t *, u16 );
703 int mps_config_get_raid_volume_pg1(struct mps_softc *sc,
704     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage1_t *config_page, u32 form,
705     u16 handle);
706 int mps_config_get_volume_wwid(struct mps_softc *sc, u16 volume_handle,
707     u64 *wwid);
708 int mps_config_get_raid_pd_pg0(struct mps_softc *sc,
709     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidPhysDiskPage0_t *config_page,
710     u32 page_address);
711 void mpssas_ir_shutdown(struct mps_softc *sc);
712 
713 int mps_reinit(struct mps_softc *sc);
714 void mpssas_handle_reinit(struct mps_softc *sc);
715 
716 void mps_base_static_config_pages(struct mps_softc *sc);
717 void mps_wd_config_pages(struct mps_softc *sc);
718 
719 int mps_mapping_initialize(struct mps_softc *);
720 void mps_mapping_topology_change_event(struct mps_softc *,
721     Mpi2EventDataSasTopologyChangeList_t *);
722 int mps_mapping_is_reinit_required(struct mps_softc *);
723 void mps_mapping_free_memory(struct mps_softc *sc);
724 int mps_config_set_dpm_pg0(struct mps_softc *, Mpi2ConfigReply_t *,
725     Mpi2DriverMappingPage0_t *, u16 );
726 void mps_mapping_exit(struct mps_softc *);
727 void mps_mapping_check_devices(struct mps_softc *, int);
728 int mps_mapping_allocate_memory(struct mps_softc *sc);
729 unsigned int mps_mapping_get_sas_id(struct mps_softc *, uint64_t , u16);
730 unsigned int mps_mapping_get_sas_id_from_handle(struct mps_softc *sc,
731     u16 handle);
732 unsigned int mps_mapping_get_raid_id(struct mps_softc *sc, u64 wwid,
733     u16 handle);
734 unsigned int mps_mapping_get_raid_id_from_handle(struct mps_softc *sc,
735     u16 volHandle);
736 void mps_mapping_enclosure_dev_status_change_event(struct mps_softc *,
737     Mpi2EventDataSasEnclDevStatusChange_t *event_data);
738 void mps_mapping_ir_config_change_event(struct mps_softc *sc,
739     Mpi2EventDataIrConfigChangeList_t *event_data);
740 
741 void mpssas_evt_handler(struct mps_softc *sc, uintptr_t data,
742     MPI2_EVENT_NOTIFICATION_REPLY *event);
743 void mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle);
744 void mpssas_prepare_volume_remove(struct mpssas_softc *sassc, uint16_t handle);
745 int mpssas_startup(struct mps_softc *sc);
746 struct mpssas_target * mpssas_find_target_by_handle(struct mpssas_softc *, int, uint16_t);
747 
748 SYSCTL_DECL(_hw_mps);
749 
750 /* Compatibility shims for different OS versions */
751 #if __FreeBSD_version >= 800001
752 #define mps_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
753     kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
754 #define mps_kproc_exit(arg)	kproc_exit(arg)
755 #else
756 #define mps_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
757     kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
758 #define mps_kproc_exit(arg)	kthread_exit(arg)
759 #endif
760 
761 #if defined(CAM_PRIORITY_XPT)
762 #define MPS_PRIORITY_XPT	CAM_PRIORITY_XPT
763 #else
764 #define MPS_PRIORITY_XPT	5
765 #endif
766 
767 #if __FreeBSD_version < 800107
768 // Prior to FreeBSD-8.0 scp3_flags was not defined.
769 #define spc3_flags reserved
770 
771 #define SPC3_SID_PROTECT    0x01
772 #define SPC3_SID_3PC        0x08
773 #define SPC3_SID_TPGS_MASK  0x30
774 #define SPC3_SID_TPGS_IMPLICIT  0x10
775 #define SPC3_SID_TPGS_EXPLICIT  0x20
776 #define SPC3_SID_ACC        0x40
777 #define SPC3_SID_SCCS       0x80
778 
779 #define CAM_PRIORITY_NORMAL CAM_PRIORITY_NONE
780 #endif
781 
782 #endif
783 
784