2 * Copyright IBM Corp. 2012
8 #define COMPONENT "zPCI"
9 #define pr_fmt(fmt) COMPONENT ": " fmt
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>
19 * Call Logical Processor
20 * Retry logic is handled by the caller.
22 static inline u8 clp_instr(void *data)
24 struct { u8 _[CLP_BLK_SIZE]; } *req = data;
29 " .insn rrf,0xb9a00000,%[ign],%[req],0x0,0x2\n"
32 : [cc] "=d" (cc), [ign] "=d" (ignored), "+m" (*req)
38 static void *clp_alloc_block(void)
40 return (void *) __get_free_pages(GFP_KERNEL, get_order(CLP_BLK_SIZE));
43 static void clp_free_block(void *ptr)
45 free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
48 static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
49 struct clp_rsp_query_pci_grp *response)
51 zdev->tlb_refresh = response->refresh;
52 zdev->dma_mask = response->dasm;
53 zdev->msi_addr = response->msia;
54 zdev->fmb_update = response->mui;
56 pr_debug("Supported number of MSI vectors: %u\n", response->noi);
57 switch (response->version) {
59 zdev->max_bus_speed = PCIE_SPEED_5_0GT;
62 zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
67 static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
69 struct clp_req_rsp_query_pci_grp *rrb;
72 rrb = clp_alloc_block();
76 memset(rrb, 0, sizeof(*rrb));
77 rrb->request.hdr.len = sizeof(rrb->request);
78 rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
79 rrb->response.hdr.len = sizeof(rrb->response);
80 rrb->request.pfgid = pfgid;
83 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
84 clp_store_query_pci_fngrp(zdev, &rrb->response);
86 pr_err("Query PCI FNGRP failed with response: %x cc: %d\n",
87 rrb->response.hdr.rsp, rc);
94 static int clp_store_query_pci_fn(struct zpci_dev *zdev,
95 struct clp_rsp_query_pci *response)
99 for (i = 0; i < PCI_BAR_COUNT; i++) {
100 zdev->bars[i].val = le32_to_cpu(response->bar[i]);
101 zdev->bars[i].size = response->bar_size[i];
103 zdev->start_dma = response->sdma;
104 zdev->end_dma = response->edma;
105 zdev->pchid = response->pchid;
106 zdev->pfgid = response->pfgid;
110 static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
112 struct clp_req_rsp_query_pci *rrb;
115 rrb = clp_alloc_block();
119 memset(rrb, 0, sizeof(*rrb));
120 rrb->request.hdr.len = sizeof(rrb->request);
121 rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
122 rrb->response.hdr.len = sizeof(rrb->response);
123 rrb->request.fh = fh;
126 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
127 rc = clp_store_query_pci_fn(zdev, &rrb->response);
130 if (rrb->response.pfgid)
131 rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
133 pr_err("Query PCI failed with response: %x cc: %d\n",
134 rrb->response.hdr.rsp, rc);
142 int clp_add_pci_device(u32 fid, u32 fh, int configured)
144 struct zpci_dev *zdev;
147 zdev = zpci_alloc_device();
149 return PTR_ERR(zdev);
154 /* Query function properties and update zdev */
155 rc = clp_query_pci_fn(zdev, fh);
160 zdev->state = ZPCI_FN_STATE_CONFIGURED;
162 zdev->state = ZPCI_FN_STATE_STANDBY;
164 rc = zpci_create_device(zdev);
170 zpci_free_device(zdev);
175 * Enable/Disable a given PCI function defined by its function handle.
177 static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
179 struct clp_req_rsp_set_pci *rrb;
180 int rc, retries = 1000;
182 rrb = clp_alloc_block();
187 memset(rrb, 0, sizeof(*rrb));
188 rrb->request.hdr.len = sizeof(rrb->request);
189 rrb->request.hdr.cmd = CLP_SET_PCI_FN;
190 rrb->response.hdr.len = sizeof(rrb->response);
191 rrb->request.fh = *fh;
192 rrb->request.oc = command;
193 rrb->request.ndas = nr_dma_as;
196 if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
202 } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
204 if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
205 *fh = rrb->response.fh;
207 pr_err("Set PCI FN failed with response: %x cc: %d\n",
208 rrb->response.hdr.rsp, rc);
215 int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
220 rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
222 /* Success -> store enabled handle in zdev */
227 int clp_disable_fh(struct zpci_dev *zdev)
232 if (!zdev_enabled(zdev))
235 dev_info(&zdev->pdev->dev, "disabling fn handle: 0x%x\n", fh);
236 rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
238 /* Success -> store disabled handle in zdev */
241 dev_err(&zdev->pdev->dev,
242 "Failed to disable fn handle: 0x%x\n", fh);
246 static void clp_check_pcifn_entry(struct clp_fh_list_entry *entry)
250 if (!entry->vendor_id)
253 /* TODO: be a little bit more scalable */
254 present = zpci_fid_present(entry->fid);
257 pr_debug("%s: device %x already present\n", __func__, entry->fid);
259 /* skip already used functions */
260 if (present && entry->config_state)
263 /* aev 306: function moved to stand-by state */
264 if (present && !entry->config_state) {
266 * The handle is already disabled, that means no iota/irq freeing via
267 * the firmware interfaces anymore. Need to free resources manually
268 * (DMA memory, debug, sysfs)...
270 zpci_stop_device(get_zdev_by_fid(entry->fid));
274 rc = clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
276 pr_err("Failed to add fid: 0x%x\n", entry->fid);
279 int clp_find_pci_devices(void)
281 struct clp_req_rsp_list_pci *rrb;
282 u64 resume_token = 0;
285 rrb = clp_alloc_block();
290 memset(rrb, 0, sizeof(*rrb));
291 rrb->request.hdr.len = sizeof(rrb->request);
292 rrb->request.hdr.cmd = CLP_LIST_PCI;
293 /* store as many entries as possible */
294 rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
295 rrb->request.resume_token = resume_token;
297 /* Get PCI function handle list */
299 if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
300 pr_err("List PCI failed with response: 0x%x cc: %d\n",
301 rrb->response.hdr.rsp, rc);
306 WARN_ON_ONCE(rrb->response.entry_size !=
307 sizeof(struct clp_fh_list_entry));
309 entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
310 rrb->response.entry_size;
311 pr_info("Detected number of PCI functions: %u\n", entries);
313 /* Store the returned resume token as input for the next call */
314 resume_token = rrb->response.resume_token;
316 for (i = 0; i < entries; i++)
317 clp_check_pcifn_entry(&rrb->response.fh_list[i]);
318 } while (resume_token);
320 pr_debug("Maximum number of supported PCI functions: %u\n",
321 rrb->response.max_fn);