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

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

Major reorganization

File size: 15.6 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 <scsi.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(2,"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(2,"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 do {
105 /* update sector count (might have been updated due to S/G limitations) */
106 SET_CDB_32(cdb.trans_len, count);
107
108 /* update S/G count and index */
109 sg_indx = ata_get_sg_indx(io);
110 sg_cnt = io->cSGList - sg_indx;
111
112 /* issue command */
113 rc = ata_cmd(ai, p, d, slot, ATA_CMD_PACKET,
114 AP_ATAPI_CMD, (void *) &cdb, sizeof(cdb),
115 AP_SGLIST, pSGList + sg_indx, sg_cnt,
116 AP_DEVICE, 0x40,
117 AP_FEATURES, ATAPI_FEAT_DMA | ATAPI_FEAT_DMA_TO_HOST,
118 AP_END);
119
120 if (rc > 0)
121 {
122 /* couldn't map all S/G elements */
123 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
124 }
125 } while (rc > 0 && sg_cnt > 0);
126
127 if (rc == 0)
128 {
129 add_workspace(pIorb)->blocks = count;
130 add_workspace(pIorb)->ppfunc = ata_read_pp;
131 }
132 else if (rc > 0)
133 {
134 iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
135 }
136 else if (rc == ATA_CMD_UNALIGNED_ADDR)
137 {
138 /* unaligned S/G addresses detected; need to use double buffers */
139 add_workspace(pIorb)->unaligned = 1;
140 return(atapi_read_unaligned(pIorb, slot));
141 }
142 else
143 {
144 iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
145 }
146
147 return(rc);
148}
149
150/******************************************************************************
151 * Read sectors from AHCI device with unaligned S/G element addresses. AHCI
152 * only allows aligned S/G addresses while OS/2 doesn't have these kind of
153 * restrictions. This doesn't happen very often but when it does, we need to
154 * use a transfer buffer and copy the data manually.
155 */
156int atapi_read_unaligned(IORBH *pIorb, int slot)
157{
158 IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
159 ADD_WORKSPACE *aws = add_workspace(pIorb);
160 ATAPI_CDB_12 cdb;
161 AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
162 int p = iorb_unit_port(pIorb);
163 int d = iorb_unit_device(pIorb);
164 int rc;
165
166 /* translate read command to SCSI/ATAPI READ12 command.
167 * READ12 seems to be the most supported READ variant - according to MMC,
168 * and it's enough even for BluRay.
169 */
170 memset(&cdb, 0x00, sizeof(cdb));
171 cdb.cmd = ATAPI_CMD_READ_12;
172 SET_CDB_32(cdb.lba, io->RBA + io->BlocksXferred);
173 SET_CDB_32(cdb.trans_len, 1UL);
174
175 /* allocate transfer buffer */
176 if ((aws->buf = MemAlloc(io->BlockSize)) == NULL)
177 {
178 iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
179 return(-1);
180 }
181
182 rc = ata_cmd(ai, p, d, slot, ATA_CMD_PACKET,
183 AP_ATAPI_CMD, (void *) &cdb, sizeof(cdb),
184 AP_VADDR, (void *) aws->buf, io->BlockSize,
185 AP_DEVICE, 0x40,
186 AP_FEATURES, ATAPI_FEAT_DMA | ATAPI_FEAT_DMA_TO_HOST,
187 AP_END);
188
189 if (rc == 0)
190 {
191 add_workspace(pIorb)->blocks = 1;
192 add_workspace(pIorb)->ppfunc = ata_read_pp;
193
194 }
195 else if (rc > 0)
196 {
197 iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
198
199 }
200 else
201 {
202 iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
203 }
204
205 return(rc);
206}
207
208/******************************************************************************
209 * Verify readability of sectors on AHCI device. This function is not expected
210 * to be called.
211 */
212int atapi_verify(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
213{
214 DPRINTF(3,"atapi_verify called\n");
215 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
216 return(-1);
217}
218
219/******************************************************************************
220 * Write sectors to AHCI device. This function is not expected to be called.
221 */
222int atapi_write(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
223{
224 DPRINTF(3,"atapi_write called\n");
225 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
226 return(-1);
227}
228
229/******************************************************************************
230 * Execute ATAPI command.
231 */
232int atapi_execute_cdb(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
233{
234 IORB_ADAPTER_PASSTHRU *pt = (IORB_ADAPTER_PASSTHRU *)pIorb;
235 SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(pt->pSGList);
236 int rc;
237 u8 cdb[ATAPI_MAX_CDB_LEN];
238 u16 cdb_len;
239
240 if (pt->ControllerCmdLen > ATAPI_MAX_CDB_LEN)
241 {
242 iorb_seterr(pIorb, IOERR_CMD_SYNTAX);
243 return -1;
244 }
245 /* AHCI requires 12 or 16 byte commands */
246 atapi_pad_cdb(Far16ToFlat(pt->pControllerCmd), pt->ControllerCmdLen,
247 (u8 *) cdb, (u16 *) &cdb_len);
248
249 if (cdb[0] == 0x12 || cdb[0] == 0x5a)
250 {
251 /* somebody sets the direction flag incorrectly for those commands */
252 pt->Flags |= PT_DIRECTION_IN;
253 }
254
255 /* we do not perform the S/G limitation recovery loop here:
256 * "ADDs are not required to iterate commands through the CDB PassThru
257 * mechanism:" -- Storage Device Driver Reference, Scatter/Gather Lists
258 */
259 rc = ata_cmd(ad_infos + iorb_unit_adapter(pIorb), iorb_unit_port(pIorb),
260 iorb_unit_device(pIorb), slot, ATA_CMD_PACKET,
261 AP_ATAPI_CMD, (void *)cdb, cdb_len,
262 AP_SGLIST, pSGList, pt->cSGList,
263 AP_WRITE, !(pt->Flags & PT_DIRECTION_IN),
264 AP_FEATURES, ATAPI_FEAT_DMA,
265 AP_FEATURES, (pt->Flags & PT_DIRECTION_IN) ? ATAPI_FEAT_DMA_TO_HOST : 0,
266 AP_END);
267
268 if (rc)
269 {
270 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
271 }
272
273 return(rc);
274}
275
276
277/******************************************************************************
278 * Request sense information for a failed command.
279 *
280 * NOTE: This function must be called right after an ATAPI command has failed
281 * and before any other commands are queued on the corresponding device.
282 * This function is typically called in the port restart context hook
283 * which is triggered by an AHCI error interrupt.
284 *
285 */
286int atapi_req_sense(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
287{
288 SCSI_STATUS_BLOCK *ssb;
289 ADD_WORKSPACE *aws = add_workspace(pIorb);
290 int rc;
291 u8 cdb[ATAPI_MIN_CDB_LEN];
292 ATAPI_CDB_6 *pcdb = (ATAPI_CDB_6 *) cdb;
293 size_t sense_buf_len = ATAPI_SENSE_LEN;
294
295 DPRINTF(2,"atapi_req_sense\n");
296
297 if ((pIorb->RequestControl & IORB_REQ_STATUSBLOCK) &&
298 pIorb->StatusBlockLen >= sizeof(*ssb) && pIorb->pStatusBlock != 0)
299 {
300 ULONG ulTmp;
301
302 ulTmp = (CastFar16ToULONG(vIorb) & 0xffff0000) + pIorb->pStatusBlock;
303 ssb = (SCSI_STATUS_BLOCK *)Far16ToFlat(CastULONGToFar16(ulTmp));
304
305 /* don't request sense data if caller asked us not to; the flag
306 * STATUS_DISABLE_REQEST_SENSE is not defined in the old DDK we've been
307 * using so we'll use the hard-coded value (0x0008) */
308 if (ssb->Flags & 0x0008U)
309 {
310 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
311 return(-1);
312 }
313
314 /* if the sense buffer requested is larger than our default, adjust
315 * the length accordingly to satisfy the caller's requirements. */
316 if (ssb->SenseData != NULL && ssb->ReqSenseLen > sense_buf_len)
317 {
318 sense_buf_len = ssb->ReqSenseLen;
319 }
320 }
321
322 /* allocate sense buffer in ADD workspace */
323 if ((aws->buf = MemAlloc(sense_buf_len)) == NULL)
324 {
325 iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
326 return(-1);
327 }
328 memset(aws->buf, 0x00, sense_buf_len);
329
330 /* prepare request sense command */
331 memset(cdb, 0x00, sizeof(cdb));
332 pcdb->cmd = ATAPI_CMD_REQUEST_SENSE;
333 pcdb->trans_len = (u8) sense_buf_len;
334
335 aws->ppfunc = atapi_req_sense_pp;
336 rc = ata_cmd(ad_infos + iorb_unit_adapter(pIorb),
337 iorb_unit_port(pIorb),
338 iorb_unit_device(pIorb),
339 slot,
340 ATA_CMD_PACKET,
341 AP_ATAPI_CMD, (void *)cdb, sizeof(cdb),
342 AP_VADDR, (void *)aws->buf, sense_buf_len,
343 AP_FEATURES, ATAPI_FEAT_DMA,
344 AP_END);
345
346 if (rc > 0)
347 {
348 iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
349 }
350 else if (rc < 0)
351 {
352 /* we failed to get info about an error -> return
353 * non specific device error
354 */
355 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
356 }
357
358 return(rc);
359}
360
361/******************************************************************************
362 * Post processing function for ATAPI request sense; examines the sense
363 * data returned and maps sense info to IORB error info.
364 */
365static void atapi_req_sense_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb)
366{
367 SCSI_STATUS_BLOCK *ssb;
368 ADD_WORKSPACE *aws = add_workspace(pIorb);
369 ATAPI_SENSE_DATA *psd = (ATAPI_SENSE_DATA *) aws->buf;
370
371 DHEXDUMP(0,psd, sizeof(*psd), "sense buffer:\n");
372
373 if ((pIorb->RequestControl & IORB_REQ_STATUSBLOCK) &&
374 pIorb->StatusBlockLen >= sizeof(*ssb) && pIorb->pStatusBlock != 0)
375 {
376 ULONG ulTmp;
377
378 /* copy sense data to IORB */
379 ulTmp = (CastFar16ToULONG(vIorb) & 0xffff0000) + pIorb->pStatusBlock;
380 ssb = (SCSI_STATUS_BLOCK *)Far16ToFlat(CastULONGToFar16(ulTmp));
381 ssb->AdapterErrorCode = 0;
382 ssb->TargetStatus = SCSI_STAT_CHECKCOND;
383 ssb->ResidualLength = 0;
384 memset(ssb->AdapterDiagInfo, 0x00, sizeof(ssb->AdapterDiagInfo));
385
386 if (ssb->SenseData != NULL)
387 {
388 memcpy(ssb->SenseData, psd, ssb->ReqSenseLen);
389 ssb->Flags |= STATUS_SENSEDATA_VALID;
390 }
391 pIorb->Status |= IORB_STATUSBLOCK_AVAIL;
392 }
393
394 /* map sense data to some IOERR_ value */
395 switch (ATAPI_GET_SENSE(psd))
396 {
397 case ASENSE_NO_SENSE:
398 case ASENSE_RECOVERED_ERROR:
399 /* no error; this shouldn't happen because we'll only call
400 * atapi_req_sense() if we received an error interrupt */
401 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
402 break;
403
404 case ASENSE_NOT_READY:
405 iorb_seterr(pIorb, IOERR_UNIT_NOT_READY);
406 break;
407
408 case ASENSE_UNIT_ATTENTION:
409 iorb_seterr(pIorb, IOERR_MEDIA_CHANGED);
410 break;
411
412 case ASENSE_MEDIUM_ERROR:
413 iorb_seterr(pIorb, IOERR_MEDIA);
414 break;
415
416 case ASENSE_ILLEGAL_REQUEST:
417 iorb_seterr(pIorb, IOERR_CMD_SYNTAX);
418 break;
419
420 case ASENSE_DATA_PROTECT:
421 iorb_seterr(pIorb, IOERR_MEDIA_WRITE_PROTECT);
422 break;
423
424 case ASENSE_BLANK_CHECK:
425 iorb_seterr(pIorb, IOERR_MEDIA_NOT_FORMATTED);
426 break;
427
428 case ASENSE_ABORTED_COMMAND:
429 case ASENSE_COPY_ABORTED:
430 iorb_seterr(pIorb, IOERR_CMD_ABORTED);
431 break;
432
433 default:
434 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
435 break;
436 }
437
438 /* mark IORB as complete */
439 aws->complete = 1;
440}
441
442/******************************************************************************
443 * Pad ATAPI commands; AHCI requires ATAPI commands to be either 12 or
444 * 16 bytes in length. This func converts commands that have a 12 byte
445 * equivalent, and pads the others to 12 bytes.
446 * cmd_out buffer is expected to be ATAPI_MAX_CDB_LEN in size.
447 * returns 0 on success, != 0 if the command can't be converted.
448 */
449int atapi_pad_cdb(u8 *cmd_in, u16 cmd_in_len, u8 *cmd_out, u16 *cmd_out_len)
450{
451 ATAPI_CDB_12 *p12;
452 u32 tmp;
453
454 if (cmd_in_len == ATAPI_MIN_CDB_LEN || cmd_in_len == ATAPI_MAX_CDB_LEN)
455 {
456 /* command does not need to be converted */
457 memcpy(cmd_out, cmd_in, cmd_in_len);
458 *cmd_out_len = cmd_in_len;
459 return 0;
460 }
461
462 memset(cmd_out, 0x00, ATAPI_MAX_CDB_LEN);
463 p12 = (ATAPI_CDB_12 *) cmd_out;
464 /* we always convert to 12 byte CDBs */
465 *cmd_out_len = ATAPI_MIN_CDB_LEN;
466
467 /* check if command can be converted */
468 switch (cmd_in[0])
469 {
470 case ATAPI_CMD_READ_6:
471 case ATAPI_CMD_WRITE_6:
472 /* convert from 6 to 12 byte equivalent */
473 p12->cmd = 0xa0 | (cmd_in[0] & 0x0f);
474 p12->flags = cmd_in[1] & 0xc0; /* 6byte cmds have no flags (FUA etc.) */
475 tmp = GET_CDB_24(cmd_in + 1) & 0x1fffffUL;
476 SET_CDB_32(p12->lba, tmp);
477 SET_CDB_32(p12->trans_len, (cmd_in[4]));
478 p12->control = cmd_in[5];
479 break;
480
481 case ATAPI_CMD_READ_10:
482 case ATAPI_CMD_WRITE_10:
483 /* convert from 10 byte to 12 byte equivalent */
484 p12->cmd = 0xa0 | (cmd_in[0] & 0x0f);
485 p12->flags = cmd_in[1];
486 p12->control = cmd_in[9];
487 memcpy(p12->lba, cmd_in + 2, 4);
488 tmp = GET_CDB_16(cmd_in + 7);
489 SET_CDB_32(p12->trans_len, tmp);
490 break;
491
492 default:
493 /* pad with zeroes to 12 bytes */
494 memset(cmd_out, 0x00, ATAPI_MIN_CDB_LEN);
495 memcpy(cmd_out, cmd_in, cmd_in_len);
496 break;
497 }
498
499 return 0;
500}
501
Note: See TracBrowser for help on using the repository browser.