libata-scsi.c 92 KB

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