libata-scsi.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994
  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/scsi_host.h>
  40. #include <scsi/scsi_eh.h>
  41. #include <scsi/scsi_device.h>
  42. #include <scsi/scsi_request.h>
  43. #include <scsi/scsi_tcq.h>
  44. #include <scsi/scsi_transport.h>
  45. #include <linux/libata.h>
  46. #include <linux/hdreg.h>
  47. #include <asm/uaccess.h>
  48. #include "libata.h"
  49. #define SECTOR_SIZE 512
  50. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
  51. static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
  52. const struct scsi_device *scsidev);
  53. static struct ata_device * ata_scsi_find_dev(struct ata_port *ap,
  54. const struct scsi_device *scsidev);
  55. static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  56. unsigned int id, unsigned int lun);
  57. #define RW_RECOVERY_MPAGE 0x1
  58. #define RW_RECOVERY_MPAGE_LEN 12
  59. #define CACHE_MPAGE 0x8
  60. #define CACHE_MPAGE_LEN 20
  61. #define CONTROL_MPAGE 0xa
  62. #define CONTROL_MPAGE_LEN 12
  63. #define ALL_MPAGES 0x3f
  64. #define ALL_SUB_MPAGES 0xff
  65. static const u8 def_rw_recovery_mpage[] = {
  66. RW_RECOVERY_MPAGE,
  67. RW_RECOVERY_MPAGE_LEN - 2,
  68. (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
  69. (1 << 6), /* ARRE (auto read reallocation) */
  70. 0, /* read retry count */
  71. 0, 0, 0, 0,
  72. 0, /* write retry count */
  73. 0, 0, 0
  74. };
  75. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  76. CACHE_MPAGE,
  77. CACHE_MPAGE_LEN - 2,
  78. 0, /* contains WCE, needs to be 0 for logic */
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, /* contains DRA, needs to be 0 for logic */
  81. 0, 0, 0, 0, 0, 0, 0
  82. };
  83. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  84. CONTROL_MPAGE,
  85. CONTROL_MPAGE_LEN - 2,
  86. 2, /* DSENSE=0, GLTSD=1 */
  87. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  88. 0, 0, 0, 0, 0xff, 0xff,
  89. 0, 30 /* extended self test time, see 05-359r1 */
  90. };
  91. /*
  92. * libata transport template. libata doesn't do real transport stuff.
  93. * It just needs the eh_timed_out hook.
  94. */
  95. struct scsi_transport_template ata_scsi_transport_template = {
  96. .eh_strategy_handler = ata_scsi_error,
  97. .eh_timed_out = ata_scsi_timed_out,
  98. .user_scan = ata_scsi_user_scan,
  99. };
  100. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
  101. void (*done)(struct scsi_cmnd *))
  102. {
  103. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  104. /* "Invalid field in cbd" */
  105. done(cmd);
  106. }
  107. /**
  108. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  109. * @sdev: SCSI device for which BIOS geometry is to be determined
  110. * @bdev: block device associated with @sdev
  111. * @capacity: capacity of SCSI device
  112. * @geom: location to which geometry will be output
  113. *
  114. * Generic bios head/sector/cylinder calculator
  115. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  116. * mapping. Some situations may arise where the disk is not
  117. * bootable if this is not used.
  118. *
  119. * LOCKING:
  120. * Defined by the SCSI layer. We don't really care.
  121. *
  122. * RETURNS:
  123. * Zero.
  124. */
  125. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  126. sector_t capacity, int geom[])
  127. {
  128. geom[0] = 255;
  129. geom[1] = 63;
  130. sector_div(capacity, 255*63);
  131. geom[2] = capacity;
  132. return 0;
  133. }
  134. /**
  135. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  136. * @scsidev: Device to which we are issuing command
  137. * @arg: User provided data for issuing command
  138. *
  139. * LOCKING:
  140. * Defined by the SCSI layer. We don't really care.
  141. *
  142. * RETURNS:
  143. * Zero on success, negative errno on error.
  144. */
  145. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  146. {
  147. int rc = 0;
  148. u8 scsi_cmd[MAX_COMMAND_SIZE];
  149. u8 args[4], *argbuf = NULL;
  150. int argsize = 0;
  151. struct scsi_sense_hdr sshdr;
  152. enum dma_data_direction data_dir;
  153. if (arg == NULL)
  154. return -EINVAL;
  155. if (copy_from_user(args, arg, sizeof(args)))
  156. return -EFAULT;
  157. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  158. if (args[3]) {
  159. argsize = SECTOR_SIZE * args[3];
  160. argbuf = kmalloc(argsize, GFP_KERNEL);
  161. if (argbuf == NULL) {
  162. rc = -ENOMEM;
  163. goto error;
  164. }
  165. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  166. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  167. block count in sector count field */
  168. data_dir = DMA_FROM_DEVICE;
  169. } else {
  170. scsi_cmd[1] = (3 << 1); /* Non-data */
  171. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  172. data_dir = DMA_NONE;
  173. }
  174. scsi_cmd[0] = ATA_16;
  175. scsi_cmd[4] = args[2];
  176. if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
  177. scsi_cmd[6] = args[3];
  178. scsi_cmd[8] = args[1];
  179. scsi_cmd[10] = 0x4f;
  180. scsi_cmd[12] = 0xc2;
  181. } else {
  182. scsi_cmd[6] = args[1];
  183. }
  184. scsi_cmd[14] = args[0];
  185. /* Good values for timeout and retries? Values below
  186. from scsi_ioctl_send_command() for default case... */
  187. if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
  188. &sshdr, (10*HZ), 5)) {
  189. rc = -EIO;
  190. goto error;
  191. }
  192. /* Need code to retrieve data from check condition? */
  193. if ((argbuf)
  194. && copy_to_user(arg + sizeof(args), argbuf, argsize))
  195. rc = -EFAULT;
  196. error:
  197. if (argbuf)
  198. kfree(argbuf);
  199. return rc;
  200. }
  201. /**
  202. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  203. * @scsidev: Device to which we are issuing command
  204. * @arg: User provided data for issuing command
  205. *
  206. * LOCKING:
  207. * Defined by the SCSI layer. We don't really care.
  208. *
  209. * RETURNS:
  210. * Zero on success, negative errno on error.
  211. */
  212. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  213. {
  214. int rc = 0;
  215. u8 scsi_cmd[MAX_COMMAND_SIZE];
  216. u8 args[7];
  217. struct scsi_sense_hdr sshdr;
  218. if (arg == NULL)
  219. return -EINVAL;
  220. if (copy_from_user(args, arg, sizeof(args)))
  221. return -EFAULT;
  222. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  223. scsi_cmd[0] = ATA_16;
  224. scsi_cmd[1] = (3 << 1); /* Non-data */
  225. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  226. scsi_cmd[4] = args[1];
  227. scsi_cmd[6] = args[2];
  228. scsi_cmd[8] = args[3];
  229. scsi_cmd[10] = args[4];
  230. scsi_cmd[12] = args[5];
  231. scsi_cmd[14] = args[0];
  232. /* Good values for timeout and retries? Values below
  233. from scsi_ioctl_send_command() for default case... */
  234. if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
  235. (10*HZ), 5))
  236. rc = -EIO;
  237. /* Need code to retrieve data from check condition? */
  238. return rc;
  239. }
  240. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  241. {
  242. int val = -EINVAL, rc = -EINVAL;
  243. switch (cmd) {
  244. case ATA_IOC_GET_IO32:
  245. val = 0;
  246. if (copy_to_user(arg, &val, 1))
  247. return -EFAULT;
  248. return 0;
  249. case ATA_IOC_SET_IO32:
  250. val = (unsigned long) arg;
  251. if (val != 0)
  252. return -EINVAL;
  253. return 0;
  254. case HDIO_DRIVE_CMD:
  255. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  256. return -EACCES;
  257. return ata_cmd_ioctl(scsidev, arg);
  258. case HDIO_DRIVE_TASK:
  259. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  260. return -EACCES;
  261. return ata_task_ioctl(scsidev, arg);
  262. default:
  263. rc = -ENOTTY;
  264. break;
  265. }
  266. return rc;
  267. }
  268. /**
  269. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  270. * @dev: ATA device to which the new command is attached
  271. * @cmd: SCSI command that originated this ATA command
  272. * @done: SCSI command completion function
  273. *
  274. * Obtain a reference to an unused ata_queued_cmd structure,
  275. * which is the basic libata structure representing a single
  276. * ATA command sent to the hardware.
  277. *
  278. * If a command was available, fill in the SCSI-specific
  279. * portions of the structure with information on the
  280. * current command.
  281. *
  282. * LOCKING:
  283. * spin_lock_irqsave(host_set lock)
  284. *
  285. * RETURNS:
  286. * Command allocated, or %NULL if none available.
  287. */
  288. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
  289. struct scsi_cmnd *cmd,
  290. void (*done)(struct scsi_cmnd *))
  291. {
  292. struct ata_queued_cmd *qc;
  293. qc = ata_qc_new_init(dev);
  294. if (qc) {
  295. qc->scsicmd = cmd;
  296. qc->scsidone = done;
  297. if (cmd->use_sg) {
  298. qc->__sg = (struct scatterlist *) cmd->request_buffer;
  299. qc->n_elem = cmd->use_sg;
  300. } else {
  301. qc->__sg = &qc->sgent;
  302. qc->n_elem = 1;
  303. }
  304. } else {
  305. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  306. done(cmd);
  307. }
  308. return qc;
  309. }
  310. /**
  311. * ata_dump_status - user friendly display of error info
  312. * @id: id of the port in question
  313. * @tf: ptr to filled out taskfile
  314. *
  315. * Decode and dump the ATA error/status registers for the user so
  316. * that they have some idea what really happened at the non
  317. * make-believe layer.
  318. *
  319. * LOCKING:
  320. * inherited from caller
  321. */
  322. void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  323. {
  324. u8 stat = tf->command, err = tf->feature;
  325. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  326. if (stat & ATA_BUSY) {
  327. printk("Busy }\n"); /* Data is not valid in this case */
  328. } else {
  329. if (stat & 0x40) printk("DriveReady ");
  330. if (stat & 0x20) printk("DeviceFault ");
  331. if (stat & 0x10) printk("SeekComplete ");
  332. if (stat & 0x08) printk("DataRequest ");
  333. if (stat & 0x04) printk("CorrectedError ");
  334. if (stat & 0x02) printk("Index ");
  335. if (stat & 0x01) printk("Error ");
  336. printk("}\n");
  337. if (err) {
  338. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  339. if (err & 0x04) printk("DriveStatusError ");
  340. if (err & 0x80) {
  341. if (err & 0x04) printk("BadCRC ");
  342. else printk("Sector ");
  343. }
  344. if (err & 0x40) printk("UncorrectableError ");
  345. if (err & 0x10) printk("SectorIdNotFound ");
  346. if (err & 0x02) printk("TrackZeroNotFound ");
  347. if (err & 0x01) printk("AddrMarkNotFound ");
  348. printk("}\n");
  349. }
  350. }
  351. }
  352. int ata_scsi_device_resume(struct scsi_device *sdev)
  353. {
  354. struct ata_port *ap = ata_shost_to_port(sdev->host);
  355. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  356. return ata_device_resume(dev);
  357. }
  358. int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
  359. {
  360. struct ata_port *ap = ata_shost_to_port(sdev->host);
  361. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  362. return ata_device_suspend(dev, state);
  363. }
  364. /**
  365. * ata_to_sense_error - convert ATA error to SCSI error
  366. * @id: ATA device number
  367. * @drv_stat: value contained in ATA status register
  368. * @drv_err: value contained in ATA error register
  369. * @sk: the sense key we'll fill out
  370. * @asc: the additional sense code we'll fill out
  371. * @ascq: the additional sense code qualifier we'll fill out
  372. * @verbose: be verbose
  373. *
  374. * Converts an ATA error into a SCSI error. Fill out pointers to
  375. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  376. * format sense blocks.
  377. *
  378. * LOCKING:
  379. * spin_lock_irqsave(host_set lock)
  380. */
  381. void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
  382. u8 *ascq, int verbose)
  383. {
  384. int i;
  385. /* Based on the 3ware driver translation table */
  386. static const unsigned char sense_table[][4] = {
  387. /* BBD|ECC|ID|MAR */
  388. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  389. /* BBD|ECC|ID */
  390. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  391. /* ECC|MC|MARK */
  392. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  393. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  394. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  395. /* MC|ID|ABRT|TRK0|MARK */
  396. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  397. /* MCR|MARK */
  398. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  399. /* Bad address mark */
  400. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  401. /* TRK0 */
  402. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  403. /* Abort & !ICRC */
  404. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  405. /* Media change request */
  406. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  407. /* SRV */
  408. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  409. /* Media change */
  410. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  411. /* ECC */
  412. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  413. /* BBD - block marked bad */
  414. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  415. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  416. };
  417. static const unsigned char stat_table[][4] = {
  418. /* Must be first because BUSY means no other bits valid */
  419. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  420. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  421. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  422. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  423. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  424. };
  425. /*
  426. * Is this an error we can process/parse
  427. */
  428. if (drv_stat & ATA_BUSY) {
  429. drv_err = 0; /* Ignore the err bits, they're invalid */
  430. }
  431. if (drv_err) {
  432. /* Look for drv_err */
  433. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  434. /* Look for best matches first */
  435. if ((sense_table[i][0] & drv_err) ==
  436. sense_table[i][0]) {
  437. *sk = sense_table[i][1];
  438. *asc = sense_table[i][2];
  439. *ascq = sense_table[i][3];
  440. goto translate_done;
  441. }
  442. }
  443. /* No immediate match */
  444. if (verbose)
  445. printk(KERN_WARNING "ata%u: no sense translation for "
  446. "error 0x%02x\n", id, drv_err);
  447. }
  448. /* Fall back to interpreting status bits */
  449. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  450. if (stat_table[i][0] & drv_stat) {
  451. *sk = stat_table[i][1];
  452. *asc = stat_table[i][2];
  453. *ascq = stat_table[i][3];
  454. goto translate_done;
  455. }
  456. }
  457. /* No error? Undecoded? */
  458. if (verbose)
  459. printk(KERN_WARNING "ata%u: no sense translation for "
  460. "status: 0x%02x\n", id, drv_stat);
  461. /* We need a sensible error return here, which is tricky, and one
  462. that won't cause people to do things like return a disk wrongly */
  463. *sk = ABORTED_COMMAND;
  464. *asc = 0x00;
  465. *ascq = 0x00;
  466. translate_done:
  467. if (verbose)
  468. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
  469. "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
  470. id, drv_stat, drv_err, *sk, *asc, *ascq);
  471. return;
  472. }
  473. /*
  474. * ata_gen_ata_desc_sense - Generate check condition sense block.
  475. * @qc: Command that completed.
  476. *
  477. * This function is specific to the ATA descriptor format sense
  478. * block specified for the ATA pass through commands. Regardless
  479. * of whether the command errored or not, return a sense
  480. * block. Copy all controller registers into the sense
  481. * block. Clear sense key, ASC & ASCQ if there is no error.
  482. *
  483. * LOCKING:
  484. * spin_lock_irqsave(host_set lock)
  485. */
  486. void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
  487. {
  488. struct scsi_cmnd *cmd = qc->scsicmd;
  489. struct ata_taskfile *tf = &qc->result_tf;
  490. unsigned char *sb = cmd->sense_buffer;
  491. unsigned char *desc = sb + 8;
  492. int verbose = qc->ap->ops->error_handler == NULL;
  493. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  494. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  495. /*
  496. * Use ata_to_sense_error() to map status register bits
  497. * onto sense key, asc & ascq.
  498. */
  499. if (qc->err_mask ||
  500. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  501. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  502. &sb[1], &sb[2], &sb[3], verbose);
  503. sb[1] &= 0x0f;
  504. }
  505. /*
  506. * Sense data is current and format is descriptor.
  507. */
  508. sb[0] = 0x72;
  509. desc[0] = 0x09;
  510. /*
  511. * Set length of additional sense data.
  512. * Since we only populate descriptor 0, the total
  513. * length is the same (fixed) length as descriptor 0.
  514. */
  515. desc[1] = sb[7] = 14;
  516. /*
  517. * Copy registers into sense buffer.
  518. */
  519. desc[2] = 0x00;
  520. desc[3] = tf->feature; /* == error reg */
  521. desc[5] = tf->nsect;
  522. desc[7] = tf->lbal;
  523. desc[9] = tf->lbam;
  524. desc[11] = tf->lbah;
  525. desc[12] = tf->device;
  526. desc[13] = tf->command; /* == status reg */
  527. /*
  528. * Fill in Extend bit, and the high order bytes
  529. * if applicable.
  530. */
  531. if (tf->flags & ATA_TFLAG_LBA48) {
  532. desc[2] |= 0x01;
  533. desc[4] = tf->hob_nsect;
  534. desc[6] = tf->hob_lbal;
  535. desc[8] = tf->hob_lbam;
  536. desc[10] = tf->hob_lbah;
  537. }
  538. }
  539. /**
  540. * ata_gen_fixed_sense - generate a SCSI fixed sense block
  541. * @qc: Command that we are erroring out
  542. *
  543. * Leverage ata_to_sense_error() to give us the codes. Fit our
  544. * LBA in here if there's room.
  545. *
  546. * LOCKING:
  547. * inherited from caller
  548. */
  549. void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
  550. {
  551. struct scsi_cmnd *cmd = qc->scsicmd;
  552. struct ata_taskfile *tf = &qc->result_tf;
  553. unsigned char *sb = cmd->sense_buffer;
  554. int verbose = qc->ap->ops->error_handler == NULL;
  555. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  556. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  557. /*
  558. * Use ata_to_sense_error() to map status register bits
  559. * onto sense key, asc & ascq.
  560. */
  561. if (qc->err_mask ||
  562. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  563. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  564. &sb[2], &sb[12], &sb[13], verbose);
  565. sb[2] &= 0x0f;
  566. }
  567. sb[0] = 0x70;
  568. sb[7] = 0x0a;
  569. if (tf->flags & ATA_TFLAG_LBA48) {
  570. /* TODO: find solution for LBA48 descriptors */
  571. }
  572. else if (tf->flags & ATA_TFLAG_LBA) {
  573. /* A small (28b) LBA will fit in the 32b info field */
  574. sb[0] |= 0x80; /* set valid bit */
  575. sb[3] = tf->device & 0x0f;
  576. sb[4] = tf->lbah;
  577. sb[5] = tf->lbam;
  578. sb[6] = tf->lbal;
  579. }
  580. else {
  581. /* TODO: C/H/S */
  582. }
  583. }
  584. static void ata_scsi_sdev_config(struct scsi_device *sdev)
  585. {
  586. sdev->use_10_for_rw = 1;
  587. sdev->use_10_for_ms = 1;
  588. }
  589. static void ata_scsi_dev_config(struct scsi_device *sdev,
  590. struct ata_device *dev)
  591. {
  592. unsigned int max_sectors;
  593. /* TODO: 2048 is an arbitrary number, not the
  594. * hardware maximum. This should be increased to
  595. * 65534 when Jens Axboe's patch for dynamically
  596. * determining max_sectors is merged.
  597. */
  598. max_sectors = ATA_MAX_SECTORS;
  599. if (dev->flags & ATA_DFLAG_LBA48)
  600. max_sectors = ATA_MAX_SECTORS_LBA48;
  601. if (dev->max_sectors)
  602. max_sectors = dev->max_sectors;
  603. blk_queue_max_sectors(sdev->request_queue, max_sectors);
  604. /*
  605. * SATA DMA transfers must be multiples of 4 byte, so
  606. * we need to pad ATAPI transfers using an extra sg.
  607. * Decrement max hw segments accordingly.
  608. */
  609. if (dev->class == ATA_DEV_ATAPI) {
  610. request_queue_t *q = sdev->request_queue;
  611. blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
  612. }
  613. if (dev->flags & ATA_DFLAG_NCQ) {
  614. int depth;
  615. depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  616. depth = min(ATA_MAX_QUEUE - 1, depth);
  617. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  618. }
  619. }
  620. /**
  621. * ata_scsi_slave_config - Set SCSI device attributes
  622. * @sdev: SCSI device to examine
  623. *
  624. * This is called before we actually start reading
  625. * and writing to the device, to configure certain
  626. * SCSI mid-layer behaviors.
  627. *
  628. * LOCKING:
  629. * Defined by SCSI layer. We don't really care.
  630. */
  631. int ata_scsi_slave_config(struct scsi_device *sdev)
  632. {
  633. struct ata_port *ap = ata_shost_to_port(sdev->host);
  634. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  635. ata_scsi_sdev_config(sdev);
  636. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  637. if (dev)
  638. ata_scsi_dev_config(sdev, dev);
  639. return 0; /* scsi layer doesn't check return value, sigh */
  640. }
  641. /**
  642. * ata_scsi_slave_destroy - SCSI device is about to be destroyed
  643. * @sdev: SCSI device to be destroyed
  644. *
  645. * @sdev is about to be destroyed for hot/warm unplugging. If
  646. * this unplugging was initiated by libata as indicated by NULL
  647. * dev->sdev, this function doesn't have to do anything.
  648. * Otherwise, SCSI layer initiated warm-unplug is in progress.
  649. * Clear dev->sdev, schedule the device for ATA detach and invoke
  650. * EH.
  651. *
  652. * LOCKING:
  653. * Defined by SCSI layer. We don't really care.
  654. */
  655. void ata_scsi_slave_destroy(struct scsi_device *sdev)
  656. {
  657. struct ata_port *ap = ata_shost_to_port(sdev->host);
  658. unsigned long flags;
  659. struct ata_device *dev;
  660. if (!ap->ops->error_handler)
  661. return;
  662. spin_lock_irqsave(&ap->host_set->lock, flags);
  663. dev = __ata_scsi_find_dev(ap, sdev);
  664. if (dev && dev->sdev) {
  665. /* SCSI device already in CANCEL state, no need to offline it */
  666. dev->sdev = NULL;
  667. dev->flags |= ATA_DFLAG_DETACH;
  668. ata_port_schedule_eh(ap);
  669. }
  670. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  671. }
  672. /**
  673. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  674. * @sdev: SCSI device to configure queue depth for
  675. * @queue_depth: new queue depth
  676. *
  677. * This is libata standard hostt->change_queue_depth callback.
  678. * SCSI will call into this callback when user tries to set queue
  679. * depth via sysfs.
  680. *
  681. * LOCKING:
  682. * SCSI layer (we don't care)
  683. *
  684. * RETURNS:
  685. * Newly configured queue depth.
  686. */
  687. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  688. {
  689. struct ata_port *ap = ata_shost_to_port(sdev->host);
  690. struct ata_device *dev;
  691. int max_depth;
  692. if (queue_depth < 1)
  693. return sdev->queue_depth;
  694. dev = ata_scsi_find_dev(ap, sdev);
  695. if (!dev || !ata_dev_enabled(dev))
  696. return sdev->queue_depth;
  697. max_depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  698. max_depth = min(ATA_MAX_QUEUE - 1, max_depth);
  699. if (queue_depth > max_depth)
  700. queue_depth = max_depth;
  701. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
  702. return queue_depth;
  703. }
  704. /**
  705. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  706. * @qc: Storage for translated ATA taskfile
  707. * @scsicmd: SCSI command to translate
  708. *
  709. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  710. * (to start). Perhaps these commands should be preceded by
  711. * CHECK POWER MODE to see what power mode the device is already in.
  712. * [See SAT revision 5 at www.t10.org]
  713. *
  714. * LOCKING:
  715. * spin_lock_irqsave(host_set lock)
  716. *
  717. * RETURNS:
  718. * Zero on success, non-zero on error.
  719. */
  720. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
  721. const u8 *scsicmd)
  722. {
  723. struct ata_taskfile *tf = &qc->tf;
  724. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  725. tf->protocol = ATA_PROT_NODATA;
  726. if (scsicmd[1] & 0x1) {
  727. ; /* ignore IMMED bit, violates sat-r05 */
  728. }
  729. if (scsicmd[4] & 0x2)
  730. goto invalid_fld; /* LOEJ bit set not supported */
  731. if (((scsicmd[4] >> 4) & 0xf) != 0)
  732. goto invalid_fld; /* power conditions not supported */
  733. if (scsicmd[4] & 0x1) {
  734. tf->nsect = 1; /* 1 sector, lba=0 */
  735. if (qc->dev->flags & ATA_DFLAG_LBA) {
  736. tf->flags |= ATA_TFLAG_LBA;
  737. tf->lbah = 0x0;
  738. tf->lbam = 0x0;
  739. tf->lbal = 0x0;
  740. tf->device |= ATA_LBA;
  741. } else {
  742. /* CHS */
  743. tf->lbal = 0x1; /* sect */
  744. tf->lbam = 0x0; /* cyl low */
  745. tf->lbah = 0x0; /* cyl high */
  746. }
  747. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  748. } else {
  749. tf->nsect = 0; /* time period value (0 implies now) */
  750. tf->command = ATA_CMD_STANDBY;
  751. /* Consider: ATA STANDBY IMMEDIATE command */
  752. }
  753. /*
  754. * Standby and Idle condition timers could be implemented but that
  755. * would require libata to implement the Power condition mode page
  756. * and allow the user to change it. Changing mode pages requires
  757. * MODE SELECT to be implemented.
  758. */
  759. return 0;
  760. invalid_fld:
  761. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  762. /* "Invalid field in cbd" */
  763. return 1;
  764. }
  765. /**
  766. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  767. * @qc: Storage for translated ATA taskfile
  768. * @scsicmd: SCSI command to translate (ignored)
  769. *
  770. * Sets up an ATA taskfile to issue FLUSH CACHE or
  771. * FLUSH CACHE EXT.
  772. *
  773. * LOCKING:
  774. * spin_lock_irqsave(host_set lock)
  775. *
  776. * RETURNS:
  777. * Zero on success, non-zero on error.
  778. */
  779. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  780. {
  781. struct ata_taskfile *tf = &qc->tf;
  782. tf->flags |= ATA_TFLAG_DEVICE;
  783. tf->protocol = ATA_PROT_NODATA;
  784. if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
  785. (ata_id_has_flush_ext(qc->dev->id)))
  786. tf->command = ATA_CMD_FLUSH_EXT;
  787. else
  788. tf->command = ATA_CMD_FLUSH;
  789. return 0;
  790. }
  791. /**
  792. * scsi_6_lba_len - Get LBA and transfer length
  793. * @scsicmd: SCSI command to translate
  794. *
  795. * Calculate LBA and transfer length for 6-byte commands.
  796. *
  797. * RETURNS:
  798. * @plba: the LBA
  799. * @plen: the transfer length
  800. */
  801. static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  802. {
  803. u64 lba = 0;
  804. u32 len = 0;
  805. VPRINTK("six-byte command\n");
  806. lba |= ((u64)scsicmd[2]) << 8;
  807. lba |= ((u64)scsicmd[3]);
  808. len |= ((u32)scsicmd[4]);
  809. *plba = lba;
  810. *plen = len;
  811. }
  812. /**
  813. * scsi_10_lba_len - Get LBA and transfer length
  814. * @scsicmd: SCSI command to translate
  815. *
  816. * Calculate LBA and transfer length for 10-byte commands.
  817. *
  818. * RETURNS:
  819. * @plba: the LBA
  820. * @plen: the transfer length
  821. */
  822. static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  823. {
  824. u64 lba = 0;
  825. u32 len = 0;
  826. VPRINTK("ten-byte command\n");
  827. lba |= ((u64)scsicmd[2]) << 24;
  828. lba |= ((u64)scsicmd[3]) << 16;
  829. lba |= ((u64)scsicmd[4]) << 8;
  830. lba |= ((u64)scsicmd[5]);
  831. len |= ((u32)scsicmd[7]) << 8;
  832. len |= ((u32)scsicmd[8]);
  833. *plba = lba;
  834. *plen = len;
  835. }
  836. /**
  837. * scsi_16_lba_len - Get LBA and transfer length
  838. * @scsicmd: SCSI command to translate
  839. *
  840. * Calculate LBA and transfer length for 16-byte commands.
  841. *
  842. * RETURNS:
  843. * @plba: the LBA
  844. * @plen: the transfer length
  845. */
  846. static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  847. {
  848. u64 lba = 0;
  849. u32 len = 0;
  850. VPRINTK("sixteen-byte command\n");
  851. lba |= ((u64)scsicmd[2]) << 56;
  852. lba |= ((u64)scsicmd[3]) << 48;
  853. lba |= ((u64)scsicmd[4]) << 40;
  854. lba |= ((u64)scsicmd[5]) << 32;
  855. lba |= ((u64)scsicmd[6]) << 24;
  856. lba |= ((u64)scsicmd[7]) << 16;
  857. lba |= ((u64)scsicmd[8]) << 8;
  858. lba |= ((u64)scsicmd[9]);
  859. len |= ((u32)scsicmd[10]) << 24;
  860. len |= ((u32)scsicmd[11]) << 16;
  861. len |= ((u32)scsicmd[12]) << 8;
  862. len |= ((u32)scsicmd[13]);
  863. *plba = lba;
  864. *plen = len;
  865. }
  866. /**
  867. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  868. * @qc: Storage for translated ATA taskfile
  869. * @scsicmd: SCSI command to translate
  870. *
  871. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  872. *
  873. * LOCKING:
  874. * spin_lock_irqsave(host_set lock)
  875. *
  876. * RETURNS:
  877. * Zero on success, non-zero on error.
  878. */
  879. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  880. {
  881. struct ata_taskfile *tf = &qc->tf;
  882. struct ata_device *dev = qc->dev;
  883. u64 dev_sectors = qc->dev->n_sectors;
  884. u64 block;
  885. u32 n_block;
  886. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  887. tf->protocol = ATA_PROT_NODATA;
  888. if (scsicmd[0] == VERIFY)
  889. scsi_10_lba_len(scsicmd, &block, &n_block);
  890. else if (scsicmd[0] == VERIFY_16)
  891. scsi_16_lba_len(scsicmd, &block, &n_block);
  892. else
  893. goto invalid_fld;
  894. if (!n_block)
  895. goto nothing_to_do;
  896. if (block >= dev_sectors)
  897. goto out_of_range;
  898. if ((block + n_block) > dev_sectors)
  899. goto out_of_range;
  900. if (dev->flags & ATA_DFLAG_LBA) {
  901. tf->flags |= ATA_TFLAG_LBA;
  902. if (lba_28_ok(block, n_block)) {
  903. /* use LBA28 */
  904. tf->command = ATA_CMD_VERIFY;
  905. tf->device |= (block >> 24) & 0xf;
  906. } else if (lba_48_ok(block, n_block)) {
  907. if (!(dev->flags & ATA_DFLAG_LBA48))
  908. goto out_of_range;
  909. /* use LBA48 */
  910. tf->flags |= ATA_TFLAG_LBA48;
  911. tf->command = ATA_CMD_VERIFY_EXT;
  912. tf->hob_nsect = (n_block >> 8) & 0xff;
  913. tf->hob_lbah = (block >> 40) & 0xff;
  914. tf->hob_lbam = (block >> 32) & 0xff;
  915. tf->hob_lbal = (block >> 24) & 0xff;
  916. } else
  917. /* request too large even for LBA48 */
  918. goto out_of_range;
  919. tf->nsect = n_block & 0xff;
  920. tf->lbah = (block >> 16) & 0xff;
  921. tf->lbam = (block >> 8) & 0xff;
  922. tf->lbal = block & 0xff;
  923. tf->device |= ATA_LBA;
  924. } else {
  925. /* CHS */
  926. u32 sect, head, cyl, track;
  927. if (!lba_28_ok(block, n_block))
  928. goto out_of_range;
  929. /* Convert LBA to CHS */
  930. track = (u32)block / dev->sectors;
  931. cyl = track / dev->heads;
  932. head = track % dev->heads;
  933. sect = (u32)block % dev->sectors + 1;
  934. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  935. (u32)block, track, cyl, head, sect);
  936. /* Check whether the converted CHS can fit.
  937. Cylinder: 0-65535
  938. Head: 0-15
  939. Sector: 1-255*/
  940. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  941. goto out_of_range;
  942. tf->command = ATA_CMD_VERIFY;
  943. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  944. tf->lbal = sect;
  945. tf->lbam = cyl;
  946. tf->lbah = cyl >> 8;
  947. tf->device |= head;
  948. }
  949. return 0;
  950. invalid_fld:
  951. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  952. /* "Invalid field in cbd" */
  953. return 1;
  954. out_of_range:
  955. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  956. /* "Logical Block Address out of range" */
  957. return 1;
  958. nothing_to_do:
  959. qc->scsicmd->result = SAM_STAT_GOOD;
  960. return 1;
  961. }
  962. /**
  963. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  964. * @qc: Storage for translated ATA taskfile
  965. * @scsicmd: SCSI command to translate
  966. *
  967. * Converts any of six SCSI read/write commands into the
  968. * ATA counterpart, including starting sector (LBA),
  969. * sector count, and taking into account the device's LBA48
  970. * support.
  971. *
  972. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  973. * %WRITE_16 are currently supported.
  974. *
  975. * LOCKING:
  976. * spin_lock_irqsave(host_set lock)
  977. *
  978. * RETURNS:
  979. * Zero on success, non-zero on error.
  980. */
  981. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  982. {
  983. struct ata_taskfile *tf = &qc->tf;
  984. struct ata_device *dev = qc->dev;
  985. u64 block;
  986. u32 n_block;
  987. qc->flags |= ATA_QCFLAG_IO;
  988. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  989. if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
  990. scsicmd[0] == WRITE_16)
  991. tf->flags |= ATA_TFLAG_WRITE;
  992. /* Calculate the SCSI LBA, transfer length and FUA. */
  993. switch (scsicmd[0]) {
  994. case READ_10:
  995. case WRITE_10:
  996. scsi_10_lba_len(scsicmd, &block, &n_block);
  997. if (unlikely(scsicmd[1] & (1 << 3)))
  998. tf->flags |= ATA_TFLAG_FUA;
  999. break;
  1000. case READ_6:
  1001. case WRITE_6:
  1002. scsi_6_lba_len(scsicmd, &block, &n_block);
  1003. /* for 6-byte r/w commands, transfer length 0
  1004. * means 256 blocks of data, not 0 block.
  1005. */
  1006. if (!n_block)
  1007. n_block = 256;
  1008. break;
  1009. case READ_16:
  1010. case WRITE_16:
  1011. scsi_16_lba_len(scsicmd, &block, &n_block);
  1012. if (unlikely(scsicmd[1] & (1 << 3)))
  1013. tf->flags |= ATA_TFLAG_FUA;
  1014. break;
  1015. default:
  1016. DPRINTK("no-byte command\n");
  1017. goto invalid_fld;
  1018. }
  1019. /* Check and compose ATA command */
  1020. if (!n_block)
  1021. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1022. * length 0 means transfer 0 block of data.
  1023. * However, for ATA R/W commands, sector count 0 means
  1024. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1025. *
  1026. * WARNING: one or two older ATA drives treat 0 as 0...
  1027. */
  1028. goto nothing_to_do;
  1029. if ((dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ)) == ATA_DFLAG_NCQ) {
  1030. /* yay, NCQ */
  1031. if (!lba_48_ok(block, n_block))
  1032. goto out_of_range;
  1033. tf->protocol = ATA_PROT_NCQ;
  1034. tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1035. if (tf->flags & ATA_TFLAG_WRITE)
  1036. tf->command = ATA_CMD_FPDMA_WRITE;
  1037. else
  1038. tf->command = ATA_CMD_FPDMA_READ;
  1039. qc->nsect = n_block;
  1040. tf->nsect = qc->tag << 3;
  1041. tf->hob_feature = (n_block >> 8) & 0xff;
  1042. tf->feature = n_block & 0xff;
  1043. tf->hob_lbah = (block >> 40) & 0xff;
  1044. tf->hob_lbam = (block >> 32) & 0xff;
  1045. tf->hob_lbal = (block >> 24) & 0xff;
  1046. tf->lbah = (block >> 16) & 0xff;
  1047. tf->lbam = (block >> 8) & 0xff;
  1048. tf->lbal = block & 0xff;
  1049. tf->device = 1 << 6;
  1050. if (tf->flags & ATA_TFLAG_FUA)
  1051. tf->device |= 1 << 7;
  1052. } else if (dev->flags & ATA_DFLAG_LBA) {
  1053. tf->flags |= ATA_TFLAG_LBA;
  1054. if (lba_28_ok(block, n_block)) {
  1055. /* use LBA28 */
  1056. tf->device |= (block >> 24) & 0xf;
  1057. } else if (lba_48_ok(block, n_block)) {
  1058. if (!(dev->flags & ATA_DFLAG_LBA48))
  1059. goto out_of_range;
  1060. /* use LBA48 */
  1061. tf->flags |= ATA_TFLAG_LBA48;
  1062. tf->hob_nsect = (n_block >> 8) & 0xff;
  1063. tf->hob_lbah = (block >> 40) & 0xff;
  1064. tf->hob_lbam = (block >> 32) & 0xff;
  1065. tf->hob_lbal = (block >> 24) & 0xff;
  1066. } else
  1067. /* request too large even for LBA48 */
  1068. goto out_of_range;
  1069. if (unlikely(ata_rwcmd_protocol(qc) < 0))
  1070. goto invalid_fld;
  1071. qc->nsect = n_block;
  1072. tf->nsect = n_block & 0xff;
  1073. tf->lbah = (block >> 16) & 0xff;
  1074. tf->lbam = (block >> 8) & 0xff;
  1075. tf->lbal = block & 0xff;
  1076. tf->device |= ATA_LBA;
  1077. } else {
  1078. /* CHS */
  1079. u32 sect, head, cyl, track;
  1080. /* The request -may- be too large for CHS addressing. */
  1081. if (!lba_28_ok(block, n_block))
  1082. goto out_of_range;
  1083. if (unlikely(ata_rwcmd_protocol(qc) < 0))
  1084. goto invalid_fld;
  1085. /* Convert LBA to CHS */
  1086. track = (u32)block / dev->sectors;
  1087. cyl = track / dev->heads;
  1088. head = track % dev->heads;
  1089. sect = (u32)block % dev->sectors + 1;
  1090. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1091. (u32)block, track, cyl, head, sect);
  1092. /* Check whether the converted CHS can fit.
  1093. Cylinder: 0-65535
  1094. Head: 0-15
  1095. Sector: 1-255*/
  1096. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1097. goto out_of_range;
  1098. qc->nsect = n_block;
  1099. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1100. tf->lbal = sect;
  1101. tf->lbam = cyl;
  1102. tf->lbah = cyl >> 8;
  1103. tf->device |= head;
  1104. }
  1105. return 0;
  1106. invalid_fld:
  1107. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1108. /* "Invalid field in cbd" */
  1109. return 1;
  1110. out_of_range:
  1111. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1112. /* "Logical Block Address out of range" */
  1113. return 1;
  1114. nothing_to_do:
  1115. qc->scsicmd->result = SAM_STAT_GOOD;
  1116. return 1;
  1117. }
  1118. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1119. {
  1120. struct scsi_cmnd *cmd = qc->scsicmd;
  1121. u8 *cdb = cmd->cmnd;
  1122. int need_sense = (qc->err_mask != 0);
  1123. /* For ATA pass thru (SAT) commands, generate a sense block if
  1124. * user mandated it or if there's an error. Note that if we
  1125. * generate because the user forced us to, a check condition
  1126. * is generated and the ATA register values are returned
  1127. * whether the command completed successfully or not. If there
  1128. * was no error, SK, ASC and ASCQ will all be zero.
  1129. */
  1130. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1131. ((cdb[2] & 0x20) || need_sense)) {
  1132. ata_gen_ata_desc_sense(qc);
  1133. } else {
  1134. if (!need_sense) {
  1135. cmd->result = SAM_STAT_GOOD;
  1136. } else {
  1137. /* TODO: decide which descriptor format to use
  1138. * for 48b LBA devices and call that here
  1139. * instead of the fixed desc, which is only
  1140. * good for smaller LBA (and maybe CHS?)
  1141. * devices.
  1142. */
  1143. ata_gen_fixed_sense(qc);
  1144. }
  1145. }
  1146. if (need_sense && !qc->ap->ops->error_handler)
  1147. ata_dump_status(qc->ap->id, &qc->result_tf);
  1148. qc->scsidone(cmd);
  1149. ata_qc_free(qc);
  1150. }
  1151. /**
  1152. * ata_scmd_need_defer - Check whether we need to defer scmd
  1153. * @dev: ATA device to which the command is addressed
  1154. * @is_io: Is the command IO (and thus possibly NCQ)?
  1155. *
  1156. * NCQ and non-NCQ commands cannot run together. As upper layer
  1157. * only knows the queue depth, we are responsible for maintaining
  1158. * exclusion. This function checks whether a new command can be
  1159. * issued to @dev.
  1160. *
  1161. * LOCKING:
  1162. * spin_lock_irqsave(host_set lock)
  1163. *
  1164. * RETURNS:
  1165. * 1 if deferring is needed, 0 otherwise.
  1166. */
  1167. static int ata_scmd_need_defer(struct ata_device *dev, int is_io)
  1168. {
  1169. struct ata_port *ap = dev->ap;
  1170. if (!(dev->flags & ATA_DFLAG_NCQ))
  1171. return 0;
  1172. if (is_io) {
  1173. if (!ata_tag_valid(ap->active_tag))
  1174. return 0;
  1175. } else {
  1176. if (!ata_tag_valid(ap->active_tag) && !ap->sactive)
  1177. return 0;
  1178. }
  1179. return 1;
  1180. }
  1181. /**
  1182. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1183. * @dev: ATA device to which the command is addressed
  1184. * @cmd: SCSI command to execute
  1185. * @done: SCSI command completion function
  1186. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1187. *
  1188. * Our ->queuecommand() function has decided that the SCSI
  1189. * command issued can be directly translated into an ATA
  1190. * command, rather than handled internally.
  1191. *
  1192. * This function sets up an ata_queued_cmd structure for the
  1193. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1194. *
  1195. * The xlat_func argument (actor) returns 0 if ready to execute
  1196. * ATA command, else 1 to finish translation. If 1 is returned
  1197. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1198. * to be set reflecting an error condition or clean (early)
  1199. * termination.
  1200. *
  1201. * LOCKING:
  1202. * spin_lock_irqsave(host_set lock)
  1203. *
  1204. * RETURNS:
  1205. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1206. * needs to be deferred.
  1207. */
  1208. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1209. void (*done)(struct scsi_cmnd *),
  1210. ata_xlat_func_t xlat_func)
  1211. {
  1212. struct ata_queued_cmd *qc;
  1213. u8 *scsicmd = cmd->cmnd;
  1214. int is_io = xlat_func == ata_scsi_rw_xlat;
  1215. VPRINTK("ENTER\n");
  1216. if (unlikely(ata_scmd_need_defer(dev, is_io)))
  1217. goto defer;
  1218. qc = ata_scsi_qc_new(dev, cmd, done);
  1219. if (!qc)
  1220. goto err_mem;
  1221. /* data is present; dma-map it */
  1222. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1223. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1224. if (unlikely(cmd->request_bufflen < 1)) {
  1225. ata_dev_printk(dev, KERN_WARNING,
  1226. "WARNING: zero len r/w req\n");
  1227. goto err_did;
  1228. }
  1229. if (cmd->use_sg)
  1230. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  1231. else
  1232. ata_sg_init_one(qc, cmd->request_buffer,
  1233. cmd->request_bufflen);
  1234. qc->dma_dir = cmd->sc_data_direction;
  1235. }
  1236. qc->complete_fn = ata_scsi_qc_complete;
  1237. if (xlat_func(qc, scsicmd))
  1238. goto early_finish;
  1239. /* select device, send command to hardware */
  1240. ata_qc_issue(qc);
  1241. VPRINTK("EXIT\n");
  1242. return 0;
  1243. early_finish:
  1244. ata_qc_free(qc);
  1245. done(cmd);
  1246. DPRINTK("EXIT - early finish (good or error)\n");
  1247. return 0;
  1248. err_did:
  1249. ata_qc_free(qc);
  1250. err_mem:
  1251. cmd->result = (DID_ERROR << 16);
  1252. done(cmd);
  1253. DPRINTK("EXIT - internal\n");
  1254. return 0;
  1255. defer:
  1256. DPRINTK("EXIT - defer\n");
  1257. return SCSI_MLQUEUE_DEVICE_BUSY;
  1258. }
  1259. /**
  1260. * ata_scsi_rbuf_get - Map response buffer.
  1261. * @cmd: SCSI command containing buffer to be mapped.
  1262. * @buf_out: Pointer to mapped area.
  1263. *
  1264. * Maps buffer contained within SCSI command @cmd.
  1265. *
  1266. * LOCKING:
  1267. * spin_lock_irqsave(host_set lock)
  1268. *
  1269. * RETURNS:
  1270. * Length of response buffer.
  1271. */
  1272. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  1273. {
  1274. u8 *buf;
  1275. unsigned int buflen;
  1276. if (cmd->use_sg) {
  1277. struct scatterlist *sg;
  1278. sg = (struct scatterlist *) cmd->request_buffer;
  1279. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  1280. buflen = sg->length;
  1281. } else {
  1282. buf = cmd->request_buffer;
  1283. buflen = cmd->request_bufflen;
  1284. }
  1285. *buf_out = buf;
  1286. return buflen;
  1287. }
  1288. /**
  1289. * ata_scsi_rbuf_put - Unmap response buffer.
  1290. * @cmd: SCSI command containing buffer to be unmapped.
  1291. * @buf: buffer to unmap
  1292. *
  1293. * Unmaps response buffer contained within @cmd.
  1294. *
  1295. * LOCKING:
  1296. * spin_lock_irqsave(host_set lock)
  1297. */
  1298. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  1299. {
  1300. if (cmd->use_sg) {
  1301. struct scatterlist *sg;
  1302. sg = (struct scatterlist *) cmd->request_buffer;
  1303. kunmap_atomic(buf - sg->offset, KM_USER0);
  1304. }
  1305. }
  1306. /**
  1307. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1308. * @args: device IDENTIFY data / SCSI command of interest.
  1309. * @actor: Callback hook for desired SCSI command simulator
  1310. *
  1311. * Takes care of the hard work of simulating a SCSI command...
  1312. * Mapping the response buffer, calling the command's handler,
  1313. * and handling the handler's return value. This return value
  1314. * indicates whether the handler wishes the SCSI command to be
  1315. * completed successfully (0), or not (in which case cmd->result
  1316. * and sense buffer are assumed to be set).
  1317. *
  1318. * LOCKING:
  1319. * spin_lock_irqsave(host_set lock)
  1320. */
  1321. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1322. unsigned int (*actor) (struct ata_scsi_args *args,
  1323. u8 *rbuf, unsigned int buflen))
  1324. {
  1325. u8 *rbuf;
  1326. unsigned int buflen, rc;
  1327. struct scsi_cmnd *cmd = args->cmd;
  1328. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  1329. memset(rbuf, 0, buflen);
  1330. rc = actor(args, rbuf, buflen);
  1331. ata_scsi_rbuf_put(cmd, rbuf);
  1332. if (rc == 0)
  1333. cmd->result = SAM_STAT_GOOD;
  1334. args->done(cmd);
  1335. }
  1336. /**
  1337. * ata_scsiop_inq_std - Simulate INQUIRY command
  1338. * @args: device IDENTIFY data / SCSI command of interest.
  1339. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1340. * @buflen: Response buffer length.
  1341. *
  1342. * Returns standard device identification data associated
  1343. * with non-VPD INQUIRY command output.
  1344. *
  1345. * LOCKING:
  1346. * spin_lock_irqsave(host_set lock)
  1347. */
  1348. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  1349. unsigned int buflen)
  1350. {
  1351. u8 hdr[] = {
  1352. TYPE_DISK,
  1353. 0,
  1354. 0x5, /* claim SPC-3 version compatibility */
  1355. 2,
  1356. 95 - 4
  1357. };
  1358. /* set scsi removeable (RMB) bit per ata bit */
  1359. if (ata_id_removeable(args->id))
  1360. hdr[1] |= (1 << 7);
  1361. VPRINTK("ENTER\n");
  1362. memcpy(rbuf, hdr, sizeof(hdr));
  1363. if (buflen > 35) {
  1364. memcpy(&rbuf[8], "ATA ", 8);
  1365. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  1366. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  1367. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1368. memcpy(&rbuf[32], "n/a ", 4);
  1369. }
  1370. if (buflen > 63) {
  1371. const u8 versions[] = {
  1372. 0x60, /* SAM-3 (no version claimed) */
  1373. 0x03,
  1374. 0x20, /* SBC-2 (no version claimed) */
  1375. 0x02,
  1376. 0x60 /* SPC-3 (no version claimed) */
  1377. };
  1378. memcpy(rbuf + 59, versions, sizeof(versions));
  1379. }
  1380. return 0;
  1381. }
  1382. /**
  1383. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1384. * @args: device IDENTIFY data / SCSI command of interest.
  1385. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1386. * @buflen: Response buffer length.
  1387. *
  1388. * Returns list of inquiry VPD pages available.
  1389. *
  1390. * LOCKING:
  1391. * spin_lock_irqsave(host_set lock)
  1392. */
  1393. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  1394. unsigned int buflen)
  1395. {
  1396. const u8 pages[] = {
  1397. 0x00, /* page 0x00, this page */
  1398. 0x80, /* page 0x80, unit serial no page */
  1399. 0x83 /* page 0x83, device ident page */
  1400. };
  1401. rbuf[3] = sizeof(pages); /* number of supported VPD pages */
  1402. if (buflen > 6)
  1403. memcpy(rbuf + 4, pages, sizeof(pages));
  1404. return 0;
  1405. }
  1406. /**
  1407. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1408. * @args: device IDENTIFY data / SCSI command of interest.
  1409. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1410. * @buflen: Response buffer length.
  1411. *
  1412. * Returns ATA device serial number.
  1413. *
  1414. * LOCKING:
  1415. * spin_lock_irqsave(host_set lock)
  1416. */
  1417. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  1418. unsigned int buflen)
  1419. {
  1420. const u8 hdr[] = {
  1421. 0,
  1422. 0x80, /* this page code */
  1423. 0,
  1424. ATA_SERNO_LEN, /* page len */
  1425. };
  1426. memcpy(rbuf, hdr, sizeof(hdr));
  1427. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  1428. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1429. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1430. return 0;
  1431. }
  1432. /**
  1433. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1434. * @args: device IDENTIFY data / SCSI command of interest.
  1435. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1436. * @buflen: Response buffer length.
  1437. *
  1438. * Yields two logical unit device identification designators:
  1439. * - vendor specific ASCII containing the ATA serial number
  1440. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1441. * name ("ATA "), model and serial numbers.
  1442. *
  1443. * LOCKING:
  1444. * spin_lock_irqsave(host_set lock)
  1445. */
  1446. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  1447. unsigned int buflen)
  1448. {
  1449. int num;
  1450. const int sat_model_serial_desc_len = 68;
  1451. const int ata_model_byte_len = 40;
  1452. rbuf[1] = 0x83; /* this page code */
  1453. num = 4;
  1454. if (buflen > (ATA_SERNO_LEN + num + 3)) {
  1455. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1456. rbuf[num + 0] = 2;
  1457. rbuf[num + 3] = ATA_SERNO_LEN;
  1458. num += 4;
  1459. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1460. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1461. num += ATA_SERNO_LEN;
  1462. }
  1463. if (buflen > (sat_model_serial_desc_len + num + 3)) {
  1464. /* SAT defined lu model and serial numbers descriptor */
  1465. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  1466. rbuf[num + 0] = 2;
  1467. rbuf[num + 1] = 1;
  1468. rbuf[num + 3] = sat_model_serial_desc_len;
  1469. num += 4;
  1470. memcpy(rbuf + num, "ATA ", 8);
  1471. num += 8;
  1472. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1473. ATA_ID_PROD_OFS, ata_model_byte_len);
  1474. num += ata_model_byte_len;
  1475. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1476. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1477. num += ATA_SERNO_LEN;
  1478. }
  1479. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  1480. return 0;
  1481. }
  1482. /**
  1483. * ata_scsiop_noop - Command handler that simply returns success.
  1484. * @args: device IDENTIFY data / SCSI command of interest.
  1485. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1486. * @buflen: Response buffer length.
  1487. *
  1488. * No operation. Simply returns success to caller, to indicate
  1489. * that the caller should successfully complete this SCSI command.
  1490. *
  1491. * LOCKING:
  1492. * spin_lock_irqsave(host_set lock)
  1493. */
  1494. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  1495. unsigned int buflen)
  1496. {
  1497. VPRINTK("ENTER\n");
  1498. return 0;
  1499. }
  1500. /**
  1501. * ata_msense_push - Push data onto MODE SENSE data output buffer
  1502. * @ptr_io: (input/output) Location to store more output data
  1503. * @last: End of output data buffer
  1504. * @buf: Pointer to BLOB being added to output buffer
  1505. * @buflen: Length of BLOB
  1506. *
  1507. * Store MODE SENSE data on an output buffer.
  1508. *
  1509. * LOCKING:
  1510. * None.
  1511. */
  1512. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  1513. const u8 *buf, unsigned int buflen)
  1514. {
  1515. u8 *ptr = *ptr_io;
  1516. if ((ptr + buflen - 1) > last)
  1517. return;
  1518. memcpy(ptr, buf, buflen);
  1519. ptr += buflen;
  1520. *ptr_io = ptr;
  1521. }
  1522. /**
  1523. * ata_msense_caching - Simulate MODE SENSE caching info page
  1524. * @id: device IDENTIFY data
  1525. * @ptr_io: (input/output) Location to store more output data
  1526. * @last: End of output data buffer
  1527. *
  1528. * Generate a caching info page, which conditionally indicates
  1529. * write caching to the SCSI layer, depending on device
  1530. * capabilities.
  1531. *
  1532. * LOCKING:
  1533. * None.
  1534. */
  1535. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  1536. const u8 *last)
  1537. {
  1538. u8 page[CACHE_MPAGE_LEN];
  1539. memcpy(page, def_cache_mpage, sizeof(page));
  1540. if (ata_id_wcache_enabled(id))
  1541. page[2] |= (1 << 2); /* write cache enable */
  1542. if (!ata_id_rahead_enabled(id))
  1543. page[12] |= (1 << 5); /* disable read ahead */
  1544. ata_msense_push(ptr_io, last, page, sizeof(page));
  1545. return sizeof(page);
  1546. }
  1547. /**
  1548. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1549. * @dev: Device associated with this MODE SENSE command
  1550. * @ptr_io: (input/output) Location to store more output data
  1551. * @last: End of output data buffer
  1552. *
  1553. * Generate a generic MODE SENSE control mode page.
  1554. *
  1555. * LOCKING:
  1556. * None.
  1557. */
  1558. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1559. {
  1560. ata_msense_push(ptr_io, last, def_control_mpage,
  1561. sizeof(def_control_mpage));
  1562. return sizeof(def_control_mpage);
  1563. }
  1564. /**
  1565. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1566. * @dev: Device associated with this MODE SENSE command
  1567. * @ptr_io: (input/output) Location to store more output data
  1568. * @last: End of output data buffer
  1569. *
  1570. * Generate a generic MODE SENSE r/w error recovery page.
  1571. *
  1572. * LOCKING:
  1573. * None.
  1574. */
  1575. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1576. {
  1577. ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
  1578. sizeof(def_rw_recovery_mpage));
  1579. return sizeof(def_rw_recovery_mpage);
  1580. }
  1581. /*
  1582. * We can turn this into a real blacklist if it's needed, for now just
  1583. * blacklist any Maxtor BANC1G10 revision firmware
  1584. */
  1585. static int ata_dev_supports_fua(u16 *id)
  1586. {
  1587. unsigned char model[41], fw[9];
  1588. if (!libata_fua)
  1589. return 0;
  1590. if (!ata_id_has_fua(id))
  1591. return 0;
  1592. ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
  1593. ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
  1594. if (strcmp(model, "Maxtor"))
  1595. return 1;
  1596. if (strcmp(fw, "BANC1G10"))
  1597. return 1;
  1598. return 0; /* blacklisted */
  1599. }
  1600. /**
  1601. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1602. * @args: device IDENTIFY data / SCSI command of interest.
  1603. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1604. * @buflen: Response buffer length.
  1605. *
  1606. * Simulate MODE SENSE commands. Assume this is invoked for direct
  1607. * access devices (e.g. disks) only. There should be no block
  1608. * descriptor for other device types.
  1609. *
  1610. * LOCKING:
  1611. * spin_lock_irqsave(host_set lock)
  1612. */
  1613. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1614. unsigned int buflen)
  1615. {
  1616. struct ata_device *dev = args->dev;
  1617. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1618. const u8 sat_blk_desc[] = {
  1619. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  1620. 0,
  1621. 0, 0x2, 0x0 /* block length: 512 bytes */
  1622. };
  1623. u8 pg, spg;
  1624. unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
  1625. u8 dpofua;
  1626. VPRINTK("ENTER\n");
  1627. six_byte = (scsicmd[0] == MODE_SENSE);
  1628. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  1629. /*
  1630. * LLBA bit in msense(10) ignored (compliant)
  1631. */
  1632. page_control = scsicmd[2] >> 6;
  1633. switch (page_control) {
  1634. case 0: /* current */
  1635. break; /* supported */
  1636. case 3: /* saved */
  1637. goto saving_not_supp;
  1638. case 1: /* changeable */
  1639. case 2: /* defaults */
  1640. default:
  1641. goto invalid_fld;
  1642. }
  1643. if (six_byte) {
  1644. output_len = 4 + (ebd ? 8 : 0);
  1645. alloc_len = scsicmd[4];
  1646. } else {
  1647. output_len = 8 + (ebd ? 8 : 0);
  1648. alloc_len = (scsicmd[7] << 8) + scsicmd[8];
  1649. }
  1650. minlen = (alloc_len < buflen) ? alloc_len : buflen;
  1651. p = rbuf + output_len;
  1652. last = rbuf + minlen - 1;
  1653. pg = scsicmd[2] & 0x3f;
  1654. spg = scsicmd[3];
  1655. /*
  1656. * No mode subpages supported (yet) but asking for _all_
  1657. * subpages may be valid
  1658. */
  1659. if (spg && (spg != ALL_SUB_MPAGES))
  1660. goto invalid_fld;
  1661. switch(pg) {
  1662. case RW_RECOVERY_MPAGE:
  1663. output_len += ata_msense_rw_recovery(&p, last);
  1664. break;
  1665. case CACHE_MPAGE:
  1666. output_len += ata_msense_caching(args->id, &p, last);
  1667. break;
  1668. case CONTROL_MPAGE: {
  1669. output_len += ata_msense_ctl_mode(&p, last);
  1670. break;
  1671. }
  1672. case ALL_MPAGES:
  1673. output_len += ata_msense_rw_recovery(&p, last);
  1674. output_len += ata_msense_caching(args->id, &p, last);
  1675. output_len += ata_msense_ctl_mode(&p, last);
  1676. break;
  1677. default: /* invalid page code */
  1678. goto invalid_fld;
  1679. }
  1680. if (minlen < 1)
  1681. return 0;
  1682. dpofua = 0;
  1683. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  1684. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  1685. dpofua = 1 << 4;
  1686. if (six_byte) {
  1687. output_len--;
  1688. rbuf[0] = output_len;
  1689. if (minlen > 2)
  1690. rbuf[2] |= dpofua;
  1691. if (ebd) {
  1692. if (minlen > 3)
  1693. rbuf[3] = sizeof(sat_blk_desc);
  1694. if (minlen > 11)
  1695. memcpy(rbuf + 4, sat_blk_desc,
  1696. sizeof(sat_blk_desc));
  1697. }
  1698. } else {
  1699. output_len -= 2;
  1700. rbuf[0] = output_len >> 8;
  1701. if (minlen > 1)
  1702. rbuf[1] = output_len;
  1703. if (minlen > 3)
  1704. rbuf[3] |= dpofua;
  1705. if (ebd) {
  1706. if (minlen > 7)
  1707. rbuf[7] = sizeof(sat_blk_desc);
  1708. if (minlen > 15)
  1709. memcpy(rbuf + 8, sat_blk_desc,
  1710. sizeof(sat_blk_desc));
  1711. }
  1712. }
  1713. return 0;
  1714. invalid_fld:
  1715. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1716. /* "Invalid field in cbd" */
  1717. return 1;
  1718. saving_not_supp:
  1719. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  1720. /* "Saving parameters not supported" */
  1721. return 1;
  1722. }
  1723. /**
  1724. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1725. * @args: device IDENTIFY data / SCSI command of interest.
  1726. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1727. * @buflen: Response buffer length.
  1728. *
  1729. * Simulate READ CAPACITY commands.
  1730. *
  1731. * LOCKING:
  1732. * spin_lock_irqsave(host_set lock)
  1733. */
  1734. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1735. unsigned int buflen)
  1736. {
  1737. u64 n_sectors;
  1738. u32 tmp;
  1739. VPRINTK("ENTER\n");
  1740. if (ata_id_has_lba(args->id)) {
  1741. if (ata_id_has_lba48(args->id))
  1742. n_sectors = ata_id_u64(args->id, 100);
  1743. else
  1744. n_sectors = ata_id_u32(args->id, 60);
  1745. } else {
  1746. /* CHS default translation */
  1747. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1748. if (ata_id_current_chs_valid(args->id))
  1749. /* CHS current translation */
  1750. n_sectors = ata_id_u32(args->id, 57);
  1751. }
  1752. n_sectors--; /* ATA TotalUserSectors - 1 */
  1753. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1754. if( n_sectors >= 0xffffffffULL )
  1755. tmp = 0xffffffff ; /* Return max count on overflow */
  1756. else
  1757. tmp = n_sectors ;
  1758. /* sector count, 32-bit */
  1759. rbuf[0] = tmp >> (8 * 3);
  1760. rbuf[1] = tmp >> (8 * 2);
  1761. rbuf[2] = tmp >> (8 * 1);
  1762. rbuf[3] = tmp;
  1763. /* sector size */
  1764. tmp = ATA_SECT_SIZE;
  1765. rbuf[6] = tmp >> 8;
  1766. rbuf[7] = tmp;
  1767. } else {
  1768. /* sector count, 64-bit */
  1769. tmp = n_sectors >> (8 * 4);
  1770. rbuf[2] = tmp >> (8 * 3);
  1771. rbuf[3] = tmp >> (8 * 2);
  1772. rbuf[4] = tmp >> (8 * 1);
  1773. rbuf[5] = tmp;
  1774. tmp = n_sectors;
  1775. rbuf[6] = tmp >> (8 * 3);
  1776. rbuf[7] = tmp >> (8 * 2);
  1777. rbuf[8] = tmp >> (8 * 1);
  1778. rbuf[9] = tmp;
  1779. /* sector size */
  1780. tmp = ATA_SECT_SIZE;
  1781. rbuf[12] = tmp >> 8;
  1782. rbuf[13] = tmp;
  1783. }
  1784. return 0;
  1785. }
  1786. /**
  1787. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1788. * @args: device IDENTIFY data / SCSI command of interest.
  1789. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1790. * @buflen: Response buffer length.
  1791. *
  1792. * Simulate REPORT LUNS command.
  1793. *
  1794. * LOCKING:
  1795. * spin_lock_irqsave(host_set lock)
  1796. */
  1797. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1798. unsigned int buflen)
  1799. {
  1800. VPRINTK("ENTER\n");
  1801. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1802. return 0;
  1803. }
  1804. /**
  1805. * ata_scsi_set_sense - Set SCSI sense data and status
  1806. * @cmd: SCSI request to be handled
  1807. * @sk: SCSI-defined sense key
  1808. * @asc: SCSI-defined additional sense code
  1809. * @ascq: SCSI-defined additional sense code qualifier
  1810. *
  1811. * Helper function that builds a valid fixed format, current
  1812. * response code and the given sense key (sk), additional sense
  1813. * code (asc) and additional sense code qualifier (ascq) with
  1814. * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
  1815. * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
  1816. *
  1817. * LOCKING:
  1818. * Not required
  1819. */
  1820. void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  1821. {
  1822. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  1823. cmd->sense_buffer[0] = 0x70; /* fixed format, current */
  1824. cmd->sense_buffer[2] = sk;
  1825. cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
  1826. cmd->sense_buffer[12] = asc;
  1827. cmd->sense_buffer[13] = ascq;
  1828. }
  1829. /**
  1830. * ata_scsi_badcmd - End a SCSI request with an error
  1831. * @cmd: SCSI request to be handled
  1832. * @done: SCSI command completion function
  1833. * @asc: SCSI-defined additional sense code
  1834. * @ascq: SCSI-defined additional sense code qualifier
  1835. *
  1836. * Helper function that completes a SCSI command with
  1837. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1838. * and the specified additional sense codes.
  1839. *
  1840. * LOCKING:
  1841. * spin_lock_irqsave(host_set lock)
  1842. */
  1843. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1844. {
  1845. DPRINTK("ENTER\n");
  1846. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
  1847. done(cmd);
  1848. }
  1849. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  1850. {
  1851. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  1852. /* FIXME: not quite right; we don't want the
  1853. * translation of taskfile registers into
  1854. * a sense descriptors, since that's only
  1855. * correct for ATA, not ATAPI
  1856. */
  1857. ata_gen_ata_desc_sense(qc);
  1858. }
  1859. qc->scsidone(qc->scsicmd);
  1860. ata_qc_free(qc);
  1861. }
  1862. /* is it pointless to prefer PIO for "safety reasons"? */
  1863. static inline int ata_pio_use_silly(struct ata_port *ap)
  1864. {
  1865. return (ap->flags & ATA_FLAG_PIO_DMA);
  1866. }
  1867. static void atapi_request_sense(struct ata_queued_cmd *qc)
  1868. {
  1869. struct ata_port *ap = qc->ap;
  1870. struct scsi_cmnd *cmd = qc->scsicmd;
  1871. DPRINTK("ATAPI request sense\n");
  1872. /* FIXME: is this needed? */
  1873. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  1874. ap->ops->tf_read(ap, &qc->tf);
  1875. /* fill these in, for the case where they are -not- overwritten */
  1876. cmd->sense_buffer[0] = 0x70;
  1877. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  1878. ata_qc_reinit(qc);
  1879. ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
  1880. qc->dma_dir = DMA_FROM_DEVICE;
  1881. memset(&qc->cdb, 0, qc->dev->cdb_len);
  1882. qc->cdb[0] = REQUEST_SENSE;
  1883. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1884. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1885. qc->tf.command = ATA_CMD_PACKET;
  1886. if (ata_pio_use_silly(ap)) {
  1887. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1888. qc->tf.feature |= ATAPI_PKT_DMA;
  1889. } else {
  1890. qc->tf.protocol = ATA_PROT_ATAPI;
  1891. qc->tf.lbam = (8 * 1024) & 0xff;
  1892. qc->tf.lbah = (8 * 1024) >> 8;
  1893. }
  1894. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  1895. qc->complete_fn = atapi_sense_complete;
  1896. ata_qc_issue(qc);
  1897. DPRINTK("EXIT\n");
  1898. }
  1899. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  1900. {
  1901. struct scsi_cmnd *cmd = qc->scsicmd;
  1902. unsigned int err_mask = qc->err_mask;
  1903. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  1904. /* handle completion from new EH */
  1905. if (unlikely(qc->ap->ops->error_handler &&
  1906. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  1907. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  1908. /* FIXME: not quite right; we don't want the
  1909. * translation of taskfile registers into a
  1910. * sense descriptors, since that's only
  1911. * correct for ATA, not ATAPI
  1912. */
  1913. ata_gen_ata_desc_sense(qc);
  1914. }
  1915. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  1916. qc->scsidone(cmd);
  1917. ata_qc_free(qc);
  1918. return;
  1919. }
  1920. /* successful completion or old EH failure path */
  1921. if (unlikely(err_mask & AC_ERR_DEV)) {
  1922. cmd->result = SAM_STAT_CHECK_CONDITION;
  1923. atapi_request_sense(qc);
  1924. return;
  1925. } else if (unlikely(err_mask)) {
  1926. /* FIXME: not quite right; we don't want the
  1927. * translation of taskfile registers into
  1928. * a sense descriptors, since that's only
  1929. * correct for ATA, not ATAPI
  1930. */
  1931. ata_gen_ata_desc_sense(qc);
  1932. } else {
  1933. u8 *scsicmd = cmd->cmnd;
  1934. if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
  1935. u8 *buf = NULL;
  1936. unsigned int buflen;
  1937. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1938. /* ATAPI devices typically report zero for their SCSI version,
  1939. * and sometimes deviate from the spec WRT response data
  1940. * format. If SCSI version is reported as zero like normal,
  1941. * then we make the following fixups: 1) Fake MMC-5 version,
  1942. * to indicate to the Linux scsi midlayer this is a modern
  1943. * device. 2) Ensure response data format / ATAPI information
  1944. * are always correct.
  1945. */
  1946. if (buf[2] == 0) {
  1947. buf[2] = 0x5;
  1948. buf[3] = 0x32;
  1949. }
  1950. ata_scsi_rbuf_put(cmd, buf);
  1951. }
  1952. cmd->result = SAM_STAT_GOOD;
  1953. }
  1954. qc->scsidone(cmd);
  1955. ata_qc_free(qc);
  1956. }
  1957. /**
  1958. * atapi_xlat - Initialize PACKET taskfile
  1959. * @qc: command structure to be initialized
  1960. * @scsicmd: SCSI CDB associated with this PACKET command
  1961. *
  1962. * LOCKING:
  1963. * spin_lock_irqsave(host_set lock)
  1964. *
  1965. * RETURNS:
  1966. * Zero on success, non-zero on failure.
  1967. */
  1968. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  1969. {
  1970. struct scsi_cmnd *cmd = qc->scsicmd;
  1971. struct ata_device *dev = qc->dev;
  1972. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1973. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1974. if (!using_pio)
  1975. /* Check whether ATAPI DMA is safe */
  1976. if (ata_check_atapi_dma(qc))
  1977. using_pio = 1;
  1978. memcpy(&qc->cdb, scsicmd, dev->cdb_len);
  1979. qc->complete_fn = atapi_qc_complete;
  1980. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1981. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1982. qc->tf.flags |= ATA_TFLAG_WRITE;
  1983. DPRINTK("direction: write\n");
  1984. }
  1985. qc->tf.command = ATA_CMD_PACKET;
  1986. /* no data, or PIO data xfer */
  1987. if (using_pio || nodata) {
  1988. if (nodata)
  1989. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1990. else
  1991. qc->tf.protocol = ATA_PROT_ATAPI;
  1992. qc->tf.lbam = (8 * 1024) & 0xff;
  1993. qc->tf.lbah = (8 * 1024) >> 8;
  1994. }
  1995. /* DMA data xfer */
  1996. else {
  1997. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1998. qc->tf.feature |= ATAPI_PKT_DMA;
  1999. if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
  2000. /* some SATA bridges need us to indicate data xfer direction */
  2001. qc->tf.feature |= ATAPI_DMADIR;
  2002. }
  2003. qc->nbytes = cmd->bufflen;
  2004. return 0;
  2005. }
  2006. static struct ata_device * ata_find_dev(struct ata_port *ap, int id)
  2007. {
  2008. if (likely(id < ATA_MAX_DEVICES))
  2009. return &ap->device[id];
  2010. return NULL;
  2011. }
  2012. static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
  2013. const struct scsi_device *scsidev)
  2014. {
  2015. /* skip commands not addressed to targets we simulate */
  2016. if (unlikely(scsidev->channel || scsidev->lun))
  2017. return NULL;
  2018. return ata_find_dev(ap, scsidev->id);
  2019. }
  2020. /**
  2021. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2022. * @ap: ATA port to which the device is attached
  2023. * @scsidev: SCSI device from which we derive the ATA device
  2024. *
  2025. * Given various information provided in struct scsi_cmnd,
  2026. * map that onto an ATA bus, and using that mapping
  2027. * determine which ata_device is associated with the
  2028. * SCSI command to be sent.
  2029. *
  2030. * LOCKING:
  2031. * spin_lock_irqsave(host_set lock)
  2032. *
  2033. * RETURNS:
  2034. * Associated ATA device, or %NULL if not found.
  2035. */
  2036. static struct ata_device *
  2037. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2038. {
  2039. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2040. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2041. return NULL;
  2042. if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
  2043. if (unlikely(dev->class == ATA_DEV_ATAPI)) {
  2044. ata_dev_printk(dev, KERN_WARNING,
  2045. "WARNING: ATAPI is %s, device ignored.\n",
  2046. atapi_enabled ? "not supported with this driver" : "disabled");
  2047. return NULL;
  2048. }
  2049. }
  2050. return dev;
  2051. }
  2052. /*
  2053. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2054. * @byte1: Byte 1 from pass-thru CDB.
  2055. *
  2056. * RETURNS:
  2057. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2058. */
  2059. static u8
  2060. ata_scsi_map_proto(u8 byte1)
  2061. {
  2062. switch((byte1 & 0x1e) >> 1) {
  2063. case 3: /* Non-data */
  2064. return ATA_PROT_NODATA;
  2065. case 6: /* DMA */
  2066. return ATA_PROT_DMA;
  2067. case 4: /* PIO Data-in */
  2068. case 5: /* PIO Data-out */
  2069. return ATA_PROT_PIO;
  2070. case 10: /* Device Reset */
  2071. case 0: /* Hard Reset */
  2072. case 1: /* SRST */
  2073. case 2: /* Bus Idle */
  2074. case 7: /* Packet */
  2075. case 8: /* DMA Queued */
  2076. case 9: /* Device Diagnostic */
  2077. case 11: /* UDMA Data-in */
  2078. case 12: /* UDMA Data-Out */
  2079. case 13: /* FPDMA */
  2080. default: /* Reserved */
  2081. break;
  2082. }
  2083. return ATA_PROT_UNKNOWN;
  2084. }
  2085. /**
  2086. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2087. * @qc: command structure to be initialized
  2088. * @scsicmd: SCSI command to convert
  2089. *
  2090. * Handles either 12 or 16-byte versions of the CDB.
  2091. *
  2092. * RETURNS:
  2093. * Zero on success, non-zero on failure.
  2094. */
  2095. static unsigned int
  2096. ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
  2097. {
  2098. struct ata_taskfile *tf = &(qc->tf);
  2099. struct scsi_cmnd *cmd = qc->scsicmd;
  2100. struct ata_device *dev = qc->dev;
  2101. if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
  2102. goto invalid_fld;
  2103. /* We may not issue DMA commands if no DMA mode is set */
  2104. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
  2105. goto invalid_fld;
  2106. if (scsicmd[1] & 0xe0)
  2107. /* PIO multi not supported yet */
  2108. goto invalid_fld;
  2109. /*
  2110. * 12 and 16 byte CDBs use different offsets to
  2111. * provide the various register values.
  2112. */
  2113. if (scsicmd[0] == ATA_16) {
  2114. /*
  2115. * 16-byte CDB - may contain extended commands.
  2116. *
  2117. * If that is the case, copy the upper byte register values.
  2118. */
  2119. if (scsicmd[1] & 0x01) {
  2120. tf->hob_feature = scsicmd[3];
  2121. tf->hob_nsect = scsicmd[5];
  2122. tf->hob_lbal = scsicmd[7];
  2123. tf->hob_lbam = scsicmd[9];
  2124. tf->hob_lbah = scsicmd[11];
  2125. tf->flags |= ATA_TFLAG_LBA48;
  2126. } else
  2127. tf->flags &= ~ATA_TFLAG_LBA48;
  2128. /*
  2129. * Always copy low byte, device and command registers.
  2130. */
  2131. tf->feature = scsicmd[4];
  2132. tf->nsect = scsicmd[6];
  2133. tf->lbal = scsicmd[8];
  2134. tf->lbam = scsicmd[10];
  2135. tf->lbah = scsicmd[12];
  2136. tf->device = scsicmd[13];
  2137. tf->command = scsicmd[14];
  2138. } else {
  2139. /*
  2140. * 12-byte CDB - incapable of extended commands.
  2141. */
  2142. tf->flags &= ~ATA_TFLAG_LBA48;
  2143. tf->feature = scsicmd[3];
  2144. tf->nsect = scsicmd[4];
  2145. tf->lbal = scsicmd[5];
  2146. tf->lbam = scsicmd[6];
  2147. tf->lbah = scsicmd[7];
  2148. tf->device = scsicmd[8];
  2149. tf->command = scsicmd[9];
  2150. }
  2151. /*
  2152. * If slave is possible, enforce correct master/slave bit
  2153. */
  2154. if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
  2155. tf->device = qc->dev->devno ?
  2156. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2157. /*
  2158. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2159. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2160. * by an update to hardware-specific registers for each
  2161. * controller (i.e. the reason for ->set_piomode(),
  2162. * ->set_dmamode(), and ->post_set_mode() hooks).
  2163. */
  2164. if ((tf->command == ATA_CMD_SET_FEATURES)
  2165. && (tf->feature == SETFEATURES_XFER))
  2166. goto invalid_fld;
  2167. /*
  2168. * Set flags so that all registers will be written,
  2169. * and pass on write indication (used for PIO/DMA
  2170. * setup.)
  2171. */
  2172. tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
  2173. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  2174. tf->flags |= ATA_TFLAG_WRITE;
  2175. /*
  2176. * Set transfer length.
  2177. *
  2178. * TODO: find out if we need to do more here to
  2179. * cover scatter/gather case.
  2180. */
  2181. qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
  2182. /* request result TF */
  2183. qc->flags |= ATA_QCFLAG_RESULT_TF;
  2184. return 0;
  2185. invalid_fld:
  2186. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2187. /* "Invalid field in cdb" */
  2188. return 1;
  2189. }
  2190. /**
  2191. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  2192. * @dev: ATA device
  2193. * @cmd: SCSI command opcode to consider
  2194. *
  2195. * Look up the SCSI command given, and determine whether the
  2196. * SCSI command is to be translated or simulated.
  2197. *
  2198. * RETURNS:
  2199. * Pointer to translation function if possible, %NULL if not.
  2200. */
  2201. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2202. {
  2203. switch (cmd) {
  2204. case READ_6:
  2205. case READ_10:
  2206. case READ_16:
  2207. case WRITE_6:
  2208. case WRITE_10:
  2209. case WRITE_16:
  2210. return ata_scsi_rw_xlat;
  2211. case SYNCHRONIZE_CACHE:
  2212. if (ata_try_flush_cache(dev))
  2213. return ata_scsi_flush_xlat;
  2214. break;
  2215. case VERIFY:
  2216. case VERIFY_16:
  2217. return ata_scsi_verify_xlat;
  2218. case ATA_12:
  2219. case ATA_16:
  2220. return ata_scsi_pass_thru;
  2221. case START_STOP:
  2222. return ata_scsi_start_stop_xlat;
  2223. }
  2224. return NULL;
  2225. }
  2226. /**
  2227. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2228. * @ap: ATA port to which the command was being sent
  2229. * @cmd: SCSI command to dump
  2230. *
  2231. * Prints the contents of a SCSI command via printk().
  2232. */
  2233. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2234. struct scsi_cmnd *cmd)
  2235. {
  2236. #ifdef ATA_DEBUG
  2237. struct scsi_device *scsidev = cmd->device;
  2238. u8 *scsicmd = cmd->cmnd;
  2239. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2240. ap->id,
  2241. scsidev->channel, scsidev->id, scsidev->lun,
  2242. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2243. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2244. scsicmd[8]);
  2245. #endif
  2246. }
  2247. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
  2248. void (*done)(struct scsi_cmnd *),
  2249. struct ata_device *dev)
  2250. {
  2251. int rc = 0;
  2252. if (dev->class == ATA_DEV_ATA) {
  2253. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  2254. cmd->cmnd[0]);
  2255. if (xlat_func)
  2256. rc = ata_scsi_translate(dev, cmd, done, xlat_func);
  2257. else
  2258. ata_scsi_simulate(dev, cmd, done);
  2259. } else
  2260. rc = ata_scsi_translate(dev, cmd, done, atapi_xlat);
  2261. return rc;
  2262. }
  2263. /**
  2264. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  2265. * @cmd: SCSI command to be sent
  2266. * @done: Completion function, called when command is complete
  2267. *
  2268. * In some cases, this function translates SCSI commands into
  2269. * ATA taskfiles, and queues the taskfiles to be sent to
  2270. * hardware. In other cases, this function simulates a
  2271. * SCSI device by evaluating and responding to certain
  2272. * SCSI commands. This creates the overall effect of
  2273. * ATA and ATAPI devices appearing as SCSI devices.
  2274. *
  2275. * LOCKING:
  2276. * Releases scsi-layer-held lock, and obtains host_set lock.
  2277. *
  2278. * RETURNS:
  2279. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  2280. * 0 otherwise.
  2281. */
  2282. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  2283. {
  2284. struct ata_port *ap;
  2285. struct ata_device *dev;
  2286. struct scsi_device *scsidev = cmd->device;
  2287. struct Scsi_Host *shost = scsidev->host;
  2288. int rc = 0;
  2289. ap = ata_shost_to_port(shost);
  2290. spin_unlock(shost->host_lock);
  2291. spin_lock(&ap->host_set->lock);
  2292. ata_scsi_dump_cdb(ap, cmd);
  2293. dev = ata_scsi_find_dev(ap, scsidev);
  2294. if (likely(dev))
  2295. rc = __ata_scsi_queuecmd(cmd, done, dev);
  2296. else {
  2297. cmd->result = (DID_BAD_TARGET << 16);
  2298. done(cmd);
  2299. }
  2300. spin_unlock(&ap->host_set->lock);
  2301. spin_lock(shost->host_lock);
  2302. return rc;
  2303. }
  2304. /**
  2305. * ata_scsi_simulate - simulate SCSI command on ATA device
  2306. * @dev: the target device
  2307. * @cmd: SCSI command being sent to device.
  2308. * @done: SCSI command completion function.
  2309. *
  2310. * Interprets and directly executes a select list of SCSI commands
  2311. * that can be handled internally.
  2312. *
  2313. * LOCKING:
  2314. * spin_lock_irqsave(host_set lock)
  2315. */
  2316. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
  2317. void (*done)(struct scsi_cmnd *))
  2318. {
  2319. struct ata_scsi_args args;
  2320. const u8 *scsicmd = cmd->cmnd;
  2321. args.dev = dev;
  2322. args.id = dev->id;
  2323. args.cmd = cmd;
  2324. args.done = done;
  2325. switch(scsicmd[0]) {
  2326. /* no-op's, complete with success */
  2327. case SYNCHRONIZE_CACHE:
  2328. case REZERO_UNIT:
  2329. case SEEK_6:
  2330. case SEEK_10:
  2331. case TEST_UNIT_READY:
  2332. case FORMAT_UNIT: /* FIXME: correct? */
  2333. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  2334. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2335. break;
  2336. case INQUIRY:
  2337. if (scsicmd[1] & 2) /* is CmdDt set? */
  2338. ata_scsi_invalid_field(cmd, done);
  2339. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  2340. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  2341. else if (scsicmd[2] == 0x00)
  2342. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  2343. else if (scsicmd[2] == 0x80)
  2344. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  2345. else if (scsicmd[2] == 0x83)
  2346. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  2347. else
  2348. ata_scsi_invalid_field(cmd, done);
  2349. break;
  2350. case MODE_SENSE:
  2351. case MODE_SENSE_10:
  2352. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  2353. break;
  2354. case MODE_SELECT: /* unconditionally return */
  2355. case MODE_SELECT_10: /* bad-field-in-cdb */
  2356. ata_scsi_invalid_field(cmd, done);
  2357. break;
  2358. case READ_CAPACITY:
  2359. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2360. break;
  2361. case SERVICE_ACTION_IN:
  2362. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  2363. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2364. else
  2365. ata_scsi_invalid_field(cmd, done);
  2366. break;
  2367. case REPORT_LUNS:
  2368. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  2369. break;
  2370. /* mandatory commands we haven't implemented yet */
  2371. case REQUEST_SENSE:
  2372. /* all other commands */
  2373. default:
  2374. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2375. /* "Invalid command operation code" */
  2376. done(cmd);
  2377. break;
  2378. }
  2379. }
  2380. void ata_scsi_scan_host(struct ata_port *ap)
  2381. {
  2382. unsigned int i;
  2383. if (ap->flags & ATA_FLAG_DISABLED)
  2384. return;
  2385. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2386. struct ata_device *dev = &ap->device[i];
  2387. struct scsi_device *sdev;
  2388. if (!ata_dev_enabled(dev) || dev->sdev)
  2389. continue;
  2390. sdev = __scsi_add_device(ap->host, 0, i, 0, NULL);
  2391. if (!IS_ERR(sdev)) {
  2392. dev->sdev = sdev;
  2393. scsi_device_put(sdev);
  2394. }
  2395. }
  2396. }
  2397. /**
  2398. * ata_scsi_offline_dev - offline attached SCSI device
  2399. * @dev: ATA device to offline attached SCSI device for
  2400. *
  2401. * This function is called from ata_eh_hotplug() and responsible
  2402. * for taking the SCSI device attached to @dev offline. This
  2403. * function is called with host_set lock which protects dev->sdev
  2404. * against clearing.
  2405. *
  2406. * LOCKING:
  2407. * spin_lock_irqsave(host_set lock)
  2408. *
  2409. * RETURNS:
  2410. * 1 if attached SCSI device exists, 0 otherwise.
  2411. */
  2412. int ata_scsi_offline_dev(struct ata_device *dev)
  2413. {
  2414. if (dev->sdev) {
  2415. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  2416. return 1;
  2417. }
  2418. return 0;
  2419. }
  2420. /**
  2421. * ata_scsi_remove_dev - remove attached SCSI device
  2422. * @dev: ATA device to remove attached SCSI device for
  2423. *
  2424. * This function is called from ata_eh_scsi_hotplug() and
  2425. * responsible for removing the SCSI device attached to @dev.
  2426. *
  2427. * LOCKING:
  2428. * Kernel thread context (may sleep).
  2429. */
  2430. static void ata_scsi_remove_dev(struct ata_device *dev)
  2431. {
  2432. struct ata_port *ap = dev->ap;
  2433. struct scsi_device *sdev;
  2434. unsigned long flags;
  2435. /* Alas, we need to grab scan_mutex to ensure SCSI device
  2436. * state doesn't change underneath us and thus
  2437. * scsi_device_get() always succeeds. The mutex locking can
  2438. * be removed if there is __scsi_device_get() interface which
  2439. * increments reference counts regardless of device state.
  2440. */
  2441. mutex_lock(&ap->host->scan_mutex);
  2442. spin_lock_irqsave(&ap->host_set->lock, flags);
  2443. /* clearing dev->sdev is protected by host_set lock */
  2444. sdev = dev->sdev;
  2445. dev->sdev = NULL;
  2446. if (sdev) {
  2447. /* If user initiated unplug races with us, sdev can go
  2448. * away underneath us after the host_set lock and
  2449. * scan_mutex are released. Hold onto it.
  2450. */
  2451. if (scsi_device_get(sdev) == 0) {
  2452. /* The following ensures the attached sdev is
  2453. * offline on return from ata_scsi_offline_dev()
  2454. * regardless it wins or loses the race
  2455. * against this function.
  2456. */
  2457. scsi_device_set_state(sdev, SDEV_OFFLINE);
  2458. } else {
  2459. WARN_ON(1);
  2460. sdev = NULL;
  2461. }
  2462. }
  2463. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2464. mutex_unlock(&ap->host->scan_mutex);
  2465. if (sdev) {
  2466. ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
  2467. sdev->sdev_gendev.bus_id);
  2468. scsi_remove_device(sdev);
  2469. scsi_device_put(sdev);
  2470. }
  2471. }
  2472. /**
  2473. * ata_scsi_hotplug - SCSI part of hotplug
  2474. * @data: Pointer to ATA port to perform SCSI hotplug on
  2475. *
  2476. * Perform SCSI part of hotplug. It's executed from a separate
  2477. * workqueue after EH completes. This is necessary because SCSI
  2478. * hot plugging requires working EH and hot unplugging is
  2479. * synchronized with hot plugging with a mutex.
  2480. *
  2481. * LOCKING:
  2482. * Kernel thread context (may sleep).
  2483. */
  2484. void ata_scsi_hotplug(void *data)
  2485. {
  2486. struct ata_port *ap = data;
  2487. int i;
  2488. if (ap->flags & ATA_FLAG_UNLOADING) {
  2489. DPRINTK("ENTER/EXIT - unloading\n");
  2490. return;
  2491. }
  2492. DPRINTK("ENTER\n");
  2493. /* unplug detached devices */
  2494. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2495. struct ata_device *dev = &ap->device[i];
  2496. unsigned long flags;
  2497. if (!(dev->flags & ATA_DFLAG_DETACHED))
  2498. continue;
  2499. spin_lock_irqsave(&ap->host_set->lock, flags);
  2500. dev->flags &= ~ATA_DFLAG_DETACHED;
  2501. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2502. ata_scsi_remove_dev(dev);
  2503. }
  2504. /* scan for new ones */
  2505. ata_scsi_scan_host(ap);
  2506. /* If we scanned while EH was in progress, scan would have
  2507. * failed silently. Requeue if there are enabled but
  2508. * unattached devices.
  2509. */
  2510. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2511. struct ata_device *dev = &ap->device[i];
  2512. if (ata_dev_enabled(dev) && !dev->sdev) {
  2513. queue_delayed_work(ata_aux_wq, &ap->hotplug_task, HZ);
  2514. break;
  2515. }
  2516. }
  2517. DPRINTK("EXIT\n");
  2518. }
  2519. /**
  2520. * ata_scsi_user_scan - indication for user-initiated bus scan
  2521. * @shost: SCSI host to scan
  2522. * @channel: Channel to scan
  2523. * @id: ID to scan
  2524. * @lun: LUN to scan
  2525. *
  2526. * This function is called when user explicitly requests bus
  2527. * scan. Set probe pending flag and invoke EH.
  2528. *
  2529. * LOCKING:
  2530. * SCSI layer (we don't care)
  2531. *
  2532. * RETURNS:
  2533. * Zero.
  2534. */
  2535. static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  2536. unsigned int id, unsigned int lun)
  2537. {
  2538. struct ata_port *ap = ata_shost_to_port(shost);
  2539. unsigned long flags;
  2540. int rc = 0;
  2541. if (!ap->ops->error_handler)
  2542. return -EOPNOTSUPP;
  2543. if ((channel != SCAN_WILD_CARD && channel != 0) ||
  2544. (lun != SCAN_WILD_CARD && lun != 0))
  2545. return -EINVAL;
  2546. spin_lock_irqsave(&ap->host_set->lock, flags);
  2547. if (id == SCAN_WILD_CARD) {
  2548. ap->eh_info.probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
  2549. ap->eh_info.action |= ATA_EH_SOFTRESET;
  2550. } else {
  2551. struct ata_device *dev = ata_find_dev(ap, id);
  2552. if (dev) {
  2553. ap->eh_info.probe_mask |= 1 << dev->devno;
  2554. ap->eh_info.action |= ATA_EH_SOFTRESET;
  2555. } else
  2556. rc = -EINVAL;
  2557. }
  2558. if (rc == 0)
  2559. ata_port_schedule_eh(ap);
  2560. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2561. return rc;
  2562. }