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