summaryrefslogtreecommitdiff
path: root/minix/lib/libbdev/driver.c
blob: 6c0a3deaec19ab08c614439251921e3d8cdd84d2 (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
/* libbdev - driver endpoint management */

#include <minix/drivers.h>
#include <minix/bdev.h>
#include <minix/ds.h>
#include <assert.h>

#include "const.h"
#include "type.h"
#include "proto.h"

static struct {
  endpoint_t endpt;
  char label[DS_MAX_KEYLEN];
} driver_tab[NR_DEVICES];

void bdev_driver_init(void)
{
/* Initialize the driver table.
 */
  int i;

  for (i = 0; i < NR_DEVICES; i++) {
	driver_tab[i].endpt = NONE;
	driver_tab[i].label[0] = '\0';
  }
}

void bdev_driver_clear(dev_t dev)
{
/* Clear information about a driver.
 */
  int major;

  major = major(dev);

  assert(major >= 0 && major < NR_DEVICES);

  driver_tab[major].endpt = NONE;
  driver_tab[major].label[0] = '\0';
}

endpoint_t bdev_driver_set(dev_t dev, char *label)
{
/* Set the label for a driver, and retrieve the associated endpoint.
 */
  int major;

  major = major(dev);

  assert(major >= 0 && major < NR_DEVICES);
  assert(strlen(label) < sizeof(driver_tab[major].label));

  strlcpy(driver_tab[major].label, label, sizeof(driver_tab[major].label));

  driver_tab[major].endpt = NONE;

  return bdev_driver_update(dev);
}

endpoint_t bdev_driver_get(dev_t dev)
{
/* Return the endpoint for a driver, or NONE if we do not know its endpoint.
 */
  int major;

  major = major(dev);

  assert(major >= 0 && major < NR_DEVICES);

  return driver_tab[major].endpt;
}

endpoint_t bdev_driver_update(dev_t dev)
{
/* Update the endpoint of a driver. The caller of this function already knows
 * that the current endpoint may no longer be valid, and must be updated.
 * Return the new endpoint upon success, and NONE otherwise.
 */
  endpoint_t endpt;
  int r, major, nr_tries;

  major = major(dev);

  assert(major >= 0 && major < NR_DEVICES);
  assert(driver_tab[major].label[0] != '\0');

  /* Repeatedly retrieve the endpoint for the driver label, and see if it is a
   * different, valid endpoint. If retrieval fails at first, we have to wait.
   * We use polling, as opposed to a DS subscription, for a number of reasons:
   * 1) DS supports only one subscription per process, and our main program may
   *    already have a subscription;
   * 2) if we block on receiving a notification from DS, we cannot impose an
   *    upper bound on the retry time;
   * 3) temporarily subscribing and then unsubscribing may cause leftover DS
   *    notifications, which the main program would then have to deal with.
   *    As of writing, unsubscribing from DS is not possible at all, anyway.
   *
   * In the normal case, the driver's label/endpoint mapping entry disappears
   * completely for a short moment, before being replaced with the new mapping.
   * Hence, failure to retrieve the entry at all does not constitute permanent
   * failure. In fact, there is no way to determine reliably that a driver has
   * failed permanently in the current approach. For this we simply rely on the
   * retry limit.
   */
  for (nr_tries = 0; nr_tries < DS_NR_TRIES; nr_tries++) {
	r = ds_retrieve_label_endpt(driver_tab[major].label, &endpt);

	if (r == OK && endpt != NONE && endpt != driver_tab[major].endpt) {
		driver_tab[major].endpt = endpt;

		return endpt;
	}

	if (nr_tries < DS_NR_TRIES - 1)
		micro_delay(DS_DELAY);
  }

  driver_tab[major].endpt = NONE;

  return NONE;
}