xref: /freebsd/sys/dev/isci/scil/scic_sds_library.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23  * The full GNU General Public License is included in this distribution
24  * in the file called LICENSE.GPL.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *   * Redistributions of source code must retain the above copyright
36  *     notice, this list of conditions and the following disclaimer.
37  *   * Redistributions in binary form must reproduce the above copyright
38  *     notice, this list of conditions and the following disclaimer in
39  *     the documentation and/or other materials provided with the
40  *     distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /**
59  * @file
60  *
61  * @brief This file contains the implementation of the public methods for a
62  *        SCIC_SDS_LIBRARY object.
63  */
64 
65 #include <dev/isci/scil/scic_library.h>
66 #include <dev/isci/scil/scic_sds_library.h>
67 #include <dev/isci/scil/scic_sds_controller.h>
68 #include <dev/isci/scil/scic_sds_request.h>
69 #include <dev/isci/scil/scic_sds_remote_device.h>
70 #include <dev/isci/scil/intel_pci.h>
71 #include <dev/isci/scil/scic_sds_pci.h>
72 #include <dev/isci/scil/scu_constants.h>
73 
74 struct SCIC_SDS_CONTROLLER;
75 
76 #define SCIC_LIBRARY_CONTROLLER_MEMORY_START(library) \
77    ((char *)(library) + sizeof(SCIC_SDS_LIBRARY_T))
78 
79 // ---------------------------------------------------------------------------
80 
81 U32 scic_library_get_object_size(
82    U8 max_controller_count
83 )
84 {
85    return   sizeof(SCIC_SDS_LIBRARY_T)
86           + scic_sds_controller_get_object_size() * max_controller_count;
87 }
88 
89 // ---------------------------------------------------------------------------
90 
91 SCI_LIBRARY_HANDLE_T scic_library_construct(
92    void                    * library_memory,
93    U8                        max_controller_count
94 )
95 {
96    SCI_STATUS status;
97    SCIC_SDS_LIBRARY_T *this_library;
98 
99    this_library = (SCIC_SDS_LIBRARY_T *)library_memory;
100 
101    this_library->max_controller_count = max_controller_count;
102 
103    this_library->controllers =
104       (SCIC_SDS_CONTROLLER_T *)((char *)library_memory + sizeof(SCIC_SDS_LIBRARY_T));
105 
106    SCI_BASE_LIBRARY_CONSTRUCT(this_library,
107                               &this_library->parent,
108                               max_controller_count,
109                               struct SCIC_SDS_CONTROLLER,
110                               status);
111    return this_library;
112 }
113 
114 // ---------------------------------------------------------------------------
115 
116 void scic_library_set_pci_info(
117    SCI_LIBRARY_HANDLE_T      library,
118    SCI_PCI_COMMON_HEADER_T * pci_header
119 )
120 {
121    SCIC_SDS_LIBRARY_T *this_library;
122    this_library = (SCIC_SDS_LIBRARY_T *)library;
123 
124    this_library->pci_device   = pci_header->device_id;
125 
126 #if defined(PBG_HBA_A0_BUILD)
127    this_library->pci_revision = SCIC_SDS_PCI_REVISION_A0;
128 #elif defined(PBG_HBA_A2_BUILD)
129    this_library->pci_revision = SCIC_SDS_PCI_REVISION_A2;
130 #elif defined(PBG_HBA_BETA_BUILD)
131    this_library->pci_revision = SCIC_SDS_PCI_REVISION_B0;
132 #elif defined(PBG_BUILD)
133    // The SCU PCI function revision ID for A0/A2 is not populated
134    // properly.  As a result, we force the revision ID to A2 for
135    // this situation.  Therefore, the standard PBG build will not
136    // work for A0.
137    if (pci_header->revision == SCIC_SDS_PCI_REVISION_A0)
138       this_library->pci_revision = SCIC_SDS_PCI_REVISION_A2;
139    else
140       this_library->pci_revision = pci_header->revision;
141 #endif
142 }
143 
144 // ---------------------------------------------------------------------------
145 
146 SCI_STATUS scic_library_allocate_controller(
147    SCI_LIBRARY_HANDLE_T    library,
148    SCI_CONTROLLER_HANDLE_T *new_controller
149 )
150 {
151    SCI_STATUS status;
152    SCIC_SDS_LIBRARY_T *this_library;
153 
154    this_library = (SCIC_SDS_LIBRARY_T *)library;
155 
156    if (
157          (  (this_library->pci_device >= 0x1D60)
158          && (this_library->pci_device <= 0x1D62)
159          )
160       || (  (this_library->pci_device >= 0x1D64)
161          && (this_library->pci_device <= 0x1D65)
162          )
163       || (  (this_library->pci_device >= 0x1D68)
164          && (this_library->pci_device <= 0x1D6F)
165          )
166       )
167    {
168       SCI_BASE_LIBRARY_ALLOCATE_CONTROLLER(
169          this_library, new_controller, &status);
170    }
171    else
172       status = SCI_FAILURE_UNSUPPORTED_PCI_DEVICE_ID;
173 
174    return status;
175 }
176 
177 // ---------------------------------------------------------------------------
178 
179 SCI_STATUS scic_library_free_controller(
180    SCI_LIBRARY_HANDLE_T library,
181    SCI_CONTROLLER_HANDLE_T controller
182 )
183 {
184    SCI_STATUS status;
185    SCIC_SDS_LIBRARY_T *this_library;
186    this_library = (SCIC_SDS_LIBRARY_T *)library;
187 
188    SCI_BASE_LIBRARY_FREE_CONTROLLER(
189       this_library, controller, struct SCIC_SDS_CONTROLLER, &status);
190 
191    return status;
192 }
193 
194 // ---------------------------------------------------------------------------
195 
196 U8 scic_library_get_pci_device_controller_count(
197    SCI_LIBRARY_HANDLE_T library
198 )
199 {
200    SCIC_SDS_LIBRARY_T *this_library;
201    U16 device_id;
202 
203    this_library = (SCIC_SDS_LIBRARY_T *)library;
204    device_id = this_library->pci_device;
205 
206    //Check if we are on a b0 or c0 which has 2 controllers
207    if (
208          // Warning: If 0x1d66 is ever defined to be a single controller
209          //          this logic will fail.
210          //          If 0x1d63 or 0x1d67 is ever defined to be dual
211          //          controller this logic will fail.
212          ((device_id & 0xFFF1) == 0x1D60)
213       && (
214             (this_library->pci_revision == SCU_PBG_HBA_REV_B0)
215          || (this_library->pci_revision == SCU_PBG_HBA_REV_C0)
216          || (this_library->pci_revision == SCU_PBG_HBA_REV_C1)
217          )
218       )
219       return 2;
220    else
221       return 1;
222 }
223 
224 // ---------------------------------------------------------------------------
225 
226 U32 scic_library_get_max_sge_size(
227    SCI_LIBRARY_HANDLE_T library
228 )
229 {
230    return SCU_IO_REQUEST_MAX_SGE_SIZE;
231 }
232 
233 // ---------------------------------------------------------------------------
234 
235 U32 scic_library_get_max_sge_count(
236    SCI_LIBRARY_HANDLE_T library
237 )
238 {
239    return SCU_IO_REQUEST_SGE_COUNT;
240 }
241 
242 // ---------------------------------------------------------------------------
243 
244 U32 scic_library_get_max_io_length(
245    SCI_LIBRARY_HANDLE_T library
246 )
247 {
248    return SCU_IO_REQUEST_MAX_TRANSFER_LENGTH;
249 }
250 
251 // ---------------------------------------------------------------------------
252 
253 U16 scic_library_get_min_timer_count(void)
254 {
255    return (U16) (scic_sds_controller_get_min_timer_count()
256                + scic_sds_remote_device_get_min_timer_count()
257                + scic_sds_request_get_min_timer_count());
258 }
259 
260 // ---------------------------------------------------------------------------
261 
262 U16 scic_library_get_max_timer_count(void)
263 {
264    return (U16) (scic_sds_controller_get_max_timer_count()
265                + scic_sds_remote_device_get_max_timer_count()
266                + scic_sds_request_get_max_timer_count());
267 }
268 
269 /**
270  *
271  */
272 U8 scic_sds_library_get_controller_index(
273    SCIC_SDS_LIBRARY_T    * library,
274    SCIC_SDS_CONTROLLER_T * controller
275 )
276 {
277    U8 index;
278 
279    for (index = 0; index < library->max_controller_count; index++)
280    {
281       if (controller == &library->controllers[index])
282       {
283          return index;
284       }
285    }
286 
287    return 0xff;
288 }
289 
290