libata-scsi.c 128 KB

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