xref: /freebsd/sys/dev/mpr/mprvar.h (revision af6a5351a1fdb1130f18be6c782c4d48916eb971)
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * Copyright (c) 2011-2015 LSI Corp.
4  * Copyright (c) 2013-2016 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 _MPRVAR_H
34 #define _MPRVAR_H
35 
36 #define MPR_DRIVER_VERSION	"13.01.00.00-fbsd"
37 
38 #define MPR_DB_MAX_WAIT		2500
39 
40 #define MPR_REQ_FRAMES		1024
41 #define MPR_EVT_REPLY_FRAMES	32
42 #define MPR_REPLY_FRAMES	MPR_REQ_FRAMES
43 #define MPR_CHAIN_FRAMES	2048
44 #define MPR_MAXIO_PAGES		(-1)
45 #define MPR_SENSE_LEN		SSD_FULL_SIZE
46 #define MPR_MSI_COUNT		1
47 #define MPR_SGE64_SIZE		12
48 #define MPR_SGE32_SIZE		8
49 #define MPR_SGC_SIZE		8
50 #define MPR_DEFAULT_CHAIN_SEG_SIZE	8
51 #define MPR_MAX_CHAIN_ELEMENT_SIZE	16
52 
53 #define MPR_FUNCTRACE(sc)			\
54 	mpr_dprint((sc), MPR_TRACE, "%s\n", __func__)
55 
56 #define	CAN_SLEEP			1
57 #define	NO_SLEEP			0
58 
59 #define MPR_PERIODIC_DELAY	1	/* 1 second heartbeat/watchdog check */
60 #define MPR_ATA_ID_TIMEOUT	5	/* 5 second timeout for SATA ID cmd */
61 
62 #define	IFAULT_IOP_OVER_TEMP_THRESHOLD_EXCEEDED	0x2810
63 
64 #define MPR_SCSI_RI_INVALID_FRAME	(0x00000002)
65 #define MPR_STRING_LENGTH               64
66 
67 #define DEFAULT_SPINUP_WAIT	3	/* seconds to wait for spinup */
68 
69 #include <sys/endian.h>
70 
71 /*
72  * host mapping related macro definitions
73  */
74 #define MPR_MAPTABLE_BAD_IDX	0xFFFFFFFF
75 #define MPR_DPM_BAD_IDX		0xFFFF
76 #define MPR_ENCTABLE_BAD_IDX	0xFF
77 #define MPR_MAX_MISSING_COUNT	0x0F
78 #define MPR_DEV_RESERVED	0x20000000
79 #define MPR_MAP_IN_USE		0x10000000
80 #define MPR_RAID_CHANNEL	1
81 #define MPR_MAP_BAD_ID		0xFFFFFFFF
82 
83 typedef uint8_t u8;
84 typedef uint16_t u16;
85 typedef uint32_t u32;
86 typedef uint64_t u64;
87 
88 /**
89  * struct dev_mapping_table - device mapping information
90  * @physical_id: SAS address for drives or WWID for RAID volumes
91  * @device_info: bitfield provides detailed info about the device
92  * @phy_bits: bitfields indicating controller phys
93  * @dpm_entry_num: index of this device in device persistent map table
94  * @dev_handle: device handle for the device pointed by this entry
95  * @channel: target channel
96  * @id: target id
97  * @missing_count: number of times the device not detected by driver
98  * @hide_flag: Hide this physical disk/not (foreign configuration)
99  * @init_complete: Whether the start of the day checks completed or not
100  * @TLR_bits: Turn TLR support on or off
101  */
102 struct dev_mapping_table {
103 	u64	physical_id;
104 	u32	device_info;
105 	u32	phy_bits;
106 	u16	dpm_entry_num;
107 	u16	dev_handle;
108 	u8	reserved1;
109 	u8	channel;
110 	u16	id;
111 	u8	missing_count;
112 	u8	init_complete;
113 	u8	TLR_bits;
114 	u8	reserved2;
115 };
116 
117 /**
118  * struct enc_mapping_table -  mapping information about an enclosure
119  * @enclosure_id: Logical ID of this enclosure
120  * @start_index: index to the entry in dev_mapping_table
121  * @phy_bits: bitfields indicating controller phys
122  * @dpm_entry_num: index of this enclosure in device persistent map table
123  * @enc_handle: device handle for the enclosure pointed by this entry
124  * @num_slots: number of slots in the enclosure
125  * @start_slot: Starting slot id
126  * @missing_count: number of times the device not detected by driver
127  * @removal_flag: used to mark the device for removal
128  * @skip_search: used as a flag to include/exclude enclosure for search
129  * @init_complete: Whether the start of the day checks completed or not
130  */
131 struct enc_mapping_table {
132 	u64	enclosure_id;
133 	u32	start_index;
134 	u32	phy_bits;
135 	u16	dpm_entry_num;
136 	u16	enc_handle;
137 	u16	num_slots;
138 	u16	start_slot;
139 	u8	missing_count;
140 	u8	removal_flag;
141 	u8	skip_search;
142 	u8	init_complete;
143 };
144 
145 /**
146  * struct map_removal_table - entries to be removed from mapping table
147  * @dpm_entry_num: index of this device in device persistent map table
148  * @dev_handle: device handle for the device pointed by this entry
149  */
150 struct map_removal_table{
151 	u16	dpm_entry_num;
152 	u16	dev_handle;
153 };
154 
155 typedef struct mpr_fw_diagnostic_buffer {
156 	size_t		size;
157 	uint8_t		extended_type;
158 	uint8_t		buffer_type;
159 	uint8_t		force_release;
160 	uint32_t	product_specific[23];
161 	uint8_t		immediate;
162 	uint8_t		enabled;
163 	uint8_t		valid_data;
164 	uint8_t		owned_by_firmware;
165 	uint32_t	unique_id;
166 } mpr_fw_diagnostic_buffer_t;
167 
168 struct mpr_softc;
169 struct mpr_command;
170 struct mprsas_softc;
171 union ccb;
172 struct mprsas_target;
173 struct mpr_column_map;
174 
175 MALLOC_DECLARE(M_MPR);
176 
177 typedef void mpr_evt_callback_t(struct mpr_softc *, uintptr_t,
178     MPI2_EVENT_NOTIFICATION_REPLY *reply);
179 typedef void mpr_command_callback_t(struct mpr_softc *, struct mpr_command *cm);
180 
181 struct mpr_chain {
182 	TAILQ_ENTRY(mpr_chain)		chain_link;
183 	void				*chain;
184 	uint64_t			chain_busaddr;
185 };
186 
187 /*
188  * This needs to be at least 2 to support SMP passthrough.
189  */
190 #define       MPR_IOVEC_COUNT 2
191 
192 struct mpr_command {
193 	TAILQ_ENTRY(mpr_command)	cm_link;
194 	TAILQ_ENTRY(mpr_command)	cm_recovery;
195 	struct mpr_softc		*cm_sc;
196 	union ccb			*cm_ccb;
197 	void				*cm_data;
198 	u_int				cm_length;
199 	u_int				cm_out_len;
200 	struct uio			cm_uio;
201 	struct iovec			cm_iovec[MPR_IOVEC_COUNT];
202 	u_int				cm_max_segs;
203 	u_int				cm_sglsize;
204 	void				*cm_sge;
205 	uint8_t				*cm_req;
206 	uint8_t				*cm_reply;
207 	uint32_t			cm_reply_data;
208 	mpr_command_callback_t		*cm_complete;
209 	void				*cm_complete_data;
210 	struct mprsas_target		*cm_targ;
211 	MPI2_REQUEST_DESCRIPTOR_UNION	cm_desc;
212 	u_int	                	cm_lun;
213 	u_int				cm_flags;
214 #define MPR_CM_FLAGS_POLLED		(1 << 0)
215 #define MPR_CM_FLAGS_COMPLETE		(1 << 1)
216 #define MPR_CM_FLAGS_SGE_SIMPLE		(1 << 2)
217 #define MPR_CM_FLAGS_DATAOUT		(1 << 3)
218 #define MPR_CM_FLAGS_DATAIN		(1 << 4)
219 #define MPR_CM_FLAGS_WAKEUP		(1 << 5)
220 #define MPR_CM_FLAGS_USE_UIO		(1 << 6)
221 #define MPR_CM_FLAGS_SMP_PASS		(1 << 7)
222 #define	MPR_CM_FLAGS_CHAIN_FAILED	(1 << 8)
223 #define	MPR_CM_FLAGS_ERROR_MASK		MPR_CM_FLAGS_CHAIN_FAILED
224 #define	MPR_CM_FLAGS_USE_CCB		(1 << 9)
225 #define	MPR_CM_FLAGS_SATA_ID_TIMEOUT	(1 << 10)
226 	u_int				cm_state;
227 #define MPR_CM_STATE_FREE		0
228 #define MPR_CM_STATE_BUSY		1
229 #define MPR_CM_STATE_TIMEDOUT		2
230 	bus_dmamap_t			cm_dmamap;
231 	struct scsi_sense_data		*cm_sense;
232 	TAILQ_HEAD(, mpr_chain)		cm_chain_list;
233 	uint32_t			cm_req_busaddr;
234 	uint32_t			cm_sense_busaddr;
235 	struct callout			cm_callout;
236 };
237 
238 struct mpr_column_map {
239 	uint16_t			dev_handle;
240 	uint8_t				phys_disk_num;
241 };
242 
243 struct mpr_event_handle {
244 	TAILQ_ENTRY(mpr_event_handle)	eh_list;
245 	mpr_evt_callback_t		*callback;
246 	void				*data;
247 	uint8_t				mask[16];
248 };
249 
250 struct mpr_softc {
251 	device_t			mpr_dev;
252 	struct cdev			*mpr_cdev;
253 	u_int				mpr_flags;
254 #define MPR_FLAGS_INTX		(1 << 0)
255 #define MPR_FLAGS_MSI		(1 << 1)
256 #define MPR_FLAGS_BUSY		(1 << 2)
257 #define MPR_FLAGS_SHUTDOWN	(1 << 3)
258 #define MPR_FLAGS_DIAGRESET	(1 << 4)
259 #define	MPR_FLAGS_ATTACH_DONE	(1 << 5)
260 	u_int				mpr_debug;
261 	u_int				disable_msix;
262 	u_int				disable_msi;
263 	int				tm_cmds_active;
264 	int				io_cmds_active;
265 	int				io_cmds_highwater;
266 	int				chain_free;
267 	int				max_chains;
268 	int				max_io_pages;
269 	int				chain_free_lowwater;
270 	uint32_t			chain_frame_size;
271 	uint16_t			chain_seg_size;
272 	u_int				enable_ssu;
273 	int				spinup_wait_time;
274 	int				use_phynum;
275 	uint64_t			chain_alloc_fail;
276 	struct sysctl_ctx_list		sysctl_ctx;
277 	struct sysctl_oid		*sysctl_tree;
278 	char                            fw_version[16];
279 	struct mpr_command		*commands;
280 	struct mpr_chain		*chains;
281 	struct callout			periodic;
282 
283 	struct mprsas_softc		*sassc;
284 	char            tmp_string[MPR_STRING_LENGTH];
285 	TAILQ_HEAD(, mpr_command)	req_list;
286 	TAILQ_HEAD(, mpr_command)	high_priority_req_list;
287 	TAILQ_HEAD(, mpr_chain)		chain_list;
288 	TAILQ_HEAD(, mpr_command)	tm_list;
289 	int				replypostindex;
290 	int				replyfreeindex;
291 
292 	struct resource			*mpr_regs_resource;
293 	bus_space_handle_t		mpr_bhandle;
294 	bus_space_tag_t			mpr_btag;
295 	int				mpr_regs_rid;
296 
297 	bus_dma_tag_t			mpr_parent_dmat;
298 	bus_dma_tag_t			buffer_dmat;
299 
300 	MPI2_IOC_FACTS_REPLY		*facts;
301 	int				num_reqs;
302 	int				num_replies;
303 	int				fqdepth;	/* Free queue */
304 	int				pqdepth;	/* Post queue */
305 
306 	uint8_t				event_mask[16];
307 	TAILQ_HEAD(, mpr_event_handle)	event_list;
308 	struct mpr_event_handle		*mpr_log_eh;
309 
310 	struct mtx			mpr_mtx;
311 	struct intr_config_hook		mpr_ich;
312 	struct resource			*mpr_irq[MPR_MSI_COUNT];
313 	void				*mpr_intrhand[MPR_MSI_COUNT];
314 	int				mpr_irq_rid[MPR_MSI_COUNT];
315 
316 	uint8_t				*req_frames;
317 	bus_addr_t			req_busaddr;
318 	bus_dma_tag_t			req_dmat;
319 	bus_dmamap_t			req_map;
320 
321 	uint8_t				*reply_frames;
322 	bus_addr_t			reply_busaddr;
323 	bus_dma_tag_t			reply_dmat;
324 	bus_dmamap_t			reply_map;
325 
326 	struct scsi_sense_data		*sense_frames;
327 	bus_addr_t			sense_busaddr;
328 	bus_dma_tag_t			sense_dmat;
329 	bus_dmamap_t			sense_map;
330 
331 	uint8_t				*chain_frames;
332 	bus_addr_t			chain_busaddr;
333 	bus_dma_tag_t			chain_dmat;
334 	bus_dmamap_t			chain_map;
335 
336 	MPI2_REPLY_DESCRIPTORS_UNION	*post_queue;
337 	bus_addr_t			post_busaddr;
338 	uint32_t			*free_queue;
339 	bus_addr_t			free_busaddr;
340 	bus_dma_tag_t			queues_dmat;
341 	bus_dmamap_t			queues_map;
342 
343 	uint8_t				*fw_diag_buffer;
344 	bus_addr_t			fw_diag_busaddr;
345 	bus_dma_tag_t			fw_diag_dmat;
346 	bus_dmamap_t			fw_diag_map;
347 
348 	uint8_t				ir_firmware;
349 
350 	/* static config pages */
351 	Mpi2IOCPage8_t			ioc_pg8;
352 	Mpi2IOUnitPage8_t		iounit_pg8;
353 
354 	/* host mapping support */
355 	struct dev_mapping_table	*mapping_table;
356 	struct enc_mapping_table	*enclosure_table;
357 	struct map_removal_table	*removal_table;
358 	uint8_t				*dpm_entry_used;
359 	uint8_t				*dpm_flush_entry;
360 	Mpi2DriverMappingPage0_t	*dpm_pg0;
361 	uint16_t			max_devices;
362 	uint16_t			max_enclosures;
363 	uint16_t			max_expanders;
364 	uint8_t				max_volumes;
365 	uint8_t				num_enc_table_entries;
366 	uint8_t				num_rsvd_entries;
367 	uint8_t				num_channels;
368 	uint16_t			max_dpm_entries;
369 	uint8_t				is_dpm_enable;
370 	uint8_t				track_mapping_events;
371 	uint32_t			pending_map_events;
372 	uint8_t				mt_full_retry;
373 	uint8_t				mt_add_device_failed;
374 
375 	/* FW diag Buffer List */
376 	mpr_fw_diagnostic_buffer_t
377 				fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_COUNT];
378 
379 	/* Event Recording IOCTL support */
380 	uint32_t			events_to_record[4];
381 	mpr_event_entry_t		recorded_events[MPR_EVENT_QUEUE_SIZE];
382 	uint8_t				event_index;
383 	uint32_t			event_number;
384 
385 	/* EEDP and TLR support */
386 	uint8_t				eedp_enabled;
387 	uint8_t				control_TLR;
388 
389 	/* Shutdown Event Handler */
390 	eventhandler_tag		shutdown_eh;
391 
392 	/* To track topo events during reset */
393 #define	MPR_DIAG_RESET_TIMEOUT	300000
394 	uint8_t				wait_for_port_enable;
395 	uint8_t				port_enable_complete;
396 	uint8_t				msleep_fake_chan;
397 
398 	/* StartStopUnit command handling at shutdown */
399 	uint32_t			SSU_refcount;
400 	uint8_t				SSU_started;
401 
402 	char				exclude_ids[80];
403 	struct timeval			lastfail;
404 };
405 
406 struct mpr_config_params {
407 	MPI2_CONFIG_EXT_PAGE_HEADER_UNION	hdr;
408 	u_int		action;
409 	u_int		page_address;	/* Attributes, not a phys address */
410 	u_int		status;
411 	void		*buffer;
412 	u_int		length;
413 	int		timeout;
414 	void		(*callback)(struct mpr_softc *, struct mpr_config_params *);
415 	void		*cbdata;
416 };
417 
418 struct scsi_read_capacity_eedp
419 {
420 	uint8_t addr[8];
421 	uint8_t length[4];
422 	uint8_t protect;
423 };
424 
425 static __inline uint32_t
426 mpr_regread(struct mpr_softc *sc, uint32_t offset)
427 {
428 	return (bus_space_read_4(sc->mpr_btag, sc->mpr_bhandle, offset));
429 }
430 
431 static __inline void
432 mpr_regwrite(struct mpr_softc *sc, uint32_t offset, uint32_t val)
433 {
434 	bus_space_write_4(sc->mpr_btag, sc->mpr_bhandle, offset, val);
435 }
436 
437 /* free_queue must have Little Endian address
438  * TODO- cm_reply_data is unwanted. We can remove it.
439  * */
440 static __inline void
441 mpr_free_reply(struct mpr_softc *sc, uint32_t busaddr)
442 {
443 	if (++sc->replyfreeindex >= sc->fqdepth)
444 		sc->replyfreeindex = 0;
445 	sc->free_queue[sc->replyfreeindex] = htole32(busaddr);
446 	mpr_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
447 }
448 
449 static __inline struct mpr_chain *
450 mpr_alloc_chain(struct mpr_softc *sc)
451 {
452 	struct mpr_chain *chain;
453 
454 	if ((chain = TAILQ_FIRST(&sc->chain_list)) != NULL) {
455 		TAILQ_REMOVE(&sc->chain_list, chain, chain_link);
456 		sc->chain_free--;
457 		if (sc->chain_free < sc->chain_free_lowwater)
458 			sc->chain_free_lowwater = sc->chain_free;
459 	} else
460 		sc->chain_alloc_fail++;
461 	return (chain);
462 }
463 
464 static __inline void
465 mpr_free_chain(struct mpr_softc *sc, struct mpr_chain *chain)
466 {
467 #if 0
468 	bzero(chain->chain, 128);
469 #endif
470 	sc->chain_free++;
471 	TAILQ_INSERT_TAIL(&sc->chain_list, chain, chain_link);
472 }
473 
474 static __inline void
475 mpr_free_command(struct mpr_softc *sc, struct mpr_command *cm)
476 {
477 	struct mpr_chain *chain, *chain_temp;
478 
479 	if (cm->cm_reply != NULL)
480 		mpr_free_reply(sc, cm->cm_reply_data);
481 	cm->cm_reply = NULL;
482 	cm->cm_flags = 0;
483 	cm->cm_complete = NULL;
484 	cm->cm_complete_data = NULL;
485 	cm->cm_ccb = NULL;
486 	cm->cm_targ = NULL;
487 	cm->cm_max_segs = 0;
488 	cm->cm_lun = 0;
489 	cm->cm_state = MPR_CM_STATE_FREE;
490 	cm->cm_data = NULL;
491 	cm->cm_length = 0;
492 	cm->cm_out_len = 0;
493 	cm->cm_sglsize = 0;
494 	cm->cm_sge = NULL;
495 
496 	TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
497 		TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
498 		mpr_free_chain(sc, chain);
499 	}
500 	TAILQ_INSERT_TAIL(&sc->req_list, cm, cm_link);
501 }
502 
503 static __inline struct mpr_command *
504 mpr_alloc_command(struct mpr_softc *sc)
505 {
506 	struct mpr_command *cm;
507 
508 	cm = TAILQ_FIRST(&sc->req_list);
509 	if (cm == NULL)
510 		return (NULL);
511 
512 	TAILQ_REMOVE(&sc->req_list, cm, cm_link);
513 	KASSERT(cm->cm_state == MPR_CM_STATE_FREE, ("mpr: Allocating busy command\n"));
514 	cm->cm_state = MPR_CM_STATE_BUSY;
515 	return (cm);
516 }
517 
518 static __inline void
519 mpr_free_high_priority_command(struct mpr_softc *sc, struct mpr_command *cm)
520 {
521 	struct mpr_chain *chain, *chain_temp;
522 
523 	if (cm->cm_reply != NULL)
524 		mpr_free_reply(sc, cm->cm_reply_data);
525 	cm->cm_reply = NULL;
526 	cm->cm_flags = 0;
527 	cm->cm_complete = NULL;
528 	cm->cm_complete_data = NULL;
529 	cm->cm_ccb = NULL;
530 	cm->cm_targ = NULL;
531 	cm->cm_lun = 0;
532 	cm->cm_state = MPR_CM_STATE_FREE;
533 	TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
534 		TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
535 		mpr_free_chain(sc, chain);
536 	}
537 	TAILQ_INSERT_TAIL(&sc->high_priority_req_list, cm, cm_link);
538 }
539 
540 static __inline struct mpr_command *
541 mpr_alloc_high_priority_command(struct mpr_softc *sc)
542 {
543 	struct mpr_command *cm;
544 
545 	cm = TAILQ_FIRST(&sc->high_priority_req_list);
546 	if (cm == NULL)
547 		return (NULL);
548 
549 	TAILQ_REMOVE(&sc->high_priority_req_list, cm, cm_link);
550 	KASSERT(cm->cm_state == MPR_CM_STATE_FREE, ("mpr: Allocating busy command\n"));
551 	cm->cm_state = MPR_CM_STATE_BUSY;
552 	return (cm);
553 }
554 
555 static __inline void
556 mpr_lock(struct mpr_softc *sc)
557 {
558 	mtx_lock(&sc->mpr_mtx);
559 }
560 
561 static __inline void
562 mpr_unlock(struct mpr_softc *sc)
563 {
564 	mtx_unlock(&sc->mpr_mtx);
565 }
566 
567 #define MPR_INFO	(1 << 0)	/* Basic info */
568 #define MPR_FAULT	(1 << 1)	/* Hardware faults */
569 #define MPR_EVENT	(1 << 2)	/* Event data from the controller */
570 #define MPR_LOG		(1 << 3)	/* Log data from the controller */
571 #define MPR_RECOVERY	(1 << 4)	/* Command error recovery tracing */
572 #define MPR_ERROR	(1 << 5)	/* Parameter errors, programming bugs */
573 #define MPR_INIT	(1 << 6)	/* Things related to system init */
574 #define MPR_XINFO	(1 << 7)	/* More detailed/noisy info */
575 #define MPR_USER	(1 << 8)	/* Trace user-generated commands */
576 #define MPR_MAPPING	(1 << 9)	/* Trace device mappings */
577 #define MPR_TRACE	(1 << 10)	/* Function-by-function trace */
578 
579 #define	MPR_SSU_DISABLE_SSD_DISABLE_HDD	0
580 #define	MPR_SSU_ENABLE_SSD_DISABLE_HDD	1
581 #define	MPR_SSU_DISABLE_SSD_ENABLE_HDD	2
582 #define	MPR_SSU_ENABLE_SSD_ENABLE_HDD	3
583 
584 #define mpr_printf(sc, args...)				\
585 	device_printf((sc)->mpr_dev, ##args)
586 
587 #define mpr_print_field(sc, msg, args...)		\
588 	printf("\t" msg, ##args)
589 
590 #define mpr_vprintf(sc, args...)			\
591 do {							\
592 	if (bootverbose)				\
593 		mpr_printf(sc, ##args);			\
594 } while (0)
595 
596 #define mpr_dprint(sc, level, msg, args...)		\
597 do {							\
598 	if ((sc)->mpr_debug & (level))			\
599 		device_printf((sc)->mpr_dev, msg, ##args);	\
600 } while (0)
601 
602 #define MPR_PRINTFIELD_START(sc, tag...)	\
603 	mpr_printf((sc), ##tag);		\
604 	mpr_print_field((sc), ":\n")
605 #define MPR_PRINTFIELD_END(sc, tag)		\
606 	mpr_printf((sc), tag "\n")
607 #define MPR_PRINTFIELD(sc, facts, attr, fmt)	\
608 	mpr_print_field((sc), #attr ": " #fmt "\n", (facts)->attr)
609 
610 static __inline void
611 mpr_from_u64(uint64_t data, U64 *mpr)
612 {
613 	(mpr)->High = htole32((uint32_t)((data) >> 32));
614 	(mpr)->Low = htole32((uint32_t)((data) & 0xffffffff));
615 }
616 
617 static __inline uint64_t
618 mpr_to_u64(U64 *data)
619 {
620 	return (((uint64_t)le32toh(data->High) << 32) | le32toh(data->Low));
621 }
622 
623 static __inline void
624 mpr_mask_intr(struct mpr_softc *sc)
625 {
626 	uint32_t mask;
627 
628 	mask = mpr_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
629 	mask |= MPI2_HIM_REPLY_INT_MASK;
630 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
631 }
632 
633 static __inline void
634 mpr_unmask_intr(struct mpr_softc *sc)
635 {
636 	uint32_t mask;
637 
638 	mask = mpr_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
639 	mask &= ~MPI2_HIM_REPLY_INT_MASK;
640 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
641 }
642 
643 int mpr_pci_setup_interrupts(struct mpr_softc *sc);
644 int mpr_pci_restore(struct mpr_softc *sc);
645 
646 int mpr_attach(struct mpr_softc *sc);
647 int mpr_free(struct mpr_softc *sc);
648 void mpr_intr(void *);
649 void mpr_intr_msi(void *);
650 void mpr_intr_locked(void *);
651 int mpr_register_events(struct mpr_softc *, uint8_t *, mpr_evt_callback_t *,
652     void *, struct mpr_event_handle **);
653 int mpr_restart(struct mpr_softc *);
654 int mpr_update_events(struct mpr_softc *, struct mpr_event_handle *, uint8_t *);
655 int mpr_deregister_events(struct mpr_softc *, struct mpr_event_handle *);
656 int mpr_push_sge(struct mpr_command *, MPI2_SGE_SIMPLE64 *, size_t, int);
657 int mpr_push_ieee_sge(struct mpr_command *, void *, int);
658 int mpr_add_dmaseg(struct mpr_command *, vm_paddr_t, size_t, u_int, int);
659 int mpr_attach_sas(struct mpr_softc *sc);
660 int mpr_detach_sas(struct mpr_softc *sc);
661 int mpr_read_config_page(struct mpr_softc *, struct mpr_config_params *);
662 int mpr_write_config_page(struct mpr_softc *, struct mpr_config_params *);
663 void mpr_memaddr_cb(void *, bus_dma_segment_t *, int , int );
664 void mpr_init_sge(struct mpr_command *cm, void *req, void *sge);
665 int mpr_attach_user(struct mpr_softc *);
666 void mpr_detach_user(struct mpr_softc *);
667 void mprsas_record_event(struct mpr_softc *sc,
668     MPI2_EVENT_NOTIFICATION_REPLY *event_reply);
669 
670 int mpr_map_command(struct mpr_softc *sc, struct mpr_command *cm);
671 int mpr_wait_command(struct mpr_softc *sc, struct mpr_command *cm, int timeout,
672     int sleep_flag);
673 int mpr_request_polled(struct mpr_softc *sc, struct mpr_command *cm);
674 
675 int mpr_config_get_bios_pg3(struct mpr_softc *sc, Mpi2ConfigReply_t
676     *mpi_reply, Mpi2BiosPage3_t *config_page);
677 int mpr_config_get_raid_volume_pg0(struct mpr_softc *sc, Mpi2ConfigReply_t
678     *mpi_reply, Mpi2RaidVolPage0_t *config_page, u32 page_address);
679 int mpr_config_get_ioc_pg8(struct mpr_softc *sc, Mpi2ConfigReply_t *,
680     Mpi2IOCPage8_t *);
681 int mpr_config_get_iounit_pg8(struct mpr_softc *sc,
682     Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage8_t *config_page);
683 int mpr_config_get_sas_device_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
684     Mpi2SasDevicePage0_t *, u32 , u16 );
685 int mpr_config_get_dpm_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
686     Mpi2DriverMappingPage0_t *, u16 );
687 int mpr_config_get_raid_volume_pg1(struct mpr_softc *sc,
688     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage1_t *config_page, u32 form,
689     u16 handle);
690 int mpr_config_get_volume_wwid(struct mpr_softc *sc, u16 volume_handle,
691     u64 *wwid);
692 int mpr_config_get_raid_pd_pg0(struct mpr_softc *sc,
693     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidPhysDiskPage0_t *config_page,
694     u32 page_address);
695 void mprsas_ir_shutdown(struct mpr_softc *sc);
696 
697 int mpr_reinit(struct mpr_softc *sc);
698 void mprsas_handle_reinit(struct mpr_softc *sc);
699 
700 void mpr_base_static_config_pages(struct mpr_softc *sc);
701 
702 int mpr_mapping_initialize(struct mpr_softc *);
703 void mpr_mapping_topology_change_event(struct mpr_softc *,
704     Mpi2EventDataSasTopologyChangeList_t *);
705 int mpr_mapping_is_reinit_required(struct mpr_softc *);
706 void mpr_mapping_free_memory(struct mpr_softc *sc);
707 int mpr_config_set_dpm_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
708     Mpi2DriverMappingPage0_t *, u16 );
709 void mpr_mapping_exit(struct mpr_softc *);
710 void mpr_mapping_check_devices(struct mpr_softc *, int);
711 int mpr_mapping_allocate_memory(struct mpr_softc *sc);
712 unsigned int mpr_mapping_get_sas_id(struct mpr_softc *, uint64_t , u16);
713 unsigned int mpr_mapping_get_sas_id_from_handle(struct mpr_softc *sc,
714     u16 handle);
715 unsigned int mpr_mapping_get_raid_id(struct mpr_softc *sc, u64 wwid,
716     u16 handle);
717 unsigned int mpr_mapping_get_raid_id_from_handle(struct mpr_softc *sc,
718     u16 volHandle);
719 void mpr_mapping_enclosure_dev_status_change_event(struct mpr_softc *,
720     Mpi2EventDataSasEnclDevStatusChange_t *event_data);
721 void mpr_mapping_ir_config_change_event(struct mpr_softc *sc,
722     Mpi2EventDataIrConfigChangeList_t *event_data);
723 
724 void mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
725     MPI2_EVENT_NOTIFICATION_REPLY *event);
726 void mprsas_prepare_remove(struct mprsas_softc *sassc, uint16_t handle);
727 void mprsas_prepare_volume_remove(struct mprsas_softc *sassc, uint16_t handle);
728 int mprsas_startup(struct mpr_softc *sc);
729 struct mprsas_target * mprsas_find_target_by_handle(struct mprsas_softc *, int,
730     uint16_t);
731 void mprsas_realloc_targets(struct mpr_softc *sc, int maxtargets);
732 struct mpr_command * mprsas_alloc_tm(struct mpr_softc *sc);
733 void mprsas_free_tm(struct mpr_softc *sc, struct mpr_command *tm);
734 void mprsas_release_simq_reinit(struct mprsas_softc *sassc);
735 int mprsas_send_reset(struct mpr_softc *sc, struct mpr_command *tm,
736     uint8_t type);
737 
738 SYSCTL_DECL(_hw_mpr);
739 
740 /* Compatibility shims for different OS versions */
741 #if __FreeBSD_version >= 800001
742 #define mpr_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
743     kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
744 #define mpr_kproc_exit(arg)	kproc_exit(arg)
745 #else
746 #define mpr_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
747     kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
748 #define mpr_kproc_exit(arg)	kthread_exit(arg)
749 #endif
750 
751 #if defined(CAM_PRIORITY_XPT)
752 #define MPR_PRIORITY_XPT	CAM_PRIORITY_XPT
753 #else
754 #define MPR_PRIORITY_XPT	5
755 #endif
756 
757 #if __FreeBSD_version < 800107
758 // Prior to FreeBSD-8.0 scp3_flags was not defined.
759 #define spc3_flags reserved
760 
761 #define SPC3_SID_PROTECT    0x01
762 #define SPC3_SID_3PC        0x08
763 #define SPC3_SID_TPGS_MASK  0x30
764 #define SPC3_SID_TPGS_IMPLICIT  0x10
765 #define SPC3_SID_TPGS_EXPLICIT  0x20
766 #define SPC3_SID_ACC        0x40
767 #define SPC3_SID_SCCS       0x80
768 
769 #define CAM_PRIORITY_NORMAL CAM_PRIORITY_NONE
770 #endif
771 
772 #endif
773 
774