libata-scsi.c 125 KB

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