libata-scsi.c 124 KB

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