blob: d1e7845e11c73364f9f71c7b3bc51eae1dbc5d8f [file] [log] [blame]
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -07001/*
2 * PS3 BD/DVD/CD-ROM Storage Driver
3 *
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include <linux/cdrom.h>
22#include <linux/highmem.h>
23
24#include <scsi/scsi.h>
25#include <scsi/scsi_cmnd.h>
26#include <scsi/scsi_dbg.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_host.h>
29
30#include <asm/lv1call.h>
31#include <asm/ps3stor.h>
32
33
34#define DEVICE_NAME "ps3rom"
35
36#define BOUNCE_SIZE (64*1024)
37
Aegis Lin51883b52008-02-01 17:35:13 +010038#define PS3ROM_MAX_SECTORS (BOUNCE_SIZE >> 9)
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -070039
40
41struct ps3rom_private {
42 struct ps3_storage_device *dev;
43 struct scsi_cmnd *curr_cmd;
44};
45
46
47#define LV1_STORAGE_SEND_ATAPI_COMMAND (1)
48
49struct lv1_atapi_cmnd_block {
50 u8 pkt[32]; /* packet command block */
51 u32 pktlen; /* should be 12 for ATAPI 8020 */
52 u32 blocks;
53 u32 block_size;
54 u32 proto; /* transfer mode */
55 u32 in_out; /* transfer direction */
56 u64 buffer; /* parameter except command block */
57 u32 arglen; /* length above */
58};
59
60enum lv1_atapi_proto {
61 NON_DATA_PROTO = 0,
62 PIO_DATA_IN_PROTO = 1,
63 PIO_DATA_OUT_PROTO = 2,
64 DMA_PROTO = 3
65};
66
67enum lv1_atapi_in_out {
68 DIR_WRITE = 0, /* memory -> device */
69 DIR_READ = 1 /* device -> memory */
70};
71
72
73static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
74{
75 struct ps3rom_private *priv = shost_priv(scsi_dev->host);
76 struct ps3_storage_device *dev = priv->dev;
77
78 dev_dbg(&dev->sbd.core, "%s:%u: id %u, lun %u, channel %u\n", __func__,
79 __LINE__, scsi_dev->id, scsi_dev->lun, scsi_dev->channel);
80
81 /*
82 * ATAPI SFF8020 devices use MODE_SENSE_10,
83 * so we can prohibit MODE_SENSE_6
84 */
85 scsi_dev->use_10_for_ms = 1;
86
87 /* we don't support {READ,WRITE}_6 */
88 scsi_dev->use_10_for_rw = 1;
89
90 return 0;
91}
92
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -070093static int ps3rom_atapi_request(struct ps3_storage_device *dev,
94 struct scsi_cmnd *cmd)
95{
96 struct lv1_atapi_cmnd_block atapi_cmnd;
97 unsigned char opcode = cmd->cmnd[0];
98 int res;
99 u64 lpar;
100
101 dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
102 __LINE__, opcode);
103
104 memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
105 memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
106 atapi_cmnd.pktlen = 12;
107 atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
FUJITA Tomonori35814742007-07-23 09:42:32 +0900108 atapi_cmnd.blocks = atapi_cmnd.arglen = scsi_bufflen(cmd);
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700109 atapi_cmnd.buffer = dev->bounce_lpar;
110
111 switch (cmd->sc_data_direction) {
112 case DMA_FROM_DEVICE:
FUJITA Tomonori35814742007-07-23 09:42:32 +0900113 if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700114 atapi_cmnd.proto = DMA_PROTO;
115 else
116 atapi_cmnd.proto = PIO_DATA_IN_PROTO;
117 atapi_cmnd.in_out = DIR_READ;
118 break;
119
120 case DMA_TO_DEVICE:
FUJITA Tomonori35814742007-07-23 09:42:32 +0900121 if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700122 atapi_cmnd.proto = DMA_PROTO;
123 else
124 atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
125 atapi_cmnd.in_out = DIR_WRITE;
FUJITA Tomonori944cf8b2008-03-09 13:44:31 +0900126 scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700127 break;
128
129 default:
130 atapi_cmnd.proto = NON_DATA_PROTO;
131 break;
132 }
133
134 lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
135 res = lv1_storage_send_device_command(dev->sbd.dev_id,
136 LV1_STORAGE_SEND_ATAPI_COMMAND,
137 lpar, sizeof(atapi_cmnd),
138 atapi_cmnd.buffer,
139 atapi_cmnd.arglen, &dev->tag);
140 if (res == LV1_DENIED_BY_POLICY) {
141 dev_dbg(&dev->sbd.core,
142 "%s:%u: ATAPI command 0x%02x denied by policy\n",
143 __func__, __LINE__, opcode);
144 return DID_ERROR << 16;
145 }
146
147 if (res) {
148 dev_err(&dev->sbd.core,
149 "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
150 __LINE__, opcode, res);
151 return DID_ERROR << 16;
152 }
153
154 return 0;
155}
156
157static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
158{
159 return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
160 cmd->cmnd[5];
161}
162
163static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
164{
165 return cmd->cmnd[7] << 8 | cmd->cmnd[8];
166}
167
168static int ps3rom_read_request(struct ps3_storage_device *dev,
169 struct scsi_cmnd *cmd, u32 start_sector,
170 u32 sectors)
171{
172 int res;
173
174 dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
175 __func__, __LINE__, sectors, start_sector);
176
177 res = lv1_storage_read(dev->sbd.dev_id,
178 dev->regions[dev->region_idx].id, start_sector,
179 sectors, 0, dev->bounce_lpar, &dev->tag);
180 if (res) {
181 dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
182 __LINE__, res);
183 return DID_ERROR << 16;
184 }
185
186 return 0;
187}
188
189static int ps3rom_write_request(struct ps3_storage_device *dev,
190 struct scsi_cmnd *cmd, u32 start_sector,
191 u32 sectors)
192{
193 int res;
194
195 dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
196 __func__, __LINE__, sectors, start_sector);
197
FUJITA Tomonori944cf8b2008-03-09 13:44:31 +0900198 scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700199
200 res = lv1_storage_write(dev->sbd.dev_id,
201 dev->regions[dev->region_idx].id, start_sector,
202 sectors, 0, dev->bounce_lpar, &dev->tag);
203 if (res) {
204 dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
205 __LINE__, res);
206 return DID_ERROR << 16;
207 }
208
209 return 0;
210}
211
212static int ps3rom_queuecommand(struct scsi_cmnd *cmd,
213 void (*done)(struct scsi_cmnd *))
214{
215 struct ps3rom_private *priv = shost_priv(cmd->device->host);
216 struct ps3_storage_device *dev = priv->dev;
217 unsigned char opcode;
218 int res;
219
220#ifdef DEBUG
221 scsi_print_command(cmd);
222#endif
223
224 priv->curr_cmd = cmd;
225 cmd->scsi_done = done;
226
227 opcode = cmd->cmnd[0];
228 /*
229 * While we can submit READ/WRITE SCSI commands as ATAPI commands,
230 * it's recommended for various reasons (performance, error handling,
231 * ...) to use lv1_storage_{read,write}() instead
232 */
233 switch (opcode) {
234 case READ_10:
235 res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
236 srb10_len(cmd));
237 break;
238
239 case WRITE_10:
240 res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
241 srb10_len(cmd));
242 break;
243
244 default:
245 res = ps3rom_atapi_request(dev, cmd);
246 break;
247 }
248
249 if (res) {
250 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
251 cmd->result = res;
252 cmd->sense_buffer[0] = 0x70;
253 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
254 priv->curr_cmd = NULL;
255 cmd->scsi_done(cmd);
256 }
257
258 return 0;
259}
260
261static int decode_lv1_status(u64 status, unsigned char *sense_key,
262 unsigned char *asc, unsigned char *ascq)
263{
264 if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
265 return -1;
266
267 *sense_key = (status >> 16) & 0xff;
268 *asc = (status >> 8) & 0xff;
269 *ascq = status & 0xff;
270 return 0;
271}
272
273static irqreturn_t ps3rom_interrupt(int irq, void *data)
274{
275 struct ps3_storage_device *dev = data;
276 struct Scsi_Host *host;
277 struct ps3rom_private *priv;
278 struct scsi_cmnd *cmd;
279 int res;
280 u64 tag, status;
281 unsigned char sense_key, asc, ascq;
282
283 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
284 /*
285 * status = -1 may mean that ATAPI transport completed OK, but
286 * ATAPI command itself resulted CHECK CONDITION
287 * so, upper layer should issue REQUEST_SENSE to check the sense data
288 */
289
290 if (tag != dev->tag)
291 dev_err(&dev->sbd.core,
292 "%s:%u: tag mismatch, got %lx, expected %lx\n",
293 __func__, __LINE__, tag, dev->tag);
294
295 if (res) {
296 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%lx\n",
297 __func__, __LINE__, res, status);
298 return IRQ_HANDLED;
299 }
300
301 host = dev->sbd.core.driver_data;
302 priv = shost_priv(host);
303 cmd = priv->curr_cmd;
304
305 if (!status) {
306 /* OK, completed */
307 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
FUJITA Tomonori944cf8b2008-03-09 13:44:31 +0900308 int len;
309
310 len = scsi_sg_copy_from_buffer(cmd,
311 dev->bounce_buf,
312 dev->bounce_size);
313
314 scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700315 }
316 cmd->result = DID_OK << 16;
317 goto done;
318 }
319
320 if (cmd->cmnd[0] == REQUEST_SENSE) {
321 /* SCSI spec says request sense should never get error */
322 dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
323 __func__, __LINE__);
324 cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
325 goto done;
326 }
327
328 if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
329 cmd->result = DID_ERROR << 16;
330 goto done;
331 }
332
333 cmd->sense_buffer[0] = 0x70;
334 cmd->sense_buffer[2] = sense_key;
335 cmd->sense_buffer[7] = 16 - 6;
336 cmd->sense_buffer[12] = asc;
337 cmd->sense_buffer[13] = ascq;
338 cmd->result = SAM_STAT_CHECK_CONDITION;
339
340done:
341 priv->curr_cmd = NULL;
342 cmd->scsi_done(cmd);
343 return IRQ_HANDLED;
344}
345
346static struct scsi_host_template ps3rom_host_template = {
347 .name = DEVICE_NAME,
348 .slave_configure = ps3rom_slave_configure,
349 .queuecommand = ps3rom_queuecommand,
350 .can_queue = 1,
351 .this_id = 7,
352 .sg_tablesize = SG_ALL,
353 .cmd_per_lun = 1,
354 .emulated = 1, /* only sg driver uses this */
355 .max_sectors = PS3ROM_MAX_SECTORS,
FUJITA Tomonori944cf8b2008-03-09 13:44:31 +0900356 .use_clustering = ENABLE_CLUSTERING,
Geert Uytterhoeven9aea8cb2007-07-21 04:37:47 -0700357 .module = THIS_MODULE,
358};
359
360
361static int __devinit ps3rom_probe(struct ps3_system_bus_device *_dev)
362{
363 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
364 int error;
365 struct Scsi_Host *host;
366 struct ps3rom_private *priv;
367
368 if (dev->blk_size != CD_FRAMESIZE) {
369 dev_err(&dev->sbd.core,
370 "%s:%u: cannot handle block size %lu\n", __func__,
371 __LINE__, dev->blk_size);
372 return -EINVAL;
373 }
374
375 dev->bounce_size = BOUNCE_SIZE;
376 dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
377 if (!dev->bounce_buf)
378 return -ENOMEM;
379
380 error = ps3stor_setup(dev, ps3rom_interrupt);
381 if (error)
382 goto fail_free_bounce;
383
384 host = scsi_host_alloc(&ps3rom_host_template,
385 sizeof(struct ps3rom_private));
386 if (!host) {
387 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
388 __func__, __LINE__);
389 goto fail_teardown;
390 }
391
392 priv = shost_priv(host);
393 dev->sbd.core.driver_data = host;
394 priv->dev = dev;
395
396 /* One device/LUN per SCSI bus */
397 host->max_id = 1;
398 host->max_lun = 1;
399
400 error = scsi_add_host(host, &dev->sbd.core);
401 if (error) {
402 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
403 __func__, __LINE__, error);
404 error = -ENODEV;
405 goto fail_host_put;
406 }
407
408 scsi_scan_host(host);
409 return 0;
410
411fail_host_put:
412 scsi_host_put(host);
413 dev->sbd.core.driver_data = NULL;
414fail_teardown:
415 ps3stor_teardown(dev);
416fail_free_bounce:
417 kfree(dev->bounce_buf);
418 return error;
419}
420
421static int ps3rom_remove(struct ps3_system_bus_device *_dev)
422{
423 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
424 struct Scsi_Host *host = dev->sbd.core.driver_data;
425
426 scsi_remove_host(host);
427 ps3stor_teardown(dev);
428 scsi_host_put(host);
429 dev->sbd.core.driver_data = NULL;
430 kfree(dev->bounce_buf);
431 return 0;
432}
433
434static struct ps3_system_bus_driver ps3rom = {
435 .match_id = PS3_MATCH_ID_STOR_ROM,
436 .core.name = DEVICE_NAME,
437 .core.owner = THIS_MODULE,
438 .probe = ps3rom_probe,
439 .remove = ps3rom_remove
440};
441
442
443static int __init ps3rom_init(void)
444{
445 return ps3_system_bus_driver_register(&ps3rom);
446}
447
448static void __exit ps3rom_exit(void)
449{
450 ps3_system_bus_driver_unregister(&ps3rom);
451}
452
453module_init(ps3rom_init);
454module_exit(ps3rom_exit);
455
456MODULE_LICENSE("GPL");
457MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
458MODULE_AUTHOR("Sony Corporation");
459MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);