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