libata-scsi.c 98 KB

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