1ff56536eSAlex Hornung /* $NetBSD: dm_table.c,v 1.5 2010/01/04 00:19:08 haad Exp $ */
2ff56536eSAlex Hornung
3ff56536eSAlex Hornung /*
4ba2ce341SAlex Hornung * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com>
5ff56536eSAlex Hornung * Copyright (c) 2008 The NetBSD Foundation, Inc.
6ff56536eSAlex Hornung * All rights reserved.
7ff56536eSAlex Hornung *
8ff56536eSAlex Hornung * This code is derived from software contributed to The NetBSD Foundation
9ff56536eSAlex Hornung * by Adam Hamsik.
10ff56536eSAlex Hornung *
11ff56536eSAlex Hornung * Redistribution and use in source and binary forms, with or without
12ff56536eSAlex Hornung * modification, are permitted provided that the following conditions
13ff56536eSAlex Hornung * are met:
14ff56536eSAlex Hornung * 1. Redistributions of source code must retain the above copyright
15ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer.
16ff56536eSAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright
17ff56536eSAlex Hornung * notice, this list of conditions and the following disclaimer in the
18ff56536eSAlex Hornung * documentation and/or other materials provided with the distribution.
19ff56536eSAlex Hornung *
20ff56536eSAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21ff56536eSAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22ff56536eSAlex Hornung * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23ff56536eSAlex Hornung * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24ff56536eSAlex Hornung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25ff56536eSAlex Hornung * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26ff56536eSAlex Hornung * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27ff56536eSAlex Hornung * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28ff56536eSAlex Hornung * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ff56536eSAlex Hornung * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ff56536eSAlex Hornung * POSSIBILITY OF SUCH DAMAGE.
31ff56536eSAlex Hornung */
32ff56536eSAlex Hornung
33*8477f730Szrj #include <sys/param.h>
345b279a20SAlex Hornung #include <sys/malloc.h>
3530ef4508STomohiro Kusumi #include <cpu/atomic.h>
36a84e173eSAlex Hornung #include <dev/disk/dm/dm.h>
37ff56536eSAlex Hornung
38ff56536eSAlex Hornung /*
39ff56536eSAlex Hornung * There are two types of users of this interface:
40ff56536eSAlex Hornung *
41ff56536eSAlex Hornung * a) Readers such as
42ff56536eSAlex Hornung * dmstrategy, dmgetdisklabel, dmsize, dm_dev_status_ioctl,
43ff56536eSAlex Hornung * dm_table_deps_ioctl, dm_table_status_ioctl, dm_table_reload_ioctl
44ff56536eSAlex Hornung *
45ff56536eSAlex Hornung * b) Writers such as
46ff56536eSAlex Hornung * dm_dev_remove_ioctl, dm_dev_resume_ioctl, dm_table_clear_ioctl
47ff56536eSAlex Hornung *
488f77157dSAlex Hornung * Writers can work with table_head only when there are no readers. We
498f77157dSAlex Hornung * simply use shared/exclusive locking to ensure this.
50ff56536eSAlex Hornung */
51ff56536eSAlex Hornung
52ff56536eSAlex Hornung /*
53ff56536eSAlex Hornung * Function to increment table user reference counter. Return id
54ff56536eSAlex Hornung * of table_id table.
55ff56536eSAlex Hornung * DM_TABLE_ACTIVE will return active table id.
56ff56536eSAlex Hornung * DM_TABLE_INACTIVE will return inactive table id.
57ff56536eSAlex Hornung */
58ff56536eSAlex Hornung static int
dm_table_busy(dm_table_head_t * head,uint8_t table_id)59ff56536eSAlex Hornung dm_table_busy(dm_table_head_t *head, uint8_t table_id)
60ff56536eSAlex Hornung {
61ff56536eSAlex Hornung uint8_t id;
62ff56536eSAlex Hornung
63ff56536eSAlex Hornung id = 0;
64ff56536eSAlex Hornung
658f77157dSAlex Hornung lockmgr(&head->table_mtx, LK_SHARED);
66ff56536eSAlex Hornung
67ff56536eSAlex Hornung if (table_id == DM_TABLE_ACTIVE)
68ff56536eSAlex Hornung id = head->cur_active_table;
69ff56536eSAlex Hornung else
70ff56536eSAlex Hornung id = 1 - head->cur_active_table;
71ff56536eSAlex Hornung
721efa8440SAlex Hornung atomic_add_int(&head->io_cnt, 1);
73ff56536eSAlex Hornung
74ff56536eSAlex Hornung return id;
75ff56536eSAlex Hornung }
76b7c11cdaSTomohiro Kusumi
77ff56536eSAlex Hornung /*
78ff56536eSAlex Hornung * Function release table lock and eventually wakeup all waiters.
79ff56536eSAlex Hornung */
80ff56536eSAlex Hornung static void
dm_table_unbusy(dm_table_head_t * head)81ff56536eSAlex Hornung dm_table_unbusy(dm_table_head_t *head)
82ff56536eSAlex Hornung {
835b279a20SAlex Hornung KKASSERT(head->io_cnt != 0);
84ff56536eSAlex Hornung
851efa8440SAlex Hornung atomic_subtract_int(&head->io_cnt, 1);
86ff56536eSAlex Hornung
875b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE);
88ff56536eSAlex Hornung }
89b7c11cdaSTomohiro Kusumi
90ff56536eSAlex Hornung /*
91ff56536eSAlex Hornung * Return current active table to caller, increment io_cnt reference counter.
92ff56536eSAlex Hornung */
93ff56536eSAlex Hornung dm_table_t *
dm_table_get_entry(dm_table_head_t * head,uint8_t table_id)94ff56536eSAlex Hornung dm_table_get_entry(dm_table_head_t *head, uint8_t table_id)
95ff56536eSAlex Hornung {
96ff56536eSAlex Hornung uint8_t id;
97ff56536eSAlex Hornung
98ff56536eSAlex Hornung id = dm_table_busy(head, table_id);
99ff56536eSAlex Hornung
100ff56536eSAlex Hornung return &head->tables[id];
101ff56536eSAlex Hornung }
102b7c11cdaSTomohiro Kusumi
103ff56536eSAlex Hornung /*
1048f77157dSAlex Hornung * Decrement io reference counter and release shared lock.
105ff56536eSAlex Hornung */
106ff56536eSAlex Hornung void
dm_table_release(dm_table_head_t * head,uint8_t table_id)107ff56536eSAlex Hornung dm_table_release(dm_table_head_t *head, uint8_t table_id)
108ff56536eSAlex Hornung {
109ff56536eSAlex Hornung dm_table_unbusy(head);
110ff56536eSAlex Hornung }
111b7c11cdaSTomohiro Kusumi
112ff56536eSAlex Hornung /*
113ff56536eSAlex Hornung * Switch table from inactive to active mode. Have to wait until io_cnt is 0.
114ff56536eSAlex Hornung */
115ff56536eSAlex Hornung void
dm_table_switch_tables(dm_table_head_t * head)116ff56536eSAlex Hornung dm_table_switch_tables(dm_table_head_t *head)
117ff56536eSAlex Hornung {
1185b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_EXCLUSIVE);
119ff56536eSAlex Hornung
120ff56536eSAlex Hornung head->cur_active_table = 1 - head->cur_active_table;
121ff56536eSAlex Hornung
1225b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE);
123ff56536eSAlex Hornung }
124b7c11cdaSTomohiro Kusumi
125ff56536eSAlex Hornung /*
126ff56536eSAlex Hornung * Destroy all table data. This function can run when there are no
127ff56536eSAlex Hornung * readers on table lists.
128ff56536eSAlex Hornung */
129ff56536eSAlex Hornung int
dm_table_destroy(dm_table_head_t * head,uint8_t table_id)130ff56536eSAlex Hornung dm_table_destroy(dm_table_head_t *head, uint8_t table_id)
131ff56536eSAlex Hornung {
132ff56536eSAlex Hornung dm_table_t *tbl;
133ff56536eSAlex Hornung dm_table_entry_t *table_en;
134ff56536eSAlex Hornung uint8_t id;
135ff56536eSAlex Hornung
1365b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_EXCLUSIVE);
137ff56536eSAlex Hornung
13828d082ddSTomohiro Kusumi dmdebug("table_id=%d io_cnt=%d\n", table_id, head->io_cnt);
139ff56536eSAlex Hornung
140ff56536eSAlex Hornung if (table_id == DM_TABLE_ACTIVE)
141ff56536eSAlex Hornung id = head->cur_active_table;
142ff56536eSAlex Hornung else
143ff56536eSAlex Hornung id = 1 - head->cur_active_table;
144ff56536eSAlex Hornung
145ff56536eSAlex Hornung tbl = &head->tables[id];
146ff56536eSAlex Hornung
1473cd1dc08STomohiro Kusumi while ((table_en = TAILQ_FIRST(tbl)) != NULL) {
1483cd1dc08STomohiro Kusumi TAILQ_REMOVE(tbl, table_en, next);
149be52293aSTomohiro Kusumi
15019bd59e4STomohiro Kusumi if (table_en->target->destroy)
15119bd59e4STomohiro Kusumi table_en->target->destroy(table_en);
152be52293aSTomohiro Kusumi table_en->target_config = NULL;
153d471f1f9STomohiro Kusumi
154d471f1f9STomohiro Kusumi dm_table_free_deps(table_en);
155d471f1f9STomohiro Kusumi
156b188342fSAdam Hoka /* decrement the refcount for the target */
157b188342fSAdam Hoka dm_target_unbusy(table_en->target);
158ff56536eSAlex Hornung
1595b279a20SAlex Hornung kfree(table_en, M_DM);
160ff56536eSAlex Hornung }
1613cd1dc08STomohiro Kusumi KKASSERT(TAILQ_EMPTY(tbl));
162ff56536eSAlex Hornung
1635b279a20SAlex Hornung lockmgr(&head->table_mtx, LK_RELEASE);
164ff56536eSAlex Hornung
165ff56536eSAlex Hornung return 0;
166ff56536eSAlex Hornung }
167b7c11cdaSTomohiro Kusumi
168ff56536eSAlex Hornung /*
169cc760215SAdam Hoka * Return length of active or inactive table in device.
170ff56536eSAlex Hornung */
171cc760215SAdam Hoka static uint64_t
_dm_table_size(dm_table_head_t * head,int table)172cc760215SAdam Hoka _dm_table_size(dm_table_head_t *head, int table)
173ff56536eSAlex Hornung {
174ff56536eSAlex Hornung dm_table_t *tbl;
175ff56536eSAlex Hornung dm_table_entry_t *table_en;
176ff56536eSAlex Hornung uint64_t length;
177ff56536eSAlex Hornung
178ff56536eSAlex Hornung length = 0;
179ff56536eSAlex Hornung
180ff56536eSAlex Hornung /* Select active table */
181f274dbd1STomohiro Kusumi tbl = dm_table_get_entry(head, table);
182ff56536eSAlex Hornung
183ff56536eSAlex Hornung /*
184ff56536eSAlex Hornung * Find out what tables I want to select.
185ff56536eSAlex Hornung * if length => rawblkno then we should used that table.
186ff56536eSAlex Hornung */
1873cd1dc08STomohiro Kusumi TAILQ_FOREACH(table_en, tbl, next) {
188ff56536eSAlex Hornung length += table_en->length;
1895b279a20SAlex Hornung }
190ff56536eSAlex Hornung
191ff56536eSAlex Hornung dm_table_unbusy(head);
192ff56536eSAlex Hornung
193ff56536eSAlex Hornung return length;
194ff56536eSAlex Hornung }
195cc760215SAdam Hoka
196cc760215SAdam Hoka uint64_t
dm_table_size(dm_table_head_t * head)197cc760215SAdam Hoka dm_table_size(dm_table_head_t *head)
198cc760215SAdam Hoka {
199cc760215SAdam Hoka return _dm_table_size(head, DM_TABLE_ACTIVE);
200cc760215SAdam Hoka }
201cc760215SAdam Hoka
202cc760215SAdam Hoka uint64_t
dm_inactive_table_size(dm_table_head_t * head)203cc760215SAdam Hoka dm_inactive_table_size(dm_table_head_t *head)
204cc760215SAdam Hoka {
205cc760215SAdam Hoka return _dm_table_size(head, DM_TABLE_INACTIVE);
206cc760215SAdam Hoka }
207cc760215SAdam Hoka
208ff56536eSAlex Hornung /*
209ff56536eSAlex Hornung * Return > 0 if table is at least one table entry (returns number of entries)
210ff56536eSAlex Hornung * and return 0 if there is not. Target count returned from this function
211ff56536eSAlex Hornung * doesn't need to be true when userspace user receive it (after return
212b7c11cdaSTomohiro Kusumi * there can be dm_dev_resume_ioctl), therefore this is only informative.
213ff56536eSAlex Hornung */
214ff56536eSAlex Hornung int
dm_table_get_target_count(dm_table_head_t * head,uint8_t table_id)215ff56536eSAlex Hornung dm_table_get_target_count(dm_table_head_t *head, uint8_t table_id)
216ff56536eSAlex Hornung {
217ff56536eSAlex Hornung dm_table_entry_t *table_en;
218ff56536eSAlex Hornung dm_table_t *tbl;
219ff56536eSAlex Hornung uint32_t target_count;
220ff56536eSAlex Hornung
221ff56536eSAlex Hornung target_count = 0;
222ff56536eSAlex Hornung
223f274dbd1STomohiro Kusumi tbl = dm_table_get_entry(head, table_id);
224ff56536eSAlex Hornung
2253cd1dc08STomohiro Kusumi TAILQ_FOREACH(table_en, tbl, next)
226ff56536eSAlex Hornung target_count++;
227ff56536eSAlex Hornung
228ff56536eSAlex Hornung dm_table_unbusy(head);
229ff56536eSAlex Hornung
230ff56536eSAlex Hornung return target_count;
231ff56536eSAlex Hornung }
232ff56536eSAlex Hornung
233ff56536eSAlex Hornung /*
234b7c11cdaSTomohiro Kusumi * Initialize dm_table_head_t structures, I'm trying to keep this structure as
235ff56536eSAlex Hornung * opaque as possible.
236ff56536eSAlex Hornung */
237ff56536eSAlex Hornung void
dm_table_head_init(dm_table_head_t * head)238ff56536eSAlex Hornung dm_table_head_init(dm_table_head_t *head)
239ff56536eSAlex Hornung {
240ff56536eSAlex Hornung head->cur_active_table = 0;
241ff56536eSAlex Hornung head->io_cnt = 0;
242ff56536eSAlex Hornung
243ff56536eSAlex Hornung /* Initialize tables. */
2443cd1dc08STomohiro Kusumi TAILQ_INIT(&head->tables[0]);
2453cd1dc08STomohiro Kusumi TAILQ_INIT(&head->tables[1]);
246ff56536eSAlex Hornung
2475b279a20SAlex Hornung lockinit(&head->table_mtx, "dmtbl", 0, LK_CANRECURSE);
248ff56536eSAlex Hornung }
249b7c11cdaSTomohiro Kusumi
250ff56536eSAlex Hornung /*
251ff56536eSAlex Hornung * Destroy all variables in table_head
252ff56536eSAlex Hornung */
253ff56536eSAlex Hornung void
dm_table_head_destroy(dm_table_head_t * head)254ff56536eSAlex Hornung dm_table_head_destroy(dm_table_head_t *head)
255ff56536eSAlex Hornung {
2563b6a19b2SMatthew Dillon KKASSERT(!lockinuse(&head->table_mtx));
2578f77157dSAlex Hornung
258b7c11cdaSTomohiro Kusumi /* tables don't exist when I call this routine, therefore it
259ff56536eSAlex Hornung * doesn't make sense to have io_cnt != 0 */
2605b279a20SAlex Hornung KKASSERT(head->io_cnt == 0);
261ff56536eSAlex Hornung
2625b279a20SAlex Hornung lockuninit(&head->table_mtx);
263ff56536eSAlex Hornung }
264d471f1f9STomohiro Kusumi
265e620de1aSTomohiro Kusumi void
dm_table_init_target(dm_table_entry_t * table_en,void * cfg)26641a68322STomohiro Kusumi dm_table_init_target(dm_table_entry_t *table_en, void *cfg)
267e620de1aSTomohiro Kusumi {
268e620de1aSTomohiro Kusumi table_en->target_config = cfg;
269e620de1aSTomohiro Kusumi }
270e620de1aSTomohiro Kusumi
271d471f1f9STomohiro Kusumi int
dm_table_add_deps(dm_table_entry_t * table_en,dm_pdev_t * pdev)272d471f1f9STomohiro Kusumi dm_table_add_deps(dm_table_entry_t *table_en, dm_pdev_t *pdev)
273d471f1f9STomohiro Kusumi {
274d471f1f9STomohiro Kusumi dm_table_head_t *head;
275d471f1f9STomohiro Kusumi dm_mapping_t *map;
276d471f1f9STomohiro Kusumi
277d471f1f9STomohiro Kusumi KKASSERT(pdev);
278d471f1f9STomohiro Kusumi
279d471f1f9STomohiro Kusumi head = &table_en->dev->table_head;
280d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_SHARED);
281d471f1f9STomohiro Kusumi
282d471f1f9STomohiro Kusumi TAILQ_FOREACH(map, &table_en->pdev_maps, next) {
2834f398b7bSTomohiro Kusumi if (map->data.pdev->udev == pdev->udev) {
284d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_RELEASE);
285d471f1f9STomohiro Kusumi return -1;
286d471f1f9STomohiro Kusumi }
287d471f1f9STomohiro Kusumi }
288d471f1f9STomohiro Kusumi
289d471f1f9STomohiro Kusumi map = kmalloc(sizeof(*map), M_DM, M_WAITOK | M_ZERO);
290d471f1f9STomohiro Kusumi map->data.pdev = pdev;
291d471f1f9STomohiro Kusumi TAILQ_INSERT_TAIL(&table_en->pdev_maps, map, next);
292d471f1f9STomohiro Kusumi
293d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_RELEASE);
294d471f1f9STomohiro Kusumi
295d471f1f9STomohiro Kusumi return 0;
296d471f1f9STomohiro Kusumi }
297d471f1f9STomohiro Kusumi
298d471f1f9STomohiro Kusumi void
dm_table_free_deps(dm_table_entry_t * table_en)299d471f1f9STomohiro Kusumi dm_table_free_deps(dm_table_entry_t *table_en)
300d471f1f9STomohiro Kusumi {
301d471f1f9STomohiro Kusumi dm_table_head_t *head;
302d471f1f9STomohiro Kusumi dm_mapping_t *map;
303d471f1f9STomohiro Kusumi
304d471f1f9STomohiro Kusumi head = &table_en->dev->table_head;
305d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_SHARED);
306d471f1f9STomohiro Kusumi
307d471f1f9STomohiro Kusumi while ((map = TAILQ_FIRST(&table_en->pdev_maps)) != NULL) {
308d471f1f9STomohiro Kusumi TAILQ_REMOVE(&table_en->pdev_maps, map, next);
309d471f1f9STomohiro Kusumi kfree(map, M_DM);
310d471f1f9STomohiro Kusumi }
311d471f1f9STomohiro Kusumi KKASSERT(TAILQ_EMPTY(&table_en->pdev_maps));
312d471f1f9STomohiro Kusumi
313d471f1f9STomohiro Kusumi lockmgr(&head->table_mtx, LK_RELEASE);
314d471f1f9STomohiro Kusumi }
315