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