Blame view

drivers/watchdog/hpwdt.c 21.4 KB
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   *	HP WatchDog Driver
   *	based on
   *
   *	SoftDog	0.05:	A Software Watchdog Device
   *
   *	(c) Copyright 2007 Hewlett-Packard Development Company, L.P.
   *	Thomas Mingarelli <thomas.mingarelli@hp.com>
   *
   *	This program is free software; you can redistribute it and/or
   *	modify it under the terms of the GNU General Public License
   *	version 2 as published by the Free Software Foundation
   *
   */
  
  #include <linux/device.h>
  #include <linux/fs.h>
  #include <linux/init.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
19
  #include <linux/io.h>
a52e6d181   dann frazier   watchdog: hpwdt (...
20
  #include <linux/bitops.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
21
22
  #include <linux/kernel.h>
  #include <linux/miscdevice.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
23
  #include <linux/module.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
24
  #include <linux/moduleparam.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
25
26
  #include <linux/pci.h>
  #include <linux/pci_ids.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
27
28
29
  #include <linux/types.h>
  #include <linux/uaccess.h>
  #include <linux/watchdog.h>
86ded1f35   dann frazier   watchdog: hpwdt (...
30
  #ifdef CONFIG_HPWDT_NMI_DECODING
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
31
  #include <linux/dmi.h>
a52e6d181   dann frazier   watchdog: hpwdt (...
32
  #include <linux/spinlock.h>
923410d0b   dann frazier   watchdog: hpwdt (...
33
34
35
  #include <linux/nmi.h>
  #include <linux/kdebug.h>
  #include <linux/notifier.h>
060264133   Bernhard Walle   [WATCHDOG] hpwdt:...
36
  #include <asm/cacheflush.h>
86ded1f35   dann frazier   watchdog: hpwdt (...
37
  #endif /* CONFIG_HPWDT_NMI_DECODING */
d48b0e173   Ingo Molnar   x86, nmi, drivers...
38
  #include <asm/nmi.h>
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
39

5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
40
  #define HPWDT_VERSION			"1.3.0"
e802e32d2   dann frazier   watchdog: hpwdt (...
41
  #define SECS_TO_TICKS(secs)		((secs) * 1000 / 128)
6f681c2ea   dann frazier   watchdog: hpwdt (...
42
43
  #define TICKS_TO_SECS(ticks)		((ticks) * 128 / 1000)
  #define HPWDT_MAX_TIMER			TICKS_TO_SECS(65535)
923410d0b   dann frazier   watchdog: hpwdt (...
44
45
46
47
48
49
50
51
52
53
54
  #define DEFAULT_MARGIN			30
  
  static unsigned int soft_margin = DEFAULT_MARGIN;	/* in seconds */
  static unsigned int reload;			/* the computed soft_margin */
  static int nowayout = WATCHDOG_NOWAYOUT;
  static char expect_release;
  static unsigned long hpwdt_is_open;
  
  static void __iomem *pci_mem_addr;		/* the PCI-memory address */
  static unsigned long __iomem *hpwdt_timer_reg;
  static unsigned long __iomem *hpwdt_timer_con;
4562f5394   Wim Van Sebroeck   watchdog: convert...
55
  static DEFINE_PCI_DEVICE_TABLE(hpwdt_devices) = {
36e3ff44c   dann frazier   watchdog: hpwdt (...
56
57
  	{ PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) },	/* iLO2 */
  	{ PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) },	/* iLO3 */
923410d0b   dann frazier   watchdog: hpwdt (...
58
59
60
  	{0},			/* terminate list */
  };
  MODULE_DEVICE_TABLE(pci, hpwdt_devices);
86ded1f35   dann frazier   watchdog: hpwdt (...
61
  #ifdef CONFIG_HPWDT_NMI_DECODING
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  #define PCI_BIOS32_SD_VALUE		0x5F32335F	/* "_32_" */
  #define CRU_BIOS_SIGNATURE_VALUE	0x55524324
  #define PCI_BIOS32_PARAGRAPH_LEN	16
  #define PCI_ROM_BASE1			0x000F0000
  #define ROM_SIZE			0x10000
  
  struct bios32_service_dir {
  	u32 signature;
  	u32 entry_point;
  	u8 revision;
  	u8 length;
  	u8 checksum;
  	u8 reserved[5];
  };
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
76
77
78
79
80
81
82
83
84
85
86
  /* type 212 */
  struct smbios_cru64_info {
  	u8 type;
  	u8 byte_length;
  	u16 handle;
  	u32 signature;
  	u64 physical_address;
  	u32 double_length;
  	u32 double_offset;
  };
  #define SMBIOS_CRU64_INFORMATION	212
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
87
88
89
90
91
92
93
94
95
96
97
  /* type 219 */
  struct smbios_proliant_info {
  	u8 type;
  	u8 byte_length;
  	u16 handle;
  	u32 power_features;
  	u32 omega_features;
  	u32 reserved;
  	u32 misc_features;
  };
  #define SMBIOS_ICRU_INFORMATION		219
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  struct cmn_registers {
  	union {
  		struct {
  			u8 ral;
  			u8 rah;
  			u16 rea2;
  		};
  		u32 reax;
  	} u1;
  	union {
  		struct {
  			u8 rbl;
  			u8 rbh;
  			u8 reb2l;
  			u8 reb2h;
  		};
  		u32 rebx;
  	} u2;
  	union {
  		struct {
  			u8 rcl;
  			u8 rch;
  			u16 rec2;
  		};
  		u32 recx;
  	} u3;
  	union {
  		struct {
  			u8 rdl;
  			u8 rdh;
  			u16 red2;
  		};
  		u32 redx;
  	} u4;
  
  	u32 resi;
  	u32 redi;
  	u16 rds;
  	u16 res;
  	u32 reflags;
  }  __attribute__((packed));
34572b29d   dann frazier   watchdog: hpwdt (...
139
  static unsigned int hpwdt_nmi_decoding;
923410d0b   dann frazier   watchdog: hpwdt (...
140
  static unsigned int allow_kdump;
44df75353   Tom Mingarelli   [WATCHDOG] hpwdt:...
141
  static unsigned int priority;		/* hpwdt at end of die_notify list */
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
142
  static unsigned int is_icru;
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
143
  static DEFINE_SPINLOCK(rom_lock);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
144
  static void *cru_rom_addr;
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
145
  static struct cmn_registers cmn_regs;
143a2e54b   Wim Van Sebroeck   [WATCHDOG] More c...
146
147
  extern asmlinkage void asminline_call(struct cmn_registers *pi86Regs,
  						unsigned long *pRomEntry);
1f6ef2342   Linus Torvalds   [watchdog] hpwdt:...
148

6b7f3d532   dann frazier   watchdog: hpwdt (...
149
  #ifdef CONFIG_X86_32
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
150
151
152
  /* --32 Bit Bios------------------------------------------------------------ */
  
  #define HPWDT_ARCH	32
1f6ef2342   Linus Torvalds   [watchdog] hpwdt:...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  asm(".text                          
  \t"
      ".align 4                       
  "
      "asminline_call:                
  \t"
      "pushl       %ebp               
  \t"
      "movl        %esp, %ebp         
  \t"
      "pusha                          
  \t"
      "pushf                          
  \t"
      "push        %es                
  \t"
      "push        %ds                
  \t"
      "pop         %es                
  \t"
      "movl        8(%ebp),%eax       
  \t"
      "movl        4(%eax),%ebx       
  \t"
      "movl        8(%eax),%ecx       
  \t"
      "movl        12(%eax),%edx      
  \t"
      "movl        16(%eax),%esi      
  \t"
      "movl        20(%eax),%edi      
  \t"
      "movl        (%eax),%eax        
  \t"
      "push        %cs                
  \t"
      "call        *12(%ebp)          
  \t"
      "pushf                          
  \t"
      "pushl       %eax               
  \t"
      "movl        8(%ebp),%eax       
  \t"
      "movl        %ebx,4(%eax)       
  \t"
      "movl        %ecx,8(%eax)       
  \t"
      "movl        %edx,12(%eax)      
  \t"
      "movl        %esi,16(%eax)      
  \t"
      "movl        %edi,20(%eax)      
  \t"
      "movw        %ds,24(%eax)       
  \t"
      "movw        %es,26(%eax)       
  \t"
      "popl        %ebx               
  \t"
      "movl        %ebx,(%eax)        
  \t"
      "popl        %ebx               
  \t"
      "movl        %ebx,28(%eax)      
  \t"
      "pop         %es                
  \t"
      "popf                           
  \t"
      "popa                           
  \t"
      "leave                          
  \t"
      "ret                            
  \t"
      ".previous");
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  
  /*
   *	cru_detect
   *
   *	Routine Description:
   *	This function uses the 32-bit BIOS Service Directory record to
   *	search for a $CRU record.
   *
   *	Return Value:
   *	0        :  SUCCESS
   *	<0       :  FAILURE
   */
  static int __devinit cru_detect(unsigned long map_entry,
  	unsigned long map_offset)
  {
  	void *bios32_map;
  	unsigned long *bios32_entrypoint;
  	unsigned long cru_physical_address;
  	unsigned long cru_length;
  	unsigned long physical_bios_base = 0;
  	unsigned long physical_bios_offset = 0;
  	int retval = -ENODEV;
  
  	bios32_map = ioremap(map_entry, (2 * PAGE_SIZE));
  
  	if (bios32_map == NULL)
  		return -ENODEV;
  
  	bios32_entrypoint = bios32_map + map_offset;
  
  	cmn_regs.u1.reax = CRU_BIOS_SIGNATURE_VALUE;
e67d668e1   Mingarelli, Thomas   watchdog: hpwdt: ...
261
  	set_memory_x((unsigned long)bios32_entrypoint, (2 * PAGE_SIZE));
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
262
263
264
265
  	asminline_call(&cmn_regs, bios32_entrypoint);
  
  	if (cmn_regs.u1.ral != 0) {
  		printk(KERN_WARNING
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
266
267
268
  			"hpwdt: Call succeeded but with an error: 0x%x
  ",
  			cmn_regs.u1.ral);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
269
270
271
272
273
  	} else {
  		physical_bios_base = cmn_regs.u2.rebx;
  		physical_bios_offset = cmn_regs.u4.redx;
  		cru_length = cmn_regs.u3.recx;
  		cru_physical_address =
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
274
  			physical_bios_base + physical_bios_offset;
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
275
276
277
278
  
  		/* If the values look OK, then map it in. */
  		if ((physical_bios_base + physical_bios_offset)) {
  			cru_rom_addr =
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
279
  				ioremap(cru_physical_address, cru_length);
e67d668e1   Mingarelli, Thomas   watchdog: hpwdt: ...
280
281
  			if (cru_rom_addr) {
  				set_memory_x((unsigned long)cru_rom_addr, cru_length);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
282
  				retval = 0;
e67d668e1   Mingarelli, Thomas   watchdog: hpwdt: ...
283
  			}
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
284
285
286
287
288
289
290
291
292
293
294
  		}
  
  		printk(KERN_DEBUG "hpwdt: CRU Base Address:   0x%lx
  ",
  			physical_bios_base);
  		printk(KERN_DEBUG "hpwdt: CRU Offset Address: 0x%lx
  ",
  			physical_bios_offset);
  		printk(KERN_DEBUG "hpwdt: CRU Length:         0x%lx
  ",
  			cru_length);
adb23631a   Kulikov Vasiliy   watchdog: hpwdt: ...
295
296
297
  		printk(KERN_DEBUG "hpwdt: CRU Mapped Address: %p
  ",
  			&cru_rom_addr);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
298
299
300
301
302
303
  	}
  	iounmap(bios32_map);
  	return retval;
  }
  
  /*
30ec910e0   Roland Dreier   [WATCHDOG] hpwdt:...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
   *	bios_checksum
   */
  static int __devinit bios_checksum(const char __iomem *ptr, int len)
  {
  	char sum = 0;
  	int i;
  
  	/*
  	 * calculate checksum of size bytes. This should add up
  	 * to zero if we have a valid header.
  	 */
  	for (i = 0; i < len; i++)
  		sum += ptr[i];
  
  	return ((sum == 0) && (len > 0));
  }
  
  /*
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
   *	bios32_present
   *
   *	Routine Description:
   *	This function finds the 32-bit BIOS Service Directory
   *
   *	Return Value:
   *	0        :  SUCCESS
   *	<0       :  FAILURE
   */
  static int __devinit bios32_present(const char __iomem *p)
  {
  	struct bios32_service_dir *bios_32_ptr;
  	int length;
  	unsigned long map_entry, map_offset;
  
  	bios_32_ptr = (struct bios32_service_dir *) p;
  
  	/*
  	 * Search for signature by checking equal to the swizzled value
  	 * instead of calling another routine to perform a strcmp.
  	 */
  	if (bios_32_ptr->signature == PCI_BIOS32_SD_VALUE) {
  		length = bios_32_ptr->length * PCI_BIOS32_PARAGRAPH_LEN;
  		if (bios_checksum(p, length)) {
  			/*
  			 * According to the spec, we're looking for the
  			 * first 4KB-aligned address below the entrypoint
  			 * listed in the header. The Service Directory code
  			 * is guaranteed to occupy no more than 2 4KB pages.
  			 */
  			map_entry = bios_32_ptr->entry_point & ~(PAGE_SIZE - 1);
  			map_offset = bios_32_ptr->entry_point - map_entry;
  
  			return cru_detect(map_entry, map_offset);
  		}
  	}
  	return -ENODEV;
  }
  
  static int __devinit detect_cru_service(void)
  {
  	char __iomem *p, *q;
  	int rc = -1;
  
  	/*
  	 * Search from 0x0f0000 through 0x0fffff, inclusive.
  	 */
  	p = ioremap(PCI_ROM_BASE1, ROM_SIZE);
  	if (p == NULL)
  		return -ENOMEM;
  
  	for (q = p; q < p + ROM_SIZE; q += 16) {
  		rc = bios32_present(q);
  		if (!rc)
  			break;
  	}
  	iounmap(p);
  	return rc;
  }
6b7f3d532   dann frazier   watchdog: hpwdt (...
381
382
383
  /* ------------------------------------------------------------------------- */
  #endif /* CONFIG_X86_32 */
  #ifdef CONFIG_X86_64
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
384
385
386
  /* --64 Bit Bios------------------------------------------------------------ */
  
  #define HPWDT_ARCH	64
1f6ef2342   Linus Torvalds   [watchdog] hpwdt:...
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  asm(".text                      
  \t"
      ".align 4                   
  "
      "asminline_call:            
  \t"
      "pushq      %rbp            
  \t"
      "movq       %rsp, %rbp      
  \t"
      "pushq      %rax            
  \t"
      "pushq      %rbx            
  \t"
      "pushq      %rdx            
  \t"
      "pushq      %r12            
  \t"
      "pushq      %r9             
  \t"
      "movq       %rsi, %r12      
  \t"
      "movq       %rdi, %r9       
  \t"
      "movl       4(%r9),%ebx     
  \t"
      "movl       8(%r9),%ecx     
  \t"
      "movl       12(%r9),%edx    
  \t"
      "movl       16(%r9),%esi    
  \t"
      "movl       20(%r9),%edi    
  \t"
      "movl       (%r9),%eax      
  \t"
      "call       *%r12           
  \t"
      "pushfq                     
  \t"
      "popq        %r12           
  \t"
1f6ef2342   Linus Torvalds   [watchdog] hpwdt:...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
      "movl       %eax, (%r9)     
  \t"
      "movl       %ebx, 4(%r9)    
  \t"
      "movl       %ecx, 8(%r9)    
  \t"
      "movl       %edx, 12(%r9)   
  \t"
      "movl       %esi, 16(%r9)   
  \t"
      "movl       %edi, 20(%r9)   
  \t"
      "movq       %r12, %rax      
  \t"
      "movl       %eax, 28(%r9)   
  \t"
      "popq       %r9             
  \t"
      "popq       %r12            
  \t"
      "popq       %rdx            
  \t"
      "popq       %rbx            
  \t"
      "popq       %rax            
  \t"
      "leave                      
  \t"
      "ret                        
  \t"
      ".previous");
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
460
461
462
463
464
  
  /*
   *	dmi_find_cru
   *
   *	Routine Description:
30ec910e0   Roland Dreier   [WATCHDOG] hpwdt:...
465
   *	This function checks whether or not a SMBIOS/DMI record is
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
466
   *	the 64bit CRU info or not
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
467
   */
e7a19c562   Jean Delvare   dmi: Let dmi_walk...
468
  static void __devinit dmi_find_cru(const struct dmi_header *dm, void *dummy)
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
469
470
471
472
473
474
475
476
  {
  	struct smbios_cru64_info *smbios_cru64_ptr;
  	unsigned long cru_physical_address;
  
  	if (dm->type == SMBIOS_CRU64_INFORMATION) {
  		smbios_cru64_ptr = (struct smbios_cru64_info *) dm;
  		if (smbios_cru64_ptr->signature == CRU_BIOS_SIGNATURE_VALUE) {
  			cru_physical_address =
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
477
478
  				smbios_cru64_ptr->physical_address +
  				smbios_cru64_ptr->double_offset;
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
479
  			cru_rom_addr = ioremap(cru_physical_address,
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
480
  				smbios_cru64_ptr->double_length);
060264133   Bernhard Walle   [WATCHDOG] hpwdt:...
481
482
  			set_memory_x((unsigned long)cru_rom_addr & PAGE_MASK,
  				smbios_cru64_ptr->double_length >> PAGE_SHIFT);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
483
484
485
  		}
  	}
  }
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
486
487
488
  static int __devinit detect_cru_service(void)
  {
  	cru_rom_addr = NULL;
e7a19c562   Jean Delvare   dmi: Let dmi_walk...
489
  	dmi_walk(dmi_find_cru, NULL);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
490
491
  
  	/* if cru_rom_addr has been set then we found a CRU service */
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
492
  	return ((cru_rom_addr != NULL) ? 0 : -ENODEV);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
493
  }
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
494
  /* ------------------------------------------------------------------------- */
6b7f3d532   dann frazier   watchdog: hpwdt (...
495
  #endif /* CONFIG_X86_64 */
86ded1f35   dann frazier   watchdog: hpwdt (...
496
  #endif /* CONFIG_HPWDT_NMI_DECODING */
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
497
498
  
  /*
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
499
500
501
502
   *	Watchdog operations
   */
  static void hpwdt_start(void)
  {
e802e32d2   dann frazier   watchdog: hpwdt (...
503
  	reload = SECS_TO_TICKS(soft_margin);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  	iowrite16(reload, hpwdt_timer_reg);
  	iowrite16(0x85, hpwdt_timer_con);
  }
  
  static void hpwdt_stop(void)
  {
  	unsigned long data;
  
  	data = ioread16(hpwdt_timer_con);
  	data &= 0xFE;
  	iowrite16(data, hpwdt_timer_con);
  }
  
  static void hpwdt_ping(void)
  {
  	iowrite16(reload, hpwdt_timer_reg);
  }
  
  static int hpwdt_change_timer(int new_margin)
  {
6f681c2ea   dann frazier   watchdog: hpwdt (...
524
  	if (new_margin < 1 || new_margin > HPWDT_MAX_TIMER) {
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
525
526
527
528
529
530
531
532
533
534
535
536
  		printk(KERN_WARNING
  			"hpwdt: New value passed in is invalid: %d seconds.
  ",
  			new_margin);
  		return -EINVAL;
  	}
  
  	soft_margin = new_margin;
  	printk(KERN_DEBUG
  		"hpwdt: New timer passed in is %d seconds.
  ",
  		new_margin);
e802e32d2   dann frazier   watchdog: hpwdt (...
537
  	reload = SECS_TO_TICKS(soft_margin);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
538
539
540
  
  	return 0;
  }
aae67f360   dann frazier   watchdog: hpwdt (...
541
542
543
544
  static int hpwdt_time_left(void)
  {
  	return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
  }
86ded1f35   dann frazier   watchdog: hpwdt (...
545
  #ifdef CONFIG_HPWDT_NMI_DECODING
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
546
  /*
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
547
548
   *	NMI Handler
   */
9c48f1c62   Don Zickus   x86, nmi: Wire up...
549
  static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
550
551
552
  {
  	unsigned long rom_pl;
  	static int die_nmi_called;
34572b29d   dann frazier   watchdog: hpwdt (...
553
  	if (!hpwdt_nmi_decoding)
243066bad   dann frazier   watchdog: hpwdt (...
554
555
556
  		goto out;
  
  	spin_lock_irqsave(&rom_lock, rom_pl);
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
557
  	if (!die_nmi_called && !is_icru)
243066bad   dann frazier   watchdog: hpwdt (...
558
559
560
  		asminline_call(&cmn_regs, cru_rom_addr);
  	die_nmi_called = 1;
  	spin_unlock_irqrestore(&rom_lock, rom_pl);
dbc018eca   Naga Chumbalkar   watchdog: hpwdt: ...
561
562
563
  
  	if (allow_kdump)
  		hpwdt_stop();
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
564
565
  	if (!is_icru) {
  		if (cmn_regs.u1.ral == 0) {
dbc018eca   Naga Chumbalkar   watchdog: hpwdt: ...
566
  			panic("An NMI occurred, "
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
567
568
569
  				"but unable to determine source.
  ");
  		}
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
570
  	}
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
571
572
573
  	panic("An NMI occurred, please see the Integrated "
  		"Management Log for details.
  ");
243066bad   dann frazier   watchdog: hpwdt (...
574
  out:
9c48f1c62   Don Zickus   x86, nmi: Wire up...
575
  	return NMI_DONE;
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
576
  }
86ded1f35   dann frazier   watchdog: hpwdt (...
577
  #endif /* CONFIG_HPWDT_NMI_DECODING */
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
578
579
  
  /*
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
   *	/dev/watchdog handling
   */
  static int hpwdt_open(struct inode *inode, struct file *file)
  {
  	/* /dev/watchdog can only be opened once */
  	if (test_and_set_bit(0, &hpwdt_is_open))
  		return -EBUSY;
  
  	/* Start the watchdog */
  	hpwdt_start();
  	hpwdt_ping();
  
  	return nonseekable_open(inode, file);
  }
  
  static int hpwdt_release(struct inode *inode, struct file *file)
  {
  	/* Stop the watchdog */
  	if (expect_release == 42) {
  		hpwdt_stop();
  	} else {
  		printk(KERN_CRIT
  			"hpwdt: Unexpected close, not stopping watchdog!
  ");
  		hpwdt_ping();
  	}
  
  	expect_release = 0;
  
  	/* /dev/watchdog is being closed, make sure it can be re-opened */
  	clear_bit(0, &hpwdt_is_open);
  
  	return 0;
  }
  
  static ssize_t hpwdt_write(struct file *file, const char __user *data,
  	size_t len, loff_t *ppos)
  {
  	/* See if we got the magic character 'V' and reload the timer */
  	if (len) {
  		if (!nowayout) {
  			size_t i;
  
  			/* note: just in case someone wrote the magic character
  			 * five months ago... */
  			expect_release = 0;
  
  			/* scan to see whether or not we got the magic char. */
  			for (i = 0; i != len; i++) {
  				char c;
7944d3a5a   Wim Van Sebroeck   [WATCHDOG] more c...
630
  				if (get_user(c, data + i))
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
631
632
633
634
635
636
637
638
639
640
641
642
  					return -EFAULT;
  				if (c == 'V')
  					expect_release = 42;
  			}
  		}
  
  		/* someone wrote to us, we should reload the timer */
  		hpwdt_ping();
  	}
  
  	return len;
  }
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
643
  static const struct watchdog_info ident = {
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
644
645
646
  	.options = WDIOF_SETTIMEOUT |
  		   WDIOF_KEEPALIVEPING |
  		   WDIOF_MAGICCLOSE,
36e3ff44c   dann frazier   watchdog: hpwdt (...
647
  	.identity = "HP iLO2+ HW Watchdog Timer",
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  };
  
  static long hpwdt_ioctl(struct file *file, unsigned int cmd,
  	unsigned long arg)
  {
  	void __user *argp = (void __user *)arg;
  	int __user *p = argp;
  	int new_margin;
  	int ret = -ENOTTY;
  
  	switch (cmd) {
  	case WDIOC_GETSUPPORT:
  		ret = 0;
  		if (copy_to_user(argp, &ident, sizeof(ident)))
  			ret = -EFAULT;
  		break;
  
  	case WDIOC_GETSTATUS:
  	case WDIOC_GETBOOTSTATUS:
  		ret = put_user(0, p);
  		break;
  
  	case WDIOC_KEEPALIVE:
  		hpwdt_ping();
  		ret = 0;
  		break;
  
  	case WDIOC_SETTIMEOUT:
  		ret = get_user(new_margin, p);
  		if (ret)
  			break;
  
  		ret = hpwdt_change_timer(new_margin);
  		if (ret)
  			break;
  
  		hpwdt_ping();
  		/* Fall */
  	case WDIOC_GETTIMEOUT:
  		ret = put_user(soft_margin, p);
  		break;
aae67f360   dann frazier   watchdog: hpwdt (...
689
690
691
692
  
  	case WDIOC_GETTIMELEFT:
  		ret = put_user(hpwdt_time_left(), p);
  		break;
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
693
694
695
696
697
698
699
  	}
  	return ret;
  }
  
  /*
   *	Kernel interfaces
   */
d5c26a597   Wim Van Sebroeck   [WATCHDOG] struct...
700
  static const struct file_operations hpwdt_fops = {
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
701
702
703
704
705
706
707
708
709
710
711
712
713
  	.owner = THIS_MODULE,
  	.llseek = no_llseek,
  	.write = hpwdt_write,
  	.unlocked_ioctl = hpwdt_ioctl,
  	.open = hpwdt_open,
  	.release = hpwdt_release,
  };
  
  static struct miscdevice hpwdt_miscdev = {
  	.minor = WATCHDOG_MINOR,
  	.name = "watchdog",
  	.fops = &hpwdt_fops,
  };
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
714
715
716
  /*
   *	Init & Exit
   */
86ded1f35   dann frazier   watchdog: hpwdt (...
717
  #ifdef CONFIG_HPWDT_NMI_DECODING
4a7863cc2   Don Zickus   x86, nmi_watchdog...
718
  #ifdef CONFIG_X86_LOCAL_APIC
34572b29d   dann frazier   watchdog: hpwdt (...
719
  static void __devinit hpwdt_check_nmi_decoding(struct pci_dev *dev)
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
720
721
722
  {
  	/*
  	 * If nmi_watchdog is turned off then we can turn on
34572b29d   dann frazier   watchdog: hpwdt (...
723
  	 * our nmi decoding capability.
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
724
  	 */
072b198a4   Don Zickus   x86, nmi_watchdog...
725
  	hpwdt_nmi_decoding = 1;
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
726
727
  }
  #else
34572b29d   dann frazier   watchdog: hpwdt (...
728
  static void __devinit hpwdt_check_nmi_decoding(struct pci_dev *dev)
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
729
  {
34572b29d   dann frazier   watchdog: hpwdt (...
730
  	dev_warn(&dev->dev, "NMI decoding is disabled. "
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
731
732
733
  		"Your kernel does not support a NMI Watchdog.
  ");
  }
4a7863cc2   Don Zickus   x86, nmi_watchdog...
734
  #endif /* CONFIG_X86_LOCAL_APIC */
2ec7ed67d   dann frazier   watchdog: hpwdt (...
735

5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
  /*
   *	dmi_find_icru
   *
   *	Routine Description:
   *	This function checks whether or not we are on an iCRU-based server.
   *	This check is independent of architecture and needs to be made for
   *	any ProLiant system.
   */
  static void __devinit dmi_find_icru(const struct dmi_header *dm, void *dummy)
  {
  	struct smbios_proliant_info *smbios_proliant_ptr;
  
  	if (dm->type == SMBIOS_ICRU_INFORMATION) {
  		smbios_proliant_ptr = (struct smbios_proliant_info *) dm;
  		if (smbios_proliant_ptr->misc_features & 0x01)
  			is_icru = 1;
  	}
  }
2ec7ed67d   dann frazier   watchdog: hpwdt (...
754
755
756
757
758
  static int __devinit hpwdt_init_nmi_decoding(struct pci_dev *dev)
  {
  	int retval;
  
  	/*
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
759
760
761
762
763
764
765
766
  	 * On typical CRU-based systems we need to map that service in
  	 * the BIOS. For 32 bit Operating Systems we need to go through
  	 * the 32 Bit BIOS Service Directory. For 64 bit Operating
  	 * Systems we get that service through SMBIOS.
  	 *
  	 * On systems that support the new iCRU service all we need to
  	 * do is call dmi_walk to get the supported flag value and skip
  	 * the old cru detect code.
2ec7ed67d   dann frazier   watchdog: hpwdt (...
767
  	 */
5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
  	dmi_walk(dmi_find_icru, NULL);
  	if (!is_icru) {
  
  		/*
  		* We need to map the ROM to get the CRU service.
  		* For 32 bit Operating Systems we need to go through the 32 Bit
  		* BIOS Service Directory
  		* For 64 bit Operating Systems we get that service through SMBIOS.
  		*/
  		retval = detect_cru_service();
  		if (retval < 0) {
  			dev_warn(&dev->dev,
  				"Unable to detect the %d Bit CRU Service.
  ",
  				HPWDT_ARCH);
  			return retval;
  		}
2ec7ed67d   dann frazier   watchdog: hpwdt (...
785

5efc7a622   Thomas Mingarelli   watchdog: hpwdt: ...
786
787
788
789
790
791
792
  		/*
  		* We know this is the only CRU call we need to make so lets keep as
  		* few instructions as possible once the NMI comes in.
  		*/
  		cmn_regs.u1.rah = 0x0D;
  		cmn_regs.u1.ral = 0x02;
  	}
2ec7ed67d   dann frazier   watchdog: hpwdt (...
793
794
795
796
797
798
  
  	/*
  	 * If the priority is set to 1, then we will be put first on the
  	 * die notify list to handle a critical NMI. The default is to
  	 * be last so other users of the NMI signal can function.
  	 */
9c48f1c62   Don Zickus   x86, nmi: Wire up...
799
800
801
  	retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout,
  					(priority) ? NMI_FLAG_FIRST : 0,
  					"hpwdt");
2ec7ed67d   dann frazier   watchdog: hpwdt (...
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
  	if (retval != 0) {
  		dev_warn(&dev->dev,
  			"Unable to register a die notifier (err=%d).
  ",
  			retval);
  		if (cru_rom_addr)
  			iounmap(cru_rom_addr);
  	}
  
  	dev_info(&dev->dev,
  			"HP Watchdog Timer Driver: NMI decoding initialized"
  			", allow kernel dump: %s (default = 0/OFF)"
  			", priority: %s (default = 0/LAST).
  ",
  			(allow_kdump == 0) ? "OFF" : "ON",
  			(priority == 0) ? "LAST" : "FIRST");
  	return 0;
  }
b77b70886   Axel Lin   watchdog: hpwdt: ...
820
  static void hpwdt_exit_nmi_decoding(void)
2ec7ed67d   dann frazier   watchdog: hpwdt (...
821
  {
9c48f1c62   Don Zickus   x86, nmi: Wire up...
822
  	unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
2ec7ed67d   dann frazier   watchdog: hpwdt (...
823
824
825
  	if (cru_rom_addr)
  		iounmap(cru_rom_addr);
  }
86ded1f35   dann frazier   watchdog: hpwdt (...
826
827
828
829
830
831
832
833
834
  #else /* !CONFIG_HPWDT_NMI_DECODING */
  static void __devinit hpwdt_check_nmi_decoding(struct pci_dev *dev)
  {
  }
  
  static int __devinit hpwdt_init_nmi_decoding(struct pci_dev *dev)
  {
  	return 0;
  }
b77b70886   Axel Lin   watchdog: hpwdt: ...
835
  static void hpwdt_exit_nmi_decoding(void)
86ded1f35   dann frazier   watchdog: hpwdt (...
836
837
838
  {
  }
  #endif /* CONFIG_HPWDT_NMI_DECODING */
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
839

7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
840
  static int __devinit hpwdt_init_one(struct pci_dev *dev,
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
841
  					const struct pci_device_id *ent)
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
842
843
844
845
  {
  	int retval;
  
  	/*
34572b29d   dann frazier   watchdog: hpwdt (...
846
  	 * Check if we can do NMI decoding or not
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
847
  	 */
34572b29d   dann frazier   watchdog: hpwdt (...
848
  	hpwdt_check_nmi_decoding(dev);
47bece87b   Thomas Mingarelli   [WATCHDOG] hpwdt:...
849
850
  
  	/*
36e3ff44c   dann frazier   watchdog: hpwdt (...
851
  	 * First let's find out if we are on an iLO2+ server. We will
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
852
  	 * not run on a legacy ASM box.
ab4ba3cde   Thomas Mingarelli   [WATCHDOG] hpwdt....
853
  	 * So we only support the G5 ProLiant servers and higher.
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
854
855
856
  	 */
  	if (dev->subsystem_vendor != PCI_VENDOR_ID_HP) {
  		dev_warn(&dev->dev,
36e3ff44c   dann frazier   watchdog: hpwdt (...
857
858
  			"This server does not have an iLO2+ ASIC.
  ");
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
859
860
861
862
863
864
865
866
867
868
869
870
871
872
  		return -ENODEV;
  	}
  
  	if (pci_enable_device(dev)) {
  		dev_warn(&dev->dev,
  			"Not possible to enable PCI Device: 0x%x:0x%x.
  ",
  			ent->vendor, ent->device);
  		return -ENODEV;
  	}
  
  	pci_mem_addr = pci_iomap(dev, 1, 0x80);
  	if (!pci_mem_addr) {
  		dev_warn(&dev->dev,
36e3ff44c   dann frazier   watchdog: hpwdt (...
873
874
  			"Unable to detect the iLO2+ server memory.
  ");
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
875
876
877
878
879
880
881
882
883
  		retval = -ENOMEM;
  		goto error_pci_iomap;
  	}
  	hpwdt_timer_reg = pci_mem_addr + 0x70;
  	hpwdt_timer_con = pci_mem_addr + 0x72;
  
  	/* Make sure that we have a valid soft_margin */
  	if (hpwdt_change_timer(soft_margin))
  		hpwdt_change_timer(DEFAULT_MARGIN);
2ec7ed67d   dann frazier   watchdog: hpwdt (...
884
885
886
887
  	/* Initialize NMI Decoding functionality */
  	retval = hpwdt_init_nmi_decoding(dev);
  	if (retval != 0)
  		goto error_init_nmi_decoding;
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
888
889
890
891
892
893
894
895
896
  
  	retval = misc_register(&hpwdt_miscdev);
  	if (retval < 0) {
  		dev_warn(&dev->dev,
  			"Unable to register miscdev on minor=%d (err=%d).
  ",
  			WATCHDOG_MINOR, retval);
  		goto error_misc_register;
  	}
2ec7ed67d   dann frazier   watchdog: hpwdt (...
897
898
899
900
  	dev_info(&dev->dev, "HP Watchdog Timer Driver: %s"
  			", timer margin: %d seconds (nowayout=%d).
  ",
  			HPWDT_VERSION, soft_margin, nowayout);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
901
902
903
  	return 0;
  
  error_misc_register:
2ec7ed67d   dann frazier   watchdog: hpwdt (...
904
905
  	hpwdt_exit_nmi_decoding();
  error_init_nmi_decoding:
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
906
907
908
909
910
911
912
913
914
915
916
917
  	pci_iounmap(dev, pci_mem_addr);
  error_pci_iomap:
  	pci_disable_device(dev);
  	return retval;
  }
  
  static void __devexit hpwdt_exit(struct pci_dev *dev)
  {
  	if (!nowayout)
  		hpwdt_stop();
  
  	misc_deregister(&hpwdt_miscdev);
2ec7ed67d   dann frazier   watchdog: hpwdt (...
918
  	hpwdt_exit_nmi_decoding();
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
  	pci_iounmap(dev, pci_mem_addr);
  	pci_disable_device(dev);
  }
  
  static struct pci_driver hpwdt_driver = {
  	.name = "hpwdt",
  	.id_table = hpwdt_devices,
  	.probe = hpwdt_init_one,
  	.remove = __devexit_p(hpwdt_exit),
  };
  
  static void __exit hpwdt_cleanup(void)
  {
  	pci_unregister_driver(&hpwdt_driver);
  }
  
  static int __init hpwdt_init(void)
  {
  	return pci_register_driver(&hpwdt_driver);
  }
  
  MODULE_AUTHOR("Tom Mingarelli");
  MODULE_DESCRIPTION("hp watchdog driver");
  MODULE_LICENSE("GPL");
d8100c3ab   Thomas Mingarelli   [WATCHDOG] hpwdt....
943
  MODULE_VERSION(HPWDT_VERSION);
7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
944
945
946
947
948
949
950
951
  MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  
  module_param(soft_margin, int, 0);
  MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
  
  module_param(nowayout, int, 0);
  MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  		__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
86ded1f35   dann frazier   watchdog: hpwdt (...
952
  #ifdef CONFIG_HPWDT_NMI_DECODING
550d299ee   dann frazier   watchdog: hpwdt (...
953
954
  module_param(allow_kdump, int, 0);
  MODULE_PARM_DESC(allow_kdump, "Start a kernel dump after NMI occurs");
44df75353   Tom Mingarelli   [WATCHDOG] hpwdt:...
955
956
957
958
  module_param(priority, int, 0);
  MODULE_PARM_DESC(priority, "The hpwdt driver handles NMIs first or last"
  		" (default = 0/Last)
  ");
86ded1f35   dann frazier   watchdog: hpwdt (...
959
  #endif /* !CONFIG_HPWDT_NMI_DECODING */
44df75353   Tom Mingarelli   [WATCHDOG] hpwdt:...
960

7f4da4745   Thomas Mingarelli   [WATCHDOG] HP Pro...
961
962
  module_init(hpwdt_init);
  module_exit(hpwdt_cleanup);