xref: /freebsd/sys/dev/hptrr/ldm.h (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*
2  * Copyright (c) HighPoint Technologies, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #include <dev/hptrr/hptrr_config.h>
29 /*
30  * $Id: ldm.h,v 1.69 2007/11/22 07:31:55 gmm Exp $
31  * Copyright (C) 2004-2005 HighPoint Technologies, Inc. All rights reserved.
32  */
33 #ifndef _HPT_LDM_H_
34 #define _HPT_LDM_H_
35 
36 #define VERMAGIC_LDM 69
37 
38 #if defined(__cplusplus)
39 extern "C" {
40 #endif
41 
42 
43 #define __hpt_set_ver(x, v1, v2, v3, v4, v5) x ## _R_ ## v1 ## _ ## v2 ## _ ## v3 ## _ ## v4 ## _ ## v5
44 #define _hpt_set_ver(x, v1, v2, v3, v4, v5)  __hpt_set_ver(x, v1, v2, v3, v4, v5)
45 #define hpt_set_ver(x)               _hpt_set_ver(x, VERMAGIC_OSM, VERMAGIC_HIM, VERMAGIC_LDM, VERMAGIC_ARRAY, MAX_MEMBERS)
46 
47 #define ldm_register_him        hpt_set_ver(ldm_register_him)
48 #define ldm_register_vdev_class hpt_set_ver(ldm_register_vdev_class)
49 #define ldm_alloc_cmds          hpt_set_ver(ldm_alloc_cmds)
50 
51 
52 #ifndef HPT_INTERFACE_VERSION
53 #define HPT_INTERFACE_VERSION 0x02000001
54 #endif
55 
56 #define MAX_PARTITIONS_PER_DISK	4
57 #if defined(__MAX_PARTITIONS_PER_DISK) && MAX_PARTITIONS_PER_DISK > __MAX_PARTITIONS_PER_DISK
58 #error "Please redefine MAX_PARTITIONS_PER_DISK!!!"
59 #endif
60 
61 #define MAX(a,b) (((a)>(b))?(a):(b))
62 #define MIN(a,b) (((a)<(b))?(a):(b))
63 
64 
65 typedef char check_HPT_TIME_is_unsigned[ (HPT_TIME)(-1) > 0 ? 1 : -1 ];
66 
67 #define hpt_time_after_eq(a, b) ((long)(a) - (long)(b) >= 0)
68 #define hpt_time_after(a, b) ((long)(a) - (long)(b) > 0)
69 
70 
71 
72 struct freelist {
73 	int dma;
74 	HPT_UINT alignment;
75 	HPT_UINT count;
76 	HPT_UINT size;
77 	void * head;
78 	struct freelist *next;
79 #if DBG
80 	char *tag;
81 	HPT_UINT reserved_count;
82 	#define freelist_debug_tag(list, _tag) (list)->tag = _tag
83 #else
84 	#define freelist_debug_tag(list, _tag)
85 #endif
86 };
87 
88 
89 void freelist_reserve(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT count);
90 
91 void *freelist_get(struct freelist *);
92 void freelist_put(struct freelist *, void *p);
93 
94 void freelist_reserve_dma(struct freelist *list, void *osext, HPT_UINT size, HPT_UINT alignment, HPT_UINT count);
95 void *freelist_get_dma(struct freelist *, BUS_ADDRESS *busaddr);
96 void freelist_put_dma(struct freelist *, void *p, BUS_ADDRESS busaddr);
97 
98 
99 #define freelist_reserve_with_tag(list, osext, size, count) \
100 	do {\
101 		freelist_debug_tag(list, #list  " at " __FILE__);\
102 		freelist_reserve(list, osext, size, count);\
103 	}while(0)
104 
105 #define freelist_reserve_dma_with_tag(list, osext, size, alignment, count) \
106 	do {\
107 		freelist_debug_tag(list, #list " at " __FILE__);\
108 		freelist_reserve_dma(list, osext, size, alignment, count);\
109 	}while(0)
110 
111 struct lock_request {
112 	HPT_U64 start, end;
113 	struct lock_request *next;
114 	struct list_head waiters; /* blocked commands */
115 	struct tq_item callback;
116 	int lock_cc;
117 };
118 
119 #define INIT_LOCK_REQUEST(req, _start, _end, _cb, _arg, _cc) \
120 	do {\
121 		(req)->next = 0;\
122 		(req)->start = _start;\
123 		(req)->end = _end;\
124 		INIT_TQ_ITEM(&(req)->callback, _cb, _arg);\
125 		INIT_LIST_HEAD(&(req)->waiters);\
126 		(req)->lock_cc = _cc;\
127 	} while (0)
128 
129 struct task_queue {
130 	struct tq_item *head, *tail;
131 };
132 
133 #define TQ_EMPTY(tq) ((tq)->head==0)
134 
135 struct dmapool_order {
136 	HPT_UINT npages;
137 	struct tq_item wakeup_fn;
138 	struct dmapool_order *next;
139 };
140 
141 struct dmapool_client {
142 	void * handle;
143 	HPT_UINT (*shrink)(void *handle, HPT_UINT npages);
144 	int (*resume)(void *handle);
145 	struct dmapool_client *next;
146 };
147 
148 typedef struct _VBUS * PVBUS;
149 typedef struct _VDEV * PVDEV;
150 
151 
152 void dmapool_register_client(PVBUS vbus, struct dmapool_client *client);
153 
154 
155 void dmapool_active(PVBUS vbus);
156 
157 /* return 0 if the request is immediately satisfied, non-zero otherwise. */
158 int dmapool_make_order(PVBUS vbus, struct dmapool_order *order);
159 
160 
161 void *dmapool_get_page(PVBUS vbus, BUS_ADDRESS *busaddr);
162 void *dmapool_get_page_at(PVBUS vbus, void *p, BUS_ADDRESS *busaddr);
163 void dmapool_put_page(PVBUS vbus, void *p, BUS_ADDRESS busaddr);
164 void dmapool_init(PVBUS vbus);
165 HPT_UINT dmapool_max_class_pages(PVBUS vbus);
166 
167 
168 struct timer_call {
169 	HPT_U32 interval; /*microseconds*/
170 	HPT_TIME expire_time; /*microseconds*/
171 	void (*proc)(void * arg);
172 	void * arg;
173 	struct timer_call ** pprev;
174 	struct timer_call * next;
175 };
176 
177 #define ldm_init_timer(timer) do { (timer)->next=0; (timer)->pprev=0; } while (0)
178 
179 #define INIT_TIMER_CALL(timer, _interval, _proc, _arg) \
180 	do { \
181 		HPT_ASSERT((timer)->next==0 && (timer)->pprev==0);\
182 		(timer)->interval = _interval;\
183 		(timer)->proc = _proc;\
184 		(timer)->arg = _arg;\
185 	} while(0)
186 
187 void ldm_request_timer(PVBUS vbus, struct timer_call * tc);
188 void ldm_remove_timer(PVBUS vbus, struct timer_call * tc);
189 void ldm_on_timer(PVBUS vbus);
190 
191 
192 typedef struct _LDM_ADAPTER
193 {
194 	struct _LDM_ADAPTER *next;
195 	HIM  *him;
196 	void *him_handle;
197 	PVBUS vbus;
198 	struct freelist freelist_dev;
199 	struct freelist freelist_plugged_dpc;
200 	HPT_BOOL master;
201 }
202 LDM_ADAPTER, *PLDM_ADAPTER;
203 
204 typedef struct _IOCTL_ARG
205 {
206 	struct list_head link;
207 	PVBUS vbus;
208 	HPT_U32 dwIoControlCode;
209 	HPT_U32 nInBufferSize;
210 	HPT_U32 nOutBufferSize;
211 	void *  lpInBuffer;
212 	void *  lpOutBuffer;
213 	HPT_U32 *lpBytesReturned;
214 	void *  ioctl_cmnd;
215 	void (* done)(struct _IOCTL_ARG *);
216 	int     result; /* HPT_IOCTL_RESULT_ */
217 	struct tq_item dpc;
218 } IOCTL_ARG;
219 
220 #define HPT_IOCTL_RESULT_OK          0
221 #define HPT_IOCTL_RESULT_FAILED     (-1)
222 #define HPT_IOCTL_RESULT_INVALID    (-2)
223 #define HPT_IOCTL_RESULT_RETRY      (-3)
224 #define HPT_IOCTL_RESULT_WRONG_VBUS (-4)
225 
226 void ldm_ioctl(	PVBUS vbus,	IOCTL_ARG *IAPnt);
227 HPT_U32 ldm_get_device_id(PVDEV vd); /* for ioctl */
228 void ldm_set_rebuild_priority(PVBUS vbus, int priority);
229 void ldm_set_autorebuild(PVBUS vbus, int enable);
230 void ldm_set_spindown_disks_timeout(PVBUS vbus, HPT_U8 timeout);
231 
232 #ifndef __HPT_RAW_LBA
233 #define __HPT_RAW_LBA HPT_RAW_LBA
234 #endif
235 
236 #include <dev/hptrr/array.h>
237 
238 typedef struct hpt_raw_disk
239 {
240 #ifdef SUPPORT_ARRAY
241 	PRAW_PARTITION raw_part_list;
242 	__HPT_RAW_LBA max_available_capacity;
243 	__HPT_RAW_LBA total_available_capacity;
244 #endif
245 	__HPT_RAW_LBA real_capacity;
246 	__HPT_RAW_LBA head_position;
247 
248 	HPT_U16 max_sectors_per_cmd;
249 	HPT_U8  max_queue_depth;
250 	HPT_U8  user_select_mode;
251 
252 	HPT_UINT  uninitialized : 1;
253 	HPT_UINT  legacy_disk : 1;
254 	HPT_UINT  is_spare : 1;
255 	HPT_UINT  v3_format : 1;
256 	HPT_UINT  need_sync : 1;
257 	HPT_UINT  temp_spare : 1;
258 	HPT_UINT  need_check_array : 1;
259 	HPT_UINT  df_user_mode_set: 1;
260 
261 	HPT_UINT  df_read_ahead_set: 1;
262 	HPT_UINT  enable_read_ahead : 1;
263 	HPT_UINT  df_write_cache_set: 1;
264 	HPT_UINT  enable_write_cache : 1;
265 	HPT_UINT  df_tcq_set: 1;
266 	HPT_UINT  enable_tcq : 1;
267 	HPT_UINT  df_ncq_set: 1;
268 	HPT_UINT  enable_ncq : 1;
269 
270 	HIM  *				him;
271 	int 				index;
272 	PLDM_ADAPTER		adapter;
273 	void *				phy_dev;
274 	PIDENTIFY_DATA2		identify_data;
275 	char    model[40];
276 
277 	struct tq_item reset_dpc;
278 	int reset_pending;
279 
280 	struct tq_item fail_dpc;
281 	int fail_pending;
282 }
283 HPT_RAW_DISK, *PHPT_RAW_DISK;
284 
285 struct vdev_class
286 {
287 	struct vdev_class *next;
288 
289 	HPT_U8   __type;
290 	HPT_U8   stripped;        /* RAID0,3,5,6 */
291 	HPT_U8   redundancy;      /* RAID1-1, RAID3/5-1, RAID6-2 */
292 	HPT_U8   must_init;       /* RAID3,5,6 */
293 	HPT_U8   docache;
294 
295 	HPT_UINT vbus_ext_size;
296 	HPT_UINT vbus_ext_offset; /* used by LDM */
297 	HPT_UINT dev_ext_size;
298 	HPT_UINT cmd_ext_size;
299 
300 
301 	void (*get_mem_info)(PVBUS vbus, void *osext, int phydev_count);
302 	void (*queue_cmd)(PCOMMAND cmd);
303 	void (*member_failed)(struct _VDEV * vd);
304 
305 
306 	void (*initialize)(PVBUS vbus);
307 	void (*release)(PVBUS vbus);
308 	int  (*add)(PVDEV vd);
309 	void (*remove)(PVDEV vd);
310 	void (*reset)(PVDEV vd);
311 	void (*sync_stamp)(PVDEV vd);
312 	int  (*support_type)(int type);
313 };
314 
315 
316 #define VDEV_CLASS_CONSTRUCTOR(type, prefix) { \
317 	0, \
318 	type, \
319 	prefix ## _stripped, \
320 	prefix ## _redundancy, \
321 	prefix ## _must_init, \
322 	0, \
323 	prefix ## _vbus_ext_size, \
324 	0, \
325 	prefix ## _dev_ext_size, \
326 	prefix ## _cmd_ext_size, \
327 	prefix ## _get_mem_info, \
328 	prefix ## _queue_cmd, \
329 	prefix ## _member_failed, \
330 	prefix ## _initialize, \
331 	prefix ## _release, \
332 	prefix ## _add, \
333 	prefix ## _remove, \
334 	prefix ## _reset, \
335 	prefix ## _sync_stamp, \
336 	0 \
337 }
338 
339 #define VD_RAW       1
340 #define VD_PARTITION 4
341 
342 #define mIsArray(vdev_type) ((vdev_type)>VD_PARTITION)
343 
344 #define VD_RAID0     5
345 #define VD_RAID1     6
346 #define VD_JBOD      7
347 #define VD_RAID5     8
348 #define VD_RAID6     9
349 #define VD_RAID3     10
350 #define VD_RAID4     11
351 #define VD_RAID1E    12
352 
353 #define MAX_VD_TYPE_ID  12
354 
355 struct vdev_class *ldm_find_vdev_class(HPT_U8 type);
356 
357 typedef struct _VDEV {
358 	PVBUS vbus;
359 	struct vdev_class *Class;
360 	HPT_U8 type;
361 	PVDEV parent;
362 	void * ext;
363 	HPT_U64 capacity;
364 	int     target_id;
365 	HPT_UINT cmds_per_request;
366 
367 	union {
368 #ifdef SUPPORT_ARRAY
369 		HPT_ARRAY array;
370 		HPT_PARTITION partition;
371 #endif
372 		HPT_RAW_DISK raw;
373 	} u;
374 
375 	HPT_U8 vf_online : 1;
376 	HPT_U8 vf_bootmark : 1;
377 	HPT_U8 vf_bootable : 1;
378 	HPT_U8 vf_resetting: 1;
379 	HPT_U8 vf_quiesced: 1;
380 	HPT_U8 vf_clslock: 1;
381 
382 	HPT_U8 cache_policy; /* see CACHE_POLICY_* */
383 
384 	HPT_UINT cq_len;
385 	HPT_UINT cmds_sent;
386 
387 	struct list_head link;
388 	struct list_head cq_wait_send;
389 	struct list_head cq_sent;
390 
391 	int cq_priority;
392 	struct list_head cq_wait_lock;
393 	struct lock_request *locks_granted;
394 	struct lock_request *locks_wait;
395 	HPT_U32 ioctl_id;
396 	void * cc_ext;
397 }
398 VDEV;
399 
400 #define CACHE_POLICY_NONE 0
401 #define CACHE_POLICY_WRITE_THROUGH 1
402 #define CACHE_POLICY_WRITE_BACK 2
403 
404 
405 extern HIM *him_list;
406 
407 
408 void ldm_register_him(PHIM him);
409 
410 
411 void ldm_register_vdev_class(struct vdev_class *Class);
412 
413 
414 HPT_BOOL ldm_register_adapter(PLDM_ADAPTER adapter);
415 
416 
417 int init_config(void);
418 
419 HPT_UINT ldm_get_vbus_size(void);
420 
421 
422 void ldm_create_vbus(PVBUS vbus, void *osext);
423 
424 
425 void ldm_get_mem_info(PVBUS vbus, void *osext);
426 
427 
428 void *ldm_get_vbus_ext(PVBUS vbus, struct vdev_class *Class);
429 
430 
431 PVBUS ldm_get_next_vbus(PVBUS vbus, void **posext);
432 
433 #define ldm_for_each_vbus(vbus, vbus_ext) \
434 	for (vbus = ldm_get_next_vbus(0, (void **)(void *)&vbus_ext); vbus; \
435 		vbus = ldm_get_next_vbus(vbus, (void **)(void *)&vbus_ext))
436 
437 
438 void ldm_initialize_vbus_async(PVBUS vbus, PLDM_ADAPTER master_adapter, void (*done)(void *osext));
439 
440 /* ldm_initialize_vbus is deprecated since it will hold the CPU too long. */
441 #define ldm_initialize_vbus(vbus, adapter) ldm_initialize_vbus_async(vbus, adapter, 0)
442 
443 
444 void ldm_release_vbus(PVBUS vbus);
445 
446 PVDEV ldm_create_vdev(PVBUS vbus, HPT_U8 type);
447 void ldm_release_vdev(PVDEV vd);
448 
449 PVDEV ldm_find_target(PVBUS vbus, int id);
450 PVDEV ldm_find_stamp(PVBUS vbus, HPT_U32 stamp, int seq);
451 
452 
453 PCOMMAND ldm_alloc_cmds(PVBUS vbus, HPT_UINT cnt);
454 void ldm_free_cmds(PCOMMAND cmd);
455 
456 HPT_UINT ldm_get_cmd_size(void);
457 PCOMMAND ldm_alloc_cmds_from_list(PVBUS vbus, struct freelist *list, HPT_UINT cnt);
458 void ldm_free_cmds_to_list(struct freelist *list, PCOMMAND cmd);
459 
460 
461 PCOMMAND __ldm_alloc_cmd(struct freelist *list);
462 
463 #ifdef OS_SUPPORT_TASK
464 #define CMD_SET_PRIORITY(cmd, pri) cmd->priority = (pri)
465 #else
466 #define CMD_SET_PRIORITY(cmd, pri)
467 #endif
468 
469 
470 #define CMD_GROUP_GET(grp, cmd) \
471 	do {\
472 		grp->grplist->count++;\
473 		cmd = __ldm_alloc_cmd(grp->grplist);\
474 		cmd->vbus = grp->vbus;\
475 		cmd->grplist = grp->grplist;\
476 		CMD_SET_PRIORITY(cmd, grp->priority);\
477 	} while(0)
478 
479 #define CMD_GROUP_PUT(grp, cmd) \
480 	do {\
481 		freelist_put(grp->grplist, cmd);\
482 		grp->grplist->count--;\
483 	} while (0)
484 
485 
486 
487 
488 void ldm_queue_cmd(PCOMMAND cmd);
489 void vdev_queue_cmd(PCOMMAND cmd);
490 void ldm_finish_cmd(PCOMMAND cmd);
491 
492 
493 int  ldm_acquire_lock(PVDEV vd, struct lock_request *req);
494 void ldm_release_lock(PVDEV vd, struct lock_request *req);
495 
496 void ldm_queue_task(struct task_queue *tq, struct tq_item *t);
497 void ldm_queue_vbus_dpc(PVBUS vbus, struct tq_item *t);
498 
499 HPT_BOOL ldm_intr(PVBUS vbus);
500 void ldm_run(PVBUS vbus);
501 int ldm_idle(PVBUS vbus);
502 
503 
504 int ldm_reset_vbus(PVBUS vbus);
505 
506 
507 void ldm_suspend(PVBUS vbus);
508 void ldm_resume(PVBUS vbus);
509 void ldm_shutdown(PVBUS vbus);/*shutdown all the controllers*/
510 
511 
512 #define HIM_EVENT_DEVICE_REMOVED 1
513 #define HIM_EVENT_DEVICE_PLUGGED 2
514 #define HIM_EVENT_DEVICE_ERROR   3
515 #define HIM_EVENT_RESET_REQUIRED 4
516 #define HIM_EVENT_QUIESCE_DEVICE 5
517 #define HIM_EVENT_UNQUIESCE_DEVICE 6
518 #define HIM_EVENT_CONFIG_CHANGED 7
519 
520 void ldm_event_notify(HPT_U32 event, void *arg1, void *arg2);
521 
522 void log_sector_repair(PVDEV vd, int success, HPT_LBA lba, HPT_U16 nsectors);
523 
524 void ldm_register_device(PVDEV vd);
525 void ldm_unregister_device(PVDEV vd);
526 
527 PVBUS him_handle_to_vbus(void * him_handle);
528 void ldm_ide_fixstring (HPT_U8 *s, const int bytecount);
529 #if defined(__cplusplus)
530 }
531 #endif
532 #endif
533