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