xref: /freebsd/sys/dev/mps/mps_mapping.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011-2015 LSI Corp.
5  * Copyright (c) 2013-2015 Avago Technologies
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
30  */
31 
32 #include <sys/cdefs.h>
33 /* TODO Move headers to mpsvar */
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/kthread.h>
42 #include <sys/taskqueue.h>
43 #include <sys/bus.h>
44 #include <sys/endian.h>
45 #include <sys/sysctl.h>
46 #include <sys/sbuf.h>
47 #include <sys/eventhandler.h>
48 #include <sys/uio.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <dev/mps/mpi/mpi2_type.h>
52 #include <dev/mps/mpi/mpi2.h>
53 #include <dev/mps/mpi/mpi2_ioc.h>
54 #include <dev/mps/mpi/mpi2_sas.h>
55 #include <dev/mps/mpi/mpi2_cnfg.h>
56 #include <dev/mps/mpi/mpi2_init.h>
57 #include <dev/mps/mpi/mpi2_tool.h>
58 #include <dev/mps/mps_ioctl.h>
59 #include <dev/mps/mpsvar.h>
60 #include <dev/mps/mps_mapping.h>
61 
62 /**
63  * _mapping_clear_map_entry - Clear a particular mapping entry.
64  * @map_entry: map table entry
65  *
66  * Returns nothing.
67  */
68 static inline void
69 _mapping_clear_map_entry(struct dev_mapping_table *map_entry)
70 {
71 	map_entry->physical_id = 0;
72 	map_entry->device_info = 0;
73 	map_entry->phy_bits = 0;
74 	map_entry->dpm_entry_num = MPS_DPM_BAD_IDX;
75 	map_entry->dev_handle = 0;
76 	map_entry->id = -1;
77 	map_entry->missing_count = 0;
78 	map_entry->init_complete = 0;
79 	map_entry->TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
80 }
81 
82 /**
83  * _mapping_clear_enc_entry - Clear a particular enclosure table entry.
84  * @enc_entry: enclosure table entry
85  *
86  * Returns nothing.
87  */
88 static inline void
89 _mapping_clear_enc_entry(struct enc_mapping_table *enc_entry)
90 {
91 	enc_entry->enclosure_id = 0;
92 	enc_entry->start_index = MPS_MAPTABLE_BAD_IDX;
93 	enc_entry->phy_bits = 0;
94 	enc_entry->dpm_entry_num = MPS_DPM_BAD_IDX;
95 	enc_entry->enc_handle = 0;
96 	enc_entry->num_slots = 0;
97 	enc_entry->start_slot = 0;
98 	enc_entry->missing_count = 0;
99 	enc_entry->removal_flag = 0;
100 	enc_entry->skip_search = 0;
101 	enc_entry->init_complete = 0;
102 }
103 
104 /**
105  * _mapping_commit_enc_entry - write a particular enc entry in DPM page0.
106  * @sc: per adapter object
107  * @enc_entry: enclosure table entry
108  *
109  * Returns 0 for success, non-zero for failure.
110  */
111 static int
112 _mapping_commit_enc_entry(struct mps_softc *sc,
113     struct enc_mapping_table *et_entry)
114 {
115 	Mpi2DriverMap0Entry_t *dpm_entry;
116 	struct dev_mapping_table *mt_entry;
117 	Mpi2ConfigReply_t mpi_reply;
118 	Mpi2DriverMappingPage0_t config_page;
119 
120 	if (!sc->is_dpm_enable)
121 		return 0;
122 
123 	memset(&config_page, 0, sizeof(Mpi2DriverMappingPage0_t));
124 	memcpy(&config_page.Header, (u8 *) sc->dpm_pg0,
125 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
126 	dpm_entry = (Mpi2DriverMap0Entry_t *)((u8 *)sc->dpm_pg0 +
127 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
128 	dpm_entry += et_entry->dpm_entry_num;
129 	dpm_entry->PhysicalIdentifier.Low =
130 	    ( 0xFFFFFFFF & et_entry->enclosure_id);
131 	dpm_entry->PhysicalIdentifier.High =
132 	    ( et_entry->enclosure_id >> 32);
133 	mt_entry = &sc->mapping_table[et_entry->start_index];
134 	dpm_entry->DeviceIndex = htole16(mt_entry->id);
135 	dpm_entry->MappingInformation = et_entry->num_slots;
136 	dpm_entry->MappingInformation <<= MPI2_DRVMAP0_MAPINFO_SLOT_SHIFT;
137 	dpm_entry->MappingInformation |= et_entry->missing_count;
138 	dpm_entry->MappingInformation = htole16(dpm_entry->MappingInformation);
139 	dpm_entry->PhysicalBitsMapping = htole32(et_entry->phy_bits);
140 	dpm_entry->Reserved1 = 0;
141 
142 	mps_dprint(sc, MPS_MAPPING, "%s: Writing DPM entry %d for enclosure.\n",
143 	    __func__, et_entry->dpm_entry_num);
144 	memcpy(&config_page.Entry, (u8 *)dpm_entry,
145 	    sizeof(Mpi2DriverMap0Entry_t));
146 	if (mps_config_set_dpm_pg0(sc, &mpi_reply, &config_page,
147 	    et_entry->dpm_entry_num)) {
148 		mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: Write of DPM "
149 		    "entry %d for enclosure failed.\n", __func__,
150 		    et_entry->dpm_entry_num);
151 		dpm_entry->MappingInformation = le16toh(dpm_entry->
152 		    MappingInformation);
153 		dpm_entry->DeviceIndex = le16toh(dpm_entry->DeviceIndex);
154 		dpm_entry->PhysicalBitsMapping =
155 		    le32toh(dpm_entry->PhysicalBitsMapping);
156 		return -1;
157 	}
158 	dpm_entry->MappingInformation = le16toh(dpm_entry->
159 	    MappingInformation);
160 	dpm_entry->DeviceIndex = le16toh(dpm_entry->DeviceIndex);
161 	dpm_entry->PhysicalBitsMapping =
162 	    le32toh(dpm_entry->PhysicalBitsMapping);
163 	return 0;
164 }
165 
166 /**
167  * _mapping_commit_map_entry - write a particular map table entry in DPM page0.
168  * @sc: per adapter object
169  * @mt_entry: mapping table entry
170  *
171  * Returns 0 for success, non-zero for failure.
172  */
173 
174 static int
175 _mapping_commit_map_entry(struct mps_softc *sc,
176     struct dev_mapping_table *mt_entry)
177 {
178 	Mpi2DriverMap0Entry_t *dpm_entry;
179 	Mpi2ConfigReply_t mpi_reply;
180 	Mpi2DriverMappingPage0_t config_page;
181 
182 	if (!sc->is_dpm_enable)
183 		return 0;
184 
185 	/*
186 	 * It's possible that this Map Entry points to a BAD DPM index. This
187 	 * can happen if the Map Entry is a for a missing device and the DPM
188 	 * entry that was being used by this device is now being used by some
189 	 * new device. So, check for a BAD DPM index and just return if so.
190 	 */
191 	if (mt_entry->dpm_entry_num == MPS_DPM_BAD_IDX) {
192 		mps_dprint(sc, MPS_MAPPING, "%s: DPM entry location for target "
193 		    "%d is invalid. DPM will not be written.\n", __func__,
194 		    mt_entry->id);
195 		return 0;
196 	}
197 
198 	memset(&config_page, 0, sizeof(Mpi2DriverMappingPage0_t));
199 	memcpy(&config_page.Header, (u8 *)sc->dpm_pg0,
200 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
201 	dpm_entry = (Mpi2DriverMap0Entry_t *)((u8 *) sc->dpm_pg0 +
202 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
203 	dpm_entry = dpm_entry + mt_entry->dpm_entry_num;
204 	dpm_entry->PhysicalIdentifier.Low = (0xFFFFFFFF &
205 	    mt_entry->physical_id);
206 	dpm_entry->PhysicalIdentifier.High = (mt_entry->physical_id >> 32);
207 	dpm_entry->DeviceIndex = htole16(mt_entry->id);
208 	dpm_entry->MappingInformation = htole16(mt_entry->missing_count);
209 	dpm_entry->PhysicalBitsMapping = 0;
210 	dpm_entry->Reserved1 = 0;
211 	memcpy(&config_page.Entry, (u8 *)dpm_entry,
212 	    sizeof(Mpi2DriverMap0Entry_t));
213 
214 	mps_dprint(sc, MPS_MAPPING, "%s: Writing DPM entry %d for target %d.\n",
215 	    __func__, mt_entry->dpm_entry_num, mt_entry->id);
216 	if (mps_config_set_dpm_pg0(sc, &mpi_reply, &config_page,
217 	    mt_entry->dpm_entry_num)) {
218 		mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: Write of DPM "
219 		    "entry %d for target %d failed.\n", __func__,
220 		    mt_entry->dpm_entry_num, mt_entry->id);
221 		dpm_entry->MappingInformation = le16toh(dpm_entry->
222 		    MappingInformation);
223 		dpm_entry->DeviceIndex = le16toh(dpm_entry->DeviceIndex);
224 		return -1;
225 	}
226 
227 	dpm_entry->MappingInformation = le16toh(dpm_entry->MappingInformation);
228 	dpm_entry->DeviceIndex = le16toh(dpm_entry->DeviceIndex);
229 	return 0;
230 }
231 
232 /**
233  * _mapping_get_ir_maprange - get start and end index for IR map range.
234  * @sc: per adapter object
235  * @start_idx: place holder for start index
236  * @end_idx: place holder for end index
237  *
238  * The IR volumes can be mapped either at start or end of the mapping table
239  * this function gets the detail of where IR volume mapping starts and ends
240  * in the device mapping table
241  *
242  * Returns nothing.
243  */
244 static void
245 _mapping_get_ir_maprange(struct mps_softc *sc, u32 *start_idx, u32 *end_idx)
246 {
247 	u16 volume_mapping_flags;
248 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
249 
250 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
251 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
252 	if (volume_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
253 		*start_idx = 0;
254 		if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
255 			*start_idx = 1;
256 	} else
257 		*start_idx = sc->max_devices - sc->max_volumes;
258 	*end_idx = *start_idx + sc->max_volumes - 1;
259 }
260 
261 /**
262  * _mapping_get_enc_idx_from_id - get enclosure index from enclosure ID
263  * @sc: per adapter object
264  * @enc_id: enclosure logical identifier
265  *
266  * Returns the index of enclosure entry on success or bad index.
267  */
268 static u8
269 _mapping_get_enc_idx_from_id(struct mps_softc *sc, u64 enc_id,
270     u64 phy_bits)
271 {
272 	struct enc_mapping_table *et_entry;
273 	u8 enc_idx = 0;
274 
275 	for (enc_idx = 0; enc_idx < sc->num_enc_table_entries; enc_idx++) {
276 		et_entry = &sc->enclosure_table[enc_idx];
277 		if ((et_entry->enclosure_id == le64toh(enc_id)) &&
278 		    (!et_entry->phy_bits || (et_entry->phy_bits &
279 		    le32toh(phy_bits))))
280 			return enc_idx;
281 	}
282 	return MPS_ENCTABLE_BAD_IDX;
283 }
284 
285 /**
286  * _mapping_get_enc_idx_from_handle - get enclosure index from handle
287  * @sc: per adapter object
288  * @enc_id: enclosure handle
289  *
290  * Returns the index of enclosure entry on success or bad index.
291  */
292 static u8
293 _mapping_get_enc_idx_from_handle(struct mps_softc *sc, u16 handle)
294 {
295 	struct enc_mapping_table *et_entry;
296 	u8 enc_idx = 0;
297 
298 	for (enc_idx = 0; enc_idx < sc->num_enc_table_entries; enc_idx++) {
299 		et_entry = &sc->enclosure_table[enc_idx];
300 		if (et_entry->missing_count)
301 			continue;
302 		if (et_entry->enc_handle == handle)
303 			return enc_idx;
304 	}
305 	return MPS_ENCTABLE_BAD_IDX;
306 }
307 
308 /**
309  * _mapping_get_high_missing_et_idx - get missing enclosure index
310  * @sc: per adapter object
311  *
312  * Search through the enclosure table and identifies the enclosure entry
313  * with high missing count and returns it's index
314  *
315  * Returns the index of enclosure entry on success or bad index.
316  */
317 static u8
318 _mapping_get_high_missing_et_idx(struct mps_softc *sc)
319 {
320 	struct enc_mapping_table *et_entry;
321 	u8 high_missing_count = 0;
322 	u8 enc_idx, high_idx = MPS_ENCTABLE_BAD_IDX;
323 
324 	for (enc_idx = 0; enc_idx < sc->num_enc_table_entries; enc_idx++) {
325 		et_entry = &sc->enclosure_table[enc_idx];
326 		if ((et_entry->missing_count > high_missing_count) &&
327 		    !et_entry->skip_search) {
328 			high_missing_count = et_entry->missing_count;
329 			high_idx = enc_idx;
330 		}
331 	}
332 	return high_idx;
333 }
334 
335 /**
336  * _mapping_get_high_missing_mt_idx - get missing map table index
337  * @sc: per adapter object
338  *
339  * Search through the map table and identifies the device entry
340  * with high missing count and returns it's index
341  *
342  * Returns the index of map table entry on success or bad index.
343  */
344 static u32
345 _mapping_get_high_missing_mt_idx(struct mps_softc *sc)
346 {
347 	u32 map_idx, high_idx = MPS_MAPTABLE_BAD_IDX;
348 	u8 high_missing_count = 0;
349 	u32 start_idx, end_idx, start_idx_ir, end_idx_ir;
350 	struct dev_mapping_table *mt_entry;
351 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
352 
353 	start_idx = 0;
354 	start_idx_ir = 0;
355 	end_idx_ir = 0;
356 	end_idx = sc->max_devices;
357 	if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
358 		start_idx = 1;
359 	if (sc->ir_firmware) {
360 		_mapping_get_ir_maprange(sc, &start_idx_ir, &end_idx_ir);
361 		if (start_idx == start_idx_ir)
362 			start_idx = end_idx_ir + 1;
363 		else
364 			end_idx = start_idx_ir;
365 	}
366 	mt_entry = &sc->mapping_table[start_idx];
367 	for (map_idx = start_idx; map_idx < end_idx; map_idx++, mt_entry++) {
368 		if (mt_entry->missing_count > high_missing_count) {
369 			high_missing_count =  mt_entry->missing_count;
370 			high_idx = map_idx;
371 		}
372 	}
373 	return high_idx;
374 }
375 
376 /**
377  * _mapping_get_ir_mt_idx_from_wwid - get map table index from volume WWID
378  * @sc: per adapter object
379  * @wwid: world wide unique ID of the volume
380  *
381  * Returns the index of map table entry on success or bad index.
382  */
383 static u32
384 _mapping_get_ir_mt_idx_from_wwid(struct mps_softc *sc, u64 wwid)
385 {
386 	u32 start_idx, end_idx, map_idx;
387 	struct dev_mapping_table *mt_entry;
388 
389 	_mapping_get_ir_maprange(sc, &start_idx, &end_idx);
390 	mt_entry = &sc->mapping_table[start_idx];
391 	for (map_idx = start_idx; map_idx <= end_idx; map_idx++, mt_entry++)
392 		if (mt_entry->physical_id == wwid)
393 			return map_idx;
394 
395 	return MPS_MAPTABLE_BAD_IDX;
396 }
397 
398 /**
399  * _mapping_get_mt_idx_from_id - get map table index from a device ID
400  * @sc: per adapter object
401  * @dev_id: device identifer (SAS Address)
402  *
403  * Returns the index of map table entry on success or bad index.
404  */
405 static u32
406 _mapping_get_mt_idx_from_id(struct mps_softc *sc, u64 dev_id)
407 {
408 	u32 map_idx;
409 	struct dev_mapping_table *mt_entry;
410 
411 	for (map_idx = 0; map_idx < sc->max_devices; map_idx++) {
412 		mt_entry = &sc->mapping_table[map_idx];
413 		if (mt_entry->physical_id == dev_id)
414 			return map_idx;
415 	}
416 	return MPS_MAPTABLE_BAD_IDX;
417 }
418 
419 /**
420  * _mapping_get_ir_mt_idx_from_handle - get map table index from volume handle
421  * @sc: per adapter object
422  * @wwid: volume device handle
423  *
424  * Returns the index of map table entry on success or bad index.
425  */
426 static u32
427 _mapping_get_ir_mt_idx_from_handle(struct mps_softc *sc, u16 volHandle)
428 {
429 	u32 start_idx, end_idx, map_idx;
430 	struct dev_mapping_table *mt_entry;
431 
432 	_mapping_get_ir_maprange(sc, &start_idx, &end_idx);
433 	mt_entry = &sc->mapping_table[start_idx];
434 	for (map_idx = start_idx; map_idx <= end_idx; map_idx++, mt_entry++)
435 		if (mt_entry->dev_handle == volHandle)
436 			return map_idx;
437 
438 	return MPS_MAPTABLE_BAD_IDX;
439 }
440 
441 /**
442  * _mapping_get_mt_idx_from_handle - get map table index from handle
443  * @sc: per adapter object
444  * @dev_id: device handle
445  *
446  * Returns the index of map table entry on success or bad index.
447  */
448 static u32
449 _mapping_get_mt_idx_from_handle(struct mps_softc *sc, u16 handle)
450 {
451 	u32 map_idx;
452 	struct dev_mapping_table *mt_entry;
453 
454 	for (map_idx = 0; map_idx < sc->max_devices; map_idx++) {
455 		mt_entry = &sc->mapping_table[map_idx];
456 		if (mt_entry->dev_handle == handle)
457 			return map_idx;
458 	}
459 	return MPS_MAPTABLE_BAD_IDX;
460 }
461 
462 /**
463  * _mapping_get_free_ir_mt_idx - get first free index for a volume
464  * @sc: per adapter object
465  *
466  * Search through mapping table for free index for a volume and if no free
467  * index then looks for a volume with high mapping index
468  *
469  * Returns the index of map table entry on success or bad index.
470  */
471 static u32
472 _mapping_get_free_ir_mt_idx(struct mps_softc *sc)
473 {
474 	u8 high_missing_count = 0;
475 	u32 start_idx, end_idx, map_idx;
476 	u32 high_idx = MPS_MAPTABLE_BAD_IDX;
477 	struct dev_mapping_table *mt_entry;
478 
479 	/*
480 	 * The IN_USE flag should be clear if the entry is available to use.
481 	 * This flag is cleared on initialization and and when a volume is
482 	 * deleted. All other times this flag should be set. If, for some
483 	 * reason, a free entry cannot be found, look for the entry with the
484 	 * highest missing count just in case there is one.
485 	 */
486 	_mapping_get_ir_maprange(sc, &start_idx, &end_idx);
487 
488 	mt_entry = &sc->mapping_table[start_idx];
489 	for (map_idx = start_idx; map_idx <= end_idx; map_idx++, mt_entry++) {
490 		if (!(mt_entry->device_info & MPS_MAP_IN_USE))
491 			return map_idx;
492 
493 		if (mt_entry->missing_count > high_missing_count) {
494 			high_missing_count = mt_entry->missing_count;
495 			high_idx = map_idx;
496 		}
497 	}
498 
499 	if (high_idx == MPS_MAPTABLE_BAD_IDX) {
500 		mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: Could not find a "
501 		    "free entry in the mapping table for a Volume. The mapping "
502 		    "table is probably corrupt.\n", __func__);
503 	}
504 
505 	return high_idx;
506 }
507 
508 /**
509  * _mapping_get_free_mt_idx - get first free index for a device
510  * @sc: per adapter object
511  * @start_idx: offset in the table to start search
512  *
513  * Returns the index of map table entry on success or bad index.
514  */
515 static u32
516 _mapping_get_free_mt_idx(struct mps_softc *sc, u32 start_idx)
517 {
518 	u32 map_idx, max_idx = sc->max_devices;
519 	struct dev_mapping_table *mt_entry = &sc->mapping_table[start_idx];
520 	u16 volume_mapping_flags;
521 
522 	volume_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
523 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
524 	if (sc->ir_firmware && (volume_mapping_flags ==
525 	    MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING))
526 		max_idx -= sc->max_volumes;
527 
528 	for (map_idx  = start_idx; map_idx < max_idx; map_idx++, mt_entry++)
529 		if (!(mt_entry->device_info & (MPS_MAP_IN_USE |
530 		    MPS_DEV_RESERVED)))
531 			return map_idx;
532 
533 	return MPS_MAPTABLE_BAD_IDX;
534 }
535 
536 /**
537  * _mapping_get_dpm_idx_from_id - get DPM index from ID
538  * @sc: per adapter object
539  * @id: volume WWID or enclosure ID or device ID
540  *
541  * Returns the index of DPM entry on success or bad index.
542  */
543 static u16
544 _mapping_get_dpm_idx_from_id(struct mps_softc *sc, u64 id, u32 phy_bits)
545 {
546 	u16 entry_num;
547 	uint64_t PhysicalIdentifier;
548 	Mpi2DriverMap0Entry_t *dpm_entry;
549 
550 	dpm_entry = (Mpi2DriverMap0Entry_t *)((u8 *)sc->dpm_pg0 +
551 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
552 	PhysicalIdentifier = dpm_entry->PhysicalIdentifier.High;
553 	PhysicalIdentifier = (PhysicalIdentifier << 32) |
554 	    dpm_entry->PhysicalIdentifier.Low;
555 	for (entry_num = 0; entry_num < sc->max_dpm_entries; entry_num++,
556 	    dpm_entry++)
557 		if ((id == PhysicalIdentifier) &&
558 		    (!phy_bits || !dpm_entry->PhysicalBitsMapping ||
559 		    (phy_bits & dpm_entry->PhysicalBitsMapping)))
560 			return entry_num;
561 
562 	return MPS_DPM_BAD_IDX;
563 }
564 
565 /**
566  * _mapping_get_free_dpm_idx - get first available DPM index
567  * @sc: per adapter object
568  *
569  * Returns the index of DPM entry on success or bad index.
570  */
571 static u32
572 _mapping_get_free_dpm_idx(struct mps_softc *sc)
573 {
574 	u16 entry_num;
575 	Mpi2DriverMap0Entry_t *dpm_entry;
576 	u16 current_entry = MPS_DPM_BAD_IDX, missing_cnt, high_missing_cnt = 0;
577 	u64 physical_id;
578 	struct dev_mapping_table *mt_entry;
579 	u32 map_idx;
580 
581  	for (entry_num = 0; entry_num < sc->max_dpm_entries; entry_num++) {
582 		dpm_entry = (Mpi2DriverMap0Entry_t *) ((u8 *)sc->dpm_pg0 +
583 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
584 		dpm_entry += entry_num;
585 		missing_cnt = dpm_entry->MappingInformation &
586 		    MPI2_DRVMAP0_MAPINFO_MISSING_MASK;
587 
588 		/*
589 		 * If entry is used and not missing, then this entry can't be
590 		 * used. Look at next one.
591 		 */
592 		if (sc->dpm_entry_used[entry_num] && !missing_cnt)
593 			continue;
594 
595 		/*
596 		 * If this entry is not used at all, then the missing count
597 		 * doesn't matter. Just use this one. Otherwise, keep looking
598 		 * and make sure the entry with the highest missing count is
599 		 * used.
600 		 */
601 		if (!sc->dpm_entry_used[entry_num]) {
602 			current_entry = entry_num;
603 			break;
604 		}
605 		if ((current_entry == MPS_DPM_BAD_IDX) ||
606 		    (missing_cnt > high_missing_cnt)) {
607 			current_entry = entry_num;
608 			high_missing_cnt = missing_cnt;
609 		}
610  	}
611 
612 	/*
613 	 * If an entry has been found to use and it's already marked as used
614 	 * it means that some device was already using this entry but it's
615 	 * missing, and that means that the connection between the missing
616 	 * device's DPM entry and the mapping table needs to be cleared. To do
617 	 * this, use the Physical ID of the old device still in the DPM entry
618 	 * to find its mapping table entry, then mark its DPM entry as BAD.
619 	 */
620 	if ((current_entry != MPS_DPM_BAD_IDX) &&
621 	    sc->dpm_entry_used[current_entry]) {
622 		dpm_entry = (Mpi2DriverMap0Entry_t *) ((u8 *)sc->dpm_pg0 +
623 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
624 		dpm_entry += current_entry;
625 		physical_id = dpm_entry->PhysicalIdentifier.High;
626 		physical_id = (physical_id << 32) |
627 		    dpm_entry->PhysicalIdentifier.Low;
628 		map_idx = _mapping_get_mt_idx_from_id(sc, physical_id);
629 		if (map_idx != MPS_MAPTABLE_BAD_IDX) {
630 			mt_entry = &sc->mapping_table[map_idx];
631 			mt_entry->dpm_entry_num = MPS_DPM_BAD_IDX;
632 		}
633 	}
634 	return current_entry;
635 }
636 
637 /**
638  * _mapping_update_ir_missing_cnt - Updates missing count for a volume
639  * @sc: per adapter object
640  * @map_idx: map table index of the volume
641  * @element: IR configuration change element
642  * @wwid: IR volume ID.
643  *
644  * Updates the missing count in the map table and in the DPM entry for a volume
645  *
646  * Returns nothing.
647  */
648 static void
649 _mapping_update_ir_missing_cnt(struct mps_softc *sc, u32 map_idx,
650     Mpi2EventIrConfigElement_t *element, u64 wwid)
651 {
652 	struct dev_mapping_table *mt_entry;
653 	u8 missing_cnt, reason = element->ReasonCode, update_dpm = 1;
654 	u16 dpm_idx;
655 	Mpi2DriverMap0Entry_t *dpm_entry;
656 
657 	/*
658 	 * Depending on the reason code, update the missing count. Always set
659 	 * the init_complete flag when here, so just do it first. That flag is
660 	 * used for volumes to make sure that the DPM entry has been updated.
661 	 * When a volume is deleted, clear the map entry's IN_USE flag so that
662 	 * the entry can be used again if another volume is created. Also clear
663 	 * its dev_handle entry so that other functions can't find this volume
664 	 * by the handle, since it's not defined any longer.
665 	 */
666 	mt_entry = &sc->mapping_table[map_idx];
667 	mt_entry->init_complete = 1;
668 	if ((reason == MPI2_EVENT_IR_CHANGE_RC_ADDED) ||
669 	    (reason == MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED)) {
670 		mt_entry->missing_count = 0;
671 	} else if (reason == MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED) {
672 		if (mt_entry->missing_count < MPS_MAX_MISSING_COUNT)
673 			mt_entry->missing_count++;
674 
675 		mt_entry->device_info &= ~MPS_MAP_IN_USE;
676 		mt_entry->dev_handle = 0;
677 	}
678 
679 	/*
680 	 * If persistent mapping is enabled, update the DPM with the new missing
681 	 * count for the volume. If the DPM index is bad, get a free one. If
682 	 * it's bad for a volume that's being deleted do nothing because that
683 	 * volume doesn't have a DPM entry.
684 	 */
685 	if (!sc->is_dpm_enable)
686 		return;
687 	dpm_idx = mt_entry->dpm_entry_num;
688 	if (dpm_idx == MPS_DPM_BAD_IDX) {
689 		if (reason == MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED)
690 		{
691 			mps_dprint(sc, MPS_MAPPING, "%s: Volume being deleted "
692 			    "is not in DPM so DPM missing count will not be "
693 			    "updated.\n", __func__);
694 			return;
695 		}
696 	}
697 	if (dpm_idx == MPS_DPM_BAD_IDX)
698 		dpm_idx = _mapping_get_free_dpm_idx(sc);
699 
700 	/*
701 	 * Got the DPM entry for the volume or found a free DPM entry if this is
702 	 * a new volume. Check if the current information is outdated.
703 	 */
704 	if (dpm_idx != MPS_DPM_BAD_IDX) {
705 		dpm_entry = (Mpi2DriverMap0Entry_t *)((u8 *)sc->dpm_pg0 +
706 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
707 		dpm_entry += dpm_idx;
708 		missing_cnt = dpm_entry->MappingInformation &
709 		    MPI2_DRVMAP0_MAPINFO_MISSING_MASK;
710 		if ((mt_entry->physical_id ==
711 		    le64toh(((u64)dpm_entry->PhysicalIdentifier.High << 32) |
712 		    (u64)dpm_entry->PhysicalIdentifier.Low)) && (missing_cnt ==
713 		    mt_entry->missing_count)) {
714 			mps_dprint(sc, MPS_MAPPING, "%s: DPM entry for volume "
715 			   "with target ID %d does not require an update.\n",
716 			    __func__, mt_entry->id);
717 			update_dpm = 0;
718 		}
719 	}
720 
721 	/*
722 	 * Update the volume's persistent info if it's new or the ID or missing
723 	 * count has changed. If a good DPM index has not been found by now,
724 	 * there is no space left in the DPM table.
725 	 */
726 	if ((dpm_idx != MPS_DPM_BAD_IDX) && update_dpm) {
727 		mps_dprint(sc, MPS_MAPPING, "%s: Update DPM entry for volume "
728 		    "with target ID %d.\n", __func__, mt_entry->id);
729 
730 		mt_entry->dpm_entry_num = dpm_idx;
731 		dpm_entry = (Mpi2DriverMap0Entry_t *)((u8 *)sc->dpm_pg0 +
732 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
733 		dpm_entry += dpm_idx;
734 		dpm_entry->PhysicalIdentifier.Low =
735 		    (0xFFFFFFFF & mt_entry->physical_id);
736 		dpm_entry->PhysicalIdentifier.High =
737 		    (mt_entry->physical_id >> 32);
738 		dpm_entry->DeviceIndex = map_idx;
739 		dpm_entry->MappingInformation = mt_entry->missing_count;
740 		dpm_entry->PhysicalBitsMapping = 0;
741 		dpm_entry->Reserved1 = 0;
742 		sc->dpm_flush_entry[dpm_idx] = 1;
743 		sc->dpm_entry_used[dpm_idx] = 1;
744 	} else if (dpm_idx == MPS_DPM_BAD_IDX) {
745 		mps_dprint(sc, MPS_INFO | MPS_MAPPING, "%s: No space to add an "
746 		    "entry in the DPM table for volume with target ID %d.\n",
747 		    __func__, mt_entry->id);
748 	}
749 }
750 
751 /**
752  * _mapping_add_to_removal_table - add DPM index to the removal table
753  * @sc: per adapter object
754  * @dpm_idx: Index of DPM entry to remove
755  *
756  * Adds a DPM entry number to the removal table.
757  *
758  * Returns nothing.
759  */
760 static void
761 _mapping_add_to_removal_table(struct mps_softc *sc, u16 dpm_idx)
762 {
763 	struct map_removal_table *remove_entry;
764 	u32 i;
765 
766 	/*
767 	 * This is only used to remove entries from the DPM in the controller.
768 	 * If DPM is not enabled, just return.
769 	 */
770 	if (!sc->is_dpm_enable)
771 		return;
772 
773 	/*
774 	 * Find the first available removal_table entry and add the new entry
775 	 * there.
776 	 */
777 	remove_entry = sc->removal_table;
778 
779 	for (i = 0; i < sc->max_devices; i++, remove_entry++) {
780 		if (remove_entry->dpm_entry_num != MPS_DPM_BAD_IDX)
781 			continue;
782 
783 		mps_dprint(sc, MPS_MAPPING, "%s: Adding DPM entry %d to table "
784 		    "for removal.\n", __func__, dpm_idx);
785 		remove_entry->dpm_entry_num = dpm_idx;
786 		break;
787 	}
788 
789 }
790 
791 /**
792  * _mapping_update_missing_count - Update missing count for a device
793  * @sc: per adapter object
794  * @topo_change: Topology change event entry
795  *
796  * Increment the missing count in the mapping table for a device that is not
797  * responding. If Persitent Mapping is used, increment the DPM entry as well.
798  * Currently, this function only increments the missing count if the device
799  * goes missing, so after initialization has completed. This means that the
800  * missing count can only go from 0 to 1 here. The missing count is incremented
801  * during initialization as well, so that's where a target's missing count can
802  * go past 1.
803  *
804  * Returns nothing.
805  */
806 static void
807 _mapping_update_missing_count(struct mps_softc *sc,
808     struct _map_topology_change *topo_change)
809 {
810 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
811 	u8 entry;
812 	struct _map_phy_change *phy_change;
813 	u32 map_idx;
814 	struct dev_mapping_table *mt_entry;
815 	Mpi2DriverMap0Entry_t *dpm_entry;
816 
817 	for (entry = 0; entry < topo_change->num_entries; entry++) {
818 		phy_change = &topo_change->phy_details[entry];
819 		if (!phy_change->dev_handle || (phy_change->reason !=
820 		    MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
821 			continue;
822 		map_idx = _mapping_get_mt_idx_from_handle(sc, phy_change->
823 		    dev_handle);
824 		phy_change->is_processed = 1;
825 		if (map_idx == MPS_MAPTABLE_BAD_IDX) {
826 			mps_dprint(sc, MPS_INFO | MPS_MAPPING, "%s: device is "
827 			    "already removed from mapping table\n", __func__);
828 			continue;
829 		}
830 		mt_entry = &sc->mapping_table[map_idx];
831 		if (mt_entry->missing_count < MPS_MAX_MISSING_COUNT)
832 			mt_entry->missing_count++;
833 
834 		/*
835 		 * When using Enc/Slot mapping, when a device is removed, it's
836 		 * mapping table information should be cleared. Otherwise, the
837 		 * target ID will be incorrect if this same device is re-added
838 		 * to a different slot.
839 		 */
840 		if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
841 		    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
842 			_mapping_clear_map_entry(mt_entry);
843 		}
844 
845 		/*
846 		 * When using device mapping, update the missing count in the
847 		 * DPM entry, but only if the missing count has changed.
848 		 */
849 		if (((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
850 		    MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) &&
851 		    sc->is_dpm_enable &&
852 		    mt_entry->dpm_entry_num != MPS_DPM_BAD_IDX) {
853 			dpm_entry =
854 			    (Mpi2DriverMap0Entry_t *) ((u8 *)sc->dpm_pg0 +
855 			    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
856 			dpm_entry += mt_entry->dpm_entry_num;
857 			if (dpm_entry->MappingInformation !=
858 			    mt_entry->missing_count) {
859 				dpm_entry->MappingInformation =
860 				    mt_entry->missing_count;
861 				sc->dpm_flush_entry[mt_entry->dpm_entry_num] =
862 				    1;
863 			}
864 		}
865 	}
866 }
867 
868 /**
869  * _mapping_find_enc_map_space -find map table entries for enclosure
870  * @sc: per adapter object
871  * @et_entry: enclosure entry
872  *
873  * Search through the mapping table defragment it and provide contiguous
874  * space in map table for a particular enclosure entry
875  *
876  * Returns start index in map table or bad index.
877  */
878 static u32
879 _mapping_find_enc_map_space(struct mps_softc *sc,
880     struct enc_mapping_table *et_entry)
881 {
882 	u16 vol_mapping_flags;
883 	u32 skip_count, end_of_table, map_idx, enc_idx;
884 	u16 num_found;
885 	u32 start_idx = MPS_MAPTABLE_BAD_IDX;
886 	struct dev_mapping_table *mt_entry;
887 	struct enc_mapping_table *enc_entry;
888 	unsigned char done_flag = 0, found_space;
889 	u16 max_num_phy_ids = le16toh(sc->ioc_pg8.MaxNumPhysicalMappedIDs);
890 
891 	skip_count = sc->num_rsvd_entries;
892 	num_found = 0;
893 
894 	vol_mapping_flags = le16toh(sc->ioc_pg8.IRVolumeMappingFlags) &
895 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
896 
897 	/*
898 	 * The end of the mapping table depends on where volumes are kept, if
899 	 * IR is enabled.
900 	 */
901 	if (!sc->ir_firmware)
902 		end_of_table = sc->max_devices;
903 	else if (vol_mapping_flags == MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING)
904 		end_of_table = sc->max_devices;
905 	else
906 		end_of_table = sc->max_devices - sc->max_volumes;
907 
908 	/*
909 	 * The skip_count is the number of entries that are reserved at the
910 	 * beginning of the mapping table. But, it does not include the number
911 	 * of Physical IDs that are reserved for direct attached devices. Look
912 	 * through the mapping table after these reserved entries to see if
913 	 * the devices for this enclosure are already mapped. The PHY bit check
914 	 * is used to make sure that at least one PHY bit is common between the
915 	 * enclosure and the device that is already mapped.
916 	 */
917 	mps_dprint(sc, MPS_MAPPING, "%s: Looking for space in the mapping "
918 	    "table for added enclosure.\n", __func__);
919 	for (map_idx = (max_num_phy_ids + skip_count);
920 	    map_idx < end_of_table; map_idx++) {
921 		mt_entry = &sc->mapping_table[map_idx];
922 		if ((et_entry->enclosure_id == mt_entry->physical_id) &&
923 		    (!mt_entry->phy_bits || (mt_entry->phy_bits &
924 		    et_entry->phy_bits))) {
925 			num_found += 1;
926 			if (num_found == et_entry->num_slots) {
927 				start_idx = (map_idx - num_found) + 1;
928 				mps_dprint(sc, MPS_MAPPING, "%s: Found space "
929 				    "in the mapping for enclosure at map index "
930 				    "%d.\n", __func__, start_idx);
931 				return start_idx;
932 			}
933 		} else
934 			num_found = 0;
935 	}
936 
937 	/*
938 	 * If the enclosure's devices are not mapped already, look for
939 	 * contiguous entries in the mapping table that are not reserved. If
940 	 * enough entries are found, return the starting index for that space.
941 	 */
942 	num_found = 0;
943 	for (map_idx = (max_num_phy_ids + skip_count);
944 	    map_idx < end_of_table; map_idx++) {
945 		mt_entry = &sc->mapping_table[map_idx];
946 		if (!(mt_entry->device_info & MPS_DEV_RESERVED)) {
947 			num_found += 1;
948 			if (num_found == et_entry->num_slots) {
949 				start_idx = (map_idx - num_found) + 1;
950 				mps_dprint(sc, MPS_MAPPING, "%s: Found space "
951 				    "in the mapping for enclosure at map index "
952 				    "%d.\n", __func__, start_idx);
953 				return start_idx;
954 			}
955 		} else
956 			num_found = 0;
957 	}
958 
959 	/*
960 	 * If here, it means that not enough space in the mapping table was
961 	 * found to support this enclosure, so go through the enclosure table to
962 	 * see if any enclosure entries have a missing count. If so, get the
963 	 * enclosure with the highest missing count and check it to see if there
964 	 * is enough space for the new enclosure.
965 	 */
966 	while (!done_flag) {
967 		enc_idx = _mapping_get_high_missing_et_idx(sc);
968 		if (enc_idx == MPS_ENCTABLE_BAD_IDX) {
969 			mps_dprint(sc, MPS_MAPPING, "%s: Not enough space was "
970 			    "found in the mapping for the added enclosure.\n",
971 			    __func__);
972 			return MPS_MAPTABLE_BAD_IDX;
973 		}
974 
975 		/*
976 		 * Found a missing enclosure. Set the skip_search flag so this
977 		 * enclosure is not checked again for a high missing count if
978 		 * the loop continues. This way, all missing enclosures can
979 		 * have their space added together to find enough space in the
980 		 * mapping table for the added enclosure. The space must be
981 		 * contiguous.
982 		 */
983 		mps_dprint(sc, MPS_MAPPING, "%s: Space from a missing "
984 		    "enclosure was found.\n", __func__);
985 		enc_entry = &sc->enclosure_table[enc_idx];
986 		enc_entry->skip_search = 1;
987 
988 		/*
989 		 * Unmark all of the missing enclosure's device's reserved
990 		 * space. These will be remarked as reserved if this missing
991 		 * enclosure's space is not used.
992 		 */
993 		mps_dprint(sc, MPS_MAPPING, "%s: Clear the reserved flag for "
994 		    "all of the map entries for the enclosure.\n", __func__);
995 		mt_entry = &sc->mapping_table[enc_entry->start_index];
996 		for (map_idx = enc_entry->start_index; map_idx <
997 		    (enc_entry->start_index + enc_entry->num_slots); map_idx++,
998 		    mt_entry++)
999 			mt_entry->device_info &= ~MPS_DEV_RESERVED;
1000 
1001 		/*
1002 		 * Now that space has been unreserved, check again to see if
1003 		 * enough space is available for the new enclosure.
1004 		 */
1005 		mps_dprint(sc, MPS_MAPPING, "%s: Check if new mapping space is "
1006 		    "enough for the new enclosure.\n", __func__);
1007 		found_space = 0;
1008 		num_found = 0;
1009 		for (map_idx = (max_num_phy_ids + skip_count);
1010 		    map_idx < end_of_table; map_idx++) {
1011 			mt_entry = &sc->mapping_table[map_idx];
1012 			if (!(mt_entry->device_info & MPS_DEV_RESERVED)) {
1013 				num_found += 1;
1014 				if (num_found == et_entry->num_slots) {
1015 					start_idx = (map_idx - num_found) + 1;
1016 					found_space = 1;
1017 					break;
1018 				}
1019 			} else
1020 				num_found = 0;
1021 		}
1022 		if (!found_space)
1023 			continue;
1024 
1025 		/*
1026 		 * If enough space was found, all of the missing enclosures that
1027 		 * will be used for the new enclosure must be added to the
1028 		 * removal table. Then all mappings for the enclosure's devices
1029 		 * and for the enclosure itself need to be cleared. There may be
1030 		 * more than one enclosure to add to the removal table and
1031 		 * clear.
1032 		 */
1033 		mps_dprint(sc, MPS_MAPPING, "%s: Found space in the mapping "
1034 		    "for enclosure at map index %d.\n", __func__, start_idx);
1035 		for (map_idx = start_idx; map_idx < (start_idx + num_found);
1036 		    map_idx++) {
1037 			enc_entry = sc->enclosure_table;
1038 			for (enc_idx = 0; enc_idx < sc->num_enc_table_entries;
1039 			    enc_idx++, enc_entry++) {
1040 				if (map_idx < enc_entry->start_index ||
1041 				    map_idx > (enc_entry->start_index +
1042 				    enc_entry->num_slots))
1043 					continue;
1044 				if (!enc_entry->removal_flag) {
1045 					mps_dprint(sc, MPS_MAPPING, "%s: "
1046 					    "Enclosure %d will be removed from "
1047 					    "the mapping table.\n", __func__,
1048 					    enc_idx);
1049 					enc_entry->removal_flag = 1;
1050 					_mapping_add_to_removal_table(sc,
1051 					    enc_entry->dpm_entry_num);
1052 				}
1053 				mt_entry = &sc->mapping_table[map_idx];
1054 				_mapping_clear_map_entry(mt_entry);
1055 				if (map_idx == (enc_entry->start_index +
1056 				    enc_entry->num_slots - 1))
1057 					_mapping_clear_enc_entry(et_entry);
1058 			}
1059 		}
1060 
1061 		/*
1062 		 * During the search for space for this enclosure, some entries
1063 		 * in the mapping table may have been unreserved. Go back and
1064 		 * change all of these to reserved again. Only the enclosures
1065 		 * with the removal_flag set should be left as unreserved. The
1066 		 * skip_search flag needs to be cleared as well so that the
1067 		 * enclosure's space will be looked at the next time space is
1068 		 * needed.
1069 		 */
1070 		enc_entry = sc->enclosure_table;
1071 		for (enc_idx = 0; enc_idx < sc->num_enc_table_entries;
1072 		    enc_idx++, enc_entry++) {
1073 			if (!enc_entry->removal_flag) {
1074 				mps_dprint(sc, MPS_MAPPING, "%s: Reset the "
1075 				    "reserved flag for all of the map entries "
1076 				    "for enclosure %d.\n", __func__, enc_idx);
1077 				mt_entry = &sc->mapping_table[enc_entry->
1078 				    start_index];
1079 				for (map_idx = enc_entry->start_index; map_idx <
1080 				    (enc_entry->start_index +
1081 				    enc_entry->num_slots); map_idx++,
1082 				    mt_entry++)
1083 					mt_entry->device_info |=
1084 					    MPS_DEV_RESERVED;
1085 				et_entry->skip_search = 0;
1086 			}
1087 		}
1088 		done_flag = 1;
1089 	}
1090 	return start_idx;
1091 }
1092 
1093 /**
1094  * _mapping_get_dev_info -get information about newly added devices
1095  * @sc: per adapter object
1096  * @topo_change: Topology change event entry
1097  *
1098  * Search through the topology change event list and issues sas device pg0
1099  * requests for the newly added device and reserved entries in tables
1100  *
1101  * Returns nothing
1102  */
1103 static void
1104 _mapping_get_dev_info(struct mps_softc *sc,
1105     struct _map_topology_change *topo_change)
1106 {
1107 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1108 	Mpi2ConfigReply_t mpi_reply;
1109 	Mpi2SasDevicePage0_t sas_device_pg0;
1110 	u8 entry, enc_idx, phy_idx;
1111 	u32 map_idx, index, device_info;
1112 	struct _map_phy_change *phy_change, *tmp_phy_change;
1113 	uint64_t sas_address;
1114 	struct enc_mapping_table *et_entry;
1115 	struct dev_mapping_table *mt_entry;
1116 	u8 add_code = MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED;
1117 	int rc = 1;
1118 
1119 	for (entry = 0; entry < topo_change->num_entries; entry++) {
1120 		phy_change = &topo_change->phy_details[entry];
1121 		if (phy_change->is_processed || !phy_change->dev_handle ||
1122 		    phy_change->reason != MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED)
1123 			continue;
1124 
1125 		if (mps_config_get_sas_device_pg0(sc, &mpi_reply,
1126 		    &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1127 		    phy_change->dev_handle)) {
1128 			phy_change->is_processed = 1;
1129 			continue;
1130 		}
1131 
1132 		/*
1133 		 * Always get SATA Identify information because this is used
1134 		 * to determine if Start/Stop Unit should be sent to the drive
1135 		 * when the system is shutdown.
1136 		 */
1137 		device_info = le32toh(sas_device_pg0.DeviceInfo);
1138 		sas_address = le32toh(sas_device_pg0.SASAddress.High);
1139 		sas_address = (sas_address << 32) |
1140 		    le32toh(sas_device_pg0.SASAddress.Low);
1141 		if ((device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE) &&
1142 		    (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)) {
1143 			rc = mpssas_get_sas_address_for_sata_disk(sc,
1144 			    &sas_address, phy_change->dev_handle, device_info,
1145 			    &phy_change->is_SATA_SSD);
1146 			if (rc) {
1147 				mps_dprint(sc, MPS_ERROR, "%s: failed to get "
1148 				    "disk type (SSD or HDD) and SAS Address "
1149 				    "for SATA device with handle 0x%04x\n",
1150 				    __func__, phy_change->dev_handle);
1151 			}
1152 		}
1153 
1154 		phy_change->physical_id = sas_address;
1155 		phy_change->slot = le16toh(sas_device_pg0.Slot);
1156 		phy_change->device_info = device_info;
1157 
1158 		/*
1159 		 * When using Enc/Slot mapping, if this device is an enclosure
1160 		 * make sure that all of its slots can fit into the mapping
1161 		 * table.
1162 		 */
1163 		if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1164 		    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
1165 			/*
1166 			 * The enclosure should already be in the enclosure
1167 			 * table due to the Enclosure Add event. If not, just
1168 			 * continue, nothing can be done.
1169 			 */
1170 			enc_idx = _mapping_get_enc_idx_from_handle(sc,
1171 			    topo_change->enc_handle);
1172 			if (enc_idx == MPS_ENCTABLE_BAD_IDX) {
1173 				phy_change->is_processed = 1;
1174 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
1175 				    "failed to add the device with handle "
1176 				    "0x%04x because enclosure handle 0x%04x "
1177 				    "is not in the mapping table\n", __func__,
1178 				    phy_change->dev_handle,
1179 				    topo_change->enc_handle);
1180 				continue;
1181 			}
1182 			if (!((phy_change->device_info &
1183 			    MPI2_SAS_DEVICE_INFO_END_DEVICE) &&
1184 			    (phy_change->device_info &
1185 			    (MPI2_SAS_DEVICE_INFO_SSP_TARGET |
1186 			    MPI2_SAS_DEVICE_INFO_STP_TARGET |
1187 			    MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))) {
1188 				phy_change->is_processed = 1;
1189 				continue;
1190 			}
1191 			et_entry = &sc->enclosure_table[enc_idx];
1192 
1193 			/*
1194 			 * If the enclosure already has a start_index, it's been
1195 			 * mapped, so go to the next Topo change.
1196 			 */
1197 			if (et_entry->start_index != MPS_MAPTABLE_BAD_IDX)
1198 				continue;
1199 
1200 			/*
1201 			 * If the Expander Handle is 0, the devices are direct
1202 			 * attached. In that case, the start_index must be just
1203 			 * after the reserved entries. Otherwise, find space in
1204 			 * the mapping table for the enclosure's devices.
1205 			 */
1206 			if (!topo_change->exp_handle) {
1207 				map_idx	= sc->num_rsvd_entries;
1208 				et_entry->start_index = map_idx;
1209 			} else {
1210 				map_idx = _mapping_find_enc_map_space(sc,
1211 				    et_entry);
1212 				et_entry->start_index = map_idx;
1213 
1214 				/*
1215 				 * If space cannot be found to hold all of the
1216 				 * enclosure's devices in the mapping table,
1217 				 * there's no need to continue checking the
1218 				 * other devices in this event. Set all of the
1219 				 * phy_details for this event (if the change is
1220 				 * for an add) as already processed because none
1221 				 * of these devices can be added to the mapping
1222 				 * table.
1223 				 */
1224 				if (et_entry->start_index ==
1225 				    MPS_MAPTABLE_BAD_IDX) {
1226 					mps_dprint(sc, MPS_ERROR | MPS_MAPPING,
1227 					    "%s: failed to add the enclosure "
1228 					    "with ID 0x%016jx because there is "
1229 					    "no free space available in the "
1230 					    "mapping table for all of the "
1231 					    "enclosure's devices.\n", __func__,
1232 					    (uintmax_t)et_entry->enclosure_id);
1233 					phy_change->is_processed = 1;
1234 					for (phy_idx = 0; phy_idx <
1235 					    topo_change->num_entries;
1236 					    phy_idx++) {
1237 						tmp_phy_change =
1238 						    &topo_change->phy_details
1239 						    [phy_idx];
1240 						if (tmp_phy_change->reason ==
1241 						    add_code)
1242 							tmp_phy_change->
1243 							    is_processed = 1;
1244 					}
1245 					break;
1246 				}
1247 			}
1248 
1249 			/*
1250 			 * Found space in the mapping table for this enclosure.
1251 			 * Initialize each mapping table entry for the
1252 			 * enclosure.
1253 			 */
1254 			mps_dprint(sc, MPS_MAPPING, "%s: Initialize %d map "
1255 			    "entries for the enclosure, starting at map index "
1256 			    " %d.\n", __func__, et_entry->num_slots, map_idx);
1257 			mt_entry = &sc->mapping_table[map_idx];
1258 			for (index = map_idx; index < (et_entry->num_slots
1259 			    + map_idx); index++, mt_entry++) {
1260 				mt_entry->device_info = MPS_DEV_RESERVED;
1261 				mt_entry->physical_id = et_entry->enclosure_id;
1262 				mt_entry->phy_bits = et_entry->phy_bits;
1263 				mt_entry->missing_count = 0;
1264 			}
1265 		}
1266 	}
1267 }
1268 
1269 /**
1270  * _mapping_set_mid_to_eid -set map table data from enclosure table
1271  * @sc: per adapter object
1272  * @et_entry: enclosure entry
1273  *
1274  * Returns nothing
1275  */
1276 static inline void
1277 _mapping_set_mid_to_eid(struct mps_softc *sc,
1278     struct enc_mapping_table *et_entry)
1279 {
1280 	struct dev_mapping_table *mt_entry;
1281 	u16 slots = et_entry->num_slots, map_idx;
1282 	u32 start_idx = et_entry->start_index;
1283 
1284 	if (start_idx != MPS_MAPTABLE_BAD_IDX) {
1285 		mt_entry = &sc->mapping_table[start_idx];
1286 		for (map_idx = 0; map_idx < slots; map_idx++, mt_entry++)
1287 			mt_entry->physical_id = et_entry->enclosure_id;
1288 	}
1289 }
1290 
1291 /**
1292  * _mapping_clear_removed_entries - mark the entries to be cleared
1293  * @sc: per adapter object
1294  *
1295  * Search through the removal table and mark the entries which needs to be
1296  * flushed to DPM and also updates the map table and enclosure table by
1297  * clearing the corresponding entries.
1298  *
1299  * Returns nothing
1300  */
1301 static void
1302 _mapping_clear_removed_entries(struct mps_softc *sc)
1303 {
1304 	u32 remove_idx;
1305 	struct map_removal_table *remove_entry;
1306 	Mpi2DriverMap0Entry_t *dpm_entry;
1307 	u8 done_flag = 0, num_entries, m, i;
1308 	struct enc_mapping_table *et_entry, *from, *to;
1309 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1310 
1311 	if (sc->is_dpm_enable) {
1312 		remove_entry = sc->removal_table;
1313 		for (remove_idx = 0; remove_idx < sc->max_devices;
1314 		    remove_idx++, remove_entry++) {
1315 			if (remove_entry->dpm_entry_num != MPS_DPM_BAD_IDX) {
1316 				dpm_entry = (Mpi2DriverMap0Entry_t *)
1317 				    ((u8 *) sc->dpm_pg0 +
1318 				    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
1319 				dpm_entry += remove_entry->dpm_entry_num;
1320 				dpm_entry->PhysicalIdentifier.Low = 0;
1321 				dpm_entry->PhysicalIdentifier.High = 0;
1322 				dpm_entry->DeviceIndex = 0;
1323 				dpm_entry->MappingInformation = 0;
1324 				dpm_entry->PhysicalBitsMapping = 0;
1325 				sc->dpm_flush_entry[remove_entry->
1326 				    dpm_entry_num] = 1;
1327 				sc->dpm_entry_used[remove_entry->dpm_entry_num]
1328 				    = 0;
1329 				remove_entry->dpm_entry_num = MPS_DPM_BAD_IDX;
1330 			}
1331 		}
1332 	}
1333 
1334 	/*
1335 	 * When using Enc/Slot mapping, if a new enclosure was added and old
1336 	 * enclosure space was needed, the enclosure table may now have gaps
1337 	 * that need to be closed. All enclosure mappings need to be contiguous
1338 	 * so that space can be reused correctly if available.
1339 	 */
1340 	if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1341 	    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
1342 		num_entries = sc->num_enc_table_entries;
1343 		while (!done_flag) {
1344 			done_flag = 1;
1345 			et_entry = sc->enclosure_table;
1346 			for (i = 0; i < num_entries; i++, et_entry++) {
1347 				if (!et_entry->enc_handle && et_entry->
1348 				    init_complete) {
1349 					done_flag = 0;
1350 					if (i != (num_entries - 1)) {
1351 						from = &sc->enclosure_table
1352 						    [i+1];
1353 						to = &sc->enclosure_table[i];
1354 						for (m = i; m < (num_entries -
1355 						    1); m++, from++, to++) {
1356 							_mapping_set_mid_to_eid
1357 							    (sc, to);
1358 							*to = *from;
1359 						}
1360 						_mapping_clear_enc_entry(to);
1361 						sc->num_enc_table_entries--;
1362 						num_entries =
1363 						    sc->num_enc_table_entries;
1364 					} else {
1365 						_mapping_clear_enc_entry
1366 						    (et_entry);
1367 						sc->num_enc_table_entries--;
1368 						num_entries =
1369 						    sc->num_enc_table_entries;
1370 					}
1371 				}
1372 			}
1373 		}
1374 	}
1375 }
1376 
1377 /**
1378  * _mapping_add_new_device -Add the new device into mapping table
1379  * @sc: per adapter object
1380  * @topo_change: Topology change event entry
1381  *
1382  * Search through the topology change event list and update map table,
1383  * enclosure table and DPM pages for the newly added devices.
1384  *
1385  * Returns nothing
1386  */
1387 static void
1388 _mapping_add_new_device(struct mps_softc *sc,
1389     struct _map_topology_change *topo_change)
1390 {
1391 	u8 enc_idx, missing_cnt, is_removed = 0;
1392 	u16 dpm_idx;
1393 	u32 search_idx, map_idx;
1394 	u32 entry;
1395 	struct dev_mapping_table *mt_entry;
1396 	struct enc_mapping_table *et_entry;
1397 	struct _map_phy_change *phy_change;
1398 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1399 	Mpi2DriverMap0Entry_t *dpm_entry;
1400 	uint64_t temp64_var;
1401 	u8 map_shift = MPI2_DRVMAP0_MAPINFO_SLOT_SHIFT;
1402 	u8 hdr_sz = sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER);
1403 	u16 max_num_phy_ids = le16toh(sc->ioc_pg8.MaxNumPhysicalMappedIDs);
1404 
1405 	for (entry = 0; entry < topo_change->num_entries; entry++) {
1406 		phy_change = &topo_change->phy_details[entry];
1407 		if (phy_change->is_processed)
1408 			continue;
1409 		if (phy_change->reason != MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED ||
1410 		    !phy_change->dev_handle) {
1411 			phy_change->is_processed = 1;
1412 			continue;
1413 		}
1414 		if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1415 		    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
1416 			enc_idx = _mapping_get_enc_idx_from_handle
1417 			    (sc, topo_change->enc_handle);
1418 			if (enc_idx == MPS_ENCTABLE_BAD_IDX) {
1419 				phy_change->is_processed = 1;
1420 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
1421 				    "failed to add the device with handle "
1422 				    "0x%04x because enclosure handle 0x%04x "
1423 				    "is not in the mapping table\n", __func__,
1424 				    phy_change->dev_handle,
1425 				    topo_change->enc_handle);
1426 				continue;
1427 			}
1428 
1429 			/*
1430 			 * If the enclosure's start_index is BAD here, it means
1431 			 * that there is no room in the mapping table to cover
1432 			 * all of the devices that could be in the enclosure.
1433 			 * There's no reason to process any of the devices for
1434 			 * this enclosure since they can't be mapped.
1435 			 */
1436 			et_entry = &sc->enclosure_table[enc_idx];
1437 			if (et_entry->start_index == MPS_MAPTABLE_BAD_IDX) {
1438 				phy_change->is_processed = 1;
1439 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
1440 				    "failed to add the device with handle "
1441 				    "0x%04x because there is no free space "
1442 				    "available in the mapping table\n",
1443 				    __func__, phy_change->dev_handle);
1444 				continue;
1445 			}
1446 
1447 			/*
1448 			 * Add this device to the mapping table at the correct
1449 			 * offset where space was found to map the enclosure.
1450 			 * Then setup the DPM entry information if being used.
1451 			 */
1452 			map_idx = et_entry->start_index + phy_change->slot -
1453 			    et_entry->start_slot;
1454 			mt_entry = &sc->mapping_table[map_idx];
1455 			mt_entry->physical_id = phy_change->physical_id;
1456 			mt_entry->id = map_idx;
1457 			mt_entry->dev_handle = phy_change->dev_handle;
1458 			mt_entry->missing_count = 0;
1459 			mt_entry->dpm_entry_num = et_entry->dpm_entry_num;
1460 			mt_entry->device_info = phy_change->device_info |
1461 			    (MPS_DEV_RESERVED | MPS_MAP_IN_USE);
1462 			if (sc->is_dpm_enable) {
1463 				dpm_idx = et_entry->dpm_entry_num;
1464 				if (dpm_idx == MPS_DPM_BAD_IDX)
1465 					dpm_idx = _mapping_get_dpm_idx_from_id
1466 					    (sc, et_entry->enclosure_id,
1467 					     et_entry->phy_bits);
1468 				if (dpm_idx == MPS_DPM_BAD_IDX) {
1469 					dpm_idx = _mapping_get_free_dpm_idx(sc);
1470 					if (dpm_idx != MPS_DPM_BAD_IDX) {
1471 						dpm_entry =
1472 						    (Mpi2DriverMap0Entry_t *)
1473 						    ((u8 *) sc->dpm_pg0 +
1474 						     hdr_sz);
1475 						dpm_entry += dpm_idx;
1476 						dpm_entry->
1477 						    PhysicalIdentifier.Low =
1478 						    (0xFFFFFFFF &
1479 						    et_entry->enclosure_id);
1480 						dpm_entry->
1481 						    PhysicalIdentifier.High =
1482 						    (et_entry->enclosure_id
1483 						     >> 32);
1484 						dpm_entry->DeviceIndex =
1485 						    (U16)et_entry->start_index;
1486 						dpm_entry->MappingInformation =
1487 						    et_entry->num_slots;
1488 						dpm_entry->MappingInformation
1489 						    <<= map_shift;
1490 						dpm_entry->PhysicalBitsMapping
1491 						    = et_entry->phy_bits;
1492 						et_entry->dpm_entry_num =
1493 						    dpm_idx;
1494 						sc->dpm_entry_used[dpm_idx] = 1;
1495 						sc->dpm_flush_entry[dpm_idx] =
1496 						    1;
1497 						phy_change->is_processed = 1;
1498 					} else {
1499 						phy_change->is_processed = 1;
1500 						mps_dprint(sc, MPS_ERROR |
1501 						    MPS_MAPPING, "%s: failed "
1502 						    "to add the device with "
1503 						    "handle 0x%04x to "
1504 						    "persistent table because "
1505 						    "there is no free space "
1506 						    "available\n", __func__,
1507 						    phy_change->dev_handle);
1508 					}
1509 				} else {
1510 					et_entry->dpm_entry_num = dpm_idx;
1511 					mt_entry->dpm_entry_num = dpm_idx;
1512 				}
1513 			}
1514 			et_entry->init_complete = 1;
1515 		} else if ((ioc_pg8_flags &
1516 		    MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1517 		    MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
1518 			/*
1519 			 * Get the mapping table index for this device. If it's
1520 			 * not in the mapping table yet, find a free entry if
1521 			 * one is available. If there are no free entries, look
1522 			 * for the entry that has the highest missing count. If
1523 			 * none of that works to find an entry in the mapping
1524 			 * table, there is a problem. Log a message and just
1525 			 * continue on.
1526 			 */
1527 			map_idx = _mapping_get_mt_idx_from_id
1528 			    (sc, phy_change->physical_id);
1529 			if (map_idx == MPS_MAPTABLE_BAD_IDX) {
1530 				search_idx = sc->num_rsvd_entries;
1531 				if (topo_change->exp_handle)
1532 					search_idx += max_num_phy_ids;
1533 				map_idx = _mapping_get_free_mt_idx(sc,
1534 				    search_idx);
1535 			}
1536 
1537 			/*
1538 			 * If an entry will be used that has a missing device,
1539 			 * clear its entry from  the DPM in the controller.
1540 			 */
1541 			if (map_idx == MPS_MAPTABLE_BAD_IDX) {
1542 				map_idx = _mapping_get_high_missing_mt_idx(sc);
1543 				if (map_idx != MPS_MAPTABLE_BAD_IDX) {
1544 					mt_entry = &sc->mapping_table[map_idx];
1545 					_mapping_add_to_removal_table(sc,
1546 					    mt_entry->dpm_entry_num);
1547 					is_removed = 1;
1548 					mt_entry->init_complete = 0;
1549 				}
1550 			}
1551 			if (map_idx != MPS_MAPTABLE_BAD_IDX) {
1552 				mt_entry = &sc->mapping_table[map_idx];
1553 				mt_entry->physical_id = phy_change->physical_id;
1554 				mt_entry->id = map_idx;
1555 				mt_entry->dev_handle = phy_change->dev_handle;
1556 				mt_entry->missing_count = 0;
1557 				mt_entry->device_info = phy_change->device_info
1558 				    | (MPS_DEV_RESERVED | MPS_MAP_IN_USE);
1559 			} else {
1560 				phy_change->is_processed = 1;
1561 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
1562 				    "failed to add the device with handle "
1563 				    "0x%04x because there is no free space "
1564 				    "available in the mapping table\n",
1565 				    __func__, phy_change->dev_handle);
1566 				continue;
1567 			}
1568 			if (sc->is_dpm_enable) {
1569 				if (mt_entry->dpm_entry_num !=
1570 				    MPS_DPM_BAD_IDX) {
1571 					dpm_idx = mt_entry->dpm_entry_num;
1572 					dpm_entry = (Mpi2DriverMap0Entry_t *)
1573 					    ((u8 *)sc->dpm_pg0 + hdr_sz);
1574 					dpm_entry += dpm_idx;
1575 					missing_cnt = dpm_entry->
1576 					    MappingInformation &
1577 					    MPI2_DRVMAP0_MAPINFO_MISSING_MASK;
1578 					temp64_var = dpm_entry->
1579 					    PhysicalIdentifier.High;
1580 					temp64_var = (temp64_var << 32) |
1581 					   dpm_entry->PhysicalIdentifier.Low;
1582 
1583 					/*
1584 					 * If the Mapping Table's info is not
1585 					 * the same as the DPM entry, clear the
1586 					 * init_complete flag so that it's
1587 					 * updated.
1588 					 */
1589 					if ((mt_entry->physical_id ==
1590 					    temp64_var) && !missing_cnt)
1591 						mt_entry->init_complete = 1;
1592 					else
1593 						mt_entry->init_complete = 0;
1594 				} else {
1595 					dpm_idx = _mapping_get_free_dpm_idx(sc);
1596 					mt_entry->init_complete = 0;
1597 				}
1598 				if (dpm_idx != MPS_DPM_BAD_IDX &&
1599 				    !mt_entry->init_complete) {
1600 					mt_entry->dpm_entry_num = dpm_idx;
1601 					dpm_entry = (Mpi2DriverMap0Entry_t *)
1602 					    ((u8 *)sc->dpm_pg0 + hdr_sz);
1603 					dpm_entry += dpm_idx;
1604 					dpm_entry->PhysicalIdentifier.Low =
1605 					    (0xFFFFFFFF &
1606 					    mt_entry->physical_id);
1607 					dpm_entry->PhysicalIdentifier.High =
1608 					    (mt_entry->physical_id >> 32);
1609 					dpm_entry->DeviceIndex = (U16) map_idx;
1610 					dpm_entry->MappingInformation = 0;
1611 					dpm_entry->PhysicalBitsMapping = 0;
1612 					sc->dpm_entry_used[dpm_idx] = 1;
1613 					sc->dpm_flush_entry[dpm_idx] = 1;
1614 					phy_change->is_processed = 1;
1615 				} else if (dpm_idx == MPS_DPM_BAD_IDX) {
1616 					phy_change->is_processed = 1;
1617 					mps_dprint(sc, MPS_ERROR | MPS_MAPPING,
1618 					    "%s: failed to add the device with "
1619 					    "handle 0x%04x to persistent table "
1620 					    "because there is no free space "
1621 					    "available\n", __func__,
1622 					    phy_change->dev_handle);
1623 				}
1624 			}
1625 			mt_entry->init_complete = 1;
1626 		}
1627 
1628 		phy_change->is_processed = 1;
1629 	}
1630 	if (is_removed)
1631 		_mapping_clear_removed_entries(sc);
1632 }
1633 
1634 /**
1635  * _mapping_flush_dpm_pages -Flush the DPM pages to NVRAM
1636  * @sc: per adapter object
1637  *
1638  * Returns nothing
1639  */
1640 static void
1641 _mapping_flush_dpm_pages(struct mps_softc *sc)
1642 {
1643 	Mpi2DriverMap0Entry_t *dpm_entry;
1644 	Mpi2ConfigReply_t mpi_reply;
1645 	Mpi2DriverMappingPage0_t config_page;
1646 	u16 entry_num;
1647 
1648 	for (entry_num = 0; entry_num < sc->max_dpm_entries; entry_num++) {
1649 		if (!sc->dpm_flush_entry[entry_num])
1650 			continue;
1651 		memset(&config_page, 0, sizeof(Mpi2DriverMappingPage0_t));
1652 		memcpy(&config_page.Header, (u8 *)sc->dpm_pg0,
1653 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
1654 		dpm_entry = (Mpi2DriverMap0Entry_t *) ((u8 *)sc->dpm_pg0 +
1655 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
1656 		dpm_entry += entry_num;
1657 		dpm_entry->MappingInformation = htole16(dpm_entry->
1658 		    MappingInformation);
1659 		dpm_entry->DeviceIndex = htole16(dpm_entry->DeviceIndex);
1660 		dpm_entry->PhysicalBitsMapping = htole32(dpm_entry->
1661 		    PhysicalBitsMapping);
1662 		memcpy(&config_page.Entry, (u8 *)dpm_entry,
1663 		    sizeof(Mpi2DriverMap0Entry_t));
1664 		/* TODO-How to handle failed writes? */
1665 		mps_dprint(sc, MPS_MAPPING, "%s: Flushing DPM entry %d.\n",
1666 		    __func__, entry_num);
1667 		if (mps_config_set_dpm_pg0(sc, &mpi_reply, &config_page,
1668 		    entry_num)) {
1669 			mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: Flush of "
1670 			    "DPM entry %d for device failed\n", __func__,
1671 			    entry_num);
1672 		} else
1673 			sc->dpm_flush_entry[entry_num] = 0;
1674 		dpm_entry->MappingInformation = le16toh(dpm_entry->
1675 		    MappingInformation);
1676 		dpm_entry->DeviceIndex = le16toh(dpm_entry->DeviceIndex);
1677 		dpm_entry->PhysicalBitsMapping = le32toh(dpm_entry->
1678 		    PhysicalBitsMapping);
1679 	}
1680 }
1681 
1682 /**
1683  * _mapping_allocate_memory- allocates the memory required for mapping tables
1684  * @sc: per adapter object
1685  *
1686  * Allocates the memory for all the tables required for host mapping
1687  *
1688  * Return 0 on success or non-zero on failure.
1689  */
1690 int
1691 mps_mapping_allocate_memory(struct mps_softc *sc)
1692 {
1693 	uint32_t dpm_pg0_sz;
1694 
1695 	sc->mapping_table = malloc((sizeof(struct dev_mapping_table) *
1696 	    sc->max_devices), M_MPT2, M_ZERO|M_NOWAIT);
1697 	if (!sc->mapping_table)
1698 		goto free_resources;
1699 
1700 	sc->removal_table = malloc((sizeof(struct map_removal_table) *
1701 	    sc->max_devices), M_MPT2, M_ZERO|M_NOWAIT);
1702 	if (!sc->removal_table)
1703 		goto free_resources;
1704 
1705 	sc->enclosure_table = malloc((sizeof(struct enc_mapping_table) *
1706 	    sc->max_enclosures), M_MPT2, M_ZERO|M_NOWAIT);
1707 	if (!sc->enclosure_table)
1708 		goto free_resources;
1709 
1710 	sc->dpm_entry_used = malloc((sizeof(u8) * sc->max_dpm_entries),
1711 	    M_MPT2, M_ZERO|M_NOWAIT);
1712 	if (!sc->dpm_entry_used)
1713 		goto free_resources;
1714 
1715 	sc->dpm_flush_entry = malloc((sizeof(u8) * sc->max_dpm_entries),
1716 	    M_MPT2, M_ZERO|M_NOWAIT);
1717 	if (!sc->dpm_flush_entry)
1718 		goto free_resources;
1719 
1720 	dpm_pg0_sz = sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER) +
1721 	    (sc->max_dpm_entries * sizeof(MPI2_CONFIG_PAGE_DRIVER_MAP0_ENTRY));
1722 
1723 	sc->dpm_pg0 = malloc(dpm_pg0_sz, M_MPT2, M_ZERO|M_NOWAIT);
1724 	if (!sc->dpm_pg0) {
1725 		printf("%s: memory alloc failed for dpm page; disabling dpm\n",
1726 		    __func__);
1727 		sc->is_dpm_enable = 0;
1728 	}
1729 
1730 	return 0;
1731 
1732 free_resources:
1733 	free(sc->mapping_table, M_MPT2);
1734 	free(sc->removal_table, M_MPT2);
1735 	free(sc->enclosure_table, M_MPT2);
1736 	free(sc->dpm_entry_used, M_MPT2);
1737 	free(sc->dpm_flush_entry, M_MPT2);
1738 	free(sc->dpm_pg0, M_MPT2);
1739 	printf("%s: device initialization failed due to failure in mapping "
1740 	    "table memory allocation\n", __func__);
1741 	return -1;
1742 }
1743 
1744 /**
1745  * mps_mapping_free_memory- frees the memory allocated for mapping tables
1746  * @sc: per adapter object
1747  *
1748  * Returns nothing.
1749  */
1750 void
1751 mps_mapping_free_memory(struct mps_softc *sc)
1752 {
1753 	free(sc->mapping_table, M_MPT2);
1754 	free(sc->removal_table, M_MPT2);
1755 	free(sc->enclosure_table, M_MPT2);
1756 	free(sc->dpm_entry_used, M_MPT2);
1757 	free(sc->dpm_flush_entry, M_MPT2);
1758 	free(sc->dpm_pg0, M_MPT2);
1759 }
1760 
1761 static void
1762 _mapping_process_dpm_pg0(struct mps_softc *sc)
1763 {
1764 	u8 missing_cnt, enc_idx;
1765 	u16 slot_id, entry_num, num_slots;
1766 	u32 map_idx, dev_idx, start_idx, end_idx;
1767 	struct dev_mapping_table *mt_entry;
1768 	Mpi2DriverMap0Entry_t *dpm_entry;
1769 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1770 	u16 max_num_phy_ids = le16toh(sc->ioc_pg8.MaxNumPhysicalMappedIDs);
1771 	struct enc_mapping_table *et_entry;
1772 	u64 physical_id;
1773 	u32 phy_bits = 0;
1774 
1775 	/*
1776 	 * start_idx and end_idx are only used for IR.
1777 	 */
1778 	if (sc->ir_firmware)
1779 		_mapping_get_ir_maprange(sc, &start_idx, &end_idx);
1780 
1781 	/*
1782 	 * Look through all of the DPM entries that were read from the
1783 	 * controller and copy them over to the driver's internal table if they
1784 	 * have a non-zero ID. At this point, any ID with a value of 0 would be
1785 	 * invalid, so don't copy it.
1786 	 */
1787 	mps_dprint(sc, MPS_MAPPING, "%s: Start copy of %d DPM entries into the "
1788 	    "mapping table.\n", __func__, sc->max_dpm_entries);
1789 	dpm_entry = (Mpi2DriverMap0Entry_t *) ((uint8_t *) sc->dpm_pg0 +
1790 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
1791 	for (entry_num = 0; entry_num < sc->max_dpm_entries; entry_num++,
1792 	    dpm_entry++) {
1793 		physical_id = dpm_entry->PhysicalIdentifier.High;
1794 		physical_id = (physical_id << 32) |
1795 		    dpm_entry->PhysicalIdentifier.Low;
1796 		if (!physical_id) {
1797 			sc->dpm_entry_used[entry_num] = 0;
1798 			continue;
1799 		}
1800 		sc->dpm_entry_used[entry_num] = 1;
1801 		dpm_entry->MappingInformation = le16toh(dpm_entry->
1802 		    MappingInformation);
1803 		missing_cnt = dpm_entry->MappingInformation &
1804 		    MPI2_DRVMAP0_MAPINFO_MISSING_MASK;
1805 		dev_idx = le16toh(dpm_entry->DeviceIndex);
1806 		phy_bits = le32toh(dpm_entry->PhysicalBitsMapping);
1807 
1808 		/*
1809 		 * Volumes are at special locations in the mapping table so
1810 		 * account for that. Volume mapping table entries do not depend
1811 		 * on the type of mapping, so continue the loop after adding
1812 		 * volumes to the mapping table.
1813 		 */
1814 		if (sc->ir_firmware && (dev_idx >= start_idx) &&
1815 		    (dev_idx <= end_idx)) {
1816 			mt_entry = &sc->mapping_table[dev_idx];
1817 			mt_entry->physical_id =
1818 			    dpm_entry->PhysicalIdentifier.High;
1819 			mt_entry->physical_id = (mt_entry->physical_id << 32) |
1820 			    dpm_entry->PhysicalIdentifier.Low;
1821 			mt_entry->id = dev_idx;
1822 			mt_entry->missing_count = missing_cnt;
1823 			mt_entry->dpm_entry_num = entry_num;
1824 			mt_entry->device_info = MPS_DEV_RESERVED;
1825 			continue;
1826 		}
1827 		if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1828 		    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
1829 			/*
1830 			 * The dev_idx for an enclosure is the start index. If
1831 			 * the start index is within the controller's default
1832 			 * enclosure area, set the number of slots for this
1833 			 * enclosure to the max allowed. Otherwise, it should be
1834 			 * a normal enclosure and the number of slots is in the
1835 			 * DPM entry's Mapping Information.
1836 			 */
1837 			if (dev_idx < (sc->num_rsvd_entries +
1838 			    max_num_phy_ids)) {
1839 				slot_id = 0;
1840 				if (ioc_pg8_flags &
1841 				    MPI2_IOCPAGE8_FLAGS_DA_START_SLOT_1)
1842 					slot_id = 1;
1843 				num_slots = max_num_phy_ids;
1844 			} else {
1845 				slot_id = 0;
1846 				num_slots = dpm_entry->MappingInformation &
1847 				    MPI2_DRVMAP0_MAPINFO_SLOT_MASK;
1848 				num_slots >>= MPI2_DRVMAP0_MAPINFO_SLOT_SHIFT;
1849 			}
1850 			enc_idx = sc->num_enc_table_entries;
1851 			if (enc_idx >= sc->max_enclosures) {
1852 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
1853 				    "Number of enclosure entries in DPM exceed "
1854 				    "the max allowed of %d.\n", __func__,
1855 				    sc->max_enclosures);
1856 				break;
1857 			}
1858 			sc->num_enc_table_entries++;
1859 			et_entry = &sc->enclosure_table[enc_idx];
1860 			physical_id = dpm_entry->PhysicalIdentifier.High;
1861 			et_entry->enclosure_id = (physical_id << 32) |
1862 			    dpm_entry->PhysicalIdentifier.Low;
1863 			et_entry->start_index = dev_idx;
1864 			et_entry->dpm_entry_num = entry_num;
1865 			et_entry->num_slots = num_slots;
1866 			et_entry->start_slot = slot_id;
1867 			et_entry->missing_count = missing_cnt;
1868 			et_entry->phy_bits = phy_bits;
1869 
1870 			/*
1871 			 * Initialize all entries for this enclosure in the
1872 			 * mapping table and mark them as reserved. The actual
1873 			 * devices have not been processed yet but when they are
1874 			 * they will use these entries. If an entry is found
1875 			 * that already has a valid DPM index, the mapping table
1876 			 * is corrupt. This can happen if the mapping type is
1877 			 * changed without clearing all of the DPM entries in
1878 			 * the controller.
1879 			 */
1880 			mt_entry = &sc->mapping_table[dev_idx];
1881 			for (map_idx = dev_idx; map_idx < (dev_idx + num_slots);
1882 			    map_idx++, mt_entry++) {
1883 				if (mt_entry->dpm_entry_num !=
1884 				    MPS_DPM_BAD_IDX) {
1885 					mps_dprint(sc, MPS_ERROR | MPS_MAPPING,
1886 					    "%s: Conflict in mapping table for "
1887 					    "enclosure %d device %d\n",
1888 					    __func__, enc_idx, map_idx);
1889 					break;
1890 				}
1891 				physical_id =
1892 				    dpm_entry->PhysicalIdentifier.High;
1893 				mt_entry->physical_id = (physical_id << 32) |
1894 				    dpm_entry->PhysicalIdentifier.Low;
1895 				mt_entry->phy_bits = phy_bits;
1896 				mt_entry->id = dev_idx;
1897 				mt_entry->dpm_entry_num = entry_num;
1898 				mt_entry->missing_count = missing_cnt;
1899 				mt_entry->device_info = MPS_DEV_RESERVED;
1900 			}
1901 		} else if ((ioc_pg8_flags &
1902 		    MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1903 		    MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
1904 			/*
1905 			 * Device mapping, so simply copy the DPM entries to the
1906 			 * mapping table, but check for a corrupt mapping table
1907 			 * (as described above in Enc/Slot mapping).
1908 			 */
1909 			map_idx = dev_idx;
1910 			mt_entry = &sc->mapping_table[map_idx];
1911 			if (mt_entry->dpm_entry_num != MPS_DPM_BAD_IDX) {
1912 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
1913 				    "Conflict in mapping table for device %d\n",
1914 				    __func__, map_idx);
1915 				break;
1916 			}
1917 			physical_id = dpm_entry->PhysicalIdentifier.High;
1918 			mt_entry->physical_id = (physical_id << 32) |
1919 			    dpm_entry->PhysicalIdentifier.Low;
1920 			mt_entry->phy_bits = phy_bits;
1921 			mt_entry->id = dev_idx;
1922 			mt_entry->missing_count = missing_cnt;
1923 			mt_entry->dpm_entry_num = entry_num;
1924 			mt_entry->device_info = MPS_DEV_RESERVED;
1925 		}
1926 	} /*close the loop for DPM table */
1927 }
1928 
1929 /*
1930  * mps_mapping_check_devices - start of the day check for device availabilty
1931  * @sc: per adapter object
1932  *
1933  * Returns nothing.
1934  */
1935 void
1936 mps_mapping_check_devices(void *data)
1937 {
1938 	u32 i;
1939 	struct dev_mapping_table *mt_entry;
1940 	struct mps_softc *sc = (struct mps_softc *)data;
1941 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
1942 	struct enc_mapping_table *et_entry;
1943 	u32 start_idx = 0, end_idx = 0;
1944 	u8 stop_device_checks = 0;
1945 
1946 	MPS_FUNCTRACE(sc);
1947 
1948 	/*
1949 	 * Clear this flag so that this function is never called again except
1950 	 * within this function if the check needs to be done again. The
1951 	 * purpose is to check for missing devices that are currently in the
1952 	 * mapping table so do this only at driver init after discovery.
1953 	 */
1954 	sc->track_mapping_events = 0;
1955 
1956 	/*
1957 	 * callout synchronization
1958 	 * This is used to prevent race conditions for the callout.
1959 	 */
1960 	mps_dprint(sc, MPS_MAPPING, "%s: Start check for missing devices.\n",
1961 	    __func__);
1962 	mtx_assert(&sc->mps_mtx, MA_OWNED);
1963 	if ((callout_pending(&sc->device_check_callout)) ||
1964 	    (!callout_active(&sc->device_check_callout))) {
1965 		mps_dprint(sc, MPS_MAPPING, "%s: Device Check Callout is "
1966 		    "already pending or not active.\n", __func__);
1967 		return;
1968 	}
1969 	callout_deactivate(&sc->device_check_callout);
1970 
1971 	/*
1972 	 * Use callout to check if any devices in the mapping table have been
1973 	 * processed yet. If ALL devices are marked as not init_complete, no
1974 	 * devices have been processed and mapped. Until devices are mapped
1975 	 * there's no reason to mark them as missing. Continue resetting this
1976 	 * callout until devices have been mapped.
1977 	 */
1978 	if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1979 	    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
1980 		et_entry = sc->enclosure_table;
1981 		for (i = 0; i < sc->num_enc_table_entries; i++, et_entry++) {
1982 			if (et_entry->init_complete) {
1983 				stop_device_checks = 1;
1984 				break;
1985 			}
1986 		}
1987 	} else if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
1988 	    MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
1989 		mt_entry = sc->mapping_table;
1990 		for (i = 0; i < sc->max_devices; i++, mt_entry++) {
1991 			if (mt_entry->init_complete) {
1992 				stop_device_checks = 1;
1993 				break;
1994 			}
1995 		}
1996 	}
1997 
1998 	/*
1999 	 * Setup another callout check after a delay. Keep doing this until
2000 	 * devices are mapped.
2001 	 */
2002 	if (!stop_device_checks) {
2003 		mps_dprint(sc, MPS_MAPPING, "%s: No devices have been mapped. "
2004 		    "Reset callout to check again after a %d second delay.\n",
2005 		    __func__, MPS_MISSING_CHECK_DELAY);
2006 		callout_reset(&sc->device_check_callout,
2007 		    MPS_MISSING_CHECK_DELAY * hz, mps_mapping_check_devices,
2008 		    sc);
2009 		return;
2010 	}
2011 	mps_dprint(sc, MPS_MAPPING, "%s: Device check complete.\n", __func__);
2012 
2013 	/*
2014 	 * Depending on the mapping type, check if devices have been processed
2015 	 * and update their missing counts if not processed.
2016 	 */
2017 	if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
2018 	    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING) {
2019 		et_entry = sc->enclosure_table;
2020 		for (i = 0; i < sc->num_enc_table_entries; i++, et_entry++) {
2021 			if (!et_entry->init_complete) {
2022 				if (et_entry->missing_count <
2023 				    MPS_MAX_MISSING_COUNT) {
2024 					mps_dprint(sc, MPS_MAPPING, "%s: "
2025 					    "Enclosure %d is missing from the "
2026 					    "topology. Update its missing "
2027 					    "count.\n", __func__, i);
2028 					et_entry->missing_count++;
2029 					if (et_entry->dpm_entry_num !=
2030 					    MPS_DPM_BAD_IDX) {
2031 						_mapping_commit_enc_entry(sc,
2032 						    et_entry);
2033 					}
2034 				}
2035 				et_entry->init_complete = 1;
2036 			}
2037 		}
2038 		if (!sc->ir_firmware)
2039 			return;
2040 		_mapping_get_ir_maprange(sc, &start_idx, &end_idx);
2041 		mt_entry = &sc->mapping_table[start_idx];
2042 	} else if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) ==
2043 	    MPI2_IOCPAGE8_FLAGS_DEVICE_PERSISTENCE_MAPPING) {
2044 		start_idx = 0;
2045 		end_idx = sc->max_devices - 1;
2046 		mt_entry = sc->mapping_table;
2047 	}
2048 
2049 	/*
2050 	 * The start and end indices have been set above according to the
2051 	 * mapping type. Go through these mappings and update any entries that
2052 	 * do not have the init_complete flag set, which means they are missing.
2053 	 */
2054 	if (end_idx == 0)
2055 		return;
2056 	for (i = start_idx; i < (end_idx + 1); i++, mt_entry++) {
2057 		if (mt_entry->device_info & MPS_DEV_RESERVED
2058 		    && !mt_entry->physical_id)
2059 			mt_entry->init_complete = 1;
2060 		else if (mt_entry->device_info & MPS_DEV_RESERVED) {
2061 			if (!mt_entry->init_complete) {
2062 				mps_dprint(sc, MPS_MAPPING, "%s: Device in "
2063 				    "mapping table at index %d is missing from "
2064 				    "topology. Update its missing count.\n",
2065 				    __func__, i);
2066 				if (mt_entry->missing_count <
2067 				    MPS_MAX_MISSING_COUNT) {
2068 					mt_entry->missing_count++;
2069 					if (mt_entry->dpm_entry_num !=
2070 					    MPS_DPM_BAD_IDX) {
2071 						_mapping_commit_map_entry(sc,
2072 						    mt_entry);
2073 					}
2074 				}
2075 				mt_entry->init_complete = 1;
2076 			}
2077 		}
2078 	}
2079 }
2080 
2081 /**
2082  * mps_mapping_initialize - initialize mapping tables
2083  * @sc: per adapter object
2084  *
2085  * Read controller persitant mapping tables into internal data area.
2086  *
2087  * Return 0 for success or non-zero for failure.
2088  */
2089 int
2090 mps_mapping_initialize(struct mps_softc *sc)
2091 {
2092 	uint16_t volume_mapping_flags, dpm_pg0_sz;
2093 	uint32_t i;
2094 	Mpi2ConfigReply_t mpi_reply;
2095 	int error;
2096 	uint8_t retry_count;
2097 	uint16_t ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
2098 
2099 	/* The additional 1 accounts for the virtual enclosure
2100 	 * created for the controller
2101 	 */
2102 	sc->max_enclosures = sc->facts->MaxEnclosures + 1;
2103 	sc->max_expanders = sc->facts->MaxSasExpanders;
2104 	sc->max_volumes = sc->facts->MaxVolumes;
2105 	sc->max_devices = sc->facts->MaxTargets + sc->max_volumes;
2106 	sc->pending_map_events = 0;
2107 	sc->num_enc_table_entries = 0;
2108 	sc->num_rsvd_entries = 0;
2109 	sc->max_dpm_entries = sc->ioc_pg8.MaxPersistentEntries;
2110 	sc->is_dpm_enable = (sc->max_dpm_entries) ? 1 : 0;
2111 	sc->track_mapping_events = 0;
2112 
2113 	mps_dprint(sc, MPS_MAPPING, "%s: Mapping table has a max of %d entries "
2114 	    "and DPM has a max of %d entries.\n", __func__, sc->max_devices,
2115 	    sc->max_dpm_entries);
2116 	if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_DISABLE_PERSISTENT_MAPPING)
2117 		sc->is_dpm_enable = 0;
2118 
2119 	if (ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_RESERVED_TARGETID_0)
2120 		sc->num_rsvd_entries = 1;
2121 
2122 	volume_mapping_flags = sc->ioc_pg8.IRVolumeMappingFlags &
2123 	    MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
2124 	if (sc->ir_firmware && (volume_mapping_flags ==
2125 	    MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING))
2126 		sc->num_rsvd_entries += sc->max_volumes;
2127 
2128 	error = mps_mapping_allocate_memory(sc);
2129 	if (error)
2130 		return (error);
2131 
2132 	for (i = 0; i < sc->max_devices; i++)
2133 		_mapping_clear_map_entry(sc->mapping_table + i);
2134 
2135 	for (i = 0; i < sc->max_enclosures; i++)
2136 		_mapping_clear_enc_entry(sc->enclosure_table + i);
2137 
2138 	for (i = 0; i < sc->max_devices; i++) {
2139 		sc->removal_table[i].dev_handle = 0;
2140 		sc->removal_table[i].dpm_entry_num = MPS_DPM_BAD_IDX;
2141 	}
2142 
2143 	memset(sc->dpm_entry_used, 0, sc->max_dpm_entries);
2144 	memset(sc->dpm_flush_entry, 0, sc->max_dpm_entries);
2145 
2146 	if (sc->is_dpm_enable) {
2147 		dpm_pg0_sz = sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER) +
2148 		    (sc->max_dpm_entries *
2149 		     sizeof(MPI2_CONFIG_PAGE_DRIVER_MAP0_ENTRY));
2150 		retry_count = 0;
2151 
2152 retry_read_dpm:
2153 		if (mps_config_get_dpm_pg0(sc, &mpi_reply, sc->dpm_pg0,
2154 		    dpm_pg0_sz)) {
2155 			mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: DPM page "
2156 			    "read failed.\n", __func__);
2157 			if (retry_count < 3) {
2158 				retry_count++;
2159 				goto retry_read_dpm;
2160 			}
2161 			sc->is_dpm_enable = 0;
2162 		}
2163 	}
2164 
2165 	if (sc->is_dpm_enable)
2166 		_mapping_process_dpm_pg0(sc);
2167 	else {
2168 		mps_dprint(sc, MPS_MAPPING, "%s: DPM processing is disabled. "
2169 		    "Device mappings will not persist across reboots or "
2170 		    "resets.\n", __func__);
2171 	}
2172 
2173 	sc->track_mapping_events = 1;
2174 	return 0;
2175 }
2176 
2177 /**
2178  * mps_mapping_exit - clear mapping table and associated memory
2179  * @sc: per adapter object
2180  *
2181  * Returns nothing.
2182  */
2183 void
2184 mps_mapping_exit(struct mps_softc *sc)
2185 {
2186 	_mapping_flush_dpm_pages(sc);
2187 	mps_mapping_free_memory(sc);
2188 }
2189 
2190 /**
2191  * mps_mapping_get_tid - return the target id for sas device and handle
2192  * @sc: per adapter object
2193  * @sas_address: sas address of the device
2194  * @handle: device handle
2195  *
2196  * Returns valid target ID on success or BAD_ID.
2197  */
2198 unsigned int
2199 mps_mapping_get_tid(struct mps_softc *sc, uint64_t sas_address, u16 handle)
2200 {
2201 	u32 map_idx;
2202 	struct dev_mapping_table *mt_entry;
2203 
2204 	for (map_idx = 0; map_idx < sc->max_devices; map_idx++) {
2205 		mt_entry = &sc->mapping_table[map_idx];
2206 		if (mt_entry->dev_handle == handle && mt_entry->physical_id ==
2207 		    sas_address)
2208 			return mt_entry->id;
2209 	}
2210 
2211 	return MPS_MAP_BAD_ID;
2212 }
2213 
2214 /**
2215  * mps_mapping_get_tid_from_handle - find a target id in mapping table using
2216  * only the dev handle.  This is just a wrapper function for the local function
2217  * _mapping_get_mt_idx_from_handle.
2218  * @sc: per adapter object
2219  * @handle: device handle
2220  *
2221  * Returns valid target ID on success or BAD_ID.
2222  */
2223 unsigned int
2224 mps_mapping_get_tid_from_handle(struct mps_softc *sc, u16 handle)
2225 {
2226 	return (_mapping_get_mt_idx_from_handle(sc, handle));
2227 }
2228 
2229 /**
2230  * mps_mapping_get_raid_tid - return the target id for raid device
2231  * @sc: per adapter object
2232  * @wwid: world wide identifier for raid volume
2233  * @volHandle: volume device handle
2234  *
2235  * Returns valid target ID on success or BAD_ID.
2236  */
2237 unsigned int
2238 mps_mapping_get_raid_tid(struct mps_softc *sc, u64 wwid, u16 volHandle)
2239 {
2240 	u32 start_idx, end_idx, map_idx;
2241 	struct dev_mapping_table *mt_entry;
2242 
2243 	_mapping_get_ir_maprange(sc, &start_idx, &end_idx);
2244 	mt_entry = &sc->mapping_table[start_idx];
2245 	for (map_idx  = start_idx; map_idx <= end_idx; map_idx++, mt_entry++) {
2246 		if (mt_entry->dev_handle == volHandle &&
2247 		    mt_entry->physical_id == wwid)
2248 			return mt_entry->id;
2249 	}
2250 
2251 	return MPS_MAP_BAD_ID;
2252 }
2253 
2254 /**
2255  * mps_mapping_get_raid_tid_from_handle - find raid device in mapping table
2256  * using only the volume dev handle.  This is just a wrapper function for the
2257  * local function _mapping_get_ir_mt_idx_from_handle.
2258  * @sc: per adapter object
2259  * @volHandle: volume device handle
2260  *
2261  * Returns valid target ID on success or BAD_ID.
2262  */
2263 unsigned int
2264 mps_mapping_get_raid_tid_from_handle(struct mps_softc *sc, u16 volHandle)
2265 {
2266 	return (_mapping_get_ir_mt_idx_from_handle(sc, volHandle));
2267 }
2268 
2269 /**
2270  * mps_mapping_enclosure_dev_status_change_event - handle enclosure events
2271  * @sc: per adapter object
2272  * @event_data: event data payload
2273  *
2274  * Return nothing.
2275  */
2276 void
2277 mps_mapping_enclosure_dev_status_change_event(struct mps_softc *sc,
2278     Mpi2EventDataSasEnclDevStatusChange_t *event_data)
2279 {
2280 	u8 enc_idx, missing_count;
2281 	struct enc_mapping_table *et_entry;
2282 	Mpi2DriverMap0Entry_t *dpm_entry;
2283 	u16 ioc_pg8_flags = le16toh(sc->ioc_pg8.Flags);
2284 	u8 map_shift = MPI2_DRVMAP0_MAPINFO_SLOT_SHIFT;
2285 	u8 update_phy_bits = 0;
2286 	u32 saved_phy_bits;
2287 	uint64_t temp64_var;
2288 
2289 	if ((ioc_pg8_flags & MPI2_IOCPAGE8_FLAGS_MASK_MAPPING_MODE) !=
2290 	    MPI2_IOCPAGE8_FLAGS_ENCLOSURE_SLOT_MAPPING)
2291 		goto out;
2292 
2293 	dpm_entry = (Mpi2DriverMap0Entry_t *)((u8 *)sc->dpm_pg0 +
2294 	    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
2295 
2296 	if (event_data->ReasonCode == MPI2_EVENT_SAS_ENCL_RC_ADDED) {
2297 		if (!event_data->NumSlots) {
2298 			mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: Enclosure "
2299 			    "with handle = 0x%x reported 0 slots.\n", __func__,
2300 			    le16toh(event_data->EnclosureHandle));
2301 			goto out;
2302 		}
2303 		temp64_var = event_data->EnclosureLogicalID.High;
2304 		temp64_var = (temp64_var << 32) |
2305 		    event_data->EnclosureLogicalID.Low;
2306 		enc_idx = _mapping_get_enc_idx_from_id(sc, temp64_var,
2307 		    event_data->PhyBits);
2308 
2309 		/*
2310 		 * If the Added enclosure is already in the Enclosure Table,
2311 		 * make sure that all the enclosure info is up to date. If
2312 		 * the enclosure was missing and has just been added back, or if
2313 		 * the enclosure's Phy Bits have changed, clear the missing
2314 		 * count and update the Phy Bits in the mapping table and in the
2315 		 * DPM, if it's being used.
2316 		 */
2317 		if (enc_idx != MPS_ENCTABLE_BAD_IDX) {
2318 			et_entry = &sc->enclosure_table[enc_idx];
2319 			if (et_entry->init_complete &&
2320 			    !et_entry->missing_count) {
2321 				mps_dprint(sc, MPS_MAPPING, "%s: Enclosure %d "
2322 				    "is already present with handle = 0x%x\n",
2323 				    __func__, enc_idx, et_entry->enc_handle);
2324 				goto out;
2325 			}
2326 			et_entry->enc_handle = le16toh(event_data->
2327 			    EnclosureHandle);
2328 			et_entry->start_slot = le16toh(event_data->StartSlot);
2329 			saved_phy_bits = et_entry->phy_bits;
2330 			et_entry->phy_bits |= le32toh(event_data->PhyBits);
2331 			if (saved_phy_bits != et_entry->phy_bits)
2332 				update_phy_bits = 1;
2333 			if (et_entry->missing_count || update_phy_bits) {
2334 				et_entry->missing_count = 0;
2335 				if (sc->is_dpm_enable &&
2336 				    et_entry->dpm_entry_num !=
2337 				    MPS_DPM_BAD_IDX) {
2338 					dpm_entry += et_entry->dpm_entry_num;
2339 					missing_count =
2340 					    (u8)(dpm_entry->MappingInformation &
2341 					    MPI2_DRVMAP0_MAPINFO_MISSING_MASK);
2342 					if (missing_count || update_phy_bits) {
2343 						dpm_entry->MappingInformation
2344 						    = et_entry->num_slots;
2345 						dpm_entry->MappingInformation
2346 						    <<= map_shift;
2347 						dpm_entry->PhysicalBitsMapping
2348 						    = et_entry->phy_bits;
2349 						sc->dpm_flush_entry[et_entry->
2350 						    dpm_entry_num] = 1;
2351 					}
2352 				}
2353 			}
2354 		} else {
2355 			/*
2356 			 * This is a new enclosure that is being added.
2357 			 * Initialize the Enclosure Table entry. It will be
2358 			 * finalized when a device is added for the enclosure
2359 			 * and the enclosure has enough space in the Mapping
2360 			 * Table to map its devices.
2361 			 */
2362 			if (sc->num_enc_table_entries < sc->max_enclosures) {
2363 				enc_idx = sc->num_enc_table_entries;
2364 				sc->num_enc_table_entries++;
2365 			} else {
2366 				enc_idx = _mapping_get_high_missing_et_idx(sc);
2367 				if (enc_idx != MPS_ENCTABLE_BAD_IDX) {
2368 					et_entry = &sc->enclosure_table[enc_idx];
2369 					_mapping_add_to_removal_table(sc,
2370 					    et_entry->dpm_entry_num);
2371 					_mapping_clear_enc_entry(et_entry);
2372 				}
2373 			}
2374 			if (enc_idx == MPS_ENCTABLE_BAD_IDX) {
2375 				mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: "
2376 				    "Enclosure cannot be added to mapping "
2377 				    "table because it's full.\n", __func__);
2378 				goto out;
2379 			}
2380 			et_entry = &sc->enclosure_table[enc_idx];
2381 			et_entry->enc_handle = le16toh(event_data->
2382 			    EnclosureHandle);
2383 			et_entry->enclosure_id = le64toh(event_data->
2384 			    EnclosureLogicalID.High);
2385 			et_entry->enclosure_id =
2386 			    ((et_entry->enclosure_id << 32) |
2387 			    le64toh(event_data->EnclosureLogicalID.Low));
2388 			et_entry->start_index = MPS_MAPTABLE_BAD_IDX;
2389 			et_entry->dpm_entry_num = MPS_DPM_BAD_IDX;
2390 			et_entry->num_slots = le16toh(event_data->NumSlots);
2391 			et_entry->start_slot = le16toh(event_data->StartSlot);
2392 			et_entry->phy_bits = le32toh(event_data->PhyBits);
2393 		}
2394 		et_entry->init_complete = 1;
2395 	} else if (event_data->ReasonCode ==
2396 	    MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING) {
2397 		/*
2398 		 * An enclosure was removed. Update its missing count and then
2399 		 * update the DPM entry with the new missing count for the
2400 		 * enclosure.
2401 		 */
2402 		enc_idx = _mapping_get_enc_idx_from_handle(sc,
2403 		    le16toh(event_data->EnclosureHandle));
2404 		if (enc_idx == MPS_ENCTABLE_BAD_IDX) {
2405 			mps_dprint(sc, MPS_ERROR | MPS_MAPPING, "%s: Cannot "
2406 			    "unmap enclosure with handle 0x%04x because it "
2407 			    "has already been deleted.\n", __func__,
2408 			    le16toh(event_data->EnclosureHandle));
2409 			goto out;
2410 		}
2411 		et_entry = &sc->enclosure_table[enc_idx];
2412 		if (et_entry->missing_count < MPS_MAX_MISSING_COUNT)
2413 			et_entry->missing_count++;
2414 		if (sc->is_dpm_enable &&
2415 		    et_entry->dpm_entry_num != MPS_DPM_BAD_IDX) {
2416 			dpm_entry += et_entry->dpm_entry_num;
2417 			dpm_entry->MappingInformation = et_entry->num_slots;
2418 			dpm_entry->MappingInformation <<= map_shift;
2419 			dpm_entry->MappingInformation |=
2420 			    et_entry->missing_count;
2421 			sc->dpm_flush_entry[et_entry->dpm_entry_num] = 1;
2422 		}
2423 		et_entry->init_complete = 1;
2424 	}
2425 
2426 out:
2427 	_mapping_flush_dpm_pages(sc);
2428 	if (sc->pending_map_events)
2429 		sc->pending_map_events--;
2430 }
2431 
2432 /**
2433  * mps_mapping_topology_change_event - handle topology change events
2434  * @sc: per adapter object
2435  * @event_data: event data payload
2436  *
2437  * Returns nothing.
2438  */
2439 void
2440 mps_mapping_topology_change_event(struct mps_softc *sc,
2441     Mpi2EventDataSasTopologyChangeList_t *event_data)
2442 {
2443 	struct _map_topology_change topo_change;
2444 	struct _map_phy_change *phy_change;
2445 	Mpi2EventSasTopoPhyEntry_t *event_phy_change;
2446 	u8 i, num_entries;
2447 
2448 	topo_change.enc_handle = le16toh(event_data->EnclosureHandle);
2449 	topo_change.exp_handle = le16toh(event_data->ExpanderDevHandle);
2450 	num_entries = event_data->NumEntries;
2451 	topo_change.num_entries = num_entries;
2452 	topo_change.start_phy_num = event_data->StartPhyNum;
2453 	topo_change.num_phys = event_data->NumPhys;
2454 	topo_change.exp_status = event_data->ExpStatus;
2455 	event_phy_change = event_data->PHY;
2456 	topo_change.phy_details = NULL;
2457 
2458 	if (!num_entries)
2459 		goto out;
2460 	phy_change = malloc(sizeof(struct _map_phy_change) * num_entries,
2461 	    M_MPT2, M_NOWAIT|M_ZERO);
2462 	topo_change.phy_details = phy_change;
2463 	if (!phy_change)
2464 		goto out;
2465 	for (i = 0; i < num_entries; i++, event_phy_change++, phy_change++) {
2466 		phy_change->dev_handle = le16toh(event_phy_change->
2467 		    AttachedDevHandle);
2468 		phy_change->reason = event_phy_change->PhyStatus &
2469 		    MPI2_EVENT_SAS_TOPO_RC_MASK;
2470 	}
2471 	_mapping_update_missing_count(sc, &topo_change);
2472 	_mapping_get_dev_info(sc, &topo_change);
2473 	_mapping_clear_removed_entries(sc);
2474 	_mapping_add_new_device(sc, &topo_change);
2475 
2476 out:
2477 	free(topo_change.phy_details, M_MPT2);
2478 	_mapping_flush_dpm_pages(sc);
2479 	if (sc->pending_map_events)
2480 		sc->pending_map_events--;
2481 }
2482 
2483 /**
2484  * mps_mapping_ir_config_change_event - handle IR config change list events
2485  * @sc: per adapter object
2486  * @event_data: event data payload
2487  *
2488  * Returns nothing.
2489  */
2490 void
2491 mps_mapping_ir_config_change_event(struct mps_softc *sc,
2492     Mpi2EventDataIrConfigChangeList_t *event_data)
2493 {
2494 	Mpi2EventIrConfigElement_t *element;
2495 	int i;
2496 	u64 *wwid_table;
2497 	u32 map_idx, flags;
2498 	struct dev_mapping_table *mt_entry;
2499 	u16 element_flags;
2500 
2501 	wwid_table = malloc(sizeof(u64) * event_data->NumElements, M_MPT2,
2502 	    M_NOWAIT | M_ZERO);
2503 	if (!wwid_table)
2504 		goto out;
2505 	element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
2506 	flags = le32toh(event_data->Flags);
2507 
2508 	/*
2509 	 * For volume changes, get the WWID for the volume and put it in a
2510 	 * table to be used in the processing of the IR change event.
2511 	 */
2512 	for (i = 0; i < event_data->NumElements; i++, element++) {
2513 		element_flags = le16toh(element->ElementFlags);
2514 		if ((element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_ADDED) &&
2515 		    (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_REMOVED) &&
2516 		    (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE)
2517 		    && (element->ReasonCode !=
2518 			MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED))
2519 			continue;
2520 		if ((element_flags &
2521 		    MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK) ==
2522 		    MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT) {
2523 			mps_config_get_volume_wwid(sc,
2524 			    le16toh(element->VolDevHandle), &wwid_table[i]);
2525 		}
2526 	}
2527 
2528 	/*
2529 	 * Check the ReasonCode for each element in the IR event and Add/Remove
2530 	 * Volumes or Physical Disks of Volumes to/from the mapping table. Use
2531 	 * the WWIDs gotten above in wwid_table.
2532 	 */
2533 	if (flags == MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
2534 		goto out;
2535 	else {
2536 		element = (Mpi2EventIrConfigElement_t *)&event_data->
2537 		    ConfigElement[0];
2538 		for (i = 0; i < event_data->NumElements; i++, element++) {
2539 			if (element->ReasonCode ==
2540 			    MPI2_EVENT_IR_CHANGE_RC_ADDED ||
2541 			    element->ReasonCode ==
2542 			    MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED) {
2543 				map_idx = _mapping_get_ir_mt_idx_from_wwid
2544 				    (sc, wwid_table[i]);
2545 				if (map_idx != MPS_MAPTABLE_BAD_IDX) {
2546 					/*
2547 					 * The volume is already in the mapping
2548 					 * table. Just update it's info.
2549 					 */
2550 					mt_entry = &sc->mapping_table[map_idx];
2551 					mt_entry->id = map_idx;
2552 					mt_entry->dev_handle = le16toh
2553 					    (element->VolDevHandle);
2554 					mt_entry->device_info =
2555 					    MPS_DEV_RESERVED | MPS_MAP_IN_USE;
2556 					_mapping_update_ir_missing_cnt(sc,
2557 					    map_idx, element, wwid_table[i]);
2558 					continue;
2559 				}
2560 
2561 				/*
2562 				 * Volume is not in mapping table yet. Find a
2563 				 * free entry in the mapping table at the
2564 				 * volume mapping locations. If no entries are
2565 				 * available, this is an error because it means
2566 				 * there are more volumes than can be mapped
2567 				 * and that should never happen for volumes.
2568 				 */
2569 				map_idx = _mapping_get_free_ir_mt_idx(sc);
2570 				if (map_idx == MPS_MAPTABLE_BAD_IDX)
2571 				{
2572 					mps_dprint(sc, MPS_ERROR | MPS_MAPPING,
2573 					    "%s: failed to add the volume with "
2574 					    "handle 0x%04x because there is no "
2575 					    "free space available in the "
2576 					    "mapping table\n", __func__,
2577 					    le16toh(element->VolDevHandle));
2578 					continue;
2579 				}
2580 				mt_entry = &sc->mapping_table[map_idx];
2581 				mt_entry->physical_id = wwid_table[i];
2582 				mt_entry->id = map_idx;
2583 				mt_entry->dev_handle = le16toh(element->
2584 				    VolDevHandle);
2585 				mt_entry->device_info = MPS_DEV_RESERVED |
2586 				    MPS_MAP_IN_USE;
2587 				_mapping_update_ir_missing_cnt(sc, map_idx,
2588 				    element, wwid_table[i]);
2589 			} else if (element->ReasonCode ==
2590 			    MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
2591 				map_idx = _mapping_get_ir_mt_idx_from_wwid(sc,
2592 				    wwid_table[i]);
2593 				if (map_idx == MPS_MAPTABLE_BAD_IDX) {
2594 					mps_dprint(sc, MPS_MAPPING,"%s: Failed "
2595 					    "to remove a volume because it has "
2596 					    "already been removed.\n",
2597 					    __func__);
2598 					continue;
2599 				}
2600 				_mapping_update_ir_missing_cnt(sc, map_idx,
2601 				    element, wwid_table[i]);
2602 			} else if (element->ReasonCode ==
2603 			    MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED) {
2604 				map_idx = _mapping_get_mt_idx_from_handle(sc,
2605 				    le16toh(element->VolDevHandle));
2606 				if (map_idx == MPS_MAPTABLE_BAD_IDX) {
2607 					mps_dprint(sc, MPS_MAPPING,"%s: Failed "
2608 					    "to remove volume with handle "
2609 					    "0x%04x because it has already "
2610 					    "been removed.\n", __func__,
2611 					    le16toh(element->VolDevHandle));
2612 					continue;
2613 				}
2614 				mt_entry = &sc->mapping_table[map_idx];
2615 				_mapping_update_ir_missing_cnt(sc, map_idx,
2616 				    element, mt_entry->physical_id);
2617 			}
2618 		}
2619 	}
2620 
2621 out:
2622 	_mapping_flush_dpm_pages(sc);
2623 	free(wwid_table, M_MPT2);
2624 	if (sc->pending_map_events)
2625 		sc->pending_map_events--;
2626 }
2627 
2628 int
2629 mps_mapping_dump(SYSCTL_HANDLER_ARGS)
2630 {
2631 	struct mps_softc *sc;
2632 	struct dev_mapping_table *mt_entry;
2633 	struct sbuf sbuf;
2634 	int i, error;
2635 
2636 	sc = (struct mps_softc *)arg1;
2637 
2638 	error = sysctl_wire_old_buffer(req, 0);
2639 	if (error != 0)
2640 		return (error);
2641 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
2642 
2643 	sbuf_printf(&sbuf, "\nindex physical_id       handle id\n");
2644 	for (i = 0; i < sc->max_devices; i++) {
2645 		mt_entry = &sc->mapping_table[i];
2646 		if (mt_entry->physical_id == 0)
2647 			continue;
2648 		sbuf_printf(&sbuf, "%4d  %jx  %04x   %hd\n",
2649 		    i, mt_entry->physical_id, mt_entry->dev_handle,
2650 		    mt_entry->id);
2651 	}
2652 	error = sbuf_finish(&sbuf);
2653 	sbuf_delete(&sbuf);
2654 	return (error);
2655 }
2656 
2657 int
2658 mps_mapping_encl_dump(SYSCTL_HANDLER_ARGS)
2659 {
2660 	struct mps_softc *sc;
2661 	struct enc_mapping_table *enc_entry;
2662 	struct sbuf sbuf;
2663 	int i, error;
2664 
2665 	sc = (struct mps_softc *)arg1;
2666 
2667 	error = sysctl_wire_old_buffer(req, 0);
2668 	if (error != 0)
2669 		return (error);
2670 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
2671 
2672 	sbuf_printf(&sbuf, "\nindex enclosure_id      handle map_index\n");
2673 	for (i = 0; i < sc->max_enclosures; i++) {
2674 		enc_entry = &sc->enclosure_table[i];
2675 		if (enc_entry->enclosure_id == 0)
2676 			continue;
2677 		sbuf_printf(&sbuf, "%4d  %jx  %04x   %d\n",
2678 		    i, enc_entry->enclosure_id, enc_entry->enc_handle,
2679 		    enc_entry->start_index);
2680 	}
2681 	error = sbuf_finish(&sbuf);
2682 	sbuf_delete(&sbuf);
2683 	return (error);
2684 }
2685