blob: 702bd2693689acc99ab59202d2b1ea2222cf16ac [file] [log] [blame]
Jan Glaubera755a452012-11-29 12:55:21 +01001/*
2 * Copyright IBM Corp. 2012
3 *
4 * Author(s):
5 * Jan Glauber <jang@linux.vnet.ibm.com>
6 */
7
8#define COMPONENT "zPCI"
9#define pr_fmt(fmt) COMPONENT ": " fmt
10
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/err.h>
14#include <linux/delay.h>
15#include <linux/pci.h>
16#include <asm/pci_clp.h>
17
18/*
19 * Call Logical Processor
20 * Retry logic is handled by the caller.
21 */
Sebastian Ottbf4ec242013-01-31 19:53:12 +010022static inline u8 clp_instr(void *data)
Jan Glaubera755a452012-11-29 12:55:21 +010023{
Sebastian Ottbf4ec242013-01-31 19:53:12 +010024 struct { u8 _[CLP_BLK_SIZE]; } *req = data;
25 u64 ignored;
Jan Glaubera755a452012-11-29 12:55:21 +010026 u8 cc;
27
28 asm volatile (
Sebastian Ottbf4ec242013-01-31 19:53:12 +010029 " .insn rrf,0xb9a00000,%[ign],%[req],0x0,0x2\n"
Jan Glaubera755a452012-11-29 12:55:21 +010030 " ipm %[cc]\n"
31 " srl %[cc],28\n"
Sebastian Ottbf4ec242013-01-31 19:53:12 +010032 : [cc] "=d" (cc), [ign] "=d" (ignored), "+m" (*req)
Jan Glaubera755a452012-11-29 12:55:21 +010033 : [req] "a" (req)
Sebastian Ottbf4ec242013-01-31 19:53:12 +010034 : "cc");
Jan Glaubera755a452012-11-29 12:55:21 +010035 return cc;
36}
37
38static void *clp_alloc_block(void)
39{
40 struct page *page = alloc_pages(GFP_KERNEL, get_order(CLP_BLK_SIZE));
41 return (page) ? page_address(page) : NULL;
42}
43
44static void clp_free_block(void *ptr)
45{
46 free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
47}
48
49static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
50 struct clp_rsp_query_pci_grp *response)
51{
Jan Glauber828b35f2012-11-29 14:33:30 +010052 zdev->tlb_refresh = response->refresh;
53 zdev->dma_mask = response->dasm;
Jan Glauber9a4da8a2012-11-29 13:05:05 +010054 zdev->msi_addr = response->msia;
Jan Glauberd0b08852012-12-11 14:53:35 +010055 zdev->fmb_update = response->mui;
Jan Glauber9a4da8a2012-11-29 13:05:05 +010056
57 pr_debug("Supported number of MSI vectors: %u\n", response->noi);
Jan Glaubera755a452012-11-29 12:55:21 +010058 switch (response->version) {
59 case 1:
60 zdev->max_bus_speed = PCIE_SPEED_5_0GT;
61 break;
62 default:
63 zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
64 break;
65 }
66}
67
68static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
69{
70 struct clp_req_rsp_query_pci_grp *rrb;
71 int rc;
72
73 rrb = clp_alloc_block();
74 if (!rrb)
75 return -ENOMEM;
76
77 memset(rrb, 0, sizeof(*rrb));
78 rrb->request.hdr.len = sizeof(rrb->request);
79 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
80 rrb->response.hdr.len = sizeof(rrb->response);
81 rrb->request.pfgid = pfgid;
82
83 rc = clp_instr(rrb);
84 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
85 clp_store_query_pci_fngrp(zdev, &rrb->response);
86 else {
87 pr_err("Query PCI FNGRP failed with response: %x cc: %d\n",
88 rrb->response.hdr.rsp, rc);
89 rc = -EIO;
90 }
91 clp_free_block(rrb);
92 return rc;
93}
94
95static int clp_store_query_pci_fn(struct zpci_dev *zdev,
96 struct clp_rsp_query_pci *response)
97{
98 int i;
99
100 for (i = 0; i < PCI_BAR_COUNT; i++) {
101 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
102 zdev->bars[i].size = response->bar_size[i];
103 }
Jan Glauber828b35f2012-11-29 14:33:30 +0100104 zdev->start_dma = response->sdma;
105 zdev->end_dma = response->edma;
Jan Glaubera755a452012-11-29 12:55:21 +0100106 zdev->pchid = response->pchid;
107 zdev->pfgid = response->pfgid;
108 return 0;
109}
110
111static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
112{
113 struct clp_req_rsp_query_pci *rrb;
114 int rc;
115
116 rrb = clp_alloc_block();
117 if (!rrb)
118 return -ENOMEM;
119
120 memset(rrb, 0, sizeof(*rrb));
121 rrb->request.hdr.len = sizeof(rrb->request);
122 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
123 rrb->response.hdr.len = sizeof(rrb->response);
124 rrb->request.fh = fh;
125
126 rc = clp_instr(rrb);
127 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
128 rc = clp_store_query_pci_fn(zdev, &rrb->response);
129 if (rc)
130 goto out;
131 if (rrb->response.pfgid)
132 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
133 } else {
134 pr_err("Query PCI failed with response: %x cc: %d\n",
135 rrb->response.hdr.rsp, rc);
136 rc = -EIO;
137 }
138out:
139 clp_free_block(rrb);
140 return rc;
141}
142
143int clp_add_pci_device(u32 fid, u32 fh, int configured)
144{
145 struct zpci_dev *zdev;
146 int rc;
147
148 zdev = zpci_alloc_device();
149 if (IS_ERR(zdev))
150 return PTR_ERR(zdev);
151
152 zdev->fh = fh;
153 zdev->fid = fid;
154
155 /* Query function properties and update zdev */
156 rc = clp_query_pci_fn(zdev, fh);
157 if (rc)
158 goto error;
159
160 if (configured)
161 zdev->state = ZPCI_FN_STATE_CONFIGURED;
162 else
163 zdev->state = ZPCI_FN_STATE_STANDBY;
164
165 rc = zpci_create_device(zdev);
166 if (rc)
167 goto error;
168 return 0;
169
170error:
171 zpci_free_device(zdev);
172 return rc;
173}
174
175/*
176 * Enable/Disable a given PCI function defined by its function handle.
177 */
178static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
179{
180 struct clp_req_rsp_set_pci *rrb;
181 int rc, retries = 1000;
182
183 rrb = clp_alloc_block();
184 if (!rrb)
185 return -ENOMEM;
186
187 do {
188 memset(rrb, 0, sizeof(*rrb));
189 rrb->request.hdr.len = sizeof(rrb->request);
190 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
191 rrb->response.hdr.len = sizeof(rrb->response);
192 rrb->request.fh = *fh;
193 rrb->request.oc = command;
194 rrb->request.ndas = nr_dma_as;
195
196 rc = clp_instr(rrb);
197 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
198 retries--;
199 if (retries < 0)
200 break;
201 msleep(1);
202 }
203 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
204
205 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
206 *fh = rrb->response.fh;
207 else {
208 pr_err("Set PCI FN failed with response: %x cc: %d\n",
209 rrb->response.hdr.rsp, rc);
210 rc = -EIO;
211 }
212 clp_free_block(rrb);
213 return rc;
214}
215
216int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
217{
218 u32 fh = zdev->fh;
219 int rc;
220
221 rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
222 if (!rc)
223 /* Success -> store enabled handle in zdev */
224 zdev->fh = fh;
225 return rc;
226}
227
228int clp_disable_fh(struct zpci_dev *zdev)
229{
230 u32 fh = zdev->fh;
231 int rc;
232
233 if (!zdev_enabled(zdev))
234 return 0;
235
236 dev_info(&zdev->pdev->dev, "disabling fn handle: 0x%x\n", fh);
237 rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
238 if (!rc)
239 /* Success -> store disabled handle in zdev */
240 zdev->fh = fh;
241 else
242 dev_err(&zdev->pdev->dev,
243 "Failed to disable fn handle: 0x%x\n", fh);
244 return rc;
245}
246
247static void clp_check_pcifn_entry(struct clp_fh_list_entry *entry)
248{
249 int present, rc;
250
251 if (!entry->vendor_id)
252 return;
253
254 /* TODO: be a little bit more scalable */
255 present = zpci_fid_present(entry->fid);
256
257 if (present)
258 pr_debug("%s: device %x already present\n", __func__, entry->fid);
259
260 /* skip already used functions */
261 if (present && entry->config_state)
262 return;
263
264 /* aev 306: function moved to stand-by state */
265 if (present && !entry->config_state) {
266 /*
267 * The handle is already disabled, that means no iota/irq freeing via
268 * the firmware interfaces anymore. Need to free resources manually
269 * (DMA memory, debug, sysfs)...
270 */
271 zpci_stop_device(get_zdev_by_fid(entry->fid));
272 return;
273 }
274
275 rc = clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
276 if (rc)
277 pr_err("Failed to add fid: 0x%x\n", entry->fid);
278}
279
280int clp_find_pci_devices(void)
281{
282 struct clp_req_rsp_list_pci *rrb;
283 u64 resume_token = 0;
284 int entries, i, rc;
285
286 rrb = clp_alloc_block();
287 if (!rrb)
288 return -ENOMEM;
289
290 do {
291 memset(rrb, 0, sizeof(*rrb));
292 rrb->request.hdr.len = sizeof(rrb->request);
293 rrb->request.hdr.cmd = CLP_LIST_PCI;
294 /* store as many entries as possible */
295 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
296 rrb->request.resume_token = resume_token;
297
298 /* Get PCI function handle list */
299 rc = clp_instr(rrb);
300 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
301 pr_err("List PCI failed with response: 0x%x cc: %d\n",
302 rrb->response.hdr.rsp, rc);
303 rc = -EIO;
304 goto out;
305 }
306
307 WARN_ON_ONCE(rrb->response.entry_size !=
308 sizeof(struct clp_fh_list_entry));
309
310 entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
311 rrb->response.entry_size;
312 pr_info("Detected number of PCI functions: %u\n", entries);
313
314 /* Store the returned resume token as input for the next call */
315 resume_token = rrb->response.resume_token;
316
317 for (i = 0; i < entries; i++)
318 clp_check_pcifn_entry(&rrb->response.fh_list[i]);
319 } while (resume_token);
320
321 pr_debug("Maximum number of supported PCI functions: %u\n",
322 rrb->response.max_fn);
323out:
324 clp_free_block(rrb);
325 return rc;
326}