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