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