libata-scsi.c 84 KB

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