libata-scsi.c 83 KB

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