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