i2o_block.c 31.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 *	Block OSM
 *
 * 	Copyright (C) 1999-2002	Red Hat Software
 *
 *	Written by Alan Cox, Building Number Three Ltd
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation; either version 2 of the License, or (at your
 *	option) any later version.
 *
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	General Public License for more details.
 *
 *	For the purpose of avoiding doubt the preferred form of the work
 *	for making modifications shall be a standards compliant form such
 *	gzipped tar and not one requiring a proprietary or patent encumbered
 *	tool to unpack.
 *
 *	Fixes/additions:
 *		Steve Ralston:
 *			Multiple device handling error fixes,
 *			Added a queue depth.
 *		Alan Cox:
 *			FC920 has an rmw bug. Dont or in the end marker.
 *			Removed queue walk, fixed for 64bitness.
 *			Rewrote much of the code over time
 *			Added indirect block lists
 *			Handle 64K limits on many controllers
 *			Don't use indirects on the Promise (breaks)
 *			Heavily chop down the queue depths
 *		Deepak Saxena:
 *			Independent queues per IOP
 *			Support for dynamic device creation/deletion
 *			Code cleanup
 *	    		Support for larger I/Os through merge* functions
 *			(taken from DAC960 driver)
 *		Boji T Kannanthanam:
 *			Set the I2O Block devices to be detected in increasing
 *			order of TIDs during boot.
 *			Search and set the I2O block device that we boot off
 *			from as the first device to be claimed (as /dev/i2o/hda)
 *			Properly attach/detach I2O gendisk structure from the
 *			system gendisk list. The I2O block devices now appear in
 *			/proc/partitions.
 *		Markus Lidel <Markus.Lidel@shadowconnect.com>:
 *			Minor bugfixes for 2.6.
 */

#include <linux/module.h>
#include <linux/i2o.h>

#include <linux/mempool.h>

#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>

Markus Lidel's avatar
Markus Lidel committed
62
63
#include <scsi/scsi.h>

Linus Torvalds's avatar
Linus Torvalds committed
64
65
66
#include "i2o_block.h"

#define OSM_NAME	"block-osm"
Markus Lidel's avatar
Markus Lidel committed
67
#define OSM_VERSION	"1.325"
Linus Torvalds's avatar
Linus Torvalds committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define OSM_DESCRIPTION	"I2O Block Device OSM"

static struct i2o_driver i2o_block_driver;

/* global Block OSM request mempool */
static struct i2o_block_mempool i2o_blk_req_pool;

/* Block OSM class handling definition */
static struct i2o_class_id i2o_block_class_id[] = {
	{I2O_CLASS_RANDOM_BLOCK_STORAGE},
	{I2O_CLASS_END}
};

/**
 *	i2o_block_device_free - free the memory of the I2O Block device
 *	@dev: I2O Block device, which should be cleaned up
 *
 *	Frees the request queue, gendisk and the i2o_block_device structure.
 */
static void i2o_block_device_free(struct i2o_block_device *dev)
{
	blk_cleanup_queue(dev->gd->queue);

	put_disk(dev->gd);

	kfree(dev);
};

/**
 *	i2o_block_remove - remove the I2O Block device from the system again
 *	@dev: I2O Block device which should be removed
 *
 *	Remove gendisk from system and free all allocated memory.
 *
 *	Always returns 0.
 */
static int i2o_block_remove(struct device *dev)
{
	struct i2o_device *i2o_dev = to_i2o_device(dev);
	struct i2o_block_device *i2o_blk_dev = dev_get_drvdata(dev);

109
110
	osm_info("device removed (TID: %03x): %s\n", i2o_dev->lct_data.tid,
		 i2o_blk_dev->gd->disk_name);
Linus Torvalds's avatar
Linus Torvalds committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

	i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0);

	del_gendisk(i2o_blk_dev->gd);

	dev_set_drvdata(dev, NULL);

	i2o_device_claim_release(i2o_dev);

	i2o_block_device_free(i2o_blk_dev);

	return 0;
};

/**
 *	i2o_block_device flush - Flush all dirty data of I2O device dev
 *	@dev: I2O device which should be flushed
 *
 *	Flushes all dirty data on device dev.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_device_flush(struct i2o_device *dev)
{
135
	struct i2o_message *msg;
Linus Torvalds's avatar
Linus Torvalds committed
136

137
138
139
	msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
	if (IS_ERR(msg))
		return PTR_ERR(msg);
Linus Torvalds's avatar
Linus Torvalds committed
140

141
142
143
144
145
	msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
	msg->u.head[1] =
	    cpu_to_le32(I2O_CMD_BLOCK_CFLUSH << 24 | HOST_TID << 12 | dev->
			lct_data.tid);
	msg->body[0] = cpu_to_le32(60 << 16);
Linus Torvalds's avatar
Linus Torvalds committed
146
147
	osm_debug("Flushing...\n");

148
	return i2o_msg_post_wait(dev->iop, msg, 60);
Linus Torvalds's avatar
Linus Torvalds committed
149
150
};

151
152
153
154
155
156
157
158
159
160
161
/**
 *	i2o_block_issue_flush - device-flush interface for block-layer
 *	@queue: the request queue of the device which should be flushed
 *	@disk: gendisk
 *	@error_sector: error offset
 *
 *	Helper function to provide flush functionality to block-layer.
 *
 *	Returns 0 on success or negative error code on failure.
 */

