libata-scsi.c 88 KB

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