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