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/module.h>
24 #include <linux/slab.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_dbg.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_eh.h>
33 #include <asm/lv1call.h>
34 #include <asm/ps3stor.h>
37 #define DEVICE_NAME "ps3rom"
39 #define BOUNCE_SIZE (64*1024)
41 #define PS3ROM_MAX_SECTORS (BOUNCE_SIZE >> 9)
44 struct ps3rom_private
{
45 struct ps3_storage_device
*dev
;
46 struct scsi_cmnd
*curr_cmd
;
50 #define LV1_STORAGE_SEND_ATAPI_COMMAND (1)
52 struct lv1_atapi_cmnd_block
{
53 u8 pkt
[32]; /* packet command block */
54 u32 pktlen
; /* should be 12 for ATAPI 8020 */
57 u32 proto
; /* transfer mode */
58 u32 in_out
; /* transfer direction */
59 u64 buffer
; /* parameter except command block */
60 u32 arglen
; /* length above */
63 enum lv1_atapi_proto
{
65 PIO_DATA_IN_PROTO
= 1,
66 PIO_DATA_OUT_PROTO
= 2,
70 enum lv1_atapi_in_out
{
71 DIR_WRITE
= 0, /* memory -> device */
72 DIR_READ
= 1 /* device -> memory */
76 static int ps3rom_slave_configure(struct scsi_device
*scsi_dev
)
78 struct ps3rom_private
*priv
= shost_priv(scsi_dev
->host
);
79 struct ps3_storage_device
*dev
= priv
->dev
;
81 dev_dbg(&dev
->sbd
.core
, "%s:%u: id %u, lun %llu, channel %u\n", __func__
,
82 __LINE__
, scsi_dev
->id
, scsi_dev
->lun
, scsi_dev
->channel
);
85 * ATAPI SFF8020 devices use MODE_SENSE_10,
86 * so we can prohibit MODE_SENSE_6
88 scsi_dev
->use_10_for_ms
= 1;
90 /* we don't support {READ,WRITE}_6 */
91 scsi_dev
->use_10_for_rw
= 1;
96 static int ps3rom_atapi_request(struct ps3_storage_device
*dev
,
97 struct scsi_cmnd
*cmd
)
99 struct lv1_atapi_cmnd_block atapi_cmnd
;
100 unsigned char opcode
= cmd
->cmnd
[0];
104 dev_dbg(&dev
->sbd
.core
, "%s:%u: send ATAPI command 0x%02x\n", __func__
,
107 memset(&atapi_cmnd
, 0, sizeof(struct lv1_atapi_cmnd_block
));
108 memcpy(&atapi_cmnd
.pkt
, cmd
->cmnd
, 12);
109 atapi_cmnd
.pktlen
= 12;
110 atapi_cmnd
.block_size
= 1; /* transfer size is block_size * blocks */
111 atapi_cmnd
.blocks
= atapi_cmnd
.arglen
= scsi_bufflen(cmd
);
112 atapi_cmnd
.buffer
= dev
->bounce_lpar
;
114 switch (cmd
->sc_data_direction
) {
115 case DMA_FROM_DEVICE
:
116 if (scsi_bufflen(cmd
) >= CD_FRAMESIZE
)
117 atapi_cmnd
.proto
= DMA_PROTO
;
119 atapi_cmnd
.proto
= PIO_DATA_IN_PROTO
;
120 atapi_cmnd
.in_out
= DIR_READ
;
124 if (scsi_bufflen(cmd
) >= CD_FRAMESIZE
)
125 atapi_cmnd
.proto
= DMA_PROTO
;
127 atapi_cmnd
.proto
= PIO_DATA_OUT_PROTO
;
128 atapi_cmnd
.in_out
= DIR_WRITE
;
129 scsi_sg_copy_to_buffer(cmd
, dev
->bounce_buf
, dev
->bounce_size
);
133 atapi_cmnd
.proto
= NON_DATA_PROTO
;
137 lpar
= ps3_mm_phys_to_lpar(__pa(&atapi_cmnd
));
138 res
= lv1_storage_send_device_command(dev
->sbd
.dev_id
,
139 LV1_STORAGE_SEND_ATAPI_COMMAND
,
140 lpar
, sizeof(atapi_cmnd
),
142 atapi_cmnd
.arglen
, &dev
->tag
);
143 if (res
== LV1_DENIED_BY_POLICY
) {
144 dev_dbg(&dev
->sbd
.core
,
145 "%s:%u: ATAPI command 0x%02x denied by policy\n",
146 __func__
, __LINE__
, opcode
);
147 return DID_ERROR
<< 16;
151 dev_err(&dev
->sbd
.core
,
152 "%s:%u: ATAPI command 0x%02x failed %d\n", __func__
,
153 __LINE__
, opcode
, res
);
154 return DID_ERROR
<< 16;
160 static inline unsigned int srb10_lba(const struct scsi_cmnd
*cmd
)
162 return cmd
->cmnd
[2] << 24 | cmd
->cmnd
[3] << 16 | cmd
->cmnd
[4] << 8 |
166 static inline unsigned int srb10_len(const struct scsi_cmnd
*cmd
)
168 return cmd
->cmnd
[7] << 8 | cmd
->cmnd
[8];
171 static int ps3rom_read_request(struct ps3_storage_device
*dev
,
172 struct scsi_cmnd
*cmd
, u32 start_sector
,
177 dev_dbg(&dev
->sbd
.core
, "%s:%u: read %u sectors starting at %u\n",
178 __func__
, __LINE__
, sectors
, start_sector
);
180 res
= lv1_storage_read(dev
->sbd
.dev_id
,
181 dev
->regions
[dev
->region_idx
].id
, start_sector
,
182 sectors
, 0, dev
->bounce_lpar
, &dev
->tag
);
184 dev_err(&dev
->sbd
.core
, "%s:%u: read failed %d\n", __func__
,
186 return DID_ERROR
<< 16;
192 static int ps3rom_write_request(struct ps3_storage_device
*dev
,
193 struct scsi_cmnd
*cmd
, u32 start_sector
,
198 dev_dbg(&dev
->sbd
.core
, "%s:%u: write %u sectors starting at %u\n",
199 __func__
, __LINE__
, sectors
, start_sector
);
201 scsi_sg_copy_to_buffer(cmd
, dev
->bounce_buf
, dev
->bounce_size
);
203 res
= lv1_storage_write(dev
->sbd
.dev_id
,
204 dev
->regions
[dev
->region_idx
].id
, start_sector
,
205 sectors
, 0, dev
->bounce_lpar
, &dev
->tag
);
207 dev_err(&dev
->sbd
.core
, "%s:%u: write failed %d\n", __func__
,
209 return DID_ERROR
<< 16;
215 static int ps3rom_queuecommand_lck(struct scsi_cmnd
*cmd
,
216 void (*done
)(struct scsi_cmnd
*))
218 struct ps3rom_private
*priv
= shost_priv(cmd
->device
->host
);
219 struct ps3_storage_device
*dev
= priv
->dev
;
220 unsigned char opcode
;
223 priv
->curr_cmd
= cmd
;
224 cmd
->scsi_done
= done
;
226 opcode
= cmd
->cmnd
[0];
228 * While we can submit READ/WRITE SCSI commands as ATAPI commands,
229 * it's recommended for various reasons (performance, error handling,
230 * ...) to use lv1_storage_{read,write}() instead
234 res
= ps3rom_read_request(dev
, cmd
, srb10_lba(cmd
),
239 res
= ps3rom_write_request(dev
, cmd
, srb10_lba(cmd
),
244 res
= ps3rom_atapi_request(dev
, cmd
);
249 memset(cmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
251 cmd
->sense_buffer
[0] = 0x70;
252 cmd
->sense_buffer
[2] = ILLEGAL_REQUEST
;
253 priv
->curr_cmd
= NULL
;
260 static DEF_SCSI_QCMD(ps3rom_queuecommand
)
262 static int decode_lv1_status(u64 status
, unsigned char *sense_key
,
263 unsigned char *asc
, unsigned char *ascq
)
265 if (((status
>> 24) & 0xff) != SAM_STAT_CHECK_CONDITION
)
268 *sense_key
= (status
>> 16) & 0xff;
269 *asc
= (status
>> 8) & 0xff;
270 *ascq
= status
& 0xff;
274 static irqreturn_t
ps3rom_interrupt(int irq
, void *data
)
276 struct ps3_storage_device
*dev
= data
;
277 struct Scsi_Host
*host
;
278 struct ps3rom_private
*priv
;
279 struct scsi_cmnd
*cmd
;
282 unsigned char sense_key
, asc
, ascq
;
284 res
= lv1_storage_get_async_status(dev
->sbd
.dev_id
, &tag
, &status
);
286 * status = -1 may mean that ATAPI transport completed OK, but
287 * ATAPI command itself resulted CHECK CONDITION
288 * so, upper layer should issue REQUEST_SENSE to check the sense data
292 dev_err(&dev
->sbd
.core
,
293 "%s:%u: tag mismatch, got %llx, expected %llx\n",
294 __func__
, __LINE__
, tag
, dev
->tag
);
297 dev_err(&dev
->sbd
.core
, "%s:%u: res=%d status=0x%llx\n",
298 __func__
, __LINE__
, res
, status
);
302 host
= ps3_system_bus_get_drvdata(&dev
->sbd
);
303 priv
= shost_priv(host
);
304 cmd
= priv
->curr_cmd
;
308 if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
) {
311 len
= scsi_sg_copy_from_buffer(cmd
,
315 scsi_set_resid(cmd
, scsi_bufflen(cmd
) - len
);
317 cmd
->result
= DID_OK
<< 16;
321 if (cmd
->cmnd
[0] == REQUEST_SENSE
) {
322 /* SCSI spec says request sense should never get error */
323 dev_err(&dev
->sbd
.core
, "%s:%u: end error without autosense\n",
325 cmd
->result
= DID_ERROR
<< 16 | SAM_STAT_CHECK_CONDITION
;
329 if (decode_lv1_status(status
, &sense_key
, &asc
, &ascq
)) {
330 cmd
->result
= DID_ERROR
<< 16;
334 scsi_build_sense_buffer(0, cmd
->sense_buffer
, sense_key
, asc
, ascq
);
335 cmd
->result
= SAM_STAT_CHECK_CONDITION
;
338 priv
->curr_cmd
= NULL
;
343 static struct scsi_host_template ps3rom_host_template
= {
345 .slave_configure
= ps3rom_slave_configure
,
346 .queuecommand
= ps3rom_queuecommand
,
349 .sg_tablesize
= SG_ALL
,
350 .emulated
= 1, /* only sg driver uses this */
351 .max_sectors
= PS3ROM_MAX_SECTORS
,
352 .module
= THIS_MODULE
,
356 static int ps3rom_probe(struct ps3_system_bus_device
*_dev
)
358 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
360 struct Scsi_Host
*host
;
361 struct ps3rom_private
*priv
;
363 if (dev
->blk_size
!= CD_FRAMESIZE
) {
364 dev_err(&dev
->sbd
.core
,
365 "%s:%u: cannot handle block size %llu\n", __func__
,
366 __LINE__
, dev
->blk_size
);
370 dev
->bounce_size
= BOUNCE_SIZE
;
371 dev
->bounce_buf
= kmalloc(BOUNCE_SIZE
, GFP_DMA
);
372 if (!dev
->bounce_buf
)
375 error
= ps3stor_setup(dev
, ps3rom_interrupt
);
377 goto fail_free_bounce
;
379 host
= scsi_host_alloc(&ps3rom_host_template
,
380 sizeof(struct ps3rom_private
));
382 dev_err(&dev
->sbd
.core
, "%s:%u: scsi_host_alloc failed\n",
388 priv
= shost_priv(host
);
389 ps3_system_bus_set_drvdata(&dev
->sbd
, host
);
392 /* One device/LUN per SCSI bus */
396 error
= scsi_add_host(host
, &dev
->sbd
.core
);
398 dev_err(&dev
->sbd
.core
, "%s:%u: scsi_host_alloc failed %d\n",
399 __func__
, __LINE__
, error
);
404 scsi_scan_host(host
);
409 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
411 ps3stor_teardown(dev
);
413 kfree(dev
->bounce_buf
);
417 static int ps3rom_remove(struct ps3_system_bus_device
*_dev
)
419 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
420 struct Scsi_Host
*host
= ps3_system_bus_get_drvdata(&dev
->sbd
);
422 scsi_remove_host(host
);
423 ps3stor_teardown(dev
);
425 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
426 kfree(dev
->bounce_buf
);
430 static struct ps3_system_bus_driver ps3rom
= {
431 .match_id
= PS3_MATCH_ID_STOR_ROM
,
432 .core
.name
= DEVICE_NAME
,
433 .core
.owner
= THIS_MODULE
,
434 .probe
= ps3rom_probe
,
435 .remove
= ps3rom_remove
439 static int __init
ps3rom_init(void)
441 return ps3_system_bus_driver_register(&ps3rom
);
444 static void __exit
ps3rom_exit(void)
446 ps3_system_bus_driver_unregister(&ps3rom
);
449 module_init(ps3rom_init
);
450 module_exit(ps3rom_exit
);
452 MODULE_LICENSE("GPL");
453 MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
454 MODULE_AUTHOR("Sony Corporation");
455 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM
);