libata-scsi.c 93 KB

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