blob: 48e00a3381c31091bef566fe3f1b7b6aa03578d1 [file] [log] [blame]
Matt Spinleree401e92017-09-18 14:15:31 -05001/**
Patrick Venturee84b4dd2018-11-01 16:06:31 -07002 * Copyright (C) 2017 IBM Corporation
Matt Spinleree401e92017-09-18 14:15:31 -05003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Patrick Venturef78d9042018-11-01 15:39:53 -070016#include "registration.hpp"
17
Matt Spinleree401e92017-09-18 14:15:31 -050018#include <org/open_power/Proc/FSI/error.hpp>
Ramesh Iyyar6230b0d2019-12-05 10:06:44 -060019#include <phosphor-logging/elog-errors.hpp>
Matt Spinleree401e92017-09-18 14:15:31 -050020#include <phosphor-logging/log.hpp>
Matt Spinleree401e92017-09-18 14:15:31 -050021
Brad Bishop5e5d4452020-10-27 19:46:13 -040022#include <filesystem>
23#include <fstream>
24
Matt Spinleree401e92017-09-18 14:15:31 -050025namespace openpower
26{
27namespace openfsi
28{
29
30using namespace phosphor::logging;
Matt Spinleree401e92017-09-18 14:15:31 -050031namespace fsi_error = sdbusplus::org::open_power::Proc::FSI::Error;
32
Joel Stanley60db8b12019-09-27 15:23:39 +093033constexpr auto masterScanPath = "/sys/class/fsi-master/fsi0/rescan";
34constexpr auto hubScanPath = "/sys/class/fsi-master/fsi1/rescan";
35constexpr auto masterCalloutPath = "/sys/class/fsi-master/fsi0/slave@00:00/raw";
Matt Spinleree401e92017-09-18 14:15:31 -050036
Matt Spinleree401e92017-09-18 14:15:31 -050037/**
38 * Writes a 1 to the sysfs file passed in to trigger
39 * the device driver to do an FSI scan.
40 *
41 * @param[in] path - the sysfs path to write a 1 to
42 */
43static void doScan(const std::string& path)
44{
45 std::ofstream file;
46
Patrick Venturef78d9042018-11-01 15:39:53 -070047 file.exceptions(std::ofstream::failbit | // logic error on operation
48 std::ofstream::badbit | // read/write error on operation
49 std::ofstream::eofbit); // end of file reached
Matt Spinleree401e92017-09-18 14:15:31 -050050 try
51 {
52 file.open(path);
53 file << "1";
54 }
55 catch (std::exception& e)
56 {
57 auto err = errno;
58 throw std::system_error(err, std::generic_category());
59 }
60}
61
62/**
63 * Performs an FSI master scan followed by an FSI hub scan.
64 * This is where the device driver detects which chips are present.
65 *
66 * This is unrelated to scanning a ring out of a chip.
67 */
68void scan()
69{
Patrick Venturef78d9042018-11-01 15:39:53 -070070 // Note: Currently the FSI device driver will always return success on both
71 // the master and hub scans. The only way we can detect something
72 // went wrong is if the master scan didn't create the hub scan file, so
73 // we will check for that.
74 // It is possible the driver will be updated in the future to actually
75 // return a failure so the code will still check for them.
Matt Spinleree401e92017-09-18 14:15:31 -050076
77 try
78 {
79 doScan(masterScanPath);
80 }
81 catch (std::system_error& e)
82 {
83 log<level::ERR>("Failed to run the FSI master scan");
84
85 using metadata = org::open_power::Proc::FSI::MasterDetectionFailure;
86
87 elog<fsi_error::MasterDetectionFailure>(
Patrick Venturef78d9042018-11-01 15:39:53 -070088 metadata::CALLOUT_ERRNO(e.code().value()),
89 metadata::CALLOUT_DEVICE_PATH(masterCalloutPath));
Matt Spinleree401e92017-09-18 14:15:31 -050090 }
91
Brad Bishop56d14d02020-10-09 15:28:51 -040092 if (!std::filesystem::exists(hubScanPath))
Matt Spinleree401e92017-09-18 14:15:31 -050093 {
94 log<level::ERR>("The FSI master scan did not create a hub scan file");
95
96 using metadata = org::open_power::Proc::FSI::MasterDetectionFailure;
97
98 elog<fsi_error::MasterDetectionFailure>(
Patrick Venturef78d9042018-11-01 15:39:53 -070099 metadata::CALLOUT_ERRNO(0),
100 metadata::CALLOUT_DEVICE_PATH(masterCalloutPath));
Matt Spinleree401e92017-09-18 14:15:31 -0500101 }
102
103 try
104 {
105 doScan(hubScanPath);
106 }
107 catch (std::system_error& e)
108 {
Patrick Venturef78d9042018-11-01 15:39:53 -0700109 // If the device driver is ever updated in the future to fail the sysfs
110 // write call on a scan failure then it should also provide some hints
111 // about which hardware failed so we can do an appropriate callout
112 // here. At this point in time, the driver shouldn't ever fail so
113 // we won't worry about guessing at the callout.
Matt Spinleree401e92017-09-18 14:15:31 -0500114
115 log<level::ERR>("Failed to run the FSI hub scan");
116
117 using metadata = org::open_power::Proc::FSI::SlaveDetectionFailure;
118
119 elog<fsi_error::SlaveDetectionFailure>(
Patrick Venturef78d9042018-11-01 15:39:53 -0700120 metadata::ERRNO(e.code().value()));
Matt Spinleree401e92017-09-18 14:15:31 -0500121 }
122}
123
Brad Bishop63508a72020-10-27 18:55:01 -0400124REGISTER_PROCEDURE("scanFSI", scan)
Matt Spinleree401e92017-09-18 14:15:31 -0500125
Patrick Venturef78d9042018-11-01 15:39:53 -0700126} // namespace openfsi
127} // namespace openpower