libata-scsi.c 107 KB

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