[PATCH] tpm: use to_pci_dev
[powerpc.git] / drivers / char / tpm / tpm.c
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  *
4  * Authors:
5  * Leendert van Doorn <leendert@watson.ibm.com>
6  * Dave Safford <safford@watson.ibm.com>
7  * Reiner Sailer <sailer@watson.ibm.com>
8  * Kylene Hall <kjhall@us.ibm.com>
9  *
10  * Maintained by: <tpmdd_devel@lists.sourceforge.net>
11  *
12  * Device driver for TCG/TCPA TPM (trusted platform module).
13  * Specifications at www.trustedcomputinggroup.org       
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation, version 2 of the
18  * License.
19  * 
20  * Note, the TPM chip is not interrupt driven (only polling)
21  * and can have very long timeouts (minutes!). Hence the unusual
22  * calls to msleep.
23  *
24  */
25
26 #include <linux/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
29 #include "tpm.h"
30
31 enum tpm_const {
32         TPM_MINOR = 224,        /* officially assigned */
33         TPM_BUFSIZE = 2048,
34         TPM_NUM_DEVICES = 256,
35         TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
36 };
37
38   /* PCI configuration addresses */
39 enum tpm_pci_config_addr {
40         PCI_GEN_PMCON_1 = 0xA0,
41         PCI_GEN1_DEC = 0xE4,
42         PCI_LPC_EN = 0xE6,
43         PCI_GEN2_DEC = 0xEC
44 };
45
46 enum tpm_config {
47         TPM_LOCK_REG = 0x0D,
48         TPM_INTERUPT_REG = 0x0A,
49         TPM_BASE_ADDR_LO = 0x08,
50         TPM_BASE_ADDR_HI = 0x09,
51         TPM_UNLOCK_VALUE = 0x55,
52         TPM_LOCK_VALUE = 0xAA,
53         TPM_DISABLE_INTERUPT_VALUE = 0x00
54 };
55
56
57 static LIST_HEAD(tpm_chip_list);
58 static DEFINE_SPINLOCK(driver_lock);
59 static int dev_mask[TPM_NUM_MASK_ENTRIES];
60
61 static void user_reader_timeout(unsigned long ptr)
62 {
63         struct tpm_chip *chip = (struct tpm_chip *) ptr;
64
65         down(&chip->buffer_mutex);
66         atomic_set(&chip->data_pending, 0);
67         memset(chip->data_buffer, 0, TPM_BUFSIZE);
68         up(&chip->buffer_mutex);
69 }
70
71 /*
72  * Initialize the LPC bus and enable the TPM ports
73  */
74 int tpm_lpc_bus_init(struct pci_dev *pci_dev, u16 base)
75 {
76         u32 lpcenable, tmp;
77         int is_lpcm = 0;
78
79         switch (pci_dev->vendor) {
80         case PCI_VENDOR_ID_INTEL:
81                 switch (pci_dev->device) {
82                 case PCI_DEVICE_ID_INTEL_82801CA_12:
83                 case PCI_DEVICE_ID_INTEL_82801DB_12:
84                         is_lpcm = 1;
85                         break;
86                 }
87                 /* init ICH (enable LPC) */
88                 pci_read_config_dword(pci_dev, PCI_GEN1_DEC, &lpcenable);
89                 lpcenable |= 0x20000000;
90                 pci_write_config_dword(pci_dev, PCI_GEN1_DEC, lpcenable);
91
92                 if (is_lpcm) {
93                         pci_read_config_dword(pci_dev, PCI_GEN1_DEC,
94                                               &lpcenable);
95                         if ((lpcenable & 0x20000000) == 0) {
96                                 dev_err(&pci_dev->dev,
97                                         "cannot enable LPC\n");
98                                 return -ENODEV;
99                         }
100                 }
101
102                 /* initialize TPM registers */
103                 pci_read_config_dword(pci_dev, PCI_GEN2_DEC, &tmp);
104
105                 if (!is_lpcm)
106                         tmp = (tmp & 0xFFFF0000) | (base & 0xFFF0);
107                 else
108                         tmp =
109                             (tmp & 0xFFFF0000) | (base & 0xFFF0) |
110                             0x00000001;
111
112                 pci_write_config_dword(pci_dev, PCI_GEN2_DEC, tmp);
113
114                 if (is_lpcm) {
115                         pci_read_config_dword(pci_dev, PCI_GEN_PMCON_1,
116                                               &tmp);
117                         tmp |= 0x00000004;      /* enable CLKRUN */
118                         pci_write_config_dword(pci_dev, PCI_GEN_PMCON_1,
119                                                tmp);
120                 }
121                 break;
122         case PCI_VENDOR_ID_AMD:
123                 /* nothing yet */
124                 break;
125         }
126
127         tpm_write_index(TPM_LOCK_REG, TPM_UNLOCK_VALUE);
128         tpm_write_index(TPM_INTERUPT_REG, TPM_DISABLE_INTERUPT_VALUE);
129         tpm_write_index(TPM_BASE_ADDR_LO, base);
130         tpm_write_index(TPM_BASE_ADDR_HI, (base & 0xFF00) >> 8);
131         tpm_write_index(TPM_LOCK_REG, TPM_LOCK_VALUE);
132
133         return 0;
134 }
135
136 EXPORT_SYMBOL_GPL(tpm_lpc_bus_init);
137
138 /*
139  * Internal kernel interface to transmit TPM commands
140  */
141 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
142                             size_t bufsiz)
143 {
144         ssize_t len;
145         u32 count;
146         __be32 *native_size;
147         unsigned long stop;
148
149         native_size = (__force __be32 *) (buf + 2);
150         count = be32_to_cpu(*native_size);
151
152         if (count == 0)
153                 return -ENODATA;
154         if (count > bufsiz) {
155                 dev_err(&chip->pci_dev->dev,
156                         "invalid count value %x %zx \n", count, bufsiz);
157                 return -E2BIG;
158         }
159
160         down(&chip->tpm_mutex);
161
162         if ((len = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
163                 dev_err(&chip->pci_dev->dev,
164                         "tpm_transmit: tpm_send: error %zd\n", len);
165                 return len;
166         }
167
168         stop = jiffies + 2 * 60 * HZ;
169         do {
170                 u8 status = inb(chip->vendor->base + 1);
171                 if ((status & chip->vendor->req_complete_mask) ==
172                     chip->vendor->req_complete_val) {
173                         goto out_recv;
174                 }
175                 msleep(TPM_TIMEOUT); /* CHECK */
176                 rmb();
177         } while (time_before(jiffies, stop));
178
179
180         chip->vendor->cancel(chip);
181         dev_err(&chip->pci_dev->dev, "Time expired\n");
182         up(&chip->tpm_mutex);
183         return -EIO;
184
185 out_recv:
186         len = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
187         if (len < 0)
188                 dev_err(&chip->pci_dev->dev,
189                         "tpm_transmit: tpm_recv: error %zd\n", len);
190         up(&chip->tpm_mutex);
191         return len;
192 }
193
194 #define TPM_DIGEST_SIZE 20
195 #define CAP_PCR_RESULT_SIZE 18
196 static const u8 cap_pcr[] = {
197         0, 193,                 /* TPM_TAG_RQU_COMMAND */
198         0, 0, 0, 22,            /* length */
199         0, 0, 0, 101,           /* TPM_ORD_GetCapability */
200         0, 0, 0, 5,
201         0, 0, 0, 4,
202         0, 0, 1, 1
203 };
204
205 #define READ_PCR_RESULT_SIZE 30
206 static const u8 pcrread[] = {
207         0, 193,                 /* TPM_TAG_RQU_COMMAND */
208         0, 0, 0, 14,            /* length */
209         0, 0, 0, 21,            /* TPM_ORD_PcrRead */
210         0, 0, 0, 0              /* PCR index */
211 };
212
213 static ssize_t show_pcrs(struct device *dev, struct device_attribute *attr, char *buf)
214 {
215         u8 data[READ_PCR_RESULT_SIZE];
216         ssize_t len;
217         int i, j, index, num_pcrs;
218         char *str = buf;
219
220         struct tpm_chip *chip =
221             pci_get_drvdata(to_pci_dev(dev));
222         if (chip == NULL)
223                 return -ENODEV;
224
225         memcpy(data, cap_pcr, sizeof(cap_pcr));
226         if ((len = tpm_transmit(chip, data, sizeof(data)))
227             < CAP_PCR_RESULT_SIZE)
228                 return len;
229
230         num_pcrs = be32_to_cpu(*((__force __be32 *) (data + 14)));
231
232         for (i = 0; i < num_pcrs; i++) {
233                 memcpy(data, pcrread, sizeof(pcrread));
234                 index = cpu_to_be32(i);
235                 memcpy(data + 10, &index, 4);
236                 if ((len = tpm_transmit(chip, data, sizeof(data)))
237                     < READ_PCR_RESULT_SIZE)
238                         return len;
239                 str += sprintf(str, "PCR-%02d: ", i);
240                 for (j = 0; j < TPM_DIGEST_SIZE; j++)
241                         str += sprintf(str, "%02X ", *(data + 10 + j));
242                 str += sprintf(str, "\n");
243         }
244         return str - buf;
245 }
246
247 static DEVICE_ATTR(pcrs, S_IRUGO, show_pcrs, NULL);
248
249 #define  READ_PUBEK_RESULT_SIZE 314
250 static const u8 readpubek[] = {
251         0, 193,                 /* TPM_TAG_RQU_COMMAND */
252         0, 0, 0, 30,            /* length */
253         0, 0, 0, 124,           /* TPM_ORD_ReadPubek */
254 };
255
256 static ssize_t show_pubek(struct device *dev, struct device_attribute *attr, char *buf)
257 {
258         u8 *data;
259         ssize_t len;
260         __be32 *native_val;
261         int i;
262         char *str = buf;
263
264         struct tpm_chip *chip =
265             pci_get_drvdata(to_pci_dev(dev));
266         if (chip == NULL)
267                 return -ENODEV;
268
269         data = kmalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
270         if (!data)
271                 return -ENOMEM;
272
273         memcpy(data, readpubek, sizeof(readpubek));
274         memset(data + sizeof(readpubek), 0, 20);        /* zero nonce */
275
276         if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
277             READ_PUBEK_RESULT_SIZE) {
278                 rc = len;
279                 goto out;
280         }
281
282         /* 
283            ignore header 10 bytes
284            algorithm 32 bits (1 == RSA )
285            encscheme 16 bits
286            sigscheme 16 bits
287            parameters (RSA 12->bytes: keybit, #primes, expbit)  
288            keylenbytes 32 bits
289            256 byte modulus
290            ignore checksum 20 bytes
291          */
292
293         native_val = (__force __be32 *) (data + 34);
294
295         str +=
296             sprintf(str,
297                     "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
298                     "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
299                     " %02X %02X %02X %02X %02X %02X %02X %02X\n"
300                     "Modulus length: %d\nModulus: \n",
301                     data[10], data[11], data[12], data[13], data[14],
302                     data[15], data[16], data[17], data[22], data[23],
303                     data[24], data[25], data[26], data[27], data[28],
304                     data[29], data[30], data[31], data[32], data[33],
305                     be32_to_cpu(*native_val)
306             );
307
308         for (i = 0; i < 256; i++) {
309                 str += sprintf(str, "%02X ", data[i + 39]);
310                 if ((i + 1) % 16 == 0)
311                         str += sprintf(str, "\n");
312         }
313         rc = str - buf;
314 out:
315         kfree(data);
316         return rc;
317 }
318
319 static DEVICE_ATTR(pubek, S_IRUGO, show_pubek, NULL);
320
321 #define CAP_VER_RESULT_SIZE 18
322 static const u8 cap_version[] = {
323         0, 193,                 /* TPM_TAG_RQU_COMMAND */
324         0, 0, 0, 18,            /* length */
325         0, 0, 0, 101,           /* TPM_ORD_GetCapability */
326         0, 0, 0, 6,
327         0, 0, 0, 0
328 };
329
330 #define CAP_MANUFACTURER_RESULT_SIZE 18
331 static const u8 cap_manufacturer[] = {
332         0, 193,                 /* TPM_TAG_RQU_COMMAND */
333         0, 0, 0, 22,            /* length */
334         0, 0, 0, 101,           /* TPM_ORD_GetCapability */
335         0, 0, 0, 5,
336         0, 0, 0, 4,
337         0, 0, 1, 3
338 };
339
340 static ssize_t show_caps(struct device *dev, struct device_attribute *attr, char *buf)
341 {
342         u8 data[sizeof(cap_manufacturer)];
343         ssize_t len;
344         char *str = buf;
345
346         struct tpm_chip *chip =
347             pci_get_drvdata(to_pci_dev(dev));
348         if (chip == NULL)
349                 return -ENODEV;
350
351         memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
352
353         if ((len = tpm_transmit(chip, data, sizeof(data))) <
354             CAP_MANUFACTURER_RESULT_SIZE)
355                 return len;
356
357         str += sprintf(str, "Manufacturer: 0x%x\n",
358                        be32_to_cpu(*(data + 14)));
359
360         memcpy(data, cap_version, sizeof(cap_version));
361
362         if ((len = tpm_transmit(chip, data, sizeof(data))) <
363             CAP_VER_RESULT_SIZE)
364                 return len;
365
366         str +=
367             sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
368                     (int) data[14], (int) data[15], (int) data[16],
369                     (int) data[17]);
370
371         return str - buf;
372 }
373
374 static DEVICE_ATTR(caps, S_IRUGO, show_caps, NULL);
375
376 /*
377  * Device file system interface to the TPM
378  */
379 int tpm_open(struct inode *inode, struct file *file)
380 {
381         int rc = 0, minor = iminor(inode);
382         struct tpm_chip *chip = NULL, *pos;
383
384         spin_lock(&driver_lock);
385
386         list_for_each_entry(pos, &tpm_chip_list, list) {
387                 if (pos->vendor->miscdev.minor == minor) {
388                         chip = pos;
389                         break;
390                 }
391         }
392
393         if (chip == NULL) {
394                 rc = -ENODEV;
395                 goto err_out;
396         }
397
398         if (chip->num_opens) {
399                 dev_dbg(&chip->pci_dev->dev,
400                         "Another process owns this TPM\n");
401                 rc = -EBUSY;
402                 goto err_out;
403         }
404
405         chip->num_opens++;
406         pci_dev_get(chip->pci_dev);
407
408         spin_unlock(&driver_lock);
409
410         chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
411         if (chip->data_buffer == NULL) {
412                 chip->num_opens--;
413                 pci_dev_put(chip->pci_dev);
414                 return -ENOMEM;
415         }
416
417         atomic_set(&chip->data_pending, 0);
418
419         file->private_data = chip;
420         return 0;
421
422 err_out:
423         spin_unlock(&driver_lock);
424         return rc;
425 }
426
427 EXPORT_SYMBOL_GPL(tpm_open);
428
429 int tpm_release(struct inode *inode, struct file *file)
430 {
431         struct tpm_chip *chip = file->private_data;
432         
433         file->private_data = NULL;
434
435         spin_lock(&driver_lock);
436         chip->num_opens--;
437         del_singleshot_timer_sync(&chip->user_read_timer);
438         atomic_set(&chip->data_pending, 0);
439
440         pci_dev_put(chip->pci_dev);
441         return 0;
442 }
443
444 EXPORT_SYMBOL_GPL(tpm_release);
445
446 ssize_t tpm_write(struct file * file, const char __user * buf,
447                   size_t size, loff_t * off)
448 {
449         struct tpm_chip *chip = file->private_data;
450         int in_size = size, out_size;
451
452         /* cannot perform a write until the read has cleared
453            either via tpm_read or a user_read_timer timeout */
454         while (atomic_read(&chip->data_pending) != 0)
455                 msleep(TPM_TIMEOUT);
456
457         down(&chip->buffer_mutex);
458
459         if (in_size > TPM_BUFSIZE)
460                 in_size = TPM_BUFSIZE;
461
462         if (copy_from_user
463             (chip->data_buffer, (void __user *) buf, in_size)) {
464                 up(&chip->buffer_mutex);
465                 return -EFAULT;
466         }
467
468         /* atomic tpm command send and result receive */
469         out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
470
471         atomic_set(&chip->data_pending, out_size);
472         up(&chip->buffer_mutex);
473
474         /* Set a timeout by which the reader must come claim the result */
475         mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
476
477         return in_size;
478 }
479
480 EXPORT_SYMBOL_GPL(tpm_write);
481
482 ssize_t tpm_read(struct file * file, char __user * buf,
483                  size_t size, loff_t * off)
484 {
485         struct tpm_chip *chip = file->private_data;
486         int ret_size;
487
488         del_singleshot_timer_sync(&chip->user_read_timer);
489         ret_size = atomic_read(&chip->data_pending);
490         atomic_set(&chip->data_pending, 0);
491         if (ret_size > 0) {     /* relay data */
492                 if (size < ret_size)
493                         ret_size = size;
494
495                 down(&chip->buffer_mutex);
496                 if (copy_to_user
497                     ((void __user *) buf, chip->data_buffer, ret_size))
498                         ret_size = -EFAULT;
499                 up(&chip->buffer_mutex);
500         }
501
502         return ret_size;
503 }
504
505 EXPORT_SYMBOL_GPL(tpm_read);
506
507 void __devexit tpm_remove(struct pci_dev *pci_dev)
508 {
509         struct tpm_chip *chip = pci_get_drvdata(pci_dev);
510
511         if (chip == NULL) {
512                 dev_err(&pci_dev->dev, "No device data found\n");
513                 return;
514         }
515
516         spin_lock(&driver_lock);
517
518         list_del(&chip->list);
519
520         spin_unlock(&driver_lock);
521
522         pci_set_drvdata(pci_dev, NULL);
523         misc_deregister(&chip->vendor->miscdev);
524
525         device_remove_file(&pci_dev->dev, &dev_attr_pubek);
526         device_remove_file(&pci_dev->dev, &dev_attr_pcrs);
527         device_remove_file(&pci_dev->dev, &dev_attr_caps);
528
529         pci_disable_device(pci_dev);
530
531         dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
532
533         kfree(chip);
534
535         pci_dev_put(pci_dev);
536 }
537
538 EXPORT_SYMBOL_GPL(tpm_remove);
539
540 static u8 savestate[] = {
541         0, 193,                 /* TPM_TAG_RQU_COMMAND */
542         0, 0, 0, 10,            /* blob length (in bytes) */
543         0, 0, 0, 152            /* TPM_ORD_SaveState */
544 };
545
546 /*
547  * We are about to suspend. Save the TPM state
548  * so that it can be restored.
549  */
550 int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state)
551 {
552         struct tpm_chip *chip = pci_get_drvdata(pci_dev);
553         if (chip == NULL)
554                 return -ENODEV;
555
556         tpm_transmit(chip, savestate, sizeof(savestate));
557         return 0;
558 }
559
560 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
561
562 /*
563  * Resume from a power safe. The BIOS already restored
564  * the TPM state.
565  */
566 int tpm_pm_resume(struct pci_dev *pci_dev)
567 {
568         struct tpm_chip *chip = pci_get_drvdata(pci_dev);
569
570         if (chip == NULL)
571                 return -ENODEV;
572
573         spin_lock(&driver_lock);
574         tpm_lpc_bus_init(pci_dev, chip->vendor->base);
575         spin_unlock(&driver_lock);
576
577         return 0;
578 }
579
580 EXPORT_SYMBOL_GPL(tpm_pm_resume);
581
582 /*
583  * Called from tpm_<specific>.c probe function only for devices 
584  * the driver has determined it should claim.  Prior to calling
585  * this function the specific probe function has called pci_enable_device
586  * upon errant exit from this function specific probe function should call
587  * pci_disable_device
588  */
589 int tpm_register_hardware(struct pci_dev *pci_dev,
590                           struct tpm_vendor_specific *entry)
591 {
592         char devname[7];
593         struct tpm_chip *chip;
594         int i, j;
595
596         /* Driver specific per-device data */
597         chip = kmalloc(sizeof(*chip), GFP_KERNEL);
598         if (chip == NULL)
599                 return -ENOMEM;
600
601         memset(chip, 0, sizeof(struct tpm_chip));
602
603         init_MUTEX(&chip->buffer_mutex);
604         init_MUTEX(&chip->tpm_mutex);
605         INIT_LIST_HEAD(&chip->list);
606
607         init_timer(&chip->user_read_timer);
608         chip->user_read_timer.function = user_reader_timeout;
609         chip->user_read_timer.data = (unsigned long) chip;
610
611         chip->vendor = entry;
612
613         chip->dev_num = -1;
614
615         for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
616                 for (j = 0; j < 8 * sizeof(int); j++)
617                         if ((dev_mask[i] & (1 << j)) == 0) {
618                                 chip->dev_num =
619                                     i * TPM_NUM_MASK_ENTRIES + j;
620                                 dev_mask[i] |= 1 << j;
621                                 goto dev_num_search_complete;
622                         }
623
624 dev_num_search_complete:
625         if (chip->dev_num < 0) {
626                 dev_err(&pci_dev->dev,
627                         "No available tpm device numbers\n");
628                 kfree(chip);
629                 return -ENODEV;
630         } else if (chip->dev_num == 0)
631                 chip->vendor->miscdev.minor = TPM_MINOR;
632         else
633                 chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
634
635         snprintf(devname, sizeof(devname), "%s%d", "tpm", chip->dev_num);
636         chip->vendor->miscdev.name = devname;
637
638         chip->vendor->miscdev.dev = &(pci_dev->dev);
639         chip->pci_dev = pci_dev_get(pci_dev);
640
641         if (misc_register(&chip->vendor->miscdev)) {
642                 dev_err(&chip->pci_dev->dev,
643                         "unable to misc_register %s, minor %d\n",
644                         chip->vendor->miscdev.name,
645                         chip->vendor->miscdev.minor);
646                 pci_dev_put(pci_dev);
647                 kfree(chip);
648                 dev_mask[i] &= !(1 << j);
649                 return -ENODEV;
650         }
651
652         pci_set_drvdata(pci_dev, chip);
653
654         list_add(&chip->list, &tpm_chip_list);
655
656         device_create_file(&pci_dev->dev, &dev_attr_pubek);
657         device_create_file(&pci_dev->dev, &dev_attr_pcrs);
658         device_create_file(&pci_dev->dev, &dev_attr_caps);
659
660         return 0;
661 }
662
663 EXPORT_SYMBOL_GPL(tpm_register_hardware);
664
665 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
666 MODULE_DESCRIPTION("TPM Driver");
667 MODULE_VERSION("2.0");
668 MODULE_LICENSE("GPL");