libata-scsi.c 94 KB

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