source: trunk/src/os2ahci/atapi.c@ 182

Last change on this file since 182 was 181, checked in by David Azarewicz, 9 years ago

Debugging changes

File size: 15.7 KB
Line 
1/******************************************************************************
2 * atapi.c - ATAPI command processing
3 *
4 * Copyright (c) 2011 thi.guten Software Development
5 * Copyright (c) 2011 Mensys B.V.
6 * Copyright (c) 2013-2016 David Azarewicz
7 *
8 * Authors: Christian Mueller, Markus Thielen
9 *
10 * Parts copied from/inspired by the Linux AHCI driver;
11 * those parts are (c) Linux AHCI/ATA maintainers
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include "os2ahci.h"
29#include "ata.h"
30#include "atapi.h"
31
32/* need this for the SCSI status block */
33#include <Dev32scsi.h>
34
35/* -------------------------- macros and constants ------------------------- */
36
37/* ------------------------ typedefs and structures ------------------------ */
38
39/* -------------------------- function prototypes -------------------------- */
40
41static void atapi_req_sense_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb);
42static int atapi_pad_cdb(u8 *cmd_in, u16 cmd_in_len, u8 *cmd_out, u16 *cmd_out_len);
43
44/* ------------------------ global/static variables ------------------------ */
45
46/* ----------------------------- start of code ----------------------------- */
47
48/******************************************************************************
49 * Get device or media geometry. This function is not expected to be called.
50 */
51int atapi_get_geometry(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
52{
53 DPRINTF(4,"atapi_get_geometry called\n");
54 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
55 return(-1);
56}
57
58/******************************************************************************
59 * Test whether unit is ready. This function is not expected to be called.
60 */
61int atapi_unit_ready(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
62{
63 DPRINTF(4,"atapi_unit_ready called\n");
64 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
65 return(-1);
66}
67
68/******************************************************************************
69 * Read sectors from AHCI device.
70 */
71int atapi_read(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
72{
73 IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
74 SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
75 ATAPI_CDB_12 cdb;
76 AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
77 USHORT count = io->BlockCount - io->BlocksXferred;
78 USHORT sg_indx;
79 USHORT sg_cnt;
80 int p = iorb_unit_port(pIorb);
81 int d = iorb_unit_device(pIorb);
82 int rc;
83
84 if (io->BlockCount == 0)
85 {
86 /* NOP; return -1 without error in IORB to indicate success */
87 return(-1);
88 }
89
90 if (add_workspace(pIorb)->unaligned)
91 {
92 /* unaligned S/G addresses present; need to use double buffers */
93 return(atapi_read_unaligned(pIorb, slot));
94 }
95
96 /* translate read command to SCSI/ATAPI READ12 command.
97 * READ12 seems to be the most supported READ variant - according to MMC,
98 * and it's enough even for BluRay.
99 */
100 memset(&cdb, 0x00, sizeof(cdb));
101 cdb.cmd = ATAPI_CMD_READ_12;
102 SET_CDB_32(cdb.lba, io->RBA + io->BlocksXferred);
103
104 DPRINTF(4, "atapi_read\n");
105
106 do {
107 /* update sector count (might have been updated due to S/G limitations) */
108 SET_CDB_32(cdb.trans_len, count);
109
110 /* update S/G count and index */
111 sg_indx = ata_get_sg_indx(io);
112 sg_cnt = io->cSGList - sg_indx;
113
114 /* issue command */
115 rc = ata_cmd(ai, p, d, slot, ATA_CMD_PACKET,
116 AP_ATAPI_CMD, (void *) &cdb, sizeof(cdb),
117 AP_SGLIST, pSGList + sg_indx, sg_cnt,
118 AP_DEVICE, 0x40,
119 AP_FEATURES, ATAPI_FEAT_DMA | ATAPI_FEAT_DMA_TO_HOST,
120 AP_END);
121
122 if (rc > 0)
123 {
124 /* couldn't map all S/G elements */
125 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
126 }
127 } while (rc > 0 && sg_cnt > 0);
128
129 if (rc == 0)
130 {
131 add_workspace(pIorb)->blocks = count;
132 add_workspace(pIorb)->ppfunc = ata_read_pp;
133 }
134 else if (rc > 0)
135 {
136 iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
137 }
138 else if (rc == ATA_CMD_UNALIGNED_ADDR)
139 {
140 /* unaligned S/G addresses detected; need to use double buffers */
141 add_workspace(pIorb)->unaligned = 1;
142 return(atapi_read_unaligned(pIorb, slot));
143 }
144 else
145 {
146 iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
147 }
148
149 return(rc);
150}
151
152/******************************************************************************
153 * Read sectors from AHCI device with unaligned S/G element addresses. AHCI
154 * only allows aligned S/G addresses while OS/2 doesn't have these kind of
155 * restrictions. This doesn't happen very often but when it does, we need to
156 * use a transfer buffer and copy the data manually.
157 */
158int atapi_read_unaligned(IORBH *pIorb, int slot)
159{
160 IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
161 ADD_WORKSPACE *aws = add_workspace(pIorb);
162 ATAPI_CDB_12 cdb;
163 AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
164 int p = iorb_unit_port(pIorb);
165 int d = iorb_unit_device(pIorb);
166 int rc;
167
168 /* translate read command to SCSI/ATAPI READ12 command.
169 * READ12 seems to be the most supported READ variant - according to MMC,
170 * and it's enough even for BluRay.
171 */
172 memset(&cdb, 0x00, sizeof(cdb));
173 cdb.cmd = ATAPI_CMD_READ_12;
174 SET_CDB_32(cdb.lba, io->RBA + io->BlocksXferred);
175 SET_CDB_32(cdb.trans_len, 1UL);
176
177 DPRINTF(4, "atapi_read_unaligned\n");
178
179 /* allocate transfer buffer */
180 if ((aws->buf = MemAlloc(io->BlockSize)) == NULL)
181 {
182 iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
183 return(-1);
184 }
185
186 rc = ata_cmd(ai, p, d, slot, ATA_CMD_PACKET,
187 AP_ATAPI_CMD, (void *) &cdb, sizeof(cdb),
188 AP_VADDR, (void *) aws->buf, io->BlockSize,
189 AP_DEVICE, 0x40,
190 AP_FEATURES, ATAPI_FEAT_DMA | ATAPI_FEAT_DMA_TO_HOST,
191 AP_END);
192
193 if (rc == 0)
194 {
195 add_workspace(pIorb)->blocks = 1;
196 add_workspace(pIorb)->ppfunc = ata_read_pp;
197
198 }
199 else if (rc > 0)
200 {
201 iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
202
203 }
204 else
205 {
206 iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
207 }
208
209 return(rc);
210}
211
212/******************************************************************************
213 * Verify readability of sectors on AHCI device. This function is not expected
214 * to be called.
215 */
216int atapi_verify(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
217{
218 DPRINTF(4,"atapi_verify called\n");
219 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
220 return(-1);
221}
222
223/******************************************************************************
224 * Write sectors to AHCI device. This function is not expected to be called.
225 */
226int atapi_write(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
227{
228 DPRINTF(4,"atapi_write called\n");
229 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
230 return(-1);
231}
232
233/******************************************************************************
234 * Execute ATAPI command.
235 */
236int atapi_execute_cdb(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
237{
238 IORB_ADAPTER_PASSTHRU *pt = (IORB_ADAPTER_PASSTHRU *)pIorb;
239 SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(pt->pSGList);
240 int rc;
241 u8 cdb[ATAPI_MAX_CDB_LEN];
242 u16 cdb_len;
243
244 if (pt->ControllerCmdLen > ATAPI_MAX_CDB_LEN)
245 {
246 iorb_seterr(pIorb, IOERR_CMD_SYNTAX);
247 return -1;
248 }
249 /* AHCI requires 12 or 16 byte commands */
250 atapi_pad_cdb(Far16ToFlat(pt->pControllerCmd), pt->ControllerCmdLen, cdb, &cdb_len);
251
252 if (cdb[0] == 0x12 || cdb[0] == 0x5a)
253 {
254 /* somebody sets the direction flag incorrectly for those commands */
255 pt->Flags |= PT_DIRECTION_IN;
256 }
257
258 /* we do not perform the S/G limitation recovery loop here:
259 * "ADDs are not required to iterate commands through the CDB PassThru
260 * mechanism:" -- Storage Device Driver Reference, Scatter/Gather Lists
261 */
262 rc = ata_cmd(ad_infos + iorb_unit_adapter(pIorb), iorb_unit_port(pIorb),
263 iorb_unit_device(pIorb), slot, ATA_CMD_PACKET,
264 AP_ATAPI_CMD, (void *)cdb, cdb_len,
265 AP_SGLIST, pSGList, pt->cSGList,
266 AP_WRITE, !(pt->Flags & PT_DIRECTION_IN),
267 AP_FEATURES, ATAPI_FEAT_DMA,
268 AP_FEATURES, (pt->Flags & PT_DIRECTION_IN) ? ATAPI_FEAT_DMA_TO_HOST : 0,
269 AP_END);
270
271 if (rc)
272 {
273 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
274 }
275
276 return(rc);
277}
278
279
280/******************************************************************************
281 * Request sense information for a failed command.
282 *
283 * NOTE: This function must be called right after an ATAPI command has failed
284 * and before any other commands are queued on the corresponding device.
285 * This function is typically called in the port restart context hook
286 * which is triggered by an AHCI error interrupt.
287 *
288 */
289int atapi_req_sense(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
290{
291 SCSI_STATUS_BLOCK *ssb;
292 ADD_WORKSPACE *aws = add_workspace(pIorb);
293 int rc;
294 u8 cdb[ATAPI_MIN_CDB_LEN];
295 ATAPI_CDB_6 *pcdb = (ATAPI_CDB_6 *) cdb;
296 size_t sense_buf_len = ATAPI_SENSE_LEN;
297
298 DPRINTF(4,"atapi_req_sense\n");
299
300 if ((pIorb->RequestControl & IORB_REQ_STATUSBLOCK) &&
301 pIorb->StatusBlockLen >= sizeof(*ssb) && pIorb->pStatusBlock != 0)
302 {
303 ULONG ulTmp;
304
305 ulTmp = (CastFar16ToULONG(vIorb) & 0xffff0000) + pIorb->pStatusBlock;
306 ssb = (SCSI_STATUS_BLOCK *)Far16ToFlat(CastULONGToFar16(ulTmp));
307
308 /* don't request sense data if caller asked us not to; the flag
309 * STATUS_DISABLE_REQEST_SENSE is not defined in the old DDK we've been
310 * using so we'll use the hard-coded value (0x0008) */
311 if (ssb->Flags & 0x0008U)
312 {
313 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
314 return(-1);
315 }
316
317 /* if the sense buffer requested is larger than our default, adjust
318 * the length accordingly to satisfy the caller's requirements. */
319 if (ssb->SenseData != NULL && ssb->ReqSenseLen > sense_buf_len)
320 {
321 sense_buf_len = ssb->ReqSenseLen;
322 }
323 }
324
325 /* allocate sense buffer in ADD workspace */
326 if ((aws->buf = MemAlloc(sense_buf_len)) == NULL)
327 {
328 iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
329 return(-1);
330 }
331 memset(aws->buf, 0x00, sense_buf_len);
332
333 /* prepare request sense command */
334 memset(cdb, 0x00, sizeof(cdb));
335 pcdb->cmd = ATAPI_CMD_REQUEST_SENSE;
336 pcdb->trans_len = (u8) sense_buf_len;
337
338 aws->ppfunc = atapi_req_sense_pp;
339 rc = ata_cmd(ad_infos + iorb_unit_adapter(pIorb),
340 iorb_unit_port(pIorb),
341 iorb_unit_device(pIorb),
342 slot,
343 ATA_CMD_PACKET,
344 AP_ATAPI_CMD, (void *)cdb, sizeof(cdb),
345 AP_VADDR, (void *)aws->buf, sense_buf_len,
346 AP_FEATURES, ATAPI_FEAT_DMA,
347 AP_END);
348
349 if (rc > 0)
350 {
351 iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
352 }
353 else if (rc < 0)
354 {
355 /* we failed to get info about an error -> return
356 * non specific device error
357 */
358 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
359 }
360
361 return(rc);
362}
363
364/******************************************************************************
365 * Post processing function for ATAPI request sense; examines the sense
366 * data returned and maps sense info to IORB error info.
367 */
368static void atapi_req_sense_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb)
369{
370 SCSI_STATUS_BLOCK *ssb;
371 ADD_WORKSPACE *aws = add_workspace(pIorb);
372 ATAPI_SENSE_DATA *psd = (ATAPI_SENSE_DATA *) aws->buf;
373
374 DHEXDUMP(0,psd, sizeof(*psd), "sense buffer:\n");
375
376 if ((pIorb->RequestControl & IORB_REQ_STATUSBLOCK) &&
377 pIorb->StatusBlockLen >= sizeof(*ssb) && pIorb->pStatusBlock != 0)
378 {
379 ULONG ulTmp;
380
381 /* copy sense data to IORB */
382 ulTmp = (CastFar16ToULONG(vIorb) & 0xffff0000) + pIorb->pStatusBlock;
383 ssb = (SCSI_STATUS_BLOCK *)Far16ToFlat(CastULONGToFar16(ulTmp));
384 ssb->AdapterErrorCode = 0;
385 ssb->TargetStatus = SCSI_STAT_CHECKCOND;
386 ssb->ResidualLength = 0;
387 memset(ssb->AdapterDiagInfo, 0x00, sizeof(ssb->AdapterDiagInfo));
388
389 if (ssb->SenseData != NULL)
390 {
391 memcpy(Far16ToFlat(ssb->SenseData), psd, ssb->ReqSenseLen);
392 ssb->Flags |= STATUS_SENSEDATA_VALID;
393 }
394 pIorb->Status |= IORB_STATUSBLOCK_AVAIL;
395 }
396
397 /* map sense data to some IOERR_ value */
398 switch (ATAPI_GET_SENSE(psd))
399 {
400 case ASENSE_NO_SENSE:
401 case ASENSE_RECOVERED_ERROR:
402 /* no error; this shouldn't happen because we'll only call
403 * atapi_req_sense() if we received an error interrupt */
404 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
405 break;
406
407 case ASENSE_NOT_READY:
408 iorb_seterr(pIorb, IOERR_UNIT_NOT_READY);
409 break;
410
411 case ASENSE_UNIT_ATTENTION:
412 iorb_seterr(pIorb, IOERR_MEDIA_CHANGED);
413 break;
414
415 case ASENSE_MEDIUM_ERROR:
416 iorb_seterr(pIorb, IOERR_MEDIA);
417 break;
418
419 case ASENSE_ILLEGAL_REQUEST:
420 iorb_seterr(pIorb, IOERR_CMD_SYNTAX);
421 break;
422
423 case ASENSE_DATA_PROTECT:
424 iorb_seterr(pIorb, IOERR_MEDIA_WRITE_PROTECT);
425 break;
426
427 case ASENSE_BLANK_CHECK:
428 iorb_seterr(pIorb, IOERR_MEDIA_NOT_FORMATTED);
429 break;
430
431 case ASENSE_ABORTED_COMMAND:
432 case ASENSE_COPY_ABORTED:
433 iorb_seterr(pIorb, IOERR_CMD_ABORTED);
434 break;
435
436 default:
437 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
438 break;
439 }
440
441 /* mark IORB as complete */
442 aws->complete = 1;
443}
444
445/******************************************************************************
446 * Pad ATAPI commands; AHCI requires ATAPI commands to be either 12 or
447 * 16 bytes in length. This func converts commands that have a 12 byte
448 * equivalent, and pads the others to 12 bytes.
449 * cmd_out buffer is expected to be ATAPI_MAX_CDB_LEN in size.
450 * returns 0 on success, != 0 if the command can't be converted.
451 */
452int atapi_pad_cdb(u8 *cmd_in, u16 cmd_in_len, u8 *cmd_out, u16 *cmd_out_len)
453{
454 ATAPI_CDB_12 *p12;
455 u32 tmp;
456
457 if (cmd_in_len == ATAPI_MIN_CDB_LEN || cmd_in_len == ATAPI_MAX_CDB_LEN)
458 {
459 /* command does not need to be converted */
460 memcpy(cmd_out, cmd_in, cmd_in_len);
461 *cmd_out_len = cmd_in_len;
462 return 0;
463 }
464
465 memset(cmd_out, 0x00, ATAPI_MAX_CDB_LEN);
466 p12 = (ATAPI_CDB_12 *) cmd_out;
467 /* we always convert to 12 byte CDBs */
468 *cmd_out_len = ATAPI_MIN_CDB_LEN;
469
470 /* check if command can be converted */
471 switch (cmd_in[0])
472 {
473 case ATAPI_CMD_READ_6:
474 case ATAPI_CMD_WRITE_6:
475 /* convert from 6 to 12 byte equivalent */
476 p12->cmd = 0xa0 | (cmd_in[0] & 0x0f);
477 p12->flags = cmd_in[1] & 0xc0; /* 6byte cmds have no flags (FUA etc.) */
478 tmp = GET_CDB_24(cmd_in + 1) & 0x1fffffUL;
479 SET_CDB_32(p12->lba, tmp);
480 SET_CDB_32(p12->trans_len, (cmd_in[4]));
481 p12->control = cmd_in[5];
482 break;
483
484 case ATAPI_CMD_READ_10:
485 case ATAPI_CMD_WRITE_10:
486 /* convert from 10 byte to 12 byte equivalent */
487 p12->cmd = 0xa0 | (cmd_in[0] & 0x0f);
488 p12->flags = cmd_in[1];
489 p12->control = cmd_in[9];
490 memcpy(p12->lba, cmd_in + 2, 4);
491 tmp = GET_CDB_16(cmd_in + 7);
492 SET_CDB_32(p12->trans_len, tmp);
493 break;
494
495 default:
496 /* pad with zeroes to 12 bytes */
497 memset(cmd_out, 0x00, ATAPI_MIN_CDB_LEN);
498 memcpy(cmd_out, cmd_in, cmd_in_len);
499 break;
500 }
501
502 return 0;
503}
504
Note: See TracBrowser for help on using the repository browser.