libata-scsi.c 99 KB

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