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