blob: b50f1e14f2a5618dab46e1a600a43d85fdc02ab3 [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
38#define PS3ROM_MAX_SECTORS (BOUNCE_SIZE / CD_FRAMESIZE)
39
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
93/*
94 * copy data from device into scatter/gather buffer
95 */
96static int fill_from_dev_buffer(struct scsi_cmnd *cmd, const void *buf)
97{
98 int k, req_len, act_len, len, active;
99 void *kaddr;
100 struct scatterlist *sgpnt;
101 unsigned int buflen;
102
103 buflen = cmd->request_bufflen;
104 if (!buflen)
105 return 0;
106
107 if (!cmd->request_buffer)
108 return -1;
109
110 sgpnt = cmd->request_buffer;
111 active = 1;
112 for (k = 0, req_len = 0, act_len = 0; k < cmd->use_sg; ++k, ++sgpnt) {
113 if (active) {
114 kaddr = kmap_atomic(sgpnt->page, KM_IRQ0);
115 len = sgpnt->length;
116 if ((req_len + len) > buflen) {
117 active = 0;
118 len = buflen - req_len;
119 }
120 memcpy(kaddr + sgpnt->offset, buf + req_len, len);
121 flush_kernel_dcache_page(sgpnt->page);
122 kunmap_atomic(kaddr, KM_IRQ0);
123 act_len += len;
124 }
125 req_len += sgpnt->length;
126 }
127 cmd->resid = req_len - act_len;
128 return 0;
129}
130
131/*
132 * copy data from scatter/gather into device's buffer
133 */
134static int fetch_to_dev_buffer(struct scsi_cmnd *cmd, void *buf)
135{
136 int k, req_len, len, fin;
137 void *kaddr;
138 struct scatterlist *sgpnt;
139 unsigned int buflen;
140
141 buflen = cmd->request_bufflen;
142 if (!buflen)
143 return 0;
144
145 if (!cmd->request_buffer)
146 return -1;
147
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);
151 len = sgpnt->length;
152 if ((req_len + len) > buflen) {
153 len = buflen - req_len;
154 fin = 1;
155 }
156 memcpy(buf + req_len, kaddr + sgpnt->offset, len);
157 kunmap_atomic(kaddr, KM_IRQ0);
158 if (fin)
159 return req_len + len;
160 req_len += sgpnt->length;
161 }
162 return req_len;
163}
164
165static int ps3rom_atapi_request(struct ps3_storage_device *dev,
166 struct scsi_cmnd *cmd)
167{
168 struct lv1_atapi_cmnd_block atapi_cmnd;
169 unsigned char opcode = cmd->cmnd[0];
170 int res;
171 u64 lpar;
172
173 dev_dbg(&dev->sbd.core, "%s:%u: send ATAPI command 0x%02x\n", __func__,
174 __LINE__, opcode);
175
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;
182
183 switch (cmd->sc_data_direction) {
184 case DMA_FROM_DEVICE:
185 if (cmd->request_bufflen >= CD_FRAMESIZE)
186 atapi_cmnd.proto = DMA_PROTO;
187 else
188 atapi_cmnd.proto = PIO_DATA_IN_PROTO;
189 atapi_cmnd.in_out = DIR_READ;
190 break;
191
192 case DMA_TO_DEVICE:
193 if (cmd->request_bufflen >= CD_FRAMESIZE)
194 atapi_cmnd.proto = DMA_PROTO;
195 else
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);
199 if (res < 0)
200 return DID_ERROR << 16;
201 break;
202
203 default:
204 atapi_cmnd.proto = NON_DATA_PROTO;
205 break;
206 }
207
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),
212 atapi_cmnd.buffer,
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;
219 }
220
221 if (res) {
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;
226 }
227
228 return 0;
229}
230
231static inline unsigned int srb10_lba(const struct scsi_cmnd *cmd)
232{
233 return cmd->cmnd[2] << 24 | cmd->cmnd[3] << 16 | cmd->cmnd[4] << 8 |
234 cmd->cmnd[5];
235}
236
237static inline unsigned int srb10_len(const struct scsi_cmnd *cmd)
238{
239 return cmd->cmnd[7] << 8 | cmd->cmnd[8];
240}
241
242static int ps3rom_read_request(struct ps3_storage_device *dev,
243 struct scsi_cmnd *cmd, u32 start_sector,
244 u32 sectors)
245{
246 int res;
247
248 dev_dbg(&dev->sbd.core, "%s:%u: read %u sectors starting at %u\n",
249 __func__, __LINE__, sectors, start_sector);
250
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);
254 if (res) {
255 dev_err(&dev->sbd.core, "%s:%u: read failed %d\n", __func__,
256 __LINE__, res);
257 return DID_ERROR << 16;
258 }
259
260 return 0;
261}
262
263static int ps3rom_write_request(struct ps3_storage_device *dev,
264 struct scsi_cmnd *cmd, u32 start_sector,
265 u32 sectors)
266{
267 int res;
268
269 dev_dbg(&dev->sbd.core, "%s:%u: write %u sectors starting at %u\n",
270 __func__, __LINE__, sectors, start_sector);
271
272 res = fetch_to_dev_buffer(cmd, dev->bounce_buf);
273 if (res < 0)
274 return DID_ERROR << 16;
275
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);
279 if (res) {
280 dev_err(&dev->sbd.core, "%s:%u: write failed %d\n", __func__,
281 __LINE__, res);
282 return DID_ERROR << 16;
283 }
284
285 return 0;
286}
287
288static int ps3rom_queuecommand(struct scsi_cmnd *cmd,
289 void (*done)(struct scsi_cmnd *))
290{
291 struct ps3rom_private *priv = shost_priv(cmd->device->host);
292 struct ps3_storage_device *dev = priv->dev;
293 unsigned char opcode;
294 int res;
295
296#ifdef DEBUG
297 scsi_print_command(cmd);
298#endif
299
300 priv->curr_cmd = cmd;
301 cmd->scsi_done = done;
302
303 opcode = cmd->cmnd[0];
304 /*
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
308 */
309 switch (opcode) {
310 case READ_10:
311 res = ps3rom_read_request(dev, cmd, srb10_lba(cmd),
312 srb10_len(cmd));
313 break;
314
315 case WRITE_10:
316 res = ps3rom_write_request(dev, cmd, srb10_lba(cmd),
317 srb10_len(cmd));
318 break;
319
320 default:
321 res = ps3rom_atapi_request(dev, cmd);
322 break;
323 }
324
325 if (res) {
326 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
327 cmd->result = res;
328 cmd->sense_buffer[0] = 0x70;
329 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
330 priv->curr_cmd = NULL;
331 cmd->scsi_done(cmd);
332 }
333
334 return 0;
335}
336
337static int decode_lv1_status(u64 status, unsigned char *sense_key,
338 unsigned char *asc, unsigned char *ascq)
339{
340 if (((status >> 24) & 0xff) != SAM_STAT_CHECK_CONDITION)
341 return -1;
342
343 *sense_key = (status >> 16) & 0xff;
344 *asc = (status >> 8) & 0xff;
345 *ascq = status & 0xff;
346 return 0;
347}
348
349static irqreturn_t ps3rom_interrupt(int irq, void *data)
350{
351 struct ps3_storage_device *dev = data;
352 struct Scsi_Host *host;
353 struct ps3rom_private *priv;
354 struct scsi_cmnd *cmd;
355 int res;
356 u64 tag, status;
357 unsigned char sense_key, asc, ascq;
358
359 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
360 /*
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
364 */
365
366 if (tag != dev->tag)
367 dev_err(&dev->sbd.core,
368 "%s:%u: tag mismatch, got %lx, expected %lx\n",
369 __func__, __LINE__, tag, dev->tag);
370
371 if (res) {
372 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%lx\n",
373 __func__, __LINE__, res, status);
374 return IRQ_HANDLED;
375 }
376
377 host = dev->sbd.core.driver_data;
378 priv = shost_priv(host);
379 cmd = priv->curr_cmd;
380
381 if (!status) {
382 /* OK, completed */
383 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
384 res = fill_from_dev_buffer(cmd, dev->bounce_buf);
385 if (res) {
386 cmd->result = DID_ERROR << 16;
387 goto done;
388 }
389 }
390 cmd->result = DID_OK << 16;
391 goto done;
392 }
393
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",
397 __func__, __LINE__);
398 cmd->result = DID_ERROR << 16 | SAM_STAT_CHECK_CONDITION;
399 goto done;
400 }
401
402 if (decode_lv1_status(status, &sense_key, &asc, &ascq)) {
403 cmd->result = DID_ERROR << 16;
404 goto done;
405 }
406
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;
413
414done:
415 priv->curr_cmd = NULL;
416 cmd->scsi_done(cmd);
417 return IRQ_HANDLED;
418}
419
420static struct scsi_host_template ps3rom_host_template = {
421 .name = DEVICE_NAME,
422 .slave_configure = ps3rom_slave_configure,
423 .queuecommand = ps3rom_queuecommand,
424 .can_queue = 1,
425 .this_id = 7,
426 .sg_tablesize = SG_ALL,
427 .cmd_per_lun = 1,
428 .emulated = 1, /* only sg driver uses this */
429 .max_sectors = PS3ROM_MAX_SECTORS,
430 .use_clustering = ENABLE_CLUSTERING,
431 .module = THIS_MODULE,
432};
433
434
435static int __devinit ps3rom_probe(struct ps3_system_bus_device *_dev)
436{
437 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
438 int error;
439 struct Scsi_Host *host;
440 struct ps3rom_private *priv;
441
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);
446 return -EINVAL;
447 }
448
449 dev->bounce_size = BOUNCE_SIZE;
450 dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
451 if (!dev->bounce_buf)
452 return -ENOMEM;
453
454 error = ps3stor_setup(dev, ps3rom_interrupt);
455 if (error)
456 goto fail_free_bounce;
457
458 host = scsi_host_alloc(&ps3rom_host_template,
459 sizeof(struct ps3rom_private));
460 if (!host) {
461 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed\n",
462 __func__, __LINE__);
463 goto fail_teardown;
464 }
465
466 priv = shost_priv(host);
467 dev->sbd.core.driver_data = host;
468 priv->dev = dev;
469
470 /* One device/LUN per SCSI bus */
471 host->max_id = 1;
472 host->max_lun = 1;
473
474 error = scsi_add_host(host, &dev->sbd.core);
475 if (error) {
476 dev_err(&dev->sbd.core, "%s:%u: scsi_host_alloc failed %d\n",
477 __func__, __LINE__, error);
478 error = -ENODEV;
479 goto fail_host_put;
480 }
481
482 scsi_scan_host(host);
483 return 0;
484
485fail_host_put:
486 scsi_host_put(host);
487 dev->sbd.core.driver_data = NULL;
488fail_teardown:
489 ps3stor_teardown(dev);
490fail_free_bounce:
491 kfree(dev->bounce_buf);
492 return error;
493}
494
495static int ps3rom_remove(struct ps3_system_bus_device *_dev)
496{
497 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
498 struct Scsi_Host *host = dev->sbd.core.driver_data;
499
500 scsi_remove_host(host);
501 ps3stor_teardown(dev);
502 scsi_host_put(host);
503 dev->sbd.core.driver_data = NULL;
504 kfree(dev->bounce_buf);
505 return 0;
506}
507
508static 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
514};
515
516
517static int __init ps3rom_init(void)
518{
519 return ps3_system_bus_driver_register(&ps3rom);
520}
521
522static void __exit ps3rom_exit(void)
523{
524 ps3_system_bus_driver_unregister(&ps3rom);
525}
526
527module_init(ps3rom_init);
528module_exit(ps3rom_exit);
529
530MODULE_LICENSE("GPL");
531MODULE_DESCRIPTION("PS3 BD/DVD/CD-ROM Storage Driver");
532MODULE_AUTHOR("Sony Corporation");
533MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_ROM);