summaryrefslogtreecommitdiffstats
path: root/sys/pci/dpt_pci.c
blob: f171c8a983ee86cb9951a3d193f6cfda78a67548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
381
382
383
384
385
386
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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 *       Copyright (c) 1997 by Simon Shapiro
 *       All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/*
 *  dptpci.c:  Pseudo device drivers for DPT on PCI on FreeBSD 
 *
 *  caveats:   We may need an eisa and an isa files too
 */

#ident "$Id: dpt_pci.c,v 1.6 1998/06/02 00:32:38 eivind Exp $"

#include "opt_devfs.h"
#include "opt_dpt.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/kernel.h>

#include <scsi/scsiconf.h>

#include <pci/pcireg.h>
#include <pci/pcivar.h>

#include <sys/dpt.h>
#include <pci/dpt_pci.h>

#include <vm/vm.h>
#include <vm/pmap.h>

#define PCI_BASEADR0  PCI_MAP_REG_START  /* I/O Address */
#define PCI_BASEADR1  PCI_MAP_REG_START + 4  /* Mem I/O Address */

#define ISA_PRIMARY_WD_ADDRESS    0x1f8

/* Global variables */

/* Function Prototypes */

static char    *dpt_pci_probe(pcici_t tag, pcidi_t type);
static void     dpt_pci_attach(pcici_t config_id, int unit);
static int      dpt_pci_shutdown(int foo, int bar);

extern struct cdevsw dpt_cdevsw;

static  struct pci_device dpt_pci_driver =
{
    "dpt",
    dpt_pci_probe,
    dpt_pci_attach,
    &dpt_unit,
    dpt_pci_shutdown
};

DATA_SET(pcidevice_set, dpt_pci_driver);

/*
 * Probe the PCI device.
 * Some of this work will have to be duplicated in _attach
 * because we do not know for sure how the two relate.
 */

