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