Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / pci / hotplug / cpqphp_nvram.c
1 /*
2  * Compaq Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM Corp.
7  *
8  * All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or (at
13  * your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18  * NON INFRINGEMENT.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Send feedback to <greg@kroah.com>
26  *
27  */
28
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/proc_fs.h>
33 #include <linux/slab.h>
34 #include <linux/workqueue.h>
35 #include <linux/pci.h>
36 #include <linux/init.h>
37 #include <asm/uaccess.h>
38 #include "cpqphp.h"
39 #include "cpqphp_nvram.h"
40
41
42 #define ROM_INT15_PHY_ADDR              0x0FF859
43 #define READ_EV                         0xD8A4
44 #define WRITE_EV                        0xD8A5
45
46 struct register_foo {
47         union {
48                 unsigned long lword;            /* eax */
49                 unsigned short word;            /* ax */
50
51                 struct {
52                         unsigned char low;      /* al */
53                         unsigned char high;     /* ah */
54                 } byte;
55         } data;
56
57         unsigned char opcode;   /* see below */
58         unsigned long length;   /* if the reg. is a pointer, how much data */
59 } __attribute__ ((packed));
60
61 struct all_reg {
62         struct register_foo eax_reg;
63         struct register_foo ebx_reg;
64         struct register_foo ecx_reg;
65         struct register_foo edx_reg;
66         struct register_foo edi_reg;
67         struct register_foo esi_reg;
68         struct register_foo eflags_reg;
69 } __attribute__ ((packed));
70
71
72 struct ev_hrt_header {
73         u8 Version;
74         u8 num_of_ctrl;
75         u8 next;
76 };
77
78 struct ev_hrt_ctrl {
79         u8 bus;
80         u8 device;
81         u8 function;
82         u8 mem_avail;
83         u8 p_mem_avail;
84         u8 io_avail;
85         u8 bus_avail;
86         u8 next;
87 };
88
89
90 static u8 evbuffer_init;
91 static u8 evbuffer_length;
92 static u8 evbuffer[1024];
93
94 static void __iomem *compaq_int15_entry_point;
95
96 static spinlock_t int15_lock;           /* lock for ordering int15_bios_call() */
97
98
99 /* This is a series of function that deals with
100    setting & getting the hotplug resource table in some environment variable.
101 */
102
103 /*
104  * We really shouldn't be doing this unless there is a _very_ good reason to!!!
105  * greg k-h
106  */
107
108
109 static u32 add_byte( u32 **p_buffer, u8 value, u32 *used, u32 *avail)
110 {
111         u8 **tByte;
112
113         if ((*used + 1) > *avail)
114                 return(1);
115         
116         *((u8*)*p_buffer) = value;
117         tByte = (u8**)p_buffer;
118         (*tByte)++;
119         *used+=1;
120         return(0);
121 }
122
123
124 static u32 add_dword( u32 **p_buffer, u32 value, u32 *used, u32 *avail)
125 {
126         if ((*used + 4) > *avail)
127                 return(1);
128
129         **p_buffer = value;
130         (*p_buffer)++;
131         *used+=4;
132         return(0);
133 }
134
135
136 /*
137  * check_for_compaq_ROM
138  *
139  * this routine verifies that the ROM OEM string is 'COMPAQ'
140  *
141  * returns 0 for non-Compaq ROM, 1 for Compaq ROM
142  */
143 static int check_for_compaq_ROM (void __iomem *rom_start)
144 {
145         u8 temp1, temp2, temp3, temp4, temp5, temp6;
146         int result = 0;
147
148         temp1 = readb(rom_start + 0xffea + 0);
149         temp2 = readb(rom_start + 0xffea + 1);
150         temp3 = readb(rom_start + 0xffea + 2);
151         temp4 = readb(rom_start + 0xffea + 3);
152         temp5 = readb(rom_start + 0xffea + 4);
153         temp6 = readb(rom_start + 0xffea + 5);
154         if ((temp1 == 'C') &&
155             (temp2 == 'O') &&
156             (temp3 == 'M') &&
157             (temp4 == 'P') &&
158             (temp5 == 'A') &&
159             (temp6 == 'Q')) {
160                 result = 1;
161         }
162         dbg ("%s - returned %d\n", __FUNCTION__, result);
163         return result;
164 }
165
166
167 static u32 access_EV (u16 operation, u8 *ev_name, u8 *buffer, u32 *buf_size)
168 {
169         unsigned long flags;
170         int op = operation;
171         int ret_val;
172         
173         if (!compaq_int15_entry_point)
174                 return -ENODEV;
175         
176         spin_lock_irqsave(&int15_lock, flags);
177         __asm__ (
178                 "xorl   %%ebx,%%ebx\n" \
179                 "xorl    %%edx,%%edx\n" \
180                 "pushf\n" \
181                 "push %%cs\n" \
182                 "cli\n" \
183                 "call *%6\n"
184                 : "=c" (*buf_size), "=a" (ret_val)
185                 : "a" (op), "c" (*buf_size), "S" (ev_name),
186                 "D" (buffer), "m" (compaq_int15_entry_point)
187                 : "%ebx", "%edx");
188         spin_unlock_irqrestore(&int15_lock, flags);
189         
190         return((ret_val & 0xFF00) >> 8);
191 }
192
193
194 /*
195  * load_HRT
196  *
197  * Read the hot plug Resource Table from NVRAM
198  */
199 static int load_HRT (void __iomem *rom_start)
200 {
201         u32 available;
202         u32 temp_dword;
203         u8 temp_byte = 0xFF;
204         u32 rc;
205
206         if (!check_for_compaq_ROM(rom_start)) {
207                 return -ENODEV;
208         }
209
210         available = 1024;
211
212         // Now load the EV
213         temp_dword = available;
214
215         rc = access_EV(READ_EV, "CQTHPS", evbuffer, &temp_dword);
216
217         evbuffer_length = temp_dword;
218
219         // We're maintaining the resource lists so write FF to invalidate old info
220         temp_dword = 1;
221
222         rc = access_EV(WRITE_EV, "CQTHPS", &temp_byte, &temp_dword);
223
224         return rc;
225 }
226
227
228 /*
229  * store_HRT
230  *
231  * Save the hot plug Resource Table in NVRAM
232  */
233 static u32 store_HRT (void __iomem *rom_start)
234 {
235         u32 *buffer;
236         u32 *pFill;
237         u32 usedbytes;
238         u32 available;
239         u32 temp_dword;
240         u32 rc;
241         u8 loop;
242         u8 numCtrl = 0;
243         struct controller *ctrl;
244         struct pci_resource *resNode;
245         struct ev_hrt_header *p_EV_header;
246         struct ev_hrt_ctrl *p_ev_ctrl;
247
248         available = 1024;
249
250         if (!check_for_compaq_ROM(rom_start)) {
251                 return(1);
252         }
253
254         buffer = (u32*) evbuffer;
255
256         if (!buffer)
257                 return(1);
258
259         pFill = buffer;
260         usedbytes = 0;
261
262         p_EV_header = (struct ev_hrt_header *) pFill;
263
264         ctrl = cpqhp_ctrl_list;
265         
266         // The revision of this structure
267         rc = add_byte( &pFill, 1 + ctrl->push_flag, &usedbytes, &available);
268         if (rc)
269                 return(rc);
270
271         // The number of controllers
272         rc = add_byte( &pFill, 1, &usedbytes, &available);
273         if (rc)
274                 return(rc);
275
276         while (ctrl) {
277                 p_ev_ctrl = (struct ev_hrt_ctrl *) pFill;
278
279                 numCtrl++;
280
281                 // The bus number
282                 rc = add_byte( &pFill, ctrl->bus, &usedbytes, &available);
283                 if (rc)
284                         return(rc);
285
286                 // The device Number
287                 rc = add_byte( &pFill, PCI_SLOT(ctrl->pci_dev->devfn), &usedbytes, &available);
288                 if (rc)
289                         return(rc);
290
291                 // The function Number
292                 rc = add_byte( &pFill, PCI_FUNC(ctrl->pci_dev->devfn), &usedbytes, &available);
293                 if (rc)
294                         return(rc);
295
296                 // Skip the number of available entries
297                 rc = add_dword( &pFill, 0, &usedbytes, &available);
298                 if (rc)
299                         return(rc);
300
301                 // Figure out memory Available
302
303                 resNode = ctrl->mem_head;
304
305                 loop = 0;
306
307                 while (resNode) {
308                         loop ++;
309
310                         // base
311                         rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
312                         if (rc)
313                                 return(rc);
314
315                         // length
316                         rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
317                         if (rc)
318                                 return(rc);
319
320                         resNode = resNode->next;
321                 }
322
323                 // Fill in the number of entries
324                 p_ev_ctrl->mem_avail = loop;
325
326                 // Figure out prefetchable memory Available
327
328                 resNode = ctrl->p_mem_head;
329
330                 loop = 0;
331
332                 while (resNode) {
333                         loop ++;
334
335                         // base
336                         rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
337                         if (rc)
338                                 return(rc);
339
340                         // length
341                         rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
342                         if (rc)
343                                 return(rc);
344
345                         resNode = resNode->next;
346                 }
347
348                 // Fill in the number of entries
349                 p_ev_ctrl->p_mem_avail = loop;
350
351                 // Figure out IO Available
352
353                 resNode = ctrl->io_head;
354
355                 loop = 0;
356
357                 while (resNode) {
358                         loop ++;
359
360                         // base
361                         rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
362                         if (rc)
363                                 return(rc);
364
365                         // length
366                         rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
367                         if (rc)
368                                 return(rc);
369
370                         resNode = resNode->next;
371                 }
372
373                 // Fill in the number of entries
374                 p_ev_ctrl->io_avail = loop;
375
376                 // Figure out bus Available
377
378                 resNode = ctrl->bus_head;
379
380                 loop = 0;
381
382                 while (resNode) {
383                         loop ++;
384
385                         // base
386                         rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
387                         if (rc)
388                                 return(rc);
389
390                         // length
391                         rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
392                         if (rc)
393                                 return(rc);
394
395                         resNode = resNode->next;
396                 }
397
398                 // Fill in the number of entries
399                 p_ev_ctrl->bus_avail = loop;
400
401                 ctrl = ctrl->next;
402         }
403         
404         p_EV_header->num_of_ctrl = numCtrl;
405
406         // Now store the EV
407
408         temp_dword = usedbytes;
409
410         rc = access_EV(WRITE_EV, "CQTHPS", (u8*) buffer, &temp_dword);
411
412         dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes, temp_dword);
413
414         evbuffer_length = temp_dword;
415
416         if (rc) {
417                 err(msg_unable_to_save);
418                 return(1);
419         }
420
421         return(0);
422 }
423
424
425 void compaq_nvram_init (void __iomem *rom_start)
426 {
427         if (rom_start) {
428                 compaq_int15_entry_point = (rom_start + ROM_INT15_PHY_ADDR - ROM_PHY_ADDR);
429         }
430         dbg("int15 entry  = %p\n", compaq_int15_entry_point);
431
432         /* initialize our int15 lock */
433         spin_lock_init(&int15_lock);
434 }
435
436
437 int compaq_nvram_load (void __iomem *rom_start, struct controller *ctrl)
438 {
439         u8 bus, device, function;
440         u8 nummem, numpmem, numio, numbus;
441         u32 rc;
442         u8 *p_byte;
443         struct pci_resource *mem_node;
444         struct pci_resource *p_mem_node;
445         struct pci_resource *io_node;
446         struct pci_resource *bus_node;
447         struct ev_hrt_ctrl *p_ev_ctrl;
448         struct ev_hrt_header *p_EV_header;
449
450         if (!evbuffer_init) {
451                 // Read the resource list information in from NVRAM
452                 if (load_HRT(rom_start))
453                         memset (evbuffer, 0, 1024);
454
455                 evbuffer_init = 1;
456         }
457
458         // If we saved information in NVRAM, use it now
459         p_EV_header = (struct ev_hrt_header *) evbuffer;
460
461         // The following code is for systems where version 1.0 of this
462         // driver has been loaded, but doesn't support the hardware.
463         // In that case, the driver would incorrectly store something
464         // in NVRAM.
465         if ((p_EV_header->Version == 2) ||
466             ((p_EV_header->Version == 1) && !ctrl->push_flag)) {
467                 p_byte = &(p_EV_header->next);
468
469                 p_ev_ctrl = (struct ev_hrt_ctrl *) &(p_EV_header->next);
470
471                 p_byte += 3;
472
473                 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
474                         return 2;
475
476                 bus = p_ev_ctrl->bus;
477                 device = p_ev_ctrl->device;
478                 function = p_ev_ctrl->function;
479
480                 while ((bus != ctrl->bus) ||
481                        (device != PCI_SLOT(ctrl->pci_dev->devfn)) || 
482                        (function != PCI_FUNC(ctrl->pci_dev->devfn))) {
483                         nummem = p_ev_ctrl->mem_avail;
484                         numpmem = p_ev_ctrl->p_mem_avail;
485                         numio = p_ev_ctrl->io_avail;
486                         numbus = p_ev_ctrl->bus_avail;
487
488                         p_byte += 4;
489
490                         if (p_byte > ((u8*)p_EV_header + evbuffer_length))
491                                 return 2;
492
493                         // Skip forward to the next entry
494                         p_byte += (nummem + numpmem + numio + numbus) * 8;
495
496                         if (p_byte > ((u8*)p_EV_header + evbuffer_length))
497                                 return 2;
498
499                         p_ev_ctrl = (struct ev_hrt_ctrl *) p_byte;
500
501                         p_byte += 3;
502
503                         if (p_byte > ((u8*)p_EV_header + evbuffer_length))
504                                 return 2;
505
506                         bus = p_ev_ctrl->bus;
507                         device = p_ev_ctrl->device;
508                         function = p_ev_ctrl->function;
509                 }
510
511                 nummem = p_ev_ctrl->mem_avail;
512                 numpmem = p_ev_ctrl->p_mem_avail;
513                 numio = p_ev_ctrl->io_avail;
514                 numbus = p_ev_ctrl->bus_avail;
515
516                 p_byte += 4;
517
518                 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
519                         return 2;
520
521                 while (nummem--) {
522                         mem_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
523
524                         if (!mem_node)
525                                 break;
526
527                         mem_node->base = *(u32*)p_byte;
528                         dbg("mem base = %8.8x\n",mem_node->base);
529                         p_byte += 4;
530
531                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
532                                 kfree(mem_node);
533                                 return 2;
534                         }
535
536                         mem_node->length = *(u32*)p_byte;
537                         dbg("mem length = %8.8x\n",mem_node->length);
538                         p_byte += 4;
539
540                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
541                                 kfree(mem_node);
542                                 return 2;
543                         }
544
545                         mem_node->next = ctrl->mem_head;
546                         ctrl->mem_head = mem_node;
547                 }
548
549                 while (numpmem--) {
550                         p_mem_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
551
552                         if (!p_mem_node)
553                                 break;
554
555                         p_mem_node->base = *(u32*)p_byte;
556                         dbg("pre-mem base = %8.8x\n",p_mem_node->base);
557                         p_byte += 4;
558
559                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
560                                 kfree(p_mem_node);
561                                 return 2;
562                         }
563
564                         p_mem_node->length = *(u32*)p_byte;
565                         dbg("pre-mem length = %8.8x\n",p_mem_node->length);
566                         p_byte += 4;
567
568                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
569                                 kfree(p_mem_node);
570                                 return 2;
571                         }
572
573                         p_mem_node->next = ctrl->p_mem_head;
574                         ctrl->p_mem_head = p_mem_node;
575                 }
576
577                 while (numio--) {
578                         io_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
579
580                         if (!io_node)
581                                 break;
582
583                         io_node->base = *(u32*)p_byte;
584                         dbg("io base = %8.8x\n",io_node->base);
585                         p_byte += 4;
586
587                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
588                                 kfree(io_node);
589                                 return 2;
590                         }
591
592                         io_node->length = *(u32*)p_byte;
593                         dbg("io length = %8.8x\n",io_node->length);
594                         p_byte += 4;
595
596                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
597                                 kfree(io_node);
598                                 return 2;
599                         }
600
601                         io_node->next = ctrl->io_head;
602                         ctrl->io_head = io_node;
603                 }
604
605                 while (numbus--) {
606                         bus_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
607
608                         if (!bus_node)
609                                 break;
610
611                         bus_node->base = *(u32*)p_byte;
612                         p_byte += 4;
613
614                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
615                                 kfree(bus_node);
616                                 return 2;
617                         }
618
619                         bus_node->length = *(u32*)p_byte;
620                         p_byte += 4;
621
622                         if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
623                                 kfree(bus_node);
624                                 return 2;
625                         }
626
627                         bus_node->next = ctrl->bus_head;
628                         ctrl->bus_head = bus_node;
629                 }
630
631                 // If all of the following fail, we don't have any resources for
632                 // hot plug add
633                 rc = 1;
634                 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
635                 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
636                 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
637                 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
638
639                 if (rc)
640                         return(rc);
641         } else {
642                 if ((evbuffer[0] != 0) && (!ctrl->push_flag)) 
643                         return 1;
644         }
645
646         return 0;
647 }
648
649         
650 int compaq_nvram_store (void __iomem *rom_start)
651 {
652         int rc = 1;
653
654         if (rom_start == NULL)
655                 return -ENODEV;
656
657         if (evbuffer_init) {
658                 rc = store_HRT(rom_start);
659                 if (rc) {
660                         err(msg_unable_to_save);
661                 }
662         }
663         return rc;
664 }
665