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