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