libata-scsi.c 98 KB

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