libata-scsi.c 104 KB

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