libata-scsi.c 76 KB

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