162
static int i2o_block_issue_flush(struct request_queue * queue, struct gendisk *disk,
163
164
165
166
167
168
169
170
171
172
173
				 sector_t * error_sector)
{
	struct i2o_block_device *i2o_blk_dev = queue->queuedata;
	int rc = -ENODEV;

	if (likely(i2o_blk_dev))
		rc = i2o_block_device_flush(i2o_blk_dev->i2o_dev);

	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
174
175
176
177
178
179
180
181
182
183
184
185
/**
 *	i2o_block_device_mount - Mount (load) the media of device dev
 *	@dev: I2O device which should receive the mount request
 *	@media_id: Media Identifier
 *
 *	Load a media into drive. Identifier should be set to -1, because the
 *	spec does not support any other value.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
{
186
187
188
189
190
191
192
193
194
195
196
197
	struct i2o_message *msg;

	msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
	if (IS_ERR(msg))
		return PTR_ERR(msg);

	msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
	msg->u.head[1] =
	    cpu_to_le32(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->
			lct_data.tid);
	msg->body[0] = cpu_to_le32(-1);
	msg->body[1] = cpu_to_le32(0x00000000);
Linus Torvalds's avatar
Linus Torvalds committed
198
199
	osm_debug("Mounting...\n");

200
	return i2o_msg_post_wait(dev->iop, msg, 2);
Linus Torvalds's avatar
Linus Torvalds committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
};

/**
 *	i2o_block_device_lock - Locks the media of device dev
 *	@dev: I2O device which should receive the lock request
 *	@media_id: Media Identifier
 *
 *	Lock media of device dev to prevent removal. The media identifier
 *	should be set to -1, because the spec does not support any other value.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
{
215
	struct i2o_message *msg;
Linus Torvalds's avatar
Linus Torvalds committed
216

217
	msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
218
	if (IS_ERR(msg))
219
		return PTR_ERR(msg);
Linus Torvalds's avatar
Linus Torvalds committed
220

221
222
223
224
225
	msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
	msg->u.head[1] =
	    cpu_to_le32(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->
			lct_data.tid);
	msg->body[0] = cpu_to_le32(-1);
Linus Torvalds's avatar
Linus Torvalds committed
226
227
	osm_debug("Locking...\n");

228
	return i2o_msg_post_wait(dev->iop, msg, 2);
Linus Torvalds's avatar
Linus Torvalds committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
};

/**
 *	i2o_block_device_unlock - Unlocks the media of device dev
 *	@dev: I2O device which should receive the unlocked request
 *	@media_id: Media Identifier
 *
 *	Unlocks the media in device dev. The media identifier should be set to
 *	-1, because the spec does not support any other value.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
{
243
	struct i2o_message *msg;
Linus Torvalds's avatar
Linus Torvalds committed
244

245
246
247
	msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
	if (IS_ERR(msg))
		return PTR_ERR(msg);
Linus Torvalds's avatar
Linus Torvalds committed
248

249
250
251
252
253
	msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
	msg->u.head[1] =
	    cpu_to_le32(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->
			lct_data.tid);
	msg->body[0] = cpu_to_le32(media_id);
Linus Torvalds's avatar
Linus Torvalds committed
254
255
	osm_debug("Unlocking...\n");

256
	return i2o_msg_post_wait(dev->iop, msg, 2);
Linus Torvalds's avatar
Linus Torvalds committed
257
258
259
260
261
};

/**
 *	i2o_block_device_power - Power management for device dev
 *	@dev: I2O device which should receive the power management request
262
 *	@op: Operation to send
Linus Torvalds's avatar
Linus Torvalds committed
263
264
265
266
267
268
269
270
271
 *
 *	Send a power management request to the device dev.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
{
	struct i2o_device *i2o_dev = dev->i2o_dev;
	struct i2o_controller *c = i2o_dev->iop;
272
	struct i2o_message *msg;
Linus Torvalds's avatar
Linus Torvalds committed
273
274
	int rc;

275
276
277
	msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
	if (IS_ERR(msg))
		return PTR_ERR(msg);
Linus Torvalds's avatar
Linus Torvalds committed
278

279
280
281
282
283
	msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
	msg->u.head[1] =
	    cpu_to_le32(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->
			lct_data.tid);
	msg->body[0] = cpu_to_le32(op << 24);
Linus Torvalds's avatar
Linus Torvalds committed
284
285
	osm_debug("Power...\n");

286
	rc = i2o_msg_post_wait(c, msg, 60);
Linus Torvalds's avatar
Linus Torvalds committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
	if (!rc)
		dev->power = op;

	return rc;
};

/**
 *	i2o_block_request_alloc - Allocate an I2O block request struct
 *
 *	Allocates an I2O block request struct and initialize the list.
 *
 *	Returns a i2o_block_request pointer on success or negative error code
 *	on failure.
 */
static inline struct i2o_block_request *i2o_block_request_alloc(void)
{
	struct i2o_block_request *ireq;

	ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
	if (!ireq)
		return ERR_PTR(-ENOMEM);

	INIT_LIST_HEAD(&ireq->queue);

	return ireq;
};

/**
 *	i2o_block_request_free - Frees a I2O block request
 *	@ireq: I2O block request which should be freed
 *
318
 *	Frees the allocated memory (give it back to the request mempool).
Linus Torvalds's avatar
Linus Torvalds committed
319
320
321
322
323
324
325
326
 */
static inline void i2o_block_request_free(struct i2o_block_request *ireq)
{
	mempool_free(ireq, i2o_blk_req_pool.pool);
};

/**
 *	i2o_block_sglist_alloc - Allocate the SG list and map it
327
 *	@c: I2O controller to which the request belongs
Linus Torvalds's avatar
Linus Torvalds committed
328
 *	@ireq: I2O block request
329
 *	@mptr: message body pointer
Linus Torvalds's avatar
Linus Torvalds committed
330
 *
331
 *	Builds the SG list and map it to be accessable by the controller.
Linus Torvalds's avatar
Linus Torvalds committed
332
 *
333
 *	Returns 0 on failure or 1 on success.
Linus Torvalds's avatar
Linus Torvalds committed
334
 */
335
336
static inline int i2o_block_sglist_alloc(struct i2o_controller *c,
					 struct i2o_block_request *ireq,
337
					 u32 ** mptr)
Linus Torvalds's avatar
Linus Torvalds committed
338
339
{
	int nents;
340
	enum dma_data_direction direction;
Linus Torvalds's avatar
Linus Torvalds committed
341

342
	ireq->dev = &c->pdev->dev;
Linus Torvalds's avatar
Linus Torvalds committed
343
344
345
	nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);

	if (rq_data_dir(ireq->req) == READ)
346
		direction = PCI_DMA_FROMDEVICE;
Linus Torvalds's avatar
Linus Torvalds committed
347
	else
348
		direction = PCI_DMA_TODEVICE;
Linus Torvalds's avatar
Linus Torvalds committed
349

350
	ireq->sg_nents = nents;
Linus Torvalds's avatar
Linus Torvalds committed
351

352
	return i2o_dma_map_sg(c, ireq->sg_table, nents, direction, mptr);
Linus Torvalds's avatar
Linus Torvalds committed
353
354
355
356
357
358
359
360
361
362
};

