libata-scsi.c 92 KB

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