xref: /openbmc/linux/drivers/s390/block/dasd_int.h (revision 3e26a691)
1 /*
2  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3  *		    Horst Hummel <Horst.Hummel@de.ibm.com>
4  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
5  * Bugreports.to..: <Linux390@de.ibm.com>
6  * Copyright IBM Corp. 1999, 2009
7  */
8 
9 #ifndef DASD_INT_H
10 #define DASD_INT_H
11 
12 /* we keep old device allocation scheme; IOW, minors are still in 0..255 */
13 #define DASD_PER_MAJOR (1U << (MINORBITS - DASD_PARTN_BITS))
14 #define DASD_PARTN_MASK ((1 << DASD_PARTN_BITS) - 1)
15 
16 /*
17  * States a dasd device can have:
18  *   new: the dasd_device structure is allocated.
19  *   known: the discipline for the device is identified.
20  *   basic: the device can do basic i/o.
21  *   unfmt: the device could not be analyzed (format is unknown).
22  *   ready: partition detection is done and the device is can do block io.
23  *   online: the device accepts requests from the block device queue.
24  *
25  * Things to do for startup state transitions:
26  *   new -> known: find discipline for the device and create devfs entries.
27  *   known -> basic: request irq line for the device.
28  *   basic -> ready: do the initial analysis, e.g. format detection,
29  *                   do block device setup and detect partitions.
30  *   ready -> online: schedule the device tasklet.
31  * Things to do for shutdown state transitions:
32  *   online -> ready: just set the new device state.
33  *   ready -> basic: flush requests from the block device layer, clear
34  *                   partition information and reset format information.
35  *   basic -> known: terminate all requests and free irq.
36  *   known -> new: remove devfs entries and forget discipline.
37  */
38 
39 #define DASD_STATE_NEW	  0
40 #define DASD_STATE_KNOWN  1
41 #define DASD_STATE_BASIC  2
42 #define DASD_STATE_UNFMT  3
43 #define DASD_STATE_READY  4
44 #define DASD_STATE_ONLINE 5
45 
46 #include <linux/module.h>
47 #include <linux/wait.h>
48 #include <linux/blkdev.h>
49 #include <linux/genhd.h>
50 #include <linux/hdreg.h>
51 #include <linux/interrupt.h>
52 #include <linux/log2.h>
53 #include <asm/ccwdev.h>
54 #include <linux/workqueue.h>
55 #include <asm/debug.h>
56 #include <asm/dasd.h>
57 #include <asm/idals.h>
58 
59 /* DASD discipline magic */
60 #define DASD_ECKD_MAGIC 0xC5C3D2C4
61 #define DASD_DIAG_MAGIC 0xC4C9C1C7
62 #define DASD_FBA_MAGIC 0xC6C2C140
63 
64 /*
65  * SECTION: Type definitions
66  */
67 struct dasd_device;
68 struct dasd_block;
69 
70 /* BIT DEFINITIONS FOR SENSE DATA */
71 #define DASD_SENSE_BIT_0 0x80
72 #define DASD_SENSE_BIT_1 0x40
73 #define DASD_SENSE_BIT_2 0x20
74 #define DASD_SENSE_BIT_3 0x10
75 
76 /* BIT DEFINITIONS FOR SIM SENSE */
77 #define DASD_SIM_SENSE 0x0F
78 #define DASD_SIM_MSG_TO_OP 0x03
79 #define DASD_SIM_LOG 0x0C
80 
81 /* lock class for nested cdev lock */
82 #define CDEV_NESTED_FIRST 1
83 #define CDEV_NESTED_SECOND 2
84 
85 /*
86  * SECTION: MACROs for klogd and s390 debug feature (dbf)
87  */
88 #define DBF_DEV_EVENT(d_level, d_device, d_str, d_data...) \
89 do { \
90 	debug_sprintf_event(d_device->debug_area, \
91 			    d_level, \
92 			    d_str "\n", \
93 			    d_data); \
94 } while(0)
95 
96 #define DBF_DEV_EXC(d_level, d_device, d_str, d_data...) \
97 do { \
98 	debug_sprintf_exception(d_device->debug_area, \
99 				d_level, \
100 				d_str "\n", \
101 				d_data); \
102 } while(0)
103 
104 #define DBF_EVENT(d_level, d_str, d_data...)\
105 do { \
106 	debug_sprintf_event(dasd_debug_area, \
107 			    d_level,\
108 			    d_str "\n", \
109 			    d_data); \
110 } while(0)
111 
112 #define DBF_EVENT_DEVID(d_level, d_cdev, d_str, d_data...)	\
113 do { \
114 	struct ccw_dev_id __dev_id;			\
115 	ccw_device_get_id(d_cdev, &__dev_id);		\
116 	debug_sprintf_event(dasd_debug_area,		\
117 			    d_level,					\
118 			    "0.%x.%04x " d_str "\n",			\
119 			    __dev_id.ssid, __dev_id.devno, d_data);	\
120 } while (0)
121 
122 #define DBF_EXC(d_level, d_str, d_data...)\
123 do { \
124 	debug_sprintf_exception(dasd_debug_area, \
125 				d_level,\
126 				d_str "\n", \
127 				d_data); \
128 } while(0)
129 
130 /* limit size for an errorstring */
131 #define ERRORLENGTH 30
132 
133 /* definition of dbf debug levels */
134 #define	DBF_EMERG	0	/* system is unusable			*/
135 #define	DBF_ALERT	1	/* action must be taken immediately	*/
136 #define	DBF_CRIT	2	/* critical conditions			*/
137 #define	DBF_ERR		3	/* error conditions			*/
138 #define	DBF_WARNING	4	/* warning conditions			*/
139 #define	DBF_NOTICE	5	/* normal but significant condition	*/
140 #define	DBF_INFO	6	/* informational			*/
141 #define	DBF_DEBUG	6	/* debug-level messages			*/
142 
143 /* messages to be written via klogd and dbf */
144 #define DEV_MESSAGE(d_loglevel,d_device,d_string,d_args...)\
145 do { \
146 	printk(d_loglevel PRINTK_HEADER " %s: " d_string "\n", \
147 	       dev_name(&d_device->cdev->dev), d_args); \
148 	DBF_DEV_EVENT(DBF_ALERT, d_device, d_string, d_args); \
149 } while(0)
150 
151 #define MESSAGE(d_loglevel,d_string,d_args...)\
152 do { \
153 	printk(d_loglevel PRINTK_HEADER " " d_string "\n", d_args); \
154 	DBF_EVENT(DBF_ALERT, d_string, d_args); \
155 } while(0)
156 
157 /* messages to be written via klogd only */
158 #define DEV_MESSAGE_LOG(d_loglevel,d_device,d_string,d_args...)\
159 do { \
160 	printk(d_loglevel PRINTK_HEADER " %s: " d_string "\n", \
161 	       dev_name(&d_device->cdev->dev), d_args); \
162 } while(0)
163 
164 #define MESSAGE_LOG(d_loglevel,d_string,d_args...)\
165 do { \
166 	printk(d_loglevel PRINTK_HEADER " " d_string "\n", d_args); \
167 } while(0)
168 
169 struct dasd_ccw_req {
170 	unsigned int magic;		/* Eye catcher */
171 	struct list_head devlist;	/* for dasd_device request queue */
172 	struct list_head blocklist;	/* for dasd_block request queue */
173 
174 	/* Where to execute what... */
175 	struct dasd_block *block;	/* the originating block device */
176 	struct dasd_device *memdev;	/* the device used to allocate this */
177 	struct dasd_device *startdev;	/* device the request is started on */
178 	struct dasd_device *basedev;	/* base device if no block->base */
179 	void *cpaddr;			/* address of ccw or tcw */
180 	unsigned char cpmode;		/* 0 = cmd mode, 1 = itcw */
181 	char status;			/* status of this request */
182 	short retries;			/* A retry counter */
183 	unsigned long flags;        	/* flags of this request */
184 
185 	/* ... and how */
186 	unsigned long starttime;	/* jiffies time of request start */
187 	unsigned long expires;		/* expiration period in jiffies */
188 	char lpm;			/* logical path mask */
189 	void *data;			/* pointer to data area */
190 
191 	/* these are important for recovering erroneous requests          */
192 	int intrc;			/* internal error, e.g. from start_IO */
193 	struct irb irb;			/* device status in case of an error */
194 	struct dasd_ccw_req *refers;	/* ERP-chain queueing. */
195 	void *function; 		/* originating ERP action */
196 
197 	/* these are for statistics only */
198 	unsigned long long buildclk;	/* TOD-clock of request generation */
199 	unsigned long long startclk;	/* TOD-clock of request start */
200 	unsigned long long stopclk;	/* TOD-clock of request interrupt */
201 	unsigned long long endclk;	/* TOD-clock of request termination */
202 
203         /* Callback that is called after reaching final status. */
204 	void (*callback)(struct dasd_ccw_req *, void *data);
205 	void *callback_data;
206 };
207 
208 /*
209  * dasd_ccw_req -> status can be:
210  */
211 #define DASD_CQR_FILLED 	0x00	/* request is ready to be processed */
212 #define DASD_CQR_DONE		0x01	/* request is completed successfully */
213 #define DASD_CQR_NEED_ERP	0x02	/* request needs recovery action */
214 #define DASD_CQR_IN_ERP 	0x03	/* request is in recovery */
215 #define DASD_CQR_FAILED 	0x04	/* request is finally failed */
216 #define DASD_CQR_TERMINATED	0x05	/* request was stopped by driver */
217 
218 #define DASD_CQR_QUEUED 	0x80	/* request is queued to be processed */
219 #define DASD_CQR_IN_IO		0x81	/* request is currently in IO */
220 #define DASD_CQR_ERROR		0x82	/* request is completed with error */
221 #define DASD_CQR_CLEAR_PENDING	0x83	/* request is clear pending */
222 #define DASD_CQR_CLEARED	0x84	/* request was cleared */
223 #define DASD_CQR_SUCCESS	0x85	/* request was successful */
224 
225 /* default expiration time*/
226 #define DASD_EXPIRES	  300
227 #define DASD_EXPIRES_MAX  40000000
228 #define DASD_RETRIES	  256
229 #define DASD_RETRIES_MAX  32768
230 
231 /* per dasd_ccw_req flags */
232 #define DASD_CQR_FLAGS_USE_ERP   0	/* use ERP for this request */
233 #define DASD_CQR_FLAGS_FAILFAST  1	/* FAILFAST */
234 #define DASD_CQR_VERIFY_PATH	 2	/* path verification request */
235 #define DASD_CQR_ALLOW_SLOCK	 3	/* Try this request even when lock was
236 					 * stolen. Should not be combined with
237 					 * DASD_CQR_FLAGS_USE_ERP
238 					 */
239 
240 /* Signature for error recovery functions. */
241 typedef struct dasd_ccw_req *(*dasd_erp_fn_t) (struct dasd_ccw_req *);
242 
243 /*
244  * A single CQR can only contain a maximum of 255 CCWs. It is limited by
245  * the locate record and locate record extended count value which can only hold
246  * 1 Byte max.
247  */
248 #define DASD_CQR_MAX_CCW 255
249 
250 /*
251  * Unique identifier for dasd device.
252  */
253 #define UA_NOT_CONFIGURED  0x00
254 #define UA_BASE_DEVICE	   0x01
255 #define UA_BASE_PAV_ALIAS  0x02
256 #define UA_HYPER_PAV_ALIAS 0x03
257 
258 struct dasd_uid {
259 	__u8 type;
260 	char vendor[4];
261 	char serial[15];
262 	__u16 ssid;
263 	__u8 real_unit_addr;
264 	__u8 base_unit_addr;
265 	char vduit[33];
266 };
267 
268 /*
269  * the struct dasd_discipline is
270  * sth like a table of virtual functions, if you think of dasd_eckd
271  * inheriting dasd...
272  * no, currently we are not planning to reimplement the driver in C++
273  */
274 struct dasd_discipline {
275 	struct module *owner;
276 	char ebcname[8];	/* a name used for tagging and printks */
277 	char name[8];		/* a name used for tagging and printks */
278 	int max_blocks;		/* maximum number of blocks to be chained */
279 
280 	struct list_head list;	/* used for list of disciplines */
281 
282 	/*
283 	 * Device recognition functions. check_device is used to verify
284 	 * the sense data and the information returned by read device
285 	 * characteristics. It returns 0 if the discipline can be used
286 	 * for the device in question. uncheck_device is called during
287 	 * device shutdown to deregister a device from its discipline.
288 	 */
289 	int (*check_device) (struct dasd_device *);
290 	void (*uncheck_device) (struct dasd_device *);
291 
292 	/*
293 	 * do_analysis is used in the step from device state "basic" to
294 	 * state "accept". It returns 0 if the device can be made ready,
295 	 * it returns -EMEDIUMTYPE if the device can't be made ready or
296 	 * -EAGAIN if do_analysis started a ccw that needs to complete
297 	 * before the analysis may be repeated.
298 	 */
299 	int (*do_analysis) (struct dasd_block *);
300 
301 	/*
302 	 * This function is called, when new paths become available.
303 	 * Disciplins may use this callback to do necessary setup work,
304 	 * e.g. verify that new path is compatible with the current
305 	 * configuration.
306 	 */
307 	int (*verify_path)(struct dasd_device *, __u8);
308 
309 	/*
310 	 * Last things to do when a device is set online, and first things
311 	 * when it is set offline.
312 	 */
313 	int (*basic_to_ready) (struct dasd_device *);
314 	int (*online_to_ready) (struct dasd_device *);
315 	int (*basic_to_known)(struct dasd_device *);
316 
317 	/* (struct dasd_device *);
318 	 * Device operation functions. build_cp creates a ccw chain for
319 	 * a block device request, start_io starts the request and
320 	 * term_IO cancels it (e.g. in case of a timeout). format_device
321 	 * returns a ccw chain to be used to format the device.
322 	 * handle_terminated_request allows to examine a cqr and prepare
323 	 * it for retry.
324 	 */
325 	struct dasd_ccw_req *(*build_cp) (struct dasd_device *,
326 					  struct dasd_block *,
327 					  struct request *);
328 	int (*start_IO) (struct dasd_ccw_req *);
329 	int (*term_IO) (struct dasd_ccw_req *);
330 	void (*handle_terminated_request) (struct dasd_ccw_req *);
331 	int (*format_device) (struct dasd_device *,
332 			      struct format_data_t *, int enable_pav);
333 	int (*free_cp) (struct dasd_ccw_req *, struct request *);
334 
335 	/*
336 	 * Error recovery functions. examine_error() returns a value that
337 	 * indicates what to do for an error condition. If examine_error()
338 	 * returns 'dasd_era_recover' erp_action() is called to create a
339 	 * special error recovery ccw. erp_postaction() is called after
340 	 * an error recovery ccw has finished its execution. dump_sense
341 	 * is called for every error condition to print the sense data
342 	 * to the console.
343 	 */
344 	dasd_erp_fn_t(*erp_action) (struct dasd_ccw_req *);
345 	dasd_erp_fn_t(*erp_postaction) (struct dasd_ccw_req *);
346 	void (*dump_sense) (struct dasd_device *, struct dasd_ccw_req *,
347 			    struct irb *);
348 	void (*dump_sense_dbf) (struct dasd_device *, struct irb *, char *);
349 	void (*check_for_device_change) (struct dasd_device *,
350 					 struct dasd_ccw_req *,
351 					 struct irb *);
352 
353         /* i/o control functions. */
354 	int (*fill_geometry) (struct dasd_block *, struct hd_geometry *);
355 	int (*fill_info) (struct dasd_device *, struct dasd_information2_t *);
356 	int (*ioctl) (struct dasd_block *, unsigned int, void __user *);
357 
358 	/* suspend/resume functions */
359 	int (*freeze) (struct dasd_device *);
360 	int (*restore) (struct dasd_device *);
361 
362 	/* reload device after state change */
363 	int (*reload) (struct dasd_device *);
364 
365 	int (*get_uid) (struct dasd_device *, struct dasd_uid *);
366 	void (*kick_validate) (struct dasd_device *);
367 	int (*check_attention)(struct dasd_device *, __u8);
368 };
369 
370 extern struct dasd_discipline *dasd_diag_discipline_pointer;
371 
372 /*
373  * Notification numbers for extended error reporting notifications:
374  * The DASD_EER_DISABLE notification is sent before a dasd_device (and it's
375  * eer pointer) is freed. The error reporting module needs to do all necessary
376  * cleanup steps.
377  * The DASD_EER_TRIGGER notification sends the actual error reports (triggers).
378  */
379 #define DASD_EER_DISABLE 0
380 #define DASD_EER_TRIGGER 1
381 
382 /* Trigger IDs for extended error reporting DASD_EER_TRIGGER notification */
383 #define DASD_EER_FATALERROR  1
384 #define DASD_EER_NOPATH      2
385 #define DASD_EER_STATECHANGE 3
386 #define DASD_EER_PPRCSUSPEND 4
387 
388 struct dasd_path {
389 	__u8 opm;
390 	__u8 tbvpm;
391 	__u8 ppm;
392 	__u8 npm;
393 	/* paths that are not used because of a special condition */
394 	__u8 cablepm; /* miss-cabled */
395 	__u8 hpfpm;   /* the HPF requirements of the other paths are not met */
396 	__u8 cuirpm;  /* CUIR varied offline */
397 };
398 
399 struct dasd_profile_info {
400 	/* legacy part of profile data, as in dasd_profile_info_t */
401 	unsigned int dasd_io_reqs;	 /* number of requests processed */
402 	unsigned int dasd_io_sects;	 /* number of sectors processed */
403 	unsigned int dasd_io_secs[32];	 /* histogram of request's sizes */
404 	unsigned int dasd_io_times[32];	 /* histogram of requests's times */
405 	unsigned int dasd_io_timps[32];	 /* h. of requests's times per sector */
406 	unsigned int dasd_io_time1[32];	 /* hist. of time from build to start */
407 	unsigned int dasd_io_time2[32];	 /* hist. of time from start to irq */
408 	unsigned int dasd_io_time2ps[32]; /* hist. of time from start to irq */
409 	unsigned int dasd_io_time3[32];	 /* hist. of time from irq to end */
410 	unsigned int dasd_io_nr_req[32]; /* hist. of # of requests in chanq */
411 
412 	/* new data */
413 	struct timespec starttod;	   /* time of start or last reset */
414 	unsigned int dasd_io_alias;	   /* requests using an alias */
415 	unsigned int dasd_io_tpm;	   /* requests using transport mode */
416 	unsigned int dasd_read_reqs;	   /* total number of read  requests */
417 	unsigned int dasd_read_sects;	   /* total number read sectors */
418 	unsigned int dasd_read_alias;	   /* read request using an alias */
419 	unsigned int dasd_read_tpm;	   /* read requests in transport mode */
420 	unsigned int dasd_read_secs[32];   /* histogram of request's sizes */
421 	unsigned int dasd_read_times[32];  /* histogram of requests's times */
422 	unsigned int dasd_read_time1[32];  /* hist. time from build to start */
423 	unsigned int dasd_read_time2[32];  /* hist. of time from start to irq */
424 	unsigned int dasd_read_time3[32];  /* hist. of time from irq to end */
425 	unsigned int dasd_read_nr_req[32]; /* hist. of # of requests in chanq */
426 };
427 
428 struct dasd_profile {
429 	struct dentry *dentry;
430 	struct dasd_profile_info *data;
431 	spinlock_t lock;
432 };
433 
434 struct dasd_device {
435 	/* Block device stuff. */
436 	struct dasd_block *block;
437 
438         unsigned int devindex;
439 	unsigned long flags;	   /* per device flags */
440 	unsigned short features;   /* copy of devmap-features (read-only!) */
441 
442 	/* extended error reporting stuff (eer) */
443 	struct dasd_ccw_req *eer_cqr;
444 
445 	/* Device discipline stuff. */
446 	struct dasd_discipline *discipline;
447 	struct dasd_discipline *base_discipline;
448 	void *private;
449 	struct dasd_path path_data;
450 
451 	/* Device state and target state. */
452 	int state, target;
453 	struct mutex state_mutex;
454 	int stopped;		/* device (ccw_device_start) was stopped */
455 
456 	/* reference count. */
457         atomic_t ref_count;
458 
459 	/* ccw queue and memory for static ccw/erp buffers. */
460 	struct list_head ccw_queue;
461 	spinlock_t mem_lock;
462 	void *ccw_mem;
463 	void *erp_mem;
464 	struct list_head ccw_chunks;
465 	struct list_head erp_chunks;
466 
467 	atomic_t tasklet_scheduled;
468         struct tasklet_struct tasklet;
469 	struct work_struct kick_work;
470 	struct work_struct restore_device;
471 	struct work_struct reload_device;
472 	struct work_struct kick_validate;
473 	struct timer_list timer;
474 
475 	debug_info_t *debug_area;
476 
477 	struct ccw_device *cdev;
478 
479 	/* hook for alias management */
480 	struct list_head alias_list;
481 
482 	/* default expiration time in s */
483 	unsigned long default_expires;
484 	unsigned long default_retries;
485 
486 	unsigned long blk_timeout;
487 
488 	struct dentry *debugfs_dentry;
489 	struct dasd_profile profile;
490 };
491 
492 struct dasd_block {
493 	/* Block device stuff. */
494 	struct gendisk *gdp;
495 	struct request_queue *request_queue;
496 	spinlock_t request_queue_lock;
497 	struct block_device *bdev;
498 	atomic_t open_count;
499 
500 	unsigned long long blocks; /* size of volume in blocks */
501 	unsigned int bp_block;	   /* bytes per block */
502 	unsigned int s2b_shift;	   /* log2 (bp_block/512) */
503 
504 	struct dasd_device *base;
505 	struct list_head ccw_queue;
506 	spinlock_t queue_lock;
507 
508 	atomic_t tasklet_scheduled;
509 	struct tasklet_struct tasklet;
510 	struct timer_list timer;
511 
512 	struct dentry *debugfs_dentry;
513 	struct dasd_profile profile;
514 };
515 
516 struct dasd_attention_data {
517 	struct dasd_device *device;
518 	__u8 lpum;
519 };
520 
521 /* reasons why device (ccw_device_start) was stopped */
522 #define DASD_STOPPED_NOT_ACC 1         /* not accessible */
523 #define DASD_STOPPED_QUIESCE 2         /* Quiesced */
524 #define DASD_STOPPED_PENDING 4         /* long busy */
525 #define DASD_STOPPED_DC_WAIT 8         /* disconnected, wait */
526 #define DASD_STOPPED_SU      16        /* summary unit check handling */
527 #define DASD_STOPPED_PM      32        /* pm state transition */
528 #define DASD_UNRESUMED_PM    64        /* pm resume failed state */
529 
530 /* per device flags */
531 #define DASD_FLAG_OFFLINE	3	/* device is in offline processing */
532 #define DASD_FLAG_EER_SNSS	4	/* A SNSS is required */
533 #define DASD_FLAG_EER_IN_USE	5	/* A SNSS request is running */
534 #define DASD_FLAG_DEVICE_RO	6	/* The device itself is read-only. Don't
535 					 * confuse this with the user specified
536 					 * read-only feature.
537 					 */
538 #define DASD_FLAG_IS_RESERVED	7	/* The device is reserved */
539 #define DASD_FLAG_LOCK_STOLEN	8	/* The device lock was stolen */
540 #define DASD_FLAG_SUSPENDED	9	/* The device was suspended */
541 #define DASD_FLAG_SAFE_OFFLINE	10	/* safe offline processing requested*/
542 #define DASD_FLAG_SAFE_OFFLINE_RUNNING	11	/* safe offline running */
543 #define DASD_FLAG_ABORTALL	12	/* Abort all noretry requests */
544 #define DASD_FLAG_PATH_VERIFY	13	/* Path verification worker running */
545 
546 #define DASD_SLEEPON_START_TAG	((void *) 1)
547 #define DASD_SLEEPON_END_TAG	((void *) 2)
548 
549 void dasd_put_device_wake(struct dasd_device *);
550 
551 /*
552  * Reference count inliners
553  */
554 static inline void
555 dasd_get_device(struct dasd_device *device)
556 {
557 	atomic_inc(&device->ref_count);
558 }
559 
560 static inline void
561 dasd_put_device(struct dasd_device *device)
562 {
563 	if (atomic_dec_return(&device->ref_count) == 0)
564 		dasd_put_device_wake(device);
565 }
566 
567 /*
568  * The static memory in ccw_mem and erp_mem is managed by a sorted
569  * list of free memory chunks.
570  */
571 struct dasd_mchunk
572 {
573 	struct list_head list;
574 	unsigned long size;
575 } __attribute__ ((aligned(8)));
576 
577 static inline void
578 dasd_init_chunklist(struct list_head *chunk_list, void *mem,
579 		    unsigned long size)
580 {
581 	struct dasd_mchunk *chunk;
582 
583 	INIT_LIST_HEAD(chunk_list);
584 	chunk = (struct dasd_mchunk *) mem;
585 	chunk->size = size - sizeof(struct dasd_mchunk);
586 	list_add(&chunk->list, chunk_list);
587 }
588 
589 static inline void *
590 dasd_alloc_chunk(struct list_head *chunk_list, unsigned long size)
591 {
592 	struct dasd_mchunk *chunk, *tmp;
593 
594 	size = (size + 7L) & -8L;
595 	list_for_each_entry(chunk, chunk_list, list) {
596 		if (chunk->size < size)
597 			continue;
598 		if (chunk->size > size + sizeof(struct dasd_mchunk)) {
599 			char *endaddr = (char *) (chunk + 1) + chunk->size;
600 			tmp = (struct dasd_mchunk *) (endaddr - size) - 1;
601 			tmp->size = size;
602 			chunk->size -= size + sizeof(struct dasd_mchunk);
603 			chunk = tmp;
604 		} else
605 			list_del(&chunk->list);
606 		return (void *) (chunk + 1);
607 	}
608 	return NULL;
609 }
610 
611 static inline void
612 dasd_free_chunk(struct list_head *chunk_list, void *mem)
613 {
614 	struct dasd_mchunk *chunk, *tmp;
615 	struct list_head *p, *left;
616 
617 	chunk = (struct dasd_mchunk *)
618 		((char *) mem - sizeof(struct dasd_mchunk));
619 	/* Find out the left neighbour in chunk_list. */
620 	left = chunk_list;
621 	list_for_each(p, chunk_list) {
622 		if (list_entry(p, struct dasd_mchunk, list) > chunk)
623 			break;
624 		left = p;
625 	}
626 	/* Try to merge with right neighbour = next element from left. */
627 	if (left->next != chunk_list) {
628 		tmp = list_entry(left->next, struct dasd_mchunk, list);
629 		if ((char *) (chunk + 1) + chunk->size == (char *) tmp) {
630 			list_del(&tmp->list);
631 			chunk->size += tmp->size + sizeof(struct dasd_mchunk);
632 		}
633 	}
634 	/* Try to merge with left neighbour. */
635 	if (left != chunk_list) {
636 		tmp = list_entry(left, struct dasd_mchunk, list);
637 		if ((char *) (tmp + 1) + tmp->size == (char *) chunk) {
638 			tmp->size += chunk->size + sizeof(struct dasd_mchunk);
639 			return;
640 		}
641 	}
642 	__list_add(&chunk->list, left, left->next);
643 }
644 
645 /*
646  * Check if bsize is in { 512, 1024, 2048, 4096 }
647  */
648 static inline int
649 dasd_check_blocksize(int bsize)
650 {
651 	if (bsize < 512 || bsize > 4096 || !is_power_of_2(bsize))
652 		return -EMEDIUMTYPE;
653 	return 0;
654 }
655 
656 /* externals in dasd.c */
657 #define DASD_PROFILE_OFF	 0
658 #define DASD_PROFILE_ON 	 1
659 #define DASD_PROFILE_GLOBAL_ONLY 2
660 
661 extern debug_info_t *dasd_debug_area;
662 extern struct dasd_profile dasd_global_profile;
663 extern unsigned int dasd_global_profile_level;
664 extern const struct block_device_operations dasd_device_operations;
665 
666 extern struct kmem_cache *dasd_page_cache;
667 
668 struct dasd_ccw_req *
669 dasd_kmalloc_request(int , int, int, struct dasd_device *);
670 struct dasd_ccw_req *
671 dasd_smalloc_request(int , int, int, struct dasd_device *);
672 void dasd_kfree_request(struct dasd_ccw_req *, struct dasd_device *);
673 void dasd_sfree_request(struct dasd_ccw_req *, struct dasd_device *);
674 void dasd_wakeup_cb(struct dasd_ccw_req *, void *);
675 
676 static inline int
677 dasd_kmalloc_set_cda(struct ccw1 *ccw, void *cda, struct dasd_device *device)
678 {
679 	return set_normalized_cda(ccw, cda);
680 }
681 
682 struct dasd_device *dasd_alloc_device(void);
683 void dasd_free_device(struct dasd_device *);
684 
685 struct dasd_block *dasd_alloc_block(void);
686 void dasd_free_block(struct dasd_block *);
687 
688 enum blk_eh_timer_return dasd_times_out(struct request *req);
689 
690 void dasd_enable_device(struct dasd_device *);
691 void dasd_set_target_state(struct dasd_device *, int);
692 void dasd_kick_device(struct dasd_device *);
693 void dasd_restore_device(struct dasd_device *);
694 void dasd_reload_device(struct dasd_device *);
695 
696 void dasd_add_request_head(struct dasd_ccw_req *);
697 void dasd_add_request_tail(struct dasd_ccw_req *);
698 int  dasd_start_IO(struct dasd_ccw_req *);
699 int  dasd_term_IO(struct dasd_ccw_req *);
700 void dasd_schedule_device_bh(struct dasd_device *);
701 void dasd_schedule_block_bh(struct dasd_block *);
702 int  dasd_sleep_on(struct dasd_ccw_req *);
703 int  dasd_sleep_on_queue(struct list_head *);
704 int  dasd_sleep_on_immediatly(struct dasd_ccw_req *);
705 int  dasd_sleep_on_interruptible(struct dasd_ccw_req *);
706 void dasd_device_set_timer(struct dasd_device *, int);
707 void dasd_device_clear_timer(struct dasd_device *);
708 void dasd_block_set_timer(struct dasd_block *, int);
709 void dasd_block_clear_timer(struct dasd_block *);
710 int  dasd_cancel_req(struct dasd_ccw_req *);
711 int dasd_flush_device_queue(struct dasd_device *);
712 int dasd_generic_probe (struct ccw_device *, struct dasd_discipline *);
713 void dasd_generic_remove (struct ccw_device *cdev);
714 int dasd_generic_set_online(struct ccw_device *, struct dasd_discipline *);
715 int dasd_generic_set_offline (struct ccw_device *cdev);
716 int dasd_generic_notify(struct ccw_device *, int);
717 int dasd_generic_last_path_gone(struct dasd_device *);
718 int dasd_generic_path_operational(struct dasd_device *);
719 void dasd_generic_shutdown(struct ccw_device *);
720 
721 void dasd_generic_handle_state_change(struct dasd_device *);
722 int dasd_generic_pm_freeze(struct ccw_device *);
723 int dasd_generic_restore_device(struct ccw_device *);
724 enum uc_todo dasd_generic_uc_handler(struct ccw_device *, struct irb *);
725 void dasd_generic_path_event(struct ccw_device *, int *);
726 int dasd_generic_verify_path(struct dasd_device *, __u8);
727 
728 int dasd_generic_read_dev_chars(struct dasd_device *, int, void *, int);
729 char *dasd_get_sense(struct irb *);
730 
731 void dasd_device_set_stop_bits(struct dasd_device *, int);
732 void dasd_device_remove_stop_bits(struct dasd_device *, int);
733 
734 int dasd_device_is_ro(struct dasd_device *);
735 
736 void dasd_profile_reset(struct dasd_profile *);
737 int dasd_profile_on(struct dasd_profile *);
738 void dasd_profile_off(struct dasd_profile *);
739 char *dasd_get_user_string(const char __user *, size_t);
740 
741 /* externals in dasd_devmap.c */
742 extern int dasd_max_devindex;
743 extern int dasd_probeonly;
744 extern int dasd_autodetect;
745 extern int dasd_nopav;
746 extern int dasd_nofcx;
747 
748 int dasd_devmap_init(void);
749 void dasd_devmap_exit(void);
750 
751 struct dasd_device *dasd_create_device(struct ccw_device *);
752 void dasd_delete_device(struct dasd_device *);
753 
754 int dasd_get_feature(struct ccw_device *, int);
755 int dasd_set_feature(struct ccw_device *, int, int);
756 
757 int dasd_add_sysfs_files(struct ccw_device *);
758 void dasd_remove_sysfs_files(struct ccw_device *);
759 
760 struct dasd_device *dasd_device_from_cdev(struct ccw_device *);
761 struct dasd_device *dasd_device_from_cdev_locked(struct ccw_device *);
762 struct dasd_device *dasd_device_from_devindex(int);
763 
764 void dasd_add_link_to_gendisk(struct gendisk *, struct dasd_device *);
765 struct dasd_device *dasd_device_from_gendisk(struct gendisk *);
766 
767 int dasd_parse(void);
768 int dasd_busid_known(const char *);
769 
770 /* externals in dasd_gendisk.c */
771 int  dasd_gendisk_init(void);
772 void dasd_gendisk_exit(void);
773 int dasd_gendisk_alloc(struct dasd_block *);
774 void dasd_gendisk_free(struct dasd_block *);
775 int dasd_scan_partitions(struct dasd_block *);
776 void dasd_destroy_partitions(struct dasd_block *);
777 
778 /* externals in dasd_ioctl.c */
779 int  dasd_ioctl(struct block_device *, fmode_t, unsigned int, unsigned long);
780 
781 /* externals in dasd_proc.c */
782 int dasd_proc_init(void);
783 void dasd_proc_exit(void);
784 
785 /* externals in dasd_erp.c */
786 struct dasd_ccw_req *dasd_default_erp_action(struct dasd_ccw_req *);
787 struct dasd_ccw_req *dasd_default_erp_postaction(struct dasd_ccw_req *);
788 struct dasd_ccw_req *dasd_alloc_erp_request(char *, int, int,
789 					    struct dasd_device *);
790 void dasd_free_erp_request(struct dasd_ccw_req *, struct dasd_device *);
791 void dasd_log_sense(struct dasd_ccw_req *, struct irb *);
792 void dasd_log_sense_dbf(struct dasd_ccw_req *cqr, struct irb *irb);
793 
794 /* externals in dasd_3990_erp.c */
795 struct dasd_ccw_req *dasd_3990_erp_action(struct dasd_ccw_req *);
796 void dasd_3990_erp_handle_sim(struct dasd_device *, char *);
797 
798 /* externals in dasd_eer.c */
799 #ifdef CONFIG_DASD_EER
800 int dasd_eer_init(void);
801 void dasd_eer_exit(void);
802 int dasd_eer_enable(struct dasd_device *);
803 void dasd_eer_disable(struct dasd_device *);
804 void dasd_eer_write(struct dasd_device *, struct dasd_ccw_req *cqr,
805 		    unsigned int id);
806 void dasd_eer_snss(struct dasd_device *);
807 
808 static inline int dasd_eer_enabled(struct dasd_device *device)
809 {
810 	return device->eer_cqr != NULL;
811 }
812 #else
813 #define dasd_eer_init()		(0)
814 #define dasd_eer_exit()		do { } while (0)
815 #define dasd_eer_enable(d)	(0)
816 #define dasd_eer_disable(d)	do { } while (0)
817 #define dasd_eer_write(d,c,i)	do { } while (0)
818 #define dasd_eer_snss(d)	do { } while (0)
819 #define dasd_eer_enabled(d)	(0)
820 #endif	/* CONFIG_DASD_ERR */
821 
822 #endif				/* DASD_H */
823