libata-scsi.c 99 KB

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