libata-scsi.c 84 KB

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