libata-scsi.c 84 KB

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