libata-scsi.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167
  1. /*
  2. * libata-scsi.c - helper library for ATA
  3. *
  4. * Maintained by: Tejun Heo <tj@kernel.org>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from
  31. * - http://www.t10.org/
  32. * - http://www.t13.org/
  33. *
  34. */
  35. #include <linux/slab.h>
  36. #include <linux/kernel.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/export.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_eh.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <scsi/scsi_transport.h>
  47. #include <linux/libata.h>
  48. #include <linux/hdreg.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/suspend.h>
  51. #include <linux/pm_qos.h>
  52. #include <asm/unaligned.h>
  53. #include "libata.h"
  54. #include "libata-transport.h"
  55. #define ATA_SCSI_RBUF_SIZE 4096
  56. static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
  57. static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
  58. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
  59. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  60. const struct scsi_device *scsidev);
  61. static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
  62. const struct scsi_device *scsidev);
  63. #define RW_RECOVERY_MPAGE 0x1
  64. #define RW_RECOVERY_MPAGE_LEN 12
  65. #define CACHE_MPAGE 0x8
  66. #define CACHE_MPAGE_LEN 20
  67. #define CONTROL_MPAGE 0xa
  68. #define CONTROL_MPAGE_LEN 12
  69. #define ALL_MPAGES 0x3f
  70. #define ALL_SUB_MPAGES 0xff
  71. static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
  72. RW_RECOVERY_MPAGE,
  73. RW_RECOVERY_MPAGE_LEN - 2,
  74. (1 << 7), /* AWRE */
  75. 0, /* read retry count */
  76. 0, 0, 0, 0,
  77. 0, /* write retry count */
  78. 0, 0, 0
  79. };
  80. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  81. CACHE_MPAGE,
  82. CACHE_MPAGE_LEN - 2,
  83. 0, /* contains WCE, needs to be 0 for logic */
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, /* contains DRA, needs to be 0 for logic */
  86. 0, 0, 0, 0, 0, 0, 0
  87. };
  88. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  89. CONTROL_MPAGE,
  90. CONTROL_MPAGE_LEN - 2,
  91. 2, /* DSENSE=0, GLTSD=1 */
  92. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  93. 0, 0, 0, 0, 0xff, 0xff,
  94. 0, 30 /* extended self test time, see 05-359r1 */
  95. };
  96. static const char *ata_lpm_policy_names[] = {
  97. [ATA_LPM_UNKNOWN] = "max_performance",
  98. [ATA_LPM_MAX_POWER] = "max_performance",
  99. [ATA_LPM_MED_POWER] = "medium_power",
  100. [ATA_LPM_MIN_POWER] = "min_power",
  101. };
  102. static ssize_t ata_scsi_lpm_store(struct device *dev,
  103. struct device_attribute *attr,
  104. const char *buf, size_t count)
  105. {
  106. struct Scsi_Host *shost = class_to_shost(dev);
  107. struct ata_port *ap = ata_shost_to_port(shost);
  108. enum ata_lpm_policy policy;
  109. unsigned long flags;
  110. /* UNKNOWN is internal state, iterate from MAX_POWER */
  111. for (policy = ATA_LPM_MAX_POWER;
  112. policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
  113. const char *name = ata_lpm_policy_names[policy];
  114. if (strncmp(name, buf, strlen(name)) == 0)
  115. break;
  116. }
  117. if (policy == ARRAY_SIZE(ata_lpm_policy_names))
  118. return -EINVAL;
  119. spin_lock_irqsave(ap->lock, flags);
  120. ap->target_lpm_policy = policy;
  121. ata_port_schedule_eh(ap);
  122. spin_unlock_irqrestore(ap->lock, flags);
  123. return count;
  124. }
  125. static ssize_t ata_scsi_lpm_show(struct device *dev,
  126. struct device_attribute *attr, char *buf)
  127. {
  128. struct Scsi_Host *shost = class_to_shost(dev);
  129. struct ata_port *ap = ata_shost_to_port(shost);
  130. if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
  131. return -EINVAL;
  132. return snprintf(buf, PAGE_SIZE, "%s\n",
  133. ata_lpm_policy_names[ap->target_lpm_policy]);
  134. }
  135. DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
  136. ata_scsi_lpm_show, ata_scsi_lpm_store);
  137. EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
  138. static ssize_t ata_scsi_park_show(struct device *device,
  139. struct device_attribute *attr, char *buf)
  140. {
  141. struct scsi_device *sdev = to_scsi_device(device);
  142. struct ata_port *ap;
  143. struct ata_link *link;
  144. struct ata_device *dev;
  145. unsigned long flags, now;
  146. unsigned int uninitialized_var(msecs);
  147. int rc = 0;
  148. ap = ata_shost_to_port(sdev->host);
  149. spin_lock_irqsave(ap->lock, flags);
  150. dev = ata_scsi_find_dev(ap, sdev);
  151. if (!dev) {
  152. rc = -ENODEV;
  153. goto unlock;
  154. }
  155. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  156. rc = -EOPNOTSUPP;
  157. goto unlock;
  158. }
  159. link = dev->link;
  160. now = jiffies;
  161. if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
  162. link->eh_context.unloaded_mask & (1 << dev->devno) &&
  163. time_after(dev->unpark_deadline, now))
  164. msecs = jiffies_to_msecs(dev->unpark_deadline - now);
  165. else
  166. msecs = 0;
  167. unlock:
  168. spin_unlock_irq(ap->lock);
  169. return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
  170. }
  171. static ssize_t ata_scsi_park_store(struct device *device,
  172. struct device_attribute *attr,
  173. const char *buf, size_t len)
  174. {
  175. struct scsi_device *sdev = to_scsi_device(device);
  176. struct ata_port *ap;
  177. struct ata_device *dev;
  178. long int input;
  179. unsigned long flags;
  180. int rc;
  181. rc = kstrtol(buf, 10, &input);
  182. if (rc)
  183. return rc;
  184. if (input < -2)
  185. return -EINVAL;
  186. if (input > ATA_TMOUT_MAX_PARK) {
  187. rc = -EOVERFLOW;
  188. input = ATA_TMOUT_MAX_PARK;
  189. }
  190. ap = ata_shost_to_port(sdev->host);
  191. spin_lock_irqsave(ap->lock, flags);
  192. dev = ata_scsi_find_dev(ap, sdev);
  193. if (unlikely(!dev)) {
  194. rc = -ENODEV;
  195. goto unlock;
  196. }
  197. if (dev->class != ATA_DEV_ATA) {
  198. rc = -EOPNOTSUPP;
  199. goto unlock;
  200. }
  201. if (input >= 0) {
  202. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  203. rc = -EOPNOTSUPP;
  204. goto unlock;
  205. }
  206. dev->unpark_deadline = ata_deadline(jiffies, input);
  207. dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
  208. ata_port_schedule_eh(ap);
  209. complete(&ap->park_req_pending);
  210. } else {
  211. switch (input) {
  212. case -1:
  213. dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
  214. break;
  215. case -2:
  216. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  217. break;
  218. }
  219. }
  220. unlock:
  221. spin_unlock_irqrestore(ap->lock, flags);
  222. return rc ? rc : len;
  223. }
  224. DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
  225. ata_scsi_park_show, ata_scsi_park_store);
  226. EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
  227. static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  228. {
  229. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  230. scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
  231. }
  232. static ssize_t
  233. ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
  234. const char *buf, size_t count)
  235. {
  236. struct Scsi_Host *shost = class_to_shost(dev);
  237. struct ata_port *ap = ata_shost_to_port(shost);
  238. if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
  239. return ap->ops->em_store(ap, buf, count);
  240. return -EINVAL;
  241. }
  242. static ssize_t
  243. ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
  244. char *buf)
  245. {
  246. struct Scsi_Host *shost = class_to_shost(dev);
  247. struct ata_port *ap = ata_shost_to_port(shost);
  248. if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
  249. return ap->ops->em_show(ap, buf);
  250. return -EINVAL;
  251. }
  252. DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
  253. ata_scsi_em_message_show, ata_scsi_em_message_store);
  254. EXPORT_SYMBOL_GPL(dev_attr_em_message);
  255. static ssize_t
  256. ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
  257. char *buf)
  258. {
  259. struct Scsi_Host *shost = class_to_shost(dev);
  260. struct ata_port *ap = ata_shost_to_port(shost);
  261. return snprintf(buf, 23, "%d\n", ap->em_message_type);
  262. }
  263. DEVICE_ATTR(em_message_type, S_IRUGO,
  264. ata_scsi_em_message_type_show, NULL);
  265. EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
  266. static ssize_t
  267. ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct scsi_device *sdev = to_scsi_device(dev);
  271. struct ata_port *ap = ata_shost_to_port(sdev->host);
  272. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  273. if (atadev && ap->ops->sw_activity_show &&
  274. (ap->flags & ATA_FLAG_SW_ACTIVITY))
  275. return ap->ops->sw_activity_show(atadev, buf);
  276. return -EINVAL;
  277. }
  278. static ssize_t
  279. ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. struct scsi_device *sdev = to_scsi_device(dev);
  283. struct ata_port *ap = ata_shost_to_port(sdev->host);
  284. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  285. enum sw_activity val;
  286. int rc;
  287. if (atadev && ap->ops->sw_activity_store &&
  288. (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
  289. val = simple_strtoul(buf, NULL, 0);
  290. switch (val) {
  291. case OFF: case BLINK_ON: case BLINK_OFF:
  292. rc = ap->ops->sw_activity_store(atadev, val);
  293. if (!rc)
  294. return count;
  295. else
  296. return rc;
  297. }
  298. }
  299. return -EINVAL;
  300. }
  301. DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
  302. ata_scsi_activity_store);
  303. EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
  304. struct device_attribute *ata_common_sdev_attrs[] = {
  305. &dev_attr_unload_heads,
  306. NULL
  307. };
  308. EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
  309. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd)
  310. {
  311. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  312. /* "Invalid field in cbd" */
  313. cmd->scsi_done(cmd);
  314. }
  315. /**
  316. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  317. * @sdev: SCSI device for which BIOS geometry is to be determined
  318. * @bdev: block device associated with @sdev
  319. * @capacity: capacity of SCSI device
  320. * @geom: location to which geometry will be output
  321. *
  322. * Generic bios head/sector/cylinder calculator
  323. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  324. * mapping. Some situations may arise where the disk is not
  325. * bootable if this is not used.
  326. *
  327. * LOCKING:
  328. * Defined by the SCSI layer. We don't really care.
  329. *
  330. * RETURNS:
  331. * Zero.
  332. */
  333. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  334. sector_t capacity, int geom[])
  335. {
  336. geom[0] = 255;
  337. geom[1] = 63;
  338. sector_div(capacity, 255*63);
  339. geom[2] = capacity;
  340. return 0;
  341. }
  342. /**
  343. * ata_scsi_unlock_native_capacity - unlock native capacity
  344. * @sdev: SCSI device to adjust device capacity for
  345. *
  346. * This function is called if a partition on @sdev extends beyond
  347. * the end of the device. It requests EH to unlock HPA.
  348. *
  349. * LOCKING:
  350. * Defined by the SCSI layer. Might sleep.
  351. */
  352. void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
  353. {
  354. struct ata_port *ap = ata_shost_to_port(sdev->host);
  355. struct ata_device *dev;
  356. unsigned long flags;
  357. spin_lock_irqsave(ap->lock, flags);
  358. dev = ata_scsi_find_dev(ap, sdev);
  359. if (dev && dev->n_sectors < dev->n_native_sectors) {
  360. dev->flags |= ATA_DFLAG_UNLOCK_HPA;
  361. dev->link->eh_info.action |= ATA_EH_RESET;
  362. ata_port_schedule_eh(ap);
  363. }
  364. spin_unlock_irqrestore(ap->lock, flags);
  365. ata_port_wait_eh(ap);
  366. }
  367. /**
  368. * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
  369. * @ap: target port
  370. * @sdev: SCSI device to get identify data for
  371. * @arg: User buffer area for identify data
  372. *
  373. * LOCKING:
  374. * Defined by the SCSI layer. We don't really care.
  375. *
  376. * RETURNS:
  377. * Zero on success, negative errno on error.
  378. */
  379. static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
  380. void __user *arg)
  381. {
  382. struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
  383. u16 __user *dst = arg;
  384. char buf[40];
  385. if (!dev)
  386. return -ENOMSG;
  387. if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
  388. return -EFAULT;
  389. ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
  390. if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
  391. return -EFAULT;
  392. ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
  393. if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
  394. return -EFAULT;
  395. ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  396. if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
  397. return -EFAULT;
  398. return 0;
  399. }
  400. /**
  401. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  402. * @scsidev: Device to which we are issuing command
  403. * @arg: User provided data for issuing command
  404. *
  405. * LOCKING:
  406. * Defined by the SCSI layer. We don't really care.
  407. *
  408. * RETURNS:
  409. * Zero on success, negative errno on error.
  410. */
  411. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  412. {
  413. int rc = 0;
  414. u8 scsi_cmd[MAX_COMMAND_SIZE];
  415. u8 args[4], *argbuf = NULL, *sensebuf = NULL;
  416. int argsize = 0;
  417. enum dma_data_direction data_dir;
  418. int cmd_result;
  419. if (arg == NULL)
  420. return -EINVAL;
  421. if (copy_from_user(args, arg, sizeof(args)))
  422. return -EFAULT;
  423. sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  424. if (!sensebuf)
  425. return -ENOMEM;
  426. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  427. if (args[3]) {
  428. argsize = ATA_SECT_SIZE * args[3];
  429. argbuf = kmalloc(argsize, GFP_KERNEL);
  430. if (argbuf == NULL) {
  431. rc = -ENOMEM;
  432. goto error;
  433. }
  434. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  435. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  436. block count in sector count field */
  437. data_dir = DMA_FROM_DEVICE;
  438. } else {
  439. scsi_cmd[1] = (3 << 1); /* Non-data */
  440. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  441. data_dir = DMA_NONE;
  442. }
  443. scsi_cmd[0] = ATA_16;
  444. scsi_cmd[4] = args[2];
  445. if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
  446. scsi_cmd[6] = args[3];
  447. scsi_cmd[8] = args[1];
  448. scsi_cmd[10] = 0x4f;
  449. scsi_cmd[12] = 0xc2;
  450. } else {
  451. scsi_cmd[6] = args[1];
  452. }
  453. scsi_cmd[14] = args[0];
  454. /* Good values for timeout and retries? Values below
  455. from scsi_ioctl_send_command() for default case... */
  456. cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
  457. sensebuf, (10*HZ), 5, 0, NULL);
  458. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  459. u8 *desc = sensebuf + 8;
  460. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  461. /* If we set cc then ATA pass-through will cause a
  462. * check condition even if no error. Filter that. */
  463. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  464. struct scsi_sense_hdr sshdr;
  465. scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
  466. &sshdr);
  467. if (sshdr.sense_key == RECOVERED_ERROR &&
  468. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  469. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  470. }
  471. /* Send userspace a few ATA registers (same as drivers/ide) */
  472. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  473. desc[0] == 0x09) { /* code is "ATA Descriptor" */
  474. args[0] = desc[13]; /* status */
  475. args[1] = desc[3]; /* error */
  476. args[2] = desc[5]; /* sector count (0:7) */
  477. if (copy_to_user(arg, args, sizeof(args)))
  478. rc = -EFAULT;
  479. }
  480. }
  481. if (cmd_result) {
  482. rc = -EIO;
  483. goto error;
  484. }
  485. if ((argbuf)
  486. && copy_to_user(arg + sizeof(args), argbuf, argsize))
  487. rc = -EFAULT;
  488. error:
  489. kfree(sensebuf);
  490. kfree(argbuf);
  491. return rc;
  492. }
  493. /**
  494. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  495. * @scsidev: Device to which we are issuing command
  496. * @arg: User provided data for issuing command
  497. *
  498. * LOCKING:
  499. * Defined by the SCSI layer. We don't really care.
  500. *
  501. * RETURNS:
  502. * Zero on success, negative errno on error.
  503. */
  504. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  505. {
  506. int rc = 0;
  507. u8 scsi_cmd[MAX_COMMAND_SIZE];
  508. u8 args[7], *sensebuf = NULL;
  509. int cmd_result;
  510. if (arg == NULL)
  511. return -EINVAL;
  512. if (copy_from_user(args, arg, sizeof(args)))
  513. return -EFAULT;
  514. sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  515. if (!sensebuf)
  516. return -ENOMEM;
  517. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  518. scsi_cmd[0] = ATA_16;
  519. scsi_cmd[1] = (3 << 1); /* Non-data */
  520. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  521. scsi_cmd[4] = args[1];
  522. scsi_cmd[6] = args[2];
  523. scsi_cmd[8] = args[3];
  524. scsi_cmd[10] = args[4];
  525. scsi_cmd[12] = args[5];
  526. scsi_cmd[13] = args[6] & 0x4f;
  527. scsi_cmd[14] = args[0];
  528. /* Good values for timeout and retries? Values below
  529. from scsi_ioctl_send_command() for default case... */
  530. cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
  531. sensebuf, (10*HZ), 5, 0, NULL);
  532. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  533. u8 *desc = sensebuf + 8;
  534. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  535. /* If we set cc then ATA pass-through will cause a
  536. * check condition even if no error. Filter that. */
  537. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  538. struct scsi_sense_hdr sshdr;
  539. scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
  540. &sshdr);
  541. if (sshdr.sense_key == RECOVERED_ERROR &&
  542. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  543. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  544. }
  545. /* Send userspace ATA registers */
  546. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  547. desc[0] == 0x09) {/* code is "ATA Descriptor" */
  548. args[0] = desc[13]; /* status */
  549. args[1] = desc[3]; /* error */
  550. args[2] = desc[5]; /* sector count (0:7) */
  551. args[3] = desc[7]; /* lbal */
  552. args[4] = desc[9]; /* lbam */
  553. args[5] = desc[11]; /* lbah */
  554. args[6] = desc[12]; /* select */
  555. if (copy_to_user(arg, args, sizeof(args)))
  556. rc = -EFAULT;
  557. }
  558. }
  559. if (cmd_result) {
  560. rc = -EIO;
  561. goto error;
  562. }
  563. error:
  564. kfree(sensebuf);
  565. return rc;
  566. }
  567. static int ata_ioc32(struct ata_port *ap)
  568. {
  569. if (ap->flags & ATA_FLAG_PIO_DMA)
  570. return 1;
  571. if (ap->pflags & ATA_PFLAG_PIO32)
  572. return 1;
  573. return 0;
  574. }
  575. int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
  576. int cmd, void __user *arg)
  577. {
  578. int val = -EINVAL, rc = -EINVAL;
  579. unsigned long flags;
  580. switch (cmd) {
  581. case ATA_IOC_GET_IO32:
  582. spin_lock_irqsave(ap->lock, flags);
  583. val = ata_ioc32(ap);
  584. spin_unlock_irqrestore(ap->lock, flags);
  585. if (copy_to_user(arg, &val, 1))
  586. return -EFAULT;
  587. return 0;
  588. case ATA_IOC_SET_IO32:
  589. val = (unsigned long) arg;
  590. rc = 0;
  591. spin_lock_irqsave(ap->lock, flags);
  592. if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
  593. if (val)
  594. ap->pflags |= ATA_PFLAG_PIO32;
  595. else
  596. ap->pflags &= ~ATA_PFLAG_PIO32;
  597. } else {
  598. if (val != ata_ioc32(ap))
  599. rc = -EINVAL;
  600. }
  601. spin_unlock_irqrestore(ap->lock, flags);
  602. return rc;
  603. case HDIO_GET_IDENTITY:
  604. return ata_get_identity(ap, scsidev, arg);
  605. case HDIO_DRIVE_CMD:
  606. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  607. return -EACCES;
  608. return ata_cmd_ioctl(scsidev, arg);
  609. case HDIO_DRIVE_TASK:
  610. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  611. return -EACCES;
  612. return ata_task_ioctl(scsidev, arg);
  613. default:
  614. rc = -ENOTTY;
  615. break;
  616. }
  617. return rc;
  618. }
  619. EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
  620. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  621. {
  622. return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
  623. scsidev, cmd, arg);
  624. }
  625. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  626. /**
  627. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  628. * @dev: ATA device to which the new command is attached
  629. * @cmd: SCSI command that originated this ATA command
  630. *
  631. * Obtain a reference to an unused ata_queued_cmd structure,
  632. * which is the basic libata structure representing a single
  633. * ATA command sent to the hardware.
  634. *
  635. * If a command was available, fill in the SCSI-specific
  636. * portions of the structure with information on the
  637. * current command.
  638. *
  639. * LOCKING:
  640. * spin_lock_irqsave(host lock)
  641. *
  642. * RETURNS:
  643. * Command allocated, or %NULL if none available.
  644. */
  645. static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
  646. struct scsi_cmnd *cmd)
  647. {
  648. struct ata_queued_cmd *qc;
  649. qc = ata_qc_new_init(dev);
  650. if (qc) {
  651. qc->scsicmd = cmd;
  652. qc->scsidone = cmd->scsi_done;
  653. qc->sg = scsi_sglist(cmd);
  654. qc->n_elem = scsi_sg_count(cmd);
  655. } else {
  656. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  657. cmd->scsi_done(cmd);
  658. }
  659. return qc;
  660. }
  661. static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
  662. {
  663. struct scsi_cmnd *scmd = qc->scsicmd;
  664. qc->extrabytes = scmd->request->extra_len;
  665. qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
  666. }
  667. /**
  668. * ata_dump_status - user friendly display of error info
  669. * @id: id of the port in question
  670. * @tf: ptr to filled out taskfile
  671. *
  672. * Decode and dump the ATA error/status registers for the user so
  673. * that they have some idea what really happened at the non
  674. * make-believe layer.
  675. *
  676. * LOCKING:
  677. * inherited from caller
  678. */
  679. static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  680. {
  681. u8 stat = tf->command, err = tf->feature;
  682. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  683. if (stat & ATA_BUSY) {
  684. printk("Busy }\n"); /* Data is not valid in this case */
  685. } else {
  686. if (stat & 0x40) printk("DriveReady ");
  687. if (stat & 0x20) printk("DeviceFault ");
  688. if (stat & 0x10) printk("SeekComplete ");
  689. if (stat & 0x08) printk("DataRequest ");
  690. if (stat & 0x04) printk("CorrectedError ");
  691. if (stat & 0x02) printk("Index ");
  692. if (stat & 0x01) printk("Error ");
  693. printk("}\n");
  694. if (err) {
  695. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  696. if (err & 0x04) printk("DriveStatusError ");
  697. if (err & 0x80) {
  698. if (err & 0x04) printk("BadCRC ");
  699. else printk("Sector ");
  700. }
  701. if (err & 0x40) printk("UncorrectableError ");
  702. if (err & 0x10) printk("SectorIdNotFound ");
  703. if (err & 0x02) printk("TrackZeroNotFound ");
  704. if (err & 0x01) printk("AddrMarkNotFound ");
  705. printk("}\n");
  706. }
  707. }
  708. }
  709. /**
  710. * ata_to_sense_error - convert ATA error to SCSI error
  711. * @id: ATA device number
  712. * @drv_stat: value contained in ATA status register
  713. * @drv_err: value contained in ATA error register
  714. * @sk: the sense key we'll fill out
  715. * @asc: the additional sense code we'll fill out
  716. * @ascq: the additional sense code qualifier we'll fill out
  717. * @verbose: be verbose
  718. *
  719. * Converts an ATA error into a SCSI error. Fill out pointers to
  720. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  721. * format sense blocks.
  722. *
  723. * LOCKING:
  724. * spin_lock_irqsave(host lock)
  725. */
  726. static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
  727. u8 *asc, u8 *ascq, int verbose)
  728. {
  729. int i;
  730. /* Based on the 3ware driver translation table */
  731. static const unsigned char sense_table[][4] = {
  732. /* BBD|ECC|ID|MAR */
  733. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  734. /* BBD|ECC|ID */
  735. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  736. /* ECC|MC|MARK */
  737. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  738. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  739. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  740. /* MC|ID|ABRT|TRK0|MARK */
  741. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  742. /* MCR|MARK */
  743. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  744. /* Bad address mark */
  745. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  746. /* TRK0 */
  747. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  748. /* Abort: 0x04 is not translated here, see below */
  749. /* Media change request */
  750. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  751. /* SRV/IDNF */
  752. {0x10, ILLEGAL_REQUEST, 0x21, 0x00}, // ID not found Logical address out of range
  753. /* MC */
  754. {0x20, UNIT_ATTENTION, 0x28, 0x00}, // Media Changed Not ready to ready change, medium may have changed
  755. /* ECC */
  756. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  757. /* BBD - block marked bad */
  758. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  759. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  760. };
  761. static const unsigned char stat_table[][4] = {
  762. /* Must be first because BUSY means no other bits valid */
  763. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  764. {0x20, HARDWARE_ERROR, 0x44, 0x00}, // Device fault, internal target failure
  765. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  766. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  767. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  768. };
  769. /*
  770. * Is this an error we can process/parse
  771. */
  772. if (drv_stat & ATA_BUSY) {
  773. drv_err = 0; /* Ignore the err bits, they're invalid */
  774. }
  775. if (drv_err) {
  776. /* Look for drv_err */
  777. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  778. /* Look for best matches first */
  779. if ((sense_table[i][0] & drv_err) ==
  780. sense_table[i][0]) {
  781. *sk = sense_table[i][1];
  782. *asc = sense_table[i][2];
  783. *ascq = sense_table[i][3];
  784. goto translate_done;
  785. }
  786. }
  787. }
  788. /*
  789. * Fall back to interpreting status bits. Note that if the drv_err
  790. * has only the ABRT bit set, we decode drv_stat. ABRT by itself
  791. * is not descriptive enough.
  792. */
  793. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  794. if (stat_table[i][0] & drv_stat) {
  795. *sk = stat_table[i][1];
  796. *asc = stat_table[i][2];
  797. *ascq = stat_table[i][3];
  798. goto translate_done;
  799. }
  800. }
  801. /*
  802. * We need a sensible error return here, which is tricky, and one
  803. * that won't cause people to do things like return a disk wrongly.
  804. */
  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. /* we don't store the ATA device signature, so we fake it */
  1851. tf.command = ATA_DRDY; /* really, this is Status reg */
  1852. tf.lbal = 0x1;
  1853. tf.nsect = 0x1;
  1854. ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
  1855. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  1856. rbuf[56] = ATA_CMD_ID_ATA;
  1857. memcpy(&rbuf[60], &args->id[0], 512);
  1858. return 0;
  1859. }
  1860. static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
  1861. {
  1862. u16 min_io_sectors;
  1863. rbuf[1] = 0xb0;
  1864. rbuf[3] = 0x3c; /* required VPD size with unmap support */
  1865. /*
  1866. * Optimal transfer length granularity.
  1867. *
  1868. * This is always one physical block, but for disks with a smaller
  1869. * logical than physical sector size we need to figure out what the
  1870. * latter is.
  1871. */
  1872. min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
  1873. put_unaligned_be16(min_io_sectors, &rbuf[6]);
  1874. /*
  1875. * Optimal unmap granularity.
  1876. *
  1877. * The ATA spec doesn't even know about a granularity or alignment
  1878. * for the TRIM command. We can leave away most of the unmap related
  1879. * VPD page entries, but we have specifify a granularity to signal
  1880. * that we support some form of unmap - in thise case via WRITE SAME
  1881. * with the unmap bit set.
  1882. */
  1883. if (ata_id_has_trim(args->id)) {
  1884. put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
  1885. put_unaligned_be32(1, &rbuf[28]);
  1886. }
  1887. return 0;
  1888. }
  1889. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  1890. {
  1891. int form_factor = ata_id_form_factor(args->id);
  1892. int media_rotation_rate = ata_id_rotation_rate(args->id);
  1893. rbuf[1] = 0xb1;
  1894. rbuf[3] = 0x3c;
  1895. rbuf[4] = media_rotation_rate >> 8;
  1896. rbuf[5] = media_rotation_rate;
  1897. rbuf[7] = form_factor;
  1898. return 0;
  1899. }
  1900. static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
  1901. {
  1902. /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
  1903. rbuf[1] = 0xb2;
  1904. rbuf[3] = 0x4;
  1905. rbuf[5] = 1 << 6; /* TPWS */
  1906. return 0;
  1907. }
  1908. /**
  1909. * ata_scsiop_noop - Command handler that simply returns success.
  1910. * @args: device IDENTIFY data / SCSI command of interest.
  1911. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1912. *
  1913. * No operation. Simply returns success to caller, to indicate
  1914. * that the caller should successfully complete this SCSI command.
  1915. *
  1916. * LOCKING:
  1917. * spin_lock_irqsave(host lock)
  1918. */
  1919. static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
  1920. {
  1921. VPRINTK("ENTER\n");
  1922. return 0;
  1923. }
  1924. /**
  1925. * modecpy - Prepare response for MODE SENSE
  1926. * @dest: output buffer
  1927. * @src: data being copied
  1928. * @n: length of mode page
  1929. * @changeable: whether changeable parameters are requested
  1930. *
  1931. * Generate a generic MODE SENSE page for either current or changeable
  1932. * parameters.
  1933. *
  1934. * LOCKING:
  1935. * None.
  1936. */
  1937. static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
  1938. {
  1939. if (changeable) {
  1940. memcpy(dest, src, 2);
  1941. memset(dest + 2, 0, n - 2);
  1942. } else {
  1943. memcpy(dest, src, n);
  1944. }
  1945. }
  1946. /**
  1947. * ata_msense_caching - Simulate MODE SENSE caching info page
  1948. * @id: device IDENTIFY data
  1949. * @buf: output buffer
  1950. * @changeable: whether changeable parameters are requested
  1951. *
  1952. * Generate a caching info page, which conditionally indicates
  1953. * write caching to the SCSI layer, depending on device
  1954. * capabilities.
  1955. *
  1956. * LOCKING:
  1957. * None.
  1958. */
  1959. static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
  1960. {
  1961. modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
  1962. if (changeable || ata_id_wcache_enabled(id))
  1963. buf[2] |= (1 << 2); /* write cache enable */
  1964. if (!changeable && !ata_id_rahead_enabled(id))
  1965. buf[12] |= (1 << 5); /* disable read ahead */
  1966. return sizeof(def_cache_mpage);
  1967. }
  1968. /**
  1969. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1970. * @buf: output buffer
  1971. * @changeable: whether changeable parameters are requested
  1972. *
  1973. * Generate a generic MODE SENSE control mode page.
  1974. *
  1975. * LOCKING:
  1976. * None.
  1977. */
  1978. static unsigned int ata_msense_ctl_mode(u8 *buf, bool changeable)
  1979. {
  1980. modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
  1981. return sizeof(def_control_mpage);
  1982. }
  1983. /**
  1984. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1985. * @buf: output buffer
  1986. * @changeable: whether changeable parameters are requested
  1987. *
  1988. * Generate a generic MODE SENSE r/w error recovery page.
  1989. *
  1990. * LOCKING:
  1991. * None.
  1992. */
  1993. static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
  1994. {
  1995. modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
  1996. changeable);
  1997. return sizeof(def_rw_recovery_mpage);
  1998. }
  1999. /*
  2000. * We can turn this into a real blacklist if it's needed, for now just
  2001. * blacklist any Maxtor BANC1G10 revision firmware
  2002. */
  2003. static int ata_dev_supports_fua(u16 *id)
  2004. {
  2005. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  2006. if (!libata_fua)
  2007. return 0;
  2008. if (!ata_id_has_fua(id))
  2009. return 0;
  2010. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  2011. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  2012. if (strcmp(model, "Maxtor"))
  2013. return 1;
  2014. if (strcmp(fw, "BANC1G10"))
  2015. return 1;
  2016. return 0; /* blacklisted */
  2017. }
  2018. /**
  2019. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  2020. * @args: device IDENTIFY data / SCSI command of interest.
  2021. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2022. *
  2023. * Simulate MODE SENSE commands. Assume this is invoked for direct
  2024. * access devices (e.g. disks) only. There should be no block
  2025. * descriptor for other device types.
  2026. *
  2027. * LOCKING:
  2028. * spin_lock_irqsave(host lock)
  2029. */
  2030. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  2031. {
  2032. struct ata_device *dev = args->dev;
  2033. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  2034. const u8 sat_blk_desc[] = {
  2035. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  2036. 0,
  2037. 0, 0x2, 0x0 /* block length: 512 bytes */
  2038. };
  2039. u8 pg, spg;
  2040. unsigned int ebd, page_control, six_byte;
  2041. u8 dpofua;
  2042. VPRINTK("ENTER\n");
  2043. six_byte = (scsicmd[0] == MODE_SENSE);
  2044. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  2045. /*
  2046. * LLBA bit in msense(10) ignored (compliant)
  2047. */
  2048. page_control = scsicmd[2] >> 6;
  2049. switch (page_control) {
  2050. case 0: /* current */
  2051. case 1: /* changeable */
  2052. case 2: /* defaults */
  2053. break; /* supported */
  2054. case 3: /* saved */
  2055. goto saving_not_supp;
  2056. default:
  2057. goto invalid_fld;
  2058. }
  2059. if (six_byte)
  2060. p += 4 + (ebd ? 8 : 0);
  2061. else
  2062. p += 8 + (ebd ? 8 : 0);
  2063. pg = scsicmd[2] & 0x3f;
  2064. spg = scsicmd[3];
  2065. /*
  2066. * No mode subpages supported (yet) but asking for _all_
  2067. * subpages may be valid
  2068. */
  2069. if (spg && (spg != ALL_SUB_MPAGES))
  2070. goto invalid_fld;
  2071. switch(pg) {
  2072. case RW_RECOVERY_MPAGE:
  2073. p += ata_msense_rw_recovery(p, page_control == 1);
  2074. break;
  2075. case CACHE_MPAGE:
  2076. p += ata_msense_caching(args->id, p, page_control == 1);
  2077. break;
  2078. case CONTROL_MPAGE:
  2079. p += ata_msense_ctl_mode(p, page_control == 1);
  2080. break;
  2081. case ALL_MPAGES:
  2082. p += ata_msense_rw_recovery(p, page_control == 1);
  2083. p += ata_msense_caching(args->id, p, page_control == 1);
  2084. p += ata_msense_ctl_mode(p, page_control == 1);
  2085. break;
  2086. default: /* invalid page code */
  2087. goto invalid_fld;
  2088. }
  2089. dpofua = 0;
  2090. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2091. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2092. dpofua = 1 << 4;
  2093. if (six_byte) {
  2094. rbuf[0] = p - rbuf - 1;
  2095. rbuf[2] |= dpofua;
  2096. if (ebd) {
  2097. rbuf[3] = sizeof(sat_blk_desc);
  2098. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2099. }
  2100. } else {
  2101. unsigned int output_len = p - rbuf - 2;
  2102. rbuf[0] = output_len >> 8;
  2103. rbuf[1] = output_len;
  2104. rbuf[3] |= dpofua;
  2105. if (ebd) {
  2106. rbuf[7] = sizeof(sat_blk_desc);
  2107. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2108. }
  2109. }
  2110. return 0;
  2111. invalid_fld:
  2112. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2113. /* "Invalid field in cbd" */
  2114. return 1;
  2115. saving_not_supp:
  2116. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2117. /* "Saving parameters not supported" */
  2118. return 1;
  2119. }
  2120. /**
  2121. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2122. * @args: device IDENTIFY data / SCSI command of interest.
  2123. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2124. *
  2125. * Simulate READ CAPACITY commands.
  2126. *
  2127. * LOCKING:
  2128. * None.
  2129. */
  2130. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2131. {
  2132. struct ata_device *dev = args->dev;
  2133. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2134. u32 sector_size; /* physical sector size in bytes */
  2135. u8 log2_per_phys;
  2136. u16 lowest_aligned;
  2137. sector_size = ata_id_logical_sector_size(dev->id);
  2138. log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
  2139. lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
  2140. VPRINTK("ENTER\n");
  2141. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2142. if (last_lba >= 0xffffffffULL)
  2143. last_lba = 0xffffffff;
  2144. /* sector count, 32-bit */
  2145. rbuf[0] = last_lba >> (8 * 3);
  2146. rbuf[1] = last_lba >> (8 * 2);
  2147. rbuf[2] = last_lba >> (8 * 1);
  2148. rbuf[3] = last_lba;
  2149. /* sector size */
  2150. rbuf[4] = sector_size >> (8 * 3);
  2151. rbuf[5] = sector_size >> (8 * 2);
  2152. rbuf[6] = sector_size >> (8 * 1);
  2153. rbuf[7] = sector_size;
  2154. } else {
  2155. /* sector count, 64-bit */
  2156. rbuf[0] = last_lba >> (8 * 7);
  2157. rbuf[1] = last_lba >> (8 * 6);
  2158. rbuf[2] = last_lba >> (8 * 5);
  2159. rbuf[3] = last_lba >> (8 * 4);
  2160. rbuf[4] = last_lba >> (8 * 3);
  2161. rbuf[5] = last_lba >> (8 * 2);
  2162. rbuf[6] = last_lba >> (8 * 1);
  2163. rbuf[7] = last_lba;
  2164. /* sector size */
  2165. rbuf[ 8] = sector_size >> (8 * 3);
  2166. rbuf[ 9] = sector_size >> (8 * 2);
  2167. rbuf[10] = sector_size >> (8 * 1);
  2168. rbuf[11] = sector_size;
  2169. rbuf[12] = 0;
  2170. rbuf[13] = log2_per_phys;
  2171. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2172. rbuf[15] = lowest_aligned;
  2173. if (ata_id_has_trim(args->id)) {
  2174. rbuf[14] |= 0x80; /* TPE */
  2175. if (ata_id_has_zero_after_trim(args->id))
  2176. rbuf[14] |= 0x40; /* TPRZ */
  2177. }
  2178. }
  2179. return 0;
  2180. }
  2181. /**
  2182. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2183. * @args: device IDENTIFY data / SCSI command of interest.
  2184. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2185. *
  2186. * Simulate REPORT LUNS command.
  2187. *
  2188. * LOCKING:
  2189. * spin_lock_irqsave(host lock)
  2190. */
  2191. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2192. {
  2193. VPRINTK("ENTER\n");
  2194. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2195. return 0;
  2196. }
  2197. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2198. {
  2199. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2200. /* FIXME: not quite right; we don't want the
  2201. * translation of taskfile registers into
  2202. * a sense descriptors, since that's only
  2203. * correct for ATA, not ATAPI
  2204. */
  2205. ata_gen_passthru_sense(qc);
  2206. }
  2207. qc->scsidone(qc->scsicmd);
  2208. ata_qc_free(qc);
  2209. }
  2210. /* is it pointless to prefer PIO for "safety reasons"? */
  2211. static inline int ata_pio_use_silly(struct ata_port *ap)
  2212. {
  2213. return (ap->flags & ATA_FLAG_PIO_DMA);
  2214. }
  2215. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2216. {
  2217. struct ata_port *ap = qc->ap;
  2218. struct scsi_cmnd *cmd = qc->scsicmd;
  2219. DPRINTK("ATAPI request sense\n");
  2220. /* FIXME: is this needed? */
  2221. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2222. #ifdef CONFIG_ATA_SFF
  2223. if (ap->ops->sff_tf_read)
  2224. ap->ops->sff_tf_read(ap, &qc->tf);
  2225. #endif
  2226. /* fill these in, for the case where they are -not- overwritten */
  2227. cmd->sense_buffer[0] = 0x70;
  2228. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2229. ata_qc_reinit(qc);
  2230. /* setup sg table and init transfer direction */
  2231. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2232. ata_sg_init(qc, &qc->sgent, 1);
  2233. qc->dma_dir = DMA_FROM_DEVICE;
  2234. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2235. qc->cdb[0] = REQUEST_SENSE;
  2236. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2237. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2238. qc->tf.command = ATA_CMD_PACKET;
  2239. if (ata_pio_use_silly(ap)) {
  2240. qc->tf.protocol = ATAPI_PROT_DMA;
  2241. qc->tf.feature |= ATAPI_PKT_DMA;
  2242. } else {
  2243. qc->tf.protocol = ATAPI_PROT_PIO;
  2244. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2245. qc->tf.lbah = 0;
  2246. }
  2247. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2248. qc->complete_fn = atapi_sense_complete;
  2249. ata_qc_issue(qc);
  2250. DPRINTK("EXIT\n");
  2251. }
  2252. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2253. {
  2254. struct scsi_cmnd *cmd = qc->scsicmd;
  2255. unsigned int err_mask = qc->err_mask;
  2256. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2257. /* handle completion from new EH */
  2258. if (unlikely(qc->ap->ops->error_handler &&
  2259. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2260. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2261. /* FIXME: not quite right; we don't want the
  2262. * translation of taskfile registers into a
  2263. * sense descriptors, since that's only
  2264. * correct for ATA, not ATAPI
  2265. */
  2266. ata_gen_passthru_sense(qc);
  2267. }
  2268. /* SCSI EH automatically locks door if sdev->locked is
  2269. * set. Sometimes door lock request continues to
  2270. * fail, for example, when no media is present. This
  2271. * creates a loop - SCSI EH issues door lock which
  2272. * fails and gets invoked again to acquire sense data
  2273. * for the failed command.
  2274. *
  2275. * If door lock fails, always clear sdev->locked to
  2276. * avoid this infinite loop.
  2277. *
  2278. * This may happen before SCSI scan is complete. Make
  2279. * sure qc->dev->sdev isn't NULL before dereferencing.
  2280. */
  2281. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
  2282. qc->dev->sdev->locked = 0;
  2283. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2284. qc->scsidone(cmd);
  2285. ata_qc_free(qc);
  2286. return;
  2287. }
  2288. /* successful completion or old EH failure path */
  2289. if (unlikely(err_mask & AC_ERR_DEV)) {
  2290. cmd->result = SAM_STAT_CHECK_CONDITION;
  2291. atapi_request_sense(qc);
  2292. return;
  2293. } else if (unlikely(err_mask)) {
  2294. /* FIXME: not quite right; we don't want the
  2295. * translation of taskfile registers into
  2296. * a sense descriptors, since that's only
  2297. * correct for ATA, not ATAPI
  2298. */
  2299. ata_gen_passthru_sense(qc);
  2300. } else {
  2301. u8 *scsicmd = cmd->cmnd;
  2302. if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
  2303. unsigned long flags;
  2304. u8 *buf;
  2305. buf = ata_scsi_rbuf_get(cmd, true, &flags);
  2306. /* ATAPI devices typically report zero for their SCSI version,
  2307. * and sometimes deviate from the spec WRT response data
  2308. * format. If SCSI version is reported as zero like normal,
  2309. * then we make the following fixups: 1) Fake MMC-5 version,
  2310. * to indicate to the Linux scsi midlayer this is a modern
  2311. * device. 2) Ensure response data format / ATAPI information
  2312. * are always correct.
  2313. */
  2314. if (buf[2] == 0) {
  2315. buf[2] = 0x5;
  2316. buf[3] = 0x32;
  2317. }
  2318. ata_scsi_rbuf_put(cmd, true, &flags);
  2319. }
  2320. cmd->result = SAM_STAT_GOOD;
  2321. }
  2322. qc->scsidone(cmd);
  2323. ata_qc_free(qc);
  2324. }
  2325. /**
  2326. * atapi_xlat - Initialize PACKET taskfile
  2327. * @qc: command structure to be initialized
  2328. *
  2329. * LOCKING:
  2330. * spin_lock_irqsave(host lock)
  2331. *
  2332. * RETURNS:
  2333. * Zero on success, non-zero on failure.
  2334. */
  2335. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2336. {
  2337. struct scsi_cmnd *scmd = qc->scsicmd;
  2338. struct ata_device *dev = qc->dev;
  2339. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2340. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2341. unsigned int nbytes;
  2342. memset(qc->cdb, 0, dev->cdb_len);
  2343. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2344. qc->complete_fn = atapi_qc_complete;
  2345. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2346. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2347. qc->tf.flags |= ATA_TFLAG_WRITE;
  2348. DPRINTK("direction: write\n");
  2349. }
  2350. qc->tf.command = ATA_CMD_PACKET;
  2351. ata_qc_set_pc_nbytes(qc);
  2352. /* check whether ATAPI DMA is safe */
  2353. if (!nodata && !using_pio && atapi_check_dma(qc))
  2354. using_pio = 1;
  2355. /* Some controller variants snoop this value for Packet
  2356. * transfers to do state machine and FIFO management. Thus we
  2357. * want to set it properly, and for DMA where it is
  2358. * effectively meaningless.
  2359. */
  2360. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2361. /* Most ATAPI devices which honor transfer chunk size don't
  2362. * behave according to the spec when odd chunk size which
  2363. * matches the transfer length is specified. If the number of
  2364. * bytes to transfer is 2n+1. According to the spec, what
  2365. * should happen is to indicate that 2n+1 is going to be
  2366. * transferred and transfer 2n+2 bytes where the last byte is
  2367. * padding.
  2368. *
  2369. * In practice, this doesn't happen. ATAPI devices first
  2370. * indicate and transfer 2n bytes and then indicate and
  2371. * transfer 2 bytes where the last byte is padding.
  2372. *
  2373. * This inconsistency confuses several controllers which
  2374. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2375. * These controllers use actual number of transferred bytes to
  2376. * update DMA poitner and transfer of 4n+2 bytes make those
  2377. * controller push DMA pointer by 4n+4 bytes because SATA data
  2378. * FISes are aligned to 4 bytes. This causes data corruption
  2379. * and buffer overrun.
  2380. *
  2381. * Always setting nbytes to even number solves this problem
  2382. * because then ATAPI devices don't have to split data at 2n
  2383. * boundaries.
  2384. */
  2385. if (nbytes & 0x1)
  2386. nbytes++;
  2387. qc->tf.lbam = (nbytes & 0xFF);
  2388. qc->tf.lbah = (nbytes >> 8);
  2389. if (nodata)
  2390. qc->tf.protocol = ATAPI_PROT_NODATA;
  2391. else if (using_pio)
  2392. qc->tf.protocol = ATAPI_PROT_PIO;
  2393. else {
  2394. /* DMA data xfer */
  2395. qc->tf.protocol = ATAPI_PROT_DMA;
  2396. qc->tf.feature |= ATAPI_PKT_DMA;
  2397. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2398. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2399. /* some SATA bridges need us to indicate data xfer direction */
  2400. qc->tf.feature |= ATAPI_DMADIR;
  2401. }
  2402. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2403. as ATAPI tape drives don't get this right otherwise */
  2404. return 0;
  2405. }
  2406. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2407. {
  2408. if (!sata_pmp_attached(ap)) {
  2409. if (likely(devno < ata_link_max_devices(&ap->link)))
  2410. return &ap->link.device[devno];
  2411. } else {
  2412. if (likely(devno < ap->nr_pmp_links))
  2413. return &ap->pmp_link[devno].device[0];
  2414. }
  2415. return NULL;
  2416. }
  2417. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2418. const struct scsi_device *scsidev)
  2419. {
  2420. int devno;
  2421. /* skip commands not addressed to targets we simulate */
  2422. if (!sata_pmp_attached(ap)) {
  2423. if (unlikely(scsidev->channel || scsidev->lun))
  2424. return NULL;
  2425. devno = scsidev->id;
  2426. } else {
  2427. if (unlikely(scsidev->id || scsidev->lun))
  2428. return NULL;
  2429. devno = scsidev->channel;
  2430. }
  2431. return ata_find_dev(ap, devno);
  2432. }
  2433. /**
  2434. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2435. * @ap: ATA port to which the device is attached
  2436. * @scsidev: SCSI device from which we derive the ATA device
  2437. *
  2438. * Given various information provided in struct scsi_cmnd,
  2439. * map that onto an ATA bus, and using that mapping
  2440. * determine which ata_device is associated with the
  2441. * SCSI command to be sent.
  2442. *
  2443. * LOCKING:
  2444. * spin_lock_irqsave(host lock)
  2445. *
  2446. * RETURNS:
  2447. * Associated ATA device, or %NULL if not found.
  2448. */
  2449. static struct ata_device *
  2450. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2451. {
  2452. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2453. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2454. return NULL;
  2455. return dev;
  2456. }
  2457. /*
  2458. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2459. * @byte1: Byte 1 from pass-thru CDB.
  2460. *
  2461. * RETURNS:
  2462. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2463. */
  2464. static u8
  2465. ata_scsi_map_proto(u8 byte1)
  2466. {
  2467. switch((byte1 & 0x1e) >> 1) {
  2468. case 3: /* Non-data */
  2469. return ATA_PROT_NODATA;
  2470. case 6: /* DMA */
  2471. case 10: /* UDMA Data-in */
  2472. case 11: /* UDMA Data-Out */
  2473. return ATA_PROT_DMA;
  2474. case 4: /* PIO Data-in */
  2475. case 5: /* PIO Data-out */
  2476. return ATA_PROT_PIO;
  2477. case 0: /* Hard Reset */
  2478. case 1: /* SRST */
  2479. case 8: /* Device Diagnostic */
  2480. case 9: /* Device Reset */
  2481. case 7: /* DMA Queued */
  2482. case 12: /* FPDMA */
  2483. case 15: /* Return Response Info */
  2484. default: /* Reserved */
  2485. break;
  2486. }
  2487. return ATA_PROT_UNKNOWN;
  2488. }
  2489. /**
  2490. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2491. * @qc: command structure to be initialized
  2492. *
  2493. * Handles either 12 or 16-byte versions of the CDB.
  2494. *
  2495. * RETURNS:
  2496. * Zero on success, non-zero on failure.
  2497. */
  2498. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2499. {
  2500. struct ata_taskfile *tf = &(qc->tf);
  2501. struct scsi_cmnd *scmd = qc->scsicmd;
  2502. struct ata_device *dev = qc->dev;
  2503. const u8 *cdb = scmd->cmnd;
  2504. if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
  2505. goto invalid_fld;
  2506. /*
  2507. * 12 and 16 byte CDBs use different offsets to
  2508. * provide the various register values.
  2509. */
  2510. if (cdb[0] == ATA_16) {
  2511. /*
  2512. * 16-byte CDB - may contain extended commands.
  2513. *
  2514. * If that is the case, copy the upper byte register values.
  2515. */
  2516. if (cdb[1] & 0x01) {
  2517. tf->hob_feature = cdb[3];
  2518. tf->hob_nsect = cdb[5];
  2519. tf->hob_lbal = cdb[7];
  2520. tf->hob_lbam = cdb[9];
  2521. tf->hob_lbah = cdb[11];
  2522. tf->flags |= ATA_TFLAG_LBA48;
  2523. } else
  2524. tf->flags &= ~ATA_TFLAG_LBA48;
  2525. /*
  2526. * Always copy low byte, device and command registers.
  2527. */
  2528. tf->feature = cdb[4];
  2529. tf->nsect = cdb[6];
  2530. tf->lbal = cdb[8];
  2531. tf->lbam = cdb[10];
  2532. tf->lbah = cdb[12];
  2533. tf->device = cdb[13];
  2534. tf->command = cdb[14];
  2535. } else {
  2536. /*
  2537. * 12-byte CDB - incapable of extended commands.
  2538. */
  2539. tf->flags &= ~ATA_TFLAG_LBA48;
  2540. tf->feature = cdb[3];
  2541. tf->nsect = cdb[4];
  2542. tf->lbal = cdb[5];
  2543. tf->lbam = cdb[6];
  2544. tf->lbah = cdb[7];
  2545. tf->device = cdb[8];
  2546. tf->command = cdb[9];
  2547. }
  2548. /* enforce correct master/slave bit */
  2549. tf->device = dev->devno ?
  2550. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2551. switch (tf->command) {
  2552. /* READ/WRITE LONG use a non-standard sect_size */
  2553. case ATA_CMD_READ_LONG:
  2554. case ATA_CMD_READ_LONG_ONCE:
  2555. case ATA_CMD_WRITE_LONG:
  2556. case ATA_CMD_WRITE_LONG_ONCE:
  2557. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
  2558. goto invalid_fld;
  2559. qc->sect_size = scsi_bufflen(scmd);
  2560. break;
  2561. /* commands using reported Logical Block size (e.g. 512 or 4K) */
  2562. case ATA_CMD_CFA_WRITE_NE:
  2563. case ATA_CMD_CFA_TRANS_SECT:
  2564. case ATA_CMD_CFA_WRITE_MULT_NE:
  2565. /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
  2566. case ATA_CMD_READ:
  2567. case ATA_CMD_READ_EXT:
  2568. case ATA_CMD_READ_QUEUED:
  2569. /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
  2570. case ATA_CMD_FPDMA_READ:
  2571. case ATA_CMD_READ_MULTI:
  2572. case ATA_CMD_READ_MULTI_EXT:
  2573. case ATA_CMD_PIO_READ:
  2574. case ATA_CMD_PIO_READ_EXT:
  2575. case ATA_CMD_READ_STREAM_DMA_EXT:
  2576. case ATA_CMD_READ_STREAM_EXT:
  2577. case ATA_CMD_VERIFY:
  2578. case ATA_CMD_VERIFY_EXT:
  2579. case ATA_CMD_WRITE:
  2580. case ATA_CMD_WRITE_EXT:
  2581. case ATA_CMD_WRITE_FUA_EXT:
  2582. case ATA_CMD_WRITE_QUEUED:
  2583. case ATA_CMD_WRITE_QUEUED_FUA_EXT:
  2584. case ATA_CMD_FPDMA_WRITE:
  2585. case ATA_CMD_WRITE_MULTI:
  2586. case ATA_CMD_WRITE_MULTI_EXT:
  2587. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  2588. case ATA_CMD_PIO_WRITE:
  2589. case ATA_CMD_PIO_WRITE_EXT:
  2590. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  2591. case ATA_CMD_WRITE_STREAM_EXT:
  2592. qc->sect_size = scmd->device->sector_size;
  2593. break;
  2594. /* Everything else uses 512 byte "sectors" */
  2595. default:
  2596. qc->sect_size = ATA_SECT_SIZE;
  2597. }
  2598. /*
  2599. * Set flags so that all registers will be written, pass on
  2600. * write indication (used for PIO/DMA setup), result TF is
  2601. * copied back and we don't whine too much about its failure.
  2602. */
  2603. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2604. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2605. tf->flags |= ATA_TFLAG_WRITE;
  2606. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2607. /*
  2608. * Set transfer length.
  2609. *
  2610. * TODO: find out if we need to do more here to
  2611. * cover scatter/gather case.
  2612. */
  2613. ata_qc_set_pc_nbytes(qc);
  2614. /* We may not issue DMA commands if no DMA mode is set */
  2615. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
  2616. goto invalid_fld;
  2617. /* sanity check for pio multi commands */
  2618. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
  2619. goto invalid_fld;
  2620. if (is_multi_taskfile(tf)) {
  2621. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2622. /* compare the passed through multi_count
  2623. * with the cached multi_count of libata
  2624. */
  2625. if (multi_count != dev->multi_count)
  2626. ata_dev_warn(dev, "invalid multi_count %u ignored\n",
  2627. multi_count);
  2628. }
  2629. /*
  2630. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2631. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2632. * by an update to hardware-specific registers for each
  2633. * controller (i.e. the reason for ->set_piomode(),
  2634. * ->set_dmamode(), and ->post_set_mode() hooks).
  2635. */
  2636. if (tf->command == ATA_CMD_SET_FEATURES &&
  2637. tf->feature == SETFEATURES_XFER)
  2638. goto invalid_fld;
  2639. /*
  2640. * Filter TPM commands by default. These provide an
  2641. * essentially uncontrolled encrypted "back door" between
  2642. * applications and the disk. Set libata.allow_tpm=1 if you
  2643. * have a real reason for wanting to use them. This ensures
  2644. * that installed software cannot easily mess stuff up without
  2645. * user intent. DVR type users will probably ship with this enabled
  2646. * for movie content management.
  2647. *
  2648. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2649. * for this and should do in future but that it is not sufficient as
  2650. * DCS is an optional feature set. Thus we also do the software filter
  2651. * so that we comply with the TC consortium stated goal that the user
  2652. * can turn off TC features of their system.
  2653. */
  2654. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
  2655. goto invalid_fld;
  2656. return 0;
  2657. invalid_fld:
  2658. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2659. /* "Invalid field in cdb" */
  2660. return 1;
  2661. }
  2662. static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
  2663. {
  2664. struct ata_taskfile *tf = &qc->tf;
  2665. struct scsi_cmnd *scmd = qc->scsicmd;
  2666. struct ata_device *dev = qc->dev;
  2667. const u8 *cdb = scmd->cmnd;
  2668. u64 block;
  2669. u32 n_block;
  2670. u32 size;
  2671. void *buf;
  2672. /* we may not issue DMA commands if no DMA mode is set */
  2673. if (unlikely(!dev->dma_mode))
  2674. goto invalid_fld;
  2675. if (unlikely(scmd->cmd_len < 16))
  2676. goto invalid_fld;
  2677. scsi_16_lba_len(cdb, &block, &n_block);
  2678. /* for now we only support WRITE SAME with the unmap bit set */
  2679. if (unlikely(!(cdb[1] & 0x8)))
  2680. goto invalid_fld;
  2681. /*
  2682. * WRITE SAME always has a sector sized buffer as payload, this
  2683. * should never be a multiple entry S/G list.
  2684. */
  2685. if (!scsi_sg_count(scmd))
  2686. goto invalid_fld;
  2687. buf = page_address(sg_page(scsi_sglist(scmd)));
  2688. size = ata_set_lba_range_entries(buf, 512, block, n_block);
  2689. tf->protocol = ATA_PROT_DMA;
  2690. tf->hob_feature = 0;
  2691. tf->feature = ATA_DSM_TRIM;
  2692. tf->hob_nsect = (size / 512) >> 8;
  2693. tf->nsect = size / 512;
  2694. tf->command = ATA_CMD_DSM;
  2695. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
  2696. ATA_TFLAG_WRITE;
  2697. ata_qc_set_pc_nbytes(qc);
  2698. return 0;
  2699. invalid_fld:
  2700. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2701. /* "Invalid field in cdb" */
  2702. return 1;
  2703. }
  2704. /**
  2705. * ata_mselect_caching - Simulate MODE SELECT for caching info page
  2706. * @qc: Storage for translated ATA taskfile
  2707. * @buf: input buffer
  2708. * @len: number of valid bytes in the input buffer
  2709. *
  2710. * Prepare a taskfile to modify caching information for the device.
  2711. *
  2712. * LOCKING:
  2713. * None.
  2714. */
  2715. static int ata_mselect_caching(struct ata_queued_cmd *qc,
  2716. const u8 *buf, int len)
  2717. {
  2718. struct ata_taskfile *tf = &qc->tf;
  2719. struct ata_device *dev = qc->dev;
  2720. char mpage[CACHE_MPAGE_LEN];
  2721. u8 wce;
  2722. /*
  2723. * The first two bytes of def_cache_mpage are a header, so offsets
  2724. * in mpage are off by 2 compared to buf. Same for len.
  2725. */
  2726. if (len != CACHE_MPAGE_LEN - 2)
  2727. return -EINVAL;
  2728. wce = buf[0] & (1 << 2);
  2729. /*
  2730. * Check that read-only bits are not modified.
  2731. */
  2732. ata_msense_caching(dev->id, mpage, false);
  2733. mpage[2] &= ~(1 << 2);
  2734. mpage[2] |= wce;
  2735. if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0)
  2736. return -EINVAL;
  2737. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  2738. tf->protocol = ATA_PROT_NODATA;
  2739. tf->nsect = 0;
  2740. tf->command = ATA_CMD_SET_FEATURES;
  2741. tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
  2742. return 0;
  2743. }
  2744. /**
  2745. * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
  2746. * @qc: Storage for translated ATA taskfile
  2747. *
  2748. * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
  2749. * Assume this is invoked for direct access devices (e.g. disks) only.
  2750. * There should be no block descriptor for other device types.
  2751. *
  2752. * LOCKING:
  2753. * spin_lock_irqsave(host lock)
  2754. */
  2755. static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
  2756. {
  2757. struct scsi_cmnd *scmd = qc->scsicmd;
  2758. const u8 *cdb = scmd->cmnd;
  2759. const u8 *p;
  2760. u8 pg, spg;
  2761. unsigned six_byte, pg_len, hdr_len, bd_len;
  2762. int len;
  2763. VPRINTK("ENTER\n");
  2764. six_byte = (cdb[0] == MODE_SELECT);
  2765. if (six_byte) {
  2766. if (scmd->cmd_len < 5)
  2767. goto invalid_fld;
  2768. len = cdb[4];
  2769. hdr_len = 4;
  2770. } else {
  2771. if (scmd->cmd_len < 9)
  2772. goto invalid_fld;
  2773. len = (cdb[7] << 8) + cdb[8];
  2774. hdr_len = 8;
  2775. }
  2776. /* We only support PF=1, SP=0. */
  2777. if ((cdb[1] & 0x11) != 0x10)
  2778. goto invalid_fld;
  2779. /* Test early for possible overrun. */
  2780. if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
  2781. goto invalid_param_len;
  2782. p = page_address(sg_page(scsi_sglist(scmd)));
  2783. /* Move past header and block descriptors. */
  2784. if (len < hdr_len)
  2785. goto invalid_param_len;
  2786. if (six_byte)
  2787. bd_len = p[3];
  2788. else
  2789. bd_len = (p[6] << 8) + p[7];
  2790. len -= hdr_len;
  2791. p += hdr_len;
  2792. if (len < bd_len)
  2793. goto invalid_param_len;
  2794. if (bd_len != 0 && bd_len != 8)
  2795. goto invalid_param;
  2796. len -= bd_len;
  2797. p += bd_len;
  2798. if (len == 0)
  2799. goto skip;
  2800. /* Parse both possible formats for the mode page headers. */
  2801. pg = p[0] & 0x3f;
  2802. if (p[0] & 0x40) {
  2803. if (len < 4)
  2804. goto invalid_param_len;
  2805. spg = p[1];
  2806. pg_len = (p[2] << 8) | p[3];
  2807. p += 4;
  2808. len -= 4;
  2809. } else {
  2810. if (len < 2)
  2811. goto invalid_param_len;
  2812. spg = 0;
  2813. pg_len = p[1];
  2814. p += 2;
  2815. len -= 2;
  2816. }
  2817. /*
  2818. * No mode subpages supported (yet) but asking for _all_
  2819. * subpages may be valid
  2820. */
  2821. if (spg && (spg != ALL_SUB_MPAGES))
  2822. goto invalid_param;
  2823. if (pg_len > len)
  2824. goto invalid_param_len;
  2825. switch (pg) {
  2826. case CACHE_MPAGE:
  2827. if (ata_mselect_caching(qc, p, pg_len) < 0)
  2828. goto invalid_param;
  2829. break;
  2830. default: /* invalid page code */
  2831. goto invalid_param;
  2832. }
  2833. /*
  2834. * Only one page has changeable data, so we only support setting one
  2835. * page at a time.
  2836. */
  2837. if (len > pg_len)
  2838. goto invalid_param;
  2839. return 0;
  2840. invalid_fld:
  2841. /* "Invalid field in CDB" */
  2842. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2843. return 1;
  2844. invalid_param:
  2845. /* "Invalid field in parameter list" */
  2846. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x26, 0x0);
  2847. return 1;
  2848. invalid_param_len:
  2849. /* "Parameter list length error" */
  2850. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  2851. return 1;
  2852. skip:
  2853. scmd->result = SAM_STAT_GOOD;
  2854. return 1;
  2855. }
  2856. /**
  2857. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  2858. * @dev: ATA device
  2859. * @cmd: SCSI command opcode to consider
  2860. *
  2861. * Look up the SCSI command given, and determine whether the
  2862. * SCSI command is to be translated or simulated.
  2863. *
  2864. * RETURNS:
  2865. * Pointer to translation function if possible, %NULL if not.
  2866. */
  2867. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2868. {
  2869. switch (cmd) {
  2870. case READ_6:
  2871. case READ_10:
  2872. case READ_16:
  2873. case WRITE_6:
  2874. case WRITE_10:
  2875. case WRITE_16:
  2876. return ata_scsi_rw_xlat;
  2877. case WRITE_SAME_16:
  2878. return ata_scsi_write_same_xlat;
  2879. case SYNCHRONIZE_CACHE:
  2880. if (ata_try_flush_cache(dev))
  2881. return ata_scsi_flush_xlat;
  2882. break;
  2883. case VERIFY:
  2884. case VERIFY_16:
  2885. return ata_scsi_verify_xlat;
  2886. case ATA_12:
  2887. case ATA_16:
  2888. return ata_scsi_pass_thru;
  2889. case MODE_SELECT:
  2890. case MODE_SELECT_10:
  2891. return ata_scsi_mode_select_xlat;
  2892. break;
  2893. case START_STOP:
  2894. return ata_scsi_start_stop_xlat;
  2895. }
  2896. return NULL;
  2897. }
  2898. /**
  2899. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2900. * @ap: ATA port to which the command was being sent
  2901. * @cmd: SCSI command to dump
  2902. *
  2903. * Prints the contents of a SCSI command via printk().
  2904. */
  2905. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2906. struct scsi_cmnd *cmd)
  2907. {
  2908. #ifdef ATA_DEBUG
  2909. struct scsi_device *scsidev = cmd->device;
  2910. u8 *scsicmd = cmd->cmnd;
  2911. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2912. ap->print_id,
  2913. scsidev->channel, scsidev->id, scsidev->lun,
  2914. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2915. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2916. scsicmd[8]);
  2917. #endif
  2918. }
  2919. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
  2920. struct ata_device *dev)
  2921. {
  2922. u8 scsi_op = scmd->cmnd[0];
  2923. ata_xlat_func_t xlat_func;
  2924. int rc = 0;
  2925. if (dev->class == ATA_DEV_ATA) {
  2926. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  2927. goto bad_cdb_len;
  2928. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2929. } else {
  2930. if (unlikely(!scmd->cmd_len))
  2931. goto bad_cdb_len;
  2932. xlat_func = NULL;
  2933. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  2934. /* relay SCSI command to ATAPI device */
  2935. int len = COMMAND_SIZE(scsi_op);
  2936. if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
  2937. goto bad_cdb_len;
  2938. xlat_func = atapi_xlat;
  2939. } else {
  2940. /* ATA_16 passthru, treat as an ATA command */
  2941. if (unlikely(scmd->cmd_len > 16))
  2942. goto bad_cdb_len;
  2943. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2944. }
  2945. }
  2946. if (xlat_func)
  2947. rc = ata_scsi_translate(dev, scmd, xlat_func);
  2948. else
  2949. ata_scsi_simulate(dev, scmd);
  2950. return rc;
  2951. bad_cdb_len:
  2952. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  2953. scmd->cmd_len, scsi_op, dev->cdb_len);
  2954. scmd->result = DID_ERROR << 16;
  2955. scmd->scsi_done(scmd);
  2956. return 0;
  2957. }
  2958. /**
  2959. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  2960. * @shost: SCSI host of command to be sent
  2961. * @cmd: SCSI command to be sent
  2962. *
  2963. * In some cases, this function translates SCSI commands into
  2964. * ATA taskfiles, and queues the taskfiles to be sent to
  2965. * hardware. In other cases, this function simulates a
  2966. * SCSI device by evaluating and responding to certain
  2967. * SCSI commands. This creates the overall effect of
  2968. * ATA and ATAPI devices appearing as SCSI devices.
  2969. *
  2970. * LOCKING:
  2971. * ATA host lock
  2972. *
  2973. * RETURNS:
  2974. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  2975. * 0 otherwise.
  2976. */
  2977. int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  2978. {
  2979. struct ata_port *ap;
  2980. struct ata_device *dev;
  2981. struct scsi_device *scsidev = cmd->device;
  2982. int rc = 0;
  2983. unsigned long irq_flags;
  2984. ap = ata_shost_to_port(shost);
  2985. spin_lock_irqsave(ap->lock, irq_flags);
  2986. ata_scsi_dump_cdb(ap, cmd);
  2987. dev = ata_scsi_find_dev(ap, scsidev);
  2988. if (likely(dev))
  2989. rc = __ata_scsi_queuecmd(cmd, dev);
  2990. else {
  2991. cmd->result = (DID_BAD_TARGET << 16);
  2992. cmd->scsi_done(cmd);
  2993. }
  2994. spin_unlock_irqrestore(ap->lock, irq_flags);
  2995. return rc;
  2996. }
  2997. /**
  2998. * ata_scsi_simulate - simulate SCSI command on ATA device
  2999. * @dev: the target device
  3000. * @cmd: SCSI command being sent to device.
  3001. *
  3002. * Interprets and directly executes a select list of SCSI commands
  3003. * that can be handled internally.
  3004. *
  3005. * LOCKING:
  3006. * spin_lock_irqsave(host lock)
  3007. */
  3008. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
  3009. {
  3010. struct ata_scsi_args args;
  3011. const u8 *scsicmd = cmd->cmnd;
  3012. u8 tmp8;
  3013. args.dev = dev;
  3014. args.id = dev->id;
  3015. args.cmd = cmd;
  3016. args.done = cmd->scsi_done;
  3017. switch(scsicmd[0]) {
  3018. /* TODO: worth improving? */
  3019. case FORMAT_UNIT:
  3020. ata_scsi_invalid_field(cmd);
  3021. break;
  3022. case INQUIRY:
  3023. if (scsicmd[1] & 2) /* is CmdDt set? */
  3024. ata_scsi_invalid_field(cmd);
  3025. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  3026. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  3027. else switch (scsicmd[2]) {
  3028. case 0x00:
  3029. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  3030. break;
  3031. case 0x80:
  3032. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  3033. break;
  3034. case 0x83:
  3035. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  3036. break;
  3037. case 0x89:
  3038. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  3039. break;
  3040. case 0xb0:
  3041. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
  3042. break;
  3043. case 0xb1:
  3044. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  3045. break;
  3046. case 0xb2:
  3047. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
  3048. break;
  3049. default:
  3050. ata_scsi_invalid_field(cmd);
  3051. break;
  3052. }
  3053. break;
  3054. case MODE_SENSE:
  3055. case MODE_SENSE_10:
  3056. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  3057. break;
  3058. case READ_CAPACITY:
  3059. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3060. break;
  3061. case SERVICE_ACTION_IN:
  3062. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  3063. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3064. else
  3065. ata_scsi_invalid_field(cmd);
  3066. break;
  3067. case REPORT_LUNS:
  3068. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  3069. break;
  3070. case REQUEST_SENSE:
  3071. ata_scsi_set_sense(cmd, 0, 0, 0);
  3072. cmd->result = (DRIVER_SENSE << 24);
  3073. cmd->scsi_done(cmd);
  3074. break;
  3075. /* if we reach this, then writeback caching is disabled,
  3076. * turning this into a no-op.
  3077. */
  3078. case SYNCHRONIZE_CACHE:
  3079. /* fall through */
  3080. /* no-op's, complete with success */
  3081. case REZERO_UNIT:
  3082. case SEEK_6:
  3083. case SEEK_10:
  3084. case TEST_UNIT_READY:
  3085. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  3086. break;
  3087. case SEND_DIAGNOSTIC:
  3088. tmp8 = scsicmd[1] & ~(1 << 3);
  3089. if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
  3090. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  3091. else
  3092. ata_scsi_invalid_field(cmd);
  3093. break;
  3094. /* all other commands */
  3095. default:
  3096. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  3097. /* "Invalid command operation code" */
  3098. cmd->scsi_done(cmd);
  3099. break;
  3100. }
  3101. }
  3102. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  3103. {
  3104. int i, rc;
  3105. for (i = 0; i < host->n_ports; i++) {
  3106. struct ata_port *ap = host->ports[i];
  3107. struct Scsi_Host *shost;
  3108. rc = -ENOMEM;
  3109. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  3110. if (!shost)
  3111. goto err_alloc;
  3112. shost->eh_noresume = 1;
  3113. *(struct ata_port **)&shost->hostdata[0] = ap;
  3114. ap->scsi_host = shost;
  3115. shost->transportt = ata_scsi_transport_template;
  3116. shost->unique_id = ap->print_id;
  3117. shost->max_id = 16;
  3118. shost->max_lun = 1;
  3119. shost->max_channel = 1;
  3120. shost->max_cmd_len = 16;
  3121. /* Schedule policy is determined by ->qc_defer()
  3122. * callback and it needs to see every deferred qc.
  3123. * Set host_blocked to 1 to prevent SCSI midlayer from
  3124. * automatically deferring requests.
  3125. */
  3126. shost->max_host_blocked = 1;
  3127. rc = scsi_add_host_with_dma(ap->scsi_host,
  3128. &ap->tdev, ap->host->dev);
  3129. if (rc)
  3130. goto err_add;
  3131. }
  3132. return 0;
  3133. err_add:
  3134. scsi_host_put(host->ports[i]->scsi_host);
  3135. err_alloc:
  3136. while (--i >= 0) {
  3137. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  3138. scsi_remove_host(shost);
  3139. scsi_host_put(shost);
  3140. }
  3141. return rc;
  3142. }
  3143. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  3144. {
  3145. int tries = 5;
  3146. struct ata_device *last_failed_dev = NULL;
  3147. struct ata_link *link;
  3148. struct ata_device *dev;
  3149. repeat:
  3150. ata_for_each_link(link, ap, EDGE) {
  3151. ata_for_each_dev(dev, link, ENABLED) {
  3152. struct scsi_device *sdev;
  3153. int channel = 0, id = 0;
  3154. if (dev->sdev)
  3155. continue;
  3156. if (ata_is_host_link(link))
  3157. id = dev->devno;
  3158. else
  3159. channel = link->pmp;
  3160. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  3161. NULL);
  3162. if (!IS_ERR(sdev)) {
  3163. dev->sdev = sdev;
  3164. scsi_device_put(sdev);
  3165. if (zpodd_dev_enabled(dev))
  3166. dev_pm_qos_expose_flags(
  3167. &sdev->sdev_gendev, 0);
  3168. } else {
  3169. dev->sdev = NULL;
  3170. }
  3171. }
  3172. }
  3173. /* If we scanned while EH was in progress or allocation
  3174. * failure occurred, scan would have failed silently. Check
  3175. * whether all devices are attached.
  3176. */
  3177. ata_for_each_link(link, ap, EDGE) {
  3178. ata_for_each_dev(dev, link, ENABLED) {
  3179. if (!dev->sdev)
  3180. goto exit_loop;
  3181. }
  3182. }
  3183. exit_loop:
  3184. if (!link)
  3185. return;
  3186. /* we're missing some SCSI devices */
  3187. if (sync) {
  3188. /* If caller requested synchrnous scan && we've made
  3189. * any progress, sleep briefly and repeat.
  3190. */
  3191. if (dev != last_failed_dev) {
  3192. msleep(100);
  3193. last_failed_dev = dev;
  3194. goto repeat;
  3195. }
  3196. /* We might be failing to detect boot device, give it
  3197. * a few more chances.
  3198. */
  3199. if (--tries) {
  3200. msleep(100);
  3201. goto repeat;
  3202. }
  3203. ata_port_err(ap,
  3204. "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
  3205. }
  3206. queue_delayed_work(system_long_wq, &ap->hotplug_task,
  3207. round_jiffies_relative(HZ));
  3208. }
  3209. /**
  3210. * ata_scsi_offline_dev - offline attached SCSI device
  3211. * @dev: ATA device to offline attached SCSI device for
  3212. *
  3213. * This function is called from ata_eh_hotplug() and responsible
  3214. * for taking the SCSI device attached to @dev offline. This
  3215. * function is called with host lock which protects dev->sdev
  3216. * against clearing.
  3217. *
  3218. * LOCKING:
  3219. * spin_lock_irqsave(host lock)
  3220. *
  3221. * RETURNS:
  3222. * 1 if attached SCSI device exists, 0 otherwise.
  3223. */
  3224. int ata_scsi_offline_dev(struct ata_device *dev)
  3225. {
  3226. if (dev->sdev) {
  3227. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  3228. return 1;
  3229. }
  3230. return 0;
  3231. }
  3232. /**
  3233. * ata_scsi_remove_dev - remove attached SCSI device
  3234. * @dev: ATA device to remove attached SCSI device for
  3235. *
  3236. * This function is called from ata_eh_scsi_hotplug() and
  3237. * responsible for removing the SCSI device attached to @dev.
  3238. *
  3239. * LOCKING:
  3240. * Kernel thread context (may sleep).
  3241. */
  3242. static void ata_scsi_remove_dev(struct ata_device *dev)
  3243. {
  3244. struct ata_port *ap = dev->link->ap;
  3245. struct scsi_device *sdev;
  3246. unsigned long flags;
  3247. /* Alas, we need to grab scan_mutex to ensure SCSI device
  3248. * state doesn't change underneath us and thus
  3249. * scsi_device_get() always succeeds. The mutex locking can
  3250. * be removed if there is __scsi_device_get() interface which
  3251. * increments reference counts regardless of device state.
  3252. */
  3253. mutex_lock(&ap->scsi_host->scan_mutex);
  3254. spin_lock_irqsave(ap->lock, flags);
  3255. if (zpodd_dev_enabled(dev))
  3256. zpodd_exit(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);