2 * PS3 BD/DVD/CD-ROM Storage Driver
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
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.
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.
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.
21 #include <linux/cdrom.h>
22 #include <linux/highmem.h>
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>
30 #include <asm/lv1call.h>
31 #include <asm/ps3stor.h>
34 #define DEVICE_NAME "ps3rom"
36 #define BOUNCE_SIZE (64*1024)
38 #define PS3ROM_MAX_SECTORS (BOUNCE_SIZE / CD_FRAMESIZE)
41 struct ps3rom_private {
42 struct ps3_storage_device *dev;
43 struct scsi_cmnd *curr_cmd;
47 #define LV1_STORAGE_SEND_ATAPI_COMMAND (1)
49 struct lv1_atapi_cmnd_block {
50 u8 pkt[32]; /* packet command block */
51 u32 pktlen; /* should be 12 for ATAPI 8020 */
54 u32 proto; /* transfer mode */
55 u32 in_out; /* transfer direction */
56 u64 buffer; /* parameter except command block */
57 u32 arglen; /* length above */
60 enum lv1_atapi_proto {
62 PIO_DATA_IN_PROTO = 1,
63 PIO_DATA_OUT_PROTO = 2,
67 enum lv1_atapi_in_out {
68 DIR_WRITE = 0, /* memory -> device */
69 DIR_READ = 1 /* device -> memory */
73 static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
75 struct ps3rom_private *priv = shost_priv(scsi_dev->host);
76 struct ps3_storage_device *dev = priv->dev;
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);
82 * ATAPI SFF8020 devices use MODE_SENSE_10,
83 * so we can prohibit MODE_SENSE_6
85 scsi_dev->use_10_for_ms = 1;
87 /* we don't support {READ,WRITE}_6 */
88 scsi_dev->use_10_for_rw = 1;
94 * copy data from device into scatter/gather buffer
96 static int fill_from_dev_buffer(struct scsi_cmnd *cmd, const void *buf)
98 int k, req_len, act_len, len, active;
100 struct scatterlist *sgpnt;
103 buflen = cmd->request_bufflen;
107 if (!cmd->request_buffer)
110 sgpnt = cmd->request_buffer;
112 for (k = 0, req_len = 0, act_len = 0; k < cmd->use_sg; ++k, ++sgpnt) {
114 kaddr = kmap_atomic(sgpnt->page, KM_IRQ0);
116 if ((req_len + len) > buflen) {
118 len = buflen - req_len;
120 memcpy(kaddr + sgpnt->offset, buf + req_len, len);
121 flush_kernel_dcache_page(sgpnt->page);
122 kunmap_atomic(kaddr, KM_IRQ0);
125 req_len += sgpnt->length;
127 cmd->resid = req_len - act_len;
132 * copy data from scatter/gather into device's buffer
134 static int fetch_to_dev_buffer(struct scsi_cmnd *cmd, void *buf)
136 int k, req_len, len, fin;
138 struct scatterlist *sgpnt;
141 buflen = cmd->request_bufflen;
145 if (!cmd->request_buffer)
148 sgpnt = cmd->request_buffer;
149 for (k = 0, req_len = 0, fin = 0; k < cmd->use_sg; ++k, ++sgpnt) {
150 kaddr = kmap_atomic(sgpnt->page, KM_IRQ0);
152 if ((req_len + len) > buflen) {
153 len = buflen - req_len;
156 memcpy(buf + req_len, kaddr + sgpnt->offset, len);
157 kunmap_atomic(kaddr, KM_IRQ0);
159 return req_len + len;
160 req_len += sgpnt->length;
165 static int ps3rom_atapi_request(struct ps3_storage_device *dev,
166 struct scsi_cmnd *cmd)
168 struct lv1_atapi_cmnd_block atapi_cmnd;
169 unsigned char opcode = cmd->cmnd[0];
173 dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
176 memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
177 memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
178 atapi_cmnd.pktlen = 12;
179 atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
180 atapi_cmnd.blocks = atapi_cmnd.arglen = cmd->request_bufflen;
181 atapi_cmnd.buffer = dev->bounce_lpar;
183 switch (cmd->sc_data_direction) {
184 case DMA_FROM_DEVICE:
185 if (cmd->request_bufflen >= CD_FRAMESIZE)
186 atapi_cmnd.proto = DMA_PROTO;
188 atapi_cmnd.proto = PIO_DATA_IN_PROTO;
189 atapi_cmnd.in_out = DIR_READ;
193 if (cmd->request_bufflen >= CD_FRAMESIZE)
194 atapi_cmnd.proto = DMA_PROTO;
196 atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
197 atapi_cmnd.in_out = DIR_WRITE;
198 res = fetch_to_dev_buffer(cmd, dev->bounce_buf);
200 return DID_ERROR << 16;
204 atapi_cmnd.proto = NON_DATA_PROTO;
208 lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
209 res = lv1_storage_send_device_command(dev->sbd.dev_id,
210 LV1_STORAGE_SEND_ATAPI_COMMAND,
211 lpar, sizeof(atapi_cmnd),
213 atapi_cmnd.arglen, &dev->tag);
214 if (res == LV1_DENIED_BY_POLICY) {
215 dev_dbg(&dev->sbd.core,
216 "%s:%u: ATAPI command 0x%02x denied by policy\n",
217 __func__, __LINE__, opcode);
218 return DID_ERROR << 16;
222 dev_err(&dev->sbd.core,
223 "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
224 __LINE__, opcode, res);
225 return DID_ERROR << 16;
231 static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
233 return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
237 static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
239 return cmd->cmnd[7] << 8 | cmd->cmnd[8];
242 static int ps3rom_read_request(struct ps3_storage_device *dev,
243 struct scsi_cmnd *cmd, u32 start_sector,
248 dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
249 __func__, __LINE__, sectors, start_sector);
251 res = lv1_storage_read(dev->sbd.dev_id,
252 dev->regions[dev->region_idx].id, start_sector,
253 sectors, 0, dev->bounce_lpar, &dev->tag);
255 dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
257 return DID_ERROR << 16;
263 static int ps3rom_write_request(struct ps3_storage_device *dev,
264 struct scsi_cmnd *cmd, u32 start_sector,
269 dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
270 __func__, __LINE__, sectors, start_sector);
272 res = fetch_to_dev_buffer(cmd, dev->bounce_buf);
274 return DID_ERROR << 16;
276 res = lv1_storage_write(dev->sbd.dev_id,
277 dev->regions[dev->region_idx].id, start_sector,
278 sectors, 0, dev->bounce_lpar, &dev->tag);
280 dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
282 return DID_ERROR << 16;
288 static int ps3rom_queuecommand(struct scsi_cmnd *cmd,
289 void (*done)(struct scsi_cmnd *))
291 struct ps3rom_private *priv = shost_priv(cmd->device->host);
292 struct ps3_storage_device *dev = priv->dev;
293 unsigned char opcode;
297 scsi_print_command(cmd);
300 priv->curr_cmd = cmd;
301 cmd->scsi_done = done;
303 opcode = cmd->cmnd[0];
305 * While we can submit READ/WRITE SCSI commands as ATAPI commands,
306 * it's recommended for various reasons (performance, error handling,
307 * ...) to use lv1_storage_{read,write}() instead
311 res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
316 res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
321 res = ps3rom_atapi_request(dev, cmd);
326 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
328 cmd->sense_buffer[0] = 0x70;
329 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
330 priv->curr_cmd = NULL;
337 static int decode_lv1_status(u64 status, unsigned char *sense_key,
338 unsigned char *asc, unsigned char *ascq)
340 if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
343 *sense_key = (status >> 16) & 0xff;
344 *asc = (status >> 8) & 0xff;
345 *ascq = status & 0xff;
349 static irqreturn_t ps3rom_interrupt(int irq, void *data)
351 struct ps3_storage_device *dev = data;
352 struct Scsi_Host *host;
353 struct ps3rom_private *priv;
354 struct scsi_cmnd *cmd;
357 unsigned char sense_key, asc, ascq;
359 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
361 * status = -1 may mean that ATAPI transport completed OK, but
362 * ATAPI command itself resulted CHECK CONDITION
363 * so, upper layer should issue REQUEST_SENSE to check the sense data
367 dev_err(&dev->sbd.core,
368 "%s:%u: tag mismatch, got %lx, expected %lx\n",
369 __func__, __LINE__, tag, dev->tag);
372 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%lx\n",
373 __func__, __LINE__, res, status);
377 host = dev->sbd.core.driver_data;
378 priv = shost_priv(host);
379 cmd = priv->curr_cmd;
383 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
384 res = fill_from_dev_buffer(cmd, dev->bounce_buf);
386 cmd->result = DID_ERROR << 16;
390 cmd->result = DID_OK << 16;
394 if (cmd->cmnd[0] == REQUEST_SENSE) {
395 /* SCSI spec says request sense should never get error */
396 dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
398 cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
402 if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
403 cmd->result = DID_ERROR << 16;
407 cmd->sense_buffer[0] = 0x70;
408 cmd->sense_buffer[2] = sense_key;
409 cmd->sense_buffer[7] = 16 - 6;
410 cmd->sense_buffer[12] = asc;
411 cmd->sense_buffer[13] = ascq;
412 cmd->result = SAM_STAT_CHECK_CONDITION;
415 priv->curr_cmd = NULL;
420 static struct scsi_host_template ps3rom_host_template = {
422 .slave_configure = ps3rom_slave_configure,
423 .queuecommand = ps3rom_queuecommand,
426 .sg_tablesize = SG_ALL,
428 .emulated = 1, /* only sg driver uses this */
429 .max_sectors = PS3ROM_MAX_SECTORS,
430 .use_clustering = ENABLE_CLUSTERING,
431 .module = THIS_MODULE,
435 static int __devinit ps3rom_probe(struct ps3_system_bus_device *_dev)
437 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
439 struct Scsi_Host *host;
440 struct ps3rom_private *priv;
442 if (dev->blk_size != CD_FRAMESIZE) {
443 dev_err(&dev->sbd.core,
444 "%s:%u: cannot handle block size %lu\n", __func__,
445 __LINE__, dev->blk_size);
449 dev->bounce_size = BOUNCE_SIZE;
450 dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
451 if (!dev->bounce_buf)
454 error = ps3stor_setup(dev, ps3rom_interrupt);
456 goto fail_free_bounce;
458 host = scsi_host_alloc(&ps3rom_host_template,
459 sizeof(struct ps3rom_private));
461 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
466 priv = shost_priv(host);
467 dev->sbd.core.driver_data = host;
470 /* One device/LUN per SCSI bus */
474 error = scsi_add_host(host, &dev->sbd.core);
476 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
477 __func__, __LINE__, error);
482 scsi_scan_host(host);
487 dev->sbd.core.driver_data = NULL;
489 ps3stor_teardown(dev);
491 kfree(dev->bounce_buf);
495 static int ps3rom_remove(struct ps3_system_bus_device *_dev)
497 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
498 struct Scsi_Host *host = dev->sbd.core.driver_data;
500 scsi_remove_host(host);
501 ps3stor_teardown(dev);
503 dev->sbd.core.driver_data = NULL;
504 kfree(dev->bounce_buf);
508 static struct ps3_system_bus_driver ps3rom = {
509 .match_id = PS3_MATCH_ID_STOR_ROM,
510 .core.name = DEVICE_NAME,
511 .core.owner = THIS_MODULE,
512 .probe = ps3rom_probe,
513 .remove = ps3rom_remove
517 static int __init ps3rom_init(void)
519 return ps3_system_bus_driver_register(&ps3rom);
522 static void __exit ps3rom_exit(void)
524 ps3_system_bus_driver_unregister(&ps3rom);
527 module_init(ps3rom_init);
528 module_exit(ps3rom_exit);
530 MODULE_LICENSE("GPL");
531 MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
532 MODULE_AUTHOR("Sony Corporation");
533 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);