libata-scsi.c 104 KB

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