1473c88f9SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2473c88f9SBruce Richardson * Copyright(c) 2010-2018 Intel Corporation
3473c88f9SBruce Richardson */
4473c88f9SBruce Richardson
5673c897fSWei Huang #include <fcntl.h>
6673c897fSWei Huang #include <inttypes.h>
7673c897fSWei Huang #include <unistd.h>
8673c897fSWei Huang
9473c88f9SBruce Richardson #include "opae_hw_api.h"
10473c88f9SBruce Richardson #include "ifpga_api.h"
11473c88f9SBruce Richardson
12473c88f9SBruce Richardson #include "ifpga_hw.h"
13473c88f9SBruce Richardson #include "ifpga_enumerate.h"
14473c88f9SBruce Richardson #include "ifpga_feature_dev.h"
15473c88f9SBruce Richardson
16673c897fSWei Huang struct dfl_fpga_enum_dfl {
17673c897fSWei Huang u64 start;
18673c897fSWei Huang u64 len;
19673c897fSWei Huang void *addr;
20673c897fSWei Huang TAILQ_ENTRY(dfl_fpga_enum_dfl) node;
21673c897fSWei Huang };
22673c897fSWei Huang
23673c897fSWei Huang TAILQ_HEAD(dfl_fpga_enum_dfls, dfl_fpga_enum_dfl);
24673c897fSWei Huang struct dfl_fpga_enum_info {
25673c897fSWei Huang struct ifpga_hw *hw;
26673c897fSWei Huang struct dfl_fpga_enum_dfls dfls;
27673c897fSWei Huang };
28673c897fSWei Huang
29473c88f9SBruce Richardson struct build_feature_devs_info {
30473c88f9SBruce Richardson struct opae_adapter_data_pci *pci_data;
31473c88f9SBruce Richardson
32473c88f9SBruce Richardson struct ifpga_afu_info *acc_info;
33473c88f9SBruce Richardson
34473c88f9SBruce Richardson void *fiu;
35473c88f9SBruce Richardson enum fpga_id_type current_type;
36473c88f9SBruce Richardson int current_port_id;
37473c88f9SBruce Richardson
38473c88f9SBruce Richardson void *ioaddr;
39473c88f9SBruce Richardson void *ioend;
40473c88f9SBruce Richardson uint64_t phys_addr;
41473c88f9SBruce Richardson
42473c88f9SBruce Richardson void *pfme_hdr;
43473c88f9SBruce Richardson
44473c88f9SBruce Richardson struct ifpga_hw *hw;
45473c88f9SBruce Richardson };
46473c88f9SBruce Richardson
feature_revision(void __iomem * start)47473c88f9SBruce Richardson static int feature_revision(void __iomem *start)
48473c88f9SBruce Richardson {
49473c88f9SBruce Richardson struct feature_header header;
50473c88f9SBruce Richardson
51473c88f9SBruce Richardson header.csr = readq(start);
52473c88f9SBruce Richardson
53473c88f9SBruce Richardson return header.revision;
54473c88f9SBruce Richardson }
55473c88f9SBruce Richardson
feature_size(void __iomem * start)56473c88f9SBruce Richardson static u32 feature_size(void __iomem *start)
57473c88f9SBruce Richardson {
58473c88f9SBruce Richardson struct feature_header header;
59473c88f9SBruce Richardson
60473c88f9SBruce Richardson header.csr = readq(start);
61473c88f9SBruce Richardson
62473c88f9SBruce Richardson /*the size of private feature is 4KB aligned*/
63473c88f9SBruce Richardson return header.next_header_offset ? header.next_header_offset:4096;
64473c88f9SBruce Richardson }
65473c88f9SBruce Richardson
feature_id(void __iomem * start)66473c88f9SBruce Richardson static u64 feature_id(void __iomem *start)
67473c88f9SBruce Richardson {
68473c88f9SBruce Richardson struct feature_header header;
69473c88f9SBruce Richardson
70473c88f9SBruce Richardson header.csr = readq(start);
71473c88f9SBruce Richardson
72473c88f9SBruce Richardson switch (header.type) {
73473c88f9SBruce Richardson case FEATURE_TYPE_FIU:
74473c88f9SBruce Richardson return FEATURE_ID_FIU_HEADER;
75473c88f9SBruce Richardson case FEATURE_TYPE_PRIVATE:
76473c88f9SBruce Richardson return header.id;
77473c88f9SBruce Richardson case FEATURE_TYPE_AFU:
78473c88f9SBruce Richardson return FEATURE_ID_AFU;
79473c88f9SBruce Richardson }
80473c88f9SBruce Richardson
81473c88f9SBruce Richardson WARN_ON(1);
82473c88f9SBruce Richardson return 0;
83473c88f9SBruce Richardson }
84473c88f9SBruce Richardson
85473c88f9SBruce Richardson static int
build_info_add_sub_feature(struct build_feature_devs_info * binfo,void __iomem * start,u64 fid,unsigned int size,unsigned int vec_start,unsigned int vec_cnt)86473c88f9SBruce Richardson build_info_add_sub_feature(struct build_feature_devs_info *binfo,
87473c88f9SBruce Richardson void __iomem *start, u64 fid, unsigned int size,
88473c88f9SBruce Richardson unsigned int vec_start,
89473c88f9SBruce Richardson unsigned int vec_cnt)
90473c88f9SBruce Richardson {
91473c88f9SBruce Richardson struct ifpga_hw *hw = binfo->hw;
92473c88f9SBruce Richardson struct ifpga_feature *feature = NULL;
93473c88f9SBruce Richardson struct feature_irq_ctx *ctx = NULL;
94473c88f9SBruce Richardson int port_id, ret = 0;
95473c88f9SBruce Richardson unsigned int i;
96473c88f9SBruce Richardson
97473c88f9SBruce Richardson fid = fid?fid:feature_id(start);
98473c88f9SBruce Richardson size = size?size:feature_size(start);
99473c88f9SBruce Richardson
100473c88f9SBruce Richardson feature = opae_malloc(sizeof(struct ifpga_feature));
101473c88f9SBruce Richardson if (!feature)
102473c88f9SBruce Richardson return -ENOMEM;
103473c88f9SBruce Richardson
104473c88f9SBruce Richardson feature->state = IFPGA_FEATURE_ATTACHED;
105473c88f9SBruce Richardson feature->addr = start;
106473c88f9SBruce Richardson feature->id = fid;
107473c88f9SBruce Richardson feature->size = size;
108473c88f9SBruce Richardson feature->revision = feature_revision(start);
109473c88f9SBruce Richardson feature->phys_addr = binfo->phys_addr +
110473c88f9SBruce Richardson ((u8 *)start - (u8 *)binfo->ioaddr);
111473c88f9SBruce Richardson feature->vec_start = vec_start;
112473c88f9SBruce Richardson feature->vec_cnt = vec_cnt;
113473c88f9SBruce Richardson
114473c88f9SBruce Richardson dev_debug(binfo, "%s: id=0x%llx, phys_addr=0x%llx, size=%u\n",
115473c88f9SBruce Richardson __func__, (unsigned long long)feature->id,
116473c88f9SBruce Richardson (unsigned long long)feature->phys_addr, size);
117473c88f9SBruce Richardson
118473c88f9SBruce Richardson if (vec_cnt) {
119473c88f9SBruce Richardson if (vec_start + vec_cnt <= vec_start)
120473c88f9SBruce Richardson return -EINVAL;
121473c88f9SBruce Richardson
122473c88f9SBruce Richardson ctx = zmalloc(sizeof(*ctx) * vec_cnt);
123473c88f9SBruce Richardson if (!ctx)
124473c88f9SBruce Richardson return -ENOMEM;
125473c88f9SBruce Richardson
126473c88f9SBruce Richardson for (i = 0; i < vec_cnt; i++) {
127473c88f9SBruce Richardson ctx[i].eventfd = -1;
128473c88f9SBruce Richardson ctx[i].idx = vec_start + i;
129473c88f9SBruce Richardson }
130473c88f9SBruce Richardson }
131473c88f9SBruce Richardson
132473c88f9SBruce Richardson feature->ctx = ctx;
133473c88f9SBruce Richardson feature->ctx_num = vec_cnt;
134473c88f9SBruce Richardson feature->vfio_dev_fd = binfo->pci_data->vfio_dev_fd;
135473c88f9SBruce Richardson
136473c88f9SBruce Richardson if (binfo->current_type == FME_ID) {
137473c88f9SBruce Richardson feature->parent = &hw->fme;
138473c88f9SBruce Richardson feature->type = FEATURE_FME_TYPE;
139473c88f9SBruce Richardson feature->name = get_fme_feature_name(fid);
140473c88f9SBruce Richardson TAILQ_INSERT_TAIL(&hw->fme.feature_list, feature, next);
141473c88f9SBruce Richardson } else if (binfo->current_type == PORT_ID) {
142473c88f9SBruce Richardson port_id = binfo->current_port_id;
143473c88f9SBruce Richardson feature->parent = &hw->port[port_id];
144473c88f9SBruce Richardson feature->type = FEATURE_PORT_TYPE;
145473c88f9SBruce Richardson feature->name = get_port_feature_name(fid);
146473c88f9SBruce Richardson TAILQ_INSERT_TAIL(&hw->port[port_id].feature_list,
147473c88f9SBruce Richardson feature, next);
148473c88f9SBruce Richardson } else {
149473c88f9SBruce Richardson return -EFAULT;
150473c88f9SBruce Richardson }
151473c88f9SBruce Richardson return ret;
152473c88f9SBruce Richardson }
153473c88f9SBruce Richardson
154473c88f9SBruce Richardson static int
create_feature_instance(struct build_feature_devs_info * binfo,void __iomem * start,u64 fid,unsigned int size,unsigned int vec_start,unsigned int vec_cnt)155473c88f9SBruce Richardson create_feature_instance(struct build_feature_devs_info *binfo,
156473c88f9SBruce Richardson void __iomem *start, u64 fid,
157473c88f9SBruce Richardson unsigned int size, unsigned int vec_start,
158473c88f9SBruce Richardson unsigned int vec_cnt)
159473c88f9SBruce Richardson {
160673c897fSWei Huang if (binfo->current_type != AFU_ID)
161673c897fSWei Huang return build_info_add_sub_feature(binfo, start, fid, size,
162673c897fSWei Huang vec_start, vec_cnt);
163673c897fSWei Huang return 0;
164473c88f9SBruce Richardson }
165473c88f9SBruce Richardson
166473c88f9SBruce Richardson /*
167473c88f9SBruce Richardson * UAFU GUID is dynamic as it can be changed after FME downloads different
168473c88f9SBruce Richardson * Green Bitstream to the port, so we treat the unknown GUIDs which are
169473c88f9SBruce Richardson * attached on port's feature list as UAFU.
170473c88f9SBruce Richardson */
feature_is_UAFU(struct build_feature_devs_info * binfo)171473c88f9SBruce Richardson static bool feature_is_UAFU(struct build_feature_devs_info *binfo)
172473c88f9SBruce Richardson {
173673c897fSWei Huang if ((binfo->current_type == PORT_ID) ||
174673c897fSWei Huang (binfo->current_type == AFU_ID))
175473c88f9SBruce Richardson return true;
176673c897fSWei Huang
177673c897fSWei Huang return false;
178473c88f9SBruce Richardson }
179473c88f9SBruce Richardson
parse_feature_uafu(struct build_feature_devs_info * binfo,struct feature_header * hdr)180673c897fSWei Huang static int parse_feature_uafu(struct build_feature_devs_info *binfo,
181473c88f9SBruce Richardson struct feature_header *hdr)
182473c88f9SBruce Richardson {
183473c88f9SBruce Richardson u64 id = PORT_FEATURE_ID_UAFU;
184473c88f9SBruce Richardson struct ifpga_afu_info *info;
185473c88f9SBruce Richardson void *start = (void *)hdr;
186473c88f9SBruce Richardson struct feature_port_header *port_hdr = binfo->ioaddr;
187473c88f9SBruce Richardson struct feature_port_capability capability;
188473c88f9SBruce Richardson int ret;
189473c88f9SBruce Richardson int size;
190473c88f9SBruce Richardson
191673c897fSWei Huang if (binfo->acc_info) {
192673c897fSWei Huang dev_info(binfo, "Sub AFU found @ %p.\n", start);
193673c897fSWei Huang return 0;
194673c897fSWei Huang }
195673c897fSWei Huang
196473c88f9SBruce Richardson capability.csr = readq(&port_hdr->capability);
197473c88f9SBruce Richardson
198673c897fSWei Huang if (binfo->current_type == AFU_ID) {
199673c897fSWei Huang size = AFU_REGION_SIZE;
200673c897fSWei Huang } else {
201673c897fSWei Huang capability.csr = readq(&port_hdr->capability);
202473c88f9SBruce Richardson size = capability.mmio_size << 10;
203673c897fSWei Huang }
204473c88f9SBruce Richardson
205473c88f9SBruce Richardson ret = create_feature_instance(binfo, hdr, id, size, 0, 0);
206473c88f9SBruce Richardson if (ret)
207473c88f9SBruce Richardson return ret;
208473c88f9SBruce Richardson
209473c88f9SBruce Richardson info = opae_malloc(sizeof(*info));
210473c88f9SBruce Richardson if (!info)
211473c88f9SBruce Richardson return -ENOMEM;
212473c88f9SBruce Richardson
213473c88f9SBruce Richardson info->region[0].addr = start;
214473c88f9SBruce Richardson info->region[0].phys_addr = binfo->phys_addr +
215473c88f9SBruce Richardson (uint8_t *)start - (uint8_t *)binfo->ioaddr;
216473c88f9SBruce Richardson info->region[0].len = size;
217673c897fSWei Huang info->num_regions = AFU_MAX_REGION;
218473c88f9SBruce Richardson
219473c88f9SBruce Richardson binfo->acc_info = info;
220473c88f9SBruce Richardson
221473c88f9SBruce Richardson return ret;
222473c88f9SBruce Richardson }
223473c88f9SBruce Richardson
224473c88f9SBruce Richardson /* create and register proper private data */
build_info_commit_dev(struct build_feature_devs_info * binfo)225473c88f9SBruce Richardson static int build_info_commit_dev(struct build_feature_devs_info *binfo)
226473c88f9SBruce Richardson {
227473c88f9SBruce Richardson struct ifpga_afu_info *info = binfo->acc_info;
228473c88f9SBruce Richardson struct ifpga_hw *hw = binfo->hw;
229473c88f9SBruce Richardson struct opae_manager *mgr;
230473c88f9SBruce Richardson struct opae_bridge *br;
231473c88f9SBruce Richardson struct opae_accelerator *acc;
232473c88f9SBruce Richardson struct ifpga_port_hw *port;
233473c88f9SBruce Richardson struct ifpga_fme_hw *fme;
234473c88f9SBruce Richardson struct ifpga_feature *feature;
235473c88f9SBruce Richardson
236673c897fSWei Huang if (binfo->current_type == PORT_ID) {
237473c88f9SBruce Richardson if (!binfo->fiu)
238473c88f9SBruce Richardson return 0;
239473c88f9SBruce Richardson
240473c88f9SBruce Richardson br = opae_bridge_alloc(hw->adapter->name, &ifpga_br_ops,
241473c88f9SBruce Richardson binfo->fiu);
242473c88f9SBruce Richardson if (!br)
243473c88f9SBruce Richardson return -ENOMEM;
244473c88f9SBruce Richardson
245473c88f9SBruce Richardson br->id = binfo->current_port_id;
246473c88f9SBruce Richardson
247473c88f9SBruce Richardson /* update irq info */
248473c88f9SBruce Richardson port = &hw->port[binfo->current_port_id];
249473c88f9SBruce Richardson feature = get_feature_by_id(&port->feature_list,
250473c88f9SBruce Richardson PORT_FEATURE_ID_UINT);
251673c897fSWei Huang if (feature && info)
252473c88f9SBruce Richardson info->num_irqs = feature->vec_cnt;
253473c88f9SBruce Richardson
254473c88f9SBruce Richardson acc = opae_accelerator_alloc(hw->adapter->name,
255473c88f9SBruce Richardson &ifpga_acc_ops, info);
256473c88f9SBruce Richardson if (!acc) {
257473c88f9SBruce Richardson opae_bridge_free(br);
258473c88f9SBruce Richardson return -ENOMEM;
259473c88f9SBruce Richardson }
260473c88f9SBruce Richardson
261673c897fSWei Huang acc->adapter = hw->adapter;
262473c88f9SBruce Richardson acc->br = br;
263473c88f9SBruce Richardson if (hw->adapter->mgr)
264473c88f9SBruce Richardson acc->mgr = hw->adapter->mgr;
265473c88f9SBruce Richardson acc->index = br->id;
266473c88f9SBruce Richardson
267473c88f9SBruce Richardson fme = &hw->fme;
268673c897fSWei Huang fme->nums_acc_region = info ? info->num_regions : 0;
269473c88f9SBruce Richardson
270473c88f9SBruce Richardson opae_adapter_add_acc(hw->adapter, acc);
271473c88f9SBruce Richardson
272473c88f9SBruce Richardson } else if (binfo->current_type == FME_ID) {
273673c897fSWei Huang if (!binfo->fiu)
274673c897fSWei Huang return 0;
275673c897fSWei Huang
276473c88f9SBruce Richardson mgr = opae_manager_alloc(hw->adapter->name, &ifpga_mgr_ops,
277473c88f9SBruce Richardson &ifpga_mgr_network_ops, binfo->fiu);
278473c88f9SBruce Richardson if (!mgr)
279473c88f9SBruce Richardson return -ENOMEM;
280473c88f9SBruce Richardson
281473c88f9SBruce Richardson mgr->adapter = hw->adapter;
282473c88f9SBruce Richardson hw->adapter->mgr = mgr;
283673c897fSWei Huang } else if (binfo->current_type == AFU_ID) {
284673c897fSWei Huang if (!info)
285673c897fSWei Huang return -EFAULT;
286673c897fSWei Huang
287673c897fSWei Huang info->num_irqs = 0;
288673c897fSWei Huang acc = opae_accelerator_alloc(hw->adapter->name,
289673c897fSWei Huang &ifpga_acc_ops, info);
290673c897fSWei Huang if (!acc)
291673c897fSWei Huang return -ENOMEM;
292673c897fSWei Huang
293673c897fSWei Huang acc->adapter = hw->adapter;
294673c897fSWei Huang acc->br = NULL;
295673c897fSWei Huang acc->mgr = NULL;
296673c897fSWei Huang acc->index = hw->num_afus++;
297673c897fSWei Huang
298673c897fSWei Huang opae_adapter_add_acc(hw->adapter, acc);
299473c88f9SBruce Richardson }
300473c88f9SBruce Richardson
301473c88f9SBruce Richardson binfo->fiu = NULL;
302473c88f9SBruce Richardson
303473c88f9SBruce Richardson return 0;
304473c88f9SBruce Richardson }
305473c88f9SBruce Richardson
306473c88f9SBruce Richardson static int
build_info_create_dev(struct build_feature_devs_info * binfo,enum fpga_id_type type,unsigned int index)307473c88f9SBruce Richardson build_info_create_dev(struct build_feature_devs_info *binfo,
308473c88f9SBruce Richardson enum fpga_id_type type, unsigned int index)
309473c88f9SBruce Richardson {
310473c88f9SBruce Richardson int ret;
311473c88f9SBruce Richardson
312673c897fSWei Huang if ((type == AFU_ID) && (binfo->current_type == PORT_ID))
313673c897fSWei Huang return 0;
314673c897fSWei Huang
315473c88f9SBruce Richardson ret = build_info_commit_dev(binfo);
316473c88f9SBruce Richardson if (ret)
317473c88f9SBruce Richardson return ret;
318473c88f9SBruce Richardson
319473c88f9SBruce Richardson binfo->current_type = type;
320673c897fSWei Huang binfo->acc_info = NULL;
321473c88f9SBruce Richardson
322473c88f9SBruce Richardson if (type == FME_ID) {
323473c88f9SBruce Richardson binfo->fiu = &binfo->hw->fme;
324473c88f9SBruce Richardson } else if (type == PORT_ID) {
325473c88f9SBruce Richardson binfo->fiu = &binfo->hw->port[index];
326473c88f9SBruce Richardson binfo->current_port_id = index;
327473c88f9SBruce Richardson }
328473c88f9SBruce Richardson
329473c88f9SBruce Richardson return 0;
330473c88f9SBruce Richardson }
331473c88f9SBruce Richardson
parse_feature_afus(struct build_feature_devs_info * binfo,struct feature_header * hdr)332673c897fSWei Huang static int parse_feature_afus(struct build_feature_devs_info *binfo,
333673c897fSWei Huang struct feature_header *hdr)
334673c897fSWei Huang {
335673c897fSWei Huang int ret;
336673c897fSWei Huang struct feature_afu_header *afu_hdr, header;
337673c897fSWei Huang u8 __iomem *start;
338673c897fSWei Huang u8 __iomem *end = binfo->ioend;
339673c897fSWei Huang
340673c897fSWei Huang ret = build_info_create_dev(binfo, AFU_ID, 0);
341673c897fSWei Huang if (ret)
342673c897fSWei Huang return ret;
343673c897fSWei Huang
344673c897fSWei Huang start = (u8 __iomem *)hdr;
345673c897fSWei Huang for (; start < end; start += header.next_afu) {
346673c897fSWei Huang if ((unsigned int)(end - start) <
347673c897fSWei Huang (unsigned int)(sizeof(*afu_hdr) + sizeof(*hdr)))
348673c897fSWei Huang return -EINVAL;
349673c897fSWei Huang
350673c897fSWei Huang hdr = (struct feature_header *)start;
351673c897fSWei Huang afu_hdr = (struct feature_afu_header *)(hdr + 1);
352673c897fSWei Huang header.csr = readq(&afu_hdr->csr);
353673c897fSWei Huang
354673c897fSWei Huang if (feature_is_UAFU(binfo)) {
355673c897fSWei Huang ret = parse_feature_uafu(binfo, hdr);
356673c897fSWei Huang if (ret)
357673c897fSWei Huang return ret;
358673c897fSWei Huang }
359673c897fSWei Huang
360673c897fSWei Huang if (!header.next_afu)
361673c897fSWei Huang break;
362673c897fSWei Huang }
363673c897fSWei Huang
364673c897fSWei Huang return 0;
365673c897fSWei Huang }
366673c897fSWei Huang
parse_feature_fme(struct build_feature_devs_info * binfo,struct feature_header * start)367473c88f9SBruce Richardson static int parse_feature_fme(struct build_feature_devs_info *binfo,
368473c88f9SBruce Richardson struct feature_header *start)
369473c88f9SBruce Richardson {
370473c88f9SBruce Richardson struct ifpga_hw *hw = binfo->hw;
371473c88f9SBruce Richardson struct ifpga_fme_hw *fme = &hw->fme;
372473c88f9SBruce Richardson int ret;
373473c88f9SBruce Richardson
374473c88f9SBruce Richardson ret = build_info_create_dev(binfo, FME_ID, 0);
375473c88f9SBruce Richardson if (ret)
376473c88f9SBruce Richardson return ret;
377473c88f9SBruce Richardson
378473c88f9SBruce Richardson /* Update FME states */
379473c88f9SBruce Richardson fme->state = IFPGA_FME_IMPLEMENTED;
380473c88f9SBruce Richardson fme->parent = hw;
381473c88f9SBruce Richardson TAILQ_INIT(&fme->feature_list);
382473c88f9SBruce Richardson spinlock_init(&fme->lock);
383473c88f9SBruce Richardson
384473c88f9SBruce Richardson return create_feature_instance(binfo, start, 0, 0, 0, 0);
385473c88f9SBruce Richardson }
386473c88f9SBruce Richardson
parse_feature_port(struct build_feature_devs_info * binfo,void __iomem * start)387473c88f9SBruce Richardson static int parse_feature_port(struct build_feature_devs_info *binfo,
388473c88f9SBruce Richardson void __iomem *start)
389473c88f9SBruce Richardson {
390473c88f9SBruce Richardson struct feature_port_header *port_hdr;
391473c88f9SBruce Richardson struct feature_port_capability capability;
392473c88f9SBruce Richardson struct ifpga_hw *hw = binfo->hw;
393473c88f9SBruce Richardson struct ifpga_port_hw *port;
394473c88f9SBruce Richardson unsigned int port_id;
395473c88f9SBruce Richardson int ret;
396473c88f9SBruce Richardson
397473c88f9SBruce Richardson /* Get current port's id */
398473c88f9SBruce Richardson port_hdr = (struct feature_port_header *)start;
399473c88f9SBruce Richardson capability.csr = readq(&port_hdr->capability);
400473c88f9SBruce Richardson port_id = capability.port_number;
401473c88f9SBruce Richardson
402473c88f9SBruce Richardson ret = build_info_create_dev(binfo, PORT_ID, port_id);
403473c88f9SBruce Richardson if (ret)
404473c88f9SBruce Richardson return ret;
405473c88f9SBruce Richardson
406473c88f9SBruce Richardson /*found a Port device*/
407473c88f9SBruce Richardson port = &hw->port[port_id];
408473c88f9SBruce Richardson port->port_id = binfo->current_port_id;
409473c88f9SBruce Richardson port->parent = hw;
410473c88f9SBruce Richardson port->state = IFPGA_PORT_ATTACHED;
411473c88f9SBruce Richardson spinlock_init(&port->lock);
412473c88f9SBruce Richardson TAILQ_INIT(&port->feature_list);
413473c88f9SBruce Richardson
414473c88f9SBruce Richardson return create_feature_instance(binfo, start, 0, 0, 0, 0);
415473c88f9SBruce Richardson }
416473c88f9SBruce Richardson
enable_port_uafu(struct build_feature_devs_info * binfo,void __iomem * start)417473c88f9SBruce Richardson static void enable_port_uafu(struct build_feature_devs_info *binfo,
418473c88f9SBruce Richardson void __iomem *start)
419473c88f9SBruce Richardson {
420473c88f9SBruce Richardson struct ifpga_port_hw *port = &binfo->hw->port[binfo->current_port_id];
421473c88f9SBruce Richardson
422473c88f9SBruce Richardson UNUSED(start);
423473c88f9SBruce Richardson
424473c88f9SBruce Richardson fpga_port_reset(port);
425473c88f9SBruce Richardson }
426473c88f9SBruce Richardson
parse_feature_fiu(struct build_feature_devs_info * binfo,struct feature_header * hdr)427473c88f9SBruce Richardson static int parse_feature_fiu(struct build_feature_devs_info *binfo,
428473c88f9SBruce Richardson struct feature_header *hdr)
429473c88f9SBruce Richardson {
430473c88f9SBruce Richardson struct feature_header header;
431473c88f9SBruce Richardson struct feature_fiu_header *fiu_hdr, fiu_header;
432473c88f9SBruce Richardson u8 __iomem *start = (u8 __iomem *)hdr;
433473c88f9SBruce Richardson int ret;
434473c88f9SBruce Richardson
435473c88f9SBruce Richardson header.csr = readq(hdr);
436473c88f9SBruce Richardson
437473c88f9SBruce Richardson switch (header.id) {
438473c88f9SBruce Richardson case FEATURE_FIU_ID_FME:
439473c88f9SBruce Richardson ret = parse_feature_fme(binfo, hdr);
440473c88f9SBruce Richardson binfo->pfme_hdr = hdr;
441473c88f9SBruce Richardson if (ret)
442473c88f9SBruce Richardson return ret;
443473c88f9SBruce Richardson break;
444473c88f9SBruce Richardson case FEATURE_FIU_ID_PORT:
445473c88f9SBruce Richardson ret = parse_feature_port(binfo, hdr);
446473c88f9SBruce Richardson enable_port_uafu(binfo, hdr);
447473c88f9SBruce Richardson if (ret)
448473c88f9SBruce Richardson return ret;
449473c88f9SBruce Richardson
450473c88f9SBruce Richardson /* Check Port FIU's next_afu pointer to User AFU DFH */
451473c88f9SBruce Richardson fiu_hdr = (struct feature_fiu_header *)(hdr + 1);
452473c88f9SBruce Richardson fiu_header.csr = readq(&fiu_hdr->csr);
453473c88f9SBruce Richardson
454473c88f9SBruce Richardson if (fiu_header.next_afu) {
455473c88f9SBruce Richardson start += fiu_header.next_afu;
456473c88f9SBruce Richardson ret = parse_feature_afus(binfo,
457473c88f9SBruce Richardson (struct feature_header *)start);
458473c88f9SBruce Richardson if (ret)
459473c88f9SBruce Richardson return ret;
460473c88f9SBruce Richardson } else {
461673c897fSWei Huang dev_info(binfo, "No AFU detected on Port\n");
462473c88f9SBruce Richardson }
463473c88f9SBruce Richardson
464473c88f9SBruce Richardson break;
465473c88f9SBruce Richardson default:
466473c88f9SBruce Richardson dev_info(binfo, "FIU TYPE %d is not supported yet.\n",
467473c88f9SBruce Richardson header.id);
468473c88f9SBruce Richardson }
469473c88f9SBruce Richardson
470473c88f9SBruce Richardson return 0;
471473c88f9SBruce Richardson }
472473c88f9SBruce Richardson
parse_feature_irqs(struct build_feature_devs_info * binfo,void __iomem * start,unsigned int * vec_start,unsigned int * vec_cnt)473473c88f9SBruce Richardson static void parse_feature_irqs(struct build_feature_devs_info *binfo,
474473c88f9SBruce Richardson void __iomem *start, unsigned int *vec_start,
475473c88f9SBruce Richardson unsigned int *vec_cnt)
476473c88f9SBruce Richardson {
477473c88f9SBruce Richardson UNUSED(binfo);
478473c88f9SBruce Richardson u64 id;
479473c88f9SBruce Richardson
480473c88f9SBruce Richardson id = feature_id(start);
481473c88f9SBruce Richardson
482673c897fSWei Huang if ((binfo->current_type == PORT_ID) && (id == PORT_FEATURE_ID_UINT)) {
483473c88f9SBruce Richardson struct feature_port_uint *port_uint = start;
484473c88f9SBruce Richardson struct feature_port_uint_cap uint_cap;
485473c88f9SBruce Richardson
486473c88f9SBruce Richardson uint_cap.csr = readq(&port_uint->capability);
487473c88f9SBruce Richardson if (uint_cap.intr_num) {
488473c88f9SBruce Richardson *vec_start = uint_cap.first_vec_num;
489473c88f9SBruce Richardson *vec_cnt = uint_cap.intr_num;
490473c88f9SBruce Richardson } else {
491473c88f9SBruce Richardson dev_debug(binfo, "UAFU doesn't support interrupt\n");
492473c88f9SBruce Richardson }
493673c897fSWei Huang } else if ((binfo->current_type == PORT_ID) &&
494673c897fSWei Huang (id == PORT_FEATURE_ID_ERROR)) {
495473c88f9SBruce Richardson struct feature_port_error *port_err = start;
496473c88f9SBruce Richardson struct feature_port_err_capability port_err_cap;
497473c88f9SBruce Richardson
498473c88f9SBruce Richardson port_err_cap.csr = readq(&port_err->error_capability);
499473c88f9SBruce Richardson if (port_err_cap.support_intr) {
500473c88f9SBruce Richardson *vec_start = port_err_cap.intr_vector_num;
501473c88f9SBruce Richardson *vec_cnt = 1;
502473c88f9SBruce Richardson } else {
503473c88f9SBruce Richardson dev_debug(&binfo, "Port error doesn't support interrupt\n");
504473c88f9SBruce Richardson }
505473c88f9SBruce Richardson
506673c897fSWei Huang } else if ((binfo->current_type == FME_ID) &&
507673c897fSWei Huang (id == FME_FEATURE_ID_GLOBAL_ERR)) {
508473c88f9SBruce Richardson struct feature_fme_err *fme_err = start;
509473c88f9SBruce Richardson struct feature_fme_error_capability fme_err_cap;
510473c88f9SBruce Richardson
511473c88f9SBruce Richardson fme_err_cap.csr = readq(&fme_err->fme_err_capability);
512473c88f9SBruce Richardson if (fme_err_cap.support_intr) {
513473c88f9SBruce Richardson *vec_start = fme_err_cap.intr_vector_num;
514473c88f9SBruce Richardson *vec_cnt = 1;
515473c88f9SBruce Richardson } else {
516473c88f9SBruce Richardson dev_debug(&binfo, "FME error doesn't support interrupt\n");
517473c88f9SBruce Richardson }
518473c88f9SBruce Richardson }
519473c88f9SBruce Richardson }
520473c88f9SBruce Richardson
parse_feature_fme_private(struct build_feature_devs_info * binfo,struct feature_header * hdr)521473c88f9SBruce Richardson static int parse_feature_fme_private(struct build_feature_devs_info *binfo,
522473c88f9SBruce Richardson struct feature_header *hdr)
523473c88f9SBruce Richardson {
524473c88f9SBruce Richardson unsigned int vec_start = 0;
525473c88f9SBruce Richardson unsigned int vec_cnt = 0;
526473c88f9SBruce Richardson
527473c88f9SBruce Richardson parse_feature_irqs(binfo, hdr, &vec_start, &vec_cnt);
528473c88f9SBruce Richardson
529473c88f9SBruce Richardson return create_feature_instance(binfo, hdr, 0, 0, vec_start, vec_cnt);
530473c88f9SBruce Richardson }
531473c88f9SBruce Richardson
parse_feature_port_private(struct build_feature_devs_info * binfo,struct feature_header * hdr)532473c88f9SBruce Richardson static int parse_feature_port_private(struct build_feature_devs_info *binfo,
533473c88f9SBruce Richardson struct feature_header *hdr)
534473c88f9SBruce Richardson {
535473c88f9SBruce Richardson unsigned int vec_start = 0;
536473c88f9SBruce Richardson unsigned int vec_cnt = 0;
537473c88f9SBruce Richardson
538473c88f9SBruce Richardson parse_feature_irqs(binfo, hdr, &vec_start, &vec_cnt);
539473c88f9SBruce Richardson
540473c88f9SBruce Richardson return create_feature_instance(binfo, hdr, 0, 0, vec_start, vec_cnt);
541473c88f9SBruce Richardson }
542473c88f9SBruce Richardson
parse_feature_private(struct build_feature_devs_info * binfo,struct feature_header * hdr)543473c88f9SBruce Richardson static int parse_feature_private(struct build_feature_devs_info *binfo,
544473c88f9SBruce Richardson struct feature_header *hdr)
545473c88f9SBruce Richardson {
546473c88f9SBruce Richardson struct feature_header header;
547473c88f9SBruce Richardson
548473c88f9SBruce Richardson header.csr = readq(hdr);
549473c88f9SBruce Richardson
550473c88f9SBruce Richardson switch (binfo->current_type) {
551473c88f9SBruce Richardson case FME_ID:
552473c88f9SBruce Richardson return parse_feature_fme_private(binfo, hdr);
553473c88f9SBruce Richardson case PORT_ID:
554473c88f9SBruce Richardson return parse_feature_port_private(binfo, hdr);
555673c897fSWei Huang case AFU_ID:
556673c897fSWei Huang dev_err(binfo, "private feature %x belonging to AFU "
557673c897fSWei Huang "is not supported yet.\n", header.id);
558673c897fSWei Huang break;
559473c88f9SBruce Richardson default:
560673c897fSWei Huang dev_err(binfo, "private feature %x belonging to TYPE %d "
561673c897fSWei Huang "(unknown_type) is not supported yet.\n",
562473c88f9SBruce Richardson header.id, binfo->current_type);
563673c897fSWei Huang break;
564473c88f9SBruce Richardson }
565473c88f9SBruce Richardson return 0;
566473c88f9SBruce Richardson }
567473c88f9SBruce Richardson
parse_feature(struct build_feature_devs_info * binfo,struct feature_header * hdr)568473c88f9SBruce Richardson static int parse_feature(struct build_feature_devs_info *binfo,
569473c88f9SBruce Richardson struct feature_header *hdr)
570473c88f9SBruce Richardson {
571473c88f9SBruce Richardson struct feature_header header;
572473c88f9SBruce Richardson int ret = 0;
573473c88f9SBruce Richardson
574473c88f9SBruce Richardson header.csr = readq(hdr);
575473c88f9SBruce Richardson
576473c88f9SBruce Richardson switch (header.type) {
577473c88f9SBruce Richardson case FEATURE_TYPE_AFU:
578473c88f9SBruce Richardson ret = parse_feature_afus(binfo, hdr);
579473c88f9SBruce Richardson break;
580473c88f9SBruce Richardson case FEATURE_TYPE_PRIVATE:
581473c88f9SBruce Richardson ret = parse_feature_private(binfo, hdr);
582473c88f9SBruce Richardson break;
583473c88f9SBruce Richardson case FEATURE_TYPE_FIU:
584473c88f9SBruce Richardson ret = parse_feature_fiu(binfo, hdr);
585473c88f9SBruce Richardson break;
586473c88f9SBruce Richardson default:
587473c88f9SBruce Richardson dev_err(binfo, "Feature Type %x is not supported.\n",
588473c88f9SBruce Richardson hdr->type);
589473c88f9SBruce Richardson };
590473c88f9SBruce Richardson
591473c88f9SBruce Richardson return ret;
592473c88f9SBruce Richardson }
593473c88f9SBruce Richardson
build_info_prepare(struct build_feature_devs_info * binfo,struct dfl_fpga_enum_dfl * dfl)594673c897fSWei Huang static int build_info_prepare(struct build_feature_devs_info *binfo,
595673c897fSWei Huang struct dfl_fpga_enum_dfl *dfl)
596473c88f9SBruce Richardson {
597673c897fSWei Huang if (!binfo || !dfl)
598673c897fSWei Huang return -EINVAL;
599673c897fSWei Huang
600673c897fSWei Huang binfo->ioaddr = dfl->addr;
601673c897fSWei Huang binfo->ioend = (u8 *)dfl->addr + dfl->len;
602673c897fSWei Huang binfo->phys_addr = dfl->start;
603673c897fSWei Huang
604673c897fSWei Huang return 0;
605673c897fSWei Huang }
606673c897fSWei Huang
parse_feature_list(struct build_feature_devs_info * binfo,struct dfl_fpga_enum_dfl * dfl)607673c897fSWei Huang static int parse_feature_list(struct build_feature_devs_info *binfo,
608673c897fSWei Huang struct dfl_fpga_enum_dfl *dfl)
609673c897fSWei Huang {
610673c897fSWei Huang u8 *start, *end;
611473c88f9SBruce Richardson struct feature_header *hdr, header;
612473c88f9SBruce Richardson int ret = 0;
613473c88f9SBruce Richardson
614673c897fSWei Huang ret = build_info_prepare(binfo, dfl);
615673c897fSWei Huang if (ret)
616673c897fSWei Huang return ret;
617673c897fSWei Huang
618673c897fSWei Huang start = (u8 *)binfo->ioaddr;
619673c897fSWei Huang end = (u8 *)binfo->ioend;
620673c897fSWei Huang
621673c897fSWei Huang /* walk through the device feature list via DFH's next DFH pointer. */
622473c88f9SBruce Richardson for (; start < end; start += header.next_header_offset) {
623473c88f9SBruce Richardson if ((unsigned int)(end - start) < (unsigned int)sizeof(*hdr)) {
624673c897fSWei Huang dev_err(binfo, "The region is too small to "
625673c897fSWei Huang "contain a feature.\n");
626473c88f9SBruce Richardson ret = -EINVAL;
627473c88f9SBruce Richardson break;
628473c88f9SBruce Richardson }
629473c88f9SBruce Richardson
630473c88f9SBruce Richardson hdr = (struct feature_header *)start;
631673c897fSWei Huang header.csr = opae_readq(hdr);
632473c88f9SBruce Richardson
633673c897fSWei Huang dev_debug(binfo, "%s: address=0x%p, val=0x%"PRIx64", "
634673c897fSWei Huang "header.id=0x%x, header.next_offset=0x%x, "
635673c897fSWei Huang "header.eol=0x%x, header.type=0x%x\n",
636673c897fSWei Huang __func__, hdr, header.csr, header.id,
637673c897fSWei Huang header.next_header_offset, header.end_of_list,
638673c897fSWei Huang header.type);
639473c88f9SBruce Richardson
640473c88f9SBruce Richardson ret = parse_feature(binfo, hdr);
641473c88f9SBruce Richardson if (ret)
642473c88f9SBruce Richardson return ret;
643473c88f9SBruce Richardson
644673c897fSWei Huang /* stop parsing if EOL(End of List) is set or offset is 0 */
645473c88f9SBruce Richardson if (header.end_of_list || !header.next_header_offset)
646473c88f9SBruce Richardson break;
647473c88f9SBruce Richardson }
648473c88f9SBruce Richardson
649473c88f9SBruce Richardson return build_info_commit_dev(binfo);
650473c88f9SBruce Richardson }
651473c88f9SBruce Richardson
build_info_free(struct build_feature_devs_info * binfo)652473c88f9SBruce Richardson static void build_info_free(struct build_feature_devs_info *binfo)
653473c88f9SBruce Richardson {
654673c897fSWei Huang opae_free(binfo);
655473c88f9SBruce Richardson }
656473c88f9SBruce Richardson
ifpga_print_device_feature_list(struct ifpga_hw * hw)657473c88f9SBruce Richardson static void ifpga_print_device_feature_list(struct ifpga_hw *hw)
658473c88f9SBruce Richardson {
659473c88f9SBruce Richardson struct ifpga_fme_hw *fme = &hw->fme;
660473c88f9SBruce Richardson struct ifpga_port_hw *port;
661473c88f9SBruce Richardson struct ifpga_feature *feature;
662473c88f9SBruce Richardson int i;
663473c88f9SBruce Richardson
664673c897fSWei Huang if (fme->state == IFPGA_FME_UNUSED) {
665673c897fSWei Huang dev_info(hw, "FME is not present\n");
666673c897fSWei Huang return;
667673c897fSWei Huang }
668673c897fSWei Huang
669473c88f9SBruce Richardson dev_info(hw, "found fme_device, is in PF: %s\n",
670473c88f9SBruce Richardson is_ifpga_hw_pf(hw) ? "yes" : "no");
671473c88f9SBruce Richardson
672473c88f9SBruce Richardson ifpga_for_each_fme_feature(fme, feature) {
673473c88f9SBruce Richardson if (feature->state != IFPGA_FEATURE_ATTACHED)
674473c88f9SBruce Richardson continue;
675473c88f9SBruce Richardson
676473c88f9SBruce Richardson dev_info(hw, "%12s: %p - %p - paddr: 0x%lx\n",
677473c88f9SBruce Richardson feature->name, feature->addr,
678473c88f9SBruce Richardson feature->addr + feature->size - 1,
679473c88f9SBruce Richardson (unsigned long)feature->phys_addr);
680473c88f9SBruce Richardson
681473c88f9SBruce Richardson }
682473c88f9SBruce Richardson
683473c88f9SBruce Richardson for (i = 0; i < MAX_FPGA_PORT_NUM; i++) {
684473c88f9SBruce Richardson port = &hw->port[i];
685473c88f9SBruce Richardson
686473c88f9SBruce Richardson if (port->state != IFPGA_PORT_ATTACHED)
687473c88f9SBruce Richardson continue;
688473c88f9SBruce Richardson
689473c88f9SBruce Richardson dev_info(hw, "port device: %d\n", port->port_id);
690473c88f9SBruce Richardson
691473c88f9SBruce Richardson ifpga_for_each_port_feature(port, feature) {
692473c88f9SBruce Richardson if (feature->state != IFPGA_FEATURE_ATTACHED)
693473c88f9SBruce Richardson continue;
694473c88f9SBruce Richardson
695473c88f9SBruce Richardson dev_info(hw, "%12s: %p - %p - paddr:0x%lx\n",
696473c88f9SBruce Richardson feature->name,
697473c88f9SBruce Richardson feature->addr,
698473c88f9SBruce Richardson feature->addr +
699473c88f9SBruce Richardson feature->size - 1,
700473c88f9SBruce Richardson (unsigned long)feature->phys_addr);
701473c88f9SBruce Richardson }
702473c88f9SBruce Richardson
703473c88f9SBruce Richardson }
704473c88f9SBruce Richardson }
705473c88f9SBruce Richardson
dfl_fpga_enum_info_alloc(struct ifpga_hw * hw)706673c897fSWei Huang static struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct ifpga_hw *hw)
707473c88f9SBruce Richardson {
708673c897fSWei Huang struct dfl_fpga_enum_info *info;
709673c897fSWei Huang
710673c897fSWei Huang info = opae_zmalloc(sizeof(*info));
711673c897fSWei Huang if (!info)
712673c897fSWei Huang return NULL;
713673c897fSWei Huang
714673c897fSWei Huang info->hw = hw;
715673c897fSWei Huang TAILQ_INIT(&info->dfls);
716673c897fSWei Huang
717673c897fSWei Huang return info;
718673c897fSWei Huang }
719673c897fSWei Huang
dfl_fpga_enum_info_free(struct dfl_fpga_enum_info * info)720673c897fSWei Huang static void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info)
721673c897fSWei Huang {
722673c897fSWei Huang struct dfl_fpga_enum_dfl *tmp, *dfl;
723673c897fSWei Huang
724673c897fSWei Huang if (!info)
725673c897fSWei Huang return;
726673c897fSWei Huang
727673c897fSWei Huang /* remove all device feature lists in the list. */
728673c897fSWei Huang for (dfl = TAILQ_FIRST(&info->dfls);
729673c897fSWei Huang dfl && (tmp = TAILQ_NEXT(dfl, node), 1);
730673c897fSWei Huang dfl = tmp) {
731673c897fSWei Huang TAILQ_REMOVE(&info->dfls, dfl, node);
732673c897fSWei Huang opae_free(dfl);
733673c897fSWei Huang }
734673c897fSWei Huang
735673c897fSWei Huang opae_free(info);
736673c897fSWei Huang }
737673c897fSWei Huang
dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info * info,u64 start,u64 len,void * addr)738673c897fSWei Huang static int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
739673c897fSWei Huang u64 start, u64 len, void *addr)
740673c897fSWei Huang {
741673c897fSWei Huang struct dfl_fpga_enum_dfl *dfl;
742673c897fSWei Huang
743673c897fSWei Huang dfl = opae_zmalloc(sizeof(*dfl));
744673c897fSWei Huang if (!dfl)
745673c897fSWei Huang return -ENOMEM;
746673c897fSWei Huang
747673c897fSWei Huang dfl->start = start;
748673c897fSWei Huang dfl->len = len;
749673c897fSWei Huang dfl->addr = addr;
750673c897fSWei Huang
751673c897fSWei Huang TAILQ_INSERT_TAIL(&info->dfls, dfl, node);
752673c897fSWei Huang
753673c897fSWei Huang return 0;
754673c897fSWei Huang }
755673c897fSWei Huang
756673c897fSWei Huang #define PCI_CFG_SPACE_SIZE 256
757673c897fSWei Huang #define PCI_CFG_SPACE_EXP_SIZE 4096
758673c897fSWei Huang #define PCI_EXT_CAP_ID(header) (header & 0x0000ffff)
759673c897fSWei Huang #define PCI_EXT_CAP_NEXT(header) ((header >> 20) & 0xffc)
760673c897fSWei Huang
761673c897fSWei Huang static int
pci_find_next_ecap(int fd,int start,u32 cap)762673c897fSWei Huang pci_find_next_ecap(int fd, int start, u32 cap)
763673c897fSWei Huang {
764673c897fSWei Huang u32 header;
765673c897fSWei Huang int ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
766673c897fSWei Huang int pos = PCI_CFG_SPACE_SIZE;
767473c88f9SBruce Richardson int ret;
768473c88f9SBruce Richardson
769673c897fSWei Huang if (start > 0)
770673c897fSWei Huang pos = start;
771673c897fSWei Huang
772673c897fSWei Huang ret = pread(fd, &header, sizeof(header), pos);
773673c897fSWei Huang if (ret < 0)
774673c897fSWei Huang return ret;
775673c897fSWei Huang
776673c897fSWei Huang /*
777673c897fSWei Huang * If we have no capabilities, this is indicated by cap ID,
778673c897fSWei Huang * cap version and next pointer all being 0.
779673c897fSWei Huang */
780673c897fSWei Huang if (header == 0)
781673c897fSWei Huang return 0;
782673c897fSWei Huang
783673c897fSWei Huang while (ttl-- > 0) {
784673c897fSWei Huang if ((PCI_EXT_CAP_ID(header) == cap) && (pos != start))
785673c897fSWei Huang return pos;
786673c897fSWei Huang
787673c897fSWei Huang pos = PCI_EXT_CAP_NEXT(header);
788673c897fSWei Huang if (pos < PCI_CFG_SPACE_SIZE)
789673c897fSWei Huang break;
790673c897fSWei Huang ret = pread(fd, &header, sizeof(header), pos);
791673c897fSWei Huang if (ret < 0)
792673c897fSWei Huang return ret;
793673c897fSWei Huang }
794673c897fSWei Huang
795673c897fSWei Huang return 0;
796673c897fSWei Huang }
797673c897fSWei Huang
798673c897fSWei Huang #define PCI_EXT_CAP_ID_VNDR 0x0B
799673c897fSWei Huang #define PCI_VNDR_HEADER 4
800673c897fSWei Huang #define PCI_VNDR_HEADER_ID(x) ((x) & 0xffff)
801673c897fSWei Huang #define PCI_VENDOR_ID_INTEL 0x8086
802673c897fSWei Huang #define PCI_VSEC_ID_INTEL_DFLS 0x43
803673c897fSWei Huang #define PCI_VNDR_DFLS_CNT 0x8
804673c897fSWei Huang #define PCI_VNDR_DFLS_RES 0xc
805673c897fSWei Huang #define PCI_VNDR_DFLS_RES_BAR_MASK GENMASK(2, 0)
806673c897fSWei Huang #define PCI_VNDR_DFLS_RES_OFF_MASK GENMASK(31, 3)
807673c897fSWei Huang
find_dfls_by_vsec(struct dfl_fpga_enum_info * info)808673c897fSWei Huang static int find_dfls_by_vsec(struct dfl_fpga_enum_info *info)
809673c897fSWei Huang {
810673c897fSWei Huang struct ifpga_hw *hw;
811673c897fSWei Huang struct opae_adapter_data_pci *pci_data;
812673c897fSWei Huang char path[64];
813673c897fSWei Huang u32 bir, offset, vndr_hdr, i, dfl_cnt, dfl_res;
814673c897fSWei Huang int fd, ret, dfl_res_off, voff = 0;
815673c897fSWei Huang u64 start, len;
816673c897fSWei Huang void *addr;
817673c897fSWei Huang
818673c897fSWei Huang if (!info || !info->hw)
819673c897fSWei Huang return -EINVAL;
820673c897fSWei Huang hw = info->hw;
821673c897fSWei Huang
822673c897fSWei Huang if (!hw->adapter || !hw->pci_data)
823673c897fSWei Huang return -EINVAL;
824673c897fSWei Huang pci_data = hw->pci_data;
825673c897fSWei Huang
826673c897fSWei Huang ret = snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/config",
827673c897fSWei Huang hw->adapter->name);
828673c897fSWei Huang if ((unsigned int)ret >= sizeof(path))
829673c897fSWei Huang return -EINVAL;
830673c897fSWei Huang
831673c897fSWei Huang fd = open(path, O_RDWR);
832673c897fSWei Huang if (fd < 0)
833673c897fSWei Huang return -EIO;
834673c897fSWei Huang
835673c897fSWei Huang while ((voff = pci_find_next_ecap(fd, voff,
836673c897fSWei Huang PCI_EXT_CAP_ID_VNDR))) {
837673c897fSWei Huang vndr_hdr = 0;
838673c897fSWei Huang ret = pread(fd, &vndr_hdr, sizeof(vndr_hdr),
839673c897fSWei Huang voff + PCI_VNDR_HEADER);
840*e53ed84aSWei Huang if (ret < 0) {
841*e53ed84aSWei Huang ret = -EIO;
842*e53ed84aSWei Huang goto free_handle;
843*e53ed84aSWei Huang }
844673c897fSWei Huang if (PCI_VNDR_HEADER_ID(vndr_hdr) == PCI_VSEC_ID_INTEL_DFLS &&
845673c897fSWei Huang pci_data->vendor_id == PCI_VENDOR_ID_INTEL)
846673c897fSWei Huang break;
847673c897fSWei Huang }
848673c897fSWei Huang
849673c897fSWei Huang if (!voff) {
850673c897fSWei Huang dev_debug(hw, "%s no DFL VSEC found\n", __func__);
851*e53ed84aSWei Huang ret = -ENODEV;
852*e53ed84aSWei Huang goto free_handle;
853673c897fSWei Huang }
854673c897fSWei Huang
855673c897fSWei Huang dfl_cnt = 0;
856673c897fSWei Huang ret = pread(fd, &dfl_cnt, sizeof(dfl_cnt), voff + PCI_VNDR_DFLS_CNT);
857*e53ed84aSWei Huang if (ret < 0) {
858*e53ed84aSWei Huang ret = -EIO;
859*e53ed84aSWei Huang goto free_handle;
860*e53ed84aSWei Huang }
861673c897fSWei Huang
862673c897fSWei Huang dfl_res_off = voff + PCI_VNDR_DFLS_RES;
863673c897fSWei Huang if (dfl_res_off + (dfl_cnt * sizeof(u32)) > PCI_CFG_SPACE_EXP_SIZE) {
864673c897fSWei Huang dev_err(hw, "%s DFL VSEC too big for PCIe config space\n",
865673c897fSWei Huang __func__);
866*e53ed84aSWei Huang ret = -EINVAL;
867*e53ed84aSWei Huang goto free_handle;
868673c897fSWei Huang }
869673c897fSWei Huang
870673c897fSWei Huang for (i = 0; i < dfl_cnt; i++, dfl_res_off += sizeof(u32)) {
871673c897fSWei Huang dfl_res = GENMASK(31, 0);
872673c897fSWei Huang ret = pread(fd, &dfl_res, sizeof(dfl_res), dfl_res_off);
873673c897fSWei Huang bir = dfl_res & PCI_VNDR_DFLS_RES_BAR_MASK;
874673c897fSWei Huang if (bir >= PCI_MAX_RESOURCE) {
875673c897fSWei Huang dev_err(hw, "%s bad bir number %d\n",
876673c897fSWei Huang __func__, bir);
877*e53ed84aSWei Huang ret = -EINVAL;
878*e53ed84aSWei Huang goto free_handle;
879673c897fSWei Huang }
880673c897fSWei Huang
881673c897fSWei Huang len = pci_data->region[bir].len;
882673c897fSWei Huang offset = dfl_res & PCI_VNDR_DFLS_RES_OFF_MASK;
883673c897fSWei Huang if (offset >= len) {
884673c897fSWei Huang dev_err(hw, "%s bad offset %u >= %"PRIu64"\n",
885673c897fSWei Huang __func__, offset, len);
886*e53ed84aSWei Huang ret = -EINVAL;
887*e53ed84aSWei Huang goto free_handle;
888673c897fSWei Huang }
889673c897fSWei Huang
890673c897fSWei Huang dev_debug(hw, "%s BAR %d offset 0x%x\n", __func__, bir, offset);
891673c897fSWei Huang len -= offset;
892673c897fSWei Huang start = pci_data->region[bir].phys_addr + offset;
893673c897fSWei Huang addr = pci_data->region[bir].addr + offset;
894673c897fSWei Huang dfl_fpga_enum_info_add_dfl(info, start, len, addr);
895673c897fSWei Huang }
896673c897fSWei Huang
897*e53ed84aSWei Huang free_handle:
898*e53ed84aSWei Huang close(fd);
899*e53ed84aSWei Huang return ret;
900673c897fSWei Huang }
901673c897fSWei Huang
902673c897fSWei Huang /* default method of finding dfls starting at offset 0 of bar 0 */
903673c897fSWei Huang static int
find_dfls_by_default(struct dfl_fpga_enum_info * info)904673c897fSWei Huang find_dfls_by_default(struct dfl_fpga_enum_info *info)
905673c897fSWei Huang {
906673c897fSWei Huang struct ifpga_hw *hw;
907673c897fSWei Huang struct opae_adapter_data_pci *pci_data;
908673c897fSWei Huang int port_num, bar, i, ret = 0;
909673c897fSWei Huang u64 start, len;
910673c897fSWei Huang void *addr;
911673c897fSWei Huang u32 offset;
912673c897fSWei Huang struct feature_header hdr;
913673c897fSWei Huang struct feature_fme_capability cap;
914673c897fSWei Huang struct feature_fme_port port;
915673c897fSWei Huang struct feature_fme_header *fme_hdr;
916673c897fSWei Huang
917673c897fSWei Huang if (!info || !info->hw)
918673c897fSWei Huang return -EINVAL;
919673c897fSWei Huang hw = info->hw;
920673c897fSWei Huang
921673c897fSWei Huang if (!hw->pci_data)
922673c897fSWei Huang return -EINVAL;
923673c897fSWei Huang pci_data = hw->pci_data;
924673c897fSWei Huang
925673c897fSWei Huang /* start to find Device Feature List from Bar 0 */
926673c897fSWei Huang addr = pci_data->region[0].addr;
927673c897fSWei Huang if (!addr)
928673c897fSWei Huang return -ENOMEM;
929673c897fSWei Huang
930673c897fSWei Huang /*
931673c897fSWei Huang * PF device has FME and Ports/AFUs, and VF device only has one
932673c897fSWei Huang * Port/AFU. Check them and add related "Device Feature List" info
933673c897fSWei Huang * for the next step enumeration.
934673c897fSWei Huang */
935673c897fSWei Huang hdr.csr = opae_readq(addr);
936673c897fSWei Huang if ((hdr.type == FEATURE_TYPE_FIU) && (hdr.id == FEATURE_FIU_ID_FME)) {
937673c897fSWei Huang start = pci_data->region[0].phys_addr;
938673c897fSWei Huang len = pci_data->region[0].len;
939673c897fSWei Huang addr = pci_data->region[0].addr;
940673c897fSWei Huang
941673c897fSWei Huang dfl_fpga_enum_info_add_dfl(info, start, len, addr);
942673c897fSWei Huang
943673c897fSWei Huang /*
944673c897fSWei Huang * find more Device Feature Lists (e.g. Ports) per information
945673c897fSWei Huang * indicated by FME module.
946673c897fSWei Huang */
947673c897fSWei Huang fme_hdr = (struct feature_fme_header *)addr;
948673c897fSWei Huang cap.csr = opae_readq(&fme_hdr->capability);
949673c897fSWei Huang port_num = (int)cap.num_ports;
950673c897fSWei Huang
951673c897fSWei Huang dev_info(hw, "port_num = %d\n", port_num);
952673c897fSWei Huang if (port_num > MAX_FPGA_PORT_NUM)
953673c897fSWei Huang port_num = MAX_FPGA_PORT_NUM;
954673c897fSWei Huang
955673c897fSWei Huang for (i = 0; i < port_num; i++) {
956673c897fSWei Huang port.csr = opae_readq(&fme_hdr->port[i]);
957673c897fSWei Huang
958673c897fSWei Huang /* skip ports which are not implemented. */
959673c897fSWei Huang if (!port.port_implemented)
960673c897fSWei Huang continue;
961673c897fSWei Huang
962673c897fSWei Huang /* skip port which only could be accessed via VF */
963673c897fSWei Huang if (port.afu_access_control == FME_AFU_ACCESS_VF)
964673c897fSWei Huang continue;
965673c897fSWei Huang
966673c897fSWei Huang /*
967673c897fSWei Huang * add Port's Device Feature List information for next
968673c897fSWei Huang * step enumeration.
969673c897fSWei Huang */
970673c897fSWei Huang bar = (int)port.port_bar;
971673c897fSWei Huang offset = port.port_offset;
972673c897fSWei Huang if (bar == FME_PORT_OFST_BAR_SKIP) {
973673c897fSWei Huang continue;
974673c897fSWei Huang } else if (bar >= PCI_MAX_RESOURCE) {
975673c897fSWei Huang dev_err(hw, "bad BAR %d for port %d\n", bar, i);
976673c897fSWei Huang ret = -EINVAL;
977673c897fSWei Huang break;
978673c897fSWei Huang }
979673c897fSWei Huang dev_info(hw, "BAR %d offset %u\n", bar, offset);
980673c897fSWei Huang
981673c897fSWei Huang len = pci_data->region[bar].len;
982673c897fSWei Huang if (offset >= len) {
983673c897fSWei Huang dev_warn(hw, "bad port offset %u >= %pa\n",
984673c897fSWei Huang offset, &len);
985673c897fSWei Huang continue;
986673c897fSWei Huang }
987673c897fSWei Huang
988673c897fSWei Huang len -= offset;
989673c897fSWei Huang start = pci_data->region[bar].phys_addr + offset;
990673c897fSWei Huang addr = pci_data->region[bar].addr + offset;
991673c897fSWei Huang dfl_fpga_enum_info_add_dfl(info, start, len, addr);
992673c897fSWei Huang }
993673c897fSWei Huang } else if ((hdr.type == FEATURE_TYPE_FIU) &&
994673c897fSWei Huang (hdr.id == FEATURE_FIU_ID_PORT)) {
995673c897fSWei Huang start = pci_data->region[0].phys_addr;
996673c897fSWei Huang len = pci_data->region[0].len;
997673c897fSWei Huang addr = pci_data->region[0].addr;
998673c897fSWei Huang
999673c897fSWei Huang dfl_fpga_enum_info_add_dfl(info, start, len, addr);
1000673c897fSWei Huang } else if (hdr.type == FEATURE_TYPE_AFU) {
1001673c897fSWei Huang start = pci_data->region[0].phys_addr;
1002673c897fSWei Huang len = pci_data->region[0].len;
1003673c897fSWei Huang addr = pci_data->region[0].addr;
1004673c897fSWei Huang
1005673c897fSWei Huang dfl_fpga_enum_info_add_dfl(info, start, len, addr);
1006673c897fSWei Huang } else {
1007673c897fSWei Huang dev_info(hw, "Unknown feature type 0x%x id 0x%x\n",
1008673c897fSWei Huang hdr.type, hdr.id);
1009673c897fSWei Huang ret = -ENODEV;
1010673c897fSWei Huang }
1011673c897fSWei Huang
1012673c897fSWei Huang return ret;
1013673c897fSWei Huang }
1014673c897fSWei Huang
dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info * info)1015673c897fSWei Huang static int dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
1016673c897fSWei Huang {
1017673c897fSWei Huang struct build_feature_devs_info *binfo;
1018673c897fSWei Huang struct dfl_fpga_enum_dfl *dfl;
1019673c897fSWei Huang int ret = 0;
1020673c897fSWei Huang
1021673c897fSWei Huang if (!info || !info->hw)
1022673c897fSWei Huang return -EINVAL;
1023673c897fSWei Huang
1024673c897fSWei Huang /* create and init build info for enumeration */
1025673c897fSWei Huang binfo = opae_zmalloc(sizeof(*binfo));
1026473c88f9SBruce Richardson if (!binfo)
1027473c88f9SBruce Richardson return -ENOMEM;
1028473c88f9SBruce Richardson
1029673c897fSWei Huang binfo->hw = info->hw;
1030673c897fSWei Huang binfo->pci_data = info->hw->pci_data;
1031673c897fSWei Huang
1032673c897fSWei Huang /*
1033673c897fSWei Huang * start enumeration for all feature devices based on Device Feature
1034673c897fSWei Huang * Lists.
1035673c897fSWei Huang */
1036673c897fSWei Huang TAILQ_FOREACH(dfl, &info->dfls, node) {
1037673c897fSWei Huang ret = parse_feature_list(binfo, dfl);
1038673c897fSWei Huang if (ret)
1039673c897fSWei Huang break;
1040673c897fSWei Huang }
1041673c897fSWei Huang
1042673c897fSWei Huang build_info_free(binfo);
1043673c897fSWei Huang
1044673c897fSWei Huang return ret;
1045673c897fSWei Huang }
1046673c897fSWei Huang
ifpga_bus_enumerate(struct ifpga_hw * hw)1047673c897fSWei Huang int ifpga_bus_enumerate(struct ifpga_hw *hw)
1048673c897fSWei Huang {
1049673c897fSWei Huang struct dfl_fpga_enum_info *info;
1050673c897fSWei Huang int ret;
1051673c897fSWei Huang
1052673c897fSWei Huang /* allocate enumeration info */
1053673c897fSWei Huang info = dfl_fpga_enum_info_alloc(hw);
1054673c897fSWei Huang if (!info)
1055673c897fSWei Huang return -ENOMEM;
1056673c897fSWei Huang
1057673c897fSWei Huang ret = find_dfls_by_vsec(info);
1058673c897fSWei Huang if (ret < 0)
1059673c897fSWei Huang ret = find_dfls_by_default(info);
1060673c897fSWei Huang
1061473c88f9SBruce Richardson if (ret)
1062473c88f9SBruce Richardson goto exit;
1063473c88f9SBruce Richardson
1064673c897fSWei Huang /* start enumeration with prepared enumeration information */
1065673c897fSWei Huang ret = dfl_fpga_feature_devs_enumerate(info);
1066673c897fSWei Huang if (ret < 0) {
1067673c897fSWei Huang dev_err(hw, "Enumeration failure\n");
1068473c88f9SBruce Richardson goto exit;
1069673c897fSWei Huang }
1070473c88f9SBruce Richardson
1071473c88f9SBruce Richardson ifpga_print_device_feature_list(hw);
1072473c88f9SBruce Richardson
1073473c88f9SBruce Richardson exit:
1074673c897fSWei Huang dfl_fpga_enum_info_free(info);
1075673c897fSWei Huang
1076473c88f9SBruce Richardson return ret;
1077473c88f9SBruce Richardson }
1078473c88f9SBruce Richardson
ifpga_print_acc_list(struct opae_adapter * adapter)1079673c897fSWei Huang static void ifpga_print_acc_list(struct opae_adapter *adapter)
1080673c897fSWei Huang {
1081673c897fSWei Huang struct opae_accelerator *acc;
1082673c897fSWei Huang struct ifpga_afu_info *info;
1083673c897fSWei Huang struct uuid guid;
1084673c897fSWei Huang char buf[48];
1085673c897fSWei Huang int i;
1086673c897fSWei Huang
1087673c897fSWei Huang opae_adapter_for_each_acc(adapter, acc) {
1088673c897fSWei Huang info = acc->data;
1089673c897fSWei Huang if (!info)
1090673c897fSWei Huang continue;
1091673c897fSWei Huang acc->ops->get_uuid(acc, &guid);
1092673c897fSWei Huang i = sprintf(buf, "%02x%02x%02x%02x-",
1093673c897fSWei Huang guid.b[15], guid.b[14], guid.b[13], guid.b[12]);
1094673c897fSWei Huang i += sprintf(buf+i, "%02x%02x-", guid.b[11], guid.b[10]);
1095673c897fSWei Huang i += sprintf(buf+i, "%02x%02x-", guid.b[9], guid.b[8]);
1096673c897fSWei Huang i += sprintf(buf+i, "%02x%02x-", guid.b[7], guid.b[6]);
1097673c897fSWei Huang sprintf(buf+i, "%02x%02x%02x%02x%02x%02x",
1098673c897fSWei Huang guid.b[5], guid.b[4], guid.b[3],
1099673c897fSWei Huang guid.b[2], guid.b[1], guid.b[0]);
1100673c897fSWei Huang dev_info(hw, "AFU(%s-%d)@%p: len:0x%"PRIx64", guid:%s\n",
1101673c897fSWei Huang acc->name, acc->index, info->region[0].addr,
1102673c897fSWei Huang info->region[0].len, buf);
1103673c897fSWei Huang }
1104673c897fSWei Huang }
1105673c897fSWei Huang
ifpga_bus_init(struct ifpga_hw * hw)1106473c88f9SBruce Richardson int ifpga_bus_init(struct ifpga_hw *hw)
1107473c88f9SBruce Richardson {
1108673c897fSWei Huang int i, ret = 0;
1109473c88f9SBruce Richardson struct ifpga_port_hw *port;
1110473c88f9SBruce Richardson
1111673c897fSWei Huang ret = fme_hw_init(&hw->fme);
1112673c897fSWei Huang if (ret)
1113673c897fSWei Huang return ret;
1114673c897fSWei Huang
1115473c88f9SBruce Richardson for (i = 0; i < MAX_FPGA_PORT_NUM; i++) {
1116473c88f9SBruce Richardson port = &hw->port[i];
1117473c88f9SBruce Richardson port_hw_init(port);
1118473c88f9SBruce Richardson }
1119673c897fSWei Huang ifpga_print_acc_list(hw->adapter);
1120473c88f9SBruce Richardson
1121473c88f9SBruce Richardson return 0;
1122473c88f9SBruce Richardson }
112382255e03SWei Huang
ifpga_bus_uinit(struct ifpga_hw * hw)112482255e03SWei Huang int ifpga_bus_uinit(struct ifpga_hw *hw)
112582255e03SWei Huang {
112682255e03SWei Huang int i;
112782255e03SWei Huang struct ifpga_port_hw *port;
112882255e03SWei Huang
112982255e03SWei Huang if (hw) {
113082255e03SWei Huang fme_hw_uinit(&hw->fme);
113182255e03SWei Huang for (i = 0; i < MAX_FPGA_PORT_NUM; i++) {
113282255e03SWei Huang port = &hw->port[i];
113382255e03SWei Huang port_hw_uinit(port);
113482255e03SWei Huang }
113582255e03SWei Huang }
113682255e03SWei Huang
113782255e03SWei Huang return 0;
113882255e03SWei Huang }
1139