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