libata-scsi.c 84 KB

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