xref: /freebsd/sys/dev/mpt/mpt.h (revision 87569f75a91f298c52a71823c04d41cf53c88889)
1 /* $FreeBSD$ */
2 /*-
3  * Generic defines for LSI '909 FC  adapters.
4  * FreeBSD Version.
5  *
6  * Copyright (c)  2000, 2001 by Greg Ansley
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 immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
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 FOR
21  * 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 /*-
30  * Copyright (c) 2002, 2006 by Matthew Jacob
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions are
35  * met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
39  *    substantially similar to the "NO WARRANTY" disclaimer below
40  *    ("Disclaimer") and any redistribution must be conditioned upon including
41  *    a substantially similar Disclaimer requirement for further binary
42  *    redistribution.
43  * 3. Neither the names of the above listed copyright holders nor the names
44  *    of any contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
57  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  * Support from Chris Ellsworth in order to make SAS adapters work
60  * is gratefully acknowledged.
61  */
62 /*
63  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
64  * Copyright (c) 2004, 2005 Justin T. Gibbs
65  * Copyright (c) 2005, WHEEL Sp. z o.o.
66  * All rights reserved.
67  *
68  * Redistribution and use in source and binary forms, with or without
69  * modification, are permitted provided that the following conditions are
70  * met:
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
74  *    substantially similar to the "NO WARRANTY" disclaimer below
75  *    ("Disclaimer") and any redistribution must be conditioned upon including
76  *    a substantially similar Disclaimer requirement for further binary
77  *    redistribution.
78  * 3. Neither the names of the above listed copyright holders nor the names
79  *    of any contributors may be used to endorse or promote products derived
80  *    from this software without specific prior written permission.
81  *
82  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
83  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
86  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
89  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
91  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
92  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93  */
94 
95 #ifndef _MPT_H_
96 #define _MPT_H_
97 
98 /********************************* OS Includes ********************************/
99 #include <sys/types.h>
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/endian.h>
103 #include <sys/eventhandler.h>
104 #if __FreeBSD_version < 500000
105 #include <sys/kernel.h>
106 #include <sys/queue.h>
107 #include <sys/malloc.h>
108 #else
109 #include <sys/lock.h>
110 #include <sys/kernel.h>
111 #include <sys/queue.h>
112 #include <sys/malloc.h>
113 #include <sys/mutex.h>
114 #include <sys/condvar.h>
115 #endif
116 #include <sys/proc.h>
117 #include <sys/bus.h>
118 #include <sys/module.h>
119 
120 #include <machine/clock.h>
121 #include <machine/cpu.h>
122 #include <machine/resource.h>
123 
124 #include <sys/rman.h>
125 
126 #if __FreeBSD_version < 500000
127 #include <pci/pcireg.h>
128 #include <pci/pcivar.h>
129 #else
130 #include <dev/pci/pcireg.h>
131 #include <dev/pci/pcivar.h>
132 #endif
133 
134 #include <machine/bus.h>
135 #include "opt_ddb.h"
136 
137 /**************************** Register Definitions ****************************/
138 #include <dev/mpt/mpt_reg.h>
139 
140 /******************************* MPI Definitions ******************************/
141 #include <dev/mpt/mpilib/mpi_type.h>
142 #include <dev/mpt/mpilib/mpi.h>
143 #include <dev/mpt/mpilib/mpi_cnfg.h>
144 #include <dev/mpt/mpilib/mpi_ioc.h>
145 #include <dev/mpt/mpilib/mpi_raid.h>
146 
147 /* XXX For mpt_debug.c */
148 #include <dev/mpt/mpilib/mpi_init.h>
149 
150 /****************************** Misc Definitions ******************************/
151 #define MPT_OK (0)
152 #define MPT_FAIL (0x10000)
153 
154 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
155 
156 /**************************** Forward Declarations ****************************/
157 struct mpt_softc;
158 struct mpt_personality;
159 typedef struct req_entry request_t;
160 
161 /************************* Personality Module Support *************************/
162 typedef int mpt_load_handler_t(struct mpt_personality *);
163 typedef int mpt_probe_handler_t(struct mpt_softc *);
164 typedef int mpt_attach_handler_t(struct mpt_softc *);
165 typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
166 				MSG_EVENT_NOTIFY_REPLY *);
167 typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
168 /* XXX Add return value and use for veto? */
169 typedef void mpt_shutdown_handler_t(struct mpt_softc *);
170 typedef void mpt_detach_handler_t(struct mpt_softc *);
171 typedef int mpt_unload_handler_t(struct mpt_personality *);
172 
173 struct mpt_personality
174 {
175 	const char		*name;
176 	uint32_t		 id;		/* Assigned identifier. */
177 	u_int			 use_count;	/* Instances using personality*/
178 	mpt_load_handler_t	*load;		/* configure personailty */
179 #define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
180 	mpt_probe_handler_t	*probe;		/* configure personailty */
181 	mpt_attach_handler_t	*attach;	/* initialize device instance */
182 	mpt_event_handler_t	*event;		/* Handle MPI event. */
183 	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
184 	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
185 	mpt_detach_handler_t	*detach;	/* release device instance */
186 	mpt_unload_handler_t	*unload;	/* Shutdown personality */
187 #define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
188 };
189 
190 int mpt_modevent(module_t, int, void *);
191 
192 /* Maximum supported number of personalities. */
193 #define MPT_MAX_PERSONALITIES	(15)
194 
195 #define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
196 	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
197 
198 #define DECLARE_MPT_PERSONALITY(name, order)				  \
199 	static moduledata_t name##_mod = {				  \
200 		#name, mpt_modevent, &name##_personality		  \
201 	};								  \
202 	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
203 	MODULE_VERSION(name, 1);					  \
204 	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
205 
206 /******************************* Bus DMA Support ******************************/
207 /* XXX Need to update bus_dmamap_sync to take a range argument. */
208 #define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
209 	bus_dmamap_sync(dma_tag, dmamap, op)
210 
211 #if __FreeBSD_version >= 501102
212 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
213 			   lowaddr, highaddr, filter, filterarg,	\
214 			   maxsize, nsegments, maxsegsz, flags,		\
215 			   dma_tagp)					\
216 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
217 			   lowaddr, highaddr, filter, filterarg,	\
218 			   maxsize, nsegments, maxsegsz, flags,		\
219 			   busdma_lock_mutex, &Giant,			\
220 			   dma_tagp)
221 #else
222 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
223 			   lowaddr, highaddr, filter, filterarg,	\
224 			   maxsize, nsegments, maxsegsz, flags,		\
225 			   dma_tagp)					\
226 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
227 			   lowaddr, highaddr, filter, filterarg,	\
228 			   maxsize, nsegments, maxsegsz, flags,		\
229 			   dma_tagp)
230 #endif
231 
232 struct mpt_map_info {
233 	struct mpt_softc *mpt;
234 	int		  error;
235 	uint32_t	  phys;
236 };
237 
238 void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
239 
240 /**************************** Kernel Thread Support ***************************/
241 #if __FreeBSD_version > 500005
242 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
243 	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
244 #else
245 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
246 	kthread_create(func, farg, proc_ptr, fmtstr, arg)
247 #endif
248 
249 /****************************** Timer Facilities ******************************/
250 #if __FreeBSD_version > 500000
251 #define mpt_callout_init(c)	callout_init(c, /*mpsafe*/0);
252 #else
253 #define mpt_callout_init(c)	callout_init(c);
254 #endif
255 
256 /********************************** Endianess *********************************/
257 static __inline uint64_t
258 u64toh(U64 s)
259 {
260 	uint64_t result;
261 
262 	result = le32toh(s.Low);
263 	result |= ((uint64_t)le32toh(s.High)) << 32;
264 	return (result);
265 }
266 
267 /**************************** MPI Transaction State ***************************/
268 typedef enum {
269 	REQ_STATE_FREE		= 0x00,
270 	REQ_STATE_ALLOCATED	= 0x01,
271 	REQ_STATE_QUEUED	= 0x02,
272 	REQ_STATE_DONE		= 0x04,
273 	REQ_STATE_TIMEDOUT	= 0x08,
274 	REQ_STATE_NEED_WAKEUP	= 0x10,
275 	REQ_STATE_MASK		= 0xFF
276 } mpt_req_state_t;
277 
278 struct req_entry {
279 	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
280 	mpt_req_state_t	state;		/* Request State Information */
281 	uint16_t	index;		/* Index of this entry */
282 	uint16_t	IOCStatus;	/* Completion status */
283 	uint32_t	serno;		/* serial number */
284 	union ccb      *ccb;		/* CAM request */
285 	void	       *req_vbuf;	/* Virtual Address of Entry */
286 	void	       *sense_vbuf;	/* Virtual Address of sense data */
287 	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
288 	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
289 	bus_dmamap_t	dmap;		/* DMA map for data buffer */
290 	struct req_entry *chain;	/* for SGE overallocations */
291 };
292 
293 /**************************** Handler Registration ****************************/
294 /*
295  * Global table of registered reply handlers.  The
296  * handler is indicated by byte 3 of the request
297  * index submitted to the IOC.  This allows the
298  * driver core to perform generic processing without
299  * any knowledge of per-personality behavior.
300  *
301  * MPT_NUM_REPLY_HANDLERS must be a power of 2
302  * to allow the easy generation of a mask.
303  *
304  * The handler offsets used by the core are hard coded
305  * allowing faster code generation when assigning a handler
306  * to a request.  All "personalities" must use the
307  * the handler registration mechanism.
308  *
309  * The IOC handlers that are rarely executed are placed
310  * at the tail of the table to make it more likely that
311  * all commonly executed handlers fit in a single cache
312  * line.
313  */
314 #define MPT_NUM_REPLY_HANDLERS		(16)
315 #define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
316 #define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
317 #define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
318 typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
319 				 MSG_DEFAULT_REPLY *reply_frame);
320 typedef union {
321 	mpt_reply_handler_t	*reply_handler;
322 } mpt_handler_t;
323 
324 typedef enum {
325 	MPT_HANDLER_REPLY,
326 	MPT_HANDLER_EVENT,
327 	MPT_HANDLER_RESET,
328 	MPT_HANDLER_SHUTDOWN
329 } mpt_handler_type;
330 
331 struct mpt_handler_record
332 {
333 	LIST_ENTRY(mpt_handler_record)	links;
334 	mpt_handler_t			handler;
335 };
336 
337 LIST_HEAD(mpt_handler_list, mpt_handler_record);
338 
339 /*
340  * The handler_id is currently unused but would contain the
341  * handler ID used in the MsgContext field to allow direction
342  * of replies to the handler.  Registrations that don't require
343  * a handler id can pass in NULL for the handler_id.
344  *
345  * Deregistrations for handlers without a handler id should
346  * pass in MPT_HANDLER_ID_NONE.
347  */
348 #define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
349 int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
350 			 mpt_handler_t, uint32_t *);
351 int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
352 			   mpt_handler_t, uint32_t);
353 
354 /******************* Per-Controller Instance Data Structures ******************/
355 TAILQ_HEAD(req_queue, req_entry);
356 
357 /* Structure for saving proper values for modifyable PCI config registers */
358 struct mpt_pci_cfg {
359 	uint16_t Command;
360 	uint16_t LatencyTimer_LineSize;
361 	uint32_t IO_BAR;
362 	uint32_t Mem0_BAR[2];
363 	uint32_t Mem1_BAR[2];
364 	uint32_t ROM_BAR;
365 	uint8_t  IntLine;
366 	uint32_t PMCSR;
367 };
368 
369 typedef enum {
370 	MPT_RVF_NONE		= 0x0,
371 	MPT_RVF_ACTIVE		= 0x1,
372 	MPT_RVF_ANNOUNCED	= 0x2,
373 	MPT_RVF_UP2DATE		= 0x4,
374 	MPT_RVF_REFERENCED	= 0x8,
375 	MPT_RVF_WCE_CHANGED	= 0x10
376 } mpt_raid_volume_flags;
377 
378 struct mpt_raid_volume {
379 	CONFIG_PAGE_RAID_VOL_0	       *config_page;
380 	MPI_RAID_VOL_INDICATOR		sync_progress;
381 	mpt_raid_volume_flags		flags;
382 	u_int				quieced_disks;
383 };
384 
385 typedef enum {
386 	MPT_RDF_NONE		= 0x00,
387 	MPT_RDF_ACTIVE		= 0x01,
388 	MPT_RDF_ANNOUNCED	= 0x02,
389 	MPT_RDF_UP2DATE		= 0x04,
390 	MPT_RDF_REFERENCED	= 0x08,
391 	MPT_RDF_QUIESCING	= 0x10,
392 	MPT_RDF_QUIESCED	= 0x20
393 } mpt_raid_disk_flags;
394 
395 struct mpt_raid_disk {
396 	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
397 	struct mpt_raid_volume	       *volume;
398 	u_int				member_number;
399 	u_int				pass_thru_active;
400 	mpt_raid_disk_flags		flags;
401 };
402 
403 struct mpt_evtf_record {
404 	MSG_EVENT_NOTIFY_REPLY		reply;
405 	uint32_t			context;
406 	LIST_ENTRY(mpt_evtf_record)	links;
407 };
408 
409 LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
410 
411 struct mpt_softc {
412 	device_t		dev;
413 #if __FreeBSD_version < 500000
414 	int			mpt_splsaved;
415 	uint32_t		mpt_islocked;
416 #else
417 	struct mtx		mpt_lock;
418 #endif
419 	uint32_t		mpt_pers_mask;
420 	uint32_t		: 14,
421 		is_sas		: 1,
422 		raid_mwce_set	: 1,
423 		getreqwaiter	: 1,
424 		shutdwn_raid    : 1,
425 		shutdwn_recovery: 1,
426 		unit		: 8,
427 		outofbeer	: 1,
428 		mpt_locksetup	: 1,
429 		disabled	: 1,
430 		is_fc		: 1,
431 		bus		: 1;	/* FC929/1030 have two busses */
432 
433 	u_int			verbose;
434 	uint32_t	cmd_serno;
435 
436 	/*
437 	 * IOC Facts
438 	 */
439 	uint16_t	mpt_global_credits;
440 	uint16_t	request_frame_size;
441 	uint8_t		mpt_max_devices;
442 	uint8_t		mpt_max_buses;
443 	uint8_t		ioc_facts_flags;
444 
445 	/*
446 	 * Port Facts
447 	 * XXX - Add multi-port support!.
448 	 */
449 	uint16_t	mpt_ini_id;
450 	uint16_t	mpt_port_type;
451 	uint16_t	mpt_proto_flags;
452 
453 	/*
454 	 * Device Configuration Information
455 	 */
456 	union {
457 		struct mpt_spi_cfg {
458 			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
459 			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
460 			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
461 			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
462 			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
463 			uint16_t			_tag_enable;
464 			uint16_t			_disc_enable;
465 			uint16_t			_update_params0;
466 			uint16_t			_update_params1;
467 		} spi;
468 #define	mpt_port_page0		cfg.spi._port_page0
469 #define	mpt_port_page1		cfg.spi._port_page1
470 #define	mpt_port_page2		cfg.spi._port_page2
471 #define	mpt_dev_page0		cfg.spi._dev_page0
472 #define	mpt_dev_page1		cfg.spi._dev_page1
473 #define	mpt_tag_enable		cfg.spi._tag_enable
474 #define	mpt_disc_enable		cfg.spi._disc_enable
475 #define	mpt_update_params0	cfg.spi._update_params0
476 #define	mpt_update_params1	cfg.spi._update_params1
477 		struct mpi_fc_cfg {
478 			uint8_t	nada;
479 		} fc;
480 	} cfg;
481 
482 	/* Controller Info */
483 	CONFIG_PAGE_IOC_2 *	ioc_page2;
484 	CONFIG_PAGE_IOC_3 *	ioc_page3;
485 
486 	/* Raid Data */
487 	struct mpt_raid_volume* raid_volumes;
488 	struct mpt_raid_disk*	raid_disks;
489 	u_int			raid_max_volumes;
490 	u_int			raid_max_disks;
491 	u_int			raid_page0_len;
492 	u_int			raid_wakeup;
493 	u_int			raid_rescan;
494 	u_int			raid_resync_rate;
495 	u_int			raid_mwce_setting;
496 	u_int			raid_queue_depth;
497 	u_int			raid_nonopt_volumes;
498 	struct proc	       *raid_thread;
499 	struct callout		raid_timer;
500 
501 	/*
502 	 * PCI Hardware info
503 	 */
504 	struct resource *	pci_irq;	/* Interrupt map for chip */
505 	void *			ih;		/* Interupt handle */
506 	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
507 
508 	/*
509 	 * DMA Mapping Stuff
510 	 */
511 	struct resource *	pci_reg;	/* Register map for chip */
512 	int			pci_mem_rid;	/* Resource ID */
513 	bus_space_tag_t		pci_st;		/* Bus tag for registers */
514 	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
515 	/* PIO versions of above. */
516 	int			pci_pio_rid;
517 	struct resource *	pci_pio_reg;
518 	bus_space_tag_t		pci_pio_st;
519 	bus_space_handle_t	pci_pio_sh;
520 
521 	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
522 	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
523 	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
524 	uint8_t		       *reply;		/* KVA of reply memory */
525 	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
526 
527 	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
528 	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
529 	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
530 	uint8_t		       *request;	/* KVA of Request memory */
531 	bus_addr_t		request_phys;	/* BusADdr of request memory */
532 
533 	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
534 
535 	/*
536 	 * Hardware management
537 	 */
538 	u_int			reset_cnt;
539 
540 	/*
541 	 * CAM && Software Management
542 	 */
543 	request_t	       *request_pool;
544 	struct req_queue	request_free_list;
545 	struct req_queue	request_pending_list;
546 	struct req_queue	request_timeout_list;
547 
548 	/*
549 	 * Deferred frame acks due to resource shortage.
550 	 */
551 	struct mpt_evtf_list	ack_frames;
552 
553 
554 	struct cam_sim	       *sim;
555 	struct cam_path	       *path;
556 
557 	struct cam_sim	       *phydisk_sim;
558 	struct cam_path	       *phydisk_path;
559 
560 	struct proc	       *recovery_thread;
561 	request_t	       *tmf_req;
562 
563 	uint32_t		sequence;	/* Sequence Number */
564 	uint32_t		timeouts;	/* timeout count */
565 	uint32_t		success;	/* successes afer timeout */
566 
567 	/* Opposing port in a 929 or 1030, or NULL */
568 	struct mpt_softc *	mpt2;
569 
570 	/* FW Image management */
571 	uint32_t		fw_image_size;
572 	uint8_t		       *fw_image;
573 	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
574 	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
575 	bus_addr_t		fw_phys;	/* BusAddr of request memory */
576 
577 	/* Shutdown Event Handler. */
578 	eventhandler_tag         eh;
579 
580 	TAILQ_ENTRY(mpt_softc)	links;
581 };
582 
583 /***************************** Locking Primatives *****************************/
584 #if __FreeBSD_version < 500000
585 #define	MPT_IFLAGS		INTR_TYPE_CAM
586 #define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
587 #define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
588 #define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
589 #define	CAMLOCK_2_MPTLOCK	MPT_LOCK
590 #define	MPT_LOCK_SETUP(mpt)
591 #define	MPT_LOCK_DESTROY(mpt)
592 
593 static __inline void mpt_lockspl(struct mpt_softc *mpt);
594 static __inline void mpt_unlockspl(struct mpt_softc *mpt);
595 
596 static __inline void
597 mpt_lockspl(struct mpt_softc *mpt)
598 {
599        int s;
600 
601        s = splcam();
602        if (mpt->mpt_islocked++ == 0) {
603                mpt->mpt_splsaved = s;
604        } else {
605                splx(s);
606 	       panic("Recursed lock with mask: 0x%x\n", s);
607        }
608 }
609 
610 static __inline void
611 mpt_unlockspl(struct mpt_softc *mpt)
612 {
613        if (mpt->mpt_islocked) {
614                if (--mpt->mpt_islocked == 0) {
615                        splx(mpt->mpt_splsaved);
616                }
617        } else
618 	       panic("Negative lock count\n");
619 }
620 
621 static __inline int
622 mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
623 	   const char *wmesg, int timo)
624 {
625 	int saved_cnt;
626 	int saved_spl;
627 	int error;
628 
629 	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
630 	saved_cnt = mpt->mpt_islocked;
631 	saved_spl = mpt->mpt_splsaved;
632 	mpt->mpt_islocked = 0;
633 	error = tsleep(ident, priority, wmesg, timo);
634 	KASSERT(mpt->mpt_islocked = 0, ("Invalid lock count on wakeup"));
635 	mpt->mpt_islocked = saved_cnt;
636 	mpt->mpt_splsaved = saved_spl;
637 	return (error);
638 }
639 
640 #else
641 #ifdef	LOCKING_WORKED_AS_IT_SHOULD
642 #error "Shouldn't Be Here!"
643 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
644 #define	MPT_LOCK_SETUP(mpt)						\
645 		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
646 		mpt->mpt_locksetup = 1
647 #define	MPT_LOCK_DESTROY(mpt)						\
648 	if (mpt->mpt_locksetup) {					\
649 		mtx_destroy(&mpt->mpt_lock);				\
650 		mpt->mpt_locksetup = 0;					\
651 	}
652 
653 #define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
654 #define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
655 #define	MPTLOCK_2_CAMLOCK(mpt)	\
656 	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
657 #define	CAMLOCK_2_MPTLOCK(mpt)	\
658 	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
659 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
660 	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
661 #else
662 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
663 #define	MPT_LOCK_SETUP(mpt)	do { } while (0)
664 #define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
665 #define	MPT_LOCK(mpt)		do { } while (0)
666 #define	MPT_UNLOCK(mpt)		do { } while (0)
667 #define	MPTLOCK_2_CAMLOCK(mpt)	do { } while (0)
668 #define	CAMLOCK_2_MPTLOCK(mpt)	do { } while (0)
669 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
670 	tsleep(ident, priority, wmesg, timo)
671 #endif
672 #endif
673 
674 /******************************* Register Access ******************************/
675 static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
676 static __inline uint32_t mpt_read(struct mpt_softc *, int);
677 static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
678 static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
679 
680 static __inline void
681 mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
682 {
683 	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
684 }
685 
686 static __inline uint32_t
687 mpt_read(struct mpt_softc *mpt, int offset)
688 {
689 	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
690 }
691 
692 /*
693  * Some operations (e.g. diagnostic register writes while the ARM proccessor
694  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
695  * busses, these operations likely map to normal register accesses.
696  */
697 static __inline void
698 mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
699 {
700 	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
701 }
702 
703 static __inline uint32_t
704 mpt_pio_read(struct mpt_softc *mpt, int offset)
705 {
706 	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
707 }
708 /*********************** Reply Frame/Request Management ***********************/
709 /* Max MPT Reply we are willing to accept (must be power of 2) */
710 #define MPT_REPLY_SIZE   	256
711 
712 #define MPT_MAX_REQUESTS(mpt)	512
713 #define MPT_REQUEST_AREA	512
714 #define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
715 #define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
716 
717 #define MPT_CONTEXT_CB_SHIFT	(16)
718 #define MPT_CBI(handle)	(handle >> MPT_CONTEXT_CB_SHIFT)
719 #define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
720 #define MPT_CONTEXT_TO_CBI(x)	\
721     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
722 #define MPT_CONTEXT_REQI_MASK 0xFFFF
723 #define MPT_CONTEXT_TO_REQI(x)	\
724     ((x) & MPT_CONTEXT_REQI_MASK)
725 
726 /*
727  * Convert a 32bit physical address returned from IOC to an
728  * offset into our reply frame memory or the kvm address needed
729  * to access the data.  The returned address is only the low
730  * 32 bits, so mask our base physical address accordingly.
731  */
732 #define MPT_REPLY_BADDR(x)		\
733 	(x << 1)
734 #define MPT_REPLY_OTOV(m, i) 		\
735 	((void *)(&m->reply[i]))
736 
737 #define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
738 do {							\
739 	if (mpt->verbose >= MPT_PRT_DEBUG)		\
740 		mpt_dump_reply_frame(mpt, reply_frame);	\
741 } while(0)
742 
743 static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
744 static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
745 
746 /*
747  * Give the reply buffer back to the IOC after we have
748  * finished processing it.
749  */
750 static __inline void
751 mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
752 {
753      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
754 }
755 
756 /* Get a reply from the IOC */
757 static __inline uint32_t
758 mpt_pop_reply_queue(struct mpt_softc *mpt)
759 {
760      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
761 }
762 
763 void mpt_complete_request_chain(struct mpt_softc *mpt,
764 				struct req_queue *chain, u_int iocstatus);
765 /************************** Scatter Gather Managment **************************/
766 /* MPT_RQSL- size of request frame, in bytes */
767 #define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
768 
769 /* MPT_NSGL- how many SG entries can fit in a request frame size */
770 #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
771 
772 /* MPT_NRFM- how many request frames can fit in each request alloc we make */
773 #define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
774 
775 /*
776  * MPT_NSGL_FIRST- # of SG elements that can fit after
777  * an I/O request but still within the request frame.
778  * Do this safely based upon SGE_IO_UNION.
779  *
780  * Note that the first element is *within* the SCSI request.
781  */
782 #define	MPT_NSGL_FIRST(mpt)	\
783     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
784     sizeof (SGE_IO_UNION))
785 
786 /***************************** IOC Initialization *****************************/
787 int mpt_reset(struct mpt_softc *, int /*reinit*/);
788 
789 /****************************** Debugging/Logging *****************************/
790 typedef struct mpt_decode_entry {
791 	char    *name;
792 	u_int	 value;
793 	u_int	 mask;
794 } mpt_decode_entry_t;
795 
796 int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
797 		     const char *name, u_int value, u_int *cur_column,
798 		     u_int wrap_point);
799 
800 enum {
801 	MPT_PRT_ALWAYS,
802 	MPT_PRT_FATAL,
803 	MPT_PRT_ERROR,
804 	MPT_PRT_WARN,
805 	MPT_PRT_INFO,
806 	MPT_PRT_DEBUG,
807 	MPT_PRT_TRACE,
808 	MPT_PRT_NONE=100
809 };
810 
811 #define mpt_lprt(mpt, level, ...)		\
812 do {						\
813 	if (level <= (mpt)->verbose)		\
814 		mpt_prt(mpt, __VA_ARGS__);	\
815 } while (0)
816 
817 #define mpt_lprtc(mpt, level, ...)		 \
818 do {						 \
819 	if (level <= (mpt)->debug_level)	 \
820 		mpt_prtc(mpt, __VA_ARGS__);	 \
821 } while (0)
822 
823 void mpt_prt(struct mpt_softc *, const char *, ...);
824 void mpt_prtc(struct mpt_softc *, const char *, ...);
825 
826 /**************************** Unclassified Routines ***************************/
827 void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
828 int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
829 					 size_t reply_len, void *reply);
830 int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
831 			     mpt_req_state_t state, mpt_req_state_t mask,
832 			     int sleep_ok, int time_ms);
833 void		mpt_enable_ints(struct mpt_softc *mpt);
834 void		mpt_disable_ints(struct mpt_softc *mpt);
835 int		mpt_attach(struct mpt_softc *mpt);
836 int		mpt_shutdown(struct mpt_softc *mpt);
837 int		mpt_detach(struct mpt_softc *mpt);
838 int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
839 				       size_t len, void *cmd);
840 request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
841 void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
842 void		mpt_intr(void *arg);
843 void		mpt_check_doorbell(struct mpt_softc *mpt);
844 void		mpt_dump_reply_frame(struct mpt_softc *mpt,
845 				     MSG_DEFAULT_REPLY *reply_frame);
846 
847 void		mpt_set_config_regs(struct mpt_softc *);
848 int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
849 				  u_int /*Action*/, u_int /*PageVersion*/,
850 				  u_int /*PageLength*/, u_int /*PageNumber*/,
851 				  u_int /*PageType*/, uint32_t /*PageAddress*/,
852 				  bus_addr_t /*addr*/, bus_size_t/*len*/,
853 				  int /*sleep_ok*/, int /*timeout_ms*/);
854 int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
855 				    int /*PageNumber*/,
856 				    uint32_t /*PageAddress*/,
857 				    CONFIG_PAGE_HEADER *,
858 				    int /*sleep_ok*/, int /*timeout_ms*/);
859 int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
860 				  uint32_t /*PageAddress*/,
861 				  CONFIG_PAGE_HEADER *, size_t /*len*/,
862 				  int /*sleep_ok*/, int /*timeout_ms*/);
863 int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
864 				   uint32_t /*PageAddress*/,
865 				   CONFIG_PAGE_HEADER *, size_t /*len*/,
866 				   int /*sleep_ok*/, int /*timeout_ms*/);
867 static __inline int
868 mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
869 		      CONFIG_PAGE_HEADER *hdr, size_t len,
870 		      int sleep_ok, int timeout_ms)
871 {
872 	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
873 				  PageAddress, hdr, len, sleep_ok, timeout_ms));
874 }
875 
876 static __inline int
877 mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
878 		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
879 		       int timeout_ms)
880 {
881 	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
882 				   PageAddress, hdr, len, sleep_ok,
883 				   timeout_ms));
884 }
885 
886 /* mpt_debug.c functions */
887 void mpt_print_reply(void *vmsg);
888 void mpt_print_db(uint32_t mb);
889 void mpt_print_config_reply(void *vmsg);
890 char *mpt_ioc_diag(uint32_t diag);
891 void mpt_req_state(mpt_req_state_t state);
892 void mpt_print_config_request(void *vmsg);
893 void mpt_print_request(void *vmsg);
894 void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
895 void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
896 #endif /* _MPT_H_ */
897