libata-scsi.c 89 KB

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