blob: 475563c3d1e40d401417c6503946f1933457980d [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>
Sebastian Otta2ab8332013-04-16 14:11:14 +020016#include <asm/pci_debug.h>
Jan Glaubera755a452012-11-29 12:55:21 +010017#include <asm/pci_clp.h>
18
19/*
20 * Call Logical Processor
21 * Retry logic is handled by the caller.
22 */
Sebastian Ottbf4ec242013-01-31 19:53:12 +010023static inline u8 clp_instr(void *data)
Jan Glaubera755a452012-11-29 12:55:21 +010024{
Sebastian Ottbf4ec242013-01-31 19:53:12 +010025 struct { u8 _[CLP_BLK_SIZE]; } *req = data;
26 u64 ignored;
Jan Glaubera755a452012-11-29 12:55:21 +010027 u8 cc;
28
29 asm volatile (
Sebastian Ottbf4ec242013-01-31 19:53:12 +010030 " .insn rrf,0xb9a00000,%[ign],%[req],0x0,0x2\n"
Jan Glaubera755a452012-11-29 12:55:21 +010031 " ipm %[cc]\n"
32 " srl %[cc],28\n"
Sebastian Ottbf4ec242013-01-31 19:53:12 +010033 : [cc] "=d" (cc), [ign] "=d" (ignored), "+m" (*req)
Jan Glaubera755a452012-11-29 12:55:21 +010034 : [req] "a" (req)
Sebastian Ottbf4ec242013-01-31 19:53:12 +010035 : "cc");
Jan Glaubera755a452012-11-29 12:55:21 +010036 return cc;
37}
38
Sebastian Ott1d578962013-08-29 19:37:28 +020039static void *clp_alloc_block(gfp_t gfp_mask)
Jan Glaubera755a452012-11-29 12:55:21 +010040{
Sebastian Ott1d578962013-08-29 19:37:28 +020041 return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE));
Jan Glaubera755a452012-11-29 12:55:21 +010042}
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
Sebastian Ott1d578962013-08-29 19:37:28 +020073 rrb = clp_alloc_block(GFP_KERNEL);
Jan Glaubera755a452012-11-29 12:55:21 +010074 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
Sebastian Ott1d578962013-08-29 19:37:28 +0200116 rrb = clp_alloc_block(GFP_KERNEL);
Jan Glaubera755a452012-11-29 12:55:21 +0100117 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
Sebastian Otta2ab8332013-04-16 14:11:14 +0200148 zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
Jan Glaubera755a452012-11-29 12:55:21 +0100149 zdev = zpci_alloc_device();
150 if (IS_ERR(zdev))
151 return PTR_ERR(zdev);
152
153 zdev->fh = fh;
154 zdev->fid = fid;
155
156 /* Query function properties and update zdev */
157 rc = clp_query_pci_fn(zdev, fh);
158 if (rc)
159 goto error;
160
161 if (configured)
162 zdev->state = ZPCI_FN_STATE_CONFIGURED;
163 else
164 zdev->state = ZPCI_FN_STATE_STANDBY;
165
166 rc = zpci_create_device(zdev);
167 if (rc)
168 goto error;
169 return 0;
170
171error:
172 zpci_free_device(zdev);
173 return rc;
174}
175
176/*
177 * Enable/Disable a given PCI function defined by its function handle.
178 */
179static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
180{
181 struct clp_req_rsp_set_pci *rrb;
Sebastian Ottd03abe52013-08-29 19:38:33 +0200182 int rc, retries = 100;
Jan Glaubera755a452012-11-29 12:55:21 +0100183
Sebastian Ott1d578962013-08-29 19:37:28 +0200184 rrb = clp_alloc_block(GFP_KERNEL);
Jan Glaubera755a452012-11-29 12:55:21 +0100185 if (!rrb)
186 return -ENOMEM;
187
188 do {
189 memset(rrb, 0, sizeof(*rrb));
190 rrb->request.hdr.len = sizeof(rrb->request);
191 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
192 rrb->response.hdr.len = sizeof(rrb->response);
193 rrb->request.fh = *fh;
194 rrb->request.oc = command;
195 rrb->request.ndas = nr_dma_as;
196
197 rc = clp_instr(rrb);
198 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
199 retries--;
200 if (retries < 0)
201 break;
Sebastian Ottd03abe52013-08-29 19:38:33 +0200202 msleep(20);
Jan Glaubera755a452012-11-29 12:55:21 +0100203 }
204 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
205
206 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
207 *fh = rrb->response.fh;
208 else {
Sebastian Otta2ab8332013-04-16 14:11:14 +0200209 zpci_dbg(0, "SPF fh:%x, cc:%d, resp:%x\n", *fh, rc,
210 rrb->response.hdr.rsp);
Jan Glaubera755a452012-11-29 12:55:21 +0100211 rc = -EIO;
212 }
213 clp_free_block(rrb);
214 return rc;
215}
216
217int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
218{
219 u32 fh = zdev->fh;
220 int rc;
221
222 rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
223 if (!rc)
224 /* Success -> store enabled handle in zdev */
225 zdev->fh = fh;
Sebastian Otta2ab8332013-04-16 14:11:14 +0200226
227 zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
Jan Glaubera755a452012-11-29 12:55:21 +0100228 return rc;
229}
230
231int clp_disable_fh(struct zpci_dev *zdev)
232{
233 u32 fh = zdev->fh;
234 int rc;
235
236 if (!zdev_enabled(zdev))
237 return 0;
238
Jan Glaubera755a452012-11-29 12:55:21 +0100239 rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
240 if (!rc)
241 /* Success -> store disabled handle in zdev */
242 zdev->fh = fh;
Sebastian Otta2ab8332013-04-16 14:11:14 +0200243
244 zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
Jan Glaubera755a452012-11-29 12:55:21 +0100245 return rc;
246}
247
Sebastian Ott1d578962013-08-29 19:37:28 +0200248static int clp_list_pci(struct clp_req_rsp_list_pci *rrb,
249 void (*cb)(struct clp_fh_list_entry *entry))
Jan Glaubera755a452012-11-29 12:55:21 +0100250{
Jan Glaubera755a452012-11-29 12:55:21 +0100251 u64 resume_token = 0;
252 int entries, i, rc;
253
Jan Glaubera755a452012-11-29 12:55:21 +0100254 do {
255 memset(rrb, 0, sizeof(*rrb));
256 rrb->request.hdr.len = sizeof(rrb->request);
257 rrb->request.hdr.cmd = CLP_LIST_PCI;
258 /* store as many entries as possible */
259 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
260 rrb->request.resume_token = resume_token;
261
262 /* Get PCI function handle list */
263 rc = clp_instr(rrb);
264 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
265 pr_err("List PCI failed with response: 0x%x cc: %d\n",
266 rrb->response.hdr.rsp, rc);
267 rc = -EIO;
268 goto out;
269 }
270
271 WARN_ON_ONCE(rrb->response.entry_size !=
272 sizeof(struct clp_fh_list_entry));
273
274 entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
275 rrb->response.entry_size;
276 pr_info("Detected number of PCI functions: %u\n", entries);
277
278 /* Store the returned resume token as input for the next call */
279 resume_token = rrb->response.resume_token;
280
281 for (i = 0; i < entries; i++)
Sebastian Ott1d578962013-08-29 19:37:28 +0200282 cb(&rrb->response.fh_list[i]);
Jan Glaubera755a452012-11-29 12:55:21 +0100283 } while (resume_token);
284
285 pr_debug("Maximum number of supported PCI functions: %u\n",
286 rrb->response.max_fn);
287out:
Sebastian Ott1d578962013-08-29 19:37:28 +0200288 return rc;
289}
290
291static void __clp_add(struct clp_fh_list_entry *entry)
292{
293 if (!entry->vendor_id)
294 return;
295
296 clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
297}
298
299static void __clp_rescan(struct clp_fh_list_entry *entry)
300{
301 struct zpci_dev *zdev;
302
303 if (!entry->vendor_id)
304 return;
305
306 zdev = get_zdev_by_fid(entry->fid);
307 if (!zdev) {
308 clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
309 return;
310 }
311
312 if (!entry->config_state) {
313 /*
314 * The handle is already disabled, that means no iota/irq freeing via
315 * the firmware interfaces anymore. Need to free resources manually
316 * (DMA memory, debug, sysfs)...
317 */
318 zpci_stop_device(zdev);
319 }
320}
321
Sebastian Ott57b59182013-08-29 19:40:01 +0200322static void __clp_update(struct clp_fh_list_entry *entry)
323{
324 struct zpci_dev *zdev;
325
326 if (!entry->vendor_id)
327 return;
328
329 zdev = get_zdev_by_fid(entry->fid);
330 if (!zdev)
331 return;
332
333 zdev->fh = entry->fh;
334}
335
Sebastian Ott1d578962013-08-29 19:37:28 +0200336int clp_scan_pci_devices(void)
337{
338 struct clp_req_rsp_list_pci *rrb;
339 int rc;
340
341 rrb = clp_alloc_block(GFP_KERNEL);
342 if (!rrb)
343 return -ENOMEM;
344
345 rc = clp_list_pci(rrb, __clp_add);
346
347 clp_free_block(rrb);
348 return rc;
349}
350
351int clp_rescan_pci_devices(void)
352{
353 struct clp_req_rsp_list_pci *rrb;
354 int rc;
355
356 rrb = clp_alloc_block(GFP_KERNEL);
357 if (!rrb)
358 return -ENOMEM;
359
360 rc = clp_list_pci(rrb, __clp_rescan);
361
Jan Glaubera755a452012-11-29 12:55:21 +0100362 clp_free_block(rrb);
363 return rc;
364}
Sebastian Ott57b59182013-08-29 19:40:01 +0200365
366int clp_rescan_pci_devices_simple(void)
367{
368 struct clp_req_rsp_list_pci *rrb;
369 int rc;
370
371 rrb = clp_alloc_block(GFP_NOWAIT);
372 if (!rrb)
373 return -ENOMEM;
374
375 rc = clp_list_pci(rrb, __clp_update);
376
377 clp_free_block(rrb);
378 return rc;
379}