libata-scsi.c 93 KB

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