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