Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
132f7629 | 2 | /* |
eba5cf3d | 3 | * Copyright (C) 2012-2020 IBM Corporation |
132f7629 | 4 | * |
1a0f1b27 | 5 | * Author: Ashley Lai <ashleydlai@gmail.com> |
132f7629 AL |
6 | * |
7 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> | |
8 | * | |
9 | * Device driver for TCG/TCPA TPM (trusted platform module). | |
10 | * Specifications at www.trustedcomputinggroup.org | |
132f7629 AL |
11 | */ |
12 | ||
13 | #include <linux/dma-mapping.h> | |
14 | #include <linux/dmapool.h> | |
15 | #include <linux/slab.h> | |
16 | #include <asm/vio.h> | |
17 | #include <asm/irq.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/list.h> | |
20 | #include <linux/spinlock.h> | |
21 | #include <linux/interrupt.h> | |
22 | #include <linux/wait.h> | |
23 | #include <asm/prom.h> | |
24 | ||
25 | #include "tpm.h" | |
26 | #include "tpm_ibmvtpm.h" | |
27 | ||
28 | static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm"; | |
29 | ||
c2a9c4bf | 30 | static const struct vio_device_id tpm_ibmvtpm_device_table[] = { |
132f7629 | 31 | { "IBM,vtpm", "IBM,vtpm"}, |
18b3670d | 32 | { "IBM,vtpm", "IBM,vtpm20"}, |
132f7629 AL |
33 | { "", "" } |
34 | }; | |
35 | MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table); | |
36 | ||
132f7629 | 37 | /** |
09c573ab | 38 | * ibmvtpm_send_crq_word() - Send a CRQ request |
fb154e0e MS |
39 | * @vdev: vio device struct |
40 | * @w1: pre-constructed first word of tpm crq (second word is reserved) | |
41 | * | |
42 | * Return: | |
43 | * 0 - Success | |
44 | * Non-zero - Failure | |
45 | */ | |
46 | static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1) | |
47 | { | |
48 | return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, 0); | |
49 | } | |
50 | ||
51 | /** | |
09c573ab | 52 | * ibmvtpm_send_crq() - Send a CRQ request |
93c12f29 | 53 | * |
132f7629 | 54 | * @vdev: vio device struct |
fb154e0e MS |
55 | * @valid: Valid field |
56 | * @msg: Type field | |
57 | * @len: Length field | |
58 | * @data: Data field | |
59 | * | |
60 | * The ibmvtpm crq is defined as follows: | |
61 | * | |
62 | * Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
63 | * ----------------------------------------------------------------------- | |
64 | * Word0 | Valid | Type | Length | Data | |
65 | * ----------------------------------------------------------------------- | |
66 | * Word1 | Reserved | |
67 | * ----------------------------------------------------------------------- | |
68 | * | |
69 | * Which matches the following structure (on bigendian host): | |
70 | * | |
71 | * struct ibmvtpm_crq { | |
72 | * u8 valid; | |
73 | * u8 msg; | |
74 | * __be16 len; | |
75 | * __be32 data; | |
76 | * __be64 reserved; | |
77 | * } __attribute__((packed, aligned(8))); | |
78 | * | |
79 | * However, the value is passed in a register so just compute the numeric value | |
80 | * to load into the register avoiding byteswap altogether. Endian only affects | |
81 | * memory loads and stores - registers are internally represented the same. | |
132f7629 | 82 | * |
93c12f29 | 83 | * Return: |
fb154e0e | 84 | * 0 (H_SUCCESS) - Success |
132f7629 AL |
85 | * Non-zero - Failure |
86 | */ | |
fb154e0e MS |
87 | static int ibmvtpm_send_crq(struct vio_dev *vdev, |
88 | u8 valid, u8 msg, u16 len, u32 data) | |
132f7629 | 89 | { |
fb154e0e MS |
90 | u64 w1 = ((u64)valid << 56) | ((u64)msg << 48) | ((u64)len << 32) | |
91 | (u64)data; | |
92 | return ibmvtpm_send_crq_word(vdev, w1); | |
132f7629 AL |
93 | } |
94 | ||
132f7629 AL |
95 | /** |
96 | * tpm_ibmvtpm_recv - Receive data after send | |
93c12f29 | 97 | * |
132f7629 AL |
98 | * @chip: tpm chip struct |
99 | * @buf: buffer to read | |
93c12f29 | 100 | * @count: size of buffer |
132f7629 | 101 | * |
93c12f29 | 102 | * Return: |
132f7629 AL |
103 | * Number of bytes read |
104 | */ | |
105 | static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count) | |
106 | { | |
9e0d39d8 | 107 | struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); |
132f7629 | 108 | u16 len; |
b5666502 | 109 | int sig; |
132f7629 | 110 | |
132f7629 AL |
111 | if (!ibmvtpm->rtce_buf) { |
112 | dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); | |
113 | return 0; | |
114 | } | |
115 | ||
6674ff14 | 116 | sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd); |
b5666502 AL |
117 | if (sig) |
118 | return -EINTR; | |
119 | ||
120 | len = ibmvtpm->res_len; | |
132f7629 | 121 | |
b5666502 | 122 | if (count < len) { |
132f7629 | 123 | dev_err(ibmvtpm->dev, |
37ab0341 | 124 | "Invalid size in recv: count=%zd, crq_size=%d\n", |
b5666502 | 125 | count, len); |
132f7629 AL |
126 | return -EIO; |
127 | } | |
128 | ||
129 | spin_lock(&ibmvtpm->rtce_lock); | |
b5666502 AL |
130 | memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len); |
131 | memset(ibmvtpm->rtce_buf, 0, len); | |
132 | ibmvtpm->res_len = 0; | |
132f7629 AL |
133 | spin_unlock(&ibmvtpm->rtce_lock); |
134 | return len; | |
135 | } | |
136 | ||
eba5cf3d GW |
137 | /** |
138 | * ibmvtpm_crq_send_init - Send a CRQ initialize message | |
139 | * @ibmvtpm: vtpm device struct | |
140 | * | |
141 | * Return: | |
142 | * 0 on success. | |
143 | * Non-zero on failure. | |
144 | */ | |
145 | static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm) | |
146 | { | |
147 | int rc; | |
148 | ||
149 | rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD); | |
150 | if (rc != H_SUCCESS) | |
151 | dev_err(ibmvtpm->dev, | |
152 | "%s failed rc=%d\n", __func__, rc); | |
153 | ||
154 | return rc; | |
155 | } | |
156 | ||
157 | /** | |
158 | * tpm_ibmvtpm_resume - Resume from suspend | |
159 | * | |
160 | * @dev: device struct | |
161 | * | |
162 | * Return: Always 0. | |
163 | */ | |
164 | static int tpm_ibmvtpm_resume(struct device *dev) | |
165 | { | |
166 | struct tpm_chip *chip = dev_get_drvdata(dev); | |
167 | struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); | |
168 | int rc = 0; | |
169 | ||
170 | do { | |
171 | if (rc) | |
172 | msleep(100); | |
173 | rc = plpar_hcall_norets(H_ENABLE_CRQ, | |
174 | ibmvtpm->vdev->unit_address); | |
175 | } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); | |
176 | ||
177 | if (rc) { | |
178 | dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc); | |
179 | return rc; | |
180 | } | |
181 | ||
182 | rc = vio_enable_interrupts(ibmvtpm->vdev); | |
183 | if (rc) { | |
184 | dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc); | |
185 | return rc; | |
186 | } | |
187 | ||
188 | rc = ibmvtpm_crq_send_init(ibmvtpm); | |
189 | if (rc) | |
190 | dev_err(dev, "Error send_init rc=%d\n", rc); | |
191 | ||
192 | return rc; | |
193 | } | |
194 | ||
132f7629 | 195 | /** |
f5595f5b | 196 | * tpm_ibmvtpm_send() - Send a TPM command |
132f7629 AL |
197 | * @chip: tpm chip struct |
198 | * @buf: buffer contains data to send | |
93c12f29 | 199 | * @count: size of buffer |
132f7629 | 200 | * |
93c12f29 | 201 | * Return: |
f5595f5b JS |
202 | * 0 on success, |
203 | * -errno on error | |
132f7629 AL |
204 | */ |
205 | static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count) | |
206 | { | |
9e0d39d8 | 207 | struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); |
eba5cf3d | 208 | bool retry = true; |
6674ff14 | 209 | int rc, sig; |
132f7629 | 210 | |
132f7629 AL |
211 | if (!ibmvtpm->rtce_buf) { |
212 | dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n"); | |
213 | return 0; | |
214 | } | |
215 | ||
216 | if (count > ibmvtpm->rtce_size) { | |
217 | dev_err(ibmvtpm->dev, | |
37ab0341 | 218 | "Invalid size in send: count=%zd, rtce_size=%d\n", |
132f7629 AL |
219 | count, ibmvtpm->rtce_size); |
220 | return -EIO; | |
221 | } | |
222 | ||
6674ff14 SB |
223 | if (ibmvtpm->tpm_processing_cmd) { |
224 | dev_info(ibmvtpm->dev, | |
225 | "Need to wait for TPM to finish\n"); | |
226 | /* wait for previous command to finish */ | |
227 | sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd); | |
228 | if (sig) | |
229 | return -EINTR; | |
230 | } | |
231 | ||
132f7629 | 232 | spin_lock(&ibmvtpm->rtce_lock); |
6674ff14 | 233 | ibmvtpm->res_len = 0; |
132f7629 | 234 | memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count); |
132f7629 | 235 | |
6674ff14 SB |
236 | /* |
237 | * set the processing flag before the Hcall, since we may get the | |
238 | * result (interrupt) before even being able to check rc. | |
239 | */ | |
240 | ibmvtpm->tpm_processing_cmd = true; | |
241 | ||
eba5cf3d | 242 | again: |
fb154e0e MS |
243 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, |
244 | IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND, | |
245 | count, ibmvtpm->rtce_dma_handle); | |
132f7629 | 246 | if (rc != H_SUCCESS) { |
eba5cf3d GW |
247 | /* |
248 | * H_CLOSED can be returned after LPM resume. Call | |
249 | * tpm_ibmvtpm_resume() to re-enable the CRQ then retry | |
250 | * ibmvtpm_send_crq() once before failing. | |
251 | */ | |
252 | if (rc == H_CLOSED && retry) { | |
253 | tpm_ibmvtpm_resume(ibmvtpm->dev); | |
254 | retry = false; | |
255 | goto again; | |
256 | } | |
132f7629 | 257 | dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc); |
6674ff14 | 258 | ibmvtpm->tpm_processing_cmd = false; |
eba5cf3d | 259 | } |
132f7629 AL |
260 | |
261 | spin_unlock(&ibmvtpm->rtce_lock); | |
eba5cf3d | 262 | return 0; |
132f7629 AL |
263 | } |
264 | ||
265 | static void tpm_ibmvtpm_cancel(struct tpm_chip *chip) | |
266 | { | |
267 | return; | |
268 | } | |
269 | ||
270 | static u8 tpm_ibmvtpm_status(struct tpm_chip *chip) | |
271 | { | |
272 | return 0; | |
273 | } | |
274 | ||
275 | /** | |
276 | * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size | |
93c12f29 | 277 | * |
132f7629 AL |
278 | * @ibmvtpm: vtpm device struct |
279 | * | |
93c12f29 WT |
280 | * Return: |
281 | * 0 on success. | |
282 | * Non-zero on failure. | |
132f7629 AL |
283 | */ |
284 | static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm) | |
285 | { | |
132f7629 AL |
286 | int rc; |
287 | ||
fb154e0e MS |
288 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, |
289 | IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0); | |
132f7629 AL |
290 | if (rc != H_SUCCESS) |
291 | dev_err(ibmvtpm->dev, | |
292 | "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc); | |
293 | ||
294 | return rc; | |
295 | } | |
296 | ||
297 | /** | |
298 | * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version | |
299 | * - Note that this is vtpm version and not tpm version | |
93c12f29 | 300 | * |
132f7629 AL |
301 | * @ibmvtpm: vtpm device struct |
302 | * | |
93c12f29 WT |
303 | * Return: |
304 | * 0 on success. | |
305 | * Non-zero on failure. | |
132f7629 AL |
306 | */ |
307 | static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm) | |
308 | { | |
132f7629 AL |
309 | int rc; |
310 | ||
fb154e0e MS |
311 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, |
312 | IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0); | |
132f7629 AL |
313 | if (rc != H_SUCCESS) |
314 | dev_err(ibmvtpm->dev, | |
315 | "ibmvtpm_crq_get_version failed rc=%d\n", rc); | |
316 | ||
317 | return rc; | |
318 | } | |
319 | ||
320 | /** | |
321 | * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message | |
322 | * @ibmvtpm: vtpm device struct | |
323 | * | |
93c12f29 WT |
324 | * Return: |
325 | * 0 on success. | |
326 | * Non-zero on failure. | |
132f7629 AL |
327 | */ |
328 | static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm) | |
329 | { | |
330 | int rc; | |
331 | ||
fb154e0e | 332 | rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD); |
132f7629 AL |
333 | if (rc != H_SUCCESS) |
334 | dev_err(ibmvtpm->dev, | |
335 | "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc); | |
336 | ||
337 | return rc; | |
338 | } | |
339 | ||
132f7629 AL |
340 | /** |
341 | * tpm_ibmvtpm_remove - ibm vtpm remove entry point | |
342 | * @vdev: vio device struct | |
343 | * | |
93c12f29 | 344 | * Return: Always 0. |
132f7629 | 345 | */ |
39af33fc | 346 | static int tpm_ibmvtpm_remove(struct vio_dev *vdev) |
132f7629 | 347 | { |
9e0d39d8 CR |
348 | struct tpm_chip *chip = dev_get_drvdata(&vdev->dev); |
349 | struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); | |
132f7629 AL |
350 | int rc = 0; |
351 | ||
afb5abc2 JS |
352 | tpm_chip_unregister(chip); |
353 | ||
132f7629 | 354 | free_irq(vdev->irq, ibmvtpm); |
132f7629 AL |
355 | |
356 | do { | |
357 | if (rc) | |
358 | msleep(100); | |
359 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | |
360 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); | |
361 | ||
362 | dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle, | |
363 | CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL); | |
364 | free_page((unsigned long)ibmvtpm->crq_queue.crq_addr); | |
365 | ||
366 | if (ibmvtpm->rtce_buf) { | |
367 | dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle, | |
368 | ibmvtpm->rtce_size, DMA_BIDIRECTIONAL); | |
369 | kfree(ibmvtpm->rtce_buf); | |
370 | } | |
371 | ||
132f7629 | 372 | kfree(ibmvtpm); |
31574d32 HCVL |
373 | /* For tpm_ibmvtpm_get_desired_dma */ |
374 | dev_set_drvdata(&vdev->dev, NULL); | |
132f7629 AL |
375 | |
376 | return 0; | |
377 | } | |
378 | ||
379 | /** | |
380 | * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver | |
381 | * @vdev: vio device struct | |
382 | * | |
93c12f29 WT |
383 | * Return: |
384 | * Number of bytes the driver needs to DMA map. | |
132f7629 AL |
385 | */ |
386 | static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev) | |
387 | { | |
9e0d39d8 | 388 | struct tpm_chip *chip = dev_get_drvdata(&vdev->dev); |
31574d32 | 389 | struct ibmvtpm_dev *ibmvtpm; |
84eb186b | 390 | |
93c12f29 WT |
391 | /* |
392 | * ibmvtpm initializes at probe time, so the data we are | |
393 | * asking for may not be set yet. Estimate that 4K required | |
394 | * for TCE-mapped buffer in addition to CRQ. | |
395 | */ | |
31574d32 HCVL |
396 | if (chip) |
397 | ibmvtpm = dev_get_drvdata(&chip->dev); | |
398 | else | |
84eb186b HCVL |
399 | return CRQ_RES_BUF_SIZE + PAGE_SIZE; |
400 | ||
132f7629 AL |
401 | return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size; |
402 | } | |
403 | ||
404 | /** | |
405 | * tpm_ibmvtpm_suspend - Suspend | |
406 | * @dev: device struct | |
407 | * | |
93c12f29 | 408 | * Return: Always 0. |
132f7629 AL |
409 | */ |
410 | static int tpm_ibmvtpm_suspend(struct device *dev) | |
411 | { | |
9e0d39d8 CR |
412 | struct tpm_chip *chip = dev_get_drvdata(dev); |
413 | struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev); | |
132f7629 AL |
414 | int rc = 0; |
415 | ||
fb154e0e MS |
416 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, |
417 | IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0); | |
132f7629 AL |
418 | if (rc != H_SUCCESS) |
419 | dev_err(ibmvtpm->dev, | |
420 | "tpm_ibmvtpm_suspend failed rc=%d\n", rc); | |
421 | ||
422 | return rc; | |
423 | } | |
424 | ||
425 | /** | |
426 | * ibmvtpm_reset_crq - Reset CRQ | |
93c12f29 | 427 | * |
132f7629 AL |
428 | * @ibmvtpm: ibm vtpm struct |
429 | * | |
93c12f29 WT |
430 | * Return: |
431 | * 0 on success. | |
432 | * Non-zero on failure. | |
132f7629 AL |
433 | */ |
434 | static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm) | |
435 | { | |
436 | int rc = 0; | |
437 | ||
438 | do { | |
439 | if (rc) | |
440 | msleep(100); | |
441 | rc = plpar_hcall_norets(H_FREE_CRQ, | |
442 | ibmvtpm->vdev->unit_address); | |
443 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); | |
444 | ||
445 | memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE); | |
446 | ibmvtpm->crq_queue.index = 0; | |
447 | ||
448 | return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address, | |
449 | ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); | |
450 | } | |
451 | ||
1f866057 SB |
452 | static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status) |
453 | { | |
454 | return (status == 0); | |
455 | } | |
456 | ||
01ad1fa7 | 457 | static const struct tpm_class_ops tpm_ibmvtpm = { |
132f7629 AL |
458 | .recv = tpm_ibmvtpm_recv, |
459 | .send = tpm_ibmvtpm_send, | |
460 | .cancel = tpm_ibmvtpm_cancel, | |
461 | .status = tpm_ibmvtpm_status, | |
462 | .req_complete_mask = 0, | |
463 | .req_complete_val = 0, | |
1f866057 | 464 | .req_canceled = tpm_ibmvtpm_req_canceled, |
132f7629 AL |
465 | }; |
466 | ||
467 | static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = { | |
468 | .suspend = tpm_ibmvtpm_suspend, | |
469 | .resume = tpm_ibmvtpm_resume, | |
470 | }; | |
471 | ||
472 | /** | |
473 | * ibmvtpm_crq_get_next - Get next responded crq | |
132f7629 | 474 | * |
93c12f29 WT |
475 | * @ibmvtpm: vtpm device struct |
476 | * | |
477 | * Return: vtpm crq pointer or NULL. | |
132f7629 AL |
478 | */ |
479 | static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm) | |
480 | { | |
481 | struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue; | |
482 | struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index]; | |
483 | ||
484 | if (crq->valid & VTPM_MSG_RES) { | |
485 | if (++crq_q->index == crq_q->num_entry) | |
486 | crq_q->index = 0; | |
b5666502 | 487 | smp_rmb(); |
132f7629 AL |
488 | } else |
489 | crq = NULL; | |
490 | return crq; | |
491 | } | |
492 | ||
493 | /** | |
494 | * ibmvtpm_crq_process - Process responded crq | |
132f7629 | 495 | * |
93c12f29 WT |
496 | * @crq: crq to be processed |
497 | * @ibmvtpm: vtpm device struct | |
498 | * | |
132f7629 AL |
499 | */ |
500 | static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq, | |
501 | struct ibmvtpm_dev *ibmvtpm) | |
502 | { | |
503 | int rc = 0; | |
504 | ||
505 | switch (crq->valid) { | |
506 | case VALID_INIT_CRQ: | |
507 | switch (crq->msg) { | |
508 | case INIT_CRQ_RES: | |
509 | dev_info(ibmvtpm->dev, "CRQ initialized\n"); | |
510 | rc = ibmvtpm_crq_send_init_complete(ibmvtpm); | |
511 | if (rc) | |
512 | dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc); | |
513 | return; | |
514 | case INIT_CRQ_COMP_RES: | |
515 | dev_info(ibmvtpm->dev, | |
516 | "CRQ initialization completed\n"); | |
517 | return; | |
518 | default: | |
519 | dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg); | |
520 | return; | |
521 | } | |
132f7629 AL |
522 | case IBMVTPM_VALID_CMD: |
523 | switch (crq->msg) { | |
524 | case VTPM_GET_RTCE_BUFFER_SIZE_RES: | |
eb71f8a5 | 525 | if (be16_to_cpu(crq->len) <= 0) { |
132f7629 AL |
526 | dev_err(ibmvtpm->dev, "Invalid rtce size\n"); |
527 | return; | |
528 | } | |
eb71f8a5 | 529 | ibmvtpm->rtce_size = be16_to_cpu(crq->len); |
132f7629 | 530 | ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size, |
60ecd86c | 531 | GFP_ATOMIC); |
132f7629 AL |
532 | if (!ibmvtpm->rtce_buf) { |
533 | dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n"); | |
534 | return; | |
535 | } | |
536 | ||
537 | ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev, | |
538 | ibmvtpm->rtce_buf, ibmvtpm->rtce_size, | |
539 | DMA_BIDIRECTIONAL); | |
540 | ||
541 | if (dma_mapping_error(ibmvtpm->dev, | |
542 | ibmvtpm->rtce_dma_handle)) { | |
543 | kfree(ibmvtpm->rtce_buf); | |
544 | ibmvtpm->rtce_buf = NULL; | |
545 | dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n"); | |
546 | } | |
547 | ||
548 | return; | |
549 | case VTPM_GET_VERSION_RES: | |
eb71f8a5 | 550 | ibmvtpm->vtpm_version = be32_to_cpu(crq->data); |
132f7629 AL |
551 | return; |
552 | case VTPM_TPM_COMMAND_RES: | |
b5666502 | 553 | /* len of the data in rtce buffer */ |
eb71f8a5 | 554 | ibmvtpm->res_len = be16_to_cpu(crq->len); |
6674ff14 | 555 | ibmvtpm->tpm_processing_cmd = false; |
b5666502 | 556 | wake_up_interruptible(&ibmvtpm->wq); |
132f7629 AL |
557 | return; |
558 | default: | |
559 | return; | |
560 | } | |
561 | } | |
562 | return; | |
563 | } | |
564 | ||
565 | /** | |
566 | * ibmvtpm_interrupt - Interrupt handler | |
93c12f29 | 567 | * |
132f7629 AL |
568 | * @irq: irq number to handle |
569 | * @vtpm_instance: vtpm that received interrupt | |
570 | * | |
571 | * Returns: | |
572 | * IRQ_HANDLED | |
573 | **/ | |
574 | static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance) | |
575 | { | |
576 | struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance; | |
132f7629 | 577 | struct ibmvtpm_crq *crq; |
132f7629 | 578 | |
b5666502 AL |
579 | /* while loop is needed for initial setup (get version and |
580 | * get rtce_size). There should be only one tpm request at any | |
581 | * given time. | |
582 | */ | |
132f7629 AL |
583 | while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) { |
584 | ibmvtpm_crq_process(crq, ibmvtpm); | |
d8d74ea3 | 585 | wake_up_interruptible(&ibmvtpm->crq_queue.wq); |
132f7629 | 586 | crq->valid = 0; |
b5666502 | 587 | smp_wmb(); |
132f7629 AL |
588 | } |
589 | ||
b5666502 | 590 | return IRQ_HANDLED; |
132f7629 AL |
591 | } |
592 | ||
593 | /** | |
594 | * tpm_ibmvtpm_probe - ibm vtpm initialize entry point | |
93c12f29 | 595 | * |
132f7629 AL |
596 | * @vio_dev: vio device struct |
597 | * @id: vio device id struct | |
598 | * | |
93c12f29 WT |
599 | * Return: |
600 | * 0 on success. | |
601 | * Non-zero on failure. | |
132f7629 | 602 | */ |
afc6d369 | 603 | static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, |
132f7629 AL |
604 | const struct vio_device_id *id) |
605 | { | |
606 | struct ibmvtpm_dev *ibmvtpm; | |
607 | struct device *dev = &vio_dev->dev; | |
608 | struct ibmvtpm_crq_queue *crq_q; | |
609 | struct tpm_chip *chip; | |
610 | int rc = -ENOMEM, rc1; | |
611 | ||
afb5abc2 JS |
612 | chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm); |
613 | if (IS_ERR(chip)) | |
614 | return PTR_ERR(chip); | |
132f7629 AL |
615 | |
616 | ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL); | |
617 | if (!ibmvtpm) { | |
618 | dev_err(dev, "kzalloc for ibmvtpm failed\n"); | |
619 | goto cleanup; | |
620 | } | |
621 | ||
9d75f089 HCVL |
622 | ibmvtpm->dev = dev; |
623 | ibmvtpm->vdev = vio_dev; | |
624 | ||
132f7629 AL |
625 | crq_q = &ibmvtpm->crq_queue; |
626 | crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL); | |
627 | if (!crq_q->crq_addr) { | |
628 | dev_err(dev, "Unable to allocate memory for crq_addr\n"); | |
629 | goto cleanup; | |
630 | } | |
631 | ||
632 | crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr); | |
d8d74ea3 | 633 | init_waitqueue_head(&crq_q->wq); |
132f7629 AL |
634 | ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr, |
635 | CRQ_RES_BUF_SIZE, | |
636 | DMA_BIDIRECTIONAL); | |
637 | ||
638 | if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) { | |
639 | dev_err(dev, "dma mapping failed\n"); | |
640 | goto cleanup; | |
641 | } | |
642 | ||
643 | rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address, | |
644 | ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); | |
645 | if (rc == H_RESOURCE) | |
646 | rc = ibmvtpm_reset_crq(ibmvtpm); | |
647 | ||
648 | if (rc) { | |
649 | dev_err(dev, "Unable to register CRQ rc=%d\n", rc); | |
650 | goto reg_crq_cleanup; | |
651 | } | |
652 | ||
132f7629 AL |
653 | rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0, |
654 | tpm_ibmvtpm_driver_name, ibmvtpm); | |
655 | if (rc) { | |
656 | dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq); | |
657 | goto init_irq_cleanup; | |
658 | } | |
659 | ||
660 | rc = vio_enable_interrupts(vio_dev); | |
661 | if (rc) { | |
662 | dev_err(dev, "Error %d enabling interrupts\n", rc); | |
663 | goto init_irq_cleanup; | |
664 | } | |
665 | ||
b5666502 AL |
666 | init_waitqueue_head(&ibmvtpm->wq); |
667 | ||
132f7629 AL |
668 | crq_q->index = 0; |
669 | ||
75254557 | 670 | dev_set_drvdata(&chip->dev, ibmvtpm); |
132f7629 | 671 | |
132f7629 AL |
672 | spin_lock_init(&ibmvtpm->rtce_lock); |
673 | ||
674 | rc = ibmvtpm_crq_send_init(ibmvtpm); | |
675 | if (rc) | |
676 | goto init_irq_cleanup; | |
677 | ||
678 | rc = ibmvtpm_crq_get_version(ibmvtpm); | |
679 | if (rc) | |
680 | goto init_irq_cleanup; | |
681 | ||
682 | rc = ibmvtpm_crq_get_rtce_size(ibmvtpm); | |
683 | if (rc) | |
684 | goto init_irq_cleanup; | |
685 | ||
d8d74ea3 SB |
686 | if (!wait_event_timeout(ibmvtpm->crq_queue.wq, |
687 | ibmvtpm->rtce_buf != NULL, | |
688 | HZ)) { | |
689 | dev_err(dev, "CRQ response timed out\n"); | |
690 | goto init_irq_cleanup; | |
691 | } | |
692 | ||
72d0556d DG |
693 | if (!strcmp(id->compat, "IBM,vtpm20")) { |
694 | chip->flags |= TPM_CHIP_FLAG_TPM2; | |
695 | rc = tpm2_get_cc_attrs_tbl(chip); | |
696 | if (rc) | |
697 | goto init_irq_cleanup; | |
698 | } | |
699 | ||
afb5abc2 | 700 | return tpm_chip_register(chip); |
132f7629 | 701 | init_irq_cleanup: |
132f7629 AL |
702 | do { |
703 | rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address); | |
704 | } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1)); | |
705 | reg_crq_cleanup: | |
706 | dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE, | |
707 | DMA_BIDIRECTIONAL); | |
708 | cleanup: | |
709 | if (ibmvtpm) { | |
710 | if (crq_q->crq_addr) | |
711 | free_page((unsigned long)crq_q->crq_addr); | |
712 | kfree(ibmvtpm); | |
713 | } | |
714 | ||
132f7629 AL |
715 | return rc; |
716 | } | |
717 | ||
718 | static struct vio_driver ibmvtpm_driver = { | |
719 | .id_table = tpm_ibmvtpm_device_table, | |
720 | .probe = tpm_ibmvtpm_probe, | |
721 | .remove = tpm_ibmvtpm_remove, | |
722 | .get_desired_dma = tpm_ibmvtpm_get_desired_dma, | |
723 | .name = tpm_ibmvtpm_driver_name, | |
724 | .pm = &tpm_ibmvtpm_pm_ops, | |
725 | }; | |
726 | ||
727 | /** | |
93c12f29 | 728 | * ibmvtpm_module_init - Initialize ibm vtpm module. |
132f7629 | 729 | * |
93c12f29 WT |
730 | * |
731 | * Return: | |
732 | * 0 on success. | |
733 | * Non-zero on failure. | |
132f7629 AL |
734 | */ |
735 | static int __init ibmvtpm_module_init(void) | |
736 | { | |
737 | return vio_register_driver(&ibmvtpm_driver); | |
738 | } | |
739 | ||
740 | /** | |
93c12f29 | 741 | * ibmvtpm_module_exit - Tear down ibm vtpm module. |
132f7629 AL |
742 | */ |
743 | static void __exit ibmvtpm_module_exit(void) | |
744 | { | |
745 | vio_unregister_driver(&ibmvtpm_driver); | |
746 | } | |
747 | ||
748 | module_init(ibmvtpm_module_init); | |
749 | module_exit(ibmvtpm_module_exit); | |
750 | ||
751 | MODULE_AUTHOR("adlai@us.ibm.com"); | |
752 | MODULE_DESCRIPTION("IBM vTPM Driver"); | |
753 | MODULE_VERSION("1.0"); | |
754 | MODULE_LICENSE("GPL"); |