xref: /freebsd/sys/dev/mpt/mpt.h (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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  * Support from LSI-Logic has also gone a great deal toward making this a
64  * workable subsystem and is gratefully acknowledged.
65  */
66 /*
67  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
68  * Copyright (c) 2004, 2005 Justin T. Gibbs
69  * Copyright (c) 2005, WHEEL Sp. z o.o.
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions are
74  * met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
78  *    substantially similar to the "NO WARRANTY" disclaimer below
79  *    ("Disclaimer") and any redistribution must be conditioned upon including
80  *    a substantially similar Disclaimer requirement for further binary
81  *    redistribution.
82  * 3. Neither the names of the above listed copyright holders nor the names
83  *    of any contributors may be used to endorse or promote products derived
84  *    from this software without specific prior written permission.
85  *
86  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
90  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
96  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  */
98 
99 #ifndef _MPT_H_
100 #define _MPT_H_
101 
102 /********************************* OS Includes ********************************/
103 #include <sys/types.h>
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/endian.h>
107 #include <sys/eventhandler.h>
108 #if __FreeBSD_version < 500000
109 #include <sys/kernel.h>
110 #include <sys/queue.h>
111 #include <sys/malloc.h>
112 #include <sys/devicestat.h>
113 #else
114 #include <sys/lock.h>
115 #include <sys/kernel.h>
116 #include <sys/queue.h>
117 #include <sys/malloc.h>
118 #include <sys/mutex.h>
119 #include <sys/condvar.h>
120 #endif
121 #include <sys/proc.h>
122 #include <sys/bus.h>
123 #include <sys/module.h>
124 
125 #include <machine/cpu.h>
126 #include <machine/resource.h>
127 
128 #if __FreeBSD_version < 500000
129 #include <machine/bus.h>
130 #include <machine/clock.h>
131 #endif
132 
133 #include <sys/rman.h>
134 
135 #if __FreeBSD_version < 500000
136 #include <pci/pcireg.h>
137 #include <pci/pcivar.h>
138 #else
139 #include <dev/pci/pcireg.h>
140 #include <dev/pci/pcivar.h>
141 #endif
142 
143 #include <machine/bus.h>
144 #include "opt_ddb.h"
145 
146 /**************************** Register Definitions ****************************/
147 #include <dev/mpt/mpt_reg.h>
148 
149 /******************************* MPI Definitions ******************************/
150 #include <dev/mpt/mpilib/mpi_type.h>
151 #include <dev/mpt/mpilib/mpi.h>
152 #include <dev/mpt/mpilib/mpi_cnfg.h>
153 #include <dev/mpt/mpilib/mpi_ioc.h>
154 #include <dev/mpt/mpilib/mpi_raid.h>
155 
156 /* XXX For mpt_debug.c */
157 #include <dev/mpt/mpilib/mpi_init.h>
158 
159 #define	MPT_S64_2_SCALAR(y)	((((int64_t)y.High) << 32) | (y.Low))
160 #define	MPT_U64_2_SCALAR(y)	((((uint64_t)y.High) << 32) | (y.Low))
161 
162 /****************************** Misc Definitions ******************************/
163 /* #define MPT_TEST_MULTIPATH	1 */
164 #define MPT_OK (0)
165 #define MPT_FAIL (0x10000)
166 
167 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
168 
169 #define	MPT_ROLE_NONE		0
170 #define	MPT_ROLE_INITIATOR	1
171 #define	MPT_ROLE_TARGET		2
172 #define	MPT_ROLE_BOTH		3
173 #define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
174 
175 /**************************** Forward Declarations ****************************/
176 struct mpt_softc;
177 struct mpt_personality;
178 typedef struct req_entry request_t;
179 
180 /************************* Personality Module Support *************************/
181 typedef int mpt_load_handler_t(struct mpt_personality *);
182 typedef int mpt_probe_handler_t(struct mpt_softc *);
183 typedef int mpt_attach_handler_t(struct mpt_softc *);
184 typedef int mpt_enable_handler_t(struct mpt_softc *);
185 typedef void mpt_ready_handler_t(struct mpt_softc *);
186 typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
187 				MSG_EVENT_NOTIFY_REPLY *);
188 typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
189 /* XXX Add return value and use for veto? */
190 typedef void mpt_shutdown_handler_t(struct mpt_softc *);
191 typedef void mpt_detach_handler_t(struct mpt_softc *);
192 typedef int mpt_unload_handler_t(struct mpt_personality *);
193 
194 struct mpt_personality
195 {
196 	const char		*name;
197 	uint32_t		 id;		/* Assigned identifier. */
198 	u_int			 use_count;	/* Instances using personality*/
199 	mpt_load_handler_t	*load;		/* configure personailty */
200 #define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
201 	mpt_probe_handler_t	*probe;		/* configure personailty */
202 	mpt_attach_handler_t	*attach;	/* initialize device instance */
203 	mpt_enable_handler_t	*enable;	/* enable device */
204 	mpt_ready_handler_t	*ready;		/* final open for business */
205 	mpt_event_handler_t	*event;		/* Handle MPI event. */
206 	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
207 	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
208 	mpt_detach_handler_t	*detach;	/* release device instance */
209 	mpt_unload_handler_t	*unload;	/* Shutdown personality */
210 #define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
211 };
212 
213 int mpt_modevent(module_t, int, void *);
214 
215 /* Maximum supported number of personalities. */
216 #define MPT_MAX_PERSONALITIES	(15)
217 
218 #define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
219 	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
220 
221 #define DECLARE_MPT_PERSONALITY(name, order)				  \
222 	static moduledata_t name##_mod = {				  \
223 		#name, mpt_modevent, &name##_personality		  \
224 	};								  \
225 	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
226 	MODULE_VERSION(name, 1);					  \
227 	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
228 
229 /******************************* Bus DMA Support ******************************/
230 /* XXX Need to update bus_dmamap_sync to take a range argument. */
231 #define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
232 	bus_dmamap_sync(dma_tag, dmamap, op)
233 
234 #if __FreeBSD_version < 600000
235 #define	bus_get_dma_tag(x)	NULL
236 #endif
237 #if __FreeBSD_version >= 501102
238 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
239 			   lowaddr, highaddr, filter, filterarg,	\
240 			   maxsize, nsegments, maxsegsz, flags,		\
241 			   dma_tagp)					\
242 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
243 			   lowaddr, highaddr, filter, filterarg,	\
244 			   maxsize, nsegments, maxsegsz, flags,		\
245 			   busdma_lock_mutex, &(mpt)->mpt_lock,		\
246 			   dma_tagp)
247 #else
248 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
249 			   lowaddr, highaddr, filter, filterarg,	\
250 			   maxsize, nsegments, maxsegsz, flags,		\
251 			   dma_tagp)					\
252 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
253 			   lowaddr, highaddr, filter, filterarg,	\
254 			   maxsize, nsegments, maxsegsz, flags,		\
255 			   dma_tagp)
256 #endif
257 
258 struct mpt_map_info {
259 	struct mpt_softc *mpt;
260 	int		  error;
261 	uint32_t	  phys;
262 };
263 
264 void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
265 /* **************************** NewBUS interrupt Crock ************************/
266 #if __FreeBSD_version < 700031
267 #define	mpt_setup_intr(d, i, f, U, if, ifa, hp)	\
268 	bus_setup_intr(d, i, f, if, ifa, hp)
269 #else
270 #define	mpt_setup_intr	bus_setup_intr
271 #endif
272 
273 /**************************** Kernel Thread Support ***************************/
274 #if __FreeBSD_version > 500005
275 #if __FreeBSD_version > 800001
276 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
277 	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
278 #else
279 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
280 	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
281 #endif
282 #else
283 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
284 	kthread_create(func, farg, proc_ptr, fmtstr, arg)
285 #endif
286 
287 /****************************** Timer Facilities ******************************/
288 #if __FreeBSD_version > 500000
289 #define mpt_callout_init(c)	callout_init(c, /*mpsafe*/1);
290 #else
291 #define mpt_callout_init(c)	callout_init(c);
292 #endif
293 
294 /********************************** Endianess *********************************/
295 #define	MPT_2_HOST64(ptr, tag)	ptr->tag = le64toh(ptr->tag)
296 #define	MPT_2_HOST32(ptr, tag)	ptr->tag = le32toh(ptr->tag)
297 #define	MPT_2_HOST16(ptr, tag)	ptr->tag = le16toh(ptr->tag)
298 
299 #define	HOST_2_MPT64(ptr, tag)	ptr->tag = htole64(ptr->tag)
300 #define	HOST_2_MPT32(ptr, tag)	ptr->tag = htole32(ptr->tag)
301 #define	HOST_2_MPT16(ptr, tag)	ptr->tag = htole16(ptr->tag)
302 
303 #if	_BYTE_ORDER == _BIG_ENDIAN
304 void mpt2host_sge_simple_union(SGE_SIMPLE_UNION *);
305 void mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *);
306 void mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *);
307 void mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *);
308 void mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *);
309 void mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *);
310 #else
311 #define	mpt2host_sge_simple_union(x)		do { ; } while (0)
312 #define	mpt2host_iocfacts_reply(x)		do { ; } while (0)
313 #define	mpt2host_portfacts_reply(x)		do { ; } while (0)
314 #define	mpt2host_config_page_ioc2(x)		do { ; } while (0)
315 #define	mpt2host_config_page_raid_vol_0(x)	do { ; } while (0)
316 #define	mpt2host_mpi_raid_vol_indicator(x)	do { ; } while (0)
317 #endif
318 
319 /**************************** MPI Transaction State ***************************/
320 typedef enum {
321 	REQ_STATE_NIL		= 0x00,
322 	REQ_STATE_FREE		= 0x01,
323 	REQ_STATE_ALLOCATED	= 0x02,
324 	REQ_STATE_QUEUED	= 0x04,
325 	REQ_STATE_DONE		= 0x08,
326 	REQ_STATE_TIMEDOUT	= 0x10,
327 	REQ_STATE_NEED_WAKEUP	= 0x20,
328 	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
329 	REQ_STATE_MASK		= 0xFF
330 } mpt_req_state_t;
331 
332 struct req_entry {
333 	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
334 	mpt_req_state_t	state;		/* Request State Information */
335 	uint16_t	index;		/* Index of this entry */
336 	uint16_t	IOCStatus;	/* Completion status */
337 	uint16_t	ResponseCode;	/* TMF Reponse Code */
338 	uint16_t	serno;		/* serial number */
339 	union ccb      *ccb;		/* CAM request */
340 	void	       *req_vbuf;	/* Virtual Address of Entry */
341 	void	       *sense_vbuf;	/* Virtual Address of sense data */
342 	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
343 	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
344 	bus_dmamap_t	dmap;		/* DMA map for data buffers */
345 	struct req_entry *chain;	/* for SGE overallocations */
346 	struct callout  callout;	/* Timeout for the request */
347 };
348 
349 typedef struct mpt_config_params {
350 	u_int		Action;
351 	u_int		PageVersion;
352 	u_int		PageLength;
353 	u_int		PageNumber;
354 	u_int		PageType;
355 	u_int		PageAddress;
356 	u_int		ExtPageLength;
357 	u_int		ExtPageType;
358 } cfgparms_t;
359 
360 /**************************** MPI Target State Info ***************************/
361 
362 typedef struct {
363 	uint32_t reply_desc;	/* current reply descriptor */
364 	uint32_t resid;		/* current data residual */
365 	uint32_t bytes_xfered;	/* current relative offset */
366 	union ccb *ccb;		/* pointer to currently active ccb */
367 	request_t *req;		/* pointer to currently active assist request */
368 	uint32_t
369 		is_local : 1,
370 		nxfers	 : 31;
371 	uint32_t tag_id;
372 	enum {
373 		TGT_STATE_NIL,
374 		TGT_STATE_LOADING,
375 		TGT_STATE_LOADED,
376 		TGT_STATE_IN_CAM,
377                 TGT_STATE_SETTING_UP_FOR_DATA,
378                 TGT_STATE_MOVING_DATA,
379                 TGT_STATE_MOVING_DATA_AND_STATUS,
380                 TGT_STATE_SENDING_STATUS
381 	} state;
382 } mpt_tgt_state_t;
383 
384 /*
385  * When we get an incoming command it has its own tag which is called the
386  * IoIndex. This is the value we gave that particular command buffer when
387  * we originally assigned it. It's just a number, really. The FC card uses
388  * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
389  * contains pointers the request_t structures related to that IoIndex.
390  *
391  * What *we* do is construct a tag out of the index for the target command
392  * which owns the incoming ATIO plus a rolling sequence number.
393  */
394 #define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
395  ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
396 
397 #ifdef	INVARIANTS
398 #define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
399 #else
400 #define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
401 #endif
402 
403 #define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
404     (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
405 
406 STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
407 #define	MPT_MAX_LUNS	256
408 typedef struct {
409 	struct mpt_hdr_stailq	atios;
410 	struct mpt_hdr_stailq	inots;
411 	int enabled;
412 } tgt_resource_t;
413 #define	MPT_MAX_ELS	64
414 
415 /**************************** Handler Registration ****************************/
416 /*
417  * Global table of registered reply handlers.  The
418  * handler is indicated by byte 3 of the request
419  * index submitted to the IOC.  This allows the
420  * driver core to perform generic processing without
421  * any knowledge of per-personality behavior.
422  *
423  * MPT_NUM_REPLY_HANDLERS must be a power of 2
424  * to allow the easy generation of a mask.
425  *
426  * The handler offsets used by the core are hard coded
427  * allowing faster code generation when assigning a handler
428  * to a request.  All "personalities" must use the
429  * the handler registration mechanism.
430  *
431  * The IOC handlers that are rarely executed are placed
432  * at the tail of the table to make it more likely that
433  * all commonly executed handlers fit in a single cache
434  * line.
435  */
436 #define MPT_NUM_REPLY_HANDLERS		(32)
437 #define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
438 #define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
439 #define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
440 typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
441     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
442 typedef union {
443 	mpt_reply_handler_t	*reply_handler;
444 } mpt_handler_t;
445 
446 typedef enum {
447 	MPT_HANDLER_REPLY,
448 	MPT_HANDLER_EVENT,
449 	MPT_HANDLER_RESET,
450 	MPT_HANDLER_SHUTDOWN
451 } mpt_handler_type;
452 
453 struct mpt_handler_record
454 {
455 	LIST_ENTRY(mpt_handler_record)	links;
456 	mpt_handler_t			handler;
457 };
458 
459 LIST_HEAD(mpt_handler_list, mpt_handler_record);
460 
461 /*
462  * The handler_id is currently unused but would contain the
463  * handler ID used in the MsgContext field to allow direction
464  * of replies to the handler.  Registrations that don't require
465  * a handler id can pass in NULL for the handler_id.
466  *
467  * Deregistrations for handlers without a handler id should
468  * pass in MPT_HANDLER_ID_NONE.
469  */
470 #define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
471 int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
472 			 mpt_handler_t, uint32_t *);
473 int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
474 			   mpt_handler_t, uint32_t);
475 
476 /******************* Per-Controller Instance Data Structures ******************/
477 TAILQ_HEAD(req_queue, req_entry);
478 
479 /* Structure for saving proper values for modifyable PCI config registers */
480 struct mpt_pci_cfg {
481 	uint16_t Command;
482 	uint16_t LatencyTimer_LineSize;
483 	uint32_t IO_BAR;
484 	uint32_t Mem0_BAR[2];
485 	uint32_t Mem1_BAR[2];
486 	uint32_t ROM_BAR;
487 	uint8_t  IntLine;
488 	uint32_t PMCSR;
489 };
490 
491 typedef enum {
492 	MPT_RVF_NONE		= 0x0,
493 	MPT_RVF_ACTIVE		= 0x1,
494 	MPT_RVF_ANNOUNCED	= 0x2,
495 	MPT_RVF_UP2DATE		= 0x4,
496 	MPT_RVF_REFERENCED	= 0x8,
497 	MPT_RVF_WCE_CHANGED	= 0x10
498 } mpt_raid_volume_flags;
499 
500 struct mpt_raid_volume {
501 	CONFIG_PAGE_RAID_VOL_0	       *config_page;
502 	MPI_RAID_VOL_INDICATOR		sync_progress;
503 	mpt_raid_volume_flags		flags;
504 	u_int				quiesced_disks;
505 };
506 
507 typedef enum {
508 	MPT_RDF_NONE		= 0x00,
509 	MPT_RDF_ACTIVE		= 0x01,
510 	MPT_RDF_ANNOUNCED	= 0x02,
511 	MPT_RDF_UP2DATE		= 0x04,
512 	MPT_RDF_REFERENCED	= 0x08,
513 	MPT_RDF_QUIESCING	= 0x10,
514 	MPT_RDF_QUIESCED	= 0x20
515 } mpt_raid_disk_flags;
516 
517 struct mpt_raid_disk {
518 	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
519 	struct mpt_raid_volume	       *volume;
520 	u_int				member_number;
521 	u_int				pass_thru_active;
522 	mpt_raid_disk_flags		flags;
523 };
524 
525 struct mpt_evtf_record {
526 	MSG_EVENT_NOTIFY_REPLY		reply;
527 	uint32_t			context;
528 	LIST_ENTRY(mpt_evtf_record)	links;
529 };
530 
531 LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
532 
533 struct mptsas_devinfo {
534 	uint16_t	dev_handle;
535 	uint16_t	parent_dev_handle;
536 	uint16_t	enclosure_handle;
537 	uint16_t	slot;
538 	uint8_t		phy_num;
539 	uint8_t		physical_port;
540 	uint8_t		target_id;
541 	uint8_t		bus;
542 	uint64_t	sas_address;
543 	uint32_t	device_info;
544 };
545 
546 struct mptsas_phyinfo {
547 	uint16_t	handle;
548 	uint8_t		phy_num;
549 	uint8_t		port_id;
550 	uint8_t		negotiated_link_rate;
551 	uint8_t		hw_link_rate;
552 	uint8_t		programmed_link_rate;
553 	uint8_t		sas_port_add_phy;
554 	struct mptsas_devinfo identify;
555 	struct mptsas_devinfo attached;
556 };
557 
558 struct mptsas_portinfo {
559 	uint16_t			num_phys;
560 	struct mptsas_phyinfo		*phy_info;
561 };
562 
563 struct mpt_softc {
564 	device_t		dev;
565 #if __FreeBSD_version < 500000
566 	uint32_t		mpt_islocked;
567 	int			mpt_splsaved;
568 #else
569 	struct mtx		mpt_lock;
570 	int			mpt_locksetup;
571 #endif
572 	uint32_t		mpt_pers_mask;
573 	uint32_t
574 				: 8,
575 		unit		: 8,
576 		ready		: 1,
577 		fw_uploaded	: 1,
578 		msi_enable	: 1,
579 		twildcard	: 1,
580 		tenabled	: 1,
581 		do_cfg_role	: 1,
582 		raid_enabled	: 1,
583 		raid_mwce_set	: 1,
584 		getreqwaiter	: 1,
585 		shutdwn_raid    : 1,
586 		shutdwn_recovery: 1,
587 		outofbeer	: 1,
588 		disabled	: 1,
589 		is_spi		: 1,
590 		is_sas		: 1,
591 		is_fc		: 1;
592 
593 	u_int			cfg_role;
594 	u_int			role;	/* role: none, ini, target, both */
595 
596 	u_int			verbose;
597 #ifdef	MPT_TEST_MULTIPATH
598 	int			failure_id;
599 #endif
600 
601 	/*
602 	 * IOC Facts
603 	 */
604 	MSG_IOC_FACTS_REPLY	ioc_facts;
605 
606 	/*
607 	 * Port Facts
608 	 */
609 	MSG_PORT_FACTS_REPLY *	port_facts;
610 #define	mpt_ini_id	port_facts[0].PortSCSIID
611 #define	mpt_max_tgtcmds	port_facts[0].MaxPostedCmdBuffers
612 
613 	/*
614 	 * Device Configuration Information
615 	 */
616 	union {
617 		struct mpt_spi_cfg {
618 			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
619 			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
620 			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
621 			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
622 			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
623 			uint16_t			_tag_enable;
624 			uint16_t			_disc_enable;
625 		} spi;
626 #define	mpt_port_page0		cfg.spi._port_page0
627 #define	mpt_port_page1		cfg.spi._port_page1
628 #define	mpt_port_page2		cfg.spi._port_page2
629 #define	mpt_dev_page0		cfg.spi._dev_page0
630 #define	mpt_dev_page1		cfg.spi._dev_page1
631 #define	mpt_tag_enable		cfg.spi._tag_enable
632 #define	mpt_disc_enable		cfg.spi._disc_enable
633 		struct mpi_fc_cfg {
634 			CONFIG_PAGE_FC_PORT_0 _port_page0;
635 			uint32_t _port_speed;
636 #define	mpt_fcport_page0	cfg.fc._port_page0
637 #define	mpt_fcport_speed	cfg.fc._port_speed
638 		} fc;
639 	} cfg;
640 #if __FreeBSD_version >= 500000
641 	/*
642 	 * Device config information stored up for sysctl to access
643 	 */
644 	union {
645 		struct {
646 			unsigned int initiator_id;
647 		} spi;
648 		struct {
649 			char wwnn[19];
650 			char wwpn[19];
651 		} fc;
652 	} scinfo;
653 #endif
654 
655 	/* Controller Info for RAID information */
656 	CONFIG_PAGE_IOC_2 *	ioc_page2;
657 	CONFIG_PAGE_IOC_3 *	ioc_page3;
658 
659 	/* Raid Data */
660 	struct mpt_raid_volume* raid_volumes;
661 	struct mpt_raid_disk*	raid_disks;
662 	u_int			raid_max_volumes;
663 	u_int			raid_max_disks;
664 	u_int			raid_page0_len;
665 	u_int			raid_wakeup;
666 	u_int			raid_rescan;
667 	u_int			raid_resync_rate;
668 	u_int			raid_mwce_setting;
669 	u_int			raid_queue_depth;
670 	u_int			raid_nonopt_volumes;
671 	struct proc	       *raid_thread;
672 	struct callout		raid_timer;
673 
674 	/*
675 	 * PCI Hardware info
676 	 */
677 	int			pci_msi_count;
678 	struct resource *	pci_irq;	/* Interrupt map for chip */
679 	void *			ih;		/* Interupt handle */
680 	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
681 
682 	/*
683 	 * DMA Mapping Stuff
684 	 */
685 	struct resource *	pci_reg;	/* Register map for chip */
686 	int			pci_mem_rid;	/* Resource ID */
687 	bus_space_tag_t		pci_st;		/* Bus tag for registers */
688 	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
689 	/* PIO versions of above. */
690 	int			pci_pio_rid;
691 	struct resource *	pci_pio_reg;
692 	bus_space_tag_t		pci_pio_st;
693 	bus_space_handle_t	pci_pio_sh;
694 
695 	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
696 	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
697 	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
698 	uint8_t		       *reply;		/* KVA of reply memory */
699 	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
700 
701 	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
702 	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
703 	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
704 	uint8_t		       *request;	/* KVA of Request memory */
705 	bus_addr_t		request_phys;	/* BusAddr of request memory */
706 
707 	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
708 
709 	/*
710 	 * Hardware management
711 	 */
712 	u_int			reset_cnt;
713 
714 	/*
715 	 * CAM && Software Management
716 	 */
717 	request_t	       *request_pool;
718 	struct req_queue	request_free_list;
719 	struct req_queue	request_pending_list;
720 	struct req_queue	request_timeout_list;
721 
722 
723 	struct cam_sim	       *sim;
724 	struct cam_path	       *path;
725 
726 	struct cam_sim	       *phydisk_sim;
727 	struct cam_path	       *phydisk_path;
728 
729 	struct proc	       *recovery_thread;
730 	request_t	       *tmf_req;
731 
732 	/*
733 	 * Deferred frame acks due to resource shortage.
734 	 */
735 	struct mpt_evtf_list	ack_frames;
736 
737 	/*
738 	 * Target Mode Support
739 	 */
740 	uint32_t		scsi_tgt_handler_id;
741 	request_t **		tgt_cmd_ptrs;
742 	request_t **		els_cmd_ptrs;	/* FC only */
743 
744 	/*
745 	 * *snork*- this is chosen to be here *just in case* somebody
746 	 * forgets to point to it exactly and we index off of trt with
747 	 * CAM_LUN_WILDCARD.
748 	 */
749 	tgt_resource_t		trt_wildcard;		/* wildcard luns */
750 	tgt_resource_t		trt[MPT_MAX_LUNS];
751 	uint16_t		tgt_cmds_allocated;
752 	uint16_t		els_cmds_allocated;	/* FC only */
753 
754 	uint16_t		timeouts;	/* timeout count */
755 	uint16_t		success;	/* successes afer timeout */
756 	uint16_t		sequence;	/* Sequence Number */
757 	uint16_t		pad3;
758 
759 
760 	/* Paired port in some dual adapters configurations */
761 	struct mpt_softc *	mpt2;
762 
763 	/* FW Image management */
764 	uint32_t		fw_image_size;
765 	uint8_t		       *fw_image;
766 	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
767 	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
768 	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
769 
770 	/* SAS Topology */
771 	struct mptsas_portinfo	*sas_portinfo;
772 
773 	/* Shutdown Event Handler. */
774 	eventhandler_tag         eh;
775 
776 	TAILQ_ENTRY(mpt_softc)	links;
777 };
778 
779 static __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
780 
781 static __inline void
782 mpt_assign_serno(struct mpt_softc *mpt, request_t *req)
783 {
784 	if ((req->serno = mpt->sequence++) == 0) {
785 		req->serno = mpt->sequence++;
786 	}
787 }
788 
789 /***************************** Locking Primitives *****************************/
790 #if __FreeBSD_version < 500000
791 #define	MPT_IFLAGS		INTR_TYPE_CAM
792 #define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
793 #define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
794 #define	MPT_OWNED(mpt)		mpt->mpt_islocked
795 #define	MPT_LOCK_ASSERT(mpt)
796 #define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
797 #define	CAMLOCK_2_MPTLOCK	MPT_LOCK
798 #define	MPT_LOCK_SETUP(mpt)
799 #define	MPT_LOCK_DESTROY(mpt)
800 
801 static __inline void mpt_lockspl(struct mpt_softc *mpt);
802 static __inline void mpt_unlockspl(struct mpt_softc *mpt);
803 
804 static __inline void
805 mpt_lockspl(struct mpt_softc *mpt)
806 {
807        int s;
808 
809        s = splcam();
810        if (mpt->mpt_islocked++ == 0) {
811                mpt->mpt_splsaved = s;
812        } else {
813                splx(s);
814 	       panic("Recursed lock with mask: 0x%x\n", s);
815        }
816 }
817 
818 static __inline void
819 mpt_unlockspl(struct mpt_softc *mpt)
820 {
821        if (mpt->mpt_islocked) {
822                if (--mpt->mpt_islocked == 0) {
823                        splx(mpt->mpt_splsaved);
824                }
825        } else
826 	       panic("Negative lock count\n");
827 }
828 
829 static __inline int
830 mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
831 	   const char *wmesg, int timo)
832 {
833 	int saved_cnt;
834 	int saved_spl;
835 	int error;
836 
837 	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
838 	saved_cnt = mpt->mpt_islocked;
839 	saved_spl = mpt->mpt_splsaved;
840 	mpt->mpt_islocked = 0;
841 	error = tsleep(ident, priority, wmesg, timo);
842 	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
843 	mpt->mpt_islocked = saved_cnt;
844 	mpt->mpt_splsaved = saved_spl;
845 	return (error);
846 }
847 
848 #define mpt_req_timeout(req, ticks, func, arg) \
849 	callout_reset(&(req)->callout, (ticks), (func), (arg));
850 #define mpt_req_untimeout(req, func, arg) \
851 	callout_stop(&(req)->callout)
852 
853 #else
854 #if 1
855 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
856 #define	MPT_LOCK_SETUP(mpt)						\
857 		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
858 		mpt->mpt_locksetup = 1
859 #define	MPT_LOCK_DESTROY(mpt)						\
860 	if (mpt->mpt_locksetup) {					\
861 		mtx_destroy(&mpt->mpt_lock);				\
862 		mpt->mpt_locksetup = 0;					\
863 	}
864 
865 #define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
866 #define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
867 #define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
868 #define	MPT_LOCK_ASSERT(mpt)	mtx_assert(&(mpt)->mpt_lock, MA_OWNED)
869 #define	MPTLOCK_2_CAMLOCK(mpt)
870 #define	CAMLOCK_2_MPTLOCK(mpt)
871 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
872 	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
873 #define mpt_req_timeout(req, ticks, func, arg) \
874 	callout_reset(&(req)->callout, (ticks), (func), (arg));
875 #define mpt_req_untimeout(req, func, arg) \
876 	callout_stop(&(req)->callout)
877 
878 #else
879 
880 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
881 #define	MPT_LOCK_SETUP(mpt)	do { } while (0)
882 #define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
883 #define	MPT_LOCK_ASSERT(mpt)	mtx_assert(&Giant, MA_OWNED)
884 #define	MPT_LOCK(mpt)		mtx_lock(&Giant)
885 #define	MPT_UNLOCK(mpt)		mtx_unlock(&Giant)
886 #define	MPTLOCK_2_CAMLOCK(mpt)
887 #define	CAMLOCK_2_MPTLOCK(mpt)
888 
889 static __inline int
890 mpt_sleep(struct mpt_softc *, void *, int, const char *, int);
891 
892 #define mpt_ccb_timeout(ccb, ticks, func, arg) \
893 	do {	\
894 		(ccb)->ccb_h.timeout_ch = timeout((func), (arg), (ticks)); \
895 	} while (0)
896 #define mpt_ccb_untimeout(ccb, func, arg) \
897 	untimeout((func), (arg), (ccb)->ccb_h.timeout_ch)
898 #define mpt_ccb_timeout_init(ccb) \
899 	callout_handle_init(&(ccb)->ccb_h.timeout_ch)
900 
901 static __inline int
902 mpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
903 {
904 	int r;
905 	r = tsleep(i, p, w, t);
906 	return (r);
907 }
908 #endif
909 #endif
910 
911 /******************************* Register Access ******************************/
912 static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
913 static __inline uint32_t mpt_read(struct mpt_softc *, int);
914 static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
915 static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
916 
917 static __inline void
918 mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
919 {
920 	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
921 }
922 
923 static __inline uint32_t
924 mpt_read(struct mpt_softc *mpt, int offset)
925 {
926 	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
927 }
928 
929 /*
930  * Some operations (e.g. diagnostic register writes while the ARM proccessor
931  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
932  * busses, these operations likely map to normal register accesses.
933  */
934 static __inline void
935 mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
936 {
937 	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
938 }
939 
940 static __inline uint32_t
941 mpt_pio_read(struct mpt_softc *mpt, int offset)
942 {
943 	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
944 }
945 /*********************** Reply Frame/Request Management ***********************/
946 /* Max MPT Reply we are willing to accept (must be power of 2) */
947 #define MPT_REPLY_SIZE   	256
948 
949 /*
950  * Must be less than 16384 in order for target mode to work
951  */
952 #define MPT_MAX_REQUESTS(mpt)	512
953 #define MPT_REQUEST_AREA	512
954 #define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
955 #define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
956 
957 #define MPT_CONTEXT_CB_SHIFT	(16)
958 #define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
959 #define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
960 #define MPT_CONTEXT_TO_CBI(x)	\
961     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
962 #define MPT_CONTEXT_REQI_MASK	0xFFFF
963 #define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
964 
965 /*
966  * Convert a 32bit physical address returned from IOC to an
967  * offset into our reply frame memory or the kvm address needed
968  * to access the data.  The returned address is only the low
969  * 32 bits, so mask our base physical address accordingly.
970  */
971 #define MPT_REPLY_BADDR(x)		\
972 	(x << 1)
973 #define MPT_REPLY_OTOV(m, i) 		\
974 	((void *)(&m->reply[i]))
975 
976 #define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
977 do {							\
978 	if (mpt->verbose > MPT_PRT_DEBUG)		\
979 		mpt_dump_reply_frame(mpt, reply_frame);	\
980 } while(0)
981 
982 static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
983 static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
984 
985 /*
986  * Give the reply buffer back to the IOC after we have
987  * finished processing it.
988  */
989 static __inline void
990 mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
991 {
992      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
993 }
994 
995 /* Get a reply from the IOC */
996 static __inline uint32_t
997 mpt_pop_reply_queue(struct mpt_softc *mpt)
998 {
999      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
1000 }
1001 
1002 void
1003 mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
1004 
1005 /************************** Scatter Gather Managment **************************/
1006 /* MPT_RQSL- size of request frame, in bytes */
1007 #define	MPT_RQSL(mpt)		(mpt->ioc_facts.RequestFrameSize << 2)
1008 
1009 /* MPT_NSGL- how many SG entries can fit in a request frame size */
1010 #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
1011 
1012 /* MPT_NRFM- how many request frames can fit in each request alloc we make */
1013 #define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
1014 
1015 /*
1016  * MPT_NSGL_FIRST- # of SG elements that can fit after
1017  * an I/O request but still within the request frame.
1018  * Do this safely based upon SGE_IO_UNION.
1019  *
1020  * Note that the first element is *within* the SCSI request.
1021  */
1022 #define	MPT_NSGL_FIRST(mpt)	\
1023     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
1024     sizeof (SGE_IO_UNION))
1025 
1026 /***************************** IOC Initialization *****************************/
1027 int mpt_reset(struct mpt_softc *, int /*reinit*/);
1028 
1029 /****************************** Debugging ************************************/
1030 typedef struct mpt_decode_entry {
1031 	char    *name;
1032 	u_int	 value;
1033 	u_int	 mask;
1034 } mpt_decode_entry_t;
1035 
1036 int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
1037 		     const char *name, u_int value, u_int *cur_column,
1038 		     u_int wrap_point);
1039 
1040 void mpt_dump_data(struct mpt_softc *, const char *, void *, int);
1041 void mpt_dump_request(struct mpt_softc *, request_t *);
1042 
1043 enum {
1044 	MPT_PRT_ALWAYS,
1045 	MPT_PRT_FATAL,
1046 	MPT_PRT_ERROR,
1047 	MPT_PRT_WARN,
1048 	MPT_PRT_INFO,
1049 	MPT_PRT_NEGOTIATION,
1050 	MPT_PRT_DEBUG,
1051 	MPT_PRT_DEBUG1,
1052 	MPT_PRT_DEBUG2,
1053 	MPT_PRT_DEBUG3,
1054 	MPT_PRT_TRACE,
1055 	MPT_PRT_NONE=100
1056 };
1057 
1058 #if __FreeBSD_version > 500000
1059 #define mpt_lprt(mpt, level, ...)		\
1060 do {						\
1061 	if (level <= (mpt)->verbose)		\
1062 		mpt_prt(mpt, __VA_ARGS__);	\
1063 } while (0)
1064 
1065 #define mpt_lprtc(mpt, level, ...)		 \
1066 do {						 \
1067 	if (level <= (mpt)->debug_level)	 \
1068 		mpt_prtc(mpt, __VA_ARGS__);	 \
1069 } while (0)
1070 #else
1071 void mpt_lprt(struct mpt_softc *, int, const char *, ...)
1072 	__printflike(3, 4);
1073 void mpt_lprtc(struct mpt_softc *, int, const char *, ...)
1074 	__printflike(3, 4);
1075 #endif
1076 void mpt_prt(struct mpt_softc *, const char *, ...)
1077 	__printflike(2, 3);
1078 void mpt_prtc(struct mpt_softc *, const char *, ...)
1079 	__printflike(2, 3);
1080 
1081 /**************************** Target Mode Related ***************************/
1082 static __inline int mpt_cdblen(uint8_t, int);
1083 static __inline int
1084 mpt_cdblen(uint8_t cdb0, int maxlen)
1085 {
1086 	int group = cdb0 >> 5;
1087 	switch (group) {
1088 	case 0:
1089 		return (6);
1090 	case 1:
1091 		return (10);
1092 	case 4:
1093 	case 5:
1094 		return (12);
1095 	default:
1096 		return (16);
1097 	}
1098 }
1099 #ifdef	INVARIANTS
1100 static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
1101 static __inline request_t *
1102 mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
1103 {
1104 	uint16_t rtg = (tag >> 18);
1105 	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1106 	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1107 	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1108 	return (mpt->tgt_cmd_ptrs[rtg]);
1109 }
1110 
1111 
1112 static __inline int
1113 mpt_req_on_free_list(struct mpt_softc *, request_t *);
1114 static __inline int
1115 mpt_req_on_pending_list(struct mpt_softc *, request_t *);
1116 
1117 static __inline void
1118 mpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1119 static __inline void
1120 mpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1121 
1122 
1123 /*
1124  * Is request on freelist?
1125  */
1126 static __inline int
1127 mpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1128 {
1129 	request_t *lrq;
1130 
1131 	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1132 		if (lrq == req) {
1133 			return (1);
1134 		}
1135 	}
1136 	return (0);
1137 }
1138 
1139 /*
1140  * Is request on pending list?
1141  */
1142 static __inline int
1143 mpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1144 {
1145 	request_t *lrq;
1146 
1147 	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1148 		if (lrq == req) {
1149 			return (1);
1150 		}
1151 	}
1152 	return (0);
1153 }
1154 
1155 /*
1156  * Make sure that req *is* part of one of the special lists
1157  */
1158 static __inline void
1159 mpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1160 {
1161 	int i;
1162 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1163 		if (req == mpt->els_cmd_ptrs[i]) {
1164 			return;
1165 		}
1166 	}
1167 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1168 		if (req == mpt->tgt_cmd_ptrs[i]) {
1169 			return;
1170 		}
1171 	}
1172 	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1173 	    s, line, req, req->serno,
1174 	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1175 }
1176 
1177 /*
1178  * Make sure that req is *not* part of one of the special lists.
1179  */
1180 static __inline void
1181 mpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1182 {
1183 	int i;
1184 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1185 		KASSERT(req != mpt->els_cmd_ptrs[i],
1186 		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1187 		    s, line, req, req->serno,
1188 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1189 	}
1190 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1191 		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1192 		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1193 		    s, line, req, req->serno,
1194 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1195 	}
1196 }
1197 #endif
1198 
1199 /*
1200  * Task Management Types, purely for internal consumption
1201  */
1202 typedef enum {
1203 	MPT_ABORT_TASK_SET=1234,
1204 	MPT_CLEAR_TASK_SET,
1205 	MPT_TARGET_RESET,
1206 	MPT_CLEAR_ACA,
1207 	MPT_TERMINATE_TASK,
1208 	MPT_NIL_TMT_VALUE=5678
1209 } mpt_task_mgmt_t;
1210 
1211 /**************************** Unclassified Routines ***************************/
1212 void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1213 int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1214 					 size_t reply_len, void *reply);
1215 int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1216 			     mpt_req_state_t state, mpt_req_state_t mask,
1217 			     int sleep_ok, int time_ms);
1218 void		mpt_enable_ints(struct mpt_softc *mpt);
1219 void		mpt_disable_ints(struct mpt_softc *mpt);
1220 int		mpt_attach(struct mpt_softc *mpt);
1221 int		mpt_shutdown(struct mpt_softc *mpt);
1222 int		mpt_detach(struct mpt_softc *mpt);
1223 int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1224 				       size_t len, void *cmd);
1225 request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1226 void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1227 void		mpt_intr(void *arg);
1228 void		mpt_check_doorbell(struct mpt_softc *mpt);
1229 void		mpt_dump_reply_frame(struct mpt_softc *mpt,
1230 				     MSG_DEFAULT_REPLY *reply_frame);
1231 
1232 void		mpt_set_config_regs(struct mpt_softc *);
1233 int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1234 				  cfgparms_t *params,
1235 				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1236 				  int /*sleep_ok*/, int /*timeout_ms*/);
1237 int		mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion,
1238 				       int PageNumber, uint32_t PageAddress,
1239 				       int ExtPageType,
1240 				       CONFIG_EXTENDED_PAGE_HEADER *rslt,
1241 				       int sleep_ok, int timeout_ms);
1242 int		mpt_read_extcfg_page(struct mpt_softc *mpt, int Action,
1243 				     uint32_t PageAddress,
1244 				     CONFIG_EXTENDED_PAGE_HEADER *hdr,
1245 				     void *buf, size_t len, int sleep_ok,
1246 				     int timeout_ms);
1247 int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1248 				    int /*PageNumber*/,
1249 				    uint32_t /*PageAddress*/,
1250 				    CONFIG_PAGE_HEADER *,
1251 				    int /*sleep_ok*/, int /*timeout_ms*/);
1252 int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1253 				  uint32_t /*PageAddress*/,
1254 				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1255 				  int /*sleep_ok*/, int /*timeout_ms*/);
1256 int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1257 				   uint32_t /*PageAddress*/,
1258 				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1259 				   int /*sleep_ok*/, int /*timeout_ms*/);
1260 static __inline int
1261 mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1262 		      CONFIG_PAGE_HEADER *hdr, size_t len,
1263 		      int sleep_ok, int timeout_ms)
1264 {
1265 	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1266 				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1267 }
1268 
1269 static __inline int
1270 mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1271 		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1272 		       int timeout_ms)
1273 {
1274 	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1275 				   PageAddress, hdr, len, sleep_ok,
1276 				   timeout_ms));
1277 }
1278 /* mpt_debug.c functions */
1279 void mpt_print_reply(void *vmsg);
1280 void mpt_print_db(uint32_t mb);
1281 void mpt_print_config_reply(void *vmsg);
1282 char *mpt_ioc_diag(uint32_t diag);
1283 void mpt_req_state(mpt_req_state_t state);
1284 void mpt_print_config_request(void *vmsg);
1285 void mpt_print_request(void *vmsg);
1286 void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1287 void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1288 #endif /* _MPT_H_ */
1289