libata-scsi.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284
  1. /*
  2. * libata-scsi.c - helper library for ATA
  3. *
  4. * Maintained by: Tejun Heo <tj@kernel.org>
  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/slab.h>
  36. #include <linux/kernel.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/export.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_eh.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <scsi/scsi_transport.h>
  47. #include <linux/libata.h>
  48. #include <linux/hdreg.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/suspend.h>
  51. #include <asm/unaligned.h>
  52. #include "libata.h"
  53. #include "libata-transport.h"
  54. #define ATA_SCSI_RBUF_SIZE 4096
  55. static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
  56. static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
  57. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
  58. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  59. const struct scsi_device *scsidev);
  60. static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
  61. const struct scsi_device *scsidev);
  62. #define RW_RECOVERY_MPAGE 0x1
  63. #define RW_RECOVERY_MPAGE_LEN 12
  64. #define CACHE_MPAGE 0x8
  65. #define CACHE_MPAGE_LEN 20
  66. #define CONTROL_MPAGE 0xa
  67. #define CONTROL_MPAGE_LEN 12
  68. #define ALL_MPAGES 0x3f
  69. #define ALL_SUB_MPAGES 0xff
  70. static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
  71. RW_RECOVERY_MPAGE,
  72. RW_RECOVERY_MPAGE_LEN - 2,
  73. (1 << 7), /* AWRE */
  74. 0, /* read retry count */
  75. 0, 0, 0, 0,
  76. 0, /* write retry count */
  77. 0, 0, 0
  78. };
  79. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  80. CACHE_MPAGE,
  81. CACHE_MPAGE_LEN - 2,
  82. 0, /* contains WCE, needs to be 0 for logic */
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, /* contains DRA, needs to be 0 for logic */
  85. 0, 0, 0, 0, 0, 0, 0
  86. };
  87. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  88. CONTROL_MPAGE,
  89. CONTROL_MPAGE_LEN - 2,
  90. 2, /* DSENSE=0, GLTSD=1 */
  91. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  92. 0, 0, 0, 0, 0xff, 0xff,
  93. 0, 30 /* extended self test time, see 05-359r1 */
  94. };
  95. static const char *ata_lpm_policy_names[] = {
  96. [ATA_LPM_UNKNOWN] = "max_performance",
  97. [ATA_LPM_MAX_POWER] = "max_performance",
  98. [ATA_LPM_MED_POWER] = "medium_power",
  99. [ATA_LPM_MIN_POWER] = "min_power",
  100. };
  101. static ssize_t ata_scsi_lpm_store(struct device *device,
  102. struct device_attribute *attr,
  103. const char *buf, size_t count)
  104. {
  105. struct Scsi_Host *shost = class_to_shost(device);
  106. struct ata_port *ap = ata_shost_to_port(shost);
  107. struct ata_link *link;
  108. struct ata_device *dev;
  109. enum ata_lpm_policy policy;
  110. unsigned long flags;
  111. /* UNKNOWN is internal state, iterate from MAX_POWER */
  112. for (policy = ATA_LPM_MAX_POWER;
  113. policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
  114. const char *name = ata_lpm_policy_names[policy];
  115. if (strncmp(name, buf, strlen(name)) == 0)
  116. break;
  117. }
  118. if (policy == ARRAY_SIZE(ata_lpm_policy_names))
  119. return -EINVAL;
  120. spin_lock_irqsave(ap->lock, flags);
  121. ata_for_each_link(link, ap, EDGE) {
  122. ata_for_each_dev(dev, &ap->link, ENABLED) {
  123. if (dev->horkage & ATA_HORKAGE_NOLPM) {
  124. count = -EOPNOTSUPP;
  125. goto out_unlock;
  126. }
  127. }
  128. }
  129. ap->target_lpm_policy = policy;
  130. ata_port_schedule_eh(ap);
  131. out_unlock:
  132. spin_unlock_irqrestore(ap->lock, flags);
  133. return count;
  134. }
  135. static ssize_t ata_scsi_lpm_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct Scsi_Host *shost = class_to_shost(dev);
  139. struct ata_port *ap = ata_shost_to_port(shost);
  140. if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
  141. return -EINVAL;
  142. return snprintf(buf, PAGE_SIZE, "%s\n",
  143. ata_lpm_policy_names[ap->target_lpm_policy]);
  144. }
  145. DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
  146. ata_scsi_lpm_show, ata_scsi_lpm_store);
  147. EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
  148. static ssize_t ata_scsi_park_show(struct device *device,
  149. struct device_attribute *attr, char *buf)
  150. {
  151. struct scsi_device *sdev = to_scsi_device(device);
  152. struct ata_port *ap;
  153. struct ata_link *link;
  154. struct ata_device *dev;
  155. unsigned long now;
  156. unsigned int uninitialized_var(msecs);
  157. int rc = 0;
  158. ap = ata_shost_to_port(sdev->host);
  159. spin_lock_irq(ap->lock);
  160. dev = ata_scsi_find_dev(ap, sdev);
  161. if (!dev) {
  162. rc = -ENODEV;
  163. goto unlock;
  164. }
  165. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  166. rc = -EOPNOTSUPP;
  167. goto unlock;
  168. }
  169. link = dev->link;
  170. now = jiffies;
  171. if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
  172. link->eh_context.unloaded_mask & (1 << dev->devno) &&
  173. time_after(dev->unpark_deadline, now))
  174. msecs = jiffies_to_msecs(dev->unpark_deadline - now);
  175. else
  176. msecs = 0;
  177. unlock:
  178. spin_unlock_irq(ap->lock);
  179. return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
  180. }
  181. static ssize_t ata_scsi_park_store(struct device *device,
  182. struct device_attribute *attr,
  183. const char *buf, size_t len)
  184. {
  185. struct scsi_device *sdev = to_scsi_device(device);
  186. struct ata_port *ap;
  187. struct ata_device *dev;
  188. long int input;
  189. unsigned long flags;
  190. int rc;
  191. rc = kstrtol(buf, 10, &input);
  192. if (rc)
  193. return rc;
  194. if (input < -2)
  195. return -EINVAL;
  196. if (input > ATA_TMOUT_MAX_PARK) {
  197. rc = -EOVERFLOW;
  198. input = ATA_TMOUT_MAX_PARK;
  199. }
  200. ap = ata_shost_to_port(sdev->host);
  201. spin_lock_irqsave(ap->lock, flags);
  202. dev = ata_scsi_find_dev(ap, sdev);
  203. if (unlikely(!dev)) {
  204. rc = -ENODEV;
  205. goto unlock;
  206. }
  207. if (dev->class != ATA_DEV_ATA &&
  208. dev->class != ATA_DEV_ZAC) {
  209. rc = -EOPNOTSUPP;
  210. goto unlock;
  211. }
  212. if (input >= 0) {
  213. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  214. rc = -EOPNOTSUPP;
  215. goto unlock;
  216. }
  217. dev->unpark_deadline = ata_deadline(jiffies, input);
  218. dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
  219. ata_port_schedule_eh(ap);
  220. complete(&ap->park_req_pending);
  221. } else {
  222. switch (input) {
  223. case -1:
  224. dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
  225. break;
  226. case -2:
  227. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  228. break;
  229. }
  230. }
  231. unlock:
  232. spin_unlock_irqrestore(ap->lock, flags);
  233. return rc ? rc : len;
  234. }
  235. DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
  236. ata_scsi_park_show, ata_scsi_park_store);
  237. EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
  238. static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  239. {
  240. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  241. scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
  242. }
  243. static ssize_t
  244. ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct Scsi_Host *shost = class_to_shost(dev);
  248. struct ata_port *ap = ata_shost_to_port(shost);
  249. if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
  250. return ap->ops->em_store(ap, buf, count);
  251. return -EINVAL;
  252. }
  253. static ssize_t
  254. ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
  255. char *buf)
  256. {
  257. struct Scsi_Host *shost = class_to_shost(dev);
  258. struct ata_port *ap = ata_shost_to_port(shost);
  259. if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
  260. return ap->ops->em_show(ap, buf);
  261. return -EINVAL;
  262. }
  263. DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
  264. ata_scsi_em_message_show, ata_scsi_em_message_store);
  265. EXPORT_SYMBOL_GPL(dev_attr_em_message);
  266. static ssize_t
  267. ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct Scsi_Host *shost = class_to_shost(dev);
  271. struct ata_port *ap = ata_shost_to_port(shost);
  272. return snprintf(buf, 23, "%d\n", ap->em_message_type);
  273. }
  274. DEVICE_ATTR(em_message_type, S_IRUGO,
  275. ata_scsi_em_message_type_show, NULL);
  276. EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
  277. static ssize_t
  278. ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
  279. char *buf)
  280. {
  281. struct scsi_device *sdev = to_scsi_device(dev);
  282. struct ata_port *ap = ata_shost_to_port(sdev->host);
  283. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  284. if (atadev && ap->ops->sw_activity_show &&
  285. (ap->flags & ATA_FLAG_SW_ACTIVITY))
  286. return ap->ops->sw_activity_show(atadev, buf);
  287. return -EINVAL;
  288. }
  289. static ssize_t
  290. ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
  291. const char *buf, size_t count)
  292. {
  293. struct scsi_device *sdev = to_scsi_device(dev);
  294. struct ata_port *ap = ata_shost_to_port(sdev->host);
  295. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  296. enum sw_activity val;
  297. int rc;
  298. if (atadev && ap->ops->sw_activity_store &&
  299. (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
  300. val = simple_strtoul(buf, NULL, 0);
  301. switch (val) {
  302. case OFF: case BLINK_ON: case BLINK_OFF:
  303. rc = ap->ops->sw_activity_store(atadev, val);
  304. if (!rc)
  305. return count;
  306. else
  307. return rc;
  308. }
  309. }
  310. return -EINVAL;
  311. }
  312. DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
  313. ata_scsi_activity_store);
  314. EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
  315. struct device_attribute *ata_common_sdev_attrs[] = {
  316. &dev_attr_unload_heads,
  317. NULL
  318. };
  319. EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
  320. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd)
  321. {
  322. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  323. /* "Invalid field in cbd" */
  324. cmd->scsi_done(cmd);
  325. }
  326. /**
  327. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  328. * @sdev: SCSI device for which BIOS geometry is to be determined
  329. * @bdev: block device associated with @sdev
  330. * @capacity: capacity of SCSI device
  331. * @geom: location to which geometry will be output
  332. *
  333. * Generic bios head/sector/cylinder calculator
  334. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  335. * mapping. Some situations may arise where the disk is not
  336. * bootable if this is not used.
  337. *
  338. * LOCKING:
  339. * Defined by the SCSI layer. We don't really care.
  340. *
  341. * RETURNS:
  342. * Zero.
  343. */
  344. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  345. sector_t capacity, int geom[])
  346. {
  347. geom[0] = 255;
  348. geom[1] = 63;
  349. sector_div(capacity, 255*63);
  350. geom[2] = capacity;
  351. return 0;
  352. }
  353. /**
  354. * ata_scsi_unlock_native_capacity - unlock native capacity
  355. * @sdev: SCSI device to adjust device capacity for
  356. *
  357. * This function is called if a partition on @sdev extends beyond
  358. * the end of the device. It requests EH to unlock HPA.
  359. *
  360. * LOCKING:
  361. * Defined by the SCSI layer. Might sleep.
  362. */
  363. void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
  364. {
  365. struct ata_port *ap = ata_shost_to_port(sdev->host);
  366. struct ata_device *dev;
  367. unsigned long flags;
  368. spin_lock_irqsave(ap->lock, flags);
  369. dev = ata_scsi_find_dev(ap, sdev);
  370. if (dev && dev->n_sectors < dev->n_native_sectors) {
  371. dev->flags |= ATA_DFLAG_UNLOCK_HPA;
  372. dev->link->eh_info.action |= ATA_EH_RESET;
  373. ata_port_schedule_eh(ap);
  374. }
  375. spin_unlock_irqrestore(ap->lock, flags);
  376. ata_port_wait_eh(ap);
  377. }
  378. /**
  379. * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
  380. * @ap: target port
  381. * @sdev: SCSI device to get identify data for
  382. * @arg: User buffer area for identify data
  383. *
  384. * LOCKING:
  385. * Defined by the SCSI layer. We don't really care.
  386. *
  387. * RETURNS:
  388. * Zero on success, negative errno on error.
  389. */
  390. static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
  391. void __user *arg)
  392. {
  393. struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
  394. u16 __user *dst = arg;
  395. char buf[40];
  396. if (!dev)
  397. return -ENOMSG;
  398. if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
  399. return -EFAULT;
  400. ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
  401. if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
  402. return -EFAULT;
  403. ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
  404. if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
  405. return -EFAULT;
  406. ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  407. if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
  408. return -EFAULT;
  409. return 0;
  410. }
  411. /**
  412. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  413. * @scsidev: Device to which we are issuing command
  414. * @arg: User provided data for issuing command
  415. *
  416. * LOCKING:
  417. * Defined by the SCSI layer. We don't really care.
  418. *
  419. * RETURNS:
  420. * Zero on success, negative errno on error.
  421. */
  422. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  423. {
  424. int rc = 0;
  425. u8 scsi_cmd[MAX_COMMAND_SIZE];
  426. u8 args[4], *argbuf = NULL, *sensebuf = NULL;
  427. int argsize = 0;
  428. enum dma_data_direction data_dir;
  429. int cmd_result;
  430. if (arg == NULL)
  431. return -EINVAL;
  432. if (copy_from_user(args, arg, sizeof(args)))
  433. return -EFAULT;
  434. sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  435. if (!sensebuf)
  436. return -ENOMEM;
  437. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  438. if (args[3]) {
  439. argsize = ATA_SECT_SIZE * args[3];
  440. argbuf = kmalloc(argsize, GFP_KERNEL);
  441. if (argbuf == NULL) {
  442. rc = -ENOMEM;
  443. goto error;
  444. }
  445. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  446. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  447. block count in sector count field */
  448. data_dir = DMA_FROM_DEVICE;
  449. } else {
  450. scsi_cmd[1] = (3 << 1); /* Non-data */
  451. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  452. data_dir = DMA_NONE;
  453. }
  454. scsi_cmd[0] = ATA_16;
  455. scsi_cmd[4] = args[2];
  456. if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
  457. scsi_cmd[6] = args[3];
  458. scsi_cmd[8] = args[1];
  459. scsi_cmd[10] = 0x4f;
  460. scsi_cmd[12] = 0xc2;
  461. } else {
  462. scsi_cmd[6] = args[1];
  463. }
  464. scsi_cmd[14] = args[0];
  465. /* Good values for timeout and retries? Values below
  466. from scsi_ioctl_send_command() for default case... */
  467. cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
  468. sensebuf, (10*HZ), 5, 0, NULL);
  469. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  470. u8 *desc = sensebuf + 8;
  471. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  472. /* If we set cc then ATA pass-through will cause a
  473. * check condition even if no error. Filter that. */
  474. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  475. struct scsi_sense_hdr sshdr;
  476. scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
  477. &sshdr);
  478. if (sshdr.sense_key == RECOVERED_ERROR &&
  479. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  480. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  481. }
  482. /* Send userspace a few ATA registers (same as drivers/ide) */
  483. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  484. desc[0] == 0x09) { /* code is "ATA Descriptor" */
  485. args[0] = desc[13]; /* status */
  486. args[1] = desc[3]; /* error */
  487. args[2] = desc[5]; /* sector count (0:7) */
  488. if (copy_to_user(arg, args, sizeof(args)))
  489. rc = -EFAULT;
  490. }
  491. }
  492. if (cmd_result) {
  493. rc = -EIO;
  494. goto error;
  495. }
  496. if ((argbuf)
  497. && copy_to_user(arg + sizeof(args), argbuf, argsize))
  498. rc = -EFAULT;
  499. error:
  500. kfree(sensebuf);
  501. kfree(argbuf);
  502. return rc;
  503. }
  504. /**
  505. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  506. * @scsidev: Device to which we are issuing command
  507. * @arg: User provided data for issuing command
  508. *
  509. * LOCKING:
  510. * Defined by the SCSI layer. We don't really care.
  511. *
  512. * RETURNS:
  513. * Zero on success, negative errno on error.
  514. */
  515. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  516. {
  517. int rc = 0;
  518. u8 scsi_cmd[MAX_COMMAND_SIZE];
  519. u8 args[7], *sensebuf = NULL;
  520. int cmd_result;
  521. if (arg == NULL)
  522. return -EINVAL;
  523. if (copy_from_user(args, arg, sizeof(args)))
  524. return -EFAULT;
  525. sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  526. if (!sensebuf)
  527. return -ENOMEM;
  528. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  529. scsi_cmd[0] = ATA_16;
  530. scsi_cmd[1] = (3 << 1); /* Non-data */
  531. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  532. scsi_cmd[4] = args[1];
  533. scsi_cmd[6] = args[2];
  534. scsi_cmd[8] = args[3];
  535. scsi_cmd[10] = args[4];
  536. scsi_cmd[12] = args[5];
  537. scsi_cmd[13] = args[6] & 0x4f;
  538. scsi_cmd[14] = args[0];
  539. /* Good values for timeout and retries? Values below
  540. from scsi_ioctl_send_command() for default case... */
  541. cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
  542. sensebuf, (10*HZ), 5, 0, NULL);
  543. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  544. u8 *desc = sensebuf + 8;
  545. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  546. /* If we set cc then ATA pass-through will cause a
  547. * check condition even if no error. Filter that. */
  548. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  549. struct scsi_sense_hdr sshdr;
  550. scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
  551. &sshdr);
  552. if (sshdr.sense_key == RECOVERED_ERROR &&
  553. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  554. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  555. }
  556. /* Send userspace ATA registers */
  557. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  558. desc[0] == 0x09) {/* code is "ATA Descriptor" */
  559. args[0] = desc[13]; /* status */
  560. args[1] = desc[3]; /* error */
  561. args[2] = desc[5]; /* sector count (0:7) */
  562. args[3] = desc[7]; /* lbal */
  563. args[4] = desc[9]; /* lbam */
  564. args[5] = desc[11]; /* lbah */
  565. args[6] = desc[12]; /* select */
  566. if (copy_to_user(arg, args, sizeof(args)))
  567. rc = -EFAULT;
  568. }
  569. }
  570. if (cmd_result) {
  571. rc = -EIO;
  572. goto error;
  573. }
  574. error:
  575. kfree(sensebuf);
  576. return rc;
  577. }
  578. static int ata_ioc32(struct ata_port *ap)
  579. {
  580. if (ap->flags & ATA_FLAG_PIO_DMA)
  581. return 1;
  582. if (ap->pflags & ATA_PFLAG_PIO32)
  583. return 1;
  584. return 0;
  585. }
  586. int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
  587. int cmd, void __user *arg)
  588. {
  589. unsigned long val;
  590. int rc = -EINVAL;
  591. unsigned long flags;
  592. switch (cmd) {
  593. case HDIO_GET_32BIT:
  594. spin_lock_irqsave(ap->lock, flags);
  595. val = ata_ioc32(ap);
  596. spin_unlock_irqrestore(ap->lock, flags);
  597. return put_user(val, (unsigned long __user *)arg);
  598. case HDIO_SET_32BIT:
  599. val = (unsigned long) arg;
  600. rc = 0;
  601. spin_lock_irqsave(ap->lock, flags);
  602. if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
  603. if (val)
  604. ap->pflags |= ATA_PFLAG_PIO32;
  605. else
  606. ap->pflags &= ~ATA_PFLAG_PIO32;
  607. } else {
  608. if (val != ata_ioc32(ap))
  609. rc = -EINVAL;
  610. }
  611. spin_unlock_irqrestore(ap->lock, flags);
  612. return rc;
  613. case HDIO_GET_IDENTITY:
  614. return ata_get_identity(ap, scsidev, arg);
  615. case HDIO_DRIVE_CMD:
  616. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  617. return -EACCES;
  618. return ata_cmd_ioctl(scsidev, arg);
  619. case HDIO_DRIVE_TASK:
  620. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  621. return -EACCES;
  622. return ata_task_ioctl(scsidev, arg);
  623. default:
  624. rc = -ENOTTY;
  625. break;
  626. }
  627. return rc;
  628. }
  629. EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
  630. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  631. {
  632. return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
  633. scsidev, cmd, arg);
  634. }
  635. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  636. /**
  637. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  638. * @dev: ATA device to which the new command is attached
  639. * @cmd: SCSI command that originated this ATA command
  640. *
  641. * Obtain a reference to an unused ata_queued_cmd structure,
  642. * which is the basic libata structure representing a single
  643. * ATA command sent to the hardware.
  644. *
  645. * If a command was available, fill in the SCSI-specific
  646. * portions of the structure with information on the
  647. * current command.
  648. *
  649. * LOCKING:
  650. * spin_lock_irqsave(host lock)
  651. *
  652. * RETURNS:
  653. * Command allocated, or %NULL if none available.
  654. */
  655. static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
  656. struct scsi_cmnd *cmd)
  657. {
  658. struct ata_queued_cmd *qc;
  659. qc = ata_qc_new_init(dev, cmd->request->tag);
  660. if (qc) {
  661. qc->scsicmd = cmd;
  662. qc->scsidone = cmd->scsi_done;
  663. qc->sg = scsi_sglist(cmd);
  664. qc->n_elem = scsi_sg_count(cmd);
  665. } else {
  666. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  667. cmd->scsi_done(cmd);
  668. }
  669. return qc;
  670. }
  671. static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
  672. {
  673. struct scsi_cmnd *scmd = qc->scsicmd;
  674. qc->extrabytes = scmd->request->extra_len;
  675. qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
  676. }
  677. /**
  678. * ata_dump_status - user friendly display of error info
  679. * @id: id of the port in question
  680. * @tf: ptr to filled out taskfile
  681. *
  682. * Decode and dump the ATA error/status registers for the user so
  683. * that they have some idea what really happened at the non
  684. * make-believe layer.
  685. *
  686. * LOCKING:
  687. * inherited from caller
  688. */
  689. static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  690. {
  691. u8 stat = tf->command, err = tf->feature;
  692. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  693. if (stat & ATA_BUSY) {
  694. printk("Busy }\n"); /* Data is not valid in this case */
  695. } else {
  696. if (stat & ATA_DRDY) printk("DriveReady ");
  697. if (stat & ATA_DF) printk("DeviceFault ");
  698. if (stat & ATA_DSC) printk("SeekComplete ");
  699. if (stat & ATA_DRQ) printk("DataRequest ");
  700. if (stat & ATA_CORR) printk("CorrectedError ");
  701. if (stat & ATA_SENSE) printk("Sense ");
  702. if (stat & ATA_ERR) printk("Error ");
  703. printk("}\n");
  704. if (err) {
  705. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  706. if (err & ATA_ABORTED) printk("DriveStatusError ");
  707. if (err & ATA_ICRC) {
  708. if (err & ATA_ABORTED)
  709. printk("BadCRC ");
  710. else printk("Sector ");
  711. }
  712. if (err & ATA_UNC) printk("UncorrectableError ");
  713. if (err & ATA_IDNF) printk("SectorIdNotFound ");
  714. if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
  715. if (err & ATA_AMNF) printk("AddrMarkNotFound ");
  716. printk("}\n");
  717. }
  718. }
  719. }
  720. /**
  721. * ata_to_sense_error - convert ATA error to SCSI error
  722. * @id: ATA device number
  723. * @drv_stat: value contained in ATA status register
  724. * @drv_err: value contained in ATA error register
  725. * @sk: the sense key we'll fill out
  726. * @asc: the additional sense code we'll fill out
  727. * @ascq: the additional sense code qualifier we'll fill out
  728. * @verbose: be verbose
  729. *
  730. * Converts an ATA error into a SCSI error. Fill out pointers to
  731. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  732. * format sense blocks.
  733. *
  734. * LOCKING:
  735. * spin_lock_irqsave(host lock)
  736. */
  737. static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
  738. u8 *asc, u8 *ascq, int verbose)
  739. {
  740. int i;
  741. /* Based on the 3ware driver translation table */
  742. static const unsigned char sense_table[][4] = {
  743. /* BBD|ECC|ID|MAR */
  744. {0xd1, ABORTED_COMMAND, 0x00, 0x00},
  745. // Device busy Aborted command
  746. /* BBD|ECC|ID */
  747. {0xd0, ABORTED_COMMAND, 0x00, 0x00},
  748. // Device busy Aborted command
  749. /* ECC|MC|MARK */
  750. {0x61, HARDWARE_ERROR, 0x00, 0x00},
  751. // Device fault Hardware error
  752. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  753. {0x84, ABORTED_COMMAND, 0x47, 0x00},
  754. // Data CRC error SCSI parity error
  755. /* MC|ID|ABRT|TRK0|MARK */
  756. {0x37, NOT_READY, 0x04, 0x00},
  757. // Unit offline Not ready
  758. /* MCR|MARK */
  759. {0x09, NOT_READY, 0x04, 0x00},
  760. // Unrecovered disk error Not ready
  761. /* Bad address mark */
  762. {0x01, MEDIUM_ERROR, 0x13, 0x00},
  763. // Address mark not found for data field
  764. /* TRK0 - Track 0 not found */
  765. {0x02, HARDWARE_ERROR, 0x00, 0x00},
  766. // Hardware error
  767. /* Abort: 0x04 is not translated here, see below */
  768. /* Media change request */
  769. {0x08, NOT_READY, 0x04, 0x00},
  770. // FIXME: faking offline
  771. /* SRV/IDNF - ID not found */
  772. {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
  773. // Logical address out of range
  774. /* MC - Media Changed */
  775. {0x20, UNIT_ATTENTION, 0x28, 0x00},
  776. // Not ready to ready change, medium may have changed
  777. /* ECC - Uncorrectable ECC error */
  778. {0x40, MEDIUM_ERROR, 0x11, 0x04},
  779. // Unrecovered read error
  780. /* BBD - block marked bad */
  781. {0x80, MEDIUM_ERROR, 0x11, 0x04},
  782. // Block marked bad Medium error, unrecovered read error
  783. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  784. };
  785. static const unsigned char stat_table[][4] = {
  786. /* Must be first because BUSY means no other bits valid */
  787. {0x80, ABORTED_COMMAND, 0x47, 0x00},
  788. // Busy, fake parity for now
  789. {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
  790. // Device ready, unaligned write command
  791. {0x20, HARDWARE_ERROR, 0x44, 0x00},
  792. // Device fault, internal target failure
  793. {0x08, ABORTED_COMMAND, 0x47, 0x00},
  794. // Timed out in xfer, fake parity for now
  795. {0x04, RECOVERED_ERROR, 0x11, 0x00},
  796. // Recovered ECC error Medium error, recovered
  797. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  798. };
  799. /*
  800. * Is this an error we can process/parse
  801. */
  802. if (drv_stat & ATA_BUSY) {
  803. drv_err = 0; /* Ignore the err bits, they're invalid */
  804. }
  805. if (drv_err) {
  806. /* Look for drv_err */
  807. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  808. /* Look for best matches first */
  809. if ((sense_table[i][0] & drv_err) ==
  810. sense_table[i][0]) {
  811. *sk = sense_table[i][1];
  812. *asc = sense_table[i][2];
  813. *ascq = sense_table[i][3];
  814. goto translate_done;
  815. }
  816. }
  817. }
  818. /*
  819. * Fall back to interpreting status bits. Note that if the drv_err
  820. * has only the ABRT bit set, we decode drv_stat. ABRT by itself
  821. * is not descriptive enough.
  822. */
  823. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  824. if (stat_table[i][0] & drv_stat) {
  825. *sk = stat_table[i][1];
  826. *asc = stat_table[i][2];
  827. *ascq = stat_table[i][3];
  828. goto translate_done;
  829. }
  830. }
  831. /*
  832. * We need a sensible error return here, which is tricky, and one
  833. * that won't cause people to do things like return a disk wrongly.
  834. */
  835. *sk = ABORTED_COMMAND;
  836. *asc = 0x00;
  837. *ascq = 0x00;
  838. translate_done:
  839. if (verbose)
  840. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
  841. "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
  842. id, drv_stat, drv_err, *sk, *asc, *ascq);
  843. return;
  844. }
  845. /*
  846. * ata_gen_passthru_sense - Generate check condition sense block.
  847. * @qc: Command that completed.
  848. *
  849. * This function is specific to the ATA descriptor format sense
  850. * block specified for the ATA pass through commands. Regardless
  851. * of whether the command errored or not, return a sense
  852. * block. Copy all controller registers into the sense
  853. * block. If there was no error, we get the request from an ATA
  854. * passthrough command, so we use the following sense data:
  855. * sk = RECOVERED ERROR
  856. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  857. *
  858. *
  859. * LOCKING:
  860. * None.
  861. */
  862. static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
  863. {
  864. struct scsi_cmnd *cmd = qc->scsicmd;
  865. struct ata_taskfile *tf = &qc->result_tf;
  866. unsigned char *sb = cmd->sense_buffer;
  867. unsigned char *desc = sb + 8;
  868. int verbose = qc->ap->ops->error_handler == NULL;
  869. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  870. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  871. /*
  872. * Use ata_to_sense_error() to map status register bits
  873. * onto sense key, asc & ascq.
  874. */
  875. if (qc->err_mask ||
  876. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  877. ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
  878. &sb[1], &sb[2], &sb[3], verbose);
  879. sb[1] &= 0x0f;
  880. } else {
  881. sb[1] = RECOVERED_ERROR;
  882. sb[2] = 0;
  883. sb[3] = 0x1D;
  884. }
  885. /*
  886. * Sense data is current and format is descriptor.
  887. */
  888. sb[0] = 0x72;
  889. desc[0] = 0x09;
  890. /* set length of additional sense data */
  891. sb[7] = 14;
  892. desc[1] = 12;
  893. /*
  894. * Copy registers into sense buffer.
  895. */
  896. desc[2] = 0x00;
  897. desc[3] = tf->feature; /* == error reg */
  898. desc[5] = tf->nsect;
  899. desc[7] = tf->lbal;
  900. desc[9] = tf->lbam;
  901. desc[11] = tf->lbah;
  902. desc[12] = tf->device;
  903. desc[13] = tf->command; /* == status reg */
  904. /*
  905. * Fill in Extend bit, and the high order bytes
  906. * if applicable.
  907. */
  908. if (tf->flags & ATA_TFLAG_LBA48) {
  909. desc[2] |= 0x01;
  910. desc[4] = tf->hob_nsect;
  911. desc[6] = tf->hob_lbal;
  912. desc[8] = tf->hob_lbam;
  913. desc[10] = tf->hob_lbah;
  914. }
  915. }
  916. /**
  917. * ata_gen_ata_sense - generate a SCSI fixed sense block
  918. * @qc: Command that we are erroring out
  919. *
  920. * Generate sense block for a failed ATA command @qc. Descriptor
  921. * format is used to accommodate LBA48 block address.
  922. *
  923. * LOCKING:
  924. * None.
  925. */
  926. static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
  927. {
  928. struct ata_device *dev = qc->dev;
  929. struct scsi_cmnd *cmd = qc->scsicmd;
  930. struct ata_taskfile *tf = &qc->result_tf;
  931. unsigned char *sb = cmd->sense_buffer;
  932. unsigned char *desc = sb + 8;
  933. int verbose = qc->ap->ops->error_handler == NULL;
  934. u64 block;
  935. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  936. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  937. /* sense data is current and format is descriptor */
  938. sb[0] = 0x72;
  939. /* Use ata_to_sense_error() to map status register bits
  940. * onto sense key, asc & ascq.
  941. */
  942. if (qc->err_mask ||
  943. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  944. ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
  945. &sb[1], &sb[2], &sb[3], verbose);
  946. sb[1] &= 0x0f;
  947. }
  948. block = ata_tf_read_block(&qc->result_tf, dev);
  949. /* information sense data descriptor */
  950. sb[7] = 12;
  951. desc[0] = 0x00;
  952. desc[1] = 10;
  953. desc[2] |= 0x80; /* valid */
  954. desc[6] = block >> 40;
  955. desc[7] = block >> 32;
  956. desc[8] = block >> 24;
  957. desc[9] = block >> 16;
  958. desc[10] = block >> 8;
  959. desc[11] = block;
  960. }
  961. static void ata_scsi_sdev_config(struct scsi_device *sdev)
  962. {
  963. sdev->use_10_for_rw = 1;
  964. sdev->use_10_for_ms = 1;
  965. sdev->no_report_opcodes = 1;
  966. sdev->no_write_same = 1;
  967. /* Schedule policy is determined by ->qc_defer() callback and
  968. * it needs to see every deferred qc. Set dev_blocked to 1 to
  969. * prevent SCSI midlayer from automatically deferring
  970. * requests.
  971. */
  972. sdev->max_device_blocked = 1;
  973. }
  974. /**
  975. * atapi_drain_needed - Check whether data transfer may overflow
  976. * @rq: request to be checked
  977. *
  978. * ATAPI commands which transfer variable length data to host
  979. * might overflow due to application error or hardare bug. This
  980. * function checks whether overflow should be drained and ignored
  981. * for @request.
  982. *
  983. * LOCKING:
  984. * None.
  985. *
  986. * RETURNS:
  987. * 1 if ; otherwise, 0.
  988. */
  989. static int atapi_drain_needed(struct request *rq)
  990. {
  991. if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
  992. return 0;
  993. if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
  994. return 0;
  995. return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
  996. }
  997. static int ata_scsi_dev_config(struct scsi_device *sdev,
  998. struct ata_device *dev)
  999. {
  1000. struct request_queue *q = sdev->request_queue;
  1001. if (!ata_id_has_unload(dev->id))
  1002. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  1003. /* configure max sectors */
  1004. blk_queue_max_hw_sectors(q, dev->max_sectors);
  1005. if (dev->class == ATA_DEV_ATAPI) {
  1006. void *buf;
  1007. sdev->sector_size = ATA_SECT_SIZE;
  1008. /* set DMA padding */
  1009. blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
  1010. /* configure draining */
  1011. buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
  1012. if (!buf) {
  1013. ata_dev_err(dev, "drain buffer allocation failed\n");
  1014. return -ENOMEM;
  1015. }
  1016. blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
  1017. } else {
  1018. sdev->sector_size = ata_id_logical_sector_size(dev->id);
  1019. sdev->manage_start_stop = 1;
  1020. }
  1021. /*
  1022. * ata_pio_sectors() expects buffer for each sector to not cross
  1023. * page boundary. Enforce it by requiring buffers to be sector
  1024. * aligned, which works iff sector_size is not larger than
  1025. * PAGE_SIZE. ATAPI devices also need the alignment as
  1026. * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
  1027. */
  1028. if (sdev->sector_size > PAGE_SIZE)
  1029. ata_dev_warn(dev,
  1030. "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
  1031. sdev->sector_size);
  1032. blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
  1033. if (dev->flags & ATA_DFLAG_AN)
  1034. set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
  1035. if (dev->flags & ATA_DFLAG_NCQ) {
  1036. int depth;
  1037. depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  1038. depth = min(ATA_MAX_QUEUE - 1, depth);
  1039. scsi_change_queue_depth(sdev, depth);
  1040. }
  1041. blk_queue_flush_queueable(q, false);
  1042. dev->sdev = sdev;
  1043. return 0;
  1044. }
  1045. /**
  1046. * ata_scsi_slave_config - Set SCSI device attributes
  1047. * @sdev: SCSI device to examine
  1048. *
  1049. * This is called before we actually start reading
  1050. * and writing to the device, to configure certain
  1051. * SCSI mid-layer behaviors.
  1052. *
  1053. * LOCKING:
  1054. * Defined by SCSI layer. We don't really care.
  1055. */
  1056. int ata_scsi_slave_config(struct scsi_device *sdev)
  1057. {
  1058. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1059. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  1060. int rc = 0;
  1061. ata_scsi_sdev_config(sdev);
  1062. if (dev)
  1063. rc = ata_scsi_dev_config(sdev, dev);
  1064. return rc;
  1065. }
  1066. /**
  1067. * ata_scsi_slave_destroy - SCSI device is about to be destroyed
  1068. * @sdev: SCSI device to be destroyed
  1069. *
  1070. * @sdev is about to be destroyed for hot/warm unplugging. If
  1071. * this unplugging was initiated by libata as indicated by NULL
  1072. * dev->sdev, this function doesn't have to do anything.
  1073. * Otherwise, SCSI layer initiated warm-unplug is in progress.
  1074. * Clear dev->sdev, schedule the device for ATA detach and invoke
  1075. * EH.
  1076. *
  1077. * LOCKING:
  1078. * Defined by SCSI layer. We don't really care.
  1079. */
  1080. void ata_scsi_slave_destroy(struct scsi_device *sdev)
  1081. {
  1082. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1083. struct request_queue *q = sdev->request_queue;
  1084. unsigned long flags;
  1085. struct ata_device *dev;
  1086. if (!ap->ops->error_handler)
  1087. return;
  1088. spin_lock_irqsave(ap->lock, flags);
  1089. dev = __ata_scsi_find_dev(ap, sdev);
  1090. if (dev && dev->sdev) {
  1091. /* SCSI device already in CANCEL state, no need to offline it */
  1092. dev->sdev = NULL;
  1093. dev->flags |= ATA_DFLAG_DETACH;
  1094. ata_port_schedule_eh(ap);
  1095. }
  1096. spin_unlock_irqrestore(ap->lock, flags);
  1097. kfree(q->dma_drain_buffer);
  1098. q->dma_drain_buffer = NULL;
  1099. q->dma_drain_size = 0;
  1100. }
  1101. /**
  1102. * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
  1103. * @ap: ATA port to which the device change the queue depth
  1104. * @sdev: SCSI device to configure queue depth for
  1105. * @queue_depth: new queue depth
  1106. *
  1107. * libsas and libata have different approaches for associating a sdev to
  1108. * its ata_port.
  1109. *
  1110. */
  1111. int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
  1112. int queue_depth)
  1113. {
  1114. struct ata_device *dev;
  1115. unsigned long flags;
  1116. if (queue_depth < 1 || queue_depth == sdev->queue_depth)
  1117. return sdev->queue_depth;
  1118. dev = ata_scsi_find_dev(ap, sdev);
  1119. if (!dev || !ata_dev_enabled(dev))
  1120. return sdev->queue_depth;
  1121. /* NCQ enabled? */
  1122. spin_lock_irqsave(ap->lock, flags);
  1123. dev->flags &= ~ATA_DFLAG_NCQ_OFF;
  1124. if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
  1125. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1126. queue_depth = 1;
  1127. }
  1128. spin_unlock_irqrestore(ap->lock, flags);
  1129. /* limit and apply queue depth */
  1130. queue_depth = min(queue_depth, sdev->host->can_queue);
  1131. queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
  1132. queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
  1133. if (sdev->queue_depth == queue_depth)
  1134. return -EINVAL;
  1135. return scsi_change_queue_depth(sdev, queue_depth);
  1136. }
  1137. /**
  1138. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  1139. * @sdev: SCSI device to configure queue depth for
  1140. * @queue_depth: new queue depth
  1141. *
  1142. * This is libata standard hostt->change_queue_depth callback.
  1143. * SCSI will call into this callback when user tries to set queue
  1144. * depth via sysfs.
  1145. *
  1146. * LOCKING:
  1147. * SCSI layer (we don't care)
  1148. *
  1149. * RETURNS:
  1150. * Newly configured queue depth.
  1151. */
  1152. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  1153. {
  1154. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1155. return __ata_change_queue_depth(ap, sdev, queue_depth);
  1156. }
  1157. /**
  1158. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  1159. * @qc: Storage for translated ATA taskfile
  1160. *
  1161. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  1162. * (to start). Perhaps these commands should be preceded by
  1163. * CHECK POWER MODE to see what power mode the device is already in.
  1164. * [See SAT revision 5 at www.t10.org]
  1165. *
  1166. * LOCKING:
  1167. * spin_lock_irqsave(host lock)
  1168. *
  1169. * RETURNS:
  1170. * Zero on success, non-zero on error.
  1171. */
  1172. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
  1173. {
  1174. struct scsi_cmnd *scmd = qc->scsicmd;
  1175. struct ata_taskfile *tf = &qc->tf;
  1176. const u8 *cdb = scmd->cmnd;
  1177. if (scmd->cmd_len < 5)
  1178. goto invalid_fld;
  1179. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1180. tf->protocol = ATA_PROT_NODATA;
  1181. if (cdb[1] & 0x1) {
  1182. ; /* ignore IMMED bit, violates sat-r05 */
  1183. }
  1184. if (cdb[4] & 0x2)
  1185. goto invalid_fld; /* LOEJ bit set not supported */
  1186. if (((cdb[4] >> 4) & 0xf) != 0)
  1187. goto invalid_fld; /* power conditions not supported */
  1188. if (cdb[4] & 0x1) {
  1189. tf->nsect = 1; /* 1 sector, lba=0 */
  1190. if (qc->dev->flags & ATA_DFLAG_LBA) {
  1191. tf->flags |= ATA_TFLAG_LBA;
  1192. tf->lbah = 0x0;
  1193. tf->lbam = 0x0;
  1194. tf->lbal = 0x0;
  1195. tf->device |= ATA_LBA;
  1196. } else {
  1197. /* CHS */
  1198. tf->lbal = 0x1; /* sect */
  1199. tf->lbam = 0x0; /* cyl low */
  1200. tf->lbah = 0x0; /* cyl high */
  1201. }
  1202. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  1203. } else {
  1204. /* Some odd clown BIOSen issue spindown on power off (ACPI S4
  1205. * or S5) causing some drives to spin up and down again.
  1206. */
  1207. if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
  1208. system_state == SYSTEM_POWER_OFF)
  1209. goto skip;
  1210. if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
  1211. system_entering_hibernation())
  1212. goto skip;
  1213. /* Issue ATA STANDBY IMMEDIATE command */
  1214. tf->command = ATA_CMD_STANDBYNOW1;
  1215. }
  1216. /*
  1217. * Standby and Idle condition timers could be implemented but that
  1218. * would require libata to implement the Power condition mode page
  1219. * and allow the user to change it. Changing mode pages requires
  1220. * MODE SELECT to be implemented.
  1221. */
  1222. return 0;
  1223. invalid_fld:
  1224. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1225. /* "Invalid field in cbd" */
  1226. return 1;
  1227. skip:
  1228. scmd->result = SAM_STAT_GOOD;
  1229. return 1;
  1230. }
  1231. /**
  1232. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  1233. * @qc: Storage for translated ATA taskfile
  1234. *
  1235. * Sets up an ATA taskfile to issue FLUSH CACHE or
  1236. * FLUSH CACHE EXT.
  1237. *
  1238. * LOCKING:
  1239. * spin_lock_irqsave(host lock)
  1240. *
  1241. * RETURNS:
  1242. * Zero on success, non-zero on error.
  1243. */
  1244. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
  1245. {
  1246. struct ata_taskfile *tf = &qc->tf;
  1247. tf->flags |= ATA_TFLAG_DEVICE;
  1248. tf->protocol = ATA_PROT_NODATA;
  1249. if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
  1250. tf->command = ATA_CMD_FLUSH_EXT;
  1251. else
  1252. tf->command = ATA_CMD_FLUSH;
  1253. /* flush is critical for IO integrity, consider it an IO command */
  1254. qc->flags |= ATA_QCFLAG_IO;
  1255. return 0;
  1256. }
  1257. /**
  1258. * scsi_6_lba_len - Get LBA and transfer length
  1259. * @cdb: SCSI command to translate
  1260. *
  1261. * Calculate LBA and transfer length for 6-byte commands.
  1262. *
  1263. * RETURNS:
  1264. * @plba: the LBA
  1265. * @plen: the transfer length
  1266. */
  1267. static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1268. {
  1269. u64 lba = 0;
  1270. u32 len;
  1271. VPRINTK("six-byte command\n");
  1272. lba |= ((u64)(cdb[1] & 0x1f)) << 16;
  1273. lba |= ((u64)cdb[2]) << 8;
  1274. lba |= ((u64)cdb[3]);
  1275. len = cdb[4];
  1276. *plba = lba;
  1277. *plen = len;
  1278. }
  1279. /**
  1280. * scsi_10_lba_len - Get LBA and transfer length
  1281. * @cdb: SCSI command to translate
  1282. *
  1283. * Calculate LBA and transfer length for 10-byte commands.
  1284. *
  1285. * RETURNS:
  1286. * @plba: the LBA
  1287. * @plen: the transfer length
  1288. */
  1289. static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1290. {
  1291. u64 lba = 0;
  1292. u32 len = 0;
  1293. VPRINTK("ten-byte command\n");
  1294. lba |= ((u64)cdb[2]) << 24;
  1295. lba |= ((u64)cdb[3]) << 16;
  1296. lba |= ((u64)cdb[4]) << 8;
  1297. lba |= ((u64)cdb[5]);
  1298. len |= ((u32)cdb[7]) << 8;
  1299. len |= ((u32)cdb[8]);
  1300. *plba = lba;
  1301. *plen = len;
  1302. }
  1303. /**
  1304. * scsi_16_lba_len - Get LBA and transfer length
  1305. * @cdb: SCSI command to translate
  1306. *
  1307. * Calculate LBA and transfer length for 16-byte commands.
  1308. *
  1309. * RETURNS:
  1310. * @plba: the LBA
  1311. * @plen: the transfer length
  1312. */
  1313. static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1314. {
  1315. u64 lba = 0;
  1316. u32 len = 0;
  1317. VPRINTK("sixteen-byte command\n");
  1318. lba |= ((u64)cdb[2]) << 56;
  1319. lba |= ((u64)cdb[3]) << 48;
  1320. lba |= ((u64)cdb[4]) << 40;
  1321. lba |= ((u64)cdb[5]) << 32;
  1322. lba |= ((u64)cdb[6]) << 24;
  1323. lba |= ((u64)cdb[7]) << 16;
  1324. lba |= ((u64)cdb[8]) << 8;
  1325. lba |= ((u64)cdb[9]);
  1326. len |= ((u32)cdb[10]) << 24;
  1327. len |= ((u32)cdb[11]) << 16;
  1328. len |= ((u32)cdb[12]) << 8;
  1329. len |= ((u32)cdb[13]);
  1330. *plba = lba;
  1331. *plen = len;
  1332. }
  1333. /**
  1334. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  1335. * @qc: Storage for translated ATA taskfile
  1336. *
  1337. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  1338. *
  1339. * LOCKING:
  1340. * spin_lock_irqsave(host lock)
  1341. *
  1342. * RETURNS:
  1343. * Zero on success, non-zero on error.
  1344. */
  1345. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
  1346. {
  1347. struct scsi_cmnd *scmd = qc->scsicmd;
  1348. struct ata_taskfile *tf = &qc->tf;
  1349. struct ata_device *dev = qc->dev;
  1350. u64 dev_sectors = qc->dev->n_sectors;
  1351. const u8 *cdb = scmd->cmnd;
  1352. u64 block;
  1353. u32 n_block;
  1354. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1355. tf->protocol = ATA_PROT_NODATA;
  1356. if (cdb[0] == VERIFY) {
  1357. if (scmd->cmd_len < 10)
  1358. goto invalid_fld;
  1359. scsi_10_lba_len(cdb, &block, &n_block);
  1360. } else if (cdb[0] == VERIFY_16) {
  1361. if (scmd->cmd_len < 16)
  1362. goto invalid_fld;
  1363. scsi_16_lba_len(cdb, &block, &n_block);
  1364. } else
  1365. goto invalid_fld;
  1366. if (!n_block)
  1367. goto nothing_to_do;
  1368. if (block >= dev_sectors)
  1369. goto out_of_range;
  1370. if ((block + n_block) > dev_sectors)
  1371. goto out_of_range;
  1372. if (dev->flags & ATA_DFLAG_LBA) {
  1373. tf->flags |= ATA_TFLAG_LBA;
  1374. if (lba_28_ok(block, n_block)) {
  1375. /* use LBA28 */
  1376. tf->command = ATA_CMD_VERIFY;
  1377. tf->device |= (block >> 24) & 0xf;
  1378. } else if (lba_48_ok(block, n_block)) {
  1379. if (!(dev->flags & ATA_DFLAG_LBA48))
  1380. goto out_of_range;
  1381. /* use LBA48 */
  1382. tf->flags |= ATA_TFLAG_LBA48;
  1383. tf->command = ATA_CMD_VERIFY_EXT;
  1384. tf->hob_nsect = (n_block >> 8) & 0xff;
  1385. tf->hob_lbah = (block >> 40) & 0xff;
  1386. tf->hob_lbam = (block >> 32) & 0xff;
  1387. tf->hob_lbal = (block >> 24) & 0xff;
  1388. } else
  1389. /* request too large even for LBA48 */
  1390. goto out_of_range;
  1391. tf->nsect = n_block & 0xff;
  1392. tf->lbah = (block >> 16) & 0xff;
  1393. tf->lbam = (block >> 8) & 0xff;
  1394. tf->lbal = block & 0xff;
  1395. tf->device |= ATA_LBA;
  1396. } else {
  1397. /* CHS */
  1398. u32 sect, head, cyl, track;
  1399. if (!lba_28_ok(block, n_block))
  1400. goto out_of_range;
  1401. /* Convert LBA to CHS */
  1402. track = (u32)block / dev->sectors;
  1403. cyl = track / dev->heads;
  1404. head = track % dev->heads;
  1405. sect = (u32)block % dev->sectors + 1;
  1406. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1407. (u32)block, track, cyl, head, sect);
  1408. /* Check whether the converted CHS can fit.
  1409. Cylinder: 0-65535
  1410. Head: 0-15
  1411. Sector: 1-255*/
  1412. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1413. goto out_of_range;
  1414. tf->command = ATA_CMD_VERIFY;
  1415. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1416. tf->lbal = sect;
  1417. tf->lbam = cyl;
  1418. tf->lbah = cyl >> 8;
  1419. tf->device |= head;
  1420. }
  1421. return 0;
  1422. invalid_fld:
  1423. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1424. /* "Invalid field in cbd" */
  1425. return 1;
  1426. out_of_range:
  1427. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1428. /* "Logical Block Address out of range" */
  1429. return 1;
  1430. nothing_to_do:
  1431. scmd->result = SAM_STAT_GOOD;
  1432. return 1;
  1433. }
  1434. /**
  1435. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  1436. * @qc: Storage for translated ATA taskfile
  1437. *
  1438. * Converts any of six SCSI read/write commands into the
  1439. * ATA counterpart, including starting sector (LBA),
  1440. * sector count, and taking into account the device's LBA48
  1441. * support.
  1442. *
  1443. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  1444. * %WRITE_16 are currently supported.
  1445. *
  1446. * LOCKING:
  1447. * spin_lock_irqsave(host lock)
  1448. *
  1449. * RETURNS:
  1450. * Zero on success, non-zero on error.
  1451. */
  1452. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
  1453. {
  1454. struct scsi_cmnd *scmd = qc->scsicmd;
  1455. const u8 *cdb = scmd->cmnd;
  1456. unsigned int tf_flags = 0;
  1457. u64 block;
  1458. u32 n_block;
  1459. int rc;
  1460. if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
  1461. tf_flags |= ATA_TFLAG_WRITE;
  1462. /* Calculate the SCSI LBA, transfer length and FUA. */
  1463. switch (cdb[0]) {
  1464. case READ_10:
  1465. case WRITE_10:
  1466. if (unlikely(scmd->cmd_len < 10))
  1467. goto invalid_fld;
  1468. scsi_10_lba_len(cdb, &block, &n_block);
  1469. if (cdb[1] & (1 << 3))
  1470. tf_flags |= ATA_TFLAG_FUA;
  1471. break;
  1472. case READ_6:
  1473. case WRITE_6:
  1474. if (unlikely(scmd->cmd_len < 6))
  1475. goto invalid_fld;
  1476. scsi_6_lba_len(cdb, &block, &n_block);
  1477. /* for 6-byte r/w commands, transfer length 0
  1478. * means 256 blocks of data, not 0 block.
  1479. */
  1480. if (!n_block)
  1481. n_block = 256;
  1482. break;
  1483. case READ_16:
  1484. case WRITE_16:
  1485. if (unlikely(scmd->cmd_len < 16))
  1486. goto invalid_fld;
  1487. scsi_16_lba_len(cdb, &block, &n_block);
  1488. if (cdb[1] & (1 << 3))
  1489. tf_flags |= ATA_TFLAG_FUA;
  1490. break;
  1491. default:
  1492. DPRINTK("no-byte command\n");
  1493. goto invalid_fld;
  1494. }
  1495. /* Check and compose ATA command */
  1496. if (!n_block)
  1497. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1498. * length 0 means transfer 0 block of data.
  1499. * However, for ATA R/W commands, sector count 0 means
  1500. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1501. *
  1502. * WARNING: one or two older ATA drives treat 0 as 0...
  1503. */
  1504. goto nothing_to_do;
  1505. qc->flags |= ATA_QCFLAG_IO;
  1506. qc->nbytes = n_block * scmd->device->sector_size;
  1507. rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
  1508. qc->tag);
  1509. if (likely(rc == 0))
  1510. return 0;
  1511. if (rc == -ERANGE)
  1512. goto out_of_range;
  1513. /* treat all other errors as -EINVAL, fall through */
  1514. invalid_fld:
  1515. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1516. /* "Invalid field in cbd" */
  1517. return 1;
  1518. out_of_range:
  1519. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1520. /* "Logical Block Address out of range" */
  1521. return 1;
  1522. nothing_to_do:
  1523. scmd->result = SAM_STAT_GOOD;
  1524. return 1;
  1525. }
  1526. static void ata_qc_done(struct ata_queued_cmd *qc)
  1527. {
  1528. struct scsi_cmnd *cmd = qc->scsicmd;
  1529. void (*done)(struct scsi_cmnd *) = qc->scsidone;
  1530. ata_qc_free(qc);
  1531. done(cmd);
  1532. }
  1533. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1534. {
  1535. struct ata_port *ap = qc->ap;
  1536. struct scsi_cmnd *cmd = qc->scsicmd;
  1537. u8 *cdb = cmd->cmnd;
  1538. int need_sense = (qc->err_mask != 0);
  1539. /* For ATA pass thru (SAT) commands, generate a sense block if
  1540. * user mandated it or if there's an error. Note that if we
  1541. * generate because the user forced us to [CK_COND =1], a check
  1542. * condition is generated and the ATA register values are returned
  1543. * whether the command completed successfully or not. If there
  1544. * was no error, we use the following sense data:
  1545. * sk = RECOVERED ERROR
  1546. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  1547. */
  1548. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1549. ((cdb[2] & 0x20) || need_sense))
  1550. ata_gen_passthru_sense(qc);
  1551. else if (need_sense)
  1552. ata_gen_ata_sense(qc);
  1553. else
  1554. cmd->result = SAM_STAT_GOOD;
  1555. if (need_sense && !ap->ops->error_handler)
  1556. ata_dump_status(ap->print_id, &qc->result_tf);
  1557. ata_qc_done(qc);
  1558. }
  1559. /**
  1560. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1561. * @dev: ATA device to which the command is addressed
  1562. * @cmd: SCSI command to execute
  1563. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1564. *
  1565. * Our ->queuecommand() function has decided that the SCSI
  1566. * command issued can be directly translated into an ATA
  1567. * command, rather than handled internally.
  1568. *
  1569. * This function sets up an ata_queued_cmd structure for the
  1570. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1571. *
  1572. * The xlat_func argument (actor) returns 0 if ready to execute
  1573. * ATA command, else 1 to finish translation. If 1 is returned
  1574. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1575. * to be set reflecting an error condition or clean (early)
  1576. * termination.
  1577. *
  1578. * LOCKING:
  1579. * spin_lock_irqsave(host lock)
  1580. *
  1581. * RETURNS:
  1582. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1583. * needs to be deferred.
  1584. */
  1585. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1586. ata_xlat_func_t xlat_func)
  1587. {
  1588. struct ata_port *ap = dev->link->ap;
  1589. struct ata_queued_cmd *qc;
  1590. int rc;
  1591. VPRINTK("ENTER\n");
  1592. qc = ata_scsi_qc_new(dev, cmd);
  1593. if (!qc)
  1594. goto err_mem;
  1595. /* data is present; dma-map it */
  1596. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1597. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1598. if (unlikely(scsi_bufflen(cmd) < 1)) {
  1599. ata_dev_warn(dev, "WARNING: zero len r/w req\n");
  1600. goto err_did;
  1601. }
  1602. ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
  1603. qc->dma_dir = cmd->sc_data_direction;
  1604. }
  1605. qc->complete_fn = ata_scsi_qc_complete;
  1606. if (xlat_func(qc))
  1607. goto early_finish;
  1608. if (ap->ops->qc_defer) {
  1609. if ((rc = ap->ops->qc_defer(qc)))
  1610. goto defer;
  1611. }
  1612. /* select device, send command to hardware */
  1613. ata_qc_issue(qc);
  1614. VPRINTK("EXIT\n");
  1615. return 0;
  1616. early_finish:
  1617. ata_qc_free(qc);
  1618. cmd->scsi_done(cmd);
  1619. DPRINTK("EXIT - early finish (good or error)\n");
  1620. return 0;
  1621. err_did:
  1622. ata_qc_free(qc);
  1623. cmd->result = (DID_ERROR << 16);
  1624. cmd->scsi_done(cmd);
  1625. err_mem:
  1626. DPRINTK("EXIT - internal\n");
  1627. return 0;
  1628. defer:
  1629. ata_qc_free(qc);
  1630. DPRINTK("EXIT - defer\n");
  1631. if (rc == ATA_DEFER_LINK)
  1632. return SCSI_MLQUEUE_DEVICE_BUSY;
  1633. else
  1634. return SCSI_MLQUEUE_HOST_BUSY;
  1635. }
  1636. /**
  1637. * ata_scsi_rbuf_get - Map response buffer.
  1638. * @cmd: SCSI command containing buffer to be mapped.
  1639. * @flags: unsigned long variable to store irq enable status
  1640. * @copy_in: copy in from user buffer
  1641. *
  1642. * Prepare buffer for simulated SCSI commands.
  1643. *
  1644. * LOCKING:
  1645. * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  1646. *
  1647. * RETURNS:
  1648. * Pointer to response buffer.
  1649. */
  1650. static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
  1651. unsigned long *flags)
  1652. {
  1653. spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
  1654. memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
  1655. if (copy_in)
  1656. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1657. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1658. return ata_scsi_rbuf;
  1659. }
  1660. /**
  1661. * ata_scsi_rbuf_put - Unmap response buffer.
  1662. * @cmd: SCSI command containing buffer to be unmapped.
  1663. * @copy_out: copy out result
  1664. * @flags: @flags passed to ata_scsi_rbuf_get()
  1665. *
  1666. * Returns rbuf buffer. The result is copied to @cmd's buffer if
  1667. * @copy_back is true.
  1668. *
  1669. * LOCKING:
  1670. * Unlocks ata_scsi_rbuf_lock.
  1671. */
  1672. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
  1673. unsigned long *flags)
  1674. {
  1675. if (copy_out)
  1676. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1677. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1678. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
  1679. }
  1680. /**
  1681. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1682. * @args: device IDENTIFY data / SCSI command of interest.
  1683. * @actor: Callback hook for desired SCSI command simulator
  1684. *
  1685. * Takes care of the hard work of simulating a SCSI command...
  1686. * Mapping the response buffer, calling the command's handler,
  1687. * and handling the handler's return value. This return value
  1688. * indicates whether the handler wishes the SCSI command to be
  1689. * completed successfully (0), or not (in which case cmd->result
  1690. * and sense buffer are assumed to be set).
  1691. *
  1692. * LOCKING:
  1693. * spin_lock_irqsave(host lock)
  1694. */
  1695. static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1696. unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
  1697. {
  1698. u8 *rbuf;
  1699. unsigned int rc;
  1700. struct scsi_cmnd *cmd = args->cmd;
  1701. unsigned long flags;
  1702. rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
  1703. rc = actor(args, rbuf);
  1704. ata_scsi_rbuf_put(cmd, rc == 0, &flags);
  1705. if (rc == 0)
  1706. cmd->result = SAM_STAT_GOOD;
  1707. args->done(cmd);
  1708. }
  1709. /**
  1710. * ata_scsiop_inq_std - Simulate INQUIRY command
  1711. * @args: device IDENTIFY data / SCSI command of interest.
  1712. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1713. *
  1714. * Returns standard device identification data associated
  1715. * with non-VPD INQUIRY command output.
  1716. *
  1717. * LOCKING:
  1718. * spin_lock_irqsave(host lock)
  1719. */
  1720. static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
  1721. {
  1722. const u8 versions[] = {
  1723. 0x00,
  1724. 0x60, /* SAM-3 (no version claimed) */
  1725. 0x03,
  1726. 0x20, /* SBC-2 (no version claimed) */
  1727. 0x02,
  1728. 0x60 /* SPC-3 (no version claimed) */
  1729. };
  1730. const u8 versions_zbc[] = {
  1731. 0x00,
  1732. 0xA0, /* SAM-5 (no version claimed) */
  1733. 0x04,
  1734. 0xC0, /* SBC-3 (no version claimed) */
  1735. 0x04,
  1736. 0x60, /* SPC-4 (no version claimed) */
  1737. 0x60,
  1738. 0x20, /* ZBC (no version claimed) */
  1739. };
  1740. u8 hdr[] = {
  1741. TYPE_DISK,
  1742. 0,
  1743. 0x5, /* claim SPC-3 version compatibility */
  1744. 2,
  1745. 95 - 4
  1746. };
  1747. VPRINTK("ENTER\n");
  1748. /* set scsi removable (RMB) bit per ata bit, or if the
  1749. * AHCI port says it's external (Hotplug-capable, eSATA).
  1750. */
  1751. if (ata_id_removable(args->id) ||
  1752. (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
  1753. hdr[1] |= (1 << 7);
  1754. if (args->dev->class == ATA_DEV_ZAC) {
  1755. hdr[0] = TYPE_ZBC;
  1756. hdr[2] = 0x6; /* ZBC is defined in SPC-4 */
  1757. }
  1758. memcpy(rbuf, hdr, sizeof(hdr));
  1759. memcpy(&rbuf[8], "ATA ", 8);
  1760. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
  1761. /* From SAT, use last 2 words from fw rev unless they are spaces */
  1762. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
  1763. if (strncmp(&rbuf[32], " ", 4) == 0)
  1764. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1765. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1766. memcpy(&rbuf[32], "n/a ", 4);
  1767. if (args->dev->class == ATA_DEV_ZAC)
  1768. memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
  1769. else
  1770. memcpy(rbuf + 58, versions, sizeof(versions));
  1771. return 0;
  1772. }
  1773. /**
  1774. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1775. * @args: device IDENTIFY data / SCSI command of interest.
  1776. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1777. *
  1778. * Returns list of inquiry VPD pages available.
  1779. *
  1780. * LOCKING:
  1781. * spin_lock_irqsave(host lock)
  1782. */
  1783. static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
  1784. {
  1785. const u8 pages[] = {
  1786. 0x00, /* page 0x00, this page */
  1787. 0x80, /* page 0x80, unit serial no page */
  1788. 0x83, /* page 0x83, device ident page */
  1789. 0x89, /* page 0x89, ata info page */
  1790. 0xb0, /* page 0xb0, block limits page */
  1791. 0xb1, /* page 0xb1, block device characteristics page */
  1792. 0xb2, /* page 0xb2, thin provisioning page */
  1793. };
  1794. rbuf[3] = sizeof(pages); /* number of supported VPD pages */
  1795. memcpy(rbuf + 4, pages, sizeof(pages));
  1796. return 0;
  1797. }
  1798. /**
  1799. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1800. * @args: device IDENTIFY data / SCSI command of interest.
  1801. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1802. *
  1803. * Returns ATA device serial number.
  1804. *
  1805. * LOCKING:
  1806. * spin_lock_irqsave(host lock)
  1807. */
  1808. static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
  1809. {
  1810. const u8 hdr[] = {
  1811. 0,
  1812. 0x80, /* this page code */
  1813. 0,
  1814. ATA_ID_SERNO_LEN, /* page len */
  1815. };
  1816. memcpy(rbuf, hdr, sizeof(hdr));
  1817. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1818. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1819. return 0;
  1820. }
  1821. /**
  1822. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1823. * @args: device IDENTIFY data / SCSI command of interest.
  1824. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1825. *
  1826. * Yields two logical unit device identification designators:
  1827. * - vendor specific ASCII containing the ATA serial number
  1828. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1829. * name ("ATA "), model and serial numbers.
  1830. *
  1831. * LOCKING:
  1832. * spin_lock_irqsave(host lock)
  1833. */
  1834. static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
  1835. {
  1836. const int sat_model_serial_desc_len = 68;
  1837. int num;
  1838. rbuf[1] = 0x83; /* this page code */
  1839. num = 4;
  1840. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1841. rbuf[num + 0] = 2;
  1842. rbuf[num + 3] = ATA_ID_SERNO_LEN;
  1843. num += 4;
  1844. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1845. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1846. num += ATA_ID_SERNO_LEN;
  1847. /* SAT defined lu model and serial numbers descriptor */
  1848. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  1849. rbuf[num + 0] = 2;
  1850. rbuf[num + 1] = 1;
  1851. rbuf[num + 3] = sat_model_serial_desc_len;
  1852. num += 4;
  1853. memcpy(rbuf + num, "ATA ", 8);
  1854. num += 8;
  1855. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
  1856. ATA_ID_PROD_LEN);
  1857. num += ATA_ID_PROD_LEN;
  1858. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
  1859. ATA_ID_SERNO_LEN);
  1860. num += ATA_ID_SERNO_LEN;
  1861. if (ata_id_has_wwn(args->id)) {
  1862. /* SAT defined lu world wide name */
  1863. /* piv=0, assoc=lu, code_set=binary, designator=NAA */
  1864. rbuf[num + 0] = 1;
  1865. rbuf[num + 1] = 3;
  1866. rbuf[num + 3] = ATA_ID_WWN_LEN;
  1867. num += 4;
  1868. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1869. ATA_ID_WWN, ATA_ID_WWN_LEN);
  1870. num += ATA_ID_WWN_LEN;
  1871. }
  1872. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  1873. return 0;
  1874. }
  1875. /**
  1876. * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  1877. * @args: device IDENTIFY data / SCSI command of interest.
  1878. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1879. *
  1880. * Yields SAT-specified ATA VPD page.
  1881. *
  1882. * LOCKING:
  1883. * spin_lock_irqsave(host lock)
  1884. */
  1885. static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
  1886. {
  1887. struct ata_taskfile tf;
  1888. memset(&tf, 0, sizeof(tf));
  1889. rbuf[1] = 0x89; /* our page code */
  1890. rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
  1891. rbuf[3] = (0x238 & 0xff);
  1892. memcpy(&rbuf[8], "linux ", 8);
  1893. memcpy(&rbuf[16], "libata ", 16);
  1894. memcpy(&rbuf[32], DRV_VERSION, 4);
  1895. /* we don't store the ATA device signature, so we fake it */
  1896. tf.command = ATA_DRDY; /* really, this is Status reg */
  1897. tf.lbal = 0x1;
  1898. tf.nsect = 0x1;
  1899. ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
  1900. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  1901. rbuf[56] = ATA_CMD_ID_ATA;
  1902. memcpy(&rbuf[60], &args->id[0], 512);
  1903. return 0;
  1904. }
  1905. static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
  1906. {
  1907. u16 min_io_sectors;
  1908. rbuf[1] = 0xb0;
  1909. rbuf[3] = 0x3c; /* required VPD size with unmap support */
  1910. /*
  1911. * Optimal transfer length granularity.
  1912. *
  1913. * This is always one physical block, but for disks with a smaller
  1914. * logical than physical sector size we need to figure out what the
  1915. * latter is.
  1916. */
  1917. min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
  1918. put_unaligned_be16(min_io_sectors, &rbuf[6]);
  1919. /*
  1920. * Optimal unmap granularity.
  1921. *
  1922. * The ATA spec doesn't even know about a granularity or alignment
  1923. * for the TRIM command. We can leave away most of the unmap related
  1924. * VPD page entries, but we have specifify a granularity to signal
  1925. * that we support some form of unmap - in thise case via WRITE SAME
  1926. * with the unmap bit set.
  1927. */
  1928. if (ata_id_has_trim(args->id)) {
  1929. put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
  1930. put_unaligned_be32(1, &rbuf[28]);
  1931. }
  1932. return 0;
  1933. }
  1934. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  1935. {
  1936. int form_factor = ata_id_form_factor(args->id);
  1937. int media_rotation_rate = ata_id_rotation_rate(args->id);
  1938. rbuf[1] = 0xb1;
  1939. rbuf[3] = 0x3c;
  1940. rbuf[4] = media_rotation_rate >> 8;
  1941. rbuf[5] = media_rotation_rate;
  1942. rbuf[7] = form_factor;
  1943. return 0;
  1944. }
  1945. static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
  1946. {
  1947. /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
  1948. rbuf[1] = 0xb2;
  1949. rbuf[3] = 0x4;
  1950. rbuf[5] = 1 << 6; /* TPWS */
  1951. return 0;
  1952. }
  1953. /**
  1954. * ata_scsiop_noop - Command handler that simply returns success.
  1955. * @args: device IDENTIFY data / SCSI command of interest.
  1956. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1957. *
  1958. * No operation. Simply returns success to caller, to indicate
  1959. * that the caller should successfully complete this SCSI command.
  1960. *
  1961. * LOCKING:
  1962. * spin_lock_irqsave(host lock)
  1963. */
  1964. static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
  1965. {
  1966. VPRINTK("ENTER\n");
  1967. return 0;
  1968. }
  1969. /**
  1970. * modecpy - Prepare response for MODE SENSE
  1971. * @dest: output buffer
  1972. * @src: data being copied
  1973. * @n: length of mode page
  1974. * @changeable: whether changeable parameters are requested
  1975. *
  1976. * Generate a generic MODE SENSE page for either current or changeable
  1977. * parameters.
  1978. *
  1979. * LOCKING:
  1980. * None.
  1981. */
  1982. static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
  1983. {
  1984. if (changeable) {
  1985. memcpy(dest, src, 2);
  1986. memset(dest + 2, 0, n - 2);
  1987. } else {
  1988. memcpy(dest, src, n);
  1989. }
  1990. }
  1991. /**
  1992. * ata_msense_caching - Simulate MODE SENSE caching info page
  1993. * @id: device IDENTIFY data
  1994. * @buf: output buffer
  1995. * @changeable: whether changeable parameters are requested
  1996. *
  1997. * Generate a caching info page, which conditionally indicates
  1998. * write caching to the SCSI layer, depending on device
  1999. * capabilities.
  2000. *
  2001. * LOCKING:
  2002. * None.
  2003. */
  2004. static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
  2005. {
  2006. modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
  2007. if (changeable || ata_id_wcache_enabled(id))
  2008. buf[2] |= (1 << 2); /* write cache enable */
  2009. if (!changeable && !ata_id_rahead_enabled(id))
  2010. buf[12] |= (1 << 5); /* disable read ahead */
  2011. return sizeof(def_cache_mpage);
  2012. }
  2013. /**
  2014. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  2015. * @buf: output buffer
  2016. * @changeable: whether changeable parameters are requested
  2017. *
  2018. * Generate a generic MODE SENSE control mode page.
  2019. *
  2020. * LOCKING:
  2021. * None.
  2022. */
  2023. static unsigned int ata_msense_ctl_mode(u8 *buf, bool changeable)
  2024. {
  2025. modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
  2026. return sizeof(def_control_mpage);
  2027. }
  2028. /**
  2029. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  2030. * @buf: output buffer
  2031. * @changeable: whether changeable parameters are requested
  2032. *
  2033. * Generate a generic MODE SENSE r/w error recovery page.
  2034. *
  2035. * LOCKING:
  2036. * None.
  2037. */
  2038. static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
  2039. {
  2040. modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
  2041. changeable);
  2042. return sizeof(def_rw_recovery_mpage);
  2043. }
  2044. /*
  2045. * We can turn this into a real blacklist if it's needed, for now just
  2046. * blacklist any Maxtor BANC1G10 revision firmware
  2047. */
  2048. static int ata_dev_supports_fua(u16 *id)
  2049. {
  2050. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  2051. if (!libata_fua)
  2052. return 0;
  2053. if (!ata_id_has_fua(id))
  2054. return 0;
  2055. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  2056. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  2057. if (strcmp(model, "Maxtor"))
  2058. return 1;
  2059. if (strcmp(fw, "BANC1G10"))
  2060. return 1;
  2061. return 0; /* blacklisted */
  2062. }
  2063. /**
  2064. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  2065. * @args: device IDENTIFY data / SCSI command of interest.
  2066. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2067. *
  2068. * Simulate MODE SENSE commands. Assume this is invoked for direct
  2069. * access devices (e.g. disks) only. There should be no block
  2070. * descriptor for other device types.
  2071. *
  2072. * LOCKING:
  2073. * spin_lock_irqsave(host lock)
  2074. */
  2075. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  2076. {
  2077. struct ata_device *dev = args->dev;
  2078. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  2079. const u8 sat_blk_desc[] = {
  2080. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  2081. 0,
  2082. 0, 0x2, 0x0 /* block length: 512 bytes */
  2083. };
  2084. u8 pg, spg;
  2085. unsigned int ebd, page_control, six_byte;
  2086. u8 dpofua;
  2087. VPRINTK("ENTER\n");
  2088. six_byte = (scsicmd[0] == MODE_SENSE);
  2089. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  2090. /*
  2091. * LLBA bit in msense(10) ignored (compliant)
  2092. */
  2093. page_control = scsicmd[2] >> 6;
  2094. switch (page_control) {
  2095. case 0: /* current */
  2096. case 1: /* changeable */
  2097. case 2: /* defaults */
  2098. break; /* supported */
  2099. case 3: /* saved */
  2100. goto saving_not_supp;
  2101. default:
  2102. goto invalid_fld;
  2103. }
  2104. if (six_byte)
  2105. p += 4 + (ebd ? 8 : 0);
  2106. else
  2107. p += 8 + (ebd ? 8 : 0);
  2108. pg = scsicmd[2] & 0x3f;
  2109. spg = scsicmd[3];
  2110. /*
  2111. * No mode subpages supported (yet) but asking for _all_
  2112. * subpages may be valid
  2113. */
  2114. if (spg && (spg != ALL_SUB_MPAGES))
  2115. goto invalid_fld;
  2116. switch(pg) {
  2117. case RW_RECOVERY_MPAGE:
  2118. p += ata_msense_rw_recovery(p, page_control == 1);
  2119. break;
  2120. case CACHE_MPAGE:
  2121. p += ata_msense_caching(args->id, p, page_control == 1);
  2122. break;
  2123. case CONTROL_MPAGE:
  2124. p += ata_msense_ctl_mode(p, page_control == 1);
  2125. break;
  2126. case ALL_MPAGES:
  2127. p += ata_msense_rw_recovery(p, page_control == 1);
  2128. p += ata_msense_caching(args->id, p, page_control == 1);
  2129. p += ata_msense_ctl_mode(p, page_control == 1);
  2130. break;
  2131. default: /* invalid page code */
  2132. goto invalid_fld;
  2133. }
  2134. dpofua = 0;
  2135. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2136. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2137. dpofua = 1 << 4;
  2138. if (six_byte) {
  2139. rbuf[0] = p - rbuf - 1;
  2140. rbuf[2] |= dpofua;
  2141. if (ebd) {
  2142. rbuf[3] = sizeof(sat_blk_desc);
  2143. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2144. }
  2145. } else {
  2146. unsigned int output_len = p - rbuf - 2;
  2147. rbuf[0] = output_len >> 8;
  2148. rbuf[1] = output_len;
  2149. rbuf[3] |= dpofua;
  2150. if (ebd) {
  2151. rbuf[7] = sizeof(sat_blk_desc);
  2152. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2153. }
  2154. }
  2155. return 0;
  2156. invalid_fld:
  2157. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2158. /* "Invalid field in cbd" */
  2159. return 1;
  2160. saving_not_supp:
  2161. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2162. /* "Saving parameters not supported" */
  2163. return 1;
  2164. }
  2165. /**
  2166. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2167. * @args: device IDENTIFY data / SCSI command of interest.
  2168. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2169. *
  2170. * Simulate READ CAPACITY commands.
  2171. *
  2172. * LOCKING:
  2173. * None.
  2174. */
  2175. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2176. {
  2177. struct ata_device *dev = args->dev;
  2178. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2179. u32 sector_size; /* physical sector size in bytes */
  2180. u8 log2_per_phys;
  2181. u16 lowest_aligned;
  2182. sector_size = ata_id_logical_sector_size(dev->id);
  2183. log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
  2184. lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
  2185. VPRINTK("ENTER\n");
  2186. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2187. if (last_lba >= 0xffffffffULL)
  2188. last_lba = 0xffffffff;
  2189. /* sector count, 32-bit */
  2190. rbuf[0] = last_lba >> (8 * 3);
  2191. rbuf[1] = last_lba >> (8 * 2);
  2192. rbuf[2] = last_lba >> (8 * 1);
  2193. rbuf[3] = last_lba;
  2194. /* sector size */
  2195. rbuf[4] = sector_size >> (8 * 3);
  2196. rbuf[5] = sector_size >> (8 * 2);
  2197. rbuf[6] = sector_size >> (8 * 1);
  2198. rbuf[7] = sector_size;
  2199. } else {
  2200. /* sector count, 64-bit */
  2201. rbuf[0] = last_lba >> (8 * 7);
  2202. rbuf[1] = last_lba >> (8 * 6);
  2203. rbuf[2] = last_lba >> (8 * 5);
  2204. rbuf[3] = last_lba >> (8 * 4);
  2205. rbuf[4] = last_lba >> (8 * 3);
  2206. rbuf[5] = last_lba >> (8 * 2);
  2207. rbuf[6] = last_lba >> (8 * 1);
  2208. rbuf[7] = last_lba;
  2209. /* sector size */
  2210. rbuf[ 8] = sector_size >> (8 * 3);
  2211. rbuf[ 9] = sector_size >> (8 * 2);
  2212. rbuf[10] = sector_size >> (8 * 1);
  2213. rbuf[11] = sector_size;
  2214. rbuf[12] = 0;
  2215. rbuf[13] = log2_per_phys;
  2216. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2217. rbuf[15] = lowest_aligned;
  2218. if (ata_id_has_trim(args->id) &&
  2219. !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
  2220. rbuf[14] |= 0x80; /* LBPME */
  2221. if (ata_id_has_zero_after_trim(args->id) &&
  2222. dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
  2223. ata_dev_info(dev, "Enabling discard_zeroes_data\n");
  2224. rbuf[14] |= 0x40; /* LBPRZ */
  2225. }
  2226. }
  2227. }
  2228. return 0;
  2229. }
  2230. /**
  2231. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2232. * @args: device IDENTIFY data / SCSI command of interest.
  2233. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2234. *
  2235. * Simulate REPORT LUNS command.
  2236. *
  2237. * LOCKING:
  2238. * spin_lock_irqsave(host lock)
  2239. */
  2240. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2241. {
  2242. VPRINTK("ENTER\n");
  2243. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2244. return 0;
  2245. }
  2246. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2247. {
  2248. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2249. /* FIXME: not quite right; we don't want the
  2250. * translation of taskfile registers into
  2251. * a sense descriptors, since that's only
  2252. * correct for ATA, not ATAPI
  2253. */
  2254. ata_gen_passthru_sense(qc);
  2255. }
  2256. ata_qc_done(qc);
  2257. }
  2258. /* is it pointless to prefer PIO for "safety reasons"? */
  2259. static inline int ata_pio_use_silly(struct ata_port *ap)
  2260. {
  2261. return (ap->flags & ATA_FLAG_PIO_DMA);
  2262. }
  2263. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2264. {
  2265. struct ata_port *ap = qc->ap;
  2266. struct scsi_cmnd *cmd = qc->scsicmd;
  2267. DPRINTK("ATAPI request sense\n");
  2268. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2269. #ifdef CONFIG_ATA_SFF
  2270. if (ap->ops->sff_tf_read)
  2271. ap->ops->sff_tf_read(ap, &qc->tf);
  2272. #endif
  2273. /* fill these in, for the case where they are -not- overwritten */
  2274. cmd->sense_buffer[0] = 0x70;
  2275. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2276. ata_qc_reinit(qc);
  2277. /* setup sg table and init transfer direction */
  2278. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2279. ata_sg_init(qc, &qc->sgent, 1);
  2280. qc->dma_dir = DMA_FROM_DEVICE;
  2281. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2282. qc->cdb[0] = REQUEST_SENSE;
  2283. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2284. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2285. qc->tf.command = ATA_CMD_PACKET;
  2286. if (ata_pio_use_silly(ap)) {
  2287. qc->tf.protocol = ATAPI_PROT_DMA;
  2288. qc->tf.feature |= ATAPI_PKT_DMA;
  2289. } else {
  2290. qc->tf.protocol = ATAPI_PROT_PIO;
  2291. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2292. qc->tf.lbah = 0;
  2293. }
  2294. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2295. qc->complete_fn = atapi_sense_complete;
  2296. ata_qc_issue(qc);
  2297. DPRINTK("EXIT\n");
  2298. }
  2299. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2300. {
  2301. struct scsi_cmnd *cmd = qc->scsicmd;
  2302. unsigned int err_mask = qc->err_mask;
  2303. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2304. /* handle completion from new EH */
  2305. if (unlikely(qc->ap->ops->error_handler &&
  2306. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2307. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2308. /* FIXME: not quite right; we don't want the
  2309. * translation of taskfile registers into a
  2310. * sense descriptors, since that's only
  2311. * correct for ATA, not ATAPI
  2312. */
  2313. ata_gen_passthru_sense(qc);
  2314. }
  2315. /* SCSI EH automatically locks door if sdev->locked is
  2316. * set. Sometimes door lock request continues to
  2317. * fail, for example, when no media is present. This
  2318. * creates a loop - SCSI EH issues door lock which
  2319. * fails and gets invoked again to acquire sense data
  2320. * for the failed command.
  2321. *
  2322. * If door lock fails, always clear sdev->locked to
  2323. * avoid this infinite loop.
  2324. *
  2325. * This may happen before SCSI scan is complete. Make
  2326. * sure qc->dev->sdev isn't NULL before dereferencing.
  2327. */
  2328. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
  2329. qc->dev->sdev->locked = 0;
  2330. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2331. ata_qc_done(qc);
  2332. return;
  2333. }
  2334. /* successful completion or old EH failure path */
  2335. if (unlikely(err_mask & AC_ERR_DEV)) {
  2336. cmd->result = SAM_STAT_CHECK_CONDITION;
  2337. atapi_request_sense(qc);
  2338. return;
  2339. } else if (unlikely(err_mask)) {
  2340. /* FIXME: not quite right; we don't want the
  2341. * translation of taskfile registers into
  2342. * a sense descriptors, since that's only
  2343. * correct for ATA, not ATAPI
  2344. */
  2345. ata_gen_passthru_sense(qc);
  2346. } else {
  2347. u8 *scsicmd = cmd->cmnd;
  2348. if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
  2349. unsigned long flags;
  2350. u8 *buf;
  2351. buf = ata_scsi_rbuf_get(cmd, true, &flags);
  2352. /* ATAPI devices typically report zero for their SCSI version,
  2353. * and sometimes deviate from the spec WRT response data
  2354. * format. If SCSI version is reported as zero like normal,
  2355. * then we make the following fixups: 1) Fake MMC-5 version,
  2356. * to indicate to the Linux scsi midlayer this is a modern
  2357. * device. 2) Ensure response data format / ATAPI information
  2358. * are always correct.
  2359. */
  2360. if (buf[2] == 0) {
  2361. buf[2] = 0x5;
  2362. buf[3] = 0x32;
  2363. }
  2364. ata_scsi_rbuf_put(cmd, true, &flags);
  2365. }
  2366. cmd->result = SAM_STAT_GOOD;
  2367. }
  2368. ata_qc_done(qc);
  2369. }
  2370. /**
  2371. * atapi_xlat - Initialize PACKET taskfile
  2372. * @qc: command structure to be initialized
  2373. *
  2374. * LOCKING:
  2375. * spin_lock_irqsave(host lock)
  2376. *
  2377. * RETURNS:
  2378. * Zero on success, non-zero on failure.
  2379. */
  2380. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2381. {
  2382. struct scsi_cmnd *scmd = qc->scsicmd;
  2383. struct ata_device *dev = qc->dev;
  2384. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2385. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2386. unsigned int nbytes;
  2387. memset(qc->cdb, 0, dev->cdb_len);
  2388. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2389. qc->complete_fn = atapi_qc_complete;
  2390. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2391. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2392. qc->tf.flags |= ATA_TFLAG_WRITE;
  2393. DPRINTK("direction: write\n");
  2394. }
  2395. qc->tf.command = ATA_CMD_PACKET;
  2396. ata_qc_set_pc_nbytes(qc);
  2397. /* check whether ATAPI DMA is safe */
  2398. if (!nodata && !using_pio && atapi_check_dma(qc))
  2399. using_pio = 1;
  2400. /* Some controller variants snoop this value for Packet
  2401. * transfers to do state machine and FIFO management. Thus we
  2402. * want to set it properly, and for DMA where it is
  2403. * effectively meaningless.
  2404. */
  2405. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2406. /* Most ATAPI devices which honor transfer chunk size don't
  2407. * behave according to the spec when odd chunk size which
  2408. * matches the transfer length is specified. If the number of
  2409. * bytes to transfer is 2n+1. According to the spec, what
  2410. * should happen is to indicate that 2n+1 is going to be
  2411. * transferred and transfer 2n+2 bytes where the last byte is
  2412. * padding.
  2413. *
  2414. * In practice, this doesn't happen. ATAPI devices first
  2415. * indicate and transfer 2n bytes and then indicate and
  2416. * transfer 2 bytes where the last byte is padding.
  2417. *
  2418. * This inconsistency confuses several controllers which
  2419. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2420. * These controllers use actual number of transferred bytes to
  2421. * update DMA poitner and transfer of 4n+2 bytes make those
  2422. * controller push DMA pointer by 4n+4 bytes because SATA data
  2423. * FISes are aligned to 4 bytes. This causes data corruption
  2424. * and buffer overrun.
  2425. *
  2426. * Always setting nbytes to even number solves this problem
  2427. * because then ATAPI devices don't have to split data at 2n
  2428. * boundaries.
  2429. */
  2430. if (nbytes & 0x1)
  2431. nbytes++;
  2432. qc->tf.lbam = (nbytes & 0xFF);
  2433. qc->tf.lbah = (nbytes >> 8);
  2434. if (nodata)
  2435. qc->tf.protocol = ATAPI_PROT_NODATA;
  2436. else if (using_pio)
  2437. qc->tf.protocol = ATAPI_PROT_PIO;
  2438. else {
  2439. /* DMA data xfer */
  2440. qc->tf.protocol = ATAPI_PROT_DMA;
  2441. qc->tf.feature |= ATAPI_PKT_DMA;
  2442. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2443. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2444. /* some SATA bridges need us to indicate data xfer direction */
  2445. qc->tf.feature |= ATAPI_DMADIR;
  2446. }
  2447. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2448. as ATAPI tape drives don't get this right otherwise */
  2449. return 0;
  2450. }
  2451. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2452. {
  2453. if (!sata_pmp_attached(ap)) {
  2454. if (likely(devno < ata_link_max_devices(&ap->link)))
  2455. return &ap->link.device[devno];
  2456. } else {
  2457. if (likely(devno < ap->nr_pmp_links))
  2458. return &ap->pmp_link[devno].device[0];
  2459. }
  2460. return NULL;
  2461. }
  2462. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2463. const struct scsi_device *scsidev)
  2464. {
  2465. int devno;
  2466. /* skip commands not addressed to targets we simulate */
  2467. if (!sata_pmp_attached(ap)) {
  2468. if (unlikely(scsidev->channel || scsidev->lun))
  2469. return NULL;
  2470. devno = scsidev->id;
  2471. } else {
  2472. if (unlikely(scsidev->id || scsidev->lun))
  2473. return NULL;
  2474. devno = scsidev->channel;
  2475. }
  2476. return ata_find_dev(ap, devno);
  2477. }
  2478. /**
  2479. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2480. * @ap: ATA port to which the device is attached
  2481. * @scsidev: SCSI device from which we derive the ATA device
  2482. *
  2483. * Given various information provided in struct scsi_cmnd,
  2484. * map that onto an ATA bus, and using that mapping
  2485. * determine which ata_device is associated with the
  2486. * SCSI command to be sent.
  2487. *
  2488. * LOCKING:
  2489. * spin_lock_irqsave(host lock)
  2490. *
  2491. * RETURNS:
  2492. * Associated ATA device, or %NULL if not found.
  2493. */
  2494. static struct ata_device *
  2495. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2496. {
  2497. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2498. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2499. return NULL;
  2500. return dev;
  2501. }
  2502. /*
  2503. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2504. * @byte1: Byte 1 from pass-thru CDB.
  2505. *
  2506. * RETURNS:
  2507. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2508. */
  2509. static u8
  2510. ata_scsi_map_proto(u8 byte1)
  2511. {
  2512. switch((byte1 & 0x1e) >> 1) {
  2513. case 3: /* Non-data */
  2514. return ATA_PROT_NODATA;
  2515. case 6: /* DMA */
  2516. case 10: /* UDMA Data-in */
  2517. case 11: /* UDMA Data-Out */
  2518. return ATA_PROT_DMA;
  2519. case 4: /* PIO Data-in */
  2520. case 5: /* PIO Data-out */
  2521. return ATA_PROT_PIO;
  2522. case 12: /* FPDMA */
  2523. return ATA_PROT_NCQ;
  2524. case 0: /* Hard Reset */
  2525. case 1: /* SRST */
  2526. case 8: /* Device Diagnostic */
  2527. case 9: /* Device Reset */
  2528. case 7: /* DMA Queued */
  2529. case 15: /* Return Response Info */
  2530. default: /* Reserved */
  2531. break;
  2532. }
  2533. return ATA_PROT_UNKNOWN;
  2534. }
  2535. /**
  2536. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2537. * @qc: command structure to be initialized
  2538. *
  2539. * Handles either 12 or 16-byte versions of the CDB.
  2540. *
  2541. * RETURNS:
  2542. * Zero on success, non-zero on failure.
  2543. */
  2544. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2545. {
  2546. struct ata_taskfile *tf = &(qc->tf);
  2547. struct scsi_cmnd *scmd = qc->scsicmd;
  2548. struct ata_device *dev = qc->dev;
  2549. const u8 *cdb = scmd->cmnd;
  2550. if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
  2551. goto invalid_fld;
  2552. /* enable LBA */
  2553. tf->flags |= ATA_TFLAG_LBA;
  2554. /*
  2555. * 12 and 16 byte CDBs use different offsets to
  2556. * provide the various register values.
  2557. */
  2558. if (cdb[0] == ATA_16) {
  2559. /*
  2560. * 16-byte CDB - may contain extended commands.
  2561. *
  2562. * If that is the case, copy the upper byte register values.
  2563. */
  2564. if (cdb[1] & 0x01) {
  2565. tf->hob_feature = cdb[3];
  2566. tf->hob_nsect = cdb[5];
  2567. tf->hob_lbal = cdb[7];
  2568. tf->hob_lbam = cdb[9];
  2569. tf->hob_lbah = cdb[11];
  2570. tf->flags |= ATA_TFLAG_LBA48;
  2571. } else
  2572. tf->flags &= ~ATA_TFLAG_LBA48;
  2573. /*
  2574. * Always copy low byte, device and command registers.
  2575. */
  2576. tf->feature = cdb[4];
  2577. tf->nsect = cdb[6];
  2578. tf->lbal = cdb[8];
  2579. tf->lbam = cdb[10];
  2580. tf->lbah = cdb[12];
  2581. tf->device = cdb[13];
  2582. tf->command = cdb[14];
  2583. } else {
  2584. /*
  2585. * 12-byte CDB - incapable of extended commands.
  2586. */
  2587. tf->flags &= ~ATA_TFLAG_LBA48;
  2588. tf->feature = cdb[3];
  2589. tf->nsect = cdb[4];
  2590. tf->lbal = cdb[5];
  2591. tf->lbam = cdb[6];
  2592. tf->lbah = cdb[7];
  2593. tf->device = cdb[8];
  2594. tf->command = cdb[9];
  2595. }
  2596. /* For NCQ commands with FPDMA protocol, copy the tag value */
  2597. if (tf->protocol == ATA_PROT_NCQ)
  2598. tf->nsect = qc->tag << 3;
  2599. /* enforce correct master/slave bit */
  2600. tf->device = dev->devno ?
  2601. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2602. switch (tf->command) {
  2603. /* READ/WRITE LONG use a non-standard sect_size */
  2604. case ATA_CMD_READ_LONG:
  2605. case ATA_CMD_READ_LONG_ONCE:
  2606. case ATA_CMD_WRITE_LONG:
  2607. case ATA_CMD_WRITE_LONG_ONCE:
  2608. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
  2609. goto invalid_fld;
  2610. qc->sect_size = scsi_bufflen(scmd);
  2611. break;
  2612. /* commands using reported Logical Block size (e.g. 512 or 4K) */
  2613. case ATA_CMD_CFA_WRITE_NE:
  2614. case ATA_CMD_CFA_TRANS_SECT:
  2615. case ATA_CMD_CFA_WRITE_MULT_NE:
  2616. /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
  2617. case ATA_CMD_READ:
  2618. case ATA_CMD_READ_EXT:
  2619. case ATA_CMD_READ_QUEUED:
  2620. /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
  2621. case ATA_CMD_FPDMA_READ:
  2622. case ATA_CMD_READ_MULTI:
  2623. case ATA_CMD_READ_MULTI_EXT:
  2624. case ATA_CMD_PIO_READ:
  2625. case ATA_CMD_PIO_READ_EXT:
  2626. case ATA_CMD_READ_STREAM_DMA_EXT:
  2627. case ATA_CMD_READ_STREAM_EXT:
  2628. case ATA_CMD_VERIFY:
  2629. case ATA_CMD_VERIFY_EXT:
  2630. case ATA_CMD_WRITE:
  2631. case ATA_CMD_WRITE_EXT:
  2632. case ATA_CMD_WRITE_FUA_EXT:
  2633. case ATA_CMD_WRITE_QUEUED:
  2634. case ATA_CMD_WRITE_QUEUED_FUA_EXT:
  2635. case ATA_CMD_FPDMA_WRITE:
  2636. case ATA_CMD_WRITE_MULTI:
  2637. case ATA_CMD_WRITE_MULTI_EXT:
  2638. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  2639. case ATA_CMD_PIO_WRITE:
  2640. case ATA_CMD_PIO_WRITE_EXT:
  2641. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  2642. case ATA_CMD_WRITE_STREAM_EXT:
  2643. qc->sect_size = scmd->device->sector_size;
  2644. break;
  2645. /* Everything else uses 512 byte "sectors" */
  2646. default:
  2647. qc->sect_size = ATA_SECT_SIZE;
  2648. }
  2649. /*
  2650. * Set flags so that all registers will be written, pass on
  2651. * write indication (used for PIO/DMA setup), result TF is
  2652. * copied back and we don't whine too much about its failure.
  2653. */
  2654. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2655. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2656. tf->flags |= ATA_TFLAG_WRITE;
  2657. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2658. /*
  2659. * Set transfer length.
  2660. *
  2661. * TODO: find out if we need to do more here to
  2662. * cover scatter/gather case.
  2663. */
  2664. ata_qc_set_pc_nbytes(qc);
  2665. /* We may not issue DMA commands if no DMA mode is set */
  2666. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
  2667. goto invalid_fld;
  2668. /* sanity check for pio multi commands */
  2669. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
  2670. goto invalid_fld;
  2671. if (is_multi_taskfile(tf)) {
  2672. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2673. /* compare the passed through multi_count
  2674. * with the cached multi_count of libata
  2675. */
  2676. if (multi_count != dev->multi_count)
  2677. ata_dev_warn(dev, "invalid multi_count %u ignored\n",
  2678. multi_count);
  2679. }
  2680. /*
  2681. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2682. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2683. * by an update to hardware-specific registers for each
  2684. * controller (i.e. the reason for ->set_piomode(),
  2685. * ->set_dmamode(), and ->post_set_mode() hooks).
  2686. */
  2687. if (tf->command == ATA_CMD_SET_FEATURES &&
  2688. tf->feature == SETFEATURES_XFER)
  2689. goto invalid_fld;
  2690. /*
  2691. * Filter TPM commands by default. These provide an
  2692. * essentially uncontrolled encrypted "back door" between
  2693. * applications and the disk. Set libata.allow_tpm=1 if you
  2694. * have a real reason for wanting to use them. This ensures
  2695. * that installed software cannot easily mess stuff up without
  2696. * user intent. DVR type users will probably ship with this enabled
  2697. * for movie content management.
  2698. *
  2699. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2700. * for this and should do in future but that it is not sufficient as
  2701. * DCS is an optional feature set. Thus we also do the software filter
  2702. * so that we comply with the TC consortium stated goal that the user
  2703. * can turn off TC features of their system.
  2704. */
  2705. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
  2706. goto invalid_fld;
  2707. return 0;
  2708. invalid_fld:
  2709. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2710. /* "Invalid field in cdb" */
  2711. return 1;
  2712. }
  2713. static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
  2714. {
  2715. struct ata_taskfile *tf = &qc->tf;
  2716. struct scsi_cmnd *scmd = qc->scsicmd;
  2717. struct ata_device *dev = qc->dev;
  2718. const u8 *cdb = scmd->cmnd;
  2719. u64 block;
  2720. u32 n_block;
  2721. u32 size;
  2722. void *buf;
  2723. /* we may not issue DMA commands if no DMA mode is set */
  2724. if (unlikely(!dev->dma_mode))
  2725. goto invalid_fld;
  2726. if (unlikely(scmd->cmd_len < 16))
  2727. goto invalid_fld;
  2728. scsi_16_lba_len(cdb, &block, &n_block);
  2729. /* for now we only support WRITE SAME with the unmap bit set */
  2730. if (unlikely(!(cdb[1] & 0x8)))
  2731. goto invalid_fld;
  2732. /*
  2733. * WRITE SAME always has a sector sized buffer as payload, this
  2734. * should never be a multiple entry S/G list.
  2735. */
  2736. if (!scsi_sg_count(scmd))
  2737. goto invalid_fld;
  2738. buf = page_address(sg_page(scsi_sglist(scmd)));
  2739. size = ata_set_lba_range_entries(buf, 512, block, n_block);
  2740. if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
  2741. /* Newer devices support queued TRIM commands */
  2742. tf->protocol = ATA_PROT_NCQ;
  2743. tf->command = ATA_CMD_FPDMA_SEND;
  2744. tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
  2745. tf->nsect = qc->tag << 3;
  2746. tf->hob_feature = (size / 512) >> 8;
  2747. tf->feature = size / 512;
  2748. tf->auxiliary = 1;
  2749. } else {
  2750. tf->protocol = ATA_PROT_DMA;
  2751. tf->hob_feature = 0;
  2752. tf->feature = ATA_DSM_TRIM;
  2753. tf->hob_nsect = (size / 512) >> 8;
  2754. tf->nsect = size / 512;
  2755. tf->command = ATA_CMD_DSM;
  2756. }
  2757. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
  2758. ATA_TFLAG_WRITE;
  2759. ata_qc_set_pc_nbytes(qc);
  2760. return 0;
  2761. invalid_fld:
  2762. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2763. /* "Invalid field in cdb" */
  2764. return 1;
  2765. }
  2766. /**
  2767. * ata_mselect_caching - Simulate MODE SELECT for caching info page
  2768. * @qc: Storage for translated ATA taskfile
  2769. * @buf: input buffer
  2770. * @len: number of valid bytes in the input buffer
  2771. *
  2772. * Prepare a taskfile to modify caching information for the device.
  2773. *
  2774. * LOCKING:
  2775. * None.
  2776. */
  2777. static int ata_mselect_caching(struct ata_queued_cmd *qc,
  2778. const u8 *buf, int len)
  2779. {
  2780. struct ata_taskfile *tf = &qc->tf;
  2781. struct ata_device *dev = qc->dev;
  2782. char mpage[CACHE_MPAGE_LEN];
  2783. u8 wce;
  2784. /*
  2785. * The first two bytes of def_cache_mpage are a header, so offsets
  2786. * in mpage are off by 2 compared to buf. Same for len.
  2787. */
  2788. if (len != CACHE_MPAGE_LEN - 2)
  2789. return -EINVAL;
  2790. wce = buf[0] & (1 << 2);
  2791. /*
  2792. * Check that read-only bits are not modified.
  2793. */
  2794. ata_msense_caching(dev->id, mpage, false);
  2795. mpage[2] &= ~(1 << 2);
  2796. mpage[2] |= wce;
  2797. if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0)
  2798. return -EINVAL;
  2799. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  2800. tf->protocol = ATA_PROT_NODATA;
  2801. tf->nsect = 0;
  2802. tf->command = ATA_CMD_SET_FEATURES;
  2803. tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
  2804. return 0;
  2805. }
  2806. /**
  2807. * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
  2808. * @qc: Storage for translated ATA taskfile
  2809. *
  2810. * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
  2811. * Assume this is invoked for direct access devices (e.g. disks) only.
  2812. * There should be no block descriptor for other device types.
  2813. *
  2814. * LOCKING:
  2815. * spin_lock_irqsave(host lock)
  2816. */
  2817. static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
  2818. {
  2819. struct scsi_cmnd *scmd = qc->scsicmd;
  2820. const u8 *cdb = scmd->cmnd;
  2821. const u8 *p;
  2822. u8 pg, spg;
  2823. unsigned six_byte, pg_len, hdr_len, bd_len;
  2824. int len;
  2825. VPRINTK("ENTER\n");
  2826. six_byte = (cdb[0] == MODE_SELECT);
  2827. if (six_byte) {
  2828. if (scmd->cmd_len < 5)
  2829. goto invalid_fld;
  2830. len = cdb[4];
  2831. hdr_len = 4;
  2832. } else {
  2833. if (scmd->cmd_len < 9)
  2834. goto invalid_fld;
  2835. len = (cdb[7] << 8) + cdb[8];
  2836. hdr_len = 8;
  2837. }
  2838. /* We only support PF=1, SP=0. */
  2839. if ((cdb[1] & 0x11) != 0x10)
  2840. goto invalid_fld;
  2841. /* Test early for possible overrun. */
  2842. if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
  2843. goto invalid_param_len;
  2844. p = page_address(sg_page(scsi_sglist(scmd)));
  2845. /* Move past header and block descriptors. */
  2846. if (len < hdr_len)
  2847. goto invalid_param_len;
  2848. if (six_byte)
  2849. bd_len = p[3];
  2850. else
  2851. bd_len = (p[6] << 8) + p[7];
  2852. len -= hdr_len;
  2853. p += hdr_len;
  2854. if (len < bd_len)
  2855. goto invalid_param_len;
  2856. if (bd_len != 0 && bd_len != 8)
  2857. goto invalid_param;
  2858. len -= bd_len;
  2859. p += bd_len;
  2860. if (len == 0)
  2861. goto skip;
  2862. /* Parse both possible formats for the mode page headers. */
  2863. pg = p[0] & 0x3f;
  2864. if (p[0] & 0x40) {
  2865. if (len < 4)
  2866. goto invalid_param_len;
  2867. spg = p[1];
  2868. pg_len = (p[2] << 8) | p[3];
  2869. p += 4;
  2870. len -= 4;
  2871. } else {
  2872. if (len < 2)
  2873. goto invalid_param_len;
  2874. spg = 0;
  2875. pg_len = p[1];
  2876. p += 2;
  2877. len -= 2;
  2878. }
  2879. /*
  2880. * No mode subpages supported (yet) but asking for _all_
  2881. * subpages may be valid
  2882. */
  2883. if (spg && (spg != ALL_SUB_MPAGES))
  2884. goto invalid_param;
  2885. if (pg_len > len)
  2886. goto invalid_param_len;
  2887. switch (pg) {
  2888. case CACHE_MPAGE:
  2889. if (ata_mselect_caching(qc, p, pg_len) < 0)
  2890. goto invalid_param;
  2891. break;
  2892. default: /* invalid page code */
  2893. goto invalid_param;
  2894. }
  2895. /*
  2896. * Only one page has changeable data, so we only support setting one
  2897. * page at a time.
  2898. */
  2899. if (len > pg_len)
  2900. goto invalid_param;
  2901. return 0;
  2902. invalid_fld:
  2903. /* "Invalid field in CDB" */
  2904. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2905. return 1;
  2906. invalid_param:
  2907. /* "Invalid field in parameter list" */
  2908. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x26, 0x0);
  2909. return 1;
  2910. invalid_param_len:
  2911. /* "Parameter list length error" */
  2912. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  2913. return 1;
  2914. skip:
  2915. scmd->result = SAM_STAT_GOOD;
  2916. return 1;
  2917. }
  2918. /**
  2919. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  2920. * @dev: ATA device
  2921. * @cmd: SCSI command opcode to consider
  2922. *
  2923. * Look up the SCSI command given, and determine whether the
  2924. * SCSI command is to be translated or simulated.
  2925. *
  2926. * RETURNS:
  2927. * Pointer to translation function if possible, %NULL if not.
  2928. */
  2929. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2930. {
  2931. switch (cmd) {
  2932. case READ_6:
  2933. case READ_10:
  2934. case READ_16:
  2935. case WRITE_6:
  2936. case WRITE_10:
  2937. case WRITE_16:
  2938. return ata_scsi_rw_xlat;
  2939. case WRITE_SAME_16:
  2940. return ata_scsi_write_same_xlat;
  2941. case SYNCHRONIZE_CACHE:
  2942. if (ata_try_flush_cache(dev))
  2943. return ata_scsi_flush_xlat;
  2944. break;
  2945. case VERIFY:
  2946. case VERIFY_16:
  2947. return ata_scsi_verify_xlat;
  2948. case ATA_12:
  2949. case ATA_16:
  2950. return ata_scsi_pass_thru;
  2951. case MODE_SELECT:
  2952. case MODE_SELECT_10:
  2953. return ata_scsi_mode_select_xlat;
  2954. break;
  2955. case START_STOP:
  2956. return ata_scsi_start_stop_xlat;
  2957. }
  2958. return NULL;
  2959. }
  2960. /**
  2961. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2962. * @ap: ATA port to which the command was being sent
  2963. * @cmd: SCSI command to dump
  2964. *
  2965. * Prints the contents of a SCSI command via printk().
  2966. */
  2967. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2968. struct scsi_cmnd *cmd)
  2969. {
  2970. #ifdef ATA_DEBUG
  2971. struct scsi_device *scsidev = cmd->device;
  2972. u8 *scsicmd = cmd->cmnd;
  2973. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2974. ap->print_id,
  2975. scsidev->channel, scsidev->id, scsidev->lun,
  2976. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2977. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2978. scsicmd[8]);
  2979. #endif
  2980. }
  2981. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
  2982. struct ata_device *dev)
  2983. {
  2984. u8 scsi_op = scmd->cmnd[0];
  2985. ata_xlat_func_t xlat_func;
  2986. int rc = 0;
  2987. if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
  2988. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  2989. goto bad_cdb_len;
  2990. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2991. } else {
  2992. if (unlikely(!scmd->cmd_len))
  2993. goto bad_cdb_len;
  2994. xlat_func = NULL;
  2995. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  2996. /* relay SCSI command to ATAPI device */
  2997. int len = COMMAND_SIZE(scsi_op);
  2998. if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
  2999. goto bad_cdb_len;
  3000. xlat_func = atapi_xlat;
  3001. } else {
  3002. /* ATA_16 passthru, treat as an ATA command */
  3003. if (unlikely(scmd->cmd_len > 16))
  3004. goto bad_cdb_len;
  3005. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3006. }
  3007. }
  3008. if (xlat_func)
  3009. rc = ata_scsi_translate(dev, scmd, xlat_func);
  3010. else
  3011. ata_scsi_simulate(dev, scmd);
  3012. return rc;
  3013. bad_cdb_len:
  3014. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  3015. scmd->cmd_len, scsi_op, dev->cdb_len);
  3016. scmd->result = DID_ERROR << 16;
  3017. scmd->scsi_done(scmd);
  3018. return 0;
  3019. }
  3020. /**
  3021. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  3022. * @shost: SCSI host of command to be sent
  3023. * @cmd: SCSI command to be sent
  3024. *
  3025. * In some cases, this function translates SCSI commands into
  3026. * ATA taskfiles, and queues the taskfiles to be sent to
  3027. * hardware. In other cases, this function simulates a
  3028. * SCSI device by evaluating and responding to certain
  3029. * SCSI commands. This creates the overall effect of
  3030. * ATA and ATAPI devices appearing as SCSI devices.
  3031. *
  3032. * LOCKING:
  3033. * ATA host lock
  3034. *
  3035. * RETURNS:
  3036. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3037. * 0 otherwise.
  3038. */
  3039. int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  3040. {
  3041. struct ata_port *ap;
  3042. struct ata_device *dev;
  3043. struct scsi_device *scsidev = cmd->device;
  3044. int rc = 0;
  3045. unsigned long irq_flags;
  3046. ap = ata_shost_to_port(shost);
  3047. spin_lock_irqsave(ap->lock, irq_flags);
  3048. ata_scsi_dump_cdb(ap, cmd);
  3049. dev = ata_scsi_find_dev(ap, scsidev);
  3050. if (likely(dev))
  3051. rc = __ata_scsi_queuecmd(cmd, dev);
  3052. else {
  3053. cmd->result = (DID_BAD_TARGET << 16);
  3054. cmd->scsi_done(cmd);
  3055. }
  3056. spin_unlock_irqrestore(ap->lock, irq_flags);
  3057. return rc;
  3058. }
  3059. /**
  3060. * ata_scsi_simulate - simulate SCSI command on ATA device
  3061. * @dev: the target device
  3062. * @cmd: SCSI command being sent to device.
  3063. *
  3064. * Interprets and directly executes a select list of SCSI commands
  3065. * that can be handled internally.
  3066. *
  3067. * LOCKING:
  3068. * spin_lock_irqsave(host lock)
  3069. */
  3070. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
  3071. {
  3072. struct ata_scsi_args args;
  3073. const u8 *scsicmd = cmd->cmnd;
  3074. u8 tmp8;
  3075. args.dev = dev;
  3076. args.id = dev->id;
  3077. args.cmd = cmd;
  3078. args.done = cmd->scsi_done;
  3079. switch(scsicmd[0]) {
  3080. /* TODO: worth improving? */
  3081. case FORMAT_UNIT:
  3082. ata_scsi_invalid_field(cmd);
  3083. break;
  3084. case INQUIRY:
  3085. if (scsicmd[1] & 2) /* is CmdDt set? */
  3086. ata_scsi_invalid_field(cmd);
  3087. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  3088. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  3089. else switch (scsicmd[2]) {
  3090. case 0x00:
  3091. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  3092. break;
  3093. case 0x80:
  3094. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  3095. break;
  3096. case 0x83:
  3097. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  3098. break;
  3099. case 0x89:
  3100. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  3101. break;
  3102. case 0xb0:
  3103. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
  3104. break;
  3105. case 0xb1:
  3106. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  3107. break;
  3108. case 0xb2:
  3109. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
  3110. break;
  3111. default:
  3112. ata_scsi_invalid_field(cmd);
  3113. break;
  3114. }
  3115. break;
  3116. case MODE_SENSE:
  3117. case MODE_SENSE_10:
  3118. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  3119. break;
  3120. case READ_CAPACITY:
  3121. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3122. break;
  3123. case SERVICE_ACTION_IN_16:
  3124. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  3125. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3126. else
  3127. ata_scsi_invalid_field(cmd);
  3128. break;
  3129. case REPORT_LUNS:
  3130. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  3131. break;
  3132. case REQUEST_SENSE:
  3133. ata_scsi_set_sense(cmd, 0, 0, 0);
  3134. cmd->result = (DRIVER_SENSE << 24);
  3135. cmd->scsi_done(cmd);
  3136. break;
  3137. /* if we reach this, then writeback caching is disabled,
  3138. * turning this into a no-op.
  3139. */
  3140. case SYNCHRONIZE_CACHE:
  3141. /* fall through */
  3142. /* no-op's, complete with success */
  3143. case REZERO_UNIT:
  3144. case SEEK_6:
  3145. case SEEK_10:
  3146. case TEST_UNIT_READY:
  3147. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  3148. break;
  3149. case SEND_DIAGNOSTIC:
  3150. tmp8 = scsicmd[1] & ~(1 << 3);
  3151. if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
  3152. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  3153. else
  3154. ata_scsi_invalid_field(cmd);
  3155. break;
  3156. /* all other commands */
  3157. default:
  3158. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  3159. /* "Invalid command operation code" */
  3160. cmd->scsi_done(cmd);
  3161. break;
  3162. }
  3163. }
  3164. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  3165. {
  3166. int i, rc;
  3167. for (i = 0; i < host->n_ports; i++) {
  3168. struct ata_port *ap = host->ports[i];
  3169. struct Scsi_Host *shost;
  3170. rc = -ENOMEM;
  3171. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  3172. if (!shost)
  3173. goto err_alloc;
  3174. shost->eh_noresume = 1;
  3175. *(struct ata_port **)&shost->hostdata[0] = ap;
  3176. ap->scsi_host = shost;
  3177. shost->transportt = ata_scsi_transport_template;
  3178. shost->unique_id = ap->print_id;
  3179. shost->max_id = 16;
  3180. shost->max_lun = 1;
  3181. shost->max_channel = 1;
  3182. shost->max_cmd_len = 16;
  3183. shost->no_write_same = 1;
  3184. /* Schedule policy is determined by ->qc_defer()
  3185. * callback and it needs to see every deferred qc.
  3186. * Set host_blocked to 1 to prevent SCSI midlayer from
  3187. * automatically deferring requests.
  3188. */
  3189. shost->max_host_blocked = 1;
  3190. rc = scsi_add_host_with_dma(ap->scsi_host,
  3191. &ap->tdev, ap->host->dev);
  3192. if (rc)
  3193. goto err_add;
  3194. }
  3195. return 0;
  3196. err_add:
  3197. scsi_host_put(host->ports[i]->scsi_host);
  3198. err_alloc:
  3199. while (--i >= 0) {
  3200. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  3201. scsi_remove_host(shost);
  3202. scsi_host_put(shost);
  3203. }
  3204. return rc;
  3205. }
  3206. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  3207. {
  3208. int tries = 5;
  3209. struct ata_device *last_failed_dev = NULL;
  3210. struct ata_link *link;
  3211. struct ata_device *dev;
  3212. repeat:
  3213. ata_for_each_link(link, ap, EDGE) {
  3214. ata_for_each_dev(dev, link, ENABLED) {
  3215. struct scsi_device *sdev;
  3216. int channel = 0, id = 0;
  3217. if (dev->sdev)
  3218. continue;
  3219. if (ata_is_host_link(link))
  3220. id = dev->devno;
  3221. else
  3222. channel = link->pmp;
  3223. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  3224. NULL);
  3225. if (!IS_ERR(sdev)) {
  3226. dev->sdev = sdev;
  3227. scsi_device_put(sdev);
  3228. } else {
  3229. dev->sdev = NULL;
  3230. }
  3231. }
  3232. }
  3233. /* If we scanned while EH was in progress or allocation
  3234. * failure occurred, scan would have failed silently. Check
  3235. * whether all devices are attached.
  3236. */
  3237. ata_for_each_link(link, ap, EDGE) {
  3238. ata_for_each_dev(dev, link, ENABLED) {
  3239. if (!dev->sdev)
  3240. goto exit_loop;
  3241. }
  3242. }
  3243. exit_loop:
  3244. if (!link)
  3245. return;
  3246. /* we're missing some SCSI devices */
  3247. if (sync) {
  3248. /* If caller requested synchrnous scan && we've made
  3249. * any progress, sleep briefly and repeat.
  3250. */
  3251. if (dev != last_failed_dev) {
  3252. msleep(100);
  3253. last_failed_dev = dev;
  3254. goto repeat;
  3255. }
  3256. /* We might be failing to detect boot device, give it
  3257. * a few more chances.
  3258. */
  3259. if (--tries) {
  3260. msleep(100);
  3261. goto repeat;
  3262. }
  3263. ata_port_err(ap,
  3264. "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
  3265. }
  3266. queue_delayed_work(system_long_wq, &ap->hotplug_task,
  3267. round_jiffies_relative(HZ));
  3268. }
  3269. /**
  3270. * ata_scsi_offline_dev - offline attached SCSI device
  3271. * @dev: ATA device to offline attached SCSI device for
  3272. *
  3273. * This function is called from ata_eh_hotplug() and responsible
  3274. * for taking the SCSI device attached to @dev offline. This
  3275. * function is called with host lock which protects dev->sdev
  3276. * against clearing.
  3277. *
  3278. * LOCKING:
  3279. * spin_lock_irqsave(host lock)
  3280. *
  3281. * RETURNS:
  3282. * 1 if attached SCSI device exists, 0 otherwise.
  3283. */
  3284. int ata_scsi_offline_dev(struct ata_device *dev)
  3285. {
  3286. if (dev->sdev) {
  3287. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  3288. return 1;
  3289. }
  3290. return 0;
  3291. }
  3292. /**
  3293. * ata_scsi_remove_dev - remove attached SCSI device
  3294. * @dev: ATA device to remove attached SCSI device for
  3295. *
  3296. * This function is called from ata_eh_scsi_hotplug() and
  3297. * responsible for removing the SCSI device attached to @dev.
  3298. *
  3299. * LOCKING:
  3300. * Kernel thread context (may sleep).
  3301. */
  3302. static void ata_scsi_remove_dev(struct ata_device *dev)
  3303. {
  3304. struct ata_port *ap = dev->link->ap;
  3305. struct scsi_device *sdev;
  3306. unsigned long flags;
  3307. /* Alas, we need to grab scan_mutex to ensure SCSI device
  3308. * state doesn't change underneath us and thus
  3309. * scsi_device_get() always succeeds. The mutex locking can
  3310. * be removed if there is __scsi_device_get() interface which
  3311. * increments reference counts regardless of device state.
  3312. */
  3313. mutex_lock(&ap->scsi_host->scan_mutex);
  3314. spin_lock_irqsave(ap->lock, flags);
  3315. /* clearing dev->sdev is protected by host lock */
  3316. sdev = dev->sdev;
  3317. dev->sdev = NULL;
  3318. if (sdev) {
  3319. /* If user initiated unplug races with us, sdev can go
  3320. * away underneath us after the host lock and
  3321. * scan_mutex are released. Hold onto it.
  3322. */
  3323. if (scsi_device_get(sdev) == 0) {
  3324. /* The following ensures the attached sdev is
  3325. * offline on return from ata_scsi_offline_dev()
  3326. * regardless it wins or loses the race
  3327. * against this function.
  3328. */
  3329. scsi_device_set_state(sdev, SDEV_OFFLINE);
  3330. } else {
  3331. WARN_ON(1);
  3332. sdev = NULL;
  3333. }
  3334. }
  3335. spin_unlock_irqrestore(ap->lock, flags);
  3336. mutex_unlock(&ap->scsi_host->scan_mutex);
  3337. if (sdev) {
  3338. ata_dev_info(dev, "detaching (SCSI %s)\n",
  3339. dev_name(&sdev->sdev_gendev));
  3340. scsi_remove_device(sdev);
  3341. scsi_device_put(sdev);
  3342. }
  3343. }
  3344. static void ata_scsi_handle_link_detach(struct ata_link *link)
  3345. {
  3346. struct ata_port *ap = link->ap;
  3347. struct ata_device *dev;
  3348. ata_for_each_dev(dev, link, ALL) {
  3349. unsigned long flags;
  3350. if (!(dev->flags & ATA_DFLAG_DETACHED))
  3351. continue;
  3352. spin_lock_irqsave(ap->lock, flags);
  3353. dev->flags &= ~ATA_DFLAG_DETACHED;
  3354. spin_unlock_irqrestore(ap->lock, flags);
  3355. if (zpodd_dev_enabled(dev))
  3356. zpodd_exit(dev);
  3357. ata_scsi_remove_dev(dev);
  3358. }
  3359. }
  3360. /**
  3361. * ata_scsi_media_change_notify - send media change event
  3362. * @dev: Pointer to the disk device with media change event
  3363. *
  3364. * Tell the block layer to send a media change notification
  3365. * event.
  3366. *
  3367. * LOCKING:
  3368. * spin_lock_irqsave(host lock)
  3369. */
  3370. void ata_scsi_media_change_notify(struct ata_device *dev)
  3371. {
  3372. if (dev->sdev)
  3373. sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
  3374. GFP_ATOMIC);
  3375. }
  3376. /**
  3377. * ata_scsi_hotplug - SCSI part of hotplug
  3378. * @work: Pointer to ATA port to perform SCSI hotplug on
  3379. *
  3380. * Perform SCSI part of hotplug. It's executed from a separate
  3381. * workqueue after EH completes. This is necessary because SCSI
  3382. * hot plugging requires working EH and hot unplugging is
  3383. * synchronized with hot plugging with a mutex.
  3384. *
  3385. * LOCKING:
  3386. * Kernel thread context (may sleep).
  3387. */
  3388. void ata_scsi_hotplug(struct work_struct *work)
  3389. {
  3390. struct ata_port *ap =
  3391. container_of(work, struct ata_port, hotplug_task.work);
  3392. int i;
  3393. if (ap->pflags & ATA_PFLAG_UNLOADING) {
  3394. DPRINTK("ENTER/EXIT - unloading\n");
  3395. return;
  3396. }
  3397. /*
  3398. * XXX - UGLY HACK
  3399. *
  3400. * The block layer suspend/resume path is fundamentally broken due
  3401. * to freezable kthreads and workqueue and may deadlock if a block
  3402. * device gets removed while resume is in progress. I don't know
  3403. * what the solution is short of removing freezable kthreads and
  3404. * workqueues altogether.
  3405. *
  3406. * The following is an ugly hack to avoid kicking off device
  3407. * removal while freezer is active. This is a joke but does avoid
  3408. * this particular deadlock scenario.
  3409. *
  3410. * https://bugzilla.kernel.org/show_bug.cgi?id=62801
  3411. * http://marc.info/?l=linux-kernel&m=138695698516487
  3412. */
  3413. #ifdef CONFIG_FREEZER
  3414. while (pm_freezing)
  3415. msleep(10);
  3416. #endif
  3417. DPRINTK("ENTER\n");
  3418. mutex_lock(&ap->scsi_scan_mutex);
  3419. /* Unplug detached devices. We cannot use link iterator here
  3420. * because PMP links have to be scanned even if PMP is
  3421. * currently not attached. Iterate manually.
  3422. */
  3423. ata_scsi_handle_link_detach(&ap->link);
  3424. if (ap->pmp_link)
  3425. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  3426. ata_scsi_handle_link_detach(&ap->pmp_link[i]);
  3427. /* scan for new ones */
  3428. ata_scsi_scan_host(ap, 0);
  3429. mutex_unlock(&ap->scsi_scan_mutex);
  3430. DPRINTK("EXIT\n");
  3431. }
  3432. /**
  3433. * ata_scsi_user_scan - indication for user-initiated bus scan
  3434. * @shost: SCSI host to scan
  3435. * @channel: Channel to scan
  3436. * @id: ID to scan
  3437. * @lun: LUN to scan
  3438. *
  3439. * This function is called when user explicitly requests bus
  3440. * scan. Set probe pending flag and invoke EH.
  3441. *
  3442. * LOCKING:
  3443. * SCSI layer (we don't care)
  3444. *
  3445. * RETURNS:
  3446. * Zero.
  3447. */
  3448. int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  3449. unsigned int id, u64 lun)
  3450. {
  3451. struct ata_port *ap = ata_shost_to_port(shost);
  3452. unsigned long flags;
  3453. int devno, rc = 0;
  3454. if (!ap->ops->error_handler)
  3455. return -EOPNOTSUPP;
  3456. if (lun != SCAN_WILD_CARD && lun)
  3457. return -EINVAL;
  3458. if (!sata_pmp_attached(ap)) {
  3459. if (channel != SCAN_WILD_CARD && channel)
  3460. return -EINVAL;
  3461. devno = id;
  3462. } else {
  3463. if (id != SCAN_WILD_CARD && id)
  3464. return -EINVAL;
  3465. devno = channel;
  3466. }
  3467. spin_lock_irqsave(ap->lock, flags);
  3468. if (devno == SCAN_WILD_CARD) {
  3469. struct ata_link *link;
  3470. ata_for_each_link(link, ap, EDGE) {
  3471. struct ata_eh_info *ehi = &link->eh_info;
  3472. ehi->probe_mask |= ATA_ALL_DEVICES;
  3473. ehi->action |= ATA_EH_RESET;
  3474. }
  3475. } else {
  3476. struct ata_device *dev = ata_find_dev(ap, devno);
  3477. if (dev) {
  3478. struct ata_eh_info *ehi = &dev->link->eh_info;
  3479. ehi->probe_mask |= 1 << dev->devno;
  3480. ehi->action |= ATA_EH_RESET;
  3481. } else
  3482. rc = -EINVAL;
  3483. }
  3484. if (rc == 0) {
  3485. ata_port_schedule_eh(ap);
  3486. spin_unlock_irqrestore(ap->lock, flags);
  3487. ata_port_wait_eh(ap);
  3488. } else
  3489. spin_unlock_irqrestore(ap->lock, flags);
  3490. return rc;
  3491. }
  3492. /**
  3493. * ata_scsi_dev_rescan - initiate scsi_rescan_device()
  3494. * @work: Pointer to ATA port to perform scsi_rescan_device()
  3495. *
  3496. * After ATA pass thru (SAT) commands are executed successfully,
  3497. * libata need to propagate the changes to SCSI layer.
  3498. *
  3499. * LOCKING:
  3500. * Kernel thread context (may sleep).
  3501. */
  3502. void ata_scsi_dev_rescan(struct work_struct *work)
  3503. {
  3504. struct ata_port *ap =
  3505. container_of(work, struct ata_port, scsi_rescan_task);
  3506. struct ata_link *link;
  3507. struct ata_device *dev;
  3508. unsigned long flags;
  3509. mutex_lock(&ap->scsi_scan_mutex);
  3510. spin_lock_irqsave(ap->lock, flags);
  3511. ata_for_each_link(link, ap, EDGE) {
  3512. ata_for_each_dev(dev, link, ENABLED) {
  3513. struct scsi_device *sdev = dev->sdev;
  3514. if (!sdev)
  3515. continue;
  3516. if (scsi_device_get(sdev))
  3517. continue;
  3518. spin_unlock_irqrestore(ap->lock, flags);
  3519. scsi_rescan_device(&(sdev->sdev_gendev));
  3520. scsi_device_put(sdev);
  3521. spin_lock_irqsave(ap->lock, flags);
  3522. }
  3523. }
  3524. spin_unlock_irqrestore(ap->lock, flags);
  3525. mutex_unlock(&ap->scsi_scan_mutex);
  3526. }
  3527. /**
  3528. * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
  3529. * @host: ATA host container for all SAS ports
  3530. * @port_info: Information from low-level host driver
  3531. * @shost: SCSI host that the scsi device is attached to
  3532. *
  3533. * LOCKING:
  3534. * PCI/etc. bus probe sem.
  3535. *
  3536. * RETURNS:
  3537. * ata_port pointer on success / NULL on failure.
  3538. */
  3539. struct ata_port *ata_sas_port_alloc(struct ata_host *host,
  3540. struct ata_port_info *port_info,
  3541. struct Scsi_Host *shost)
  3542. {
  3543. struct ata_port *ap;
  3544. ap = ata_port_alloc(host);
  3545. if (!ap)
  3546. return NULL;
  3547. ap->port_no = 0;
  3548. ap->lock = &host->lock;
  3549. ap->pio_mask = port_info->pio_mask;
  3550. ap->mwdma_mask = port_info->mwdma_mask;
  3551. ap->udma_mask = port_info->udma_mask;
  3552. ap->flags |= port_info->flags;
  3553. ap->ops = port_info->port_ops;
  3554. ap->cbl = ATA_CBL_SATA;
  3555. return ap;
  3556. }
  3557. EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  3558. /**
  3559. * ata_sas_port_start - Set port up for dma.
  3560. * @ap: Port to initialize
  3561. *
  3562. * Called just after data structures for each port are
  3563. * initialized.
  3564. *
  3565. * May be used as the port_start() entry in ata_port_operations.
  3566. *
  3567. * LOCKING:
  3568. * Inherited from caller.
  3569. */
  3570. int ata_sas_port_start(struct ata_port *ap)
  3571. {
  3572. /*
  3573. * the port is marked as frozen at allocation time, but if we don't
  3574. * have new eh, we won't thaw it
  3575. */
  3576. if (!ap->ops->error_handler)
  3577. ap->pflags &= ~ATA_PFLAG_FROZEN;
  3578. return 0;
  3579. }
  3580. EXPORT_SYMBOL_GPL(ata_sas_port_start);
  3581. /**
  3582. * ata_port_stop - Undo ata_sas_port_start()
  3583. * @ap: Port to shut down
  3584. *
  3585. * May be used as the port_stop() entry in ata_port_operations.
  3586. *
  3587. * LOCKING:
  3588. * Inherited from caller.
  3589. */
  3590. void ata_sas_port_stop(struct ata_port *ap)
  3591. {
  3592. }
  3593. EXPORT_SYMBOL_GPL(ata_sas_port_stop);
  3594. /**
  3595. * ata_sas_async_probe - simply schedule probing and return
  3596. * @ap: Port to probe
  3597. *
  3598. * For batch scheduling of probe for sas attached ata devices, assumes
  3599. * the port has already been through ata_sas_port_init()
  3600. */
  3601. void ata_sas_async_probe(struct ata_port *ap)
  3602. {
  3603. __ata_port_probe(ap);
  3604. }
  3605. EXPORT_SYMBOL_GPL(ata_sas_async_probe);
  3606. int ata_sas_sync_probe(struct ata_port *ap)
  3607. {
  3608. return ata_port_probe(ap);
  3609. }
  3610. EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
  3611. /**
  3612. * ata_sas_port_init - Initialize a SATA device
  3613. * @ap: SATA port to initialize
  3614. *
  3615. * LOCKING:
  3616. * PCI/etc. bus probe sem.
  3617. *
  3618. * RETURNS:
  3619. * Zero on success, non-zero on error.
  3620. */
  3621. int ata_sas_port_init(struct ata_port *ap)
  3622. {
  3623. int rc = ap->ops->port_start(ap);
  3624. if (rc)
  3625. return rc;
  3626. ap->print_id = atomic_inc_return(&ata_print_id);
  3627. return 0;
  3628. }
  3629. EXPORT_SYMBOL_GPL(ata_sas_port_init);
  3630. /**
  3631. * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
  3632. * @ap: SATA port to destroy
  3633. *
  3634. */
  3635. void ata_sas_port_destroy(struct ata_port *ap)
  3636. {
  3637. if (ap->ops->port_stop)
  3638. ap->ops->port_stop(ap);
  3639. kfree(ap);
  3640. }
  3641. EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
  3642. /**
  3643. * ata_sas_slave_configure - Default slave_config routine for libata devices
  3644. * @sdev: SCSI device to configure
  3645. * @ap: ATA port to which SCSI device is attached
  3646. *
  3647. * RETURNS:
  3648. * Zero.
  3649. */
  3650. int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
  3651. {
  3652. ata_scsi_sdev_config(sdev);
  3653. ata_scsi_dev_config(sdev, ap->link.device);
  3654. return 0;
  3655. }
  3656. EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
  3657. /**
  3658. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  3659. * @cmd: SCSI command to be sent
  3660. * @ap: ATA port to which the command is being sent
  3661. *
  3662. * RETURNS:
  3663. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3664. * 0 otherwise.
  3665. */
  3666. int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
  3667. {
  3668. int rc = 0;
  3669. ata_scsi_dump_cdb(ap, cmd);
  3670. if (likely(ata_dev_enabled(ap->link.device)))
  3671. rc = __ata_scsi_queuecmd(cmd, ap->link.device);
  3672. else {
  3673. cmd->result = (DID_BAD_TARGET << 16);
  3674. cmd->scsi_done(cmd);
  3675. }
  3676. return rc;
  3677. }
  3678. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
  3679. int ata_sas_allocate_tag(struct ata_port *ap)
  3680. {
  3681. unsigned int max_queue = ap->host->n_tags;
  3682. unsigned int i, tag;
  3683. for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
  3684. tag = tag < max_queue ? tag : 0;
  3685. /* the last tag is reserved for internal command. */
  3686. if (tag == ATA_TAG_INTERNAL)
  3687. continue;
  3688. if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
  3689. ap->sas_last_tag = tag;
  3690. return tag;
  3691. }
  3692. }
  3693. return -1;
  3694. }
  3695. void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
  3696. {
  3697. clear_bit(tag, &ap->sas_tag_allocated);
  3698. }