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