libata-scsi.c 84 KB

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