xref: /openbmc/phosphor-logging/extensions/openpower-pels/callouts.cpp (revision 075c79237505ea3b810a461f5f514e4d520a0c44)
1711d51d8SMatt Spinler /**
2711d51d8SMatt Spinler  * Copyright © 2019 IBM Corporation
3711d51d8SMatt Spinler  *
4711d51d8SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5711d51d8SMatt Spinler  * you may not use this file except in compliance with the License.
6711d51d8SMatt Spinler  * You may obtain a copy of the License at
7711d51d8SMatt Spinler  *
8711d51d8SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9711d51d8SMatt Spinler  *
10711d51d8SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11711d51d8SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12711d51d8SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13711d51d8SMatt Spinler  * See the License for the specific language governing permissions and
14711d51d8SMatt Spinler  * limitations under the License.
15711d51d8SMatt Spinler  */
1632f13c91SMatt Spinler #include "callouts.hpp"
1732f13c91SMatt Spinler 
18cc9adb79SMatt Spinler #include <phosphor-logging/lg2.hpp>
192544b419SPatrick Williams 
202ea96f6cSMatt Spinler #include <algorithm>
21e0366f31SMatt Spinler 
2232f13c91SMatt Spinler namespace openpower
2332f13c91SMatt Spinler {
2432f13c91SMatt Spinler namespace pels
2532f13c91SMatt Spinler {
2632f13c91SMatt Spinler namespace src
2732f13c91SMatt Spinler {
2832f13c91SMatt Spinler 
Callouts(Stream & pel)2932f13c91SMatt Spinler Callouts::Callouts(Stream& pel)
3032f13c91SMatt Spinler {
3132f13c91SMatt Spinler     pel >> _subsectionID >> _subsectionFlags >> _subsectionWordLength;
3232f13c91SMatt Spinler 
3332f13c91SMatt Spinler     size_t currentLength = sizeof(_subsectionID) + sizeof(_subsectionFlags) +
3432f13c91SMatt Spinler                            sizeof(_subsectionWordLength);
3532f13c91SMatt Spinler 
3632f13c91SMatt Spinler     while ((_subsectionWordLength * 4) > currentLength)
3732f13c91SMatt Spinler     {
3832f13c91SMatt Spinler         _callouts.emplace_back(new Callout(pel));
3932f13c91SMatt Spinler         currentLength += _callouts.back()->flattenedSize();
4032f13c91SMatt Spinler     }
4132f13c91SMatt Spinler }
4232f13c91SMatt Spinler 
flatten(Stream & pel) const43724d0d8cSMatt Spinler void Callouts::flatten(Stream& pel) const
4432f13c91SMatt Spinler {
4532f13c91SMatt Spinler     pel << _subsectionID << _subsectionFlags << _subsectionWordLength;
4632f13c91SMatt Spinler 
4732f13c91SMatt Spinler     for (auto& callout : _callouts)
4832f13c91SMatt Spinler     {
4932f13c91SMatt Spinler         callout->flatten(pel);
5032f13c91SMatt Spinler     }
5132f13c91SMatt Spinler }
5232f13c91SMatt Spinler 
addCallout(std::unique_ptr<Callout> callout)53e0366f31SMatt Spinler void Callouts::addCallout(std::unique_ptr<Callout> callout)
54e0366f31SMatt Spinler {
554efed0efSMatt Spinler     bool shouldAdd = true;
564efed0efSMatt Spinler 
574efed0efSMatt Spinler     // Check if there is already a callout for this FRU
58*075c7923SPatrick Williams     auto it = std::ranges::find_if(_callouts, [&callout](const auto& c) {
59*075c7923SPatrick Williams         return *callout == *c;
60*075c7923SPatrick Williams     });
614efed0efSMatt Spinler 
624efed0efSMatt Spinler     // If the callout already exists, but the new one has a higher
634efed0efSMatt Spinler     // priority, change the existing callout's priority to this
644efed0efSMatt Spinler     // new value and don't add the new one.
654efed0efSMatt Spinler     if (it != _callouts.end())
664efed0efSMatt Spinler     {
674efed0efSMatt Spinler         if (*callout > **it)
684efed0efSMatt Spinler         {
694efed0efSMatt Spinler             (*it)->setPriority(callout->priority());
704efed0efSMatt Spinler         }
714efed0efSMatt Spinler         shouldAdd = false;
724efed0efSMatt Spinler     }
734efed0efSMatt Spinler 
744efed0efSMatt Spinler     if (shouldAdd)
754efed0efSMatt Spinler     {
76e0366f31SMatt Spinler         if (_callouts.size() < maxNumberOfCallouts)
77e0366f31SMatt Spinler         {
78e0366f31SMatt Spinler             _callouts.push_back(std::move(callout));
79e0366f31SMatt Spinler 
80e0366f31SMatt Spinler             _subsectionWordLength += _callouts.back()->flattenedSize() / 4;
81e0366f31SMatt Spinler         }
82e0366f31SMatt Spinler         else
83e0366f31SMatt Spinler         {
84cc9adb79SMatt Spinler             lg2::info("Dropping PEL callout because at max");
85e0366f31SMatt Spinler         }
864efed0efSMatt Spinler     }
8753ef1552SMiguel Gomez 
884efed0efSMatt Spinler     std::ranges::sort(_callouts,
894efed0efSMatt Spinler                       [](const auto& c1, const auto& c2) { return *c1 > *c2; });
90e0366f31SMatt Spinler }
9153ef1552SMiguel Gomez 
9232f13c91SMatt Spinler } // namespace src
9332f13c91SMatt Spinler } // namespace pels
9432f13c91SMatt Spinler } // namespace openpower
95