static char *
dpt_pci_probe(pcici_t tag, pcidi_t type)
{
    static char silly_message[64];
    static int  already_announced = 0;

    u_int32_t  dpt_id;
    u_int32_t command;
    u_int32_t class;

#define pci_device  tag.cfg2.port
#define pci_bus     tag.cfg2.forward
#define pci_index   tag.cfg2.enable

#ifndef PCI_COMMAND_MASTER_ENABLE
#define PCI_COMMAND_MASTER_ENABLE 0x00000004
#endif

#ifndef PCI_SUBCLASS_MASS_STORAGE_SCSI
#define PCI_SUBCLASS_MASS_STORAGE_SCSI 0x00000000
#endif

    if ( bootverbose && !already_announced ) {
	printf("DPT:  PCI SCSI HBA Driver, version %d.%d.%d\n",
	       DPT_RELEASE, DPT_VERSION, DPT_PATCH);
	++already_announced;
    }

    if ((dpt_id = (type & 0xffff0000) >> 16) == DPT_DEVICE_ID) {
	/* This one appears to belong to us, but what is it? */
	class = pci_conf_read(tag, PCI_CLASS_REG);
	if (((class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE) &&
	    ((class & PCI_SUBCLASS_MASK) == PCI_SUBCLASS_MASS_STORAGE_SCSI) ) {
	    /* It is a SCSI storage device.  How do talk to it? */
	    command = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
#ifdef DPT_ALLOW_MEMIO
	    if ( ((command & PCI_COMMAND_IO_ENABLE) == 0)
		 && ((command & PCI_COMMAND_MEM_ENABLE) == 0) )
#else
	    if ( ((command & PCI_COMMAND_IO_ENABLE) == 0) )
#endif /* DPT_ALLOW_MEMIO */
		{
		    printf("DPT:  Cannot map the controller registers :-(\n");
		    return(NULL);
		}
	} else {
	    printf("DPT:  Device is not Mass Storage, nor SCSI controller\n");
	    return(NULL);
	}

	command = pci_conf_read(tag, PCI_COMMAND_STATUS_REG);
	if ( (command & PCI_COMMAND_MASTER_ENABLE) == 0 ) {
	    printf("DPT:  Cannot be functional without BUSMASTER. :-(\n");
	    return (NULL);
	}

#ifdef DPT_DEBUG_PCI
	printf("DPT:  Controller is %s mapable\n",
	       (command & PCI_COMMAND_MEM_ENABLE)
	       ? "MEMORY"
	       : ((command & PCI_COMMAND_IO_ENABLE)
		  ? "I/O"
		  : "NOT"));
#endif
	return ("DPT Caching SCSI RAID Controller");
    } 

#if defined(DPT_DEBUG_PCI) && defined(DPT_DEBUG_WARN)
    printf("DPT:  Unknown Controller Type %x Found\n", dpt_id);
    printf("     (class = %x, command = %x\n", class, command);
#endif
    return (NULL);
}

static void
dpt_pci_attach(pcici_t config_id, int unit)
{
    int          ospl;
    int          result;
    int          ndx;

    vm_offset_t  vaddr;
    vm_offset_t  paddr;
    u_int16_t    io_base;
    u_int32_t    command;
    u_int32_t    data;
    dpt_conf_t  *config;
    dpt_softc_t *dpt;

    if (dpt_controllers_present >= DPT_MAX_ADAPTERS) {
	  printf("dpt%d: More than %d Adapters found!  Adapter rejected\n",
			 unit, DPT_MAX_ADAPTERS);
	  return;
    }

    if ((dpt = (dpt_softc_t *) malloc(sizeof(dpt_softc_t), M_DEVBUF, M_NOWAIT))
		== NULL) {
	  printf("dpt%d: Failed to allocate %d bytes for a DPT softc\n",
			 unit, sizeof(dpt_softc_t));
	  return;
    }

    /*
     * Initialize the queues.  See dpt.h for details. We do this here,
     * as we may get hit with interrupts at any moment and we want to
     * have a minimal structure in place to handle them. We also want to
     * register interrupts correctly. To do so, we need a valid dpt
     * structure. To have that, we need this  minimal setup here.
     */
    bzero(dpt, sizeof(dpt_softc_t));
    
    TAILQ_INIT(&dpt->free_ccbs);
    TAILQ_INIT(&dpt->waiting_ccbs);
    TAILQ_INIT(&dpt->submitted_ccbs);
    TAILQ_INIT(&dpt->completed_ccbs);

    if (TAILQ_EMPTY(&dpt_softc_list)) {
	  TAILQ_INIT(&dpt_softc_list);
    }

    TAILQ_INSERT_TAIL(&dpt_softc_list, dpt, links);
    dpt->queue_status       = DPT_QUEUES_NONE_ACTIVE;
    dpt->commands_processed = 0;

#ifdef DPT_MEASURE_PERFORMANCE
    dpt_reset_performance(dpt);
#endif        /* DPT_MEASURE_PERFORMANCE */

    dpt->unit = unit;
    dpt->handle_interrupts = 0;  /* 
								  * Do not set to 1 until all
								  * initialization is done 
								  */
    dpt->v_membase = NULL;
    dpt->p_membase = NULL;
    io_base = 0;
    vaddr   = 0;
    paddr   = 0;
    command = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);

#ifdef DPT_ALLOW_MEMIO
    if ( (command & PCI_COMMAND_MEM_ENABLE) == 0 ) {
#ifdef DPT_DEBUG_PCI
	  printf("dpt%d: Cannot be memory mapped\n", unit);
#endif
	force_io:
	  if ((command & PCI_COMMAND_IO_ENABLE) == 0 ) {
	    printf("dpt%d: Cannot be I/O mapped either :-(\n", unit);
	    free(dpt, M_DEVBUF);
	    return;
	  } else {
	    data = pci_conf_read(config_id, PCI_MAP_REG_START);
	    if ( pci_map_port(config_id, PCI_MAP_REG_START, &io_base) == 0 ) {
#ifdef DPT_DEBUG_ERROR
		  printf("dpt%d: Failed to map as I/O :-(\n", unit);
#endif
		  free(dpt, M_DEVBUF);
		  return;
	    } else {
		  dpt->io_base = io_base + 0x10;
#ifdef DPT_DEBUG_PCI
		  printf("dpt%d: Mapped registers to I/O space, "
				 "starting at %x\n",
				 dpt->unit, dpt->io_base);
#endif
	    }
	  }
    } else {
	  if ( pci_map_mem(config_id, PCI_MAP_REG_START + 4, &vaddr,
					   &paddr) == 0 ) {
#ifdef DPT_DEBUG_ERROR
	    printf("dpt%d: Failed to map as MEMORY.\n"
			   "  Attemting to force I/O mapping\n", unit);
#endif
	    goto force_io;
	  } else {
	    dpt->v_membase = (volatile u_int8_t *)(vaddr + 0x10);
	    dpt->p_membase = (volatile u_int8_t *)(paddr + 0x10);
#ifdef DPT_DEBUG_PCI
	    printf("dpt%d: Mapped registers to MEMORY space, "
			   "starting at %x/%x\n",
			   dpt->unit, dpt->v_membase, dpt->p_membase);
#endif
	  }
    }

#else /* !DPT_ALLOW_MEMIO */
    data = pci_conf_read(config_id, PCI_MAP_REG_START);
    if ((command & PCI_COMMAND_IO_ENABLE) == 0 ) {
	  printf("dpt%d: Registers cannot be I/O mapped :-(\n", unit);
	  free(dpt, M_DEVBUF);
	  return;
    } else {
	  if ( pci_map_port(config_id, PCI_MAP_REG_START, &io_base) == 0 ) {
#ifdef DPT_DEBUG_ERROR
	    printf("dpt%d: Failed to map registers as I/O :-(\n", unit);
#endif
	    free(dpt, M_DEVBUF);
	    return;
	  } else {
	    dpt->io_base = io_base + 0x10;
#ifdef DPT_DEBUG_PCI
	    printf("dpt%d: Mapped registers to I/O space, starting at %x\n",
			   dpt->unit, dpt->io_base);
#endif
	  }
    }
#endif /* !DPT_ALLOW_MEMIO */
  
    if (pci_map_int(config_id, dpt_intr, (void *)dpt, &cam_imask) == 0) {
#ifdef DPT_DEBUG_WARN
	  printf("dpt%d: Failed to map interrupt :-(\n", unit);
#endif
	  free(dpt, M_DEVBUF);
	  return;
    }

    /* If the DPT is mapped as an IDE controller, let it be IDE controller */
    if (io_base == (ISA_PRIMARY_WD_ADDRESS)) {
#ifdef DPT_DEBUG_WARN
	  printf("dpt%d: Mapped as an IDE controller.  "
			 "Disabling SCSI setup\n", unit);
#endif
	  free(dpt, M_DEVBUF);
	  return;
    } else {
	  if ((config = dpt_get_conf(dpt, 0xc1, 7,
								 sizeof(dpt_conf_t), 1)) == NULL) {
#ifdef DPT_DEBUG_ERROR
	    printf("dpt%d: Failed to get board configuration (%x)\n",
			   unit, BaseRegister(dpt));
#endif
	    free(dpt, M_DEVBUF);
	    return;
	  }
    }

    dpt->max_id      = config->MAX_ID;
    dpt->max_lun     = config->MAX_LUN;
    dpt->irq         = config->IRQ;
    dpt->channels    = config->MAX_CHAN;
    dpt->dma_channel = (8 - config->DMA_channel) & 7;

#ifdef DPT_DEBUG_SETUP
    printf("dpt%d: max_id = %d, max_chan = %d, max_lun = %d\n",
		   dpt->unit, dpt->max_id, dpt->channels, dpt->max_lun);
#endif

    if (result = dpt_setup(dpt, config)) {
	  free(config, M_TEMP);
	  free(dpt, M_DEVBUF);
	  printf("dpt%d: dpt_setup failed (%d).  Driver Disabled :-(\n",
			 dpt->unit, result);
    } else {
	  /* clean up the informational data, and display */
	  char clean_vendor[9];
	  char clean_model[17];
	  char clean_firmware[5];
	  char clean_protocol[5];
	  char clean_other[7];

	  int     ndx;

	  strncpy(clean_other, dpt->board_data.otherData, 8);
	  clean_other[6] = '\0';
	  for (ndx = 5; ndx >= 0; ndx--) {
	    if (clean_other[ndx] == ' ')
		  clean_other[ndx] = '\0';
	    else
		  break;
	  }
	  strncpy(dpt->board_data.otherData, clean_other, 6);

	  strncpy(clean_vendor, dpt->board_data.vendor, 8);
	  clean_vendor[8] = '\0';
	  for (ndx = 7; ndx >= 0; ndx--) {
	    if (clean_vendor[ndx] == ' ')
		  clean_vendor[ndx] = '\0';
	    else
		  break;
	  }
	  strncpy(dpt->board_data.vendor, clean_vendor, 8);

	  strncpy(clean_model, dpt->board_data.modelNum, 16);
	  clean_model[16] = '\0';
	  for (ndx = 15; ndx >= 0; ndx--) {
	    if (clean_model[ndx] == ' ')
		  clean_model[ndx] = '\0';
	    else
		  break;
	  }
	  strncpy(dpt->board_data.modelNum, clean_model, 16);

	  strncpy(clean_firmware, dpt->board_data.firmware, 4);
	  clean_firmware[4] = '\0';
	  for (ndx = 3; ndx >= 0; ndx--) {
	    if (clean_firmware[ndx] == ' ')
		  clean_firmware[ndx] = '\0';
	    else
		  break;
	  }
	  strncpy(dpt->board_data.firmware, clean_firmware, 4);

	  strncpy(clean_protocol, dpt->board_data.protocol, 4);
	  clean_protocol[4] = '\0';
	  for (ndx = 3; ndx >= 0; ndx--) {
	    if (clean_protocol[ndx] == ' ')
		  clean_protocol[ndx] = '\0';
	    else
		  break;
	  }
	  strncpy(dpt->board_data.protocol, clean_protocol, 4);

	  dpt_detect_cache(dpt);

	  printf("dpt%d: %s type %x, model %s firmware %s, Protocol %s \n"
			 "      on port %x with %s cache.  LED = %s\n",
			 dpt->unit, clean_vendor, dpt->board_data.deviceType,
			 clean_model, clean_firmware, clean_protocol, dpt->io_base,
			 (dpt->cache_type == DPT_NO_CACHE)
			 ? "Disabled"
			 : (dpt->cache_type == DPT_CACHE_WRITETHROUGH)
			 ? "Write-Through"
			 : "Write-Back",
			 i2bin(dpt_blinking_led(dpt), 8));
	  printf("dpt%d: Enabled Options:\n", dpt->unit);
#ifdef DPT_LOST_IRQ
	  printf("      Recover Lost Interrupts\n");
#endif
#ifdef DPT_VERIFY_HINTR
	  printf("      Verify Lost Transactions\n");
#endif
#ifdef DPT_RESTRICTED_FREELIST
	  printf("      Restrict the Freelist Size\n");
#endif
#ifdef DPT_MEASURE_PERFORMANCE
	  printf("      Collect Metrics\n");
#endif
#ifdef DPT_FREELIST_IS_STACK
	  printf("      Optimize CPU Cache\n");
#endif
#ifdef DPT_HANDLE_TIMEOUTS
	  printf("      Handle Timeouts\n");
#endif
#ifdef DPT_ALLOW_MEMIO
	  printf("      Allow I/O to be Memeory Mapped\n");
#endif
#ifdef DPT_HINTR_CHECK_SOFTC
	  printf("      Validate SoftC at Interrupt\n");
#endif

	  /* register shutdown handlers */
	  result = at_shutdown((bootlist_fn)dpt_shutdown, (void *)dpt,
						   SHUTDOWN_POST_SYNC);
	  switch ( result ) {
	  case 0:
#ifdef DPT_DEBUG_SHUTDOWN
		printf("dpt%d: Shutdown handler registered\n", dpt->unit);
#endif
		break;
	  default:
#ifdef DPT_DEBUG_WARN
		printf("dpt%d: Failed to register shutdown handler (%d)\n",
			   dpt->unit, result);
#endif
		break;
	  }

	  /* Attach SCSI devices */
	  dpt_attach(dpt);
	  ++dpt_controllers_present;

	  /*
	   * Now we create the DEVFS entry.
	   * This would be normally done from dpt_control.c,
	   * But since it appears to be called before we do here,
	   * We never get the entries made.
       */
#ifdef DEVFS
	  (void) devfs_add_devswf(&dpt_cdevsw, dpt->unit, DV_CHR,
				   			  UID_ROOT, GID_WHEEL, 0600,
				   			  "dpt%d", dpt->unit);
	  (void) devfs_add_devswf(&dpt_cdevsw, dpt->unit | SCSI_CONTROL_MASK,
							  DV_CHR,
							  UID_ROOT, GID_WHEEL, 0600,
							  "dpt%d.ctl", dpt->unit);
#endif
    }
}

static int
dpt_pci_shutdown(int foo, int bar)
{
#ifdef DPT_DEBUG_WARN
    printf("dpt_pci_shutdown(%x, %x)\n", foo, bar);
#endif
    return (0);
}

/* End of the DPT PCI part of the driver */

/*
 * Hello emacs, these are the
 * Local Variables:
 *  c-indent-level:               8
 *  c-continued-statement-offset: 8
 *  c-continued-brace-offset:     0
 *  c-brace-offset:              -8
 *  c-brace-imaginary-offset:     0
 *  c-argdecl-indent:             8
 *  c-label-offset:              -8
 *  c++-hanging-braces:           1
 *  c++-access-specifier-offset: -8
 *  c++-empty-arglist-indent:     8
 *  c++-friend-offset:            0
 * End:
 */
OpenPOWER on IntegriCloud