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); | |
273 | int rc = 0; | |
274 | ||
275 | free_irq(vdev->irq, ibmvtpm); | |
132f7629 AL |
276 | |
277 | do { | |
278 | if (rc) | |
279 | msleep(100); | |
280 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | |
281 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); | |
282 | ||
283 | dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle, | |
284 | CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL); | |
285 | free_page((unsigned long)ibmvtpm->crq_queue.crq_addr); | |
286 | ||
287 | if (ibmvtpm->rtce_buf) { | |
288 | dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle, | |
289 | ibmvtpm->rtce_size, DMA_BIDIRECTIONAL); | |
290 | kfree(ibmvtpm->rtce_buf); | |
291 | } | |
292 | ||
293 | tpm_remove_hardware(ibmvtpm->dev); | |
294 | ||
295 | kfree(ibmvtpm); | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | /** | |
301 | * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver | |
302 | * @vdev: vio device struct | |
303 | * | |
304 | * Return value: | |
305 | * Number of bytes the driver needs to DMA map | |
306 | */ | |
307 | static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev) | |
308 | { | |
309 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(&vdev->dev); | |
84eb186b HCVL |
310 | |
311 | /* ibmvtpm initializes at probe time, so the data we are | |
312 | * asking for may not be set yet. Estimate that 4K required | |
313 | * for TCE-mapped buffer in addition to CRQ. | |
314 | */ | |
315 | if (!ibmvtpm) | |
316 | return CRQ_RES_BUF_SIZE + PAGE_SIZE; | |
317 | ||
132f7629 AL |
318 | return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size; |
319 | } | |
320 | ||
321 | /** | |
322 | * tpm_ibmvtpm_suspend - Suspend | |
323 | * @dev: device struct | |
324 | * | |
325 | * Return value: | |
326 | * 0 | |
327 | */ | |
328 | static int tpm_ibmvtpm_suspend(struct device *dev) | |
329 | { | |
330 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev); | |
331 | struct ibmvtpm_crq crq; | |
332 | u64 *buf = (u64 *) &crq; | |
333 | int rc = 0; | |
334 | ||
335 | crq.valid = (u8)IBMVTPM_VALID_CMD; | |
336 | crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND; | |
337 | ||
338 | rc = ibmvtpm_send_crq(ibmvtpm->vdev, buf[0], buf[1]); | |
339 | if (rc != H_SUCCESS) | |
340 | dev_err(ibmvtpm->dev, | |
341 | "tpm_ibmvtpm_suspend failed rc=%d\n", rc); | |
342 | ||
343 | return rc; | |
344 | } | |
345 | ||
346 | /** | |
347 | * ibmvtpm_reset_crq - Reset CRQ | |
348 | * @ibmvtpm: ibm vtpm struct | |
349 | * | |
350 | * Return value: | |
351 | * 0 - Success | |
352 | * Non-zero - Failure | |
353 | */ | |
354 | static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm) | |
355 | { | |
356 | int rc = 0; | |
357 | ||
358 | do { | |
359 | if (rc) | |
360 | msleep(100); | |
361 | rc = plpar_hcall_norets(H_FREE_CRQ, | |
362 | ibmvtpm->vdev->unit_address); | |
363 | } while (rc == H_BUSY || H_IS_LONG_BUSY(rc)); | |
364 | ||
365 | memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE); | |
366 | ibmvtpm->crq_queue.index = 0; | |
367 | ||
368 | return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address, | |
369 | ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); | |
370 | } | |
371 | ||
372 | /** | |
373 | * tpm_ibmvtpm_resume - Resume from suspend | |
374 | * @dev: device struct | |
375 | * | |
376 | * Return value: | |
377 | * 0 | |
378 | */ | |
379 | static int tpm_ibmvtpm_resume(struct device *dev) | |
380 | { | |
381 | struct ibmvtpm_dev *ibmvtpm = ibmvtpm_get_data(dev); | |
132f7629 AL |
382 | int rc = 0; |
383 | ||
384 | do { | |
385 | if (rc) | |
386 | msleep(100); | |
387 | rc = plpar_hcall_norets(H_ENABLE_CRQ, | |
388 | ibmvtpm->vdev->unit_address); | |
389 | } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc)); | |
390 | ||
391 | if (rc) { | |
392 | dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc); | |
393 | return rc; | |
394 | } | |
395 | ||
b5666502 AL |
396 | rc = vio_enable_interrupts(ibmvtpm->vdev); |
397 | if (rc) { | |
398 | dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc); | |
399 | return rc; | |
400 | } | |
132f7629 AL |
401 | |
402 | rc = ibmvtpm_crq_send_init(ibmvtpm); | |
403 | if (rc) | |
404 | dev_err(dev, "Error send_init rc=%d\n", rc); | |
405 | ||
406 | return rc; | |
407 | } | |
408 | ||
1f866057 SB |
409 | static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status) |
410 | { | |
411 | return (status == 0); | |
412 | } | |
413 | ||
01ad1fa7 | 414 | static const struct tpm_class_ops tpm_ibmvtpm = { |
132f7629 AL |
415 | .recv = tpm_ibmvtpm_recv, |
416 | .send = tpm_ibmvtpm_send, | |
417 | .cancel = tpm_ibmvtpm_cancel, | |
418 | .status = tpm_ibmvtpm_status, | |
419 | .req_complete_mask = 0, | |
420 | .req_complete_val = 0, | |
1f866057 | 421 | .req_canceled = tpm_ibmvtpm_req_canceled, |
132f7629 AL |
422 | }; |
423 | ||
424 | static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = { | |
425 | .suspend = tpm_ibmvtpm_suspend, | |
426 | .resume = tpm_ibmvtpm_resume, | |
427 | }; | |
428 | ||
429 | /** | |
430 | * ibmvtpm_crq_get_next - Get next responded crq | |
431 | * @ibmvtpm vtpm device struct | |
432 | * | |
433 | * Return value: | |
434 | * vtpm crq pointer | |
435 | */ | |
436 | static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm) | |
437 | { | |
438 | struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue; | |
439 | struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index]; | |
440 | ||
441 | if (crq->valid & VTPM_MSG_RES) { | |
442 | if (++crq_q->index == crq_q->num_entry) | |
443 | crq_q->index = 0; | |
b5666502 | 444 | smp_rmb(); |
132f7629 AL |
445 | } else |
446 | crq = NULL; | |
447 | return crq; | |
448 | } | |
449 | ||
450 | /** | |
451 | * ibmvtpm_crq_process - Process responded crq | |
452 | * @crq crq to be processed | |
453 | * @ibmvtpm vtpm device struct | |
454 | * | |
455 | * Return value: | |
456 | * Nothing | |
457 | */ | |
458 | static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq, | |
459 | struct ibmvtpm_dev *ibmvtpm) | |
460 | { | |
461 | int rc = 0; | |
462 | ||
463 | switch (crq->valid) { | |
464 | case VALID_INIT_CRQ: | |
465 | switch (crq->msg) { | |
466 | case INIT_CRQ_RES: | |
467 | dev_info(ibmvtpm->dev, "CRQ initialized\n"); | |
468 | rc = ibmvtpm_crq_send_init_complete(ibmvtpm); | |
469 | if (rc) | |
470 | dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc); | |
471 | return; | |
472 | case INIT_CRQ_COMP_RES: | |
473 | dev_info(ibmvtpm->dev, | |
474 | "CRQ initialization completed\n"); | |
475 | return; | |
476 | default: | |
477 | dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg); | |
478 | return; | |
479 | } | |
132f7629 AL |
480 | case IBMVTPM_VALID_CMD: |
481 | switch (crq->msg) { | |
482 | case VTPM_GET_RTCE_BUFFER_SIZE_RES: | |
483 | if (crq->len <= 0) { | |
484 | dev_err(ibmvtpm->dev, "Invalid rtce size\n"); | |
485 | return; | |
486 | } | |
487 | ibmvtpm->rtce_size = crq->len; | |
488 | ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size, | |
489 | GFP_KERNEL); | |
490 | if (!ibmvtpm->rtce_buf) { | |
491 | dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n"); | |
492 | return; | |
493 | } | |
494 | ||
495 | ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev, | |
496 | ibmvtpm->rtce_buf, ibmvtpm->rtce_size, | |
497 | DMA_BIDIRECTIONAL); | |
498 | ||
499 | if (dma_mapping_error(ibmvtpm->dev, | |
500 | ibmvtpm->rtce_dma_handle)) { | |
501 | kfree(ibmvtpm->rtce_buf); | |
502 | ibmvtpm->rtce_buf = NULL; | |
503 | dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n"); | |
504 | } | |
505 | ||
506 | return; | |
507 | case VTPM_GET_VERSION_RES: | |
508 | ibmvtpm->vtpm_version = crq->data; | |
509 | return; | |
510 | case VTPM_TPM_COMMAND_RES: | |
b5666502 AL |
511 | /* len of the data in rtce buffer */ |
512 | ibmvtpm->res_len = crq->len; | |
513 | wake_up_interruptible(&ibmvtpm->wq); | |
132f7629 AL |
514 | return; |
515 | default: | |
516 | return; | |
517 | } | |
518 | } | |
519 | return; | |
520 | } | |
521 | ||
522 | /** | |
523 | * ibmvtpm_interrupt - Interrupt handler | |
524 | * @irq: irq number to handle | |
525 | * @vtpm_instance: vtpm that received interrupt | |
526 | * | |
527 | * Returns: | |
528 | * IRQ_HANDLED | |
529 | **/ | |
530 | static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance) | |
531 | { | |
532 | struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance; | |
132f7629 | 533 | struct ibmvtpm_crq *crq; |
132f7629 | 534 | |
b5666502 AL |
535 | /* while loop is needed for initial setup (get version and |
536 | * get rtce_size). There should be only one tpm request at any | |
537 | * given time. | |
538 | */ | |
132f7629 AL |
539 | while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) { |
540 | ibmvtpm_crq_process(crq, ibmvtpm); | |
541 | crq->valid = 0; | |
b5666502 | 542 | smp_wmb(); |
132f7629 AL |
543 | } |
544 | ||
b5666502 | 545 | return IRQ_HANDLED; |
132f7629 AL |
546 | } |
547 | ||
548 | /** | |
549 | * tpm_ibmvtpm_probe - ibm vtpm initialize entry point | |
550 | * @vio_dev: vio device struct | |
551 | * @id: vio device id struct | |
552 | * | |
553 | * Return value: | |
554 | * 0 - Success | |
555 | * Non-zero - Failure | |
556 | */ | |
afc6d369 | 557 | static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev, |
132f7629 AL |
558 | const struct vio_device_id *id) |
559 | { | |
560 | struct ibmvtpm_dev *ibmvtpm; | |
561 | struct device *dev = &vio_dev->dev; | |
562 | struct ibmvtpm_crq_queue *crq_q; | |
563 | struct tpm_chip *chip; | |
564 | int rc = -ENOMEM, rc1; | |
565 | ||
566 | chip = tpm_register_hardware(dev, &tpm_ibmvtpm); | |
567 | if (!chip) { | |
568 | dev_err(dev, "tpm_register_hardware failed\n"); | |
569 | return -ENODEV; | |
570 | } | |
571 | ||
572 | ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL); | |
573 | if (!ibmvtpm) { | |
574 | dev_err(dev, "kzalloc for ibmvtpm failed\n"); | |
575 | goto cleanup; | |
576 | } | |
577 | ||
578 | crq_q = &ibmvtpm->crq_queue; | |
579 | crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL); | |
580 | if (!crq_q->crq_addr) { | |
581 | dev_err(dev, "Unable to allocate memory for crq_addr\n"); | |
582 | goto cleanup; | |
583 | } | |
584 | ||
585 | crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr); | |
586 | ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr, | |
587 | CRQ_RES_BUF_SIZE, | |
588 | DMA_BIDIRECTIONAL); | |
589 | ||
590 | if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) { | |
591 | dev_err(dev, "dma mapping failed\n"); | |
592 | goto cleanup; | |
593 | } | |
594 | ||
595 | rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address, | |
596 | ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE); | |
597 | if (rc == H_RESOURCE) | |
598 | rc = ibmvtpm_reset_crq(ibmvtpm); | |
599 | ||
600 | if (rc) { | |
601 | dev_err(dev, "Unable to register CRQ rc=%d\n", rc); | |
602 | goto reg_crq_cleanup; | |
603 | } | |
604 | ||
132f7629 AL |
605 | rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0, |
606 | tpm_ibmvtpm_driver_name, ibmvtpm); | |
607 | if (rc) { | |
608 | dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq); | |
609 | goto init_irq_cleanup; | |
610 | } | |
611 | ||
612 | rc = vio_enable_interrupts(vio_dev); | |
613 | if (rc) { | |
614 | dev_err(dev, "Error %d enabling interrupts\n", rc); | |
615 | goto init_irq_cleanup; | |
616 | } | |
617 | ||
b5666502 AL |
618 | init_waitqueue_head(&ibmvtpm->wq); |
619 | ||
132f7629 AL |
620 | crq_q->index = 0; |
621 | ||
622 | ibmvtpm->dev = dev; | |
623 | ibmvtpm->vdev = vio_dev; | |
775585e4 | 624 | TPM_VPRIV(chip) = (void *)ibmvtpm; |
132f7629 | 625 | |
132f7629 AL |
626 | spin_lock_init(&ibmvtpm->rtce_lock); |
627 | ||
628 | rc = ibmvtpm_crq_send_init(ibmvtpm); | |
629 | if (rc) | |
630 | goto init_irq_cleanup; | |
631 | ||
632 | rc = ibmvtpm_crq_get_version(ibmvtpm); | |
633 | if (rc) | |
634 | goto init_irq_cleanup; | |
635 | ||
636 | rc = ibmvtpm_crq_get_rtce_size(ibmvtpm); | |
637 | if (rc) | |
638 | goto init_irq_cleanup; | |
639 | ||
640 | return rc; | |
641 | init_irq_cleanup: | |
132f7629 AL |
642 | do { |
643 | rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address); | |
644 | } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1)); | |
645 | reg_crq_cleanup: | |
646 | dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE, | |
647 | DMA_BIDIRECTIONAL); | |
648 | cleanup: | |
649 | if (ibmvtpm) { | |
650 | if (crq_q->crq_addr) | |
651 | free_page((unsigned long)crq_q->crq_addr); | |
652 | kfree(ibmvtpm); | |
653 | } | |
654 | ||
655 | tpm_remove_hardware(dev); | |
656 | ||
657 | return rc; | |
658 | } | |
659 | ||
660 | static struct vio_driver ibmvtpm_driver = { | |
661 | .id_table = tpm_ibmvtpm_device_table, | |
662 | .probe = tpm_ibmvtpm_probe, | |
663 | .remove = tpm_ibmvtpm_remove, | |
664 | .get_desired_dma = tpm_ibmvtpm_get_desired_dma, | |
665 | .name = tpm_ibmvtpm_driver_name, | |
666 | .pm = &tpm_ibmvtpm_pm_ops, | |
667 | }; | |
668 | ||
669 | /** | |
670 | * ibmvtpm_module_init - Initialize ibm vtpm module | |
671 | * | |
672 | * Return value: | |
673 | * 0 -Success | |
674 | * Non-zero - Failure | |
675 | */ | |
676 | static int __init ibmvtpm_module_init(void) | |
677 | { | |
678 | return vio_register_driver(&ibmvtpm_driver); | |
679 | } | |
680 | ||
681 | /** | |
682 | * ibmvtpm_module_exit - Teardown ibm vtpm module | |
683 | * | |
684 | * Return value: | |
685 | * Nothing | |
686 | */ | |
687 | static void __exit ibmvtpm_module_exit(void) | |
688 | { | |
689 | vio_unregister_driver(&ibmvtpm_driver); | |
690 | } | |
691 | ||
692 | module_init(ibmvtpm_module_init); | |
693 | module_exit(ibmvtpm_module_exit); | |
694 | ||
695 | MODULE_AUTHOR("adlai@us.ibm.com"); | |
696 | MODULE_DESCRIPTION("IBM vTPM Driver"); | |
697 | MODULE_VERSION("1.0"); | |
698 | MODULE_LICENSE("GPL"); |