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