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>
23 #include <linux/slab.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_dbg.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_eh.h>
32 #include <asm/lv1call.h>
33 #include <asm/ps3stor.h>
36 #define DEVICE_NAME "ps3rom"
38 #define BOUNCE_SIZE (64*1024)
40 #define PS3ROM_MAX_SECTORS (BOUNCE_SIZE >> 9)
43 struct ps3rom_private {
44 struct ps3_storage_device *dev;
45 struct scsi_cmnd *curr_cmd;
49 #define LV1_STORAGE_SEND_ATAPI_COMMAND (1)
51 struct lv1_atapi_cmnd_block {
52 u8 pkt[32]; /* packet command block */
53 u32 pktlen; /* should be 12 for ATAPI 8020 */
56 u32 proto; /* transfer mode */
57 u32 in_out; /* transfer direction */
58 u64 buffer; /* parameter except command block */
59 u32 arglen; /* length above */
62 enum lv1_atapi_proto {
64 PIO_DATA_IN_PROTO = 1,
65 PIO_DATA_OUT_PROTO = 2,
69 enum lv1_atapi_in_out {
70 DIR_WRITE = 0, /* memory -> device */
71 DIR_READ = 1 /* device -> memory */
75 static int ps3rom_slave_configure(struct scsi_device *scsi_dev)
77 struct ps3rom_private *priv = shost_priv(scsi_dev->host);
78 struct ps3_storage_device *dev = priv->dev;
80 dev_dbg(&dev->sbd.core, "%s:%u: id %u, lun %u, channel %u\n", __func__,
81 __LINE__, scsi_dev->id, scsi_dev->lun, scsi_dev->channel);
84 * ATAPI SFF8020 devices use MODE_SENSE_10,
85 * so we can prohibit MODE_SENSE_6
87 scsi_dev->use_10_for_ms = 1;
89 /* we don't support {READ,WRITE}_6 */
90 scsi_dev->use_10_for_rw = 1;
95 static int ps3rom_atapi_request(struct ps3_storage_device *dev,
96 struct scsi_cmnd *cmd)
98 struct lv1_atapi_cmnd_block atapi_cmnd;
99 unsigned char opcode = cmd->cmnd[0];
103 dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
106 memset(&atapi_cmnd, 0, sizeof(struct lv1_atapi_cmnd_block));
107 memcpy(&atapi_cmnd.pkt, cmd->cmnd, 12);
108 atapi_cmnd.pktlen = 12;
109 atapi_cmnd.block_size = 1; /* transfer size is block_size * blocks */
110 atapi_cmnd.blocks = atapi_cmnd.arglen = scsi_bufflen(cmd);
111 atapi_cmnd.buffer = dev->bounce_lpar;
113 switch (cmd->sc_data_direction) {
114 case DMA_FROM_DEVICE:
115 if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
116 atapi_cmnd.proto = DMA_PROTO;
118 atapi_cmnd.proto = PIO_DATA_IN_PROTO;
119 atapi_cmnd.in_out = DIR_READ;
123 if (scsi_bufflen(cmd) >= CD_FRAMESIZE)
124 atapi_cmnd.proto = DMA_PROTO;
126 atapi_cmnd.proto = PIO_DATA_OUT_PROTO;
127 atapi_cmnd.in_out = DIR_WRITE;
128 scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
132 atapi_cmnd.proto = NON_DATA_PROTO;
136 lpar = ps3_mm_phys_to_lpar(__pa(&atapi_cmnd));
137 res = lv1_storage_send_device_command(dev->sbd.dev_id,
138 LV1_STORAGE_SEND_ATAPI_COMMAND,
139 lpar, sizeof(atapi_cmnd),
141 atapi_cmnd.arglen, &dev->tag);
142 if (res == LV1_DENIED_BY_POLICY) {
143 dev_dbg(&dev->sbd.core,
144 "%s:%u: ATAPI command 0x%02x denied by policy\n",
145 __func__, __LINE__, opcode);
146 return DID_ERROR << 16;
150 dev_err(&dev->sbd.core,
151 "%s:%u: ATAPI command 0x%02x failed %d\n", __func__,
152 __LINE__, opcode, res);
153 return DID_ERROR << 16;
159 static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
161 return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
165 static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
167 return cmd->cmnd[7] << 8 | cmd->cmnd[8];
170 static int ps3rom_read_request(struct ps3_storage_device *dev,
171 struct scsi_cmnd *cmd, u32 start_sector,
176 dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
177 __func__, __LINE__, sectors, start_sector);
179 res = lv1_storage_read(dev->sbd.dev_id,
180 dev->regions[dev->region_idx].id, start_sector,
181 sectors, 0, dev->bounce_lpar, &dev->tag);
183 dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
185 return DID_ERROR << 16;
191 static int ps3rom_write_request(struct ps3_storage_device *dev,
192 struct scsi_cmnd *cmd, u32 start_sector,
197 dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
198 __func__, __LINE__, sectors, start_sector);
200 scsi_sg_copy_to_buffer(cmd, dev->bounce_buf, dev->bounce_size);
202 res = lv1_storage_write(dev->sbd.dev_id,
203 dev->regions[dev->region_idx].id, start_sector,
204 sectors, 0, dev->bounce_lpar, &dev->tag);
206 dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
208 return DID_ERROR << 16;
214 static int ps3rom_queuecommand(struct scsi_cmnd *cmd,
215 void (*done)(struct scsi_cmnd *))
217 struct ps3rom_private *priv = shost_priv(cmd->device->host);
218 struct ps3_storage_device *dev = priv->dev;
219 unsigned char opcode;
223 scsi_print_command(cmd);
226 priv->curr_cmd = cmd;
227 cmd->scsi_done = done;
229 opcode = cmd->cmnd[0];
231 * While we can submit READ/WRITE SCSI commands as ATAPI commands,
232 * it's recommended for various reasons (performance, error handling,
233 * ...) to use lv1_storage_{read,write}() instead
237 res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
242 res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
247 res = ps3rom_atapi_request(dev, cmd);
252 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
254 cmd->sense_buffer[0] = 0x70;
255 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
256 priv->curr_cmd = NULL;
263 static int decode_lv1_status(u64 status, unsigned char *sense_key,
264 unsigned char *asc, unsigned char *ascq)
266 if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
269 *sense_key = (status >> 16) & 0xff;
270 *asc = (status >> 8) & 0xff;
271 *ascq = status & 0xff;
275 static irqreturn_t ps3rom_interrupt(int irq, void *data)
277 struct ps3_storage_device *dev = data;
278 struct Scsi_Host *host;
279 struct ps3rom_private *priv;
280 struct scsi_cmnd *cmd;
283 unsigned char sense_key, asc, ascq;
285 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
287 * status = -1 may mean that ATAPI transport completed OK, but
288 * ATAPI command itself resulted CHECK CONDITION
289 * so, upper layer should issue REQUEST_SENSE to check the sense data
293 dev_err(&dev->sbd.core,
294 "%s:%u: tag mismatch, got %llx, expected %llx\n",
295 __func__, __LINE__, tag, dev->tag);
298 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
299 __func__, __LINE__, res, status);
303 host = ps3_system_bus_get_drvdata(&dev->sbd);
304 priv = shost_priv(host);
305 cmd = priv->curr_cmd;
309 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
312 len = scsi_sg_copy_from_buffer(cmd,
316 scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
318 cmd->result = DID_OK << 16;
322 if (cmd->cmnd[0] == REQUEST_SENSE) {
323 /* SCSI spec says request sense should never get error */
324 dev_err(&dev->sbd.core, "%s:%u: end error without autosense\n",
326 cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
330 if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
331 cmd->result = DID_ERROR << 16;
335 scsi_build_sense_buffer(0, cmd->sense_buffer, sense_key, asc, ascq);
336 cmd->result = SAM_STAT_CHECK_CONDITION;
339 priv->curr_cmd = NULL;
344 static struct scsi_host_template ps3rom_host_template = {
346 .slave_configure = ps3rom_slave_configure,
347 .queuecommand = ps3rom_queuecommand,
350 .sg_tablesize = SG_ALL,
352 .emulated = 1, /* only sg driver uses this */
353 .max_sectors = PS3ROM_MAX_SECTORS,
354 .use_clustering = ENABLE_CLUSTERING,
355 .module = THIS_MODULE,
359 static int __devinit ps3rom_probe(struct ps3_system_bus_device *_dev)
361 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
363 struct Scsi_Host *host;
364 struct ps3rom_private *priv;
366 if (dev->blk_size != CD_FRAMESIZE) {
367 dev_err(&dev->sbd.core,
368 "%s:%u: cannot handle block size %llu\n", __func__,
369 __LINE__, dev->blk_size);
373 dev->bounce_size = BOUNCE_SIZE;
374 dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
375 if (!dev->bounce_buf)
378 error = ps3stor_setup(dev, ps3rom_interrupt);
380 goto fail_free_bounce;
382 host = scsi_host_alloc(&ps3rom_host_template,
383 sizeof(struct ps3rom_private));
385 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
390 priv = shost_priv(host);
391 ps3_system_bus_set_drvdata(&dev->sbd, host);
394 /* One device/LUN per SCSI bus */
398 error = scsi_add_host(host, &dev->sbd.core);
400 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
401 __func__, __LINE__, error);
406 scsi_scan_host(host);
411 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
413 ps3stor_teardown(dev);
415 kfree(dev->bounce_buf);
419 static int ps3rom_remove(struct ps3_system_bus_device *_dev)
421 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
422 struct Scsi_Host *host = ps3_system_bus_get_drvdata(&dev->sbd);
424 scsi_remove_host(host);
425 ps3stor_teardown(dev);
427 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
428 kfree(dev->bounce_buf);
432 static struct ps3_system_bus_driver ps3rom = {
433 .match_id = PS3_MATCH_ID_STOR_ROM,
434 .core.name = DEVICE_NAME,
435 .core.owner = THIS_MODULE,
436 .probe = ps3rom_probe,
437 .remove = ps3rom_remove
441 static int __init ps3rom_init(void)
443 return ps3_system_bus_driver_register(&ps3rom);
446 static void __exit ps3rom_exit(void)
448 ps3_system_bus_driver_unregister(&ps3rom);
451 module_init(ps3rom_init);
452 module_exit(ps3rom_exit);
454 MODULE_LICENSE("GPL");
455 MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
456 MODULE_AUTHOR("Sony Corporation");
457 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);