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