xref: /openbmc/linux/drivers/block/ps3disk.c (revision c6131fa528c4fc57605c474bf8c83821aff164c0)
1*c6131fa5SGeert Uytterhoeven /*
2*c6131fa5SGeert Uytterhoeven  * PS3 Disk Storage Driver
3*c6131fa5SGeert Uytterhoeven  *
4*c6131fa5SGeert Uytterhoeven  * Copyright (C) 2007 Sony Computer Entertainment Inc.
5*c6131fa5SGeert Uytterhoeven  * Copyright 2007 Sony Corp.
6*c6131fa5SGeert Uytterhoeven  *
7*c6131fa5SGeert Uytterhoeven  * This program is free software; you can redistribute it and/or modify it
8*c6131fa5SGeert Uytterhoeven  * under the terms of the GNU General Public License as published
9*c6131fa5SGeert Uytterhoeven  * by the Free Software Foundation; version 2 of the License.
10*c6131fa5SGeert Uytterhoeven  *
11*c6131fa5SGeert Uytterhoeven  * This program is distributed in the hope that it will be useful, but
12*c6131fa5SGeert Uytterhoeven  * WITHOUT ANY WARRANTY; without even the implied warranty of
13*c6131fa5SGeert Uytterhoeven  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*c6131fa5SGeert Uytterhoeven  * General Public License for more details.
15*c6131fa5SGeert Uytterhoeven  *
16*c6131fa5SGeert Uytterhoeven  * You should have received a copy of the GNU General Public License along
17*c6131fa5SGeert Uytterhoeven  * with this program; if not, write to the Free Software Foundation, Inc.,
18*c6131fa5SGeert Uytterhoeven  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*c6131fa5SGeert Uytterhoeven  */
20*c6131fa5SGeert Uytterhoeven 
21*c6131fa5SGeert Uytterhoeven #include <linux/ata.h>
22*c6131fa5SGeert Uytterhoeven #include <linux/blkdev.h>
23*c6131fa5SGeert Uytterhoeven 
24*c6131fa5SGeert Uytterhoeven #include <asm/lv1call.h>
25*c6131fa5SGeert Uytterhoeven #include <asm/ps3stor.h>
26*c6131fa5SGeert Uytterhoeven #include <asm/firmware.h>
27*c6131fa5SGeert Uytterhoeven 
28*c6131fa5SGeert Uytterhoeven 
29*c6131fa5SGeert Uytterhoeven #define DEVICE_NAME		"ps3disk"
30*c6131fa5SGeert Uytterhoeven 
31*c6131fa5SGeert Uytterhoeven #define BOUNCE_SIZE		(64*1024)
32*c6131fa5SGeert Uytterhoeven 
33*c6131fa5SGeert Uytterhoeven #define PS3DISK_MAX_DISKS	16
34*c6131fa5SGeert Uytterhoeven #define PS3DISK_MINORS		16
35*c6131fa5SGeert Uytterhoeven 
36*c6131fa5SGeert Uytterhoeven 
37*c6131fa5SGeert Uytterhoeven #define PS3DISK_NAME		"ps3d%c"
38*c6131fa5SGeert Uytterhoeven 
39*c6131fa5SGeert Uytterhoeven 
40*c6131fa5SGeert Uytterhoeven struct ps3disk_private {
41*c6131fa5SGeert Uytterhoeven 	spinlock_t lock;		/* Request queue spinlock */
42*c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
43*c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
44*c6131fa5SGeert Uytterhoeven 	unsigned int blocking_factor;
45*c6131fa5SGeert Uytterhoeven 	struct request *req;
46*c6131fa5SGeert Uytterhoeven 	u64 raw_capacity;
47*c6131fa5SGeert Uytterhoeven 	unsigned char model[ATA_ID_PROD_LEN+1];
48*c6131fa5SGeert Uytterhoeven };
49*c6131fa5SGeert Uytterhoeven 
50*c6131fa5SGeert Uytterhoeven 
51*c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_SEND_ATA_COMMAND	(2)
52*c6131fa5SGeert Uytterhoeven #define LV1_STORAGE_ATA_HDDOUT		(0x23)
53*c6131fa5SGeert Uytterhoeven 
54*c6131fa5SGeert Uytterhoeven struct lv1_ata_cmnd_block {
55*c6131fa5SGeert Uytterhoeven 	u16	features;
56*c6131fa5SGeert Uytterhoeven 	u16	sector_count;
57*c6131fa5SGeert Uytterhoeven 	u16	LBA_low;
58*c6131fa5SGeert Uytterhoeven 	u16	LBA_mid;
59*c6131fa5SGeert Uytterhoeven 	u16	LBA_high;
60*c6131fa5SGeert Uytterhoeven 	u8	device;
61*c6131fa5SGeert Uytterhoeven 	u8	command;
62*c6131fa5SGeert Uytterhoeven 	u32	is_ext;
63*c6131fa5SGeert Uytterhoeven 	u32	proto;
64*c6131fa5SGeert Uytterhoeven 	u32	in_out;
65*c6131fa5SGeert Uytterhoeven 	u32	size;
66*c6131fa5SGeert Uytterhoeven 	u64	buffer;
67*c6131fa5SGeert Uytterhoeven 	u32	arglen;
68*c6131fa5SGeert Uytterhoeven };
69*c6131fa5SGeert Uytterhoeven 
70*c6131fa5SGeert Uytterhoeven enum lv1_ata_proto {
71*c6131fa5SGeert Uytterhoeven 	NON_DATA_PROTO     = 0,
72*c6131fa5SGeert Uytterhoeven 	PIO_DATA_IN_PROTO  = 1,
73*c6131fa5SGeert Uytterhoeven 	PIO_DATA_OUT_PROTO = 2,
74*c6131fa5SGeert Uytterhoeven 	DMA_PROTO = 3
75*c6131fa5SGeert Uytterhoeven };
76*c6131fa5SGeert Uytterhoeven 
77*c6131fa5SGeert Uytterhoeven enum lv1_ata_in_out {
78*c6131fa5SGeert Uytterhoeven 	DIR_WRITE = 0,			/* memory -> device */
79*c6131fa5SGeert Uytterhoeven 	DIR_READ = 1			/* device -> memory */
80*c6131fa5SGeert Uytterhoeven };
81*c6131fa5SGeert Uytterhoeven 
82*c6131fa5SGeert Uytterhoeven static int ps3disk_major;
83*c6131fa5SGeert Uytterhoeven 
84*c6131fa5SGeert Uytterhoeven 
85*c6131fa5SGeert Uytterhoeven static struct block_device_operations ps3disk_fops = {
86*c6131fa5SGeert Uytterhoeven 	.owner		= THIS_MODULE,
87*c6131fa5SGeert Uytterhoeven };
88*c6131fa5SGeert Uytterhoeven 
89*c6131fa5SGeert Uytterhoeven 
90*c6131fa5SGeert Uytterhoeven static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
91*c6131fa5SGeert Uytterhoeven 				   struct request *req, int gather)
92*c6131fa5SGeert Uytterhoeven {
93*c6131fa5SGeert Uytterhoeven 	unsigned int offset = 0;
94*c6131fa5SGeert Uytterhoeven 	struct bio *bio;
95*c6131fa5SGeert Uytterhoeven 	sector_t sector;
96*c6131fa5SGeert Uytterhoeven 	struct bio_vec *bvec;
97*c6131fa5SGeert Uytterhoeven 	unsigned int i = 0, j;
98*c6131fa5SGeert Uytterhoeven 	size_t size;
99*c6131fa5SGeert Uytterhoeven 	void *buf;
100*c6131fa5SGeert Uytterhoeven 
101*c6131fa5SGeert Uytterhoeven 	rq_for_each_bio(bio, req) {
102*c6131fa5SGeert Uytterhoeven 		sector = bio->bi_sector;
103*c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
104*c6131fa5SGeert Uytterhoeven 			"%s:%u: bio %u: %u segs %u sectors from %lu\n",
105*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, i, bio_segments(bio),
106*c6131fa5SGeert Uytterhoeven 			bio_sectors(bio), sector);
107*c6131fa5SGeert Uytterhoeven 		bio_for_each_segment(bvec, bio, j) {
108*c6131fa5SGeert Uytterhoeven 			size = bvec->bv_len;
109*c6131fa5SGeert Uytterhoeven 			buf = __bio_kmap_atomic(bio, j, KM_IRQ0);
110*c6131fa5SGeert Uytterhoeven 			if (gather)
111*c6131fa5SGeert Uytterhoeven 				memcpy(dev->bounce_buf+offset, buf, size);
112*c6131fa5SGeert Uytterhoeven 			else
113*c6131fa5SGeert Uytterhoeven 				memcpy(buf, dev->bounce_buf+offset, size);
114*c6131fa5SGeert Uytterhoeven 			offset += size;
115*c6131fa5SGeert Uytterhoeven 			flush_kernel_dcache_page(bio_iovec_idx(bio, j)->bv_page);
116*c6131fa5SGeert Uytterhoeven 			__bio_kunmap_atomic(bio, KM_IRQ0);
117*c6131fa5SGeert Uytterhoeven 		}
118*c6131fa5SGeert Uytterhoeven 		i++;
119*c6131fa5SGeert Uytterhoeven 	}
120*c6131fa5SGeert Uytterhoeven }
121*c6131fa5SGeert Uytterhoeven 
122*c6131fa5SGeert Uytterhoeven static int ps3disk_submit_request_sg(struct ps3_storage_device *dev,
123*c6131fa5SGeert Uytterhoeven 				     struct request *req)
124*c6131fa5SGeert Uytterhoeven {
125*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
126*c6131fa5SGeert Uytterhoeven 	int write = rq_data_dir(req), res;
127*c6131fa5SGeert Uytterhoeven 	const char *op = write ? "write" : "read";
128*c6131fa5SGeert Uytterhoeven 	u64 start_sector, sectors;
129*c6131fa5SGeert Uytterhoeven 	unsigned int region_id = dev->regions[dev->region_idx].id;
130*c6131fa5SGeert Uytterhoeven 
131*c6131fa5SGeert Uytterhoeven #ifdef DEBUG
132*c6131fa5SGeert Uytterhoeven 	unsigned int n = 0;
133*c6131fa5SGeert Uytterhoeven 	struct bio *bio;
134*c6131fa5SGeert Uytterhoeven 
135*c6131fa5SGeert Uytterhoeven 	rq_for_each_bio(bio, req)
136*c6131fa5SGeert Uytterhoeven 		n++;
137*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core,
138*c6131fa5SGeert Uytterhoeven 		"%s:%u: %s req has %u bios for %lu sectors %lu hard sectors\n",
139*c6131fa5SGeert Uytterhoeven 		__func__, __LINE__, op, n, req->nr_sectors,
140*c6131fa5SGeert Uytterhoeven 		req->hard_nr_sectors);
141*c6131fa5SGeert Uytterhoeven #endif
142*c6131fa5SGeert Uytterhoeven 
143*c6131fa5SGeert Uytterhoeven 	start_sector = req->sector * priv->blocking_factor;
144*c6131fa5SGeert Uytterhoeven 	sectors = req->nr_sectors * priv->blocking_factor;
145*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: %s %lu sectors starting at %lu\n",
146*c6131fa5SGeert Uytterhoeven 		__func__, __LINE__, op, sectors, start_sector);
147*c6131fa5SGeert Uytterhoeven 
148*c6131fa5SGeert Uytterhoeven 	if (write) {
149*c6131fa5SGeert Uytterhoeven 		ps3disk_scatter_gather(dev, req, 1);
150*c6131fa5SGeert Uytterhoeven 
151*c6131fa5SGeert Uytterhoeven 		res = lv1_storage_write(dev->sbd.dev_id, region_id,
152*c6131fa5SGeert Uytterhoeven 					start_sector, sectors, 0,
153*c6131fa5SGeert Uytterhoeven 					dev->bounce_lpar, &dev->tag);
154*c6131fa5SGeert Uytterhoeven 	} else {
155*c6131fa5SGeert Uytterhoeven 		res = lv1_storage_read(dev->sbd.dev_id, region_id,
156*c6131fa5SGeert Uytterhoeven 				       start_sector, sectors, 0,
157*c6131fa5SGeert Uytterhoeven 				       dev->bounce_lpar, &dev->tag);
158*c6131fa5SGeert Uytterhoeven 	}
159*c6131fa5SGeert Uytterhoeven 	if (res) {
160*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
161*c6131fa5SGeert Uytterhoeven 			__LINE__, op, res);
162*c6131fa5SGeert Uytterhoeven 		end_request(req, 0);
163*c6131fa5SGeert Uytterhoeven 		return 0;
164*c6131fa5SGeert Uytterhoeven 	}
165*c6131fa5SGeert Uytterhoeven 
166*c6131fa5SGeert Uytterhoeven 	priv->req = req;
167*c6131fa5SGeert Uytterhoeven 	return 1;
168*c6131fa5SGeert Uytterhoeven }
169*c6131fa5SGeert Uytterhoeven 
170*c6131fa5SGeert Uytterhoeven static int ps3disk_submit_flush_request(struct ps3_storage_device *dev,
171*c6131fa5SGeert Uytterhoeven 					struct request *req)
172*c6131fa5SGeert Uytterhoeven {
173*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
174*c6131fa5SGeert Uytterhoeven 	u64 res;
175*c6131fa5SGeert Uytterhoeven 
176*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
177*c6131fa5SGeert Uytterhoeven 
178*c6131fa5SGeert Uytterhoeven 	res = lv1_storage_send_device_command(dev->sbd.dev_id,
179*c6131fa5SGeert Uytterhoeven 					      LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
180*c6131fa5SGeert Uytterhoeven 					      0, &dev->tag);
181*c6131fa5SGeert Uytterhoeven 	if (res) {
182*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%lx\n",
183*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
184*c6131fa5SGeert Uytterhoeven 		end_request(req, 0);
185*c6131fa5SGeert Uytterhoeven 		return 0;
186*c6131fa5SGeert Uytterhoeven 	}
187*c6131fa5SGeert Uytterhoeven 
188*c6131fa5SGeert Uytterhoeven 	priv->req = req;
189*c6131fa5SGeert Uytterhoeven 	return 1;
190*c6131fa5SGeert Uytterhoeven }
191*c6131fa5SGeert Uytterhoeven 
192*c6131fa5SGeert Uytterhoeven static void ps3disk_do_request(struct ps3_storage_device *dev,
193*c6131fa5SGeert Uytterhoeven 			       request_queue_t *q)
194*c6131fa5SGeert Uytterhoeven {
195*c6131fa5SGeert Uytterhoeven 	struct request *req;
196*c6131fa5SGeert Uytterhoeven 
197*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
198*c6131fa5SGeert Uytterhoeven 
199*c6131fa5SGeert Uytterhoeven 	while ((req = elv_next_request(q))) {
200*c6131fa5SGeert Uytterhoeven 		if (blk_fs_request(req)) {
201*c6131fa5SGeert Uytterhoeven 			if (ps3disk_submit_request_sg(dev, req))
202*c6131fa5SGeert Uytterhoeven 				break;
203*c6131fa5SGeert Uytterhoeven 		} else if (req->cmd_type == REQ_TYPE_FLUSH) {
204*c6131fa5SGeert Uytterhoeven 			if (ps3disk_submit_flush_request(dev, req))
205*c6131fa5SGeert Uytterhoeven 				break;
206*c6131fa5SGeert Uytterhoeven 		} else {
207*c6131fa5SGeert Uytterhoeven 			blk_dump_rq_flags(req, DEVICE_NAME " bad request");
208*c6131fa5SGeert Uytterhoeven 			end_request(req, 0);
209*c6131fa5SGeert Uytterhoeven 			continue;
210*c6131fa5SGeert Uytterhoeven 		}
211*c6131fa5SGeert Uytterhoeven 	}
212*c6131fa5SGeert Uytterhoeven }
213*c6131fa5SGeert Uytterhoeven 
214*c6131fa5SGeert Uytterhoeven static void ps3disk_request(request_queue_t *q)
215*c6131fa5SGeert Uytterhoeven {
216*c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = q->queuedata;
217*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
218*c6131fa5SGeert Uytterhoeven 
219*c6131fa5SGeert Uytterhoeven 	if (priv->req) {
220*c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u busy\n", __func__, __LINE__);
221*c6131fa5SGeert Uytterhoeven 		return;
222*c6131fa5SGeert Uytterhoeven 	}
223*c6131fa5SGeert Uytterhoeven 
224*c6131fa5SGeert Uytterhoeven 	ps3disk_do_request(dev, q);
225*c6131fa5SGeert Uytterhoeven }
226*c6131fa5SGeert Uytterhoeven 
227*c6131fa5SGeert Uytterhoeven static irqreturn_t ps3disk_interrupt(int irq, void *data)
228*c6131fa5SGeert Uytterhoeven {
229*c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = data;
230*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
231*c6131fa5SGeert Uytterhoeven 	struct request *req;
232*c6131fa5SGeert Uytterhoeven 	int res, read, uptodate;
233*c6131fa5SGeert Uytterhoeven 	u64 tag, status;
234*c6131fa5SGeert Uytterhoeven 	unsigned long num_sectors;
235*c6131fa5SGeert Uytterhoeven 	const char *op;
236*c6131fa5SGeert Uytterhoeven 
237*c6131fa5SGeert Uytterhoeven 	res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
238*c6131fa5SGeert Uytterhoeven 
239*c6131fa5SGeert Uytterhoeven 	if (tag != dev->tag)
240*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
241*c6131fa5SGeert Uytterhoeven 			"%s:%u: tag mismatch, got %lx, expected %lx\n",
242*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, tag, dev->tag);
243*c6131fa5SGeert Uytterhoeven 
244*c6131fa5SGeert Uytterhoeven 	if (res) {
245*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%lx\n",
246*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res, status);
247*c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
248*c6131fa5SGeert Uytterhoeven 	}
249*c6131fa5SGeert Uytterhoeven 
250*c6131fa5SGeert Uytterhoeven 	priv = dev->sbd.core.driver_data;
251*c6131fa5SGeert Uytterhoeven 	req = priv->req;
252*c6131fa5SGeert Uytterhoeven 	if (!req) {
253*c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core,
254*c6131fa5SGeert Uytterhoeven 			"%s:%u non-block layer request completed\n", __func__,
255*c6131fa5SGeert Uytterhoeven 			__LINE__);
256*c6131fa5SGeert Uytterhoeven 		dev->lv1_status = status;
257*c6131fa5SGeert Uytterhoeven 		complete(&dev->done);
258*c6131fa5SGeert Uytterhoeven 		return IRQ_HANDLED;
259*c6131fa5SGeert Uytterhoeven 	}
260*c6131fa5SGeert Uytterhoeven 
261*c6131fa5SGeert Uytterhoeven 	if (req->cmd_type == REQ_TYPE_FLUSH) {
262*c6131fa5SGeert Uytterhoeven 		read = 0;
263*c6131fa5SGeert Uytterhoeven 		num_sectors = req->hard_cur_sectors;
264*c6131fa5SGeert Uytterhoeven 		op = "flush";
265*c6131fa5SGeert Uytterhoeven 	} else {
266*c6131fa5SGeert Uytterhoeven 		read = !rq_data_dir(req);
267*c6131fa5SGeert Uytterhoeven 		num_sectors = req->nr_sectors;
268*c6131fa5SGeert Uytterhoeven 		op = read ? "read" : "write";
269*c6131fa5SGeert Uytterhoeven 	}
270*c6131fa5SGeert Uytterhoeven 	if (status) {
271*c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%lx\n", __func__,
272*c6131fa5SGeert Uytterhoeven 			__LINE__, op, status);
273*c6131fa5SGeert Uytterhoeven 		uptodate = 0;
274*c6131fa5SGeert Uytterhoeven 	} else {
275*c6131fa5SGeert Uytterhoeven 		dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
276*c6131fa5SGeert Uytterhoeven 			__LINE__, op);
277*c6131fa5SGeert Uytterhoeven 		uptodate = 1;
278*c6131fa5SGeert Uytterhoeven 		if (read)
279*c6131fa5SGeert Uytterhoeven 			ps3disk_scatter_gather(dev, req, 0);
280*c6131fa5SGeert Uytterhoeven 	}
281*c6131fa5SGeert Uytterhoeven 
282*c6131fa5SGeert Uytterhoeven 	spin_lock(&priv->lock);
283*c6131fa5SGeert Uytterhoeven 	if (!end_that_request_first(req, uptodate, num_sectors)) {
284*c6131fa5SGeert Uytterhoeven 		add_disk_randomness(req->rq_disk);
285*c6131fa5SGeert Uytterhoeven 		blkdev_dequeue_request(req);
286*c6131fa5SGeert Uytterhoeven 		end_that_request_last(req, uptodate);
287*c6131fa5SGeert Uytterhoeven 	}
288*c6131fa5SGeert Uytterhoeven 	priv->req = NULL;
289*c6131fa5SGeert Uytterhoeven 	ps3disk_do_request(dev, priv->queue);
290*c6131fa5SGeert Uytterhoeven 	spin_unlock(&priv->lock);
291*c6131fa5SGeert Uytterhoeven 
292*c6131fa5SGeert Uytterhoeven 	return IRQ_HANDLED;
293*c6131fa5SGeert Uytterhoeven }
294*c6131fa5SGeert Uytterhoeven 
295*c6131fa5SGeert Uytterhoeven static int ps3disk_sync_cache(struct ps3_storage_device *dev)
296*c6131fa5SGeert Uytterhoeven {
297*c6131fa5SGeert Uytterhoeven 	u64 res;
298*c6131fa5SGeert Uytterhoeven 
299*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
300*c6131fa5SGeert Uytterhoeven 
301*c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
302*c6131fa5SGeert Uytterhoeven 	if (res) {
303*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%lx\n",
304*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
305*c6131fa5SGeert Uytterhoeven 		return -EIO;
306*c6131fa5SGeert Uytterhoeven 	}
307*c6131fa5SGeert Uytterhoeven 	return 0;
308*c6131fa5SGeert Uytterhoeven }
309*c6131fa5SGeert Uytterhoeven 
310*c6131fa5SGeert Uytterhoeven 
311*c6131fa5SGeert Uytterhoeven /* ATA helpers copied from drivers/ata/libata-core.c */
312*c6131fa5SGeert Uytterhoeven 
313*c6131fa5SGeert Uytterhoeven static void swap_buf_le16(u16 *buf, unsigned int buf_words)
314*c6131fa5SGeert Uytterhoeven {
315*c6131fa5SGeert Uytterhoeven #ifdef __BIG_ENDIAN
316*c6131fa5SGeert Uytterhoeven 	unsigned int i;
317*c6131fa5SGeert Uytterhoeven 
318*c6131fa5SGeert Uytterhoeven 	for (i = 0; i < buf_words; i++)
319*c6131fa5SGeert Uytterhoeven 		buf[i] = le16_to_cpu(buf[i]);
320*c6131fa5SGeert Uytterhoeven #endif /* __BIG_ENDIAN */
321*c6131fa5SGeert Uytterhoeven }
322*c6131fa5SGeert Uytterhoeven 
323*c6131fa5SGeert Uytterhoeven static u64 ata_id_n_sectors(const u16 *id)
324*c6131fa5SGeert Uytterhoeven {
325*c6131fa5SGeert Uytterhoeven 	if (ata_id_has_lba(id)) {
326*c6131fa5SGeert Uytterhoeven 		if (ata_id_has_lba48(id))
327*c6131fa5SGeert Uytterhoeven 			return ata_id_u64(id, 100);
328*c6131fa5SGeert Uytterhoeven 		else
329*c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 60);
330*c6131fa5SGeert Uytterhoeven 	} else {
331*c6131fa5SGeert Uytterhoeven 		if (ata_id_current_chs_valid(id))
332*c6131fa5SGeert Uytterhoeven 			return ata_id_u32(id, 57);
333*c6131fa5SGeert Uytterhoeven 		else
334*c6131fa5SGeert Uytterhoeven 			return id[1] * id[3] * id[6];
335*c6131fa5SGeert Uytterhoeven 	}
336*c6131fa5SGeert Uytterhoeven }
337*c6131fa5SGeert Uytterhoeven 
338*c6131fa5SGeert Uytterhoeven static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
339*c6131fa5SGeert Uytterhoeven 			  unsigned int len)
340*c6131fa5SGeert Uytterhoeven {
341*c6131fa5SGeert Uytterhoeven 	unsigned int c;
342*c6131fa5SGeert Uytterhoeven 
343*c6131fa5SGeert Uytterhoeven 	while (len > 0) {
344*c6131fa5SGeert Uytterhoeven 		c = id[ofs] >> 8;
345*c6131fa5SGeert Uytterhoeven 		*s = c;
346*c6131fa5SGeert Uytterhoeven 		s++;
347*c6131fa5SGeert Uytterhoeven 
348*c6131fa5SGeert Uytterhoeven 		c = id[ofs] & 0xff;
349*c6131fa5SGeert Uytterhoeven 		*s = c;
350*c6131fa5SGeert Uytterhoeven 		s++;
351*c6131fa5SGeert Uytterhoeven 
352*c6131fa5SGeert Uytterhoeven 		ofs++;
353*c6131fa5SGeert Uytterhoeven 		len -= 2;
354*c6131fa5SGeert Uytterhoeven 	}
355*c6131fa5SGeert Uytterhoeven }
356*c6131fa5SGeert Uytterhoeven 
357*c6131fa5SGeert Uytterhoeven static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
358*c6131fa5SGeert Uytterhoeven 			    unsigned int len)
359*c6131fa5SGeert Uytterhoeven {
360*c6131fa5SGeert Uytterhoeven 	unsigned char *p;
361*c6131fa5SGeert Uytterhoeven 
362*c6131fa5SGeert Uytterhoeven 	WARN_ON(!(len & 1));
363*c6131fa5SGeert Uytterhoeven 
364*c6131fa5SGeert Uytterhoeven 	ata_id_string(id, s, ofs, len - 1);
365*c6131fa5SGeert Uytterhoeven 
366*c6131fa5SGeert Uytterhoeven 	p = s + strnlen(s, len - 1);
367*c6131fa5SGeert Uytterhoeven 	while (p > s && p[-1] == ' ')
368*c6131fa5SGeert Uytterhoeven 		p--;
369*c6131fa5SGeert Uytterhoeven 	*p = '\0';
370*c6131fa5SGeert Uytterhoeven }
371*c6131fa5SGeert Uytterhoeven 
372*c6131fa5SGeert Uytterhoeven static int ps3disk_identify(struct ps3_storage_device *dev)
373*c6131fa5SGeert Uytterhoeven {
374*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
375*c6131fa5SGeert Uytterhoeven 	struct lv1_ata_cmnd_block ata_cmnd;
376*c6131fa5SGeert Uytterhoeven 	u16 *id = dev->bounce_buf;
377*c6131fa5SGeert Uytterhoeven 	u64 res;
378*c6131fa5SGeert Uytterhoeven 
379*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
380*c6131fa5SGeert Uytterhoeven 
381*c6131fa5SGeert Uytterhoeven 	memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
382*c6131fa5SGeert Uytterhoeven 	ata_cmnd.command = ATA_CMD_ID_ATA;
383*c6131fa5SGeert Uytterhoeven 	ata_cmnd.sector_count = 1;
384*c6131fa5SGeert Uytterhoeven 	ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
385*c6131fa5SGeert Uytterhoeven 	ata_cmnd.buffer = dev->bounce_lpar;
386*c6131fa5SGeert Uytterhoeven 	ata_cmnd.proto = PIO_DATA_IN_PROTO;
387*c6131fa5SGeert Uytterhoeven 	ata_cmnd.in_out = DIR_READ;
388*c6131fa5SGeert Uytterhoeven 
389*c6131fa5SGeert Uytterhoeven 	res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
390*c6131fa5SGeert Uytterhoeven 				   ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
391*c6131fa5SGeert Uytterhoeven 				   sizeof(ata_cmnd), ata_cmnd.buffer,
392*c6131fa5SGeert Uytterhoeven 				   ata_cmnd.arglen);
393*c6131fa5SGeert Uytterhoeven 	if (res) {
394*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%lx\n",
395*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
396*c6131fa5SGeert Uytterhoeven 		return -EIO;
397*c6131fa5SGeert Uytterhoeven 	}
398*c6131fa5SGeert Uytterhoeven 
399*c6131fa5SGeert Uytterhoeven 	swap_buf_le16(id, ATA_ID_WORDS);
400*c6131fa5SGeert Uytterhoeven 
401*c6131fa5SGeert Uytterhoeven 	/* All we're interested in are raw capacity and model name */
402*c6131fa5SGeert Uytterhoeven 	priv->raw_capacity = ata_id_n_sectors(id);
403*c6131fa5SGeert Uytterhoeven 	ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
404*c6131fa5SGeert Uytterhoeven 	return 0;
405*c6131fa5SGeert Uytterhoeven }
406*c6131fa5SGeert Uytterhoeven 
407*c6131fa5SGeert Uytterhoeven static void ps3disk_prepare_flush(request_queue_t *q, struct request *req)
408*c6131fa5SGeert Uytterhoeven {
409*c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = q->queuedata;
410*c6131fa5SGeert Uytterhoeven 
411*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
412*c6131fa5SGeert Uytterhoeven 
413*c6131fa5SGeert Uytterhoeven 	memset(req->cmd, 0, sizeof(req->cmd));
414*c6131fa5SGeert Uytterhoeven 	req->cmd_type = REQ_TYPE_FLUSH;
415*c6131fa5SGeert Uytterhoeven }
416*c6131fa5SGeert Uytterhoeven 
417*c6131fa5SGeert Uytterhoeven static int ps3disk_issue_flush(request_queue_t *q, struct gendisk *gendisk,
418*c6131fa5SGeert Uytterhoeven 			       sector_t *sector)
419*c6131fa5SGeert Uytterhoeven {
420*c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = q->queuedata;
421*c6131fa5SGeert Uytterhoeven 	struct request *req;
422*c6131fa5SGeert Uytterhoeven 	int res;
423*c6131fa5SGeert Uytterhoeven 
424*c6131fa5SGeert Uytterhoeven 	dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
425*c6131fa5SGeert Uytterhoeven 
426*c6131fa5SGeert Uytterhoeven 	req = blk_get_request(q, WRITE, __GFP_WAIT);
427*c6131fa5SGeert Uytterhoeven 	ps3disk_prepare_flush(q, req);
428*c6131fa5SGeert Uytterhoeven 	res = blk_execute_rq(q, gendisk, req, 0);
429*c6131fa5SGeert Uytterhoeven 	if (res)
430*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: flush request failed %d\n",
431*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__, res);
432*c6131fa5SGeert Uytterhoeven 	blk_put_request(req);
433*c6131fa5SGeert Uytterhoeven 	return res;
434*c6131fa5SGeert Uytterhoeven }
435*c6131fa5SGeert Uytterhoeven 
436*c6131fa5SGeert Uytterhoeven 
437*c6131fa5SGeert Uytterhoeven static unsigned long ps3disk_mask;
438*c6131fa5SGeert Uytterhoeven 
439*c6131fa5SGeert Uytterhoeven static DEFINE_MUTEX(ps3disk_mask_mutex);
440*c6131fa5SGeert Uytterhoeven 
441*c6131fa5SGeert Uytterhoeven static int __devinit ps3disk_probe(struct ps3_system_bus_device *_dev)
442*c6131fa5SGeert Uytterhoeven {
443*c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
444*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv;
445*c6131fa5SGeert Uytterhoeven 	int error;
446*c6131fa5SGeert Uytterhoeven 	unsigned int devidx;
447*c6131fa5SGeert Uytterhoeven 	struct request_queue *queue;
448*c6131fa5SGeert Uytterhoeven 	struct gendisk *gendisk;
449*c6131fa5SGeert Uytterhoeven 
450*c6131fa5SGeert Uytterhoeven 	if (dev->blk_size < 512) {
451*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core,
452*c6131fa5SGeert Uytterhoeven 			"%s:%u: cannot handle block size %lu\n", __func__,
453*c6131fa5SGeert Uytterhoeven 			__LINE__, dev->blk_size);
454*c6131fa5SGeert Uytterhoeven 		return -EINVAL;
455*c6131fa5SGeert Uytterhoeven 	}
456*c6131fa5SGeert Uytterhoeven 
457*c6131fa5SGeert Uytterhoeven 	BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
458*c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
459*c6131fa5SGeert Uytterhoeven 	devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
460*c6131fa5SGeert Uytterhoeven 	if (devidx >= PS3DISK_MAX_DISKS) {
461*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
462*c6131fa5SGeert Uytterhoeven 			__LINE__);
463*c6131fa5SGeert Uytterhoeven 		mutex_unlock(&ps3disk_mask_mutex);
464*c6131fa5SGeert Uytterhoeven 		return -ENOSPC;
465*c6131fa5SGeert Uytterhoeven 	}
466*c6131fa5SGeert Uytterhoeven 	__set_bit(devidx, &ps3disk_mask);
467*c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
468*c6131fa5SGeert Uytterhoeven 
469*c6131fa5SGeert Uytterhoeven 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
470*c6131fa5SGeert Uytterhoeven 	if (!priv) {
471*c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
472*c6131fa5SGeert Uytterhoeven 		goto fail;
473*c6131fa5SGeert Uytterhoeven 	}
474*c6131fa5SGeert Uytterhoeven 
475*c6131fa5SGeert Uytterhoeven 	dev->sbd.core.driver_data = priv;
476*c6131fa5SGeert Uytterhoeven 	spin_lock_init(&priv->lock);
477*c6131fa5SGeert Uytterhoeven 
478*c6131fa5SGeert Uytterhoeven 	dev->bounce_size = BOUNCE_SIZE;
479*c6131fa5SGeert Uytterhoeven 	dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
480*c6131fa5SGeert Uytterhoeven 	if (!dev->bounce_buf) {
481*c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
482*c6131fa5SGeert Uytterhoeven 		goto fail_free_priv;
483*c6131fa5SGeert Uytterhoeven 	}
484*c6131fa5SGeert Uytterhoeven 
485*c6131fa5SGeert Uytterhoeven 	error = ps3stor_setup(dev, ps3disk_interrupt);
486*c6131fa5SGeert Uytterhoeven 	if (error)
487*c6131fa5SGeert Uytterhoeven 		goto fail_free_bounce;
488*c6131fa5SGeert Uytterhoeven 
489*c6131fa5SGeert Uytterhoeven 	ps3disk_identify(dev);
490*c6131fa5SGeert Uytterhoeven 
491*c6131fa5SGeert Uytterhoeven 	queue = blk_init_queue(ps3disk_request, &priv->lock);
492*c6131fa5SGeert Uytterhoeven 	if (!queue) {
493*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
494*c6131fa5SGeert Uytterhoeven 			__func__, __LINE__);
495*c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
496*c6131fa5SGeert Uytterhoeven 		goto fail_teardown;
497*c6131fa5SGeert Uytterhoeven 	}
498*c6131fa5SGeert Uytterhoeven 
499*c6131fa5SGeert Uytterhoeven 	priv->queue = queue;
500*c6131fa5SGeert Uytterhoeven 	queue->queuedata = dev;
501*c6131fa5SGeert Uytterhoeven 
502*c6131fa5SGeert Uytterhoeven 	blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
503*c6131fa5SGeert Uytterhoeven 
504*c6131fa5SGeert Uytterhoeven 	blk_queue_max_sectors(queue, dev->bounce_size >> 9);
505*c6131fa5SGeert Uytterhoeven 	blk_queue_segment_boundary(queue, -1UL);
506*c6131fa5SGeert Uytterhoeven 	blk_queue_dma_alignment(queue, dev->blk_size-1);
507*c6131fa5SGeert Uytterhoeven 	blk_queue_hardsect_size(queue, dev->blk_size);
508*c6131fa5SGeert Uytterhoeven 
509*c6131fa5SGeert Uytterhoeven 	blk_queue_issue_flush_fn(queue, ps3disk_issue_flush);
510*c6131fa5SGeert Uytterhoeven 	blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH,
511*c6131fa5SGeert Uytterhoeven 			  ps3disk_prepare_flush);
512*c6131fa5SGeert Uytterhoeven 
513*c6131fa5SGeert Uytterhoeven 	blk_queue_max_phys_segments(queue, -1);
514*c6131fa5SGeert Uytterhoeven 	blk_queue_max_hw_segments(queue, -1);
515*c6131fa5SGeert Uytterhoeven 	blk_queue_max_segment_size(queue, dev->bounce_size);
516*c6131fa5SGeert Uytterhoeven 
517*c6131fa5SGeert Uytterhoeven 	gendisk = alloc_disk(PS3DISK_MINORS);
518*c6131fa5SGeert Uytterhoeven 	if (!gendisk) {
519*c6131fa5SGeert Uytterhoeven 		dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
520*c6131fa5SGeert Uytterhoeven 			__LINE__);
521*c6131fa5SGeert Uytterhoeven 		error = -ENOMEM;
522*c6131fa5SGeert Uytterhoeven 		goto fail_cleanup_queue;
523*c6131fa5SGeert Uytterhoeven 	}
524*c6131fa5SGeert Uytterhoeven 
525*c6131fa5SGeert Uytterhoeven 	priv->gendisk = gendisk;
526*c6131fa5SGeert Uytterhoeven 	gendisk->major = ps3disk_major;
527*c6131fa5SGeert Uytterhoeven 	gendisk->first_minor = devidx * PS3DISK_MINORS;
528*c6131fa5SGeert Uytterhoeven 	gendisk->fops = &ps3disk_fops;
529*c6131fa5SGeert Uytterhoeven 	gendisk->queue = queue;
530*c6131fa5SGeert Uytterhoeven 	gendisk->private_data = dev;
531*c6131fa5SGeert Uytterhoeven 	gendisk->driverfs_dev = &dev->sbd.core;
532*c6131fa5SGeert Uytterhoeven 	snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
533*c6131fa5SGeert Uytterhoeven 		 devidx+'a');
534*c6131fa5SGeert Uytterhoeven 	priv->blocking_factor = dev->blk_size >> 9;
535*c6131fa5SGeert Uytterhoeven 	set_capacity(gendisk,
536*c6131fa5SGeert Uytterhoeven 		     dev->regions[dev->region_idx].size*priv->blocking_factor);
537*c6131fa5SGeert Uytterhoeven 
538*c6131fa5SGeert Uytterhoeven 	dev_info(&dev->sbd.core,
539*c6131fa5SGeert Uytterhoeven 		 "%s is a %s (%lu MiB total, %lu MiB for OtherOS)\n",
540*c6131fa5SGeert Uytterhoeven 		 gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
541*c6131fa5SGeert Uytterhoeven 		 get_capacity(gendisk) >> 11);
542*c6131fa5SGeert Uytterhoeven 
543*c6131fa5SGeert Uytterhoeven 	add_disk(gendisk);
544*c6131fa5SGeert Uytterhoeven 	return 0;
545*c6131fa5SGeert Uytterhoeven 
546*c6131fa5SGeert Uytterhoeven fail_cleanup_queue:
547*c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(queue);
548*c6131fa5SGeert Uytterhoeven fail_teardown:
549*c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
550*c6131fa5SGeert Uytterhoeven fail_free_bounce:
551*c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
552*c6131fa5SGeert Uytterhoeven fail_free_priv:
553*c6131fa5SGeert Uytterhoeven 	kfree(priv);
554*c6131fa5SGeert Uytterhoeven 	dev->sbd.core.driver_data = NULL;
555*c6131fa5SGeert Uytterhoeven fail:
556*c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
557*c6131fa5SGeert Uytterhoeven 	__clear_bit(devidx, &ps3disk_mask);
558*c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
559*c6131fa5SGeert Uytterhoeven 	return error;
560*c6131fa5SGeert Uytterhoeven }
561*c6131fa5SGeert Uytterhoeven 
562*c6131fa5SGeert Uytterhoeven static int ps3disk_remove(struct ps3_system_bus_device *_dev)
563*c6131fa5SGeert Uytterhoeven {
564*c6131fa5SGeert Uytterhoeven 	struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
565*c6131fa5SGeert Uytterhoeven 	struct ps3disk_private *priv = dev->sbd.core.driver_data;
566*c6131fa5SGeert Uytterhoeven 
567*c6131fa5SGeert Uytterhoeven 	mutex_lock(&ps3disk_mask_mutex);
568*c6131fa5SGeert Uytterhoeven 	__clear_bit(priv->gendisk->first_minor / PS3DISK_MINORS,
569*c6131fa5SGeert Uytterhoeven 		    &ps3disk_mask);
570*c6131fa5SGeert Uytterhoeven 	mutex_unlock(&ps3disk_mask_mutex);
571*c6131fa5SGeert Uytterhoeven 	del_gendisk(priv->gendisk);
572*c6131fa5SGeert Uytterhoeven 	blk_cleanup_queue(priv->queue);
573*c6131fa5SGeert Uytterhoeven 	put_disk(priv->gendisk);
574*c6131fa5SGeert Uytterhoeven 	dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
575*c6131fa5SGeert Uytterhoeven 	ps3disk_sync_cache(dev);
576*c6131fa5SGeert Uytterhoeven 	ps3stor_teardown(dev);
577*c6131fa5SGeert Uytterhoeven 	kfree(dev->bounce_buf);
578*c6131fa5SGeert Uytterhoeven 	kfree(priv);
579*c6131fa5SGeert Uytterhoeven 	dev->sbd.core.driver_data = NULL;
580*c6131fa5SGeert Uytterhoeven 	return 0;
581*c6131fa5SGeert Uytterhoeven }
582*c6131fa5SGeert Uytterhoeven 
583*c6131fa5SGeert Uytterhoeven static struct ps3_system_bus_driver ps3disk = {
584*c6131fa5SGeert Uytterhoeven 	.match_id	= PS3_MATCH_ID_STOR_DISK,
585*c6131fa5SGeert Uytterhoeven 	.core.name	= DEVICE_NAME,
586*c6131fa5SGeert Uytterhoeven 	.core.owner	= THIS_MODULE,
587*c6131fa5SGeert Uytterhoeven 	.probe		= ps3disk_probe,
588*c6131fa5SGeert Uytterhoeven 	.remove		= ps3disk_remove,
589*c6131fa5SGeert Uytterhoeven 	.shutdown	= ps3disk_remove,
590*c6131fa5SGeert Uytterhoeven };
591*c6131fa5SGeert Uytterhoeven 
592*c6131fa5SGeert Uytterhoeven 
593*c6131fa5SGeert Uytterhoeven static int __init ps3disk_init(void)
594*c6131fa5SGeert Uytterhoeven {
595*c6131fa5SGeert Uytterhoeven 	int error;
596*c6131fa5SGeert Uytterhoeven 
597*c6131fa5SGeert Uytterhoeven 	if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
598*c6131fa5SGeert Uytterhoeven 		return -ENODEV;
599*c6131fa5SGeert Uytterhoeven 
600*c6131fa5SGeert Uytterhoeven 	error = register_blkdev(0, DEVICE_NAME);
601*c6131fa5SGeert Uytterhoeven 	if (error <= 0) {
602*c6131fa5SGeert Uytterhoeven 		printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
603*c6131fa5SGeert Uytterhoeven 		       __LINE__, error);
604*c6131fa5SGeert Uytterhoeven 		return error;
605*c6131fa5SGeert Uytterhoeven 	}
606*c6131fa5SGeert Uytterhoeven 	ps3disk_major = error;
607*c6131fa5SGeert Uytterhoeven 
608*c6131fa5SGeert Uytterhoeven 	pr_info("%s:%u: registered block device major %d\n", __func__,
609*c6131fa5SGeert Uytterhoeven 		__LINE__, ps3disk_major);
610*c6131fa5SGeert Uytterhoeven 
611*c6131fa5SGeert Uytterhoeven 	error = ps3_system_bus_driver_register(&ps3disk);
612*c6131fa5SGeert Uytterhoeven 	if (error)
613*c6131fa5SGeert Uytterhoeven 		unregister_blkdev(ps3disk_major, DEVICE_NAME);
614*c6131fa5SGeert Uytterhoeven 
615*c6131fa5SGeert Uytterhoeven 	return error;
616*c6131fa5SGeert Uytterhoeven }
617*c6131fa5SGeert Uytterhoeven 
618*c6131fa5SGeert Uytterhoeven static void __exit ps3disk_exit(void)
619*c6131fa5SGeert Uytterhoeven {
620*c6131fa5SGeert Uytterhoeven 	ps3_system_bus_driver_unregister(&ps3disk);
621*c6131fa5SGeert Uytterhoeven 	unregister_blkdev(ps3disk_major, DEVICE_NAME);
622*c6131fa5SGeert Uytterhoeven }
623*c6131fa5SGeert Uytterhoeven 
624*c6131fa5SGeert Uytterhoeven module_init(ps3disk_init);
625*c6131fa5SGeert Uytterhoeven module_exit(ps3disk_exit);
626*c6131fa5SGeert Uytterhoeven 
627*c6131fa5SGeert Uytterhoeven MODULE_LICENSE("GPL");
628*c6131fa5SGeert Uytterhoeven MODULE_DESCRIPTION("PS3 Disk Storage Driver");
629*c6131fa5SGeert Uytterhoeven MODULE_AUTHOR("Sony Corporation");
630*c6131fa5SGeert Uytterhoeven MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);
631