libata-scsi.c 97 KB

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