libata-scsi.c 94 KB

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