libata-scsi.c 83 KB

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