xref: /freebsd/sys/dev/mpr/mprvar.h (revision 6966ac055c3b7a39266fb982493330df7a097997)
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * Copyright (c) 2011-2015 LSI Corp.
4  * Copyright (c) 2013-2016 Avago Technologies
5  * Copyright 2000-2020 Broadcom Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
30  *
31  * $FreeBSD$
32  */
33 
34 #ifndef _MPRVAR_H
35 #define _MPRVAR_H
36 
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 
40 #define MPR_DRIVER_VERSION	"23.00.00.00-fbsd"
41 
42 #define MPR_DB_MAX_WAIT		2500
43 
44 #define MPR_REQ_FRAMES		2048
45 #define MPR_PRI_REQ_FRAMES	128
46 #define MPR_EVT_REPLY_FRAMES	32
47 #define MPR_REPLY_FRAMES	MPR_REQ_FRAMES
48 #define MPR_CHAIN_FRAMES	16384
49 #define MPR_MAXIO_PAGES		(-1)
50 #define MPR_SENSE_LEN		SSD_FULL_SIZE
51 #define MPR_MSI_MAX		1
52 #define MPR_MSIX_MAX		96
53 #define MPR_SGE64_SIZE		12
54 #define MPR_SGE32_SIZE		8
55 #define MPR_SGC_SIZE		8
56 #define MPR_DEFAULT_CHAIN_SEG_SIZE	8
57 #define MPR_MAX_CHAIN_ELEMENT_SIZE	16
58 
59 /*
60  * PCIe NVMe Specific defines
61  */
62 //SLM-for now just use the same value as a SAS disk
63 #define NVME_QDEPTH			MPR_REQ_FRAMES
64 #define PRP_ENTRY_SIZE			8
65 #define NVME_CMD_PRP1_OFFSET		24	/* PRP1 offset in NVMe cmd */
66 #define NVME_CMD_PRP2_OFFSET		32	/* PRP2 offset in NVMe cmd */
67 #define NVME_ERROR_RESPONSE_SIZE	16	/* Max NVME Error Response */
68 #define HOST_PAGE_SIZE_4K		12
69 
70 #define MPR_FUNCTRACE(sc)			\
71 	mpr_dprint((sc), MPR_TRACE, "%s\n", __func__)
72 
73 #define	CAN_SLEEP			1
74 #define	NO_SLEEP			0
75 
76 #define MPR_PERIODIC_DELAY	1	/* 1 second heartbeat/watchdog check */
77 #define MPR_ATA_ID_TIMEOUT	5	/* 5 second timeout for SATA ID cmd */
78 #define MPR_MISSING_CHECK_DELAY	10	/* 10 seconds between missing check */
79 
80 #define	IFAULT_IOP_OVER_TEMP_THRESHOLD_EXCEEDED	0x2810
81 
82 #define MPR_SCSI_RI_INVALID_FRAME	(0x00000002)
83 
84 #define DEFAULT_SPINUP_WAIT	3	/* seconds to wait for spinup */
85 
86 #include <sys/endian.h>
87 
88 /*
89  * host mapping related macro definitions
90  */
91 #define MPR_MAPTABLE_BAD_IDX	0xFFFFFFFF
92 #define MPR_DPM_BAD_IDX		0xFFFF
93 #define MPR_ENCTABLE_BAD_IDX	0xFF
94 #define MPR_MAX_MISSING_COUNT	0x0F
95 #define MPR_DEV_RESERVED	0x20000000
96 #define MPR_MAP_IN_USE		0x10000000
97 #define MPR_MAP_BAD_ID		0xFFFFFFFF
98 
99 typedef uint8_t u8;
100 typedef uint16_t u16;
101 typedef uint32_t u32;
102 typedef uint64_t u64;
103 
104 typedef struct _MPI2_CONFIG_PAGE_MAN_11
105 {
106     MPI2_CONFIG_PAGE_HEADER             Header;         	/* 0x00 */
107     U8					FlashTime;		/* 0x04 */
108     U8					NVTime;			/* 0x05 */
109     U16					Flag;			/* 0x06 */
110     U8					RFIoTimeout;		/* 0x08 */
111     U8					EEDPTagMode;		/* 0x09 */
112     U8					AWTValue;		/* 0x0A */
113     U8					Reserve1;		/* 0x0B */
114     U8					MaxCmdFrames;		/* 0x0C */
115     U8					Reserve2;		/* 0x0D */
116     U16					AddlFlags;		/* 0x0E */
117     U32					SysRefClk;		/* 0x10 */
118     U64					Reserve3[3];		/* 0x14 */
119     U16					AddlFlags2;		/* 0x2C */
120     U8					AddlFlags3;		/* 0x2E */
121     U8					Reserve4;		/* 0x2F */
122     U64					opDebugEnable;		/* 0x30 */
123     U64					PlDebugEnable;		/* 0x38 */
124     U64					IrDebugEnable;		/* 0x40 */
125     U32					BoardPowerRequirement;	/* 0x48 */
126     U8					NVMeAbortTO;		/* 0x4C */
127     U8					Reserve5;		/* 0x4D */
128     U16					Reserve6;		/* 0x4E */
129     U32					Reserve7[3];		/* 0x50 */
130 } MPI2_CONFIG_PAGE_MAN_11,
131   MPI2_POINTER PTR_MPI2_CONFIG_PAGE_MAN_11,
132   Mpi2ManufacturingPage11_t, MPI2_POINTER pMpi2ManufacturingPage11_t;
133 
134 #define MPI2_MAN_PG11_ADDLFLAGS2_CUSTOM_TM_HANDLING_MASK	(0x0010)
135 
136 /**
137  * struct dev_mapping_table - device mapping information
138  * @physical_id: SAS address for drives or WWID for RAID volumes
139  * @device_info: bitfield provides detailed info about the device
140  * @phy_bits: bitfields indicating controller phys
141  * @dpm_entry_num: index of this device in device persistent map table
142  * @dev_handle: device handle for the device pointed by this entry
143  * @id: target id
144  * @missing_count: number of times the device not detected by driver
145  * @hide_flag: Hide this physical disk/not (foreign configuration)
146  * @init_complete: Whether the start of the day checks completed or not
147  * @TLR_bits: Turn TLR support on or off
148  */
149 struct dev_mapping_table {
150 	u64	physical_id;
151 	u32	device_info;
152 	u32	phy_bits;
153 	u16	dpm_entry_num;
154 	u16	dev_handle;
155 	u16	reserved1;
156 	u16	id;
157 	u8	missing_count;
158 	u8	init_complete;
159 	u8	TLR_bits;
160 	u8	reserved2;
161 };
162 
163 /**
164  * struct enc_mapping_table -  mapping information about an enclosure
165  * @enclosure_id: Logical ID of this enclosure
166  * @start_index: index to the entry in dev_mapping_table
167  * @phy_bits: bitfields indicating controller phys
168  * @dpm_entry_num: index of this enclosure in device persistent map table
169  * @enc_handle: device handle for the enclosure pointed by this entry
170  * @num_slots: number of slots in the enclosure
171  * @start_slot: Starting slot id
172  * @missing_count: number of times the device not detected by driver
173  * @removal_flag: used to mark the device for removal
174  * @skip_search: used as a flag to include/exclude enclosure for search
175  * @init_complete: Whether the start of the day checks completed or not
176  */
177 struct enc_mapping_table {
178 	u64	enclosure_id;
179 	u32	start_index;
180 	u32	phy_bits;
181 	u16	dpm_entry_num;
182 	u16	enc_handle;
183 	u16	num_slots;
184 	u16	start_slot;
185 	u8	missing_count;
186 	u8	removal_flag;
187 	u8	skip_search;
188 	u8	init_complete;
189 };
190 
191 /**
192  * struct map_removal_table - entries to be removed from mapping table
193  * @dpm_entry_num: index of this device in device persistent map table
194  * @dev_handle: device handle for the device pointed by this entry
195  */
196 struct map_removal_table{
197 	u16	dpm_entry_num;
198 	u16	dev_handle;
199 };
200 
201 typedef struct mpr_fw_diagnostic_buffer {
202 	size_t		size;
203 	uint8_t		extended_type;
204 	uint8_t		buffer_type;
205 	uint8_t		force_release;
206 	uint32_t	product_specific[23];
207 	uint8_t		immediate;
208 	uint8_t		enabled;
209 	uint8_t		valid_data;
210 	uint8_t		owned_by_firmware;
211 	uint32_t	unique_id;
212 } mpr_fw_diagnostic_buffer_t;
213 
214 struct mpr_softc;
215 struct mpr_command;
216 struct mprsas_softc;
217 union ccb;
218 struct mprsas_target;
219 struct mpr_column_map;
220 
221 MALLOC_DECLARE(M_MPR);
222 
223 typedef void mpr_evt_callback_t(struct mpr_softc *, uintptr_t,
224     MPI2_EVENT_NOTIFICATION_REPLY *reply);
225 typedef void mpr_command_callback_t(struct mpr_softc *, struct mpr_command *cm);
226 
227 struct mpr_chain {
228 	TAILQ_ENTRY(mpr_chain)		chain_link;
229 	void				*chain;
230 	uint64_t			chain_busaddr;
231 };
232 
233 struct mpr_prp_page {
234 	TAILQ_ENTRY(mpr_prp_page)	prp_page_link;
235 	uint64_t			*prp_page;
236 	uint64_t			prp_page_busaddr;
237 };
238 
239 /*
240  * This needs to be at least 2 to support SMP passthrough.
241  */
242 #define       MPR_IOVEC_COUNT 2
243 
244 struct mpr_command {
245 	TAILQ_ENTRY(mpr_command)	cm_link;
246 	TAILQ_ENTRY(mpr_command)	cm_recovery;
247 	struct mpr_softc		*cm_sc;
248 	union ccb			*cm_ccb;
249 	void				*cm_data;
250 	u_int				cm_length;
251 	u_int				cm_out_len;
252 	struct uio			cm_uio;
253 	struct iovec			cm_iovec[MPR_IOVEC_COUNT];
254 	u_int				cm_max_segs;
255 	u_int				cm_sglsize;
256 	void				*cm_sge;
257 	uint8_t				*cm_req;
258 	uint8_t				*cm_reply;
259 	uint32_t			cm_reply_data;
260 	mpr_command_callback_t		*cm_complete;
261 	void				*cm_complete_data;
262 	struct mprsas_target		*cm_targ;
263 	MPI2_REQUEST_DESCRIPTOR_UNION	cm_desc;
264 	u_int	                	cm_lun;
265 	u_int				cm_flags;
266 #define MPR_CM_FLAGS_POLLED		(1 << 0)
267 #define MPR_CM_FLAGS_COMPLETE		(1 << 1)
268 #define MPR_CM_FLAGS_SGE_SIMPLE		(1 << 2)
269 #define MPR_CM_FLAGS_DATAOUT		(1 << 3)
270 #define MPR_CM_FLAGS_DATAIN		(1 << 4)
271 #define MPR_CM_FLAGS_WAKEUP		(1 << 5)
272 #define MPR_CM_FLAGS_USE_UIO		(1 << 6)
273 #define MPR_CM_FLAGS_SMP_PASS		(1 << 7)
274 #define	MPR_CM_FLAGS_CHAIN_FAILED	(1 << 8)
275 #define	MPR_CM_FLAGS_ERROR_MASK		MPR_CM_FLAGS_CHAIN_FAILED
276 #define	MPR_CM_FLAGS_USE_CCB		(1 << 9)
277 #define	MPR_CM_FLAGS_SATA_ID_TIMEOUT	(1 << 10)
278 #define MPR_CM_FLAGS_ON_RECOVERY	(1 << 12)
279 #define MPR_CM_FLAGS_TIMEDOUT		(1 << 13)
280 	u_int				cm_state;
281 #define MPR_CM_STATE_FREE		0
282 #define MPR_CM_STATE_BUSY		1
283 #define MPR_CM_STATE_INQUEUE		2
284 	bus_dmamap_t			cm_dmamap;
285 	struct scsi_sense_data		*cm_sense;
286 	uint64_t			*nvme_error_response;
287 	TAILQ_HEAD(, mpr_chain)		cm_chain_list;
288  	TAILQ_HEAD(, mpr_prp_page)	cm_prp_page_list;
289 	uint32_t			cm_req_busaddr;
290 	bus_addr_t			cm_sense_busaddr;
291 	struct callout			cm_callout;
292 	mpr_command_callback_t		*cm_timeout_handler;
293 };
294 
295 struct mpr_column_map {
296 	uint16_t			dev_handle;
297 	uint8_t				phys_disk_num;
298 };
299 
300 struct mpr_event_handle {
301 	TAILQ_ENTRY(mpr_event_handle)	eh_list;
302 	mpr_evt_callback_t		*callback;
303 	void				*data;
304 	uint8_t				mask[16];
305 };
306 
307 struct mpr_busdma_context {
308 	int				completed;
309 	int				abandoned;
310 	int				error;
311 	bus_addr_t			*addr;
312 	struct mpr_softc		*softc;
313 	bus_dmamap_t			buffer_dmamap;
314 	bus_dma_tag_t			buffer_dmat;
315 };
316 
317 struct mpr_queue {
318 	struct mpr_softc		*sc;
319 	int				qnum;
320 	MPI2_REPLY_DESCRIPTORS_UNION	*post_queue;
321 	int				replypostindex;
322 #ifdef notyet
323 	ck_ring_buffer_t		*ringmem;
324 	ck_ring_buffer_t		*chainmem;
325 	ck_ring_t			req_ring;
326 	ck_ring_t			chain_ring;
327 #endif
328 	bus_dma_tag_t			buffer_dmat;
329 	int				io_cmds_highwater;
330 	int				chain_free_lowwater;
331 	int				chain_alloc_fail;
332 	struct resource			*irq;
333 	void				*intrhand;
334 	int				irq_rid;
335 };
336 
337 struct mpr_softc {
338 	device_t			mpr_dev;
339 	struct cdev			*mpr_cdev;
340 	u_int				mpr_flags;
341 #define MPR_FLAGS_INTX		(1 << 0)
342 #define MPR_FLAGS_MSI		(1 << 1)
343 #define MPR_FLAGS_BUSY		(1 << 2)
344 #define MPR_FLAGS_SHUTDOWN	(1 << 3)
345 #define MPR_FLAGS_DIAGRESET	(1 << 4)
346 #define	MPR_FLAGS_ATTACH_DONE	(1 << 5)
347 #define	MPR_FLAGS_GEN35_IOC	(1 << 6)
348 #define	MPR_FLAGS_REALLOCATED	(1 << 7)
349 #define	MPR_FLAGS_SEA_IOC	(1 << 8)
350 	u_int				mpr_debug;
351 	int				msi_msgs;
352 	u_int				reqframesz;
353 	u_int				replyframesz;
354 	u_int				atomic_desc_capable;
355 	int				tm_cmds_active;
356 	int				io_cmds_active;
357 	int				io_cmds_highwater;
358 	int				chain_free;
359 	int				max_chains;
360 	int				max_io_pages;
361 	u_int				maxio;
362 	int				chain_free_lowwater;
363 	uint32_t			chain_frame_size;
364 	int				prp_buffer_size;
365 	int				prp_pages_free;
366 	int				prp_pages_free_lowwater;
367 	u_int				enable_ssu;
368 	int				spinup_wait_time;
369 	int				use_phynum;
370 	uint64_t			chain_alloc_fail;
371 	uint64_t			prp_page_alloc_fail;
372 	struct sysctl_ctx_list		sysctl_ctx;
373 	struct sysctl_oid		*sysctl_tree;
374 	char                            fw_version[16];
375 	struct mpr_command		*commands;
376 	struct mpr_chain		*chains;
377 	struct mpr_prp_page		*prps;
378 	struct callout			periodic;
379 	struct callout			device_check_callout;
380 	struct mpr_queue		*queues;
381 
382 	struct mprsas_softc		*sassc;
383 	TAILQ_HEAD(, mpr_command)	req_list;
384 	TAILQ_HEAD(, mpr_command)	high_priority_req_list;
385 	TAILQ_HEAD(, mpr_chain)		chain_list;
386 	TAILQ_HEAD(, mpr_prp_page)	prp_page_list;
387 	TAILQ_HEAD(, mpr_command)	tm_list;
388 	int				replypostindex;
389 	int				replyfreeindex;
390 
391 	struct resource			*mpr_regs_resource;
392 	bus_space_handle_t		mpr_bhandle;
393 	bus_space_tag_t			mpr_btag;
394 	int				mpr_regs_rid;
395 
396 	bus_dma_tag_t			mpr_parent_dmat;
397 	bus_dma_tag_t			buffer_dmat;
398 
399 	MPI2_IOC_FACTS_REPLY		*facts;
400 	int				num_reqs;
401 	int				num_prireqs;
402 	int				num_replies;
403 	int				num_chains;
404 	int				fqdepth;	/* Free queue */
405 	int				pqdepth;	/* Post queue */
406 
407 	uint8_t				event_mask[16];
408 	TAILQ_HEAD(, mpr_event_handle)	event_list;
409 	struct mpr_event_handle		*mpr_log_eh;
410 
411 	struct mtx			mpr_mtx;
412 	struct intr_config_hook		mpr_ich;
413 
414 	uint8_t				*req_frames;
415 	bus_addr_t			req_busaddr;
416 	bus_dma_tag_t			req_dmat;
417 	bus_dmamap_t			req_map;
418 
419 	uint8_t				*reply_frames;
420 	bus_addr_t			reply_busaddr;
421 	bus_dma_tag_t			reply_dmat;
422 	bus_dmamap_t			reply_map;
423 
424 	struct scsi_sense_data		*sense_frames;
425 	bus_addr_t			sense_busaddr;
426 	bus_dma_tag_t			sense_dmat;
427 	bus_dmamap_t			sense_map;
428 
429 	uint8_t				*chain_frames;
430 	bus_dma_tag_t			chain_dmat;
431 	bus_dmamap_t			chain_map;
432 
433 	uint8_t				*prp_pages;
434 	bus_addr_t			prp_page_busaddr;
435 	bus_dma_tag_t			prp_page_dmat;
436 	bus_dmamap_t			prp_page_map;
437 
438 	MPI2_REPLY_DESCRIPTORS_UNION	*post_queue;
439 	bus_addr_t			post_busaddr;
440 	uint32_t			*free_queue;
441 	bus_addr_t			free_busaddr;
442 	bus_dma_tag_t			queues_dmat;
443 	bus_dmamap_t			queues_map;
444 
445 	uint8_t				*fw_diag_buffer;
446 	bus_addr_t			fw_diag_busaddr;
447 	bus_dma_tag_t			fw_diag_dmat;
448 	bus_dmamap_t			fw_diag_map;
449 
450 	uint8_t				ir_firmware;
451 
452 	/* static config pages */
453 	Mpi2IOCPage8_t			ioc_pg8;
454 	Mpi2IOUnitPage8_t		iounit_pg8;
455 
456 	/* host mapping support */
457 	struct dev_mapping_table	*mapping_table;
458 	struct enc_mapping_table	*enclosure_table;
459 	struct map_removal_table	*removal_table;
460 	uint8_t				*dpm_entry_used;
461 	uint8_t				*dpm_flush_entry;
462 	Mpi2DriverMappingPage0_t	*dpm_pg0;
463 	uint16_t			max_devices;
464 	uint16_t			max_enclosures;
465 	uint16_t			max_expanders;
466 	uint8_t				max_volumes;
467 	uint8_t				num_enc_table_entries;
468 	uint8_t				num_rsvd_entries;
469 	uint16_t			max_dpm_entries;
470 	uint8_t				is_dpm_enable;
471 	uint8_t				track_mapping_events;
472 	uint32_t			pending_map_events;
473 
474 	/* FW diag Buffer List */
475 	mpr_fw_diagnostic_buffer_t
476 				fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_COUNT];
477 
478 	/* Event Recording IOCTL support */
479 	uint32_t			events_to_record[4];
480 	mpr_event_entry_t		recorded_events[MPR_EVENT_QUEUE_SIZE];
481 	uint8_t				event_index;
482 	uint32_t			event_number;
483 
484 	/* EEDP and TLR support */
485 	uint8_t				eedp_enabled;
486 	uint8_t				control_TLR;
487 
488 	/* Shutdown Event Handler */
489 	eventhandler_tag		shutdown_eh;
490 
491 	/* To track topo events during reset */
492 #define	MPR_DIAG_RESET_TIMEOUT	300000
493 	uint8_t				wait_for_port_enable;
494 	uint8_t				port_enable_complete;
495 	uint8_t				msleep_fake_chan;
496 
497 	/* StartStopUnit command handling at shutdown */
498 	uint32_t			SSU_refcount;
499 	uint8_t				SSU_started;
500 
501 	/* Configuration tunables */
502 	u_int				disable_msix;
503 	u_int				disable_msi;
504 	u_int				max_msix;
505 	u_int				max_reqframes;
506 	u_int				max_prireqframes;
507 	u_int				max_replyframes;
508 	u_int				max_evtframes;
509 	char				exclude_ids[80];
510 
511 	struct timeval			lastfail;
512 	uint8_t				custom_nvme_tm_handling;
513 	uint8_t				nvme_abort_timeout;
514 };
515 
516 struct mpr_config_params {
517 	MPI2_CONFIG_EXT_PAGE_HEADER_UNION	hdr;
518 	u_int		action;
519 	u_int		page_address;	/* Attributes, not a phys address */
520 	u_int		status;
521 	void		*buffer;
522 	u_int		length;
523 	int		timeout;
524 	void		(*callback)(struct mpr_softc *, struct mpr_config_params *);
525 	void		*cbdata;
526 };
527 
528 struct scsi_read_capacity_eedp
529 {
530 	uint8_t addr[8];
531 	uint8_t length[4];
532 	uint8_t protect;
533 };
534 
535 static __inline uint32_t
536 mpr_regread(struct mpr_softc *sc, uint32_t offset)
537 {
538 	uint32_t ret_val, i = 0;
539 	do {
540 		ret_val =
541 		    bus_space_read_4(sc->mpr_btag, sc->mpr_bhandle, offset);
542 	} while((sc->mpr_flags & MPR_FLAGS_SEA_IOC) &&
543 	    (ret_val == 0) && (++i < 3));
544 
545 	return ret_val;
546 }
547 
548 static __inline void
549 mpr_regwrite(struct mpr_softc *sc, uint32_t offset, uint32_t val)
550 {
551 	bus_space_write_4(sc->mpr_btag, sc->mpr_bhandle, offset, val);
552 }
553 
554 /* free_queue must have Little Endian address
555  * TODO- cm_reply_data is unwanted. We can remove it.
556  * */
557 static __inline void
558 mpr_free_reply(struct mpr_softc *sc, uint32_t busaddr)
559 {
560 	if (++sc->replyfreeindex >= sc->fqdepth)
561 		sc->replyfreeindex = 0;
562 	sc->free_queue[sc->replyfreeindex] = htole32(busaddr);
563 	mpr_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
564 }
565 
566 static __inline struct mpr_chain *
567 mpr_alloc_chain(struct mpr_softc *sc)
568 {
569 	struct mpr_chain *chain;
570 
571 	if ((chain = TAILQ_FIRST(&sc->chain_list)) != NULL) {
572 		TAILQ_REMOVE(&sc->chain_list, chain, chain_link);
573 		sc->chain_free--;
574 		if (sc->chain_free < sc->chain_free_lowwater)
575 			sc->chain_free_lowwater = sc->chain_free;
576 	} else
577 		sc->chain_alloc_fail++;
578 	return (chain);
579 }
580 
581 static __inline void
582 mpr_free_chain(struct mpr_softc *sc, struct mpr_chain *chain)
583 {
584 #if 0
585 	bzero(chain->chain, 128);
586 #endif
587 	sc->chain_free++;
588 	TAILQ_INSERT_TAIL(&sc->chain_list, chain, chain_link);
589 }
590 
591 static __inline struct mpr_prp_page *
592 mpr_alloc_prp_page(struct mpr_softc *sc)
593 {
594 	struct mpr_prp_page *prp_page;
595 
596 	if ((prp_page = TAILQ_FIRST(&sc->prp_page_list)) != NULL) {
597 		TAILQ_REMOVE(&sc->prp_page_list, prp_page, prp_page_link);
598 		sc->prp_pages_free--;
599 		if (sc->prp_pages_free < sc->prp_pages_free_lowwater)
600 			sc->prp_pages_free_lowwater = sc->prp_pages_free;
601 	} else
602 		sc->prp_page_alloc_fail++;
603 	return (prp_page);
604 }
605 
606 static __inline void
607 mpr_free_prp_page(struct mpr_softc *sc, struct mpr_prp_page *prp_page)
608 {
609 	sc->prp_pages_free++;
610 	TAILQ_INSERT_TAIL(&sc->prp_page_list, prp_page, prp_page_link);
611 }
612 
613 static __inline void
614 mpr_free_command(struct mpr_softc *sc, struct mpr_command *cm)
615 {
616 	struct mpr_chain *chain, *chain_temp;
617 	struct mpr_prp_page *prp_page, *prp_page_temp;
618 
619 	KASSERT(cm->cm_state == MPR_CM_STATE_BUSY, ("state not busy\n"));
620 
621 	if (cm->cm_reply != NULL)
622 		mpr_free_reply(sc, cm->cm_reply_data);
623 	cm->cm_reply = NULL;
624 	cm->cm_flags = 0;
625 	cm->cm_complete = NULL;
626 	cm->cm_complete_data = NULL;
627 	cm->cm_ccb = NULL;
628 	cm->cm_targ = NULL;
629 	cm->cm_max_segs = 0;
630 	cm->cm_lun = 0;
631 	cm->cm_state = MPR_CM_STATE_FREE;
632 	cm->cm_data = NULL;
633 	cm->cm_length = 0;
634 	cm->cm_out_len = 0;
635 	cm->cm_sglsize = 0;
636 	cm->cm_sge = NULL;
637 
638 	TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
639 		TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
640 		mpr_free_chain(sc, chain);
641 	}
642 	TAILQ_FOREACH_SAFE(prp_page, &cm->cm_prp_page_list, prp_page_link,
643 	    prp_page_temp) {
644 		TAILQ_REMOVE(&cm->cm_prp_page_list, prp_page, prp_page_link);
645 		mpr_free_prp_page(sc, prp_page);
646 	}
647 	TAILQ_INSERT_TAIL(&sc->req_list, cm, cm_link);
648 }
649 
650 static __inline struct mpr_command *
651 mpr_alloc_command(struct mpr_softc *sc)
652 {
653 	struct mpr_command *cm;
654 
655 	cm = TAILQ_FIRST(&sc->req_list);
656 	if (cm == NULL)
657 		return (NULL);
658 
659 	KASSERT(cm->cm_state == MPR_CM_STATE_FREE,
660 	    ("mpr: Allocating busy command\n"));
661 
662 	TAILQ_REMOVE(&sc->req_list, cm, cm_link);
663 	cm->cm_state = MPR_CM_STATE_BUSY;
664 	cm->cm_timeout_handler = NULL;
665 	return (cm);
666 }
667 
668 static __inline void
669 mpr_free_high_priority_command(struct mpr_softc *sc, struct mpr_command *cm)
670 {
671 	struct mpr_chain *chain, *chain_temp;
672 
673 	KASSERT(cm->cm_state == MPR_CM_STATE_BUSY, ("state not busy\n"));
674 
675 	if (cm->cm_reply != NULL)
676 		mpr_free_reply(sc, cm->cm_reply_data);
677 	cm->cm_reply = NULL;
678 	cm->cm_flags = 0;
679 	cm->cm_complete = NULL;
680 	cm->cm_complete_data = NULL;
681 	cm->cm_ccb = NULL;
682 	cm->cm_targ = NULL;
683 	cm->cm_lun = 0;
684 	cm->cm_state = MPR_CM_STATE_FREE;
685 	TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
686 		TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
687 		mpr_free_chain(sc, chain);
688 	}
689 	TAILQ_INSERT_TAIL(&sc->high_priority_req_list, cm, cm_link);
690 }
691 
692 static __inline struct mpr_command *
693 mpr_alloc_high_priority_command(struct mpr_softc *sc)
694 {
695 	struct mpr_command *cm;
696 
697 	cm = TAILQ_FIRST(&sc->high_priority_req_list);
698 	if (cm == NULL)
699 		return (NULL);
700 
701 	KASSERT(cm->cm_state == MPR_CM_STATE_FREE,
702 	    ("mpr: Allocating busy command\n"));
703 
704 	TAILQ_REMOVE(&sc->high_priority_req_list, cm, cm_link);
705 	cm->cm_state = MPR_CM_STATE_BUSY;
706 	cm->cm_timeout_handler = NULL;
707 	cm->cm_desc.HighPriority.RequestFlags =
708 	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
709 	return (cm);
710 }
711 
712 static __inline void
713 mpr_lock(struct mpr_softc *sc)
714 {
715 	mtx_lock(&sc->mpr_mtx);
716 }
717 
718 static __inline void
719 mpr_unlock(struct mpr_softc *sc)
720 {
721 	mtx_unlock(&sc->mpr_mtx);
722 }
723 
724 #define MPR_INFO	(1 << 0)	/* Basic info */
725 #define MPR_FAULT	(1 << 1)	/* Hardware faults */
726 #define MPR_EVENT	(1 << 2)	/* Event data from the controller */
727 #define MPR_LOG		(1 << 3)	/* Log data from the controller */
728 #define MPR_RECOVERY	(1 << 4)	/* Command error recovery tracing */
729 #define MPR_ERROR	(1 << 5)	/* Parameter errors, programming bugs */
730 #define MPR_INIT	(1 << 6)	/* Things related to system init */
731 #define MPR_XINFO	(1 << 7)	/* More detailed/noisy info */
732 #define MPR_USER	(1 << 8)	/* Trace user-generated commands */
733 #define MPR_MAPPING	(1 << 9)	/* Trace device mappings */
734 #define MPR_TRACE	(1 << 10)	/* Function-by-function trace */
735 
736 #define	MPR_SSU_DISABLE_SSD_DISABLE_HDD	0
737 #define	MPR_SSU_ENABLE_SSD_DISABLE_HDD	1
738 #define	MPR_SSU_DISABLE_SSD_ENABLE_HDD	2
739 #define	MPR_SSU_ENABLE_SSD_ENABLE_HDD	3
740 
741 #define mpr_printf(sc, args...)				\
742 	device_printf((sc)->mpr_dev, ##args)
743 
744 #define mpr_print_field(sc, msg, args...)		\
745 	printf("\t" msg, ##args)
746 
747 #define mpr_vprintf(sc, args...)			\
748 do {							\
749 	if (bootverbose)				\
750 		mpr_printf(sc, ##args);			\
751 } while (0)
752 
753 #define mpr_dprint(sc, level, msg, args...)		\
754 do {							\
755 	if ((sc)->mpr_debug & (level))			\
756 		device_printf((sc)->mpr_dev, msg, ##args);	\
757 } while (0)
758 
759 #define MPR_PRINTFIELD_START(sc, tag...)	\
760 	mpr_printf((sc), ##tag);		\
761 	mpr_print_field((sc), ":\n")
762 #define MPR_PRINTFIELD_END(sc, tag)		\
763 	mpr_printf((sc), tag "\n")
764 #define MPR_PRINTFIELD(sc, facts, attr, fmt)	\
765 	mpr_print_field((sc), #attr ": " #fmt "\n", (facts)->attr)
766 
767 static __inline void
768 mpr_from_u64(uint64_t data, U64 *mpr)
769 {
770 	(mpr)->High = htole32((uint32_t)((data) >> 32));
771 	(mpr)->Low = htole32((uint32_t)((data) & 0xffffffff));
772 }
773 
774 static __inline uint64_t
775 mpr_to_u64(U64 *data)
776 {
777 	return (((uint64_t)le32toh(data->High) << 32) | le32toh(data->Low));
778 }
779 
780 static __inline void
781 mpr_mask_intr(struct mpr_softc *sc)
782 {
783 	uint32_t mask;
784 
785 	mask = mpr_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
786 	mask |= MPI2_HIM_REPLY_INT_MASK;
787 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
788 }
789 
790 static __inline void
791 mpr_unmask_intr(struct mpr_softc *sc)
792 {
793 	uint32_t mask;
794 
795 	mask = mpr_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
796 	mask &= ~MPI2_HIM_REPLY_INT_MASK;
797 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
798 }
799 
800 int mpr_pci_setup_interrupts(struct mpr_softc *sc);
801 void mpr_pci_free_interrupts(struct mpr_softc *sc);
802 int mpr_pci_restore(struct mpr_softc *sc);
803 
804 void mpr_get_tunables(struct mpr_softc *sc);
805 int mpr_attach(struct mpr_softc *sc);
806 int mpr_free(struct mpr_softc *sc);
807 void mpr_intr(void *);
808 void mpr_intr_msi(void *);
809 void mpr_intr_locked(void *);
810 int mpr_register_events(struct mpr_softc *, uint8_t *, mpr_evt_callback_t *,
811     void *, struct mpr_event_handle **);
812 int mpr_restart(struct mpr_softc *);
813 int mpr_update_events(struct mpr_softc *, struct mpr_event_handle *, uint8_t *);
814 int mpr_deregister_events(struct mpr_softc *, struct mpr_event_handle *);
815 void mpr_build_nvme_prp(struct mpr_softc *sc, struct mpr_command *cm,
816     Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request, void *data,
817     uint32_t data_in_sz, uint32_t data_out_sz);
818 int mpr_push_sge(struct mpr_command *, MPI2_SGE_SIMPLE64 *, size_t, int);
819 int mpr_push_ieee_sge(struct mpr_command *, void *, int);
820 int mpr_add_dmaseg(struct mpr_command *, vm_paddr_t, size_t, u_int, int);
821 int mpr_attach_sas(struct mpr_softc *sc);
822 int mpr_detach_sas(struct mpr_softc *sc);
823 int mpr_read_config_page(struct mpr_softc *, struct mpr_config_params *);
824 int mpr_write_config_page(struct mpr_softc *, struct mpr_config_params *);
825 void mpr_memaddr_cb(void *, bus_dma_segment_t *, int , int );
826 void mpr_memaddr_wait_cb(void *, bus_dma_segment_t *, int , int );
827 void mpr_init_sge(struct mpr_command *cm, void *req, void *sge);
828 int mpr_attach_user(struct mpr_softc *);
829 void mpr_detach_user(struct mpr_softc *);
830 void mprsas_record_event(struct mpr_softc *sc,
831     MPI2_EVENT_NOTIFICATION_REPLY *event_reply);
832 
833 int mpr_map_command(struct mpr_softc *sc, struct mpr_command *cm);
834 int mpr_wait_command(struct mpr_softc *sc, struct mpr_command **cm, int timeout,
835     int sleep_flag);
836 int mpr_request_polled(struct mpr_softc *sc, struct mpr_command **cm);
837 
838 int mpr_config_get_bios_pg3(struct mpr_softc *sc, Mpi2ConfigReply_t
839     *mpi_reply, Mpi2BiosPage3_t *config_page);
840 int mpr_config_get_raid_volume_pg0(struct mpr_softc *sc, Mpi2ConfigReply_t
841     *mpi_reply, Mpi2RaidVolPage0_t *config_page, u32 page_address);
842 int mpr_config_get_ioc_pg8(struct mpr_softc *sc, Mpi2ConfigReply_t *,
843     Mpi2IOCPage8_t *);
844 int mpr_config_get_iounit_pg8(struct mpr_softc *sc,
845     Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage8_t *config_page);
846 int mpr_config_get_sas_device_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
847     Mpi2SasDevicePage0_t *, u32 , u16 );
848 int mpr_config_get_pcie_device_pg0(struct mpr_softc *sc, Mpi2ConfigReply_t
849     *mpi_reply, Mpi26PCIeDevicePage0_t *config_page, u32 form, u16 handle);
850 int mpr_config_get_pcie_device_pg2(struct mpr_softc *sc, Mpi2ConfigReply_t
851     *mpi_reply, Mpi26PCIeDevicePage2_t *config_page, u32 form, u16 handle);
852 int mpr_config_get_dpm_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
853     Mpi2DriverMappingPage0_t *, u16 );
854 int mpr_config_get_raid_volume_pg1(struct mpr_softc *sc,
855     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage1_t *config_page, u32 form,
856     u16 handle);
857 int mpr_config_get_volume_wwid(struct mpr_softc *sc, u16 volume_handle,
858     u64 *wwid);
859 int mpr_config_get_raid_pd_pg0(struct mpr_softc *sc,
860     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidPhysDiskPage0_t *config_page,
861     u32 page_address);
862 int mpr_config_get_man_pg11(struct mpr_softc *sc, Mpi2ConfigReply_t *mpi_reply,
863     Mpi2ManufacturingPage11_t *config_page);
864 void mprsas_ir_shutdown(struct mpr_softc *sc, int howto);
865 
866 int mpr_reinit(struct mpr_softc *sc);
867 void mprsas_handle_reinit(struct mpr_softc *sc);
868 
869 void mpr_base_static_config_pages(struct mpr_softc *sc);
870 
871 int mpr_mapping_initialize(struct mpr_softc *);
872 void mpr_mapping_topology_change_event(struct mpr_softc *,
873     Mpi2EventDataSasTopologyChangeList_t *);
874 void mpr_mapping_pcie_topology_change_event(struct mpr_softc *sc,
875     Mpi26EventDataPCIeTopologyChangeList_t *event_data);
876 void mpr_mapping_free_memory(struct mpr_softc *sc);
877 int mpr_config_set_dpm_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
878     Mpi2DriverMappingPage0_t *, u16 );
879 void mpr_mapping_exit(struct mpr_softc *);
880 void mpr_mapping_check_devices(void *);
881 int mpr_mapping_allocate_memory(struct mpr_softc *sc);
882 unsigned int mpr_mapping_get_tid(struct mpr_softc *, uint64_t , u16);
883 unsigned int mpr_mapping_get_tid_from_handle(struct mpr_softc *sc,
884     u16 handle);
885 unsigned int mpr_mapping_get_raid_tid(struct mpr_softc *sc, u64 wwid,
886     u16 volHandle);
887 unsigned int mpr_mapping_get_raid_tid_from_handle(struct mpr_softc *sc,
888     u16 volHandle);
889 void mpr_mapping_enclosure_dev_status_change_event(struct mpr_softc *,
890     Mpi2EventDataSasEnclDevStatusChange_t *event_data);
891 void mpr_mapping_ir_config_change_event(struct mpr_softc *sc,
892     Mpi2EventDataIrConfigChangeList_t *event_data);
893 
894 void mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
895     MPI2_EVENT_NOTIFICATION_REPLY *event);
896 void mprsas_prepare_remove(struct mprsas_softc *sassc, uint16_t handle);
897 void mprsas_prepare_volume_remove(struct mprsas_softc *sassc, uint16_t handle);
898 int mprsas_startup(struct mpr_softc *sc);
899 struct mprsas_target * mprsas_find_target_by_handle(struct mprsas_softc *, int,
900     uint16_t);
901 void mprsas_realloc_targets(struct mpr_softc *sc, int maxtargets);
902 struct mpr_command * mprsas_alloc_tm(struct mpr_softc *sc);
903 void mprsas_free_tm(struct mpr_softc *sc, struct mpr_command *tm);
904 void mprsas_release_simq_reinit(struct mprsas_softc *sassc);
905 int mprsas_send_reset(struct mpr_softc *sc, struct mpr_command *tm,
906     uint8_t type);
907 
908 SYSCTL_DECL(_hw_mpr);
909 
910 /* Compatibility shims for different OS versions */
911 #if __FreeBSD_version >= 800001
912 #define mpr_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
913     kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
914 #define mpr_kproc_exit(arg)	kproc_exit(arg)
915 #else
916 #define mpr_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
917     kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
918 #define mpr_kproc_exit(arg)	kthread_exit(arg)
919 #endif
920 
921 #if defined(CAM_PRIORITY_XPT)
922 #define MPR_PRIORITY_XPT	CAM_PRIORITY_XPT
923 #else
924 #define MPR_PRIORITY_XPT	5
925 #endif
926 
927 #if __FreeBSD_version < 800107
928 // Prior to FreeBSD-8.0 scp3_flags was not defined.
929 #define spc3_flags reserved
930 
931 #define SPC3_SID_PROTECT    0x01
932 #define SPC3_SID_3PC        0x08
933 #define SPC3_SID_TPGS_MASK  0x30
934 #define SPC3_SID_TPGS_IMPLICIT  0x10
935 #define SPC3_SID_TPGS_EXPLICIT  0x20
936 #define SPC3_SID_ACC        0x40
937 #define SPC3_SID_SCCS       0x80
938 
939 #define CAM_PRIORITY_NORMAL CAM_PRIORITY_NONE
940 #endif
941 
942 /* Definitions for SCSI unmap translation to NVMe DSM command */
943 
944 /* UNMAP block descriptor structure */
945 struct unmap_blk_desc {
946 	uint64_t slba;
947 	uint32_t nlb;
948 	uint32_t resv;
949 };
950 
951 /* UNMAP command's data */
952 struct unmap_parm_list {
953 	uint16_t unmap_data_len;
954 	uint16_t unmap_blk_desc_data_len;
955 	uint32_t resv;
956 	struct unmap_blk_desc desc[0];
957 };
958 
959 /* SCSI ADDITIONAL SENSE Codes */
960 #define FIXED_SENSE_DATA                                0x70
961 #define SCSI_ASC_NO_SENSE                               0x00
962 #define SCSI_ASC_PERIPHERAL_DEV_WRITE_FAULT             0x03
963 #define SCSI_ASC_LUN_NOT_READY                          0x04
964 #define SCSI_ASC_WARNING                                0x0B
965 #define SCSI_ASC_LOG_BLOCK_GUARD_CHECK_FAILED           0x10
966 #define SCSI_ASC_LOG_BLOCK_APPTAG_CHECK_FAILED          0x10
967 #define SCSI_ASC_LOG_BLOCK_REFTAG_CHECK_FAILED          0x10
968 #define SCSI_ASC_UNRECOVERED_READ_ERROR                 0x11
969 #define SCSI_ASC_MISCOMPARE_DURING_VERIFY               0x1D
970 #define SCSI_ASC_ACCESS_DENIED_INVALID_LUN_ID           0x20
971 #define SCSI_ASC_ILLEGAL_COMMAND                        0x20
972 #define SCSI_ASC_ILLEGAL_BLOCK                          0x21
973 #define SCSI_ASC_INVALID_CDB                            0x24
974 #define SCSI_ASC_INVALID_LUN                            0x25
975 #define SCSI_ASC_INVALID_PARAMETER                      0x26
976 #define SCSI_ASC_FORMAT_COMMAND_FAILED                  0x31
977 #define SCSI_ASC_INTERNAL_TARGET_FAILURE                0x44
978 
979 /* SCSI ADDITIONAL SENSE Code Qualifiers */
980 #define SCSI_ASCQ_CAUSE_NOT_REPORTABLE                  0x00
981 #define SCSI_ASCQ_FORMAT_COMMAND_FAILED                 0x01
982 #define SCSI_ASCQ_LOG_BLOCK_GUARD_CHECK_FAILED          0x01
983 #define SCSI_ASCQ_LOG_BLOCK_APPTAG_CHECK_FAILED         0x02
984 #define SCSI_ASCQ_LOG_BLOCK_REFTAG_CHECK_FAILED         0x03
985 #define SCSI_ASCQ_FORMAT_IN_PROGRESS                    0x04
986 #define SCSI_ASCQ_POWER_LOSS_EXPECTED                   0x08
987 #define SCSI_ASCQ_INVALID_LUN_ID                        0x09
988 
989 #endif
990 
991