/**
 *	i2o_block_sglist_free - Frees the SG list
 *	@ireq: I2O block request from which the SG should be freed
 *
 *	Frees the SG list from the I2O block request.
 */
static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
{
363
	enum dma_data_direction direction;
Linus Torvalds's avatar
Linus Torvalds committed
364

365
366
367
368
369
370
	if (rq_data_dir(ireq->req) == READ)
		direction = PCI_DMA_FROMDEVICE;
	else
		direction = PCI_DMA_TODEVICE;

	dma_unmap_sg(ireq->dev, ireq->sg_table, ireq->sg_nents, direction);
Linus Torvalds's avatar
Linus Torvalds committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
};

/**
 *	i2o_block_prep_req_fn - Allocates I2O block device specific struct
 *	@q: request queue for the request
 *	@req: the request to prepare
 *
 *	Allocate the necessary i2o_block_request struct and connect it to
 *	the request. This is needed that we not loose the SG list later on.
 *
 *	Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
 */
static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
{
	struct i2o_block_device *i2o_blk_dev = q->queuedata;
	struct i2o_block_request *ireq;

388
389
390
391
392
	if (unlikely(!i2o_blk_dev)) {
		osm_err("block device already removed\n");
		return BLKPREP_KILL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
393
394
395
396
397
398
399
400
401
402
403
	/* connect the i2o_block_request to the request */
	if (!req->special) {
		ireq = i2o_block_request_alloc();
		if (unlikely(IS_ERR(ireq))) {
			osm_debug("unable to allocate i2o_block_request!\n");
			return BLKPREP_DEFER;
		}

		ireq->i2o_blk_dev = i2o_blk_dev;
		req->special = ireq;
		ireq->req = req;
404
	}
Linus Torvalds's avatar
Linus Torvalds committed
405
	/* do not come back here */
406
	req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds's avatar
Linus Torvalds committed
407
408
409
410
411
412

	return BLKPREP_OK;
};

/**
 *	i2o_block_delayed_request_fn - delayed request queue function
David Howells's avatar
David Howells committed
413
 *	@work: the delayed request with the queue to start
Linus Torvalds's avatar
Linus Torvalds committed
414
415
416
417
418
419
 *
 *	If the request queue is stopped for a disk, and there is no open
 *	request, a new event is created, which calls this function to start
 *	the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
 *	be started again.
 */
David Howells's avatar
David Howells committed
420
static void i2o_block_delayed_request_fn(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
421
{
David Howells's avatar
David Howells committed
422
423
424
	struct i2o_block_delayed_request *dreq =
		container_of(work, struct i2o_block_delayed_request,
			     work.work);
Linus Torvalds's avatar
Linus Torvalds committed
425
426
427
428
429
430
431
432
433
434
	struct request_queue *q = dreq->queue;
	unsigned long flags;

	spin_lock_irqsave(q->queue_lock, flags);
	blk_start_queue(q);
	spin_unlock_irqrestore(q->queue_lock, flags);
	kfree(dreq);
};

/**
435
436
437
438
 *	i2o_block_end_request - Post-processing of completed commands
 *	@req: request which should be completed
 *	@uptodate: 1 for success, 0 for I/O error, < 0 for specific error
 *	@nr_bytes: number of bytes to complete
Linus Torvalds's avatar
Linus Torvalds committed
439
 *
440
 *	Mark the request as complete. The lock must not be held when entering.
Linus Torvalds's avatar
Linus Torvalds committed
441
442
 *
 */
443
444
static void i2o_block_end_request(struct request *req, int uptodate,
				  int nr_bytes)
Linus Torvalds's avatar
Linus Torvalds committed
445
{
446
447
	struct i2o_block_request *ireq = req->special;
	struct i2o_block_device *dev = ireq->i2o_blk_dev;
448
	struct request_queue *q = req->q;
Linus Torvalds's avatar
Linus Torvalds committed
449
450
	unsigned long flags;

451
	if (end_that_request_chunk(req, uptodate, nr_bytes)) {
452
		int leftover = (req->hard_nr_sectors << KERNEL_SECTOR_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
453

454
455
		if (blk_pc_request(req))
			leftover = req->data_len;
Linus Torvalds's avatar
Linus Torvalds committed
456

457
458
459
		if (end_io_error(uptodate))
			end_that_request_chunk(req, 0, leftover);
	}
Linus Torvalds's avatar
Linus Torvalds committed
460

461
	add_disk_randomness(req->rq_disk);
Linus Torvalds's avatar
Linus Torvalds committed
462

463
	spin_lock_irqsave(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
464

465
	end_that_request_last(req, uptodate);
466
467
468
469
470

	if (likely(dev)) {
		dev->open_queue_depth--;
		list_del(&ireq->queue);
	}
Linus Torvalds's avatar
Linus Torvalds committed
471

472
	blk_start_queue(q);
Linus Torvalds's avatar
Linus Torvalds committed
473

474
	spin_unlock_irqrestore(q->queue_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
475

476
477
478
	i2o_block_sglist_free(ireq);
	i2o_block_request_free(ireq);
};
Linus Torvalds's avatar
Linus Torvalds committed
479

480
481
482
483
/**
 *	i2o_block_reply - Block OSM reply handler.
 *	@c: I2O controller from which the message arrives
 *	@m: message id of reply
484
 *	@msg: the actual I2O message reply
485
486
487
488
489
490
491
492
493
 *
 *	This function gets all the message replies.
 *
 */
static int i2o_block_reply(struct i2o_controller *c, u32 m,
			   struct i2o_message *msg)
{
	struct request *req;
	int uptodate = 1;
Linus Torvalds's avatar
Linus Torvalds committed
494
495
496
497
498
499
500
501
502
503
504
505

	req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
	if (unlikely(!req)) {
		osm_err("NULL reply received!\n");
		return -1;
	}

	/*
	 *      Lets see what is cooking. We stuffed the
	 *      request in the context.
	 */

506
507
	if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
		u32 status = le32_to_cpu(msg->body[0]);
Linus Torvalds's avatar
Linus Torvalds committed
508
509
510
511
512
513
514
515
516
517
518
519
		/*
		 *      Device not ready means two things. One is that the
		 *      the thing went offline (but not a removal media)
		 *
		 *      The second is that you have a SuperTrak 100 and the
		 *      firmware got constipated. Unlike standard i2o card
		 *      setups the supertrak returns an error rather than
		 *      blocking for the timeout in these cases.
		 *
		 *      Don't stick a supertrak100 into cache aggressive modes
		 */

520
521
		osm_err("TID %03x error status: 0x%02x, detailed status: "
			"0x%04x\n", (le32_to_cpu(msg->u.head[1]) >> 12 & 0xfff),
522
			status >> 24, status & 0xffff);
Linus Torvalds's avatar
Linus Torvalds committed
523

524
		req->errors++;
Linus Torvalds's avatar
Linus Torvalds committed
525

526
527
		uptodate = 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
528

529
	i2o_block_end_request(req, uptodate, le32_to_cpu(msg->body[1]));
Linus Torvalds's avatar
Linus Torvalds committed
530
531
532
533

	return 1;
};

David Howells's avatar
David Howells committed
534
static void i2o_block_event(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
535
{
David Howells's avatar
David Howells committed
536
	struct i2o_event *evt = container_of(work, struct i2o_event, work);
537
	osm_debug("event received\n");
538
	kfree(evt);
Linus Torvalds's avatar
Linus Torvalds committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
};

/*
 *	SCSI-CAM for ioctl geometry mapping
 *	Duplicated with SCSI - this should be moved into somewhere common
 *	perhaps genhd ?
 *
 * LBA -> CHS mapping table taken from:
 *
 * "Incorporating the I2O Architecture into BIOS for Intel Architecture
 *  Platforms"
 *
 * This is an I2O document that is only available to I2O members,
 * not developers.
 *
 * From my understanding, this is how all the I2O cards do this
 *
 * Disk Size      | Sectors | Heads | Cylinders
 * ---------------+---------+-------+-------------------
 * 1 < X <= 528M  | 63      | 16    | X/(63 * 16 * 512)
 * 528M < X <= 1G | 63      | 32    | X/(63 * 32 * 512)
 * 1 < X <528M    | 63      | 16    | X/(63 * 16 * 512)
 * 1 < X <528M    | 63      | 16    | X/(63 * 16 * 512)
 *
 */
#define	BLOCK_SIZE_528M		1081344
#define	BLOCK_SIZE_1G		2097152
#define	BLOCK_SIZE_21G		4403200
#define	BLOCK_SIZE_42G		8806400
#define	BLOCK_SIZE_84G		17612800

static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
				unsigned char *hds, unsigned char *secs)
{
	unsigned long heads, sectors, cylinders;

	sectors = 63L;		/* Maximize sectors per track */
	if (capacity <= BLOCK_SIZE_528M)
		heads = 16;
	else if (capacity <= BLOCK_SIZE_1G)
		heads = 32;
	else if (capacity <= BLOCK_SIZE_21G)
		heads = 64;
	else if (capacity <= BLOCK_SIZE_42G)
		heads = 128;
	else
		heads = 255;

	cylinders = (unsigned long)capacity / (heads * sectors);

	*cyls = (unsigned short)cylinders;	/* Stuff return values */
	*secs = (unsigned char)sectors;
	*hds = (unsigned char)heads;
}

/**
 *	i2o_block_open - Open the block device
596
597
 *	@inode: inode for block device being opened
 *	@file: file to open
Linus Torvalds's avatar
Linus Torvalds committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
 *
 *	Power up the device, mount and lock the media. This function is called,
 *	if the block device is opened for access.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_open(struct inode *inode, struct file *file)
{
	struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;

	if (!dev->i2o_dev)
		return -ENODEV;

	if (dev->power > 0x1f)
		i2o_block_device_power(dev, 0x02);

	i2o_block_device_mount(dev->i2o_dev, -1);

	i2o_block_device_lock(dev->i2o_dev, -1);

	osm_debug("Ready.\n");

	return 0;
};

/**
 *	i2o_block_release - Release the I2O block device
625
626
 *	@inode: inode for block device being released
 *	@file: file to close
Linus Torvalds's avatar
Linus Torvalds committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
 *
 *	Unlock and unmount the media, and power down the device. Gets called if
 *	the block device is closed.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_release(struct inode *inode, struct file *file)
{
	struct gendisk *disk = inode->i_bdev->bd_disk;
	struct i2o_block_device *dev = disk->private_data;
	u8 operation;

	/*
	 * This is to deail with the case of an application
	 * opening a device and then the device dissapears while
	 * it's in use, and then the application tries to release
	 * it.  ex: Unmounting a deleted RAID volume at reboot.
	 * If we send messages, it will just cause FAILs since
	 * the TID no longer exists.
	 */
	if (!dev->i2o_dev)
		return 0;

	i2o_block_device_flush(dev->i2o_dev);

	i2o_block_device_unlock(dev->i2o_dev, -1);

	if (dev->flags & (1 << 3 | 1 << 4))	/* Removable */
		operation = 0x21;
	else
		operation = 0x24;

	i2o_block_device_power(dev, operation);

	return 0;
}

664
665
666
667
668
669
670
static int i2o_block_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
	i2o_block_biosparam(get_capacity(bdev->bd_disk),
			    &geo->cylinders, &geo->heads, &geo->sectors);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
671
672
/**
 *	i2o_block_ioctl - Issue device specific ioctl calls.
673
674
 *	@inode: inode for block device ioctl
 *	@file: file for ioctl
Linus Torvalds's avatar
Linus Torvalds committed
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
 *	@cmd: ioctl command
 *	@arg: arg
 *
 *	Handles ioctl request for the block device.
 *
 *	Return 0 on success or negative error on failure.
 */
static int i2o_block_ioctl(struct inode *inode, struct file *file,
			   unsigned int cmd, unsigned long arg)
{
	struct gendisk *disk = inode->i_bdev->bd_disk;
	struct i2o_block_device *dev = disk->private_data;

	/* Anyone capable of this syscall can do *real bad* things */

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	switch (cmd) {
	case BLKI2OGRSTRAT:
		return put_user(dev->rcache, (int __user *)arg);
	case BLKI2OGWSTRAT:
		return put_user(dev->wcache, (int __user *)arg);
	case BLKI2OSRSTRAT:
		if (arg < 0 || arg > CACHE_SMARTFETCH)
			return -EINVAL;
		dev->rcache = arg;
		break;
	case BLKI2OSWSTRAT:
		if (arg != 0
		    && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
			return -EINVAL;
		dev->wcache = arg;
		break;
	}
	return -ENOTTY;
};

/**
 *	i2o_block_media_changed - Have we seen a media change?
 *	@disk: gendisk which should be verified
 *
 *	Verifies if the media has changed.
 *
 *	Returns 1 if the media was changed or 0 otherwise.
 */
static int i2o_block_media_changed(struct gendisk *disk)
{
	struct i2o_block_device *p = disk->private_data;

	if (p->media_change_flag) {
		p->media_change_flag = 0;
		return 1;
	}
	return 0;
}

/**
 *	i2o_block_transfer - Transfer a request to/from the I2O controller
 *	@req: the request which should be transfered
 *
 *	This function converts the request into a I2O message. The necessary
 *	DMA buffers are allocated and after everything is setup post the message
 *	to the I2O controller. No cleanup is done by this function. It is done
 *	on the interrupt side when the reply arrives.
 *
 *	Return 0 on success or negative error code on failure.
 */
static int i2o_block_transfer(struct request *req)
{
	struct i2o_block_device *dev = req->rq_disk->private_data;
746
	struct i2o_controller *c;
747
	u32 tid = dev->i2o_dev->lct_data.tid;
748
749
	struct i2o_message *msg;
	u32 *mptr;
Linus Torvalds's avatar
Linus Torvalds committed
750
751
	struct i2o_block_request *ireq = req->special;
	u32 tcntxt;
752
753
	u32 sgl_offset = SGL_OFFSET_8;
	u32 ctl_flags = 0x00000000;
Linus Torvalds's avatar
Linus Torvalds committed
754
	int rc;
755
756
757
758
759
760
761
762
763
	u32 cmd;

	if (unlikely(!dev->i2o_dev)) {
		osm_err("transfer to removed drive\n");
		rc = -ENODEV;
		goto exit;
	}

	c = dev->i2o_dev->iop;
Linus Torvalds's avatar
Linus Torvalds committed
764

765
766
767
	msg = i2o_msg_get(c);
	if (IS_ERR(msg)) {
		rc = PTR_ERR(msg);
Linus Torvalds's avatar
Linus Torvalds committed
768
769
770
771
772
773
774
775
776
		goto exit;
	}

	tcntxt = i2o_cntxt_list_add(c, req);
	if (!tcntxt) {
		rc = -ENOMEM;
		goto nop_msg;
	}

777
778
	msg->u.s.icntxt = cpu_to_le32(i2o_block_driver.context);
	msg->u.s.tcntxt = cpu_to_le32(tcntxt);
Linus Torvalds's avatar
Linus Torvalds committed
779

780
	mptr = &msg->body[0];
Linus Torvalds's avatar
Linus Torvalds committed
781
782

	if (rq_data_dir(req) == READ) {
783
784
		cmd = I2O_CMD_BLOCK_READ << 24;

Linus Torvalds's avatar
Linus Torvalds committed
785
786
		switch (dev->rcache) {
		case CACHE_PREFETCH:
787
			ctl_flags = 0x201F0008;
Linus Torvalds's avatar
Linus Torvalds committed
788
			break;
789

Linus Torvalds's avatar
Linus Torvalds committed
790
791
		case CACHE_SMARTFETCH:
			if (req->nr_sectors > 16)
792
				ctl_flags = 0x201F0008;
Linus Torvalds's avatar
Linus Torvalds committed
793
			else
794
795
796
797
				ctl_flags = 0x001F0000;
			break;

		default:
Linus Torvalds's avatar
Linus Torvalds committed
798
799
800
			break;
		}
	} else {
801
802
		cmd = I2O_CMD_BLOCK_WRITE << 24;

Linus Torvalds's avatar
Linus Torvalds committed
803
804
		switch (dev->wcache) {
		case CACHE_WRITETHROUGH:
805
			ctl_flags = 0x001F0008;
Linus Torvalds's avatar
Linus Torvalds committed
806
807
			break;
		case CACHE_WRITEBACK:
808
			ctl_flags = 0x001F0010;
Linus Torvalds's avatar
Linus Torvalds committed
809
810
811
			break;
		case CACHE_SMARTBACK:
			if (req->nr_sectors > 16)
812
				ctl_flags = 0x001F0004;
Linus Torvalds's avatar
Linus Torvalds committed
813
			else
814
				ctl_flags = 0x001F0010;
Linus Torvalds's avatar
Linus Torvalds committed
815
816
817
			break;
		case CACHE_SMARTTHROUGH:
			if (req->nr_sectors > 16)
818
				ctl_flags = 0x001F0004;
Linus Torvalds's avatar
Linus Torvalds committed
819
			else
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
				ctl_flags = 0x001F0010;
		default:
			break;
		}
	}

#ifdef CONFIG_I2O_EXT_ADAPTEC
	if (c->adaptec) {
		u8 cmd[10];
		u32 scsi_flags;
		u16 hwsec = queue_hardsect_size(req->q) >> KERNEL_SECTOR_SHIFT;

		memset(cmd, 0, 10);

		sgl_offset = SGL_OFFSET_12;

836
837
		msg->u.head[1] =
		    cpu_to_le32(I2O_CMD_PRIVATE << 24 | HOST_TID << 12 | tid);
838

839
840
		*mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
		*mptr++ = cpu_to_le32(tid);
841
842
843
844
845
846
847

		/*
		 * ENABLE_DISCONNECT
		 * SIMPLE_TAG
		 * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
		 */
		if (rq_data_dir(req) == READ) {
Markus Lidel's avatar
Markus Lidel committed
848
			cmd[0] = READ_10;
849
850
			scsi_flags = 0x60a0000a;
		} else {
Markus Lidel's avatar
Markus Lidel committed
851
			cmd[0] = WRITE_10;
852
			scsi_flags = 0xa0a0000a;
Linus Torvalds's avatar
Linus Torvalds committed
853
		}
854

855
		*mptr++ = cpu_to_le32(scsi_flags);
856
857
858
859

		*((u32 *) & cmd[2]) = cpu_to_be32(req->sector * hwsec);
		*((u16 *) & cmd[7]) = cpu_to_be16(req->nr_sectors * hwsec);

860
		memcpy(mptr, cmd, 10);
861
		mptr += 4;
862
		*mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
863
864
865
	} else
#endif
	{
866
867
868
869
870
871
872
		msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
		*mptr++ = cpu_to_le32(ctl_flags);
		*mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
		*mptr++ =
		    cpu_to_le32((u32) (req->sector << KERNEL_SECTOR_SHIFT));
		*mptr++ =
		    cpu_to_le32(req->sector >> (32 - KERNEL_SECTOR_SHIFT));
Linus Torvalds's avatar
Linus Torvalds committed
873
874
	}

875
876
877
	if (!i2o_block_sglist_alloc(c, ireq, &mptr)) {
		rc = -ENOMEM;
		goto context_remove;
Linus Torvalds's avatar
Linus Torvalds committed
878
879
	}

880
881
	msg->u.head[0] =
	    cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
Linus Torvalds's avatar
Linus Torvalds committed
882
883
884
885

	list_add_tail(&ireq->queue, &dev->open_queue);
	dev->open_queue_depth++;

886
	i2o_msg_post(c, msg);
Linus Torvalds's avatar
Linus Torvalds committed
887
888
889
890
891
892
893

	return 0;

      context_remove:
	i2o_cntxt_list_remove(c, req);

      nop_msg:
894
	i2o_msg_nop(c, msg);
Linus Torvalds's avatar
Linus Torvalds committed
895
896
897
898
899
900
901

      exit:
	return rc;
};

/**
 *	i2o_block_request_fn - request queue handling function
902
 *	@q: request queue from which the request could be fetched
Linus Torvalds's avatar
Linus Torvalds committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
 *
 *	Takes the next request from the queue, transfers it and if no error
 *	occurs dequeue it from the queue. On arrival of the reply the message
 *	will be processed further. If an error occurs requeue the request.
 */
static void i2o_block_request_fn(struct request_queue *q)
{
	struct request *req;

	while (!blk_queue_plugged(q)) {
		req = elv_next_request(q);
		if (!req)
			break;

		if (blk_fs_request(req)) {
			struct i2o_block_delayed_request *dreq;
			struct i2o_block_request *ireq = req->special;
			unsigned int queue_depth;

			queue_depth = ireq->i2o_blk_dev->open_queue_depth;

924
			if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS) {
Linus Torvalds's avatar
Linus Torvalds committed
925
926
927
				if (!i2o_block_transfer(req)) {
					blkdev_dequeue_request(req);
					continue;
928
929
930
				} else
					osm_info("transfer error\n");
			}
Linus Torvalds's avatar
Linus Torvalds committed
931
932
933
934
935
936
937
938
939
940

			if (queue_depth)
				break;

			/* stop the queue and retry later */
			dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
			if (!dreq)
				continue;

			dreq->queue = q;
David Howells's avatar
David Howells committed
941
942
			INIT_DELAYED_WORK(&dreq->work,
					  i2o_block_delayed_request_fn);
Linus Torvalds's avatar
Linus Torvalds committed
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962

			if (!queue_delayed_work(i2o_block_driver.event_queue,
						&dreq->work,
						I2O_BLOCK_RETRY_TIME))
				kfree(dreq);
			else {
				blk_stop_queue(q);
				break;
			}
		} else
			end_request(req, 0);
	}
};

/* I2O Block device operations definition */
static struct block_device_operations i2o_block_fops = {
	.owner = THIS_MODULE,
	.open = i2o_block_open,
	.release = i2o_block_release,
	.ioctl = i2o_block_ioctl,
963
	.getgeo = i2o_block_getgeo,
Linus Torvalds's avatar
Linus Torvalds committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
	.media_changed = i2o_block_media_changed
};

/**
 *	i2o_block_device_alloc - Allocate memory for a I2O Block device
 *
 *	Allocate memory for the i2o_block_device struct, gendisk and request
 *	queue and initialize them as far as no additional information is needed.
 *
 *	Returns a pointer to the allocated I2O Block device on succes or a
 *	negative error code on failure.
 */
static struct i2o_block_device *i2o_block_device_alloc(void)
{
	struct i2o_block_device *dev;
	struct gendisk *gd;
	struct request_queue *queue;
	int rc;

Markus Lidel's avatar
Markus Lidel committed
983
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
	if (!dev) {
		osm_err("Insufficient memory to allocate I2O Block disk.\n");
		rc = -ENOMEM;
		goto exit;
	}

	INIT_LIST_HEAD(&dev->open_queue);
	spin_lock_init(&dev->lock);
	dev->rcache = CACHE_PREFETCH;
	dev->wcache = CACHE_WRITEBACK;

	/* allocate a gendisk with 16 partitions */
	gd = alloc_disk(16);
	if (!gd) {
		osm_err("Insufficient memory to allocate gendisk.\n");
		rc = -ENOMEM;
		goto cleanup_dev;
	}

	/* initialize the request queue */
	queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
	if (!queue) {
		osm_err("Insufficient memory to allocate request queue.\n");
		rc = -ENOMEM;
		goto cleanup_queue;
	}

	blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
1012
	blk_queue_issue_flush_fn(queue, i2o_block_issue_flush);
Linus Torvalds's avatar
Linus Torvalds committed
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

	gd->major = I2O_MAJOR;
	gd->queue = queue;
	gd->fops = &i2o_block_fops;
	gd->private_data = dev;

	dev->gd = gd;

	return dev;

      cleanup_queue:
	put_disk(gd);

      cleanup_dev:
	kfree(dev);

      exit:
	return ERR_PTR(rc);
};

/**
 *	i2o_block_probe - verify if dev is a I2O Block device and install it
 *	@dev: device to verify if it is a I2O Block device
 *
 *	We only verify if the user_tid of the device is 0xfff and then install
 *	the device. Otherwise it is used by some other device (e. g. RAID).
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int i2o_block_probe(struct device *dev)
{
	struct i2o_device *i2o_dev = to_i2o_device(dev);
	struct i2o_controller *c = i2o_dev->iop;
1046
	struct i2o_block_device *i2o_blk_dev;
Linus Torvalds's avatar
Linus Torvalds committed
1047
1048
1049
1050
1051
1052
	struct gendisk *gd;
	struct request_queue *queue;
	static int unit = 0;
	int rc;
	u64 size;
	u32 blocksize;
1053
	u16 body_size = 4;
Markus Lidel's avatar
Markus Lidel committed
1054
	u16 power;
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
	unsigned short max_sectors;

#ifdef CONFIG_I2O_EXT_ADAPTEC
	if (c->adaptec)
		body_size = 8;
#endif

	if (c->limit_sectors)
		max_sectors = I2O_MAX_SECTORS_LIMITED;
	else
		max_sectors = I2O_MAX_SECTORS;
Linus Torvalds's avatar
Linus Torvalds committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098

	/* skip devices which are used by IOP */
	if (i2o_dev->lct_data.user_tid != 0xfff) {
		osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
		return -ENODEV;
	}

	if (i2o_device_claim(i2o_dev)) {
		osm_warn("Unable to claim device. Installation aborted\n");
		rc = -EFAULT;
		goto exit;
	}

	i2o_blk_dev = i2o_block_device_alloc();
	if (IS_ERR(i2o_blk_dev)) {
		osm_err("could not alloc a new I2O block device");
		rc = PTR_ERR(i2o_blk_dev);
		goto claim_release;
	}

	i2o_blk_dev->i2o_dev = i2o_dev;
	dev_set_drvdata(dev, i2o_blk_dev);

	/* setup gendisk */
	gd = i2o_blk_dev->gd;
	gd->first_minor = unit << 4;
	sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
	gd->driverfs_dev = &i2o_dev->device;

	/* setup request queue */
	queue = gd->queue;
	queue->queuedata = i2o_blk_dev;

1099
1100
1101
	blk_queue_max_phys_segments(queue, I2O_MAX_PHYS_SEGMENTS);
	blk_queue_max_sectors(queue, max_sectors);
	blk_queue_max_hw_segments(queue, i2o_sg_tablesize(c, body_size));
Linus Torvalds's avatar
Linus Torvalds committed
1102

1103
1104
1105
	osm_debug("max sectors = %d\n", queue->max_phys_segments);
	osm_debug("phys segments = %d\n", queue->max_sectors);
	osm_debug("max hw segments = %d\n", queue->max_hw_segments);
Linus Torvalds's avatar
Linus Torvalds committed
1106
1107
1108
1109
1110

	/*
	 *      Ask for the current media data. If that isn't supported
	 *      then we ask for the device capacity data
	 */
Markus Lidel's avatar
Markus Lidel committed
1111
1112
1113
	if (!i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) ||
	    !i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
		blk_queue_hardsect_size(queue, le32_to_cpu(blocksize));
1114
1115
	} else
		osm_warn("unable to get blocksize of %s\n", gd->disk_name);
Linus Torvalds's avatar
Linus Torvalds committed
1116

Markus Lidel's avatar
Markus Lidel committed
1117
1118
1119
	if (!i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) ||
	    !i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
		set_capacity(gd, le64_to_cpu(size) >> KERNEL_SECTOR_SHIFT);
1120
1121
	} else
		osm_warn("could not get size of %s\n", gd->disk_name);
1122

Markus Lidel's avatar
Markus Lidel committed
1123
1124
	if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &power, 2))
		i2o_blk_dev->power = power;
