libata-scsi.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454
  1. /*
  2. * libata-scsi.c - helper library for ATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from
  31. * - http://www.t10.org/
  32. * - http://www.t13.org/
  33. *
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/spinlock.h>
  38. #include <scsi/scsi.h>
  39. #include "scsi.h"
  40. #include <scsi/scsi_host.h>
  41. #include <linux/libata.h>
  42. #include <linux/hdreg.h>
  43. #include <asm/uaccess.h>
  44. #include "libata.h"
  45. #define SECTOR_SIZE 512
  46. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
  47. static struct ata_device *
  48. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
  49. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
  50. void (*done)(struct scsi_cmnd *))
  51. {
  52. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  53. /* "Invalid field in cbd" */
  54. done(cmd);
  55. }
  56. /**
  57. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  58. * @sdev: SCSI device for which BIOS geometry is to be determined
  59. * @bdev: block device associated with @sdev
  60. * @capacity: capacity of SCSI device
  61. * @geom: location to which geometry will be output
  62. *
  63. * Generic bios head/sector/cylinder calculator
  64. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  65. * mapping. Some situations may arise where the disk is not
  66. * bootable if this is not used.
  67. *
  68. * LOCKING:
  69. * Defined by the SCSI layer. We don't really care.
  70. *
  71. * RETURNS:
  72. * Zero.
  73. */
  74. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  75. sector_t capacity, int geom[])
  76. {
  77. geom[0] = 255;
  78. geom[1] = 63;
  79. sector_div(capacity, 255*63);
  80. geom[2] = capacity;
  81. return 0;
  82. }
  83. /**
  84. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  85. * @dev: Device to whom we are issuing command
  86. * @arg: User provided data for issuing command
  87. *
  88. * LOCKING:
  89. * Defined by the SCSI layer. We don't really care.
  90. *
  91. * RETURNS:
  92. * Zero on success, negative errno on error.
  93. */
  94. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  95. {
  96. int rc = 0;
  97. u8 scsi_cmd[MAX_COMMAND_SIZE];
  98. u8 args[4], *argbuf = NULL;
  99. int argsize = 0;
  100. struct scsi_request *sreq;
  101. if (NULL == (void *)arg)
  102. return -EINVAL;
  103. if (copy_from_user(args, arg, sizeof(args)))
  104. return -EFAULT;
  105. sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
  106. if (!sreq)
  107. return -EINTR;
  108. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  109. if (args[3]) {
  110. argsize = SECTOR_SIZE * args[3];
  111. argbuf = kmalloc(argsize, GFP_KERNEL);
  112. if (argbuf == NULL) {
  113. rc = -ENOMEM;
  114. goto error;
  115. }
  116. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  117. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  118. block count in sector count field */
  119. sreq->sr_data_direction = DMA_FROM_DEVICE;
  120. } else {
  121. scsi_cmd[1] = (3 << 1); /* Non-data */
  122. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  123. sreq->sr_data_direction = DMA_NONE;
  124. }
  125. scsi_cmd[0] = ATA_16;
  126. scsi_cmd[4] = args[2];
  127. if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
  128. scsi_cmd[6] = args[3];
  129. scsi_cmd[8] = args[1];
  130. scsi_cmd[10] = 0x4f;
  131. scsi_cmd[12] = 0xc2;
  132. } else {
  133. scsi_cmd[6] = args[1];
  134. }
  135. scsi_cmd[14] = args[0];
  136. /* Good values for timeout and retries? Values below
  137. from scsi_ioctl_send_command() for default case... */
  138. scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
  139. if (sreq->sr_result) {
  140. rc = -EIO;
  141. goto error;
  142. }
  143. /* Need code to retrieve data from check condition? */
  144. if ((argbuf)
  145. && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
  146. rc = -EFAULT;
  147. error:
  148. scsi_release_request(sreq);
  149. if (argbuf)
  150. kfree(argbuf);
  151. return rc;
  152. }
  153. /**
  154. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  155. * @dev: Device to whom we are issuing command
  156. * @arg: User provided data for issuing command
  157. *
  158. * LOCKING:
  159. * Defined by the SCSI layer. We don't really care.
  160. *
  161. * RETURNS:
  162. * Zero on success, negative errno on error.
  163. */
  164. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  165. {
  166. int rc = 0;
  167. u8 scsi_cmd[MAX_COMMAND_SIZE];
  168. u8 args[7];
  169. struct scsi_request *sreq;
  170. if (NULL == (void *)arg)
  171. return -EINVAL;
  172. if (copy_from_user(args, arg, sizeof(args)))
  173. return -EFAULT;
  174. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  175. scsi_cmd[0] = ATA_16;
  176. scsi_cmd[1] = (3 << 1); /* Non-data */
  177. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  178. scsi_cmd[4] = args[1];
  179. scsi_cmd[6] = args[2];
  180. scsi_cmd[8] = args[3];
  181. scsi_cmd[10] = args[4];
  182. scsi_cmd[12] = args[5];
  183. scsi_cmd[14] = args[0];
  184. sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
  185. if (!sreq) {
  186. rc = -EINTR;
  187. goto error;
  188. }
  189. sreq->sr_data_direction = DMA_NONE;
  190. /* Good values for timeout and retries? Values below
  191. from scsi_ioctl_send_command() for default case... */
  192. scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
  193. if (sreq->sr_result) {
  194. rc = -EIO;
  195. goto error;
  196. }
  197. /* Need code to retrieve data from check condition? */
  198. error:
  199. scsi_release_request(sreq);
  200. return rc;
  201. }
  202. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  203. {
  204. struct ata_port *ap;
  205. struct ata_device *dev;
  206. int val = -EINVAL, rc = -EINVAL;
  207. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  208. if (!ap)
  209. goto out;
  210. dev = ata_scsi_find_dev(ap, scsidev);
  211. if (!dev) {
  212. rc = -ENODEV;
  213. goto out;
  214. }
  215. switch (cmd) {
  216. case ATA_IOC_GET_IO32:
  217. val = 0;
  218. if (copy_to_user(arg, &val, 1))
  219. return -EFAULT;
  220. return 0;
  221. case ATA_IOC_SET_IO32:
  222. val = (unsigned long) arg;
  223. if (val != 0)
  224. return -EINVAL;
  225. return 0;
  226. case HDIO_DRIVE_CMD:
  227. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  228. return -EACCES;
  229. return ata_cmd_ioctl(scsidev, arg);
  230. case HDIO_DRIVE_TASK:
  231. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  232. return -EACCES;
  233. return ata_task_ioctl(scsidev, arg);
  234. default:
  235. rc = -ENOTTY;
  236. break;
  237. }
  238. out:
  239. return rc;
  240. }
  241. /**
  242. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  243. * @ap: ATA port to which the new command is attached
  244. * @dev: ATA device to which the new command is attached
  245. * @cmd: SCSI command that originated this ATA command
  246. * @done: SCSI command completion function
  247. *
  248. * Obtain a reference to an unused ata_queued_cmd structure,
  249. * which is the basic libata structure representing a single
  250. * ATA command sent to the hardware.
  251. *
  252. * If a command was available, fill in the SCSI-specific
  253. * portions of the structure with information on the
  254. * current command.
  255. *
  256. * LOCKING:
  257. * spin_lock_irqsave(host_set lock)
  258. *
  259. * RETURNS:
  260. * Command allocated, or %NULL if none available.
  261. */
  262. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
  263. struct ata_device *dev,
  264. struct scsi_cmnd *cmd,
  265. void (*done)(struct scsi_cmnd *))
  266. {
  267. struct ata_queued_cmd *qc;
  268. qc = ata_qc_new_init(ap, dev);
  269. if (qc) {
  270. qc->scsicmd = cmd;
  271. qc->scsidone = done;
  272. if (cmd->use_sg) {
  273. qc->sg = (struct scatterlist *) cmd->request_buffer;
  274. qc->n_elem = cmd->use_sg;
  275. } else {
  276. qc->sg = &qc->sgent;
  277. qc->n_elem = 1;
  278. }
  279. } else {
  280. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  281. done(cmd);
  282. }
  283. return qc;
  284. }
  285. /**
  286. * ata_dump_status - user friendly display of error info
  287. * @id: id of the port in question
  288. * @tf: ptr to filled out taskfile
  289. *
  290. * Decode and dump the ATA error/status registers for the user so
  291. * that they have some idea what really happened at the non
  292. * make-believe layer.
  293. *
  294. * LOCKING:
  295. * inherited from caller
  296. */
  297. void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  298. {
  299. u8 stat = tf->command, err = tf->feature;
  300. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  301. if (stat & ATA_BUSY) {
  302. printk("Busy }\n"); /* Data is not valid in this case */
  303. } else {
  304. if (stat & 0x40) printk("DriveReady ");
  305. if (stat & 0x20) printk("DeviceFault ");
  306. if (stat & 0x10) printk("SeekComplete ");
  307. if (stat & 0x08) printk("DataRequest ");
  308. if (stat & 0x04) printk("CorrectedError ");
  309. if (stat & 0x02) printk("Index ");
  310. if (stat & 0x01) printk("Error ");
  311. printk("}\n");
  312. if (err) {
  313. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  314. if (err & 0x04) printk("DriveStatusError ");
  315. if (err & 0x80) {
  316. if (err & 0x04) printk("BadCRC ");
  317. else printk("Sector ");
  318. }
  319. if (err & 0x40) printk("UncorrectableError ");
  320. if (err & 0x10) printk("SectorIdNotFound ");
  321. if (err & 0x02) printk("TrackZeroNotFound ");
  322. if (err & 0x01) printk("AddrMarkNotFound ");
  323. printk("}\n");
  324. }
  325. }
  326. }
  327. /**
  328. * ata_to_sense_error - convert ATA error to SCSI error
  329. * @drv_stat: value contained in ATA status register
  330. * @drv_err: value contained in ATA error register
  331. * @sk: the sense key we'll fill out
  332. * @asc: the additional sense code we'll fill out
  333. * @ascq: the additional sense code qualifier we'll fill out
  334. *
  335. * Converts an ATA error into a SCSI error. Fill out pointers to
  336. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  337. * format sense blocks.
  338. *
  339. * LOCKING:
  340. * spin_lock_irqsave(host_set lock)
  341. */
  342. void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
  343. u8 *ascq)
  344. {
  345. int i;
  346. /* Based on the 3ware driver translation table */
  347. static unsigned char sense_table[][4] = {
  348. /* BBD|ECC|ID|MAR */
  349. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  350. /* BBD|ECC|ID */
  351. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  352. /* ECC|MC|MARK */
  353. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  354. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  355. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  356. /* MC|ID|ABRT|TRK0|MARK */
  357. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  358. /* MCR|MARK */
  359. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  360. /* Bad address mark */
  361. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  362. /* TRK0 */
  363. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  364. /* Abort & !ICRC */
  365. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  366. /* Media change request */
  367. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  368. /* SRV */
  369. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  370. /* Media change */
  371. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  372. /* ECC */
  373. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  374. /* BBD - block marked bad */
  375. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  376. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  377. };
  378. static unsigned char stat_table[][4] = {
  379. /* Must be first because BUSY means no other bits valid */
  380. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  381. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  382. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  383. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  384. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  385. };
  386. /*
  387. * Is this an error we can process/parse
  388. */
  389. if (drv_stat & ATA_BUSY) {
  390. drv_err = 0; /* Ignore the err bits, they're invalid */
  391. }
  392. if (drv_err) {
  393. /* Look for drv_err */
  394. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  395. /* Look for best matches first */
  396. if ((sense_table[i][0] & drv_err) ==
  397. sense_table[i][0]) {
  398. *sk = sense_table[i][1];
  399. *asc = sense_table[i][2];
  400. *ascq = sense_table[i][3];
  401. goto translate_done;
  402. }
  403. }
  404. /* No immediate match */
  405. printk(KERN_WARNING "ata%u: no sense translation for "
  406. "error 0x%02x\n", id, drv_err);
  407. }
  408. /* Fall back to interpreting status bits */
  409. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  410. if (stat_table[i][0] & drv_stat) {
  411. *sk = stat_table[i][1];
  412. *asc = stat_table[i][2];
  413. *ascq = stat_table[i][3];
  414. goto translate_done;
  415. }
  416. }
  417. /* No error? Undecoded? */
  418. printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
  419. id, drv_stat);
  420. /* For our last chance pick, use medium read error because
  421. * it's much more common than an ATA drive telling you a write
  422. * has failed.
  423. */
  424. *sk = MEDIUM_ERROR;
  425. *asc = 0x11; /* "unrecovered read error" */
  426. *ascq = 0x04; /* "auto-reallocation failed" */
  427. translate_done:
  428. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
  429. "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
  430. *sk, *asc, *ascq);
  431. return;
  432. }
  433. /*
  434. * ata_gen_ata_desc_sense - Generate check condition sense block.
  435. * @qc: Command that completed.
  436. *
  437. * This function is specific to the ATA descriptor format sense
  438. * block specified for the ATA pass through commands. Regardless
  439. * of whether the command errored or not, return a sense
  440. * block. Copy all controller registers into the sense
  441. * block. Clear sense key, ASC & ASCQ if there is no error.
  442. *
  443. * LOCKING:
  444. * spin_lock_irqsave(host_set lock)
  445. */
  446. void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
  447. {
  448. struct scsi_cmnd *cmd = qc->scsicmd;
  449. struct ata_taskfile *tf = &qc->tf;
  450. unsigned char *sb = cmd->sense_buffer;
  451. unsigned char *desc = sb + 8;
  452. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  453. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  454. /*
  455. * Read the controller registers.
  456. */
  457. assert(NULL != qc->ap->ops->tf_read);
  458. qc->ap->ops->tf_read(qc->ap, tf);
  459. /*
  460. * Use ata_to_sense_error() to map status register bits
  461. * onto sense key, asc & ascq.
  462. */
  463. if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
  464. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  465. &sb[1], &sb[2], &sb[3]);
  466. sb[1] &= 0x0f;
  467. }
  468. /*
  469. * Sense data is current and format is descriptor.
  470. */
  471. sb[0] = 0x72;
  472. desc[0] = 0x09;
  473. /*
  474. * Set length of additional sense data.
  475. * Since we only populate descriptor 0, the total
  476. * length is the same (fixed) length as descriptor 0.
  477. */
  478. desc[1] = sb[7] = 14;
  479. /*
  480. * Copy registers into sense buffer.
  481. */
  482. desc[2] = 0x00;
  483. desc[3] = tf->feature; /* == error reg */
  484. desc[5] = tf->nsect;
  485. desc[7] = tf->lbal;
  486. desc[9] = tf->lbam;
  487. desc[11] = tf->lbah;
  488. desc[12] = tf->device;
  489. desc[13] = tf->command; /* == status reg */
  490. /*
  491. * Fill in Extend bit, and the high order bytes
  492. * if applicable.
  493. */
  494. if (tf->flags & ATA_TFLAG_LBA48) {
  495. desc[2] |= 0x01;
  496. desc[4] = tf->hob_nsect;
  497. desc[6] = tf->hob_lbal;
  498. desc[8] = tf->hob_lbam;
  499. desc[10] = tf->hob_lbah;
  500. }
  501. }
  502. /**
  503. * ata_gen_fixed_sense - generate a SCSI fixed sense block
  504. * @qc: Command that we are erroring out
  505. *
  506. * Leverage ata_to_sense_error() to give us the codes. Fit our
  507. * LBA in here if there's room.
  508. *
  509. * LOCKING:
  510. * inherited from caller
  511. */
  512. void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
  513. {
  514. struct scsi_cmnd *cmd = qc->scsicmd;
  515. struct ata_taskfile *tf = &qc->tf;
  516. unsigned char *sb = cmd->sense_buffer;
  517. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  518. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  519. /*
  520. * Read the controller registers.
  521. */
  522. assert(NULL != qc->ap->ops->tf_read);
  523. qc->ap->ops->tf_read(qc->ap, tf);
  524. /*
  525. * Use ata_to_sense_error() to map status register bits
  526. * onto sense key, asc & ascq.
  527. */
  528. if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
  529. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  530. &sb[2], &sb[12], &sb[13]);
  531. sb[2] &= 0x0f;
  532. }
  533. sb[0] = 0x70;
  534. sb[7] = 0x0a;
  535. if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
  536. /* A small (28b) LBA will fit in the 32b info field */
  537. sb[0] |= 0x80; /* set valid bit */
  538. sb[3] = tf->device & 0x0f;
  539. sb[4] = tf->lbah;
  540. sb[5] = tf->lbam;
  541. sb[6] = tf->lbal;
  542. }
  543. }
  544. /**
  545. * ata_scsi_slave_config - Set SCSI device attributes
  546. * @sdev: SCSI device to examine
  547. *
  548. * This is called before we actually start reading
  549. * and writing to the device, to configure certain
  550. * SCSI mid-layer behaviors.
  551. *
  552. * LOCKING:
  553. * Defined by SCSI layer. We don't really care.
  554. */
  555. int ata_scsi_slave_config(struct scsi_device *sdev)
  556. {
  557. sdev->use_10_for_rw = 1;
  558. sdev->use_10_for_ms = 1;
  559. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  560. if (sdev->id < ATA_MAX_DEVICES) {
  561. struct ata_port *ap;
  562. struct ata_device *dev;
  563. ap = (struct ata_port *) &sdev->host->hostdata[0];
  564. dev = &ap->device[sdev->id];
  565. /* TODO: 1024 is an arbitrary number, not the
  566. * hardware maximum. This should be increased to
  567. * 65534 when Jens Axboe's patch for dynamically
  568. * determining max_sectors is merged.
  569. */
  570. if ((dev->flags & ATA_DFLAG_LBA48) &&
  571. ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
  572. /*
  573. * do not overwrite sdev->host->max_sectors, since
  574. * other drives on this host may not support LBA48
  575. */
  576. blk_queue_max_sectors(sdev->request_queue, 2048);
  577. }
  578. }
  579. return 0; /* scsi layer doesn't check return value, sigh */
  580. }
  581. /**
  582. * ata_scsi_error - SCSI layer error handler callback
  583. * @host: SCSI host on which error occurred
  584. *
  585. * Handles SCSI-layer-thrown error events.
  586. *
  587. * LOCKING:
  588. * Inherited from SCSI layer (none, can sleep)
  589. *
  590. * RETURNS:
  591. * Zero.
  592. */
  593. int ata_scsi_error(struct Scsi_Host *host)
  594. {
  595. struct ata_port *ap;
  596. DPRINTK("ENTER\n");
  597. ap = (struct ata_port *) &host->hostdata[0];
  598. ap->ops->eng_timeout(ap);
  599. /* TODO: this is per-command; when queueing is supported
  600. * this code will either change or move to a more
  601. * appropriate place
  602. */
  603. host->host_failed--;
  604. INIT_LIST_HEAD(&host->eh_cmd_q);
  605. DPRINTK("EXIT\n");
  606. return 0;
  607. }
  608. /**
  609. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  610. * @qc: Storage for translated ATA taskfile
  611. * @scsicmd: SCSI command to translate
  612. *
  613. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  614. * (to start). Perhaps these commands should be preceded by
  615. * CHECK POWER MODE to see what power mode the device is already in.
  616. * [See SAT revision 5 at www.t10.org]
  617. *
  618. * LOCKING:
  619. * spin_lock_irqsave(host_set lock)
  620. *
  621. * RETURNS:
  622. * Zero on success, non-zero on error.
  623. */
  624. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
  625. u8 *scsicmd)
  626. {
  627. struct ata_taskfile *tf = &qc->tf;
  628. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  629. tf->protocol = ATA_PROT_NODATA;
  630. if (scsicmd[1] & 0x1) {
  631. ; /* ignore IMMED bit, violates sat-r05 */
  632. }
  633. if (scsicmd[4] & 0x2)
  634. goto invalid_fld; /* LOEJ bit set not supported */
  635. if (((scsicmd[4] >> 4) & 0xf) != 0)
  636. goto invalid_fld; /* power conditions not supported */
  637. if (scsicmd[4] & 0x1) {
  638. tf->nsect = 1; /* 1 sector, lba=0 */
  639. if (qc->dev->flags & ATA_DFLAG_LBA) {
  640. qc->tf.flags |= ATA_TFLAG_LBA;
  641. tf->lbah = 0x0;
  642. tf->lbam = 0x0;
  643. tf->lbal = 0x0;
  644. tf->device |= ATA_LBA;
  645. } else {
  646. /* CHS */
  647. tf->lbal = 0x1; /* sect */
  648. tf->lbam = 0x0; /* cyl low */
  649. tf->lbah = 0x0; /* cyl high */
  650. }
  651. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  652. } else {
  653. tf->nsect = 0; /* time period value (0 implies now) */
  654. tf->command = ATA_CMD_STANDBY;
  655. /* Consider: ATA STANDBY IMMEDIATE command */
  656. }
  657. /*
  658. * Standby and Idle condition timers could be implemented but that
  659. * would require libata to implement the Power condition mode page
  660. * and allow the user to change it. Changing mode pages requires
  661. * MODE SELECT to be implemented.
  662. */
  663. return 0;
  664. invalid_fld:
  665. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  666. /* "Invalid field in cbd" */
  667. return 1;
  668. }
  669. /**
  670. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  671. * @qc: Storage for translated ATA taskfile
  672. * @scsicmd: SCSI command to translate (ignored)
  673. *
  674. * Sets up an ATA taskfile to issue FLUSH CACHE or
  675. * FLUSH CACHE EXT.
  676. *
  677. * LOCKING:
  678. * spin_lock_irqsave(host_set lock)
  679. *
  680. * RETURNS:
  681. * Zero on success, non-zero on error.
  682. */
  683. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  684. {
  685. struct ata_taskfile *tf = &qc->tf;
  686. tf->flags |= ATA_TFLAG_DEVICE;
  687. tf->protocol = ATA_PROT_NODATA;
  688. if ((tf->flags & ATA_TFLAG_LBA48) &&
  689. (ata_id_has_flush_ext(qc->dev->id)))
  690. tf->command = ATA_CMD_FLUSH_EXT;
  691. else
  692. tf->command = ATA_CMD_FLUSH;
  693. return 0;
  694. }
  695. /**
  696. * scsi_6_lba_len - Get LBA and transfer length
  697. * @scsicmd: SCSI command to translate
  698. *
  699. * Calculate LBA and transfer length for 6-byte commands.
  700. *
  701. * RETURNS:
  702. * @plba: the LBA
  703. * @plen: the transfer length
  704. */
  705. static void scsi_6_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
  706. {
  707. u64 lba = 0;
  708. u32 len = 0;
  709. VPRINTK("six-byte command\n");
  710. lba |= ((u64)scsicmd[2]) << 8;
  711. lba |= ((u64)scsicmd[3]);
  712. len |= ((u32)scsicmd[4]);
  713. *plba = lba;
  714. *plen = len;
  715. }
  716. /**
  717. * scsi_10_lba_len - Get LBA and transfer length
  718. * @scsicmd: SCSI command to translate
  719. *
  720. * Calculate LBA and transfer length for 10-byte commands.
  721. *
  722. * RETURNS:
  723. * @plba: the LBA
  724. * @plen: the transfer length
  725. */
  726. static void scsi_10_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
  727. {
  728. u64 lba = 0;
  729. u32 len = 0;
  730. VPRINTK("ten-byte command\n");
  731. lba |= ((u64)scsicmd[2]) << 24;
  732. lba |= ((u64)scsicmd[3]) << 16;
  733. lba |= ((u64)scsicmd[4]) << 8;
  734. lba |= ((u64)scsicmd[5]);
  735. len |= ((u32)scsicmd[7]) << 8;
  736. len |= ((u32)scsicmd[8]);
  737. *plba = lba;
  738. *plen = len;
  739. }
  740. /**
  741. * scsi_16_lba_len - Get LBA and transfer length
  742. * @scsicmd: SCSI command to translate
  743. *
  744. * Calculate LBA and transfer length for 16-byte commands.
  745. *
  746. * RETURNS:
  747. * @plba: the LBA
  748. * @plen: the transfer length
  749. */
  750. static void scsi_16_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
  751. {
  752. u64 lba = 0;
  753. u32 len = 0;
  754. VPRINTK("sixteen-byte command\n");
  755. lba |= ((u64)scsicmd[2]) << 56;
  756. lba |= ((u64)scsicmd[3]) << 48;
  757. lba |= ((u64)scsicmd[4]) << 40;
  758. lba |= ((u64)scsicmd[5]) << 32;
  759. lba |= ((u64)scsicmd[6]) << 24;
  760. lba |= ((u64)scsicmd[7]) << 16;
  761. lba |= ((u64)scsicmd[8]) << 8;
  762. lba |= ((u64)scsicmd[9]);
  763. len |= ((u32)scsicmd[10]) << 24;
  764. len |= ((u32)scsicmd[11]) << 16;
  765. len |= ((u32)scsicmd[12]) << 8;
  766. len |= ((u32)scsicmd[13]);
  767. *plba = lba;
  768. *plen = len;
  769. }
  770. /**
  771. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  772. * @qc: Storage for translated ATA taskfile
  773. * @scsicmd: SCSI command to translate
  774. *
  775. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  776. *
  777. * LOCKING:
  778. * spin_lock_irqsave(host_set lock)
  779. *
  780. * RETURNS:
  781. * Zero on success, non-zero on error.
  782. */
  783. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  784. {
  785. struct ata_taskfile *tf = &qc->tf;
  786. struct ata_device *dev = qc->dev;
  787. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  788. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  789. u64 dev_sectors = qc->dev->n_sectors;
  790. u64 block;
  791. u32 n_block;
  792. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  793. tf->protocol = ATA_PROT_NODATA;
  794. if (scsicmd[0] == VERIFY)
  795. scsi_10_lba_len(scsicmd, &block, &n_block);
  796. else if (scsicmd[0] == VERIFY_16)
  797. scsi_16_lba_len(scsicmd, &block, &n_block);
  798. else
  799. goto invalid_fld;
  800. if (!n_block)
  801. goto nothing_to_do;
  802. if (block >= dev_sectors)
  803. goto out_of_range;
  804. if ((block + n_block) > dev_sectors)
  805. goto out_of_range;
  806. if (lba48) {
  807. if (n_block > (64 * 1024))
  808. goto invalid_fld;
  809. } else {
  810. if (n_block > 256)
  811. goto invalid_fld;
  812. }
  813. if (lba) {
  814. if (lba48) {
  815. tf->command = ATA_CMD_VERIFY_EXT;
  816. tf->hob_nsect = (n_block >> 8) & 0xff;
  817. tf->hob_lbah = (block >> 40) & 0xff;
  818. tf->hob_lbam = (block >> 32) & 0xff;
  819. tf->hob_lbal = (block >> 24) & 0xff;
  820. } else {
  821. tf->command = ATA_CMD_VERIFY;
  822. tf->device |= (block >> 24) & 0xf;
  823. }
  824. tf->nsect = n_block & 0xff;
  825. tf->lbah = (block >> 16) & 0xff;
  826. tf->lbam = (block >> 8) & 0xff;
  827. tf->lbal = block & 0xff;
  828. tf->device |= ATA_LBA;
  829. } else {
  830. /* CHS */
  831. u32 sect, head, cyl, track;
  832. /* Convert LBA to CHS */
  833. track = (u32)block / dev->sectors;
  834. cyl = track / dev->heads;
  835. head = track % dev->heads;
  836. sect = (u32)block % dev->sectors + 1;
  837. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  838. (u32)block, track, cyl, head, sect);
  839. /* Check whether the converted CHS can fit.
  840. Cylinder: 0-65535
  841. Head: 0-15
  842. Sector: 1-255*/
  843. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  844. goto out_of_range;
  845. tf->command = ATA_CMD_VERIFY;
  846. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  847. tf->lbal = sect;
  848. tf->lbam = cyl;
  849. tf->lbah = cyl >> 8;
  850. tf->device |= head;
  851. }
  852. return 0;
  853. invalid_fld:
  854. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  855. /* "Invalid field in cbd" */
  856. return 1;
  857. out_of_range:
  858. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  859. /* "Logical Block Address out of range" */
  860. return 1;
  861. nothing_to_do:
  862. qc->scsicmd->result = SAM_STAT_GOOD;
  863. return 1;
  864. }
  865. /**
  866. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  867. * @qc: Storage for translated ATA taskfile
  868. * @scsicmd: SCSI command to translate
  869. *
  870. * Converts any of six SCSI read/write commands into the
  871. * ATA counterpart, including starting sector (LBA),
  872. * sector count, and taking into account the device's LBA48
  873. * support.
  874. *
  875. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  876. * %WRITE_16 are currently supported.
  877. *
  878. * LOCKING:
  879. * spin_lock_irqsave(host_set lock)
  880. *
  881. * RETURNS:
  882. * Zero on success, non-zero on error.
  883. */
  884. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  885. {
  886. struct ata_taskfile *tf = &qc->tf;
  887. struct ata_device *dev = qc->dev;
  888. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  889. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  890. u64 block;
  891. u32 n_block;
  892. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  893. tf->protocol = qc->dev->xfer_protocol;
  894. if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
  895. scsicmd[0] == READ_16) {
  896. tf->command = qc->dev->read_cmd;
  897. } else {
  898. tf->command = qc->dev->write_cmd;
  899. tf->flags |= ATA_TFLAG_WRITE;
  900. }
  901. /* Calculate the SCSI LBA and transfer length. */
  902. switch (scsicmd[0]) {
  903. case READ_10:
  904. case WRITE_10:
  905. scsi_10_lba_len(scsicmd, &block, &n_block);
  906. break;
  907. case READ_6:
  908. case WRITE_6:
  909. scsi_6_lba_len(scsicmd, &block, &n_block);
  910. /* for 6-byte r/w commands, transfer length 0
  911. * means 256 blocks of data, not 0 block.
  912. */
  913. if (!n_block)
  914. n_block = 256;
  915. break;
  916. case READ_16:
  917. case WRITE_16:
  918. scsi_16_lba_len(scsicmd, &block, &n_block);
  919. break;
  920. default:
  921. DPRINTK("no-byte command\n");
  922. goto invalid_fld;
  923. }
  924. /* Check and compose ATA command */
  925. if (!n_block)
  926. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  927. * length 0 means transfer 0 block of data.
  928. * However, for ATA R/W commands, sector count 0 means
  929. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  930. */
  931. goto nothing_to_do;
  932. if (lba) {
  933. if (lba48) {
  934. /* The request -may- be too large for LBA48. */
  935. if ((block >> 48) || (n_block > 65536))
  936. goto out_of_range;
  937. tf->hob_nsect = (n_block >> 8) & 0xff;
  938. tf->hob_lbah = (block >> 40) & 0xff;
  939. tf->hob_lbam = (block >> 32) & 0xff;
  940. tf->hob_lbal = (block >> 24) & 0xff;
  941. } else {
  942. /* LBA28 */
  943. /* The request -may- be too large for LBA28. */
  944. if ((block >> 28) || (n_block > 256))
  945. goto out_of_range;
  946. tf->device |= (block >> 24) & 0xf;
  947. }
  948. qc->nsect = n_block;
  949. tf->nsect = n_block & 0xff;
  950. tf->lbah = (block >> 16) & 0xff;
  951. tf->lbam = (block >> 8) & 0xff;
  952. tf->lbal = block & 0xff;
  953. tf->device |= ATA_LBA;
  954. } else {
  955. /* CHS */
  956. u32 sect, head, cyl, track;
  957. /* The request -may- be too large for CHS addressing. */
  958. if ((block >> 28) || (n_block > 256))
  959. goto out_of_range;
  960. /* Convert LBA to CHS */
  961. track = (u32)block / dev->sectors;
  962. cyl = track / dev->heads;
  963. head = track % dev->heads;
  964. sect = (u32)block % dev->sectors + 1;
  965. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  966. (u32)block, track, cyl, head, sect);
  967. /* Check whether the converted CHS can fit.
  968. Cylinder: 0-65535
  969. Head: 0-15
  970. Sector: 1-255*/
  971. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  972. goto out_of_range;
  973. qc->nsect = n_block;
  974. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  975. tf->lbal = sect;
  976. tf->lbam = cyl;
  977. tf->lbah = cyl >> 8;
  978. tf->device |= head;
  979. }
  980. return 0;
  981. invalid_fld:
  982. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  983. /* "Invalid field in cbd" */
  984. return 1;
  985. out_of_range:
  986. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  987. /* "Logical Block Address out of range" */
  988. return 1;
  989. nothing_to_do:
  990. qc->scsicmd->result = SAM_STAT_GOOD;
  991. return 1;
  992. }
  993. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  994. {
  995. struct scsi_cmnd *cmd = qc->scsicmd;
  996. int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
  997. /* For ATA pass thru (SAT) commands, generate a sense block if
  998. * user mandated it or if there's an error. Note that if we
  999. * generate because the user forced us to, a check condition
  1000. * is generated and the ATA register values are returned
  1001. * whether the command completed successfully or not. If there
  1002. * was no error, SK, ASC and ASCQ will all be zero.
  1003. */
  1004. if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
  1005. ((cmd->cmnd[2] & 0x20) || need_sense)) {
  1006. ata_gen_ata_desc_sense(qc);
  1007. } else {
  1008. if (!need_sense) {
  1009. cmd->result = SAM_STAT_GOOD;
  1010. } else {
  1011. /* TODO: decide which descriptor format to use
  1012. * for 48b LBA devices and call that here
  1013. * instead of the fixed desc, which is only
  1014. * good for smaller LBA (and maybe CHS?)
  1015. * devices.
  1016. */
  1017. ata_gen_fixed_sense(qc);
  1018. }
  1019. }
  1020. if (need_sense) {
  1021. /* The ata_gen_..._sense routines fill in tf */
  1022. ata_dump_status(qc->ap->id, &qc->tf);
  1023. }
  1024. qc->scsidone(cmd);
  1025. return 0;
  1026. }
  1027. /**
  1028. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1029. * @ap: ATA port to which the command is addressed
  1030. * @dev: ATA device to which the command is addressed
  1031. * @cmd: SCSI command to execute
  1032. * @done: SCSI command completion function
  1033. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1034. *
  1035. * Our ->queuecommand() function has decided that the SCSI
  1036. * command issued can be directly translated into an ATA
  1037. * command, rather than handled internally.
  1038. *
  1039. * This function sets up an ata_queued_cmd structure for the
  1040. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1041. *
  1042. * The xlat_func argument (actor) returns 0 if ready to execute
  1043. * ATA command, else 1 to finish translation. If 1 is returned
  1044. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1045. * to be set reflecting an error condition or clean (early)
  1046. * termination.
  1047. *
  1048. * LOCKING:
  1049. * spin_lock_irqsave(host_set lock)
  1050. */
  1051. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  1052. struct scsi_cmnd *cmd,
  1053. void (*done)(struct scsi_cmnd *),
  1054. ata_xlat_func_t xlat_func)
  1055. {
  1056. struct ata_queued_cmd *qc;
  1057. u8 *scsicmd = cmd->cmnd;
  1058. VPRINTK("ENTER\n");
  1059. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  1060. if (!qc)
  1061. goto err_mem;
  1062. /* data is present; dma-map it */
  1063. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1064. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1065. if (unlikely(cmd->request_bufflen < 1)) {
  1066. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  1067. ap->id, dev->devno);
  1068. goto err_did;
  1069. }
  1070. if (cmd->use_sg)
  1071. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  1072. else
  1073. ata_sg_init_one(qc, cmd->request_buffer,
  1074. cmd->request_bufflen);
  1075. qc->dma_dir = cmd->sc_data_direction;
  1076. }
  1077. qc->complete_fn = ata_scsi_qc_complete;
  1078. if (xlat_func(qc, scsicmd))
  1079. goto early_finish;
  1080. /* select device, send command to hardware */
  1081. if (ata_qc_issue(qc))
  1082. goto err_did;
  1083. VPRINTK("EXIT\n");
  1084. return;
  1085. early_finish:
  1086. ata_qc_free(qc);
  1087. done(cmd);
  1088. DPRINTK("EXIT - early finish (good or error)\n");
  1089. return;
  1090. err_did:
  1091. ata_qc_free(qc);
  1092. err_mem:
  1093. cmd->result = (DID_ERROR << 16);
  1094. done(cmd);
  1095. DPRINTK("EXIT - internal\n");
  1096. return;
  1097. }
  1098. /**
  1099. * ata_scsi_rbuf_get - Map response buffer.
  1100. * @cmd: SCSI command containing buffer to be mapped.
  1101. * @buf_out: Pointer to mapped area.
  1102. *
  1103. * Maps buffer contained within SCSI command @cmd.
  1104. *
  1105. * LOCKING:
  1106. * spin_lock_irqsave(host_set lock)
  1107. *
  1108. * RETURNS:
  1109. * Length of response buffer.
  1110. */
  1111. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  1112. {
  1113. u8 *buf;
  1114. unsigned int buflen;
  1115. if (cmd->use_sg) {
  1116. struct scatterlist *sg;
  1117. sg = (struct scatterlist *) cmd->request_buffer;
  1118. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  1119. buflen = sg->length;
  1120. } else {
  1121. buf = cmd->request_buffer;
  1122. buflen = cmd->request_bufflen;
  1123. }
  1124. *buf_out = buf;
  1125. return buflen;
  1126. }
  1127. /**
  1128. * ata_scsi_rbuf_put - Unmap response buffer.
  1129. * @cmd: SCSI command containing buffer to be unmapped.
  1130. * @buf: buffer to unmap
  1131. *
  1132. * Unmaps response buffer contained within @cmd.
  1133. *
  1134. * LOCKING:
  1135. * spin_lock_irqsave(host_set lock)
  1136. */
  1137. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  1138. {
  1139. if (cmd->use_sg) {
  1140. struct scatterlist *sg;
  1141. sg = (struct scatterlist *) cmd->request_buffer;
  1142. kunmap_atomic(buf - sg->offset, KM_USER0);
  1143. }
  1144. }
  1145. /**
  1146. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1147. * @args: device IDENTIFY data / SCSI command of interest.
  1148. * @actor: Callback hook for desired SCSI command simulator
  1149. *
  1150. * Takes care of the hard work of simulating a SCSI command...
  1151. * Mapping the response buffer, calling the command's handler,
  1152. * and handling the handler's return value. This return value
  1153. * indicates whether the handler wishes the SCSI command to be
  1154. * completed successfully (0), or not (in which case cmd->result
  1155. * and sense buffer are assumed to be set).
  1156. *
  1157. * LOCKING:
  1158. * spin_lock_irqsave(host_set lock)
  1159. */
  1160. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1161. unsigned int (*actor) (struct ata_scsi_args *args,
  1162. u8 *rbuf, unsigned int buflen))
  1163. {
  1164. u8 *rbuf;
  1165. unsigned int buflen, rc;
  1166. struct scsi_cmnd *cmd = args->cmd;
  1167. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  1168. memset(rbuf, 0, buflen);
  1169. rc = actor(args, rbuf, buflen);
  1170. ata_scsi_rbuf_put(cmd, rbuf);
  1171. if (rc == 0)
  1172. cmd->result = SAM_STAT_GOOD;
  1173. args->done(cmd);
  1174. }
  1175. /**
  1176. * ata_scsiop_inq_std - Simulate INQUIRY command
  1177. * @args: device IDENTIFY data / SCSI command of interest.
  1178. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1179. * @buflen: Response buffer length.
  1180. *
  1181. * Returns standard device identification data associated
  1182. * with non-EVPD INQUIRY command output.
  1183. *
  1184. * LOCKING:
  1185. * spin_lock_irqsave(host_set lock)
  1186. */
  1187. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  1188. unsigned int buflen)
  1189. {
  1190. u8 hdr[] = {
  1191. TYPE_DISK,
  1192. 0,
  1193. 0x5, /* claim SPC-3 version compatibility */
  1194. 2,
  1195. 95 - 4
  1196. };
  1197. /* set scsi removeable (RMB) bit per ata bit */
  1198. if (ata_id_removeable(args->id))
  1199. hdr[1] |= (1 << 7);
  1200. VPRINTK("ENTER\n");
  1201. memcpy(rbuf, hdr, sizeof(hdr));
  1202. if (buflen > 35) {
  1203. memcpy(&rbuf[8], "ATA ", 8);
  1204. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  1205. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  1206. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1207. memcpy(&rbuf[32], "n/a ", 4);
  1208. }
  1209. if (buflen > 63) {
  1210. const u8 versions[] = {
  1211. 0x60, /* SAM-3 (no version claimed) */
  1212. 0x03,
  1213. 0x20, /* SBC-2 (no version claimed) */
  1214. 0x02,
  1215. 0x60 /* SPC-3 (no version claimed) */
  1216. };
  1217. memcpy(rbuf + 59, versions, sizeof(versions));
  1218. }
  1219. return 0;
  1220. }
  1221. /**
  1222. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  1223. * @args: device IDENTIFY data / SCSI command of interest.
  1224. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1225. * @buflen: Response buffer length.
  1226. *
  1227. * Returns list of inquiry EVPD pages available.
  1228. *
  1229. * LOCKING:
  1230. * spin_lock_irqsave(host_set lock)
  1231. */
  1232. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  1233. unsigned int buflen)
  1234. {
  1235. const u8 pages[] = {
  1236. 0x00, /* page 0x00, this page */
  1237. 0x80, /* page 0x80, unit serial no page */
  1238. 0x83 /* page 0x83, device ident page */
  1239. };
  1240. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  1241. if (buflen > 6)
  1242. memcpy(rbuf + 4, pages, sizeof(pages));
  1243. return 0;
  1244. }
  1245. /**
  1246. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  1247. * @args: device IDENTIFY data / SCSI command of interest.
  1248. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1249. * @buflen: Response buffer length.
  1250. *
  1251. * Returns ATA device serial number.
  1252. *
  1253. * LOCKING:
  1254. * spin_lock_irqsave(host_set lock)
  1255. */
  1256. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  1257. unsigned int buflen)
  1258. {
  1259. const u8 hdr[] = {
  1260. 0,
  1261. 0x80, /* this page code */
  1262. 0,
  1263. ATA_SERNO_LEN, /* page len */
  1264. };
  1265. memcpy(rbuf, hdr, sizeof(hdr));
  1266. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  1267. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  1268. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1269. return 0;
  1270. }
  1271. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  1272. /**
  1273. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  1274. * @args: device IDENTIFY data / SCSI command of interest.
  1275. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1276. * @buflen: Response buffer length.
  1277. *
  1278. * Returns device identification. Currently hardcoded to
  1279. * return "Linux ATA-SCSI simulator".
  1280. *
  1281. * LOCKING:
  1282. * spin_lock_irqsave(host_set lock)
  1283. */
  1284. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  1285. unsigned int buflen)
  1286. {
  1287. rbuf[1] = 0x83; /* this page code */
  1288. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  1289. /* our one and only identification descriptor (vendor-specific) */
  1290. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  1291. rbuf[4 + 0] = 2; /* code set: ASCII */
  1292. rbuf[4 + 3] = strlen(inq_83_str);
  1293. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  1294. }
  1295. return 0;
  1296. }
  1297. /**
  1298. * ata_scsiop_noop - Command handler that simply returns success.
  1299. * @args: device IDENTIFY data / SCSI command of interest.
  1300. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1301. * @buflen: Response buffer length.
  1302. *
  1303. * No operation. Simply returns success to caller, to indicate
  1304. * that the caller should successfully complete this SCSI command.
  1305. *
  1306. * LOCKING:
  1307. * spin_lock_irqsave(host_set lock)
  1308. */
  1309. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  1310. unsigned int buflen)
  1311. {
  1312. VPRINTK("ENTER\n");
  1313. return 0;
  1314. }
  1315. /**
  1316. * ata_msense_push - Push data onto MODE SENSE data output buffer
  1317. * @ptr_io: (input/output) Location to store more output data
  1318. * @last: End of output data buffer
  1319. * @buf: Pointer to BLOB being added to output buffer
  1320. * @buflen: Length of BLOB
  1321. *
  1322. * Store MODE SENSE data on an output buffer.
  1323. *
  1324. * LOCKING:
  1325. * None.
  1326. */
  1327. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  1328. const u8 *buf, unsigned int buflen)
  1329. {
  1330. u8 *ptr = *ptr_io;
  1331. if ((ptr + buflen - 1) > last)
  1332. return;
  1333. memcpy(ptr, buf, buflen);
  1334. ptr += buflen;
  1335. *ptr_io = ptr;
  1336. }
  1337. /**
  1338. * ata_msense_caching - Simulate MODE SENSE caching info page
  1339. * @id: device IDENTIFY data
  1340. * @ptr_io: (input/output) Location to store more output data
  1341. * @last: End of output data buffer
  1342. *
  1343. * Generate a caching info page, which conditionally indicates
  1344. * write caching to the SCSI layer, depending on device
  1345. * capabilities.
  1346. *
  1347. * LOCKING:
  1348. * None.
  1349. */
  1350. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  1351. const u8 *last)
  1352. {
  1353. u8 page[] = {
  1354. 0x8, /* page code */
  1355. 0x12, /* page length */
  1356. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
  1357. 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
  1358. };
  1359. if (ata_id_wcache_enabled(id))
  1360. page[2] |= (1 << 2); /* write cache enable */
  1361. if (!ata_id_rahead_enabled(id))
  1362. page[12] |= (1 << 5); /* disable read ahead */
  1363. ata_msense_push(ptr_io, last, page, sizeof(page));
  1364. return sizeof(page);
  1365. }
  1366. /**
  1367. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1368. * @dev: Device associated with this MODE SENSE command
  1369. * @ptr_io: (input/output) Location to store more output data
  1370. * @last: End of output data buffer
  1371. *
  1372. * Generate a generic MODE SENSE control mode page.
  1373. *
  1374. * LOCKING:
  1375. * None.
  1376. */
  1377. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1378. {
  1379. const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
  1380. /* byte 2: set the descriptor format sense data bit (bit 2)
  1381. * since we need to support returning this format for SAT
  1382. * commands and any SCSI commands against a 48b LBA device.
  1383. */
  1384. ata_msense_push(ptr_io, last, page, sizeof(page));
  1385. return sizeof(page);
  1386. }
  1387. /**
  1388. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1389. * @dev: Device associated with this MODE SENSE command
  1390. * @ptr_io: (input/output) Location to store more output data
  1391. * @last: End of output data buffer
  1392. *
  1393. * Generate a generic MODE SENSE r/w error recovery page.
  1394. *
  1395. * LOCKING:
  1396. * None.
  1397. */
  1398. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1399. {
  1400. const u8 page[] = {
  1401. 0x1, /* page code */
  1402. 0xa, /* page length */
  1403. (1 << 7) | (1 << 6), /* note auto r/w reallocation */
  1404. 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
  1405. };
  1406. ata_msense_push(ptr_io, last, page, sizeof(page));
  1407. return sizeof(page);
  1408. }
  1409. /**
  1410. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1411. * @args: device IDENTIFY data / SCSI command of interest.
  1412. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1413. * @buflen: Response buffer length.
  1414. *
  1415. * Simulate MODE SENSE commands.
  1416. *
  1417. * LOCKING:
  1418. * spin_lock_irqsave(host_set lock)
  1419. */
  1420. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1421. unsigned int buflen)
  1422. {
  1423. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1424. unsigned int page_control, six_byte, output_len;
  1425. VPRINTK("ENTER\n");
  1426. six_byte = (scsicmd[0] == MODE_SENSE);
  1427. /* we only support saved and current values (which we treat
  1428. * in the same manner)
  1429. */
  1430. page_control = scsicmd[2] >> 6;
  1431. switch (page_control) {
  1432. case 0: /* current */
  1433. break; /* supported */
  1434. case 3: /* saved */
  1435. goto saving_not_supp;
  1436. case 1: /* changeable */
  1437. case 2: /* defaults */
  1438. default:
  1439. goto invalid_fld;
  1440. }
  1441. if (six_byte)
  1442. output_len = 4;
  1443. else
  1444. output_len = 8;
  1445. p = rbuf + output_len;
  1446. last = rbuf + buflen - 1;
  1447. switch(scsicmd[2] & 0x3f) {
  1448. case 0x01: /* r/w error recovery */
  1449. output_len += ata_msense_rw_recovery(&p, last);
  1450. break;
  1451. case 0x08: /* caching */
  1452. output_len += ata_msense_caching(args->id, &p, last);
  1453. break;
  1454. case 0x0a: { /* control mode */
  1455. output_len += ata_msense_ctl_mode(&p, last);
  1456. break;
  1457. }
  1458. case 0x3f: /* all pages */
  1459. output_len += ata_msense_rw_recovery(&p, last);
  1460. output_len += ata_msense_caching(args->id, &p, last);
  1461. output_len += ata_msense_ctl_mode(&p, last);
  1462. break;
  1463. default: /* invalid page code */
  1464. goto invalid_fld;
  1465. }
  1466. if (six_byte) {
  1467. output_len--;
  1468. rbuf[0] = output_len;
  1469. } else {
  1470. output_len -= 2;
  1471. rbuf[0] = output_len >> 8;
  1472. rbuf[1] = output_len;
  1473. }
  1474. return 0;
  1475. invalid_fld:
  1476. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1477. /* "Invalid field in cbd" */
  1478. return 1;
  1479. saving_not_supp:
  1480. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  1481. /* "Saving parameters not supported" */
  1482. return 1;
  1483. }
  1484. /**
  1485. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1486. * @args: device IDENTIFY data / SCSI command of interest.
  1487. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1488. * @buflen: Response buffer length.
  1489. *
  1490. * Simulate READ CAPACITY commands.
  1491. *
  1492. * LOCKING:
  1493. * spin_lock_irqsave(host_set lock)
  1494. */
  1495. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1496. unsigned int buflen)
  1497. {
  1498. u64 n_sectors;
  1499. u32 tmp;
  1500. VPRINTK("ENTER\n");
  1501. if (ata_id_has_lba(args->id)) {
  1502. if (ata_id_has_lba48(args->id))
  1503. n_sectors = ata_id_u64(args->id, 100);
  1504. else
  1505. n_sectors = ata_id_u32(args->id, 60);
  1506. } else {
  1507. /* CHS default translation */
  1508. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1509. if (ata_id_current_chs_valid(args->id))
  1510. /* CHS current translation */
  1511. n_sectors = ata_id_u32(args->id, 57);
  1512. }
  1513. n_sectors--; /* ATA TotalUserSectors - 1 */
  1514. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1515. if( n_sectors >= 0xffffffffULL )
  1516. tmp = 0xffffffff ; /* Return max count on overflow */
  1517. else
  1518. tmp = n_sectors ;
  1519. /* sector count, 32-bit */
  1520. rbuf[0] = tmp >> (8 * 3);
  1521. rbuf[1] = tmp >> (8 * 2);
  1522. rbuf[2] = tmp >> (8 * 1);
  1523. rbuf[3] = tmp;
  1524. /* sector size */
  1525. tmp = ATA_SECT_SIZE;
  1526. rbuf[6] = tmp >> 8;
  1527. rbuf[7] = tmp;
  1528. } else {
  1529. /* sector count, 64-bit */
  1530. tmp = n_sectors >> (8 * 4);
  1531. rbuf[2] = tmp >> (8 * 3);
  1532. rbuf[3] = tmp >> (8 * 2);
  1533. rbuf[4] = tmp >> (8 * 1);
  1534. rbuf[5] = tmp;
  1535. tmp = n_sectors;
  1536. rbuf[6] = tmp >> (8 * 3);
  1537. rbuf[7] = tmp >> (8 * 2);
  1538. rbuf[8] = tmp >> (8 * 1);
  1539. rbuf[9] = tmp;
  1540. /* sector size */
  1541. tmp = ATA_SECT_SIZE;
  1542. rbuf[12] = tmp >> 8;
  1543. rbuf[13] = tmp;
  1544. }
  1545. return 0;
  1546. }
  1547. /**
  1548. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1549. * @args: device IDENTIFY data / SCSI command of interest.
  1550. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1551. * @buflen: Response buffer length.
  1552. *
  1553. * Simulate REPORT LUNS command.
  1554. *
  1555. * LOCKING:
  1556. * spin_lock_irqsave(host_set lock)
  1557. */
  1558. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1559. unsigned int buflen)
  1560. {
  1561. VPRINTK("ENTER\n");
  1562. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1563. return 0;
  1564. }
  1565. /**
  1566. * ata_scsi_set_sense - Set SCSI sense data and status
  1567. * @cmd: SCSI request to be handled
  1568. * @sk: SCSI-defined sense key
  1569. * @asc: SCSI-defined additional sense code
  1570. * @ascq: SCSI-defined additional sense code qualifier
  1571. *
  1572. * Helper function that builds a valid fixed format, current
  1573. * response code and the given sense key (sk), additional sense
  1574. * code (asc) and additional sense code qualifier (ascq) with
  1575. * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
  1576. * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
  1577. *
  1578. * LOCKING:
  1579. * Not required
  1580. */
  1581. void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  1582. {
  1583. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  1584. cmd->sense_buffer[0] = 0x70; /* fixed format, current */
  1585. cmd->sense_buffer[2] = sk;
  1586. cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
  1587. cmd->sense_buffer[12] = asc;
  1588. cmd->sense_buffer[13] = ascq;
  1589. }
  1590. /**
  1591. * ata_scsi_badcmd - End a SCSI request with an error
  1592. * @cmd: SCSI request to be handled
  1593. * @done: SCSI command completion function
  1594. * @asc: SCSI-defined additional sense code
  1595. * @ascq: SCSI-defined additional sense code qualifier
  1596. *
  1597. * Helper function that completes a SCSI command with
  1598. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1599. * and the specified additional sense codes.
  1600. *
  1601. * LOCKING:
  1602. * spin_lock_irqsave(host_set lock)
  1603. */
  1604. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1605. {
  1606. DPRINTK("ENTER\n");
  1607. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
  1608. done(cmd);
  1609. }
  1610. void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
  1611. struct scsi_cmnd *cmd)
  1612. {
  1613. DECLARE_COMPLETION(wait);
  1614. struct ata_queued_cmd *qc;
  1615. unsigned long flags;
  1616. int rc;
  1617. DPRINTK("ATAPI request sense\n");
  1618. qc = ata_qc_new_init(ap, dev);
  1619. BUG_ON(qc == NULL);
  1620. /* FIXME: is this needed? */
  1621. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  1622. ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
  1623. qc->dma_dir = DMA_FROM_DEVICE;
  1624. memset(&qc->cdb, 0, ap->cdb_len);
  1625. qc->cdb[0] = REQUEST_SENSE;
  1626. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1627. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1628. qc->tf.command = ATA_CMD_PACKET;
  1629. qc->tf.protocol = ATA_PROT_ATAPI;
  1630. qc->tf.lbam = (8 * 1024) & 0xff;
  1631. qc->tf.lbah = (8 * 1024) >> 8;
  1632. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  1633. qc->waiting = &wait;
  1634. qc->complete_fn = ata_qc_complete_noop;
  1635. spin_lock_irqsave(&ap->host_set->lock, flags);
  1636. rc = ata_qc_issue(qc);
  1637. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1638. if (rc)
  1639. ata_port_disable(ap);
  1640. else
  1641. wait_for_completion(&wait);
  1642. DPRINTK("EXIT\n");
  1643. }
  1644. static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  1645. {
  1646. struct scsi_cmnd *cmd = qc->scsicmd;
  1647. VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat);
  1648. if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ)))
  1649. ata_to_sense_error(qc, drv_stat);
  1650. else if (unlikely(drv_stat & ATA_ERR)) {
  1651. DPRINTK("request check condition\n");
  1652. /* FIXME: command completion with check condition
  1653. * but no sense causes the error handler to run,
  1654. * which then issues REQUEST SENSE, fills in the sense
  1655. * buffer, and completes the command (for the second
  1656. * time). We need to issue REQUEST SENSE some other
  1657. * way, to avoid completing the command twice.
  1658. */
  1659. cmd->result = SAM_STAT_CHECK_CONDITION;
  1660. qc->scsidone(cmd);
  1661. return 1;
  1662. }
  1663. else {
  1664. u8 *scsicmd = cmd->cmnd;
  1665. if (scsicmd[0] == INQUIRY) {
  1666. u8 *buf = NULL;
  1667. unsigned int buflen;
  1668. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1669. /* ATAPI devices typically report zero for their SCSI version,
  1670. * and sometimes deviate from the spec WRT response data
  1671. * format. If SCSI version is reported as zero like normal,
  1672. * then we make the following fixups: 1) Fake MMC-5 version,
  1673. * to indicate to the Linux scsi midlayer this is a modern
  1674. * device. 2) Ensure response data format / ATAPI information
  1675. * are always correct.
  1676. */
  1677. /* FIXME: do we ever override EVPD pages and the like, with
  1678. * this code?
  1679. */
  1680. if (buf[2] == 0) {
  1681. buf[2] = 0x5;
  1682. buf[3] = 0x32;
  1683. }
  1684. ata_scsi_rbuf_put(cmd, buf);
  1685. }
  1686. cmd->result = SAM_STAT_GOOD;
  1687. }
  1688. qc->scsidone(cmd);
  1689. return 0;
  1690. }
  1691. /**
  1692. * atapi_xlat - Initialize PACKET taskfile
  1693. * @qc: command structure to be initialized
  1694. * @scsicmd: SCSI CDB associated with this PACKET command
  1695. *
  1696. * LOCKING:
  1697. * spin_lock_irqsave(host_set lock)
  1698. *
  1699. * RETURNS:
  1700. * Zero on success, non-zero on failure.
  1701. */
  1702. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  1703. {
  1704. struct scsi_cmnd *cmd = qc->scsicmd;
  1705. struct ata_device *dev = qc->dev;
  1706. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1707. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1708. if (!using_pio)
  1709. /* Check whether ATAPI DMA is safe */
  1710. if (ata_check_atapi_dma(qc))
  1711. using_pio = 1;
  1712. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1713. qc->complete_fn = atapi_qc_complete;
  1714. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1715. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1716. qc->tf.flags |= ATA_TFLAG_WRITE;
  1717. DPRINTK("direction: write\n");
  1718. }
  1719. qc->tf.command = ATA_CMD_PACKET;
  1720. /* no data, or PIO data xfer */
  1721. if (using_pio || nodata) {
  1722. if (nodata)
  1723. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1724. else
  1725. qc->tf.protocol = ATA_PROT_ATAPI;
  1726. qc->tf.lbam = (8 * 1024) & 0xff;
  1727. qc->tf.lbah = (8 * 1024) >> 8;
  1728. }
  1729. /* DMA data xfer */
  1730. else {
  1731. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1732. qc->tf.feature |= ATAPI_PKT_DMA;
  1733. #ifdef ATAPI_ENABLE_DMADIR
  1734. /* some SATA bridges need us to indicate data xfer direction */
  1735. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1736. qc->tf.feature |= ATAPI_DMADIR;
  1737. #endif
  1738. }
  1739. qc->nbytes = cmd->bufflen;
  1740. return 0;
  1741. }
  1742. /**
  1743. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1744. * @ap: ATA port to which the device is attached
  1745. * @scsidev: SCSI device from which we derive the ATA device
  1746. *
  1747. * Given various information provided in struct scsi_cmnd,
  1748. * map that onto an ATA bus, and using that mapping
  1749. * determine which ata_device is associated with the
  1750. * SCSI command to be sent.
  1751. *
  1752. * LOCKING:
  1753. * spin_lock_irqsave(host_set lock)
  1754. *
  1755. * RETURNS:
  1756. * Associated ATA device, or %NULL if not found.
  1757. */
  1758. static struct ata_device *
  1759. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
  1760. {
  1761. struct ata_device *dev;
  1762. /* skip commands not addressed to targets we simulate */
  1763. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1764. dev = &ap->device[scsidev->id];
  1765. else
  1766. return NULL;
  1767. if (unlikely((scsidev->channel != 0) ||
  1768. (scsidev->lun != 0)))
  1769. return NULL;
  1770. if (unlikely(!ata_dev_present(dev)))
  1771. return NULL;
  1772. if (!atapi_enabled) {
  1773. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1774. return NULL;
  1775. }
  1776. return dev;
  1777. }
  1778. /*
  1779. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  1780. * @byte1: Byte 1 from pass-thru CDB.
  1781. *
  1782. * RETURNS:
  1783. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  1784. */
  1785. static u8
  1786. ata_scsi_map_proto(u8 byte1)
  1787. {
  1788. switch((byte1 & 0x1e) >> 1) {
  1789. case 3: /* Non-data */
  1790. return ATA_PROT_NODATA;
  1791. case 6: /* DMA */
  1792. return ATA_PROT_DMA;
  1793. case 4: /* PIO Data-in */
  1794. case 5: /* PIO Data-out */
  1795. if (byte1 & 0xe0) {
  1796. return ATA_PROT_PIO_MULT;
  1797. }
  1798. return ATA_PROT_PIO;
  1799. case 10: /* Device Reset */
  1800. case 0: /* Hard Reset */
  1801. case 1: /* SRST */
  1802. case 2: /* Bus Idle */
  1803. case 7: /* Packet */
  1804. case 8: /* DMA Queued */
  1805. case 9: /* Device Diagnostic */
  1806. case 11: /* UDMA Data-in */
  1807. case 12: /* UDMA Data-Out */
  1808. case 13: /* FPDMA */
  1809. default: /* Reserved */
  1810. break;
  1811. }
  1812. return ATA_PROT_UNKNOWN;
  1813. }
  1814. /**
  1815. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  1816. * @qc: command structure to be initialized
  1817. * @cmd: SCSI command to convert
  1818. *
  1819. * Handles either 12 or 16-byte versions of the CDB.
  1820. *
  1821. * RETURNS:
  1822. * Zero on success, non-zero on failure.
  1823. */
  1824. static unsigned int
  1825. ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
  1826. {
  1827. struct ata_taskfile *tf = &(qc->tf);
  1828. struct scsi_cmnd *cmd = qc->scsicmd;
  1829. if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
  1830. return 1;
  1831. /*
  1832. * 12 and 16 byte CDBs use different offsets to
  1833. * provide the various register values.
  1834. */
  1835. if (scsicmd[0] == ATA_16) {
  1836. /*
  1837. * 16-byte CDB - may contain extended commands.
  1838. *
  1839. * If that is the case, copy the upper byte register values.
  1840. */
  1841. if (scsicmd[1] & 0x01) {
  1842. tf->hob_feature = scsicmd[3];
  1843. tf->hob_nsect = scsicmd[5];
  1844. tf->hob_lbal = scsicmd[7];
  1845. tf->hob_lbam = scsicmd[9];
  1846. tf->hob_lbah = scsicmd[11];
  1847. tf->flags |= ATA_TFLAG_LBA48;
  1848. } else
  1849. tf->flags &= ~ATA_TFLAG_LBA48;
  1850. /*
  1851. * Always copy low byte, device and command registers.
  1852. */
  1853. tf->feature = scsicmd[4];
  1854. tf->nsect = scsicmd[6];
  1855. tf->lbal = scsicmd[8];
  1856. tf->lbam = scsicmd[10];
  1857. tf->lbah = scsicmd[12];
  1858. tf->device = scsicmd[13];
  1859. tf->command = scsicmd[14];
  1860. } else {
  1861. /*
  1862. * 12-byte CDB - incapable of extended commands.
  1863. */
  1864. tf->flags &= ~ATA_TFLAG_LBA48;
  1865. tf->feature = scsicmd[3];
  1866. tf->nsect = scsicmd[4];
  1867. tf->lbal = scsicmd[5];
  1868. tf->lbam = scsicmd[6];
  1869. tf->lbah = scsicmd[7];
  1870. tf->device = scsicmd[8];
  1871. tf->command = scsicmd[9];
  1872. }
  1873. /*
  1874. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  1875. * SET_FEATURES - XFER MODE must be preceded/succeeded
  1876. * by an update to hardware-specific registers for each
  1877. * controller (i.e. the reason for ->set_piomode(),
  1878. * ->set_dmamode(), and ->post_set_mode() hooks).
  1879. */
  1880. if ((tf->command == ATA_CMD_SET_FEATURES)
  1881. && (tf->feature == SETFEATURES_XFER))
  1882. return 1;
  1883. /*
  1884. * Set flags so that all registers will be written,
  1885. * and pass on write indication (used for PIO/DMA
  1886. * setup.)
  1887. */
  1888. tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
  1889. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1890. tf->flags |= ATA_TFLAG_WRITE;
  1891. /*
  1892. * Set transfer length.
  1893. *
  1894. * TODO: find out if we need to do more here to
  1895. * cover scatter/gather case.
  1896. */
  1897. qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
  1898. return 0;
  1899. }
  1900. /**
  1901. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1902. * @dev: ATA device
  1903. * @cmd: SCSI command opcode to consider
  1904. *
  1905. * Look up the SCSI command given, and determine whether the
  1906. * SCSI command is to be translated or simulated.
  1907. *
  1908. * RETURNS:
  1909. * Pointer to translation function if possible, %NULL if not.
  1910. */
  1911. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1912. {
  1913. switch (cmd) {
  1914. case READ_6:
  1915. case READ_10:
  1916. case READ_16:
  1917. case WRITE_6:
  1918. case WRITE_10:
  1919. case WRITE_16:
  1920. return ata_scsi_rw_xlat;
  1921. case SYNCHRONIZE_CACHE:
  1922. if (ata_try_flush_cache(dev))
  1923. return ata_scsi_flush_xlat;
  1924. break;
  1925. case VERIFY:
  1926. case VERIFY_16:
  1927. return ata_scsi_verify_xlat;
  1928. case ATA_12:
  1929. case ATA_16:
  1930. return ata_scsi_pass_thru;
  1931. case START_STOP:
  1932. return ata_scsi_start_stop_xlat;
  1933. }
  1934. return NULL;
  1935. }
  1936. /**
  1937. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  1938. * @ap: ATA port to which the command was being sent
  1939. * @cmd: SCSI command to dump
  1940. *
  1941. * Prints the contents of a SCSI command via printk().
  1942. */
  1943. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  1944. struct scsi_cmnd *cmd)
  1945. {
  1946. #ifdef ATA_DEBUG
  1947. struct scsi_device *scsidev = cmd->device;
  1948. u8 *scsicmd = cmd->cmnd;
  1949. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1950. ap->id,
  1951. scsidev->channel, scsidev->id, scsidev->lun,
  1952. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  1953. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  1954. scsicmd[8]);
  1955. #endif
  1956. }
  1957. /**
  1958. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  1959. * @cmd: SCSI command to be sent
  1960. * @done: Completion function, called when command is complete
  1961. *
  1962. * In some cases, this function translates SCSI commands into
  1963. * ATA taskfiles, and queues the taskfiles to be sent to
  1964. * hardware. In other cases, this function simulates a
  1965. * SCSI device by evaluating and responding to certain
  1966. * SCSI commands. This creates the overall effect of
  1967. * ATA and ATAPI devices appearing as SCSI devices.
  1968. *
  1969. * LOCKING:
  1970. * Releases scsi-layer-held lock, and obtains host_set lock.
  1971. *
  1972. * RETURNS:
  1973. * Zero.
  1974. */
  1975. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1976. {
  1977. struct ata_port *ap;
  1978. struct ata_device *dev;
  1979. struct scsi_device *scsidev = cmd->device;
  1980. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  1981. ata_scsi_dump_cdb(ap, cmd);
  1982. dev = ata_scsi_find_dev(ap, scsidev);
  1983. if (unlikely(!dev)) {
  1984. cmd->result = (DID_BAD_TARGET << 16);
  1985. done(cmd);
  1986. goto out_unlock;
  1987. }
  1988. if (dev->class == ATA_DEV_ATA) {
  1989. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  1990. cmd->cmnd[0]);
  1991. if (xlat_func)
  1992. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  1993. else
  1994. ata_scsi_simulate(dev->id, cmd, done);
  1995. } else
  1996. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  1997. out_unlock:
  1998. return 0;
  1999. }
  2000. /**
  2001. * ata_scsi_simulate - simulate SCSI command on ATA device
  2002. * @id: current IDENTIFY data for target device.
  2003. * @cmd: SCSI command being sent to device.
  2004. * @done: SCSI command completion function.
  2005. *
  2006. * Interprets and directly executes a select list of SCSI commands
  2007. * that can be handled internally.
  2008. *
  2009. * LOCKING:
  2010. * spin_lock_irqsave(host_set lock)
  2011. */
  2012. void ata_scsi_simulate(u16 *id,
  2013. struct scsi_cmnd *cmd,
  2014. void (*done)(struct scsi_cmnd *))
  2015. {
  2016. struct ata_scsi_args args;
  2017. u8 *scsicmd = cmd->cmnd;
  2018. args.id = id;
  2019. args.cmd = cmd;
  2020. args.done = done;
  2021. switch(scsicmd[0]) {
  2022. /* no-op's, complete with success */
  2023. case SYNCHRONIZE_CACHE:
  2024. case REZERO_UNIT:
  2025. case SEEK_6:
  2026. case SEEK_10:
  2027. case TEST_UNIT_READY:
  2028. case FORMAT_UNIT: /* FIXME: correct? */
  2029. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  2030. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2031. break;
  2032. case INQUIRY:
  2033. if (scsicmd[1] & 2) /* is CmdDt set? */
  2034. ata_scsi_invalid_field(cmd, done);
  2035. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  2036. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  2037. else if (scsicmd[2] == 0x00)
  2038. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  2039. else if (scsicmd[2] == 0x80)
  2040. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  2041. else if (scsicmd[2] == 0x83)
  2042. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  2043. else
  2044. ata_scsi_invalid_field(cmd, done);
  2045. break;
  2046. case MODE_SENSE:
  2047. case MODE_SENSE_10:
  2048. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  2049. break;
  2050. case MODE_SELECT: /* unconditionally return */
  2051. case MODE_SELECT_10: /* bad-field-in-cdb */
  2052. ata_scsi_invalid_field(cmd, done);
  2053. break;
  2054. case READ_CAPACITY:
  2055. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2056. break;
  2057. case SERVICE_ACTION_IN:
  2058. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  2059. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2060. else
  2061. ata_scsi_invalid_field(cmd, done);
  2062. break;
  2063. case REPORT_LUNS:
  2064. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  2065. break;
  2066. /* mandatory commands we haven't implemented yet */
  2067. case REQUEST_SENSE:
  2068. /* all other commands */
  2069. default:
  2070. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2071. /* "Invalid command operation code" */
  2072. done(cmd);
  2073. break;
  2074. }
  2075. }
  2076. void ata_scsi_scan_host(struct ata_port *ap)
  2077. {
  2078. struct ata_device *dev;
  2079. unsigned int i;
  2080. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  2081. return;
  2082. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2083. dev = &ap->device[i];
  2084. if (ata_dev_present(dev))
  2085. scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
  2086. }
  2087. }