A few cosmetic changes found while making the previous commit.
[xeon-phi-kernel-module] / host / uos_download.c
CommitLineData
800f879a
AT
1/*
2 * Copyright 2010-2017 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * Disclaimer: The codes contained in these modules may be specific to
14 * the Intel Software Development Platform codenamed Knights Ferry,
15 * and the Intel product codenamed Knights Corner, and are not backward
16 * compatible with other Intel products. Additionally, Intel will NOT
17 * support the codes or instruction set in future products.
18 *
19 * Intel offers no warranty of any kind regarding the code. This code is
20 * licensed on an "AS IS" basis and Intel is not obligated to provide
21 * any support, assistance, installation, training, or other services
22 * of any kind. Intel is also not obligated to provide any updates,
23 * enhancements or extensions. Intel specifically disclaims any warranty
24 * of merchantability, non-infringement, fitness for any particular
25 * purpose, and any other warranty.
26 *
27 * Further, Intel disclaims all liability of any kind, including but
28 * not limited to liability for infringement of any proprietary rights,
29 * relating to the use of the code, even if Intel is notified of the
30 * possibility of such liability. Except as expressly stated in an Intel
31 * license agreement provided with this code and agreed upon with Intel,
32 * no license, express or implied, by estoppel or otherwise, to any
33 * intellectual property rights is granted herein.
34 */
35
36/* contains code to download uos on MIC card */
37
38#include "mic_common.h"
39#include <mic/ringbuffer.h>
40#include "micint.h"
41#include <linux/virtio_ring.h>
42#include <linux/virtio_blk.h>
43#include "mic/mic_virtio.h"
44#include <linux/proc_fs.h>
45#include "mic/micveth.h"
46
47
48#define APERTURE_SEGMENT_SIZE ((1) * 1024 * 1024 * 1024ULL)
49
50#define UOS_RESERVE_SIZE_MIN ((128) * 1024 * 1024)
51#define OS_RESERVE_SIZE_MIN ((32) * 1024 * 1024)
52#define UOS_RESERVE_SIZE_MAX (((4) * 1024 * 1024 * 1024ULL) - ((4) * 1024))
53#define UOS_RESERVE_PERCENT 50
54
55#define UOS_WATCHDOG_TIMEOUT 5000 // default watchdog timeout in milliseconds
56
57#define PCIE_CLASS_CODE(x) ((x) >> 24 )
58
59/* zombie class code as per the HAS is 0xFF
60 * but on KNC, we found it as 0x03
61 */
62#define ZOMBIE_CLASS_CODE 0x03
63#define DISABLE_BAR 0x02
64#define RESET_FAILED_F2 12870
65#define RESET_FAILED_F4 13382
66
67void ramoops_remove(mic_ctx_t *mic_ctx);
68
69static struct proc_dir_entry *ramoops_dir;
70struct proc_dir_entry *vmcore_dir;
71
72
73static void adapter_dpc(unsigned long dpc);
74extern int mic_vhost_blk_probe(bd_info_t *bd_info);
75extern void mic_vhost_blk_remove(bd_info_t *bd_info);
76
77/* driver wide global common data */
78mic_data_t mic_data;
79extern int usagemode_param;
80extern bool mic_crash_dump_enabled;
81extern bool mic_watchdog_auto_reboot;
82
83static int64_t etc_comp = 0;
84
85static uint64_t
86etc_read(uint8_t *mmio_va)
87{
88 uint32_t low;
89 uint32_t hi1,hi2;
90
91 do {
92 hi1 = SBOX_READ(mmio_va, SBOX_ELAPSED_TIME_HIGH);
93 low = SBOX_READ(mmio_va, SBOX_ELAPSED_TIME_LOW);
94 hi2 = SBOX_READ(mmio_va, SBOX_ELAPSED_TIME_HIGH);
95 } while(hi1 != hi2);
96
97 return((uint64_t)((((uint64_t)hi1 << 32) | low) >> 5));
98}
99
100static int64_t
101calc_deltaf(mic_ctx_t *mic_ctx)
102{
103 const int64_t ETC_CLK_FREQ = 15625000;
104 const uint32_t TIME_DELAY_IN_SEC = 10;
105 const int64_t etc_cnt1 = ETC_CLK_FREQ * TIME_DELAY_IN_SEC;
106 int64_t etc_cnt2;
107
108 uint64_t cnt1, cnt2;
109 int64_t deltaf_in_ppm, deltaf;
110
111 /*
112 * (etc_freq2 / etc_freq1) = (etc_count2 / etc_count1)
113 * etc_freq1 = ETC_CLK_FREQ
114 * => etc_count1 = TIME_DELAY_IN_SEC * ETC_CLK_FREQ
115 * (etc_freq2 / etc_freq1) = (etc_count2 / etc_count1)
116 * etc_freq2 = etc_freq1 * (etc_count2 / etc_count1)
117 * etc_freq2 - etc_freq1 = etc_freq1((etc_count2 / etc_count1) - 1)
118 * deltaf = etc_freq1(etc_count2 - etc_count1)/etc_count1
119 * deltaf_in_ppm = deltaf * 10 ^ 6 / etc_freq1
120 * deltaf_in_ppm = ((etc_count2 - etc_count1) * 10 ^ 6) / etc_count1
121 */
122 /* Need to implement the monotonic/irqsave logic for windows */
123 unsigned long flags;
124 struct timespec ts1, ts2;
125 int64_t mono_ns;
126 int i = 0;
127 do {
128 local_irq_save(flags);
129 cnt1 = etc_read(mic_ctx->mmio.va);
130 getrawmonotonic(&ts1);
131 local_irq_restore(flags);
132 mdelay(TIME_DELAY_IN_SEC * 1000);
133 local_irq_save(flags);
134 cnt2 = etc_read(mic_ctx->mmio.va);
135 getrawmonotonic(&ts2);
136 local_irq_restore(flags);
137 etc_cnt2 = cnt2 - cnt1;
138 ts2 = timespec_sub(ts2, ts1);
139 mono_ns = timespec_to_ns(&ts2);
140 /* Recalculate etc_cnt2 based on getrawmonotonic */
141 etc_cnt2 = (etc_cnt2 * TIME_DELAY_IN_SEC * 1000 * 1000 * 1000) / mono_ns;
142 deltaf = ( ETC_CLK_FREQ * (etc_cnt2 - etc_cnt1)) / etc_cnt1;
143 deltaf_in_ppm = (1000 * 1000 * (etc_cnt2 - etc_cnt1)) / etc_cnt1;
144 i++;
145 /*
146 * HSD #4844900
147 * On some of the systems deltaf_in_ppm is turning out
148 * way higher than expected. The only reasons I can think of
149 * are:
150 * i) mmio traffic cauing variable delays for mmio read
151 * ii) NMIs affecting this code
152 */
153 } while (i < 10 && (deltaf_in_ppm > 2700 || deltaf_in_ppm < -2700));
154
155 pr_debug("etc deltaf: %lld\n", deltaf);
156 /*
157 * For intel chipsets, Spread Spectrum Clocking (SSC) (in the limit)
158 * is downspread with a frequency of 30hz and an amplitude of 0.5%
159 * which translates to 2500ppm. This is also the ppm observed on KNC + CrownPass
160 * Hence, if ppm > 2500, the code would need to retry to eliminate any chance of error
161 * Added an error margin of 1ppm (etc mmio reads can take really long time)
162 */
163 if (deltaf_in_ppm > 2700 || deltaf_in_ppm < -2700) {
164 printk(KERN_ERR "ETC timer compensation(%lldppm) is much higher"
165 "than expected\n", deltaf_in_ppm);
166 /*
167 * HSD #4844900
168 * Clamp etc compensation to 2500ppm
169 */
170 if (deltaf_in_ppm > 2700)
171 deltaf_in_ppm = 2500;
172 else
173 deltaf_in_ppm = -2500;
174 deltaf = (ETC_CLK_FREQ * deltaf_in_ppm) / (1000 * 1000);
175 }
176 if (deltaf > 0 && deltaf <= 10)
177 deltaf = 0;
178 return deltaf;
179}
180
181void
182calculate_etc_compensation(mic_ctx_t *mic_ctx)
183{
184 if (mic_ctx->bi_family == FAMILY_KNC) {
185 if (!etc_comp)
186 etc_comp = calc_deltaf(mic_ctx);
187 mic_ctx->etc_comp = etc_comp;
188 }
189}
190
191/*
192 DESCRIPTION:: waits for bootstrap loader is finished
193 PARAMETERS::
194 [in]void *mmio_va - virtual address to access MMIO registers
195 RETURN_VALUE:: 0 if successful, non-zero if failure
196*/
197int
198wait_for_bootstrap(uint8_t *mmio_va)
199{
200 uint32_t scratch2 = 0;
201 int count = 0;
202#ifdef MIC_IS_EMULATION
203 int wait_time = 0;
204#endif
205
206 // Wait until the boot loader is finished
207 while (!SCRATCH2_DOWNLOAD_STATUS(scratch2)) {
208 msleep(100);
209 if (count == 600) {
210#ifndef MIC_IS_EMULATION
211 printk("Firmware is not responding with ready bit\n");
212 return -EIO;
213#else
214 /* We don't want to be polling too often on the emulator, it is SLOW! */
215 pr_debug("Wait for bootstrap: %d min(s) \n", wait_time++);
216 count = 0;
217#endif
218 }
219
220 count++;
221 scratch2 = SBOX_READ(mmio_va, SBOX_SCRATCH2);
222 }
223
224 return 0;
225}
226
227/*
228 DESCRIPTION::gets adapter memory size. calculates size based on scratch register 0
229 PARAMETERS::
230 [in]void *mmio_va - virtual address to access MMIO registers
231 [out]uint32_t *adapter_mem_size - adapter memory size
232 RETURN_VALUE:: none
233*/
234void
235get_adapter_memsize(uint8_t *mmio_va, uint32_t *adapter_mem_size)
236{
237 uint32_t memsize = 0;
238 uint32_t scratch0 = {0};
239
240 scratch0 = SBOX_READ(mmio_va, SBOX_SCRATCH0);
241 memsize = SCRATCH0_MEM_SIZE_KB(scratch0) * ((1) * 1024);
242
243 // Adjust the memory size based on the memory usage
244 switch (SCRATCH0_MEM_USAGE(scratch0)) {
245 case SCR0_MEM_ALL:
246 // Do nothing
247 break;
248
249 case SCR0_MEM_HALF:
250 memsize /= 2;
251 break;
252
253 case SCR0_MEM_THIRD:
254 memsize /= 3;
255 break;
256
257 case SCR0_MEM_FOURTH:
258 memsize /= 4;
259 break;
260
261 default:
262 // DBG_ASSERT_MSG(false, "Invalid memory usage specified by the bootstrap.\n");
263 break;
264 }
265
266 *adapter_mem_size = memsize;
267}
268
269/*
270 DESCRIPTION:: gets uos load offset from scratch register 2
271 PARAMETERS::
272 [in]void *mmio_va - virtual address to access MMIO registers
273 [out]uint32_t *uos_load_offset - offset at which uos will be loaded
274 RETURN_VALUE:: none
275*/
276void
277get_uos_loadoffset(uint8_t *mmio_va, uint32_t *uos_load_offset)
278{
279 uint32_t scratch2 = 0;
280
281 scratch2 = SBOX_READ(mmio_va, SBOX_SCRATCH2);
282 *uos_load_offset = SCRATCH2_DOWNLOAD_ADDR(scratch2);
283}
284
285/*
286 DESCRIPTION:: gets reserved size for uos
287 PARAMETERS::
288 [out]uint32_t *uos_reserve_size - reserved uos size
289 RETURN_VALUE:: none
290*/
291void
292get_uos_reserved_size(uint8_t* mmio_va, uint32_t adapter_memsize, uint32_t *uos_reserve_size)
293{
294 uint32_t reserve_size = 0;
295
296 // Only calculate if not explicitly specified by the user
297 reserve_size = (uint32_t)(adapter_memsize * UOS_RESERVE_PERCENT / 100);
298
299 // Make sure there is at least WINDOWS_RESERVE_SIZE_MIN bytes
300 reserve_size = GET_MIN(reserve_size, adapter_memsize - OS_RESERVE_SIZE_MIN);
301
302 // Keep in mind maximum uos reserve size is uint32_t, so we never overflow
303 reserve_size = GET_MIN(reserve_size, UOS_RESERVE_SIZE_MAX);
304 reserve_size = GET_MAX(reserve_size, UOS_RESERVE_SIZE_MIN);
305
306 // Always align uos reserve size to a page
307 reserve_size = (uint32_t)AlignLow(reserve_size, ((4) * 1024));
308
309 *uos_reserve_size = reserve_size;
310}
311
312/*
313 DESCRIPTION:: gets APIC ID from scratch register 2
314 PARAMETERS::
315 [in]void *mmio_va - virtual address to access MMIO registers
316 [out]uint32_t *apic_id - apic id
317 RETURN_VALUE:: none
318*/
319void
320get_apic_id(uint8_t *mmio_va, uint32_t *apic_id)
321{
322 uint32_t scratch2 = 0;
323
324 scratch2 = SBOX_READ(mmio_va, SBOX_SCRATCH2);
325 *apic_id = SCRATCH2_APIC_ID(scratch2);
326}
327
328/*
329 DESCRIPTION::program the PCI aperture as a contiguous window. (only supports upto 4GB memory)
330 PARAMETERS::
331 [in]mic_ctx_t *mic_ctx - mic ctx
332 [in]int gtt_index - beginning gtt entry index
333 [in]uint64_t phy_addr - physical address for PCI aperture
334 [in]uint32_t num_bytes - size of PCI aperture
335 RETURN_VALUE:: None
336 */
337void
338set_pci_aperture(mic_ctx_t *mic_ctx, uint32_t gtt_index, uint64_t phy_addr, uint32_t num_bytes)
339{
340 uint32_t num_pages;
341 uint32_t gtt_entry;
342 uint32_t i;
343
344 num_pages = ALIGN(num_bytes, PAGE_SIZE) >> PAGE_SHIFT;
345
346 for (i = 0; i < num_pages; i++) {
347
348 gtt_entry = ((uint32_t)(phy_addr >> PAGE_SHIFT) + i) << 1 | 0x1u;
349 GTT_WRITE(gtt_entry, mic_ctx->mmio.va, (gtt_index + i)*sizeof(gtt_entry));
350 }
351
352 // XPU_RACE_CONDITION:
353 // Writing GttTlbFlushReg DOES NOT flush all write transactions from SBOX to GDDR
354 // because GttTlbFlushReg is an SBOX register and transaction terminates in SBOX
355 // MMIO write must use MIC ringbus to be serializing.
356 // Writing GTT itself DOES serialize: GTT is in MMIO space, and write goes to the ringbus
357 // MemoryBarrier makes sure all writes make it to GDDR before tlbFlush write
358 smp_mb(); // FIXME: only needs SFENCE
359
360 // write any value to cause a flush
361 SBOX_WRITE(1, mic_ctx->mmio.va, SBOX_TLB_FLUSH);
362}
363
364/*
365 DESCRIPTION:: Programs a scratch register that the bootstrap reads to determine
366 how large is uOS image.
367 PARAMETERS::
368 [in]void *mmio_va - virtual address to mmio register,
369 [in]uint32_t uos_size - size of uos image
370 RETURN_VALUE:: none
371*/
372void
373set_uos_size(uint8_t *mmio_va, uint32_t uos_size)
374{
375 uint32_t scratch5;
376
377 scratch5 = uos_size;
378 // XPU_RACE_CONDITION: write to MMIO space is uncached and flushes WC buffers
379 SBOX_WRITE(scratch5, mmio_va, SBOX_SCRATCH5);
380}
381
382/*
383 DESCRIPTION:: Programs a scratch register that the uOS reads to determine how
384 much memory to reserve.
385 PARAMETERS::
386 [in]void *mmio_va - virtual address to mmio register,
387 [in]uint32_t uos_reserved_size - size of memory to be reserved by uos.
388 RETURN_VALUE:: none
389*/
390void
391set_uos_reserved_size(uint8_t *mmio_va, uint32_t uos_reserved_size)
392{
393 uint32_t scratch3;
394
395 scratch3 = uos_reserved_size;
396 // XPU_RACE_CONDITION: write to MMIO space is uncached and flushes WC buffers
397 SBOX_WRITE(scratch3, mmio_va, SBOX_SCRATCH3);
398}
399
400/*
401 DESCRIPTION:: .
402 PARAMETERS::
403 [in]uint32_t device_id - device ID,
404 RETURN_VALUE:: family type
405*/
406product_family_t
407get_product_family(uint32_t device_id)
408{
409 product_family_t product_family;
410
411 switch (device_id) {
412 case PCI_DEVICE_ABR_2249:
413 case PCI_DEVICE_ABR_224a:
414 product_family = FAMILY_ABR;
415 break;
416
417 case PCI_DEVICE_KNC_2250:
418 case PCI_DEVICE_KNC_2251:
419 case PCI_DEVICE_KNC_2252:
420 case PCI_DEVICE_KNC_2253:
421 case PCI_DEVICE_KNC_2254:
422 case PCI_DEVICE_KNC_2255:
423 case PCI_DEVICE_KNC_2256:
424 case PCI_DEVICE_KNC_2257:
425 case PCI_DEVICE_KNC_2258:
426 case PCI_DEVICE_KNC_2259:
427 case PCI_DEVICE_KNC_225a:
428 case PCI_DEVICE_KNC_225b:
429 case PCI_DEVICE_KNC_225c:
430 case PCI_DEVICE_KNC_225d:
431 case PCI_DEVICE_KNC_225e:
432 product_family = FAMILY_KNC;
433 break;
434
435 default:
436 pr_debug( "Invalid/Unknown device ID %d\r\n", device_id);
437 product_family = FAMILY_UNKNOWN;
438 break;
439 }
440
441 return product_family;
442}
443
444/*
445 DESCRIPTION:: loads uos image at given path into gddr
446 PARAMETERS::
447 [in]mic_ctx_t *mic_ctx - mic context
448 [in]imgname - file path for uos file to be loaded
449 [out]uos_size - size of uos image
450 */
451int
452load_uos_into_gddr(mic_ctx_t *mic_ctx, char *imgname, uint32_t* uos_size, uint64_t *uos_cmd_offset)
453{
454 void *aperture_va;
455 uint8_t *mmio_va;
456 uint32_t apic_id = 0;
457 uint32_t uos_load_offset = 0;
458 uint32_t adapter_memsize = 0;
459 int status = 0;
460
461 aperture_va = mic_ctx->aper.va;
462 mmio_va = mic_ctx->mmio.va;
463
464 if (mic_ctx->state != MIC_BOOT) {
465 printk("Not in booting state\n");
466 return -EPERM;
467 }
468
469 status = mic_get_file_size(imgname, uos_size);
470
471 if (status) {
472 mic_ctx->state = MIC_BOOTFAIL;
473 printk("Linux image not found at %s , status returned %d\n", imgname, status);
474 return status;
475 }
476
477 get_uos_loadoffset(mmio_va, &uos_load_offset);
478 // Determine the uOS reserve size after we have the m_pXpu interface
479 get_adapter_memsize(mmio_va, &adapter_memsize);
480
481 get_apic_id(mmio_va, &apic_id);
482 // store apic_id in adapter context for later use
483 mic_ctx->apic_id = apic_id;
484
485 if (mic_ctx->bi_family == FAMILY_ABR){
486 // Program the PCI aperture as a contiguous window
487 // Need an extra page to provide enough buffer space for command line arguments.
488 set_pci_aperture(mic_ctx, 0, uos_load_offset, *uos_size + PAGE_SIZE);
489 uos_load_offset = 0;
490 }
491
492 // transfer uOs image file to gddr
493 status = mic_load_file(imgname, ((uint8_t*)aperture_va) + uos_load_offset, *uos_size);
494
495 // for the emulator we want to skip "downloading" the file
496 *uos_cmd_offset = (uint64_t)uos_load_offset + *uos_size;
497
498 // This only applies to KNF bootstrap, it is NOT needed for KNC
499 if (mic_ctx->bi_family == FAMILY_ABR) {
500 // clear UOS load offset register after uOS was uploaded
501 SBOX_WRITE(0, mmio_va, SBOX_SCRATCH2);
502 SBOX_READ(mmio_va, SBOX_SCRATCH2);
503 }
504
505 return status;
506}
507
508/*
509 DESCRIPTION:: loads uos initramfs image at given path into gddr for KNC.
510 PARAMETERS::
511 [in]mic_ctx_t *mic_ctx - mic context
512 [in]initramfsname - file path for uos initramfs file to be loaded
513 */
514int
515load_initramfs(mic_ctx_t *mic_ctx, char *initramfsname, uint32_t *initramfs_image, uint32_t *initramfs_size)
516{
517 uint8_t *aperture_va;
518 uint8_t *mmio_va;
519 uint32_t apic_id = 0;
520 uint32_t uos_load_offset = 0;
521 uint32_t file_load_offset = 0;
522 uint32_t adapter_memsize = 0;
523 uint32_t file_size = 0;
524 int status = 0;
525 uint32_t *ramfs_addr_ptr;
526
527 aperture_va = mic_ctx->aper.va;
528 mmio_va = mic_ctx->mmio.va;
529
530 if (mic_ctx->state != MIC_BOOT) {
531 printk("Not in booting state\n");
532 return -EPERM;
533 }
534
535 status = mic_get_file_size(initramfsname, &file_size);
536
537 if (status) {
538 mic_ctx->state = MIC_BOOTFAIL;
539 printk("Init ram disk image not found at %s , status returned %d\n", initramfsname, status);
540 return status;
541 }
542
543 get_uos_loadoffset(mmio_va, &uos_load_offset);
544 file_load_offset = uos_load_offset << 1; /* Place initramfs higher than kernel; 128MB is ok */
545
546 *initramfs_size = file_size;
547 *initramfs_image = file_load_offset;
548
549 // Determine the uOS reserve size after we have the m_pXpu interface
550 get_adapter_memsize(mmio_va, &adapter_memsize);
551 get_apic_id(mmio_va, &apic_id);
552
553 // store apic_id in adapter context for later use
554 mic_ctx->apic_id = apic_id;
555
556 // transfer uOs image file to gddr
557 status = mic_load_file(initramfsname, aperture_va + file_load_offset, file_size);
558
559 // write the initramfs load address and size to the fields in the kernel header
560 ramfs_addr_ptr = (uint32_t *)(aperture_va + uos_load_offset + 0x218);
561 *ramfs_addr_ptr = file_load_offset;
562 ramfs_addr_ptr = (uint32_t *)(aperture_va + uos_load_offset + 0x21c);
563 *ramfs_addr_ptr = *initramfs_size;
564
565 return status;
566}
567
568struct tmpqp {
569 uint64_t ep;
570 uint64_t magic;
571};
572
573int
574load_command_line(mic_ctx_t *mic_ctx, uint64_t uos_cmd_offset)
575{
576 void *cmd_line_va = mic_ctx->aper.va + uos_cmd_offset;
577 uint32_t cmdlen = 0;
578 char *buf = NULL;
579#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)) || defined(RHEL_RELEASE_CODE)
580 struct board_info *bi = mic_ctx->bd_info;
581#endif
582
583#ifdef USE_VCONSOLE
584 micvcons_t *vcons = &mic_ctx->bi_vcons;
585 dma_addr_t vc_hdr_dma_addr = 0;
586#endif
587
588 /*
589 * mic_ctx->boot_mem will also be set in IOCTL to boot the card in restricted memory
590 * FIXME::This code is added to keep the backward compatibility with IOCTLs
591 */
592 if (mic_ctx->bi_family == FAMILY_KNC)
593 if (mic_ctx->boot_mem == 0 || mic_ctx->boot_mem > mic_ctx->aper.len >> 20)
594 mic_ctx->boot_mem = (uint32_t)(mic_ctx->aper.len >> 20);
595 if (!(buf = kzalloc(MIC_CMDLINE_BUFSIZE, GFP_KERNEL))) {
596 printk(KERN_ERR "failed to allocate %d bytes for uOS command line\n",
597 MIC_CMDLINE_BUFSIZE);
598 return -ENOMEM;
599 }
600
601 cmdlen = snprintf(buf, MIC_CMDLINE_BUFSIZE, "card=%d vnet=%s scif_id=%d scif_addr=0x%llx",
602 mic_ctx->bi_id, mic_vnet_modes[mic_vnet_mode],
603 mic_ctx->bi_id + 1, mic_ctx->bi_scif.si_pa);
604
605 if (mic_vnet_mode == VNET_MODE_DMA) {
606 struct micvnet_info *vnet_info = mic_ctx->bi_vethinfo;
607 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
608 " vnet_addr=0x%llx", vnet_info->vi_rp_phys);
609 }
610
611#ifdef USE_VCONSOLE
612 if (vcons->dc_enabled)
613 vc_hdr_dma_addr = vcons->dc_hdr_dma_addr;
614
615 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
616 " vcons_hdr_addr=0x%llx", vc_hdr_dma_addr);
617#endif
618
619#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)) || defined(RHEL_RELEASE_CODE)
620 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen, " virtio_addr=0x%llx",
621 mic_ctx_map_single(mic_ctx, bi->bi_virtio, sizeof(struct vb_shared)));
622#endif
623
624 if (mic_ctx->boot_mem)
625 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
626 " mem=%dM", mic_ctx->boot_mem);
627 mic_ctx->boot_mem = 0;
628
629 if (mic_ctx->ramoops_size)
630 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
631 " ramoops_size=%d ramoops_addr=0x%llx",
632 mic_ctx->ramoops_size, mic_ctx->ramoops_pa[0]);
633
634 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
635 " p2p=%d p2p_proxy=%d", mic_p2p_enable, mic_p2p_proxy_enable);
636
637 if (mic_ctx->bi_family == FAMILY_KNC)
638 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
639 " etc_comp=%lld", mic_ctx->etc_comp);
640
641 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
642 " reg_cache=%d", mic_reg_cache_enable);
643 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
644 " ulimit=%d", mic_ulimit_check);
645 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
646 " huge_page=%d", mic_huge_page_enable);
647 if (mic_crash_dump_enabled)
648 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
649 " crashkernel=1M@80M");
650 /*
651 * Limitations in the Intel Jaketown and Ivytown platforms require SCIF
652 * to proxy P2P DMA read transfers in order to convert them into a P2P DMA
653 * write for better performance. The SCIF module on MIC needs the
654 * numa node the MIC is connected to on the host to make decisions
655 * about whether to proxy P2P DMA reads or not based on whether the two MIC
656 * devices are connected to the same QPI/socket/numa node or not.
657 * The assumption here is that a socket/QPI will have a unique
658 * numa node number.
659 */
660 pr_debug("CPU family = %d, CPU model = %d\n", boot_cpu_data.x86, boot_cpu_data.x86_model);
661
662 if (mic_p2p_proxy_enable && (boot_cpu_data.x86==6) &&
663 (boot_cpu_data.x86_model == 45 || boot_cpu_data.x86_model == 62)) {
664 int numa_node = dev_to_node(&mic_ctx->bi_pdev->dev);
665 if (-1 != numa_node) {
666 if (boot_cpu_data.x86_model == 45)
667 ms_info.mi_proxy_dma_threshold = SCIF_PROXY_DMA_THRESHOLD_JKT;
668 if (boot_cpu_data.x86_model == 62)
669 ms_info.mi_proxy_dma_threshold = SCIF_PROXY_DMA_THRESHOLD_IVT;
670 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
671 " numa_node=%d", numa_node);
672 cmdlen += snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
673 " p2p_proxy_thresh=%lld", ms_info.mi_proxy_dma_threshold);
674 }
675 }
676
677 if (mic_ctx->sysfs_info.cmdline != NULL)
678 snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
679 " %s", mic_ctx->sysfs_info.cmdline);
680 else
681 snprintf(buf + cmdlen, MIC_CMDLINE_BUFSIZE - cmdlen,
682 " hostname=mic%d ipaddr=171.31.%d.2 quiet console=ttyS0,115200n8",
683 mic_ctx->bi_id, mic_ctx->bi_id + 1);
684
685 memcpy_toio(cmd_line_va, buf, strlen(buf) + 1);
686
687 if (mic_ctx->sysfs_info.kernel_cmdline != NULL)
688 kfree(mic_ctx->sysfs_info.kernel_cmdline);
689
690 if ((mic_ctx->sysfs_info.kernel_cmdline = kmalloc(strlen(buf) + 1, GFP_KERNEL)) != NULL)
691 strcpy(mic_ctx->sysfs_info.kernel_cmdline, buf);
692
693 kfree(buf);
694 return 0;
695}
696
697/*
698 DESCRIPTION:: method responsible for programming scratch register with uos image size
699 and notifying bootstrap to start booting uos
700 PARAMETERS::
701 [in]mic_ctx_t *mic_ctx - mic context
702 [in]uint32_t uos_size - size of uos image
703 */
704int
705notify_uosboot(mic_ctx_t *mic_ctx, uint32_t uos_size)
706{
707 int status = 0;
708 uint32_t adapter_memsize = 0;
709 uint32_t uos_reserved_size = 0;
710 uint8_t* mmio_va = mic_ctx->mmio.va;
711
712 // Program the register with uOS image size for bootstrap
713 set_uos_size(mmio_va, uos_size);
714
715 get_adapter_memsize(mmio_va, &adapter_memsize);
716
717 // Program the register to inform the uOS of how much space to reserve
718 get_uos_reserved_size(mmio_va, adapter_memsize, &uos_reserved_size);
719 set_uos_reserved_size(mmio_va, uos_reserved_size);
720
721 mic_send_bootstrap_intr(mic_ctx);
722
723 return status;
724}
725
726/*
727 DESCRIPTION :: boots Linux OS on the card
728 PARAMETERS ::
729 [in]mic_ctx_t *mic_ctx - mic context
730 [in]char *imgname - file path for uos image to be loaded on the card
731 RETURN_VALUE:: 0 if successful, non-zero if failure
732*/
733int
734boot_linux_uos(mic_ctx_t *mic_ctx, char *imgname, char *initramfsname)
735{
736 int status = 0;
737 uint32_t uos_size = 0;
738 uint64_t uos_cmd_offset = 0;
739 uint32_t initramfs_image = 0;
740 uint32_t initramfs_size = 0;
741
742 printk("MIC %d Booting\n", mic_ctx->bi_id);
743
744 if (mic_ctx->state != MIC_BOOT) {
745 printk(KERN_ERR "MIC %d is not in offline mode\n", mic_ctx->bi_id);
746 return -EPERM;
747 }
748
749 //loads uos image at given path into gddr
750 if ((status = load_uos_into_gddr(mic_ctx, imgname, &uos_size, &uos_cmd_offset)) != 0) {
751 printk("Cannot load uos in gddr\n");
752 return status;
753 }
754
755 if (initramfsname && (status = load_initramfs(mic_ctx, initramfsname, &initramfs_image, &initramfs_size)) != 0) {
756 printk("Cannot load initramfs in gddr\n");
757 return status;
758 }
759
760 status = load_command_line(mic_ctx, uos_cmd_offset);
761
762 //program scratch register with uos image size and notify bootstrap
763 status = notify_uosboot(mic_ctx, uos_size);
764
765 return status;
766}
767
768/*
769 DESCRIPTION :: boots Maintenance mode handler on the card
770 PARAMETERS ::
771 [in]mic_ctx_t *mic_ctx - mic context
772 [in]char *imgname - file path for uos image to be loaded on the card
773 RETURN_VALUE:: 0 if successful, non-zero if failure
774*/
775int boot_micdev_app(mic_ctx_t *mic_ctx, char *imgname)
776{
777 int status = 0;
778 uint32_t uos_size = 0;
779 uint8_t *mmio_va = 0;
780 uint64_t uos_cmd_offset = 0;
781 int32_t temp_scratch2 = 0;
782
783 printk("MIC %d Booting\n", mic_ctx->bi_id);
784 mmio_va = mic_ctx->mmio.va;
785 status = load_uos_into_gddr(mic_ctx, imgname, &uos_size, &uos_cmd_offset);
786 if(status) {
787 printk("Cannot load uos in gddr\n");
788 goto exit;
789 }
790
791 temp_scratch2 = SBOX_READ(mmio_va, SBOX_SCRATCH2);
792 /* clear download bit */
793 temp_scratch2 = SCRATCH2_CLEAR_DOWNLOAD_STATUS(temp_scratch2);
794 SBOX_WRITE(temp_scratch2, mmio_va, SBOX_SCRATCH2);
795
796 //program scratch register with uos image size and notify bootstrap
797 status = notify_uosboot(mic_ctx, uos_size);
798 if(status)
799 goto exit;
800 status = wait_for_bootstrap(mmio_va);
801exit:
802 if(status) {
803 mic_setstate(mic_ctx, MIC_BOOTFAIL);
804 } else {
805 mic_setstate(mic_ctx, MIC_ONLINE);
806 mic_ctx->boot_count++;
807 printk("ELF booted succesfully\n");
808 ;
809 }
810 return status;
811}
812
813/* Perform hardware reset of the device */
814void
815reset_timer(unsigned long arg)
816{
817 mic_ctx_t *mic_ctx = (mic_ctx_t *)arg;
818 uint32_t scratch2 = 0;
819 uint32_t postcode = mic_getpostcode(mic_ctx);
820
821 printk("mic%d: Resetting (Post Code %c%c)\n", mic_ctx->bi_id,
822 postcode & 0xff, (postcode >> 8) & 0xff);
823 mic_ctx->reset_count++;
824
825 /* Assuming that the bootstrap takes around 90 seconds to reset,
826 * we fail after 300 seconds, thus allowing 3 attempts to reset
827 */
828 if (mic_ctx->reset_count == RESET_FAIL_TIME ||
829 !postcode || 0xffffffff == postcode || mic_ctx->state == MIC_RESETFAIL) {
830 mic_ctx->reset_count = 0;
831 mic_setstate(mic_ctx, MIC_RESETFAIL);
832 wake_up(&mic_ctx->resetwq);
833 printk("MIC %d RESETFAIL postcode %c%c %d\n", mic_ctx->bi_id,
834 postcode & 0xff, (postcode >> 8) & 0xff, postcode);
835 return;
836 }
837
838 /* check for F2 or F4 error codes from bootstrap */
839 if ((postcode == RESET_FAILED_F2) || (postcode == RESET_FAILED_F4)) {
840 if (mic_ctx->resetworkq) {
841 queue_work(mic_ctx->resetworkq, &mic_ctx->resetwork);
842 } else {
843 mic_ctx->reset_count = 0;
844 mic_setstate(mic_ctx, MIC_RESETFAIL);
845 wake_up(&mic_ctx->resetwq);
846 return;
847 }
848 }
849
850 /* checking if bootstrap is ready or still resetting */
851 scratch2 = SBOX_READ(mic_ctx->mmio.va, SBOX_SCRATCH2);
852 if (SCRATCH2_DOWNLOAD_STATUS(scratch2)) {
853 mic_ctx->boot_start = 0;
854 mic_setstate(mic_ctx, MIC_READY);
855
856 if (mic_ctx->msie)
857 mic_enable_msi_interrupts(mic_ctx);
858 mic_enable_interrupts(mic_ctx);
859 mic_smpt_restore(mic_ctx);
860 micscif_start(mic_ctx);
861
862 wake_up(&mic_ctx->resetwq);
863 mic_ctx->reset_count = 0;
864
865 return;
866 }
867
868 mic_ctx->boot_timer.function = reset_timer;
869 mic_ctx->boot_timer.data = (unsigned long)mic_ctx;
870 mic_ctx->boot_timer.expires = jiffies + HZ;
871
872 add_timer(&mic_ctx->boot_timer);
873}
874
875void
876adapter_wait_reset(mic_ctx_t *mic_ctx)
877{
878 mic_ctx->boot_timer.function = reset_timer;
879 mic_ctx->boot_timer.data = (unsigned long)mic_ctx;
880 mic_ctx->boot_timer.expires = jiffies + HZ;
881 mic_ctx->boot_start = jiffies;
882
883 add_timer(&mic_ctx->boot_timer);
884}
885
886void
887adapter_reset(mic_ctx_t *mic_ctx, int wait_reset, int reattempt)
888{
889 uint32_t resetReg;
890 mutex_lock(&mic_ctx->state_lock);
891 /* TODO: check state for lost node as well once design is done */
892 if ((mic_ctx->state == MIC_RESET || mic_ctx->state == MIC_READY) && (reattempt == 0)) {
893 if (wait_reset == 0) {
894 mic_setstate(mic_ctx, MIC_INVALID);
895 del_timer_sync(&mic_ctx->boot_timer);
896 mutex_unlock(&mic_ctx->state_lock);
897 return;
898 }
899 mutex_unlock(&mic_ctx->state_lock);
900 return;
901 }
902
903 mic_setstate(mic_ctx, MIC_RESET);
904
905 mutex_unlock(&mic_ctx->state_lock);
906
907 del_timer_sync(&mic_ctx->boot_timer);
908
909 //Write 0 to uos download status otherwise we might continue booting
910 //before reset has completed...
911 SBOX_WRITE(0, mic_ctx->mmio.va, SBOX_SCRATCH2);
912
913 // Virtual network link value should be 0 before reset
914 SBOX_WRITE(0, mic_ctx->mmio.va, SBOX_SCRATCH14);
915
916 // Data from Doorbell1 about restart/shutdown should be 0 before reset
917 SBOX_WRITE(0, mic_ctx->mmio.va, SBOX_SDBIC1);
918
919 //This will trigger reset
920 resetReg = SBOX_READ(mic_ctx->mmio.va, SBOX_RGCR);
921 resetReg |= 0x1;
922 SBOX_WRITE(resetReg, mic_ctx->mmio.va, SBOX_RGCR);
923
924 /* At least of KNF it seems we really want to delay at least 1 second */
925 /* after touching reset to prevent a lot of problems. */
926 msleep(1000);
927
928 if (!wait_reset) {
929 return;
930 }
931
932 adapter_wait_reset(mic_ctx);
933
934}
935
936void ramoops_flip(mic_ctx_t *mic_ctx);
937
938int
939adapter_shutdown_device(mic_ctx_t *mic_ctx)
940{
941 ;
942
943 if (micpm_get_reference(mic_ctx, true))
944 return 0;
945
946 mutex_lock(&mic_ctx->state_lock);
947 if (mic_ctx->state == MIC_ONLINE) {
948 mic_setstate(mic_ctx, MIC_SHUTDOWN);
949
950 /*
951 * Writing to SBOX RDMASR0 will generate an interrupt
952 * on the uOS which will initiate orderly shutdown.
953 */
954 mic_send_sht_intr(mic_ctx);
955 }
956 mutex_unlock(&mic_ctx->state_lock);
957
958 micpm_put_reference(mic_ctx);
959 return 0;
960}
961
962int
963adapter_stop_device(mic_ctx_t *mic_ctx, int wait_reset, int reattempt)
964{
965 ;
966
967 micvcons_stop(mic_ctx);
968#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34) || \
969 defined(RHEL_RELEASE_CODE)
970 mic_vhost_blk_stop(mic_ctx->bd_info);
971#endif
972 micveth_stop(mic_ctx);
973
974 micpm_stop(mic_ctx);
975 micscif_stop(mic_ctx);
976 vmcore_remove(mic_ctx);
977 close_dma_device(mic_ctx->bi_id + 1, &mic_ctx->dma_handle);
978 ramoops_flip(mic_ctx);
979
980 /* Calling adapter_reset after issuing Host shutdown/reboot
981 * leads to randon NMIs. These are not rleated to any Card in
982 * specific but occurs on the PCI bridge. */
983 if ((system_state == SYSTEM_POWER_OFF) ||
984 (system_state == SYSTEM_RESTART) ||
985 (system_state == SYSTEM_HALT))
986 return 0;
987 adapter_reset(mic_ctx, wait_reset, reattempt);
988
989 return 0;
990}
991
992static void
993destroy_reset_workqueue(mic_ctx_t *mic_ctx)
994{
995 struct workqueue_struct *tempworkq;
996 tempworkq = mic_ctx->resetworkq;
997 mic_ctx->resetworkq = NULL;
998 destroy_workqueue(tempworkq);
999 del_timer_sync(&mic_ctx->boot_timer);
1000}
1001
1002int
1003adapter_remove(mic_ctx_t *mic_ctx)
1004{
1005
1006#ifdef USE_VCONSOLE
1007 if (mic_ctx->bi_vcons.dc_hdr_virt) {
1008 mic_ctx_unmap_single(mic_ctx, mic_ctx->bi_vcons.dc_hdr_dma_addr,
1009 sizeof(struct vcons_buf));
1010 kfree(mic_ctx->bi_vcons.dc_hdr_virt);
1011 mic_ctx->bi_vcons.dc_hdr_virt = NULL;
1012 }
1013
1014 if (mic_ctx->bi_vcons.dc_buf_virt) {
1015 mic_ctx_unmap_single(mic_ctx, mic_ctx->bi_vcons.dc_dma_addr,
1016 MICVCONS_BUF_SIZE);
1017 free_pages((uint64_t)mic_ctx->bi_vcons.dc_buf_virt, 0);
1018 mic_ctx->bi_vcons.dc_buf_virt = NULL;
1019 }
1020#endif
1021
1022 mic_psmi_uninit(mic_ctx);
1023 micpm_remove(mic_ctx);
1024 micscif_remove(mic_ctx);
1025#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)) || defined(RHEL_RELEASE_CODE)
1026 mic_vhost_blk_remove(mic_ctx->bd_info);
1027#endif
1028 micveth_remove(mic_ctx);
1029 mic_unreg_irqhandler(mic_ctx, 0x1, "MIC SHUTDOWN DoorBell 1");
1030
1031 ramoops_remove(mic_ctx);
1032 vmcore_remove(mic_ctx);
1033 mic_smpt_uninit(mic_ctx);
1034 /* Make sure that no reset timer is running after the workqueue is destroyed */
1035 destroy_reset_workqueue(mic_ctx);
1036
1037 if (mic_ctx->mmio.va) {
1038 iounmap((void *)mic_ctx->mmio.va);
1039 mic_ctx->mmio.va = 0;
1040 }
1041
1042 if (mic_ctx->aper.va) {
1043 iounmap((void *)mic_ctx->aper.va);
1044 mic_ctx->aper.va = 0;
1045 }
1046
1047
1048 return 0;
1049}
1050
1051#define MIC_MAX_BOOT_TIME 180 // Maximum number of seconds to wait for boot to complete
1052
1053static void
1054online_timer(unsigned long arg)
1055{
1056 mic_ctx_t *mic_ctx = (mic_ctx_t *)arg;
1057 uint64_t delay = (jiffies - mic_ctx->boot_start) / HZ;
1058
1059 if (mic_ctx->state == MIC_ONLINE)
1060 return;
1061
1062 if (delay > MIC_MAX_BOOT_TIME) {
1063 printk("Fail booting MIC %d. Wait time execeed %d seconds\n", mic_ctx->bi_id, MIC_MAX_BOOT_TIME);
1064 mic_ctx->state = MIC_BOOTFAIL;
1065 return;
1066 }
1067
1068 mic_ctx->boot_timer.function = online_timer;
1069 mic_ctx->boot_timer.data = (unsigned long)mic_ctx;
1070 mic_ctx->boot_timer.expires = jiffies + HZ;
1071 add_timer(&mic_ctx->boot_timer);
1072
1073 if (!(delay % 5))
1074 printk("Waiting for MIC %d boot %lld\n", mic_ctx->bi_id, delay);
1075}
1076
1077static void
1078boot_timer(unsigned long arg)
1079{
1080 mic_ctx_t *mic_ctx = (mic_ctx_t *)arg;
1081 struct micvnet_info *vnet_info = (struct micvnet_info *) mic_ctx->bi_vethinfo;
1082 uint64_t delay = (jiffies - mic_ctx->boot_start) / HZ;
1083 bool timer_restart = false;
1084
1085 if ((mic_ctx->state != MIC_BOOT) && (mic_ctx->state != MIC_ONLINE)) {
1086 return;
1087 }
1088
1089 if (delay > MIC_MAX_BOOT_TIME) {
1090 printk("Fail booting MIC %d. Wait time execeed %d seconds\n", mic_ctx->bi_id, MIC_MAX_BOOT_TIME);
1091 mic_ctx->state = MIC_BOOTFAIL;
1092 return;
1093 }
1094
1095 if (!(delay % 5))
1096 printk("Waiting for MIC %d boot %lld\n", mic_ctx->bi_id, delay);
1097
1098 if (mic_vnet_mode != VNET_MODE_DMA)
1099 timer_restart = (SBOX_READ(mic_ctx->mmio.va, SBOX_SCRATCH14) == 0)?
1100 true : false;
1101 else if (atomic_read(&vnet_info->vi_state) != MICVNET_STATE_LINKUP)
1102 timer_restart = (mic_ctx->state != MIC_ONLINE)? true: false;
1103
1104 if (timer_restart) {
1105 mic_ctx->boot_timer.function = boot_timer;
1106 mic_ctx->boot_timer.data = (unsigned long)mic_ctx;
1107 mic_ctx->boot_timer.expires = jiffies + HZ;
1108
1109 add_timer(&mic_ctx->boot_timer);
1110 return;
1111 }
1112
1113 mic_ctx->boot_timer.function = online_timer;
1114 mic_ctx->boot_timer.data = (unsigned long)mic_ctx;
1115 mic_ctx->boot_timer.expires = jiffies + HZ;
1116 add_timer(&mic_ctx->boot_timer);
1117
1118 printk("MIC %d Network link is up\n", mic_ctx->bi_id);
1119 schedule_work(&mic_ctx->boot_ws);
1120}
1121
1122void
1123post_boot_startup(struct work_struct *work)
1124{
1125
1126 mic_ctx_t *mic_ctx
1127 = container_of(work, mic_ctx_t, boot_ws);
1128
1129 if (micpm_get_reference(mic_ctx, true) != 0)
1130 return;
1131
1132 // We should only enable DMA after uos is booted
1133 BUG_ON(open_dma_device(mic_ctx->bi_id+1,
1134 mic_ctx->mmio.va + HOST_SBOX_BASE_ADDRESS,
1135 &mic_ctx->dma_handle));
1136 if (micveth_start(mic_ctx))
1137 printk(KERN_ERR "%s: micveth_start failed\n", __FUNCTION__);
1138 micpm_put_reference(mic_ctx);
1139
1140}
1141
1142void
1143attempt_reset(struct work_struct *work)
1144{
1145 mic_ctx_t *mic_ctx
1146 = container_of(work, mic_ctx_t, resetwork);
1147 printk("Reattempting reset after F2/F4 failure\n");
1148 adapter_reset(mic_ctx, RESET_WAIT, RESET_REATTEMPT);
1149}
1150
1151static void
1152ioremap_work(struct work_struct *work)
1153{
1154 mic_ctx_t *mic_ctx
1155 = container_of(work, mic_ctx_t, ioremapwork);
1156 mic_ctx->aper.va = ioremap_wc(mic_ctx->aper.pa, mic_ctx->aper.len);
1157 if (mic_ctx->aper.va == NULL) {
1158 printk(KERN_ERR "mic %d: failed to map aperture space\n", mic_ctx->bi_id);
1159 mutex_lock(&mic_ctx->state_lock);
1160 mic_setstate(mic_ctx, MIC_RESETFAIL);
1161 mutex_unlock(&mic_ctx->state_lock);
1162 }
1163 wake_up(&mic_ctx->ioremapwq);
1164}
1165
1166int
1167adapter_post_boot_device(mic_ctx_t *mic_ctx)
1168{
1169 mic_ctx->boot_timer.function = boot_timer;
1170 mic_ctx->boot_timer.data = (unsigned long)mic_ctx;
1171 mic_ctx->boot_timer.expires = jiffies + HZ;
1172 mic_ctx->boot_start = jiffies;
1173
1174 add_timer(&mic_ctx->boot_timer);
1175 return 0;
1176}
1177
1178int
1179mic_shutdown_host_doorbell_intr_handler(mic_ctx_t *mic_ctx, int doorbell)
1180{
1181 struct micscif_dev *dev = &scif_dev[mic_get_scifnode_id(mic_ctx)];
1182 mic_ctx->sdbic1 = SBOX_READ(mic_ctx->mmio.va, SBOX_SDBIC1);
1183 SBOX_WRITE(0x0, mic_ctx->mmio.va, SBOX_SDBIC1);
1184 if (mic_ctx->sdbic1)
1185 queue_delayed_work(dev->sd_ln_wq,
1186 &dev->sd_watchdog_work, 0);
1187 return 0;
1188}
1189
1190#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
1191static int
1192ramoops_proc_show(struct seq_file *m, void *data)
1193{
1194 uint64_t id = ((uint64_t)data) & 0xffffffff;
1195 uint64_t entry = ((uint64_t)data) >> 32;
1196 struct list_head *pos, *tmpq;
1197 bd_info_t *bd = NULL;
1198 mic_ctx_t *mic_ctx = NULL;
1199 char *record;
1200 char *end;
1201 int size = 0;
1202 int l = 0;
1203 char *output;
1204 unsigned long flags;
1205
1206 list_for_each_safe(pos, tmpq, &mic_data.dd_bdlist) {
1207 bd = list_entry(pos, bd_info_t, bi_list);
1208 mic_ctx = &bd->bi_ctx;
1209 if (mic_ctx->bi_id == id)
1210 break;
1211 }
1212
1213 if (mic_ctx == NULL)
1214 return 0;
1215
1216 spin_lock_irqsave(&mic_ctx->ramoops_lock, flags);
1217
1218 record = mic_ctx->ramoops_va[entry];
1219 if (record == NULL) {
1220 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1221 return -EEXIST;
1222 }
1223
1224 size = mic_ctx->ramoops_size;
1225 end = record + size;
1226
1227 if ((output = kzalloc(size, GFP_ATOMIC)) == NULL) {
1228 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1229 return -ENOMEM;
1230 }
1231
1232 l += scnprintf(output, size, "%s", record);
1233
1234 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1235
1236 seq_printf(m, "%s", output);
1237 return 0;
1238}
1239
1240static int
1241ramoops_proc_open(struct inode *inode, struct file *file)
1242{
1243 return single_open(file, ramoops_proc_show, NULL);
1244}
1245
1246struct file_operations ramoops_proc_fops = {
1247 .open = ramoops_proc_open,
1248 .read = seq_read,
1249 .llseek = seq_lseek,
1250 .release = single_release,
1251};
1252
1253#else // LINUX VERSION
1254static int
1255ramoops_read(char *buf, char **start, off_t offset, int len, int *eof, void *data)
1256{
1257 uint64_t id = ((uint64_t)data) & 0xffffffff;
1258 uint64_t entry = ((uint64_t)data) >> 32;
1259 struct list_head *pos, *tmpq;
1260 bd_info_t *bd = NULL;
1261 mic_ctx_t *mic_ctx = NULL;
1262 char *record;
1263 char *end;
1264 int size = 0;
1265 int l = 0;
1266 int left_to_read;
1267 char *output;
1268 unsigned long flags;
1269
1270 list_for_each_safe(pos, tmpq, &mic_data.dd_bdlist) {
1271 bd = list_entry(pos, bd_info_t, bi_list);
1272 mic_ctx = &bd->bi_ctx;
1273 if (mic_ctx->bi_id == id)
1274 break;
1275 }
1276
1277 if (mic_ctx == NULL)
1278 return 0;
1279
1280 spin_lock_irqsave(&mic_ctx->ramoops_lock, flags);
1281
1282 record = mic_ctx->ramoops_va[entry];
1283 if (record == NULL) {
1284 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1285 *eof = 1;
1286 return 0;
1287 }
1288
1289 size = mic_ctx->ramoops_size;
1290 end = record + size;
1291
1292 if ((output = kzalloc(size, GFP_ATOMIC)) == NULL) {
1293 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1294 return -ENOMEM;
1295 }
1296
1297 l += scnprintf(output, size, "%s", record);
1298
1299 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1300
1301 left_to_read = l - offset;
1302 if (left_to_read < 0)
1303 left_to_read = 0;
1304 if (left_to_read == 0)
1305 *eof = 1;
1306
1307 left_to_read = min(len, left_to_read);
1308 memcpy(buf, output + offset, left_to_read);
1309 kfree(output);
1310 *start = buf;
1311 return left_to_read;
1312}
1313#endif // LINUX VERSION
1314
1315int
1316set_ramoops_pa(mic_ctx_t *mic_ctx)
1317{
1318 if (mic_ctx->ramoops_pa[0] == 0L) {
1319 kfree(mic_ctx->ramoops_va[0]);
1320 mic_ctx->ramoops_size = 0;
1321 mic_ctx->ramoops_va[0] = NULL;
1322 return 1;
1323 }
1324 return 0;
1325}
1326
1327int ramoops_count = 4;
1328
1329void
1330ramoops_probe(mic_ctx_t *mic_ctx)
1331{
1332 char name[64];
1333
1334 mic_ctx->ramoops_size = ramoops_count * PAGE_SIZE;
1335 if ((mic_ctx->ramoops_va[0] = kzalloc(mic_ctx->ramoops_size, GFP_KERNEL)) != NULL) {
1336 spin_lock_init(&mic_ctx->ramoops_lock);
1337 mic_ctx->ramoops_va[1] = NULL;
1338
1339 mic_ctx->ramoops_pa[0] = mic_ctx_map_single(mic_ctx, mic_ctx->ramoops_va[0],
1340 mic_ctx->ramoops_size);
1341 if (set_ramoops_pa(mic_ctx))
1342 return;
1343
1344#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
1345 snprintf(name, 64, "mic%d", mic_ctx->bi_id);
1346 proc_create_data(name, 0444, ramoops_dir, &ramoops_proc_fops,
1347 (void *)(long)mic_ctx->bi_id);
1348
1349 snprintf(name, 64, "mic%d_prev", mic_ctx->bi_id);
1350 proc_create_data(name, 0444, ramoops_dir, &ramoops_proc_fops,
1351 (void *)((long)mic_ctx->bi_id | (1L << 32)));
1352#else // LINUX VERSION
1353 snprintf(name, 64, "mic%d", mic_ctx->bi_id);
1354 if (create_proc_read_entry(name, 0444, ramoops_dir, ramoops_read,
1355 (void *)(long)mic_ctx->bi_id) == NULL)
1356 printk("Failed to intialize /proc/mic_ramoops/%s\n", name);
1357
1358 snprintf(name, 64, "mic%d_prev", mic_ctx->bi_id);
1359 if (create_proc_read_entry(name, 0444, ramoops_dir, ramoops_read,
1360 (void *)((long)mic_ctx->bi_id | (1L << 32))) == NULL)
1361 printk("Failed to intialize /proc/mic_ramoops/%s\n", name);
1362#endif //LINUX VERSION
1363 } else {
1364 mic_ctx->ramoops_size = 0;
1365 }
1366}
1367
1368void
1369ramoops_flip(mic_ctx_t *mic_ctx)
1370{
1371 unsigned long flags;
1372
1373 if (mic_ctx->ramoops_size == 0)
1374 return;
1375
1376 spin_lock_irqsave(&mic_ctx->ramoops_lock, flags);
1377 if (mic_ctx->ramoops_va[1] != NULL) {
1378 mic_ctx_unmap_single(mic_ctx, mic_ctx->ramoops_pa[1], mic_ctx->ramoops_size);
1379 kfree(mic_ctx->ramoops_va[1]);
1380 }
1381
1382 mic_ctx->ramoops_pa[1] = mic_ctx->ramoops_pa[0];
1383 mic_ctx->ramoops_va[1] = mic_ctx->ramoops_va[0];
1384 if ((mic_ctx->ramoops_va[0] = kzalloc(mic_ctx->ramoops_size, GFP_ATOMIC)) != NULL) {
1385 mic_ctx->ramoops_pa[0] = mic_ctx_map_single(mic_ctx, mic_ctx->ramoops_va[0],
1386 mic_ctx->ramoops_size);
1387 set_ramoops_pa(mic_ctx);
1388 }
1389 spin_unlock_irqrestore(&mic_ctx->ramoops_lock, flags);
1390}
1391
1392int
1393adapter_probe(mic_ctx_t *mic_ctx)
1394{
1395 int db;
1396 uint32_t scratch13;
1397 int32_t status = 0;
1398
1399 // Init the irq information
1400 atomic_set(&mic_ctx->bi_irq.mi_received, 0);
1401 spin_lock_init(&mic_ctx->bi_irq.mi_lock);
1402 tasklet_init(&mic_ctx->bi_dpc, adapter_dpc, (unsigned long)&mic_ctx->bi_dpc);
1403
1404 for (db = 0; db < MIC_NUM_DB; db++) {
1405 INIT_LIST_HEAD(&mic_ctx->bi_irq.mi_dblist[db]);
1406 }
1407
1408 if (mic_ctx->msie)
1409 mic_enable_msi_interrupts(mic_ctx);
1410
1411 scratch13 = SBOX_READ(mic_ctx->mmio.va, SBOX_SCRATCH13);
1412 mic_ctx->bi_stepping = SCRATCH13_STEP_ID(scratch13);
1413 mic_ctx->bi_substepping = SCRATCH13_SUB_STEP(scratch13);
1414#ifdef MIC_IS_EMULATION
1415 mic_ctx->bi_platform = PLATFORM_EMULATOR;
1416#else
1417 mic_ctx->bi_platform = SCRATCH13_PLATFORM_ID(scratch13);
1418#endif
1419
1420 mic_enable_interrupts(mic_ctx);
1421 if (micveth_probe(mic_ctx))
1422 printk(KERN_ERR "%s: micveth_probe failed\n", __FUNCTION__);
1423
1424#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)) || defined(RHEL_RELEASE_CODE)
1425 if (mic_vhost_blk_probe(mic_ctx->bd_info))
1426 printk(KERN_ERR "%s: mic_vhost_blk_probe failed\n", __FUNCTION__);
1427#endif
1428 micscif_probe(mic_ctx);
1429 if(micpm_probe(mic_ctx))
1430 printk(KERN_ERR "%s: micpm_probe failed\n", __FUNCTION__);
1431
1432 mic_reg_irqhandler(mic_ctx, 1, "MIC SHUTDOWN DoorBell 1",
1433 mic_shutdown_host_doorbell_intr_handler);
1434
1435 ramoops_probe(mic_ctx);
1436 if (status) {
1437 printk("boot_linux_uos failed \n");
1438 return status;
1439 }
1440
1441 // We should only enable DMA after uos is booted
1442 //mic_dma_lib_init(mic_ctx->mmio.va+HOST_SBOX_BASE_ADDRESS);
1443
1444 return status;
1445}
1446
1447int
1448adapter_start_device(mic_ctx_t *mic_ctx)
1449{
1450 int ret;
1451
1452 mutex_lock(&mic_ctx->state_lock);
1453 if (mic_ctx->state == MIC_READY) {
1454 mic_setstate(mic_ctx, MIC_BOOT);
1455 } else {
1456 mutex_unlock(&mic_ctx->state_lock);
1457 /* TODO: Unknown state handling? */
1458 printk(KERN_ERR "%s %d state %d??\n",
1459 __func__, __LINE__, mic_ctx->state);
1460 ret = -EINVAL;
1461 goto exit;
1462 }
1463 mutex_unlock(&mic_ctx->state_lock);
1464 mic_ctx->mode = MODE_LINUX;
1465 ret = boot_linux_uos(mic_ctx, mic_ctx->image, mic_ctx->initramfs);
1466 if (ret) {
1467 printk(KERN_ERR "boot_linux_uos failed %d\n", ret);
1468 goto exit;
1469 }
1470
1471 ret = adapter_post_boot_device(mic_ctx);
1472 if (ret) {
1473 printk(KERN_ERR "adapter post boot failed %d\n", ret);
1474 goto exit;
1475 }
1476
1477 pr_debug("adapter started successfully\n");
1478exit:
1479 return ret;
1480}
1481
1482int
1483adapter_init_device(mic_ctx_t *mic_ctx)
1484{
1485#ifdef USE_VCONSOLE
1486 struct vcons_buf *vcons_buf;
1487#endif
1488 uint32_t mmio_data_cc; /* mmio data from class code register */
1489 uint32_t mmio_data_bar; /* mmio data from bar enable register */
1490 uint32_t device_id;
1491 int err = 0;
1492
1493 spin_lock_init(&mic_ctx->sysfs_lock);
1494 mic_setstate(mic_ctx, MIC_RESET);
1495 mic_ctx->mode = MODE_NONE;
1496 mic_ctx->reset_count = 0;
1497 mutex_init (&mic_ctx->state_lock);
1498 init_waitqueue_head(&mic_ctx->resetwq);
1499 init_waitqueue_head(&mic_ctx->ioremapwq);
1500 init_timer(&mic_ctx->boot_timer);
1501 if (!(mic_ctx->resetworkq = __mic_create_singlethread_workqueue("RESET WORK")))
1502 return -ENOMEM;
1503 if (!(mic_ctx->ioremapworkq = __mic_create_singlethread_workqueue("IOREMAP_WORK"))) {
1504 err = -EINVAL;
1505 goto destroy_reset_wq;
1506 }
1507 INIT_WORK(&mic_ctx->ioremapwork, ioremap_work);
1508 INIT_WORK(&mic_ctx->boot_ws, post_boot_startup);
1509 INIT_WORK(&mic_ctx->resetwork, attempt_reset);
1510 atomic_set(&mic_ctx->gate_interrupt, 0);
1511
1512 device_id = mic_ctx->bi_pdev->device;
1513 mic_ctx->bi_family = get_product_family(device_id);
1514
1515 if ((mic_ctx->mmio.va = ioremap_nocache(mic_ctx->mmio.pa,
1516 mic_ctx->mmio.len)) == NULL) {
1517 printk("mic %d: failed to map mmio space\n", mic_ctx->bi_id);
1518 err = -ENOMEM;
1519 goto destroy_remap_wq;
1520 }
1521
1522 if (mic_ctx->aper.pa == 0) {
1523 /*
1524 * Read class code from SBOX_PCIE_PCI_REVISION_ID_AND_C_0X8 register
1525 * If the mode is zombie, then
1526 * 1> Aperture is not available
1527 * 2> Register 0x5CD4 is written to 0x00000002 to disable all BARs except MMIO
1528 * 3> Register 0x5808 is written to 0xFF0000XX to set the class ID to a generic PCI device.
1529 */
1530 mmio_data_cc = SBOX_READ(mic_ctx->mmio.va, SBOX_PCIE_PCI_REVISION_ID_AND_C_0X8);
1531 mmio_data_cc = PCIE_CLASS_CODE(mmio_data_cc);
1532 mmio_data_bar = SBOX_READ(mic_ctx->mmio.va, SBOX_PCIE_BAR_ENABLE);
1533
1534 if((mmio_data_cc == ZOMBIE_CLASS_CODE) && (mmio_data_bar == DISABLE_BAR)) {
1535 mic_ctx->card_usage_mode = USAGE_MODE_ZOMBIE;
1536 usagemode_param = USAGE_MODE_ZOMBIE;
1537 } else {
1538 printk("Error: Not in zombie mode and aperture is 0\n");
1539 err = -EINVAL;
1540 goto adap_init_unmapmmio;
1541 }
1542 } else {
1543 if (mic_ctx->ioremapworkq) {
1544 queue_work(mic_ctx->ioremapworkq, &mic_ctx->ioremapwork);
1545 } else {
1546 if ((mic_ctx->aper.va = ioremap_wc(mic_ctx->aper.pa, mic_ctx->aper.len)) == NULL) {
1547 printk("mic %d: failed to map aperture space\n", mic_ctx->bi_id);
1548 err = -EINVAL;
1549 goto adap_init_unmapmmio;
1550 }
1551 }
1552 }
1553
1554 mic_debug_init(mic_ctx);
1555 mic_smpt_init(mic_ctx);
1556#ifdef USE_VCONSOLE
1557 // Allocate memory for PCI serial console
1558 mic_ctx->bi_vcons.dc_buf_virt = (void *)get_zeroed_page(GFP_KERNEL);
1559 mic_ctx->bi_vcons.dc_hdr_virt = kzalloc(sizeof(struct vcons_buf), GFP_KERNEL);
1560
1561 if ((!mic_ctx->bi_vcons.dc_buf_virt) || (!mic_ctx->bi_vcons.dc_hdr_virt)) {
1562 printk(KERN_ERR "mic %d: failed to allocate memory for vcons buffer\n",
1563 mic_ctx->bi_id);
1564 mic_ctx->bi_vcons.dc_enabled = 0;
1565 if (mic_ctx->bi_vcons.dc_buf_virt)
1566 free_pages((uint64_t)mic_ctx->bi_vcons.dc_buf_virt, 0);
1567 if (mic_ctx->bi_vcons.dc_hdr_virt)
1568 kfree(mic_ctx->bi_vcons.dc_hdr_virt);
1569 } else {
1570 mic_ctx->bi_vcons.dc_hdr_dma_addr = mic_ctx_map_single(mic_ctx,
1571 mic_ctx->bi_vcons.dc_hdr_virt,
1572 sizeof(struct vcons_buf));
1573 mic_ctx->bi_vcons.dc_dma_addr = mic_ctx_map_single(mic_ctx,
1574 mic_ctx->bi_vcons.dc_buf_virt,
1575 MICVCONS_BUF_SIZE);
1576 if ((!mic_ctx->bi_vcons.dc_dma_addr) ||
1577 (!mic_ctx->bi_vcons.dc_hdr_dma_addr))
1578 mic_ctx->bi_vcons.dc_enabled = 0;
1579 else
1580 mic_ctx->bi_vcons.dc_enabled = 1;
1581 mic_ctx->bi_vcons.dc_size = MICVCONS_BUF_SIZE;
1582 vcons_buf = (struct vcons_buf *)(mic_ctx->bi_vcons.dc_hdr_virt);
1583 vcons_buf->o_buf_dma_addr = mic_ctx->bi_vcons.dc_dma_addr;
1584 vcons_buf->o_size = MICVCONS_BUF_SIZE;
1585 smp_wmb();
1586 vcons_buf->host_magic = MIC_HOST_VCONS_READY;
1587 vcons_buf->host_rb_ver = micscif_rb_get_version();
1588 }
1589#endif // USE_VCONSOLE
1590 mic_ctx->boot_mem = 0;
1591 mic_psmi_init(mic_ctx);
1592 mic_ctx->dma_handle = NULL;
1593 mic_ctx->sdbic1 = 0;
1594 // To avoid hazard on Windows, sku_build_table is done on DriverEntry
1595 sku_build_table();
1596 device_id = mic_ctx->bi_pdev->device;
1597 sku_find(mic_ctx, device_id);
1598 // To avoid hazard on Windows, sku_destroy_table is done on MicUnload
1599 sku_destroy_table();
1600
1601 /* Determine the amount of compensation that needs to be applied to MIC's ETC timer */
1602 calculate_etc_compensation(mic_ctx);
1603
1604 return 0;
1605
1606adap_init_unmapmmio:
1607 iounmap(mic_ctx->mmio.va);
1608destroy_remap_wq:
1609 destroy_workqueue(mic_ctx->ioremapworkq);
1610destroy_reset_wq:
1611 destroy_workqueue(mic_ctx->resetworkq);
1612 return err;
1613}
1614
1615void
1616mic_enable_interrupts(mic_ctx_t *mic_ctx)
1617{
1618 ENABLE_MIC_INTERRUPTS(mic_ctx->mmio.va);
1619}
1620
1621void
1622mic_disable_interrupts(mic_ctx_t *mic_ctx)
1623{
1624 uint32_t sboxSice0reg;
1625
1626 sboxSice0reg = SBOX_READ(mic_ctx->mmio.va, SBOX_SICE0);
1627 SBOX_WRITE(sboxSice0reg, mic_ctx->mmio.va, SBOX_SICC0);
1628}
1629
1630void
1631mic_enable_msi_interrupts(mic_ctx_t *mic_ctx)
1632{
1633 uint32_t sboxMXARreg;
1634
1635 // Only support single MSI interrupt for now
1636 sboxMXARreg = SBOX_SICE0_DBR_BITS(0xf) | SBOX_SICE0_DMA_BITS(0xff);
1637 if (mic_ctx->bi_family == FAMILY_KNC)
1638 SBOX_WRITE(sboxMXARreg, mic_ctx->mmio.va, SBOX_MXAR0_K1OM);
1639 else
1640 SBOX_WRITE(sboxMXARreg, mic_ctx->mmio.va, SBOX_MXAR0);
1641}
1642
1643int
1644mic_reg_irqhandler(mic_ctx_t *mic_ctx, int doorbell, char *idstring,
1645 int (*irqfunc)(mic_ctx_t *mic_ctx, int doorbell))
1646{
1647 mic_irqhandler_t *irqhandle;
1648 unsigned long flags;
1649
1650 if (doorbell > MIC_IRQ_MAX) {
1651 return EINVAL;
1652 }
1653
1654 if (!(irqhandle = kmalloc(sizeof(mic_irqhandler_t), GFP_ATOMIC)))
1655 goto memerror1;
1656
1657 if (!(irqhandle->ih_idstring = kmalloc(strlen(idstring) + 1, GFP_ATOMIC)))
1658 goto memerror2;
1659
1660 irqhandle->ih_func = irqfunc;
1661 strcpy(irqhandle->ih_idstring, idstring);
1662
1663 spin_lock_irqsave(&mic_ctx->bi_irq.mi_lock, flags);
1664 list_add_tail(&irqhandle->ih_list, &mic_ctx->bi_irq.mi_dblist[doorbell]);
1665 spin_unlock_irqrestore(&mic_ctx->bi_irq.mi_lock, flags);
1666 return 0;
1667
1668memerror2:
1669 kfree(irqhandle);
1670memerror1:
1671 return -ENOMEM;
1672}
1673
1674int
1675mic_unreg_irqhandler(mic_ctx_t *mic_ctx, int doorbell, char *idstring)
1676{
1677 mic_irqhandler_t *irqhandle;
1678 struct list_head *pos, *tmpq;
1679 unsigned long flags;
1680
1681 spin_lock_irqsave(&mic_ctx->bi_irq.mi_lock, flags);
1682 list_for_each_safe(pos, tmpq, &mic_ctx->bi_irq.mi_dblist[doorbell]) {
1683 irqhandle = list_entry(pos, mic_irqhandler_t, ih_list);
1684 if (strcmp(idstring, irqhandle->ih_idstring) == 0) {
1685 list_del(pos);
1686 kfree(irqhandle->ih_idstring);
1687 kfree(irqhandle);
1688 }
1689 }
1690 spin_unlock_irqrestore(&mic_ctx->bi_irq.mi_lock, flags);
1691
1692 return 0;
1693}
1694
1695static __always_inline
1696void adapter_process_one_interrupt(mic_ctx_t *mic_ctx, uint32_t events)
1697{
1698 mic_irqhandler_t *irqhandle;
1699 struct list_head *pos;
1700 int doorbell;
1701
1702 atomic_inc(&mic_ctx->bi_irq.mi_received);
1703
1704 if (SBOX_SICR0_DBR(events)) {
1705 for (doorbell = 0; doorbell < 4; doorbell++) {
1706 if (SBOX_SICR0_DBR(events) & (0x1 << doorbell)) {
1707 spin_lock(&mic_ctx->bi_irq.mi_lock);
1708 list_for_each(pos, &mic_ctx->bi_irq.mi_dblist[doorbell]) {
1709 irqhandle = list_entry(pos, mic_irqhandler_t, ih_list);
1710 irqhandle->ih_func(mic_ctx, doorbell);
1711 }
1712 spin_unlock(&mic_ctx->bi_irq.mi_lock);
1713 }
1714 }
1715
1716 }
1717
1718 if (SBOX_SICR0_DMA(events))
1719 host_dma_interrupt_handler(mic_ctx->dma_handle, events);
1720}
1721
1722int
1723adapter_isr(mic_ctx_t *mic_ctx)
1724{
1725 volatile uint32_t sboxSicr0reg;
1726 if (atomic_cmpxchg(&mic_ctx->gate_interrupt, 0, 1) == 1)
1727 return -1;
1728
1729 sboxSicr0reg = SBOX_READ(mic_ctx->mmio.va, SBOX_SICR0);
1730
1731 if (unlikely(!sboxSicr0reg)) {
1732 // Spurious interrupt
1733 atomic_set(&mic_ctx->gate_interrupt, 0);
1734 return -1;
1735 }
1736
1737 // tell mic that we recived interrupt otherwise it will keep sending them
1738 SBOX_WRITE(sboxSicr0reg, mic_ctx->mmio.va, SBOX_SICR0);
1739
1740 // This only applies to KNC B0
1741 if (FAMILY_KNC == mic_ctx->bi_family &&
1742 mic_ctx->bi_stepping >= KNC_B0_STEP)
1743 mic_enable_interrupts(mic_ctx);
1744
1745 atomic_set(&mic_ctx->gate_interrupt, 0);
1746 adapter_process_one_interrupt(mic_ctx, sboxSicr0reg);
1747 return 0;
1748}
1749
1750int
1751adapter_imsr(mic_ctx_t *mic_ctx)
1752{
1753#if 0 /* TODO: disable interrupt when KNC auto-enable isn't used */
1754 mic_disable_interrupts(mic_ctx);
1755#endif
1756 tasklet_schedule(&mic_ctx->bi_dpc);
1757 return 0;
1758}
1759
1760static void adapter_dpc(unsigned long dpc)
1761{
1762 mic_ctx_t *mic_ctx =
1763 container_of((struct tasklet_struct *)dpc, mic_ctx_t, bi_dpc);
1764
1765 volatile uint32_t sboxSicr0reg;
1766
1767 if (atomic_cmpxchg(&mic_ctx->gate_interrupt, 0, 1) == 1)
1768 return;
1769
1770 /* Clear pending bit array */
1771 if (FAMILY_KNC == mic_ctx->bi_family) {
1772 if (KNC_A_STEP == mic_ctx->bi_stepping)
1773 SBOX_WRITE(1, mic_ctx->mmio.va, SBOX_MSIXPBACR_K1OM);
1774 } else
1775 SBOX_WRITE(1, mic_ctx->mmio.va, SBOX_MSIXPBACR);
1776
1777 sboxSicr0reg = SBOX_READ(mic_ctx->mmio.va, SBOX_SICR0);
1778 if (unlikely(!sboxSicr0reg)) {
1779 atomic_set(&mic_ctx->gate_interrupt, 0);
1780 return;
1781 }
1782
1783 SBOX_WRITE(sboxSicr0reg, mic_ctx->mmio.va, SBOX_SICR0);
1784
1785 // This only applies to KNC B0
1786 if (FAMILY_KNC == mic_ctx->bi_family &&
1787 mic_ctx->bi_stepping >= KNC_B0_STEP)
1788 mic_enable_interrupts(mic_ctx);
1789
1790 atomic_set(&mic_ctx->gate_interrupt, 0);
1791 adapter_process_one_interrupt(mic_ctx, sboxSicr0reg);
1792}
1793
1794void ramoops_init(void)
1795{
1796#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
1797 ramoops_dir = proc_mkdir("mic_ramoops", NULL);
1798#else
1799 ramoops_dir = create_proc_entry("mic_ramoops", S_IFDIR | S_IRUGO, NULL);
1800#endif
1801}
1802
1803void ramoops_exit(void)
1804{
1805 remove_proc_entry("mic_ramoops", NULL);
1806}
1807
1808void ramoops_remove(mic_ctx_t *mic_ctx)
1809{
1810 char name[64];
1811 int i;
1812
1813 snprintf(name, 64, "mic%d", mic_ctx->bi_id);
1814 remove_proc_entry(name, ramoops_dir);
1815
1816 snprintf(name, 64, "mic%d_prev", mic_ctx->bi_id);
1817 remove_proc_entry(name, ramoops_dir);
1818 if (mic_ctx->ramoops_size == 0)
1819 return;
1820
1821 for (i = 0; i < 2; i++) {
1822 if (mic_ctx->ramoops_va[i] != NULL) {
1823 mic_ctx_unmap_single(mic_ctx, mic_ctx->ramoops_pa[i],
1824 mic_ctx->ramoops_size);
1825 kfree(mic_ctx->ramoops_va[i]);
1826 }
1827 }
1828}
1829
1830void vmcore_init(void)
1831{
1832#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0))
1833 vmcore_dir = proc_mkdir("mic_vmcore", NULL);
1834#else
1835 vmcore_dir = create_proc_entry("mic_vmcore", S_IFDIR | S_IRUGO, NULL);
1836#endif
1837}
1838
1839void vmcore_exit(void)
1840{
1841 if (vmcore_dir) {
1842 remove_proc_entry("mic_vmcore", NULL);
1843 vmcore_dir = NULL;
1844 }
1845}
1846
1847void vmcore_remove(mic_ctx_t *mic_ctx)
1848{
1849 char name[64];
1850
1851 snprintf(name, 64, "mic%d", mic_ctx->bi_id);
1852 if (mic_ctx->vmcore_dir) {
1853 remove_proc_entry(name, vmcore_dir);
1854 mic_ctx->vmcore_dir = NULL;
1855 }
1856 if (mic_ctx->elfcorebuf) {
1857 kfree(mic_ctx->elfcorebuf);
1858 mic_ctx->elfcorebuf = NULL;
1859 mic_ctx->elfcorebuf_sz = 0;
1860 mic_ctx->vmcore_size = 0;
1861 }
1862}
1863
1864
1865void
1866adapter_init(void)
1867{
1868 // Per driver init ONLY.
1869 mic_dma_init();
1870 micscif_init();
1871 micpm_init();
1872 ramoops_init();
1873 vmcore_init();
1874 INIT_LIST_HEAD(&mic_data.dd_bdlist);
1875}
1876
1877
1878void show_stepping_comm(mic_ctx_t *mic_ctx,char *buf)
1879{
1880#define STEPINGSTRSIZE 3
1881 char string[STEPINGSTRSIZE];
1882 switch (mic_ctx->bi_family) {
1883 case FAMILY_ABR:
1884 switch (mic_ctx->bi_stepping) {
1885 case 0:
1886 string[0] = 'A';
1887 string[1] = mic_ctx->bi_substepping + '0';
1888 break;
1889 case 2:
1890 string[0] = 'B';
1891 string[1] = '0';
1892 break;
1893 case 3:
1894 string[0] = 'B';
1895 string[1] = '1';
1896 break;
1897 case 4:
1898 string[0] = 'C';
1899 string[1] = '0';
1900 break;
1901 case 5:
1902 string[0] = 'C';
1903 string[1] = '1';
1904 break;
1905 case 6:
1906 string[0] = 'D';
1907 string[1] = '0';
1908 break;
1909 default:
1910 string[0] = '?';
1911 string[1] = '?';
1912 break;
1913 }
1914 break;
1915 case FAMILY_KNC:
1916 switch (mic_ctx->bi_stepping) {
1917 case KNC_A_STEP:
1918 string[0] = 'A';
1919 string[1] = '0';
1920 break;
1921 case KNC_B0_STEP:
1922 string[0] = 'B';
1923 string[1] = '0';
1924 break;
1925 case KNC_B1_STEP:
1926 string[0] = 'B';
1927 string[1] = '1';
1928 break;
1929 case KNC_C_STEP:
1930 string[0] = 'C';
1931 string[1] = '0';
1932 break;
1933 default:
1934 string[0] = '?';
1935 string[1] = '?';
1936 break;
1937 }
1938 break;
1939 default:
1940 string[0] = '?';
1941 string[1] = '?';
1942 break;
1943 }
1944
1945 string[2] = '\0';
1946
1947 strncpy(buf,string,STEPINGSTRSIZE);
1948}
1949
1950