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