libata-scsi.c 91 KB

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