Linus Torvalds's avatar
Linus Torvalds committed
1125
1126
1127
1128
1129
1130
1131

	i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);

	add_disk(gd);

	unit++;

1132
1133
1134
	osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
		 i2o_blk_dev->gd->disk_name);

Linus Torvalds's avatar
Linus Torvalds committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
	return 0;

      claim_release:
	i2o_device_claim_release(i2o_dev);

      exit:
	return rc;
};

/* Block OSM driver struct */
static struct i2o_driver i2o_block_driver = {
	.name = OSM_NAME,
	.event = i2o_block_event,
	.reply = i2o_block_reply,
	.classes = i2o_block_class_id,
	.driver = {
		   .probe = i2o_block_probe,
		   .remove = i2o_block_remove,
		   },
};

/**
 *	i2o_block_init - Block OSM initialization function
 *
 *	Allocate the slab and mempool for request structs, registers i2o_block
 *	block device and finally register the Block OSM in the I2O core.
 *
 *	Returns 0 on success or negative error code on failure.
 */
static int __init i2o_block_init(void)
{
	int rc;
	int size;

	printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");

	/* Allocate request mempool and slab */
	size = sizeof(struct i2o_block_request);
	i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
1174
						  SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1175
1176
1177
1178
1179
1180
	if (!i2o_blk_req_pool.slab) {
		osm_err("can't init request slab\n");
		rc = -ENOMEM;
		goto exit;
	}

1181
1182
1183
	i2o_blk_req_pool.pool =
		mempool_create_slab_pool(I2O_BLOCK_REQ_MEMPOOL_SIZE,
					 i2o_blk_req_pool.slab);
Linus Torvalds's avatar
Linus Torvalds committed
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
	if (!i2o_blk_req_pool.pool) {
		osm_err("can't init request mempool\n");
		rc = -ENOMEM;
		goto free_slab;
	}

	/* Register the block device interfaces */
	rc = register_blkdev(I2O_MAJOR, "i2o_block");
	if (rc) {
		osm_err("unable to register block device\n");
		goto free_mempool;
	}
#ifdef MODULE
	osm_info("registered device at major %d\n", I2O_MAJOR);
#endif

	/* Register Block OSM into I2O core */
	rc = i2o_driver_register(&i2o_block_driver);
	if (rc) {
		osm_err("Could not register Block driver\n");
		goto unregister_blkdev;
	}

	return 0;

      unregister_blkdev:
	unregister_blkdev(I2O_MAJOR, "i2o_block");

      free_mempool:
	mempool_destroy(i2o_blk_req_pool.pool);

      free_slab:
	kmem_cache_destroy(i2o_blk_req_pool.slab);

      exit:
	return rc;
};

/**
 *	i2o_block_exit - Block OSM exit function
 *
 *	Unregisters Block OSM from I2O core, unregisters i2o_block block device
 *	and frees the mempool and slab.
 */
static void __exit i2o_block_exit(void)
{
	/* Unregister I2O Block OSM from I2O core */
	i2o_driver_unregister(&i2o_block_driver);

	/* Unregister block device */
	unregister_blkdev(I2O_MAJOR, "i2o_block");

	/* Free request mempool and slab */
	mempool_destroy(i2o_blk_req_pool.pool);
	kmem_cache_destroy(i2o_blk_req_pool.slab);
};

MODULE_AUTHOR("Red Hat");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(OSM_DESCRIPTION);
MODULE_VERSION(OSM_VERSION);

module_init(i2o_block_init);
module_exit(i2o_block_exit);