xref: /freebsd/sys/dev/mpt/mpt.h (revision c128b2d129a8e305b673ef5e3da76af1fb91ae60)
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 	union ccb      *ccb;		/* CAM request */
284 	void	       *req_vbuf;	/* Virtual Address of Entry */
285 	void	       *sense_vbuf;	/* Virtual Address of sense data */
286 	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
287 	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
288 	bus_dmamap_t	dmap;		/* DMA map for data buffer */
289 	struct req_entry *chain;	/* for SGE overallocations */
290 };
291 
292 /**************************** Handler Registration ****************************/
293 /*
294  * Global table of registered reply handlers.  The
295  * handler is indicated by byte 3 of the request
296  * index submitted to the IOC.  This allows the
297  * driver core to perform generic processing without
298  * any knowledge of per-personality behavior.
299  *
300  * MPT_NUM_REPLY_HANDLERS must be a power of 2
301  * to allow the easy generation of a mask.
302  *
303  * The handler offsets used by the core are hard coded
304  * allowing faster code generation when assigning a handler
305  * to a request.  All "personalities" must use the
306  * the handler registration mechanism.
307  *
308  * The IOC handlers that are rarely executed are placed
309  * at the tail of the table to make it more likely that
310  * all commonly executed handlers fit in a single cache
311  * line.
312  */
313 #define MPT_NUM_REPLY_HANDLERS		(16)
314 #define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
315 #define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
316 #define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
317 typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
318 				 MSG_DEFAULT_REPLY *reply_frame);
319 typedef union {
320 	mpt_reply_handler_t	*reply_handler;
321 } mpt_handler_t;
322 
323 typedef enum {
324 	MPT_HANDLER_REPLY,
325 	MPT_HANDLER_EVENT,
326 	MPT_HANDLER_RESET,
327 	MPT_HANDLER_SHUTDOWN
328 } mpt_handler_type;
329 
330 struct mpt_handler_record
331 {
332 	LIST_ENTRY(mpt_handler_record)	links;
333 	mpt_handler_t			handler;
334 };
335 
336 LIST_HEAD(mpt_handler_list, mpt_handler_record);
337 
338 /*
339  * The handler_id is currently unused but would contain the
340  * handler ID used in the MsgContext field to allow direction
341  * of replies to the handler.  Registrations that don't require
342  * a handler id can pass in NULL for the handler_id.
343  *
344  * Deregistrations for handlers without a handler id should
345  * pass in MPT_HANDLER_ID_NONE.
346  */
347 #define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
348 int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
349 			 mpt_handler_t, uint32_t *);
350 int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
351 			   mpt_handler_t, uint32_t);
352 
353 /******************* Per-Controller Instance Data Structures ******************/
354 TAILQ_HEAD(req_queue, req_entry);
355 
356 /* Structure for saving proper values for modifyable PCI config registers */
357 struct mpt_pci_cfg {
358 	uint16_t Command;
359 	uint16_t LatencyTimer_LineSize;
360 	uint32_t IO_BAR;
361 	uint32_t Mem0_BAR[2];
362 	uint32_t Mem1_BAR[2];
363 	uint32_t ROM_BAR;
364 	uint8_t  IntLine;
365 	uint32_t PMCSR;
366 };
367 
368 typedef enum {
369 	MPT_RVF_NONE		= 0x0,
370 	MPT_RVF_ACTIVE		= 0x1,
371 	MPT_RVF_ANNOUNCED	= 0x2,
372 	MPT_RVF_UP2DATE		= 0x4,
373 	MPT_RVF_REFERENCED	= 0x8,
374 	MPT_RVF_WCE_CHANGED	= 0x10
375 } mpt_raid_volume_flags;
376 
377 struct mpt_raid_volume {
378 	CONFIG_PAGE_RAID_VOL_0	       *config_page;
379 	MPI_RAID_VOL_INDICATOR		sync_progress;
380 	mpt_raid_volume_flags		flags;
381 	u_int				quieced_disks;
382 };
383 
384 typedef enum {
385 	MPT_RDF_NONE		= 0x00,
386 	MPT_RDF_ACTIVE		= 0x01,
387 	MPT_RDF_ANNOUNCED	= 0x02,
388 	MPT_RDF_UP2DATE		= 0x04,
389 	MPT_RDF_REFERENCED	= 0x08,
390 	MPT_RDF_QUIESCING	= 0x10,
391 	MPT_RDF_QUIESCED	= 0x20
392 } mpt_raid_disk_flags;
393 
394 struct mpt_raid_disk {
395 	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
396 	struct mpt_raid_volume	       *volume;
397 	u_int				member_number;
398 	u_int				pass_thru_active;
399 	mpt_raid_disk_flags		flags;
400 };
401 
402 struct mpt_evtf_record {
403 	MSG_EVENT_NOTIFY_REPLY		reply;
404 	uint32_t			context;
405 	LIST_ENTRY(mpt_evtf_record)	links;
406 };
407 
408 LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
409 
410 struct mpt_softc {
411 	device_t		dev;
412 #if __FreeBSD_version < 500000
413 	int			mpt_splsaved;
414 	uint32_t		mpt_islocked;
415 #else
416 	struct mtx		mpt_lock;
417 #endif
418 	uint32_t		mpt_pers_mask;
419 	uint32_t		: 14,
420 		is_sas		: 1,
421 		raid_mwce_set	: 1,
422 		getreqwaiter	: 1,
423 		shutdwn_raid    : 1,
424 		shutdwn_recovery: 1,
425 		unit		: 8,
426 		outofbeer	: 1,
427 		mpt_locksetup	: 1,
428 		disabled	: 1,
429 		is_fc		: 1,
430 		bus		: 1;	/* FC929/1030 have two busses */
431 
432 	u_int			verbose;
433 
434 	/*
435 	 * IOC Facts
436 	 */
437 	uint16_t	mpt_global_credits;
438 	uint16_t	request_frame_size;
439 	uint8_t		mpt_max_devices;
440 	uint8_t		mpt_max_buses;
441 	uint8_t		ioc_facts_flags;
442 
443 	/*
444 	 * Port Facts
445 	 * XXX - Add multi-port support!.
446 	 */
447 	uint16_t	mpt_ini_id;
448 	uint16_t	mpt_port_type;
449 	uint16_t	mpt_proto_flags;
450 
451 	/*
452 	 * Device Configuration Information
453 	 */
454 	union {
455 		struct mpt_spi_cfg {
456 			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
457 			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
458 			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
459 			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
460 			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
461 			uint16_t			_tag_enable;
462 			uint16_t			_disc_enable;
463 			uint16_t			_update_params0;
464 			uint16_t			_update_params1;
465 		} spi;
466 #define	mpt_port_page0		cfg.spi._port_page0
467 #define	mpt_port_page1		cfg.spi._port_page1
468 #define	mpt_port_page2		cfg.spi._port_page2
469 #define	mpt_dev_page0		cfg.spi._dev_page0
470 #define	mpt_dev_page1		cfg.spi._dev_page1
471 #define	mpt_tag_enable		cfg.spi._tag_enable
472 #define	mpt_disc_enable		cfg.spi._disc_enable
473 #define	mpt_update_params0	cfg.spi._update_params0
474 #define	mpt_update_params1	cfg.spi._update_params1
475 		struct mpi_fc_cfg {
476 			uint8_t	nada;
477 		} fc;
478 	} cfg;
479 
480 	/* Controller Info */
481 	CONFIG_PAGE_IOC_2 *	ioc_page2;
482 	CONFIG_PAGE_IOC_3 *	ioc_page3;
483 
484 	/* Raid Data */
485 	struct mpt_raid_volume* raid_volumes;
486 	struct mpt_raid_disk*	raid_disks;
487 	u_int			raid_max_volumes;
488 	u_int			raid_max_disks;
489 	u_int			raid_page0_len;
490 	u_int			raid_wakeup;
491 	u_int			raid_rescan;
492 	u_int			raid_resync_rate;
493 	u_int			raid_mwce_setting;
494 	u_int			raid_queue_depth;
495 	u_int			raid_nonopt_volumes;
496 	struct proc	       *raid_thread;
497 	struct callout		raid_timer;
498 
499 	/*
500 	 * PCI Hardware info
501 	 */
502 	struct resource *	pci_irq;	/* Interrupt map for chip */
503 	void *			ih;		/* Interupt handle */
504 	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
505 
506 	/*
507 	 * DMA Mapping Stuff
508 	 */
509 	struct resource *	pci_reg;	/* Register map for chip */
510 	int			pci_mem_rid;	/* Resource ID */
511 	bus_space_tag_t		pci_st;		/* Bus tag for registers */
512 	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
513 	/* PIO versions of above. */
514 	int			pci_pio_rid;
515 	struct resource *	pci_pio_reg;
516 	bus_space_tag_t		pci_pio_st;
517 	bus_space_handle_t	pci_pio_sh;
518 
519 	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
520 	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
521 	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
522 	uint8_t		       *reply;		/* KVA of reply memory */
523 	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
524 
525 	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
526 	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
527 	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
528 	uint8_t		       *request;	/* KVA of Request memory */
529 	bus_addr_t		request_phys;	/* BusADdr of request memory */
530 
531 	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
532 
533 	/*
534 	 * Hardware management
535 	 */
536 	u_int			reset_cnt;
537 
538 	/*
539 	 * CAM && Software Management
540 	 */
541 	request_t	       *request_pool;
542 	struct req_queue	request_free_list;
543 	struct req_queue	request_pending_list;
544 	struct req_queue	request_timeout_list;
545 
546 	/*
547 	 * Deferred frame acks due to resource shortage.
548 	 */
549 	struct mpt_evtf_list	ack_frames;
550 
551 
552 	struct cam_sim	       *sim;
553 	struct cam_path	       *path;
554 
555 	struct cam_sim	       *phydisk_sim;
556 	struct cam_path	       *phydisk_path;
557 
558 	struct proc	       *recovery_thread;
559 	request_t	       *tmf_req;
560 
561 	uint32_t		sequence;	/* Sequence Number */
562 	uint32_t		timeouts;	/* timeout count */
563 	uint32_t		success;	/* successes afer timeout */
564 
565 	/* Opposing port in a 929 or 1030, or NULL */
566 	struct mpt_softc *	mpt2;
567 
568 	/* FW Image management */
569 	uint32_t		fw_image_size;
570 	uint8_t		       *fw_image;
571 	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
572 	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
573 	bus_addr_t		fw_phys;	/* BusAddr of request memory */
574 
575 	/* Shutdown Event Handler. */
576 	eventhandler_tag         eh;
577 
578 	TAILQ_ENTRY(mpt_softc)	links;
579 };
580 
581 /***************************** Locking Primatives *****************************/
582 #if __FreeBSD_version < 500000
583 #define	MPT_IFLAGS		INTR_TYPE_CAM
584 #define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
585 #define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
586 #define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
587 #define	CAMLOCK_2_MPTLOCK	MPT_LOCK
588 #define	MPT_LOCK_SETUP(mpt)
589 #define	MPT_LOCK_DESTROY(mpt)
590 
591 static __inline void mpt_lockspl(struct mpt_softc *mpt);
592 static __inline void mpt_unlockspl(struct mpt_softc *mpt);
593 
594 static __inline void
595 mpt_lockspl(struct mpt_softc *mpt)
596 {
597        int s;
598 
599        s = splcam();
600        if (mpt->mpt_islocked++ == 0) {
601                mpt->mpt_splsaved = s;
602        } else {
603                splx(s);
604 	       panic("Recursed lock with mask: 0x%x\n", s);
605        }
606 }
607 
608 static __inline void
609 mpt_unlockspl(struct mpt_softc *mpt)
610 {
611        if (mpt->mpt_islocked) {
612                if (--mpt->mpt_islocked == 0) {
613                        splx(mpt->mpt_splsaved);
614                }
615        } else
616 	       panic("Negative lock count\n");
617 }
618 
619 static __inline int
620 mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
621 	   const char *wmesg, int timo)
622 {
623 	int saved_cnt;
624 	int saved_spl;
625 	int error;
626 
627 	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
628 	saved_cnt = mpt->mpt_islocked;
629 	saved_spl = mpt->mpt_splsaved;
630 	mpt->mpt_islocked = 0;
631 	error = tsleep(ident, priority, wmesg, timo);
632 	KASSERT(mpt->mpt_islocked = 0, ("Invalid lock count on wakeup"));
633 	mpt->mpt_islocked = saved_cnt;
634 	mpt->mpt_splsaved = saved_spl;
635 	return (error);
636 }
637 
638 #else
639 #ifdef	LOCKING_WORKED_AS_IT_SHOULD
640 #error "Shouldn't Be Here!"
641 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
642 #define	MPT_LOCK_SETUP(mpt)						\
643 		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
644 		mpt->mpt_locksetup = 1
645 #define	MPT_LOCK_DESTROY(mpt)						\
646 	if (mpt->mpt_locksetup) {					\
647 		mtx_destroy(&mpt->mpt_lock);				\
648 		mpt->mpt_locksetup = 0;					\
649 	}
650 
651 #define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
652 #define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
653 #define	MPTLOCK_2_CAMLOCK(mpt)	\
654 	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
655 #define	CAMLOCK_2_MPTLOCK(mpt)	\
656 	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
657 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
658 	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
659 #else
660 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
661 #define	MPT_LOCK_SETUP(mpt)	do { } while (0)
662 #define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
663 #define	MPT_LOCK(mpt)		do { } while (0)
664 #define	MPT_UNLOCK(mpt)		do { } while (0)
665 #define	MPTLOCK_2_CAMLOCK(mpt)	do { } while (0)
666 #define	CAMLOCK_2_MPTLOCK(mpt)	do { } while (0)
667 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
668 	tsleep(ident, priority, wmesg, timo)
669 #endif
670 #endif
671 
672 /******************************* Register Access ******************************/
673 static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
674 static __inline uint32_t mpt_read(struct mpt_softc *, int);
675 static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
676 static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
677 
678 static __inline void
679 mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
680 {
681 	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
682 }
683 
684 static __inline uint32_t
685 mpt_read(struct mpt_softc *mpt, int offset)
686 {
687 	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
688 }
689 
690 /*
691  * Some operations (e.g. diagnostic register writes while the ARM proccessor
692  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
693  * busses, these operations likely map to normal register accesses.
694  */
695 static __inline void
696 mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
697 {
698 	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
699 }
700 
701 static __inline uint32_t
702 mpt_pio_read(struct mpt_softc *mpt, int offset)
703 {
704 	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
705 }
706 /*********************** Reply Frame/Request Management ***********************/
707 /* Max MPT Reply we are willing to accept (must be power of 2) */
708 #define MPT_REPLY_SIZE   	256
709 
710 #define MPT_MAX_REQUESTS(mpt)	512
711 #define MPT_REQUEST_AREA	512
712 #define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
713 #define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
714 
715 #define MPT_CONTEXT_CB_SHIFT	(16)
716 #define MPT_CBI(handle)	(handle >> MPT_CONTEXT_CB_SHIFT)
717 #define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
718 #define MPT_CONTEXT_TO_CBI(x)	\
719     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
720 #define MPT_CONTEXT_REQI_MASK 0xFFFF
721 #define MPT_CONTEXT_TO_REQI(x)	\
722     ((x) & MPT_CONTEXT_REQI_MASK)
723 
724 /*
725  * Convert a 32bit physical address returned from IOC to an
726  * offset into our reply frame memory or the kvm address needed
727  * to access the data.  The returned address is only the low
728  * 32 bits, so mask our base physical address accordingly.
729  */
730 #define MPT_REPLY_BADDR(x)		\
731 	(x << 1)
732 #define MPT_REPLY_OTOV(m, i) 		\
733 	((void *)(&m->reply[i]))
734 
735 #define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
736 do {							\
737 	if (mpt->verbose >= MPT_PRT_DEBUG)		\
738 		mpt_dump_reply_frame(mpt, reply_frame);	\
739 } while(0)
740 
741 static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
742 static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
743 
744 /*
745  * Give the reply buffer back to the IOC after we have
746  * finished processing it.
747  */
748 static __inline void
749 mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
750 {
751      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
752 }
753 
754 /* Get a reply from the IOC */
755 static __inline uint32_t
756 mpt_pop_reply_queue(struct mpt_softc *mpt)
757 {
758      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
759 }
760 
761 void mpt_complete_request_chain(struct mpt_softc *mpt,
762 				struct req_queue *chain, u_int iocstatus);
763 /************************** Scatter Gather Managment **************************/
764 /* MPT_RQSL- size of request frame, in bytes */
765 #define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
766 
767 /* MPT_NSGL- how many SG entries can fit in a request frame size */
768 #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
769 
770 /* MPT_NRFM- how many request frames can fit in each request alloc we make */
771 #define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
772 
773 /*
774  * MPT_NSGL_FIRST- # of SG elements that can fit after
775  * an I/O request but still within the request frame.
776  * Do this safely based upon SGE_IO_UNION.
777  *
778  * Note that the first element is *within* the SCSI request.
779  */
780 #define	MPT_NSGL_FIRST(mpt)	\
781     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
782     sizeof (SGE_IO_UNION))
783 
784 /***************************** IOC Initialization *****************************/
785 int mpt_reset(struct mpt_softc *, int /*reinit*/);
786 
787 /****************************** Debugging/Logging *****************************/
788 typedef struct mpt_decode_entry {
789 	char    *name;
790 	u_int	 value;
791 	u_int	 mask;
792 } mpt_decode_entry_t;
793 
794 int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
795 		     const char *name, u_int value, u_int *cur_column,
796 		     u_int wrap_point);
797 
798 enum {
799 	MPT_PRT_ALWAYS,
800 	MPT_PRT_FATAL,
801 	MPT_PRT_ERROR,
802 	MPT_PRT_WARN,
803 	MPT_PRT_INFO,
804 	MPT_PRT_DEBUG,
805 	MPT_PRT_TRACE,
806 	MPT_PRT_NONE=100
807 };
808 
809 #define mpt_lprt(mpt, level, ...)		\
810 do {						\
811 	if (level <= (mpt)->verbose)		\
812 		mpt_prt(mpt, __VA_ARGS__);	\
813 } while (0)
814 
815 #define mpt_lprtc(mpt, level, ...)		 \
816 do {						 \
817 	if (level <= (mpt)->debug_level)	 \
818 		mpt_prtc(mpt, __VA_ARGS__);	 \
819 } while (0)
820 
821 void mpt_prt(struct mpt_softc *, const char *, ...);
822 void mpt_prtc(struct mpt_softc *, const char *, ...);
823 
824 /**************************** Unclassified Routines ***************************/
825 void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
826 int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
827 					 size_t reply_len, void *reply);
828 int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
829 			     mpt_req_state_t state, mpt_req_state_t mask,
830 			     int sleep_ok, int time_ms);
831 void		mpt_enable_ints(struct mpt_softc *mpt);
832 void		mpt_disable_ints(struct mpt_softc *mpt);
833 int		mpt_attach(struct mpt_softc *mpt);
834 int		mpt_shutdown(struct mpt_softc *mpt);
835 int		mpt_detach(struct mpt_softc *mpt);
836 int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
837 				       size_t len, void *cmd);
838 request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
839 void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
840 void		mpt_intr(void *arg);
841 void		mpt_check_doorbell(struct mpt_softc *mpt);
842 void		mpt_dump_reply_frame(struct mpt_softc *mpt,
843 				     MSG_DEFAULT_REPLY *reply_frame);
844 
845 void		mpt_set_config_regs(struct mpt_softc *);
846 int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
847 				  u_int /*Action*/, u_int /*PageVersion*/,
848 				  u_int /*PageLength*/, u_int /*PageNumber*/,
849 				  u_int /*PageType*/, uint32_t /*PageAddress*/,
850 				  bus_addr_t /*addr*/, bus_size_t/*len*/,
851 				  int /*sleep_ok*/, int /*timeout_ms*/);
852 int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
853 				    int /*PageNumber*/,
854 				    uint32_t /*PageAddress*/,
855 				    CONFIG_PAGE_HEADER *,
856 				    int /*sleep_ok*/, int /*timeout_ms*/);
857 int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
858 				  uint32_t /*PageAddress*/,
859 				  CONFIG_PAGE_HEADER *, size_t /*len*/,
860 				  int /*sleep_ok*/, int /*timeout_ms*/);
861 int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
862 				   uint32_t /*PageAddress*/,
863 				   CONFIG_PAGE_HEADER *, size_t /*len*/,
864 				   int /*sleep_ok*/, int /*timeout_ms*/);
865 static __inline int
866 mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
867 		      CONFIG_PAGE_HEADER *hdr, size_t len,
868 		      int sleep_ok, int timeout_ms)
869 {
870 	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
871 				  PageAddress, hdr, len, sleep_ok, timeout_ms));
872 }
873 
874 static __inline int
875 mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
876 		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
877 		       int timeout_ms)
878 {
879 	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
880 				   PageAddress, hdr, len, sleep_ok,
881 				   timeout_ms));
882 }
883 
884 /* mpt_debug.c functions */
885 void mpt_print_reply(void *vmsg);
886 void mpt_print_db(uint32_t mb);
887 void mpt_print_config_reply(void *vmsg);
888 char *mpt_ioc_diag(uint32_t diag);
889 void mpt_req_state(mpt_req_state_t state);
890 void mpt_print_config_request(void *vmsg);
891 void mpt_print_request(void *vmsg);
892 void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
893 void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
894 #endif /* _MPT_H_ */
895