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