libata-scsi.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298
  1. /*
  2. * libata-scsi.c - helper library for ATA
  3. *
  4. * Maintained by: Tejun Heo <tj@kernel.org>
  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/slab.h>
  36. #include <linux/kernel.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/export.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_eh.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <scsi/scsi_transport.h>
  47. #include <linux/libata.h>
  48. #include <linux/hdreg.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/suspend.h>
  51. #include <asm/unaligned.h>
  52. #include "libata.h"
  53. #include "libata-transport.h"
  54. #define ATA_SCSI_RBUF_SIZE 4096
  55. static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
  56. static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
  57. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
  58. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  59. const struct scsi_device *scsidev);
  60. static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
  61. const struct scsi_device *scsidev);
  62. #define RW_RECOVERY_MPAGE 0x1
  63. #define RW_RECOVERY_MPAGE_LEN 12
  64. #define CACHE_MPAGE 0x8
  65. #define CACHE_MPAGE_LEN 20
  66. #define CONTROL_MPAGE 0xa
  67. #define CONTROL_MPAGE_LEN 12
  68. #define ALL_MPAGES 0x3f
  69. #define ALL_SUB_MPAGES 0xff
  70. static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
  71. RW_RECOVERY_MPAGE,
  72. RW_RECOVERY_MPAGE_LEN - 2,
  73. (1 << 7), /* AWRE */
  74. 0, /* read retry count */
  75. 0, 0, 0, 0,
  76. 0, /* write retry count */
  77. 0, 0, 0
  78. };
  79. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  80. CACHE_MPAGE,
  81. CACHE_MPAGE_LEN - 2,
  82. 0, /* contains WCE, needs to be 0 for logic */
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, /* contains DRA, needs to be 0 for logic */
  85. 0, 0, 0, 0, 0, 0, 0
  86. };
  87. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  88. CONTROL_MPAGE,
  89. CONTROL_MPAGE_LEN - 2,
  90. 2, /* DSENSE=0, GLTSD=1 */
  91. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  92. 0, 0, 0, 0, 0xff, 0xff,
  93. 0, 30 /* extended self test time, see 05-359r1 */
  94. };
  95. static const char *ata_lpm_policy_names[] = {
  96. [ATA_LPM_UNKNOWN] = "max_performance",
  97. [ATA_LPM_MAX_POWER] = "max_performance",
  98. [ATA_LPM_MED_POWER] = "medium_power",
  99. [ATA_LPM_MIN_POWER] = "min_power",
  100. };
  101. static ssize_t ata_scsi_lpm_store(struct device *device,
  102. struct device_attribute *attr,
  103. const char *buf, size_t count)
  104. {
  105. struct Scsi_Host *shost = class_to_shost(device);
  106. struct ata_port *ap = ata_shost_to_port(shost);
  107. struct ata_link *link;
  108. struct ata_device *dev;
  109. enum ata_lpm_policy policy;
  110. unsigned long flags;
  111. /* UNKNOWN is internal state, iterate from MAX_POWER */
  112. for (policy = ATA_LPM_MAX_POWER;
  113. policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
  114. const char *name = ata_lpm_policy_names[policy];
  115. if (strncmp(name, buf, strlen(name)) == 0)
  116. break;
  117. }
  118. if (policy == ARRAY_SIZE(ata_lpm_policy_names))
  119. return -EINVAL;
  120. spin_lock_irqsave(ap->lock, flags);
  121. ata_for_each_link(link, ap, EDGE) {
  122. ata_for_each_dev(dev, &ap->link, ENABLED) {
  123. if (dev->horkage & ATA_HORKAGE_NOLPM) {
  124. count = -EOPNOTSUPP;
  125. goto out_unlock;
  126. }
  127. }
  128. }
  129. ap->target_lpm_policy = policy;
  130. ata_port_schedule_eh(ap);
  131. out_unlock:
  132. spin_unlock_irqrestore(ap->lock, flags);
  133. return count;
  134. }
  135. static ssize_t ata_scsi_lpm_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. struct Scsi_Host *shost = class_to_shost(dev);
  139. struct ata_port *ap = ata_shost_to_port(shost);
  140. if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
  141. return -EINVAL;
  142. return snprintf(buf, PAGE_SIZE, "%s\n",
  143. ata_lpm_policy_names[ap->target_lpm_policy]);
  144. }
  145. DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
  146. ata_scsi_lpm_show, ata_scsi_lpm_store);
  147. EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
  148. static ssize_t ata_scsi_park_show(struct device *device,
  149. struct device_attribute *attr, char *buf)
  150. {
  151. struct scsi_device *sdev = to_scsi_device(device);
  152. struct ata_port *ap;
  153. struct ata_link *link;
  154. struct ata_device *dev;
  155. unsigned long flags, now;
  156. unsigned int uninitialized_var(msecs);
  157. int rc = 0;
  158. ap = ata_shost_to_port(sdev->host);
  159. spin_lock_irqsave(ap->lock, flags);
  160. dev = ata_scsi_find_dev(ap, sdev);
  161. if (!dev) {
  162. rc = -ENODEV;
  163. goto unlock;
  164. }
  165. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  166. rc = -EOPNOTSUPP;
  167. goto unlock;
  168. }
  169. link = dev->link;
  170. now = jiffies;
  171. if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
  172. link->eh_context.unloaded_mask & (1 << dev->devno) &&
  173. time_after(dev->unpark_deadline, now))
  174. msecs = jiffies_to_msecs(dev->unpark_deadline - now);
  175. else
  176. msecs = 0;
  177. unlock:
  178. spin_unlock_irq(ap->lock);
  179. return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
  180. }
  181. static ssize_t ata_scsi_park_store(struct device *device,
  182. struct device_attribute *attr,
  183. const char *buf, size_t len)
  184. {
  185. struct scsi_device *sdev = to_scsi_device(device);
  186. struct ata_port *ap;
  187. struct ata_device *dev;
  188. long int input;
  189. unsigned long flags;
  190. int rc;
  191. rc = kstrtol(buf, 10, &input);
  192. if (rc)
  193. return rc;
  194. if (input < -2)
  195. return -EINVAL;
  196. if (input > ATA_TMOUT_MAX_PARK) {
  197. rc = -EOVERFLOW;
  198. input = ATA_TMOUT_MAX_PARK;
  199. }
  200. ap = ata_shost_to_port(sdev->host);
  201. spin_lock_irqsave(ap->lock, flags);
  202. dev = ata_scsi_find_dev(ap, sdev);
  203. if (unlikely(!dev)) {
  204. rc = -ENODEV;
  205. goto unlock;
  206. }
  207. if (dev->class != ATA_DEV_ATA &&
  208. dev->class != ATA_DEV_ZAC) {
  209. rc = -EOPNOTSUPP;
  210. goto unlock;
  211. }
  212. if (input >= 0) {
  213. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  214. rc = -EOPNOTSUPP;
  215. goto unlock;
  216. }
  217. dev->unpark_deadline = ata_deadline(jiffies, input);
  218. dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
  219. ata_port_schedule_eh(ap);
  220. complete(&ap->park_req_pending);
  221. } else {
  222. switch (input) {
  223. case -1:
  224. dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
  225. break;
  226. case -2:
  227. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  228. break;
  229. }
  230. }
  231. unlock:
  232. spin_unlock_irqrestore(ap->lock, flags);
  233. return rc ? rc : len;
  234. }
  235. DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
  236. ata_scsi_park_show, ata_scsi_park_store);
  237. EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
  238. void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  239. {
  240. if (!cmd)
  241. return;
  242. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  243. scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
  244. }
  245. void ata_scsi_set_sense_information(struct scsi_cmnd *cmd,
  246. const struct ata_taskfile *tf)
  247. {
  248. u64 information;
  249. if (!cmd)
  250. return;
  251. information = ata_tf_read_block(tf, NULL);
  252. scsi_set_sense_information(cmd->sense_buffer, information);
  253. }
  254. static ssize_t
  255. ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
  256. const char *buf, size_t count)
  257. {
  258. struct Scsi_Host *shost = class_to_shost(dev);
  259. struct ata_port *ap = ata_shost_to_port(shost);
  260. if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
  261. return ap->ops->em_store(ap, buf, count);
  262. return -EINVAL;
  263. }
  264. static ssize_t
  265. ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
  266. char *buf)
  267. {
  268. struct Scsi_Host *shost = class_to_shost(dev);
  269. struct ata_port *ap = ata_shost_to_port(shost);
  270. if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
  271. return ap->ops->em_show(ap, buf);
  272. return -EINVAL;
  273. }
  274. DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
  275. ata_scsi_em_message_show, ata_scsi_em_message_store);
  276. EXPORT_SYMBOL_GPL(dev_attr_em_message);
  277. static ssize_t
  278. ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
  279. char *buf)
  280. {
  281. struct Scsi_Host *shost = class_to_shost(dev);
  282. struct ata_port *ap = ata_shost_to_port(shost);
  283. return snprintf(buf, 23, "%d\n", ap->em_message_type);
  284. }
  285. DEVICE_ATTR(em_message_type, S_IRUGO,
  286. ata_scsi_em_message_type_show, NULL);
  287. EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
  288. static ssize_t
  289. ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
  290. char *buf)
  291. {
  292. struct scsi_device *sdev = to_scsi_device(dev);
  293. struct ata_port *ap = ata_shost_to_port(sdev->host);
  294. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  295. if (atadev && ap->ops->sw_activity_show &&
  296. (ap->flags & ATA_FLAG_SW_ACTIVITY))
  297. return ap->ops->sw_activity_show(atadev, buf);
  298. return -EINVAL;
  299. }
  300. static ssize_t
  301. ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
  302. const char *buf, size_t count)
  303. {
  304. struct scsi_device *sdev = to_scsi_device(dev);
  305. struct ata_port *ap = ata_shost_to_port(sdev->host);
  306. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  307. enum sw_activity val;
  308. int rc;
  309. if (atadev && ap->ops->sw_activity_store &&
  310. (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
  311. val = simple_strtoul(buf, NULL, 0);
  312. switch (val) {
  313. case OFF: case BLINK_ON: case BLINK_OFF:
  314. rc = ap->ops->sw_activity_store(atadev, val);
  315. if (!rc)
  316. return count;
  317. else
  318. return rc;
  319. }
  320. }
  321. return -EINVAL;
  322. }
  323. DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
  324. ata_scsi_activity_store);
  325. EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
  326. struct device_attribute *ata_common_sdev_attrs[] = {
  327. &dev_attr_unload_heads,
  328. NULL
  329. };
  330. EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
  331. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd)
  332. {
  333. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  334. /* "Invalid field in cbd" */
  335. cmd->scsi_done(cmd);
  336. }
  337. /**
  338. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  339. * @sdev: SCSI device for which BIOS geometry is to be determined
  340. * @bdev: block device associated with @sdev
  341. * @capacity: capacity of SCSI device
  342. * @geom: location to which geometry will be output
  343. *
  344. * Generic bios head/sector/cylinder calculator
  345. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  346. * mapping. Some situations may arise where the disk is not
  347. * bootable if this is not used.
  348. *
  349. * LOCKING:
  350. * Defined by the SCSI layer. We don't really care.
  351. *
  352. * RETURNS:
  353. * Zero.
  354. */
  355. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  356. sector_t capacity, int geom[])
  357. {
  358. geom[0] = 255;
  359. geom[1] = 63;
  360. sector_div(capacity, 255*63);
  361. geom[2] = capacity;
  362. return 0;
  363. }
  364. /**
  365. * ata_scsi_unlock_native_capacity - unlock native capacity
  366. * @sdev: SCSI device to adjust device capacity for
  367. *
  368. * This function is called if a partition on @sdev extends beyond
  369. * the end of the device. It requests EH to unlock HPA.
  370. *
  371. * LOCKING:
  372. * Defined by the SCSI layer. Might sleep.
  373. */
  374. void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
  375. {
  376. struct ata_port *ap = ata_shost_to_port(sdev->host);
  377. struct ata_device *dev;
  378. unsigned long flags;
  379. spin_lock_irqsave(ap->lock, flags);
  380. dev = ata_scsi_find_dev(ap, sdev);
  381. if (dev && dev->n_sectors < dev->n_native_sectors) {
  382. dev->flags |= ATA_DFLAG_UNLOCK_HPA;
  383. dev->link->eh_info.action |= ATA_EH_RESET;
  384. ata_port_schedule_eh(ap);
  385. }
  386. spin_unlock_irqrestore(ap->lock, flags);
  387. ata_port_wait_eh(ap);
  388. }
  389. /**
  390. * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
  391. * @ap: target port
  392. * @sdev: SCSI device to get identify data for
  393. * @arg: User buffer area for identify data
  394. *
  395. * LOCKING:
  396. * Defined by the SCSI layer. We don't really care.
  397. *
  398. * RETURNS:
  399. * Zero on success, negative errno on error.
  400. */
  401. static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
  402. void __user *arg)
  403. {
  404. struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
  405. u16 __user *dst = arg;
  406. char buf[40];
  407. if (!dev)
  408. return -ENOMSG;
  409. if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
  410. return -EFAULT;
  411. ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
  412. if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
  413. return -EFAULT;
  414. ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
  415. if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
  416. return -EFAULT;
  417. ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  418. if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
  419. return -EFAULT;
  420. return 0;
  421. }
  422. /**
  423. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  424. * @scsidev: Device to which we are issuing command
  425. * @arg: User provided data for issuing command
  426. *
  427. * LOCKING:
  428. * Defined by the SCSI layer. We don't really care.
  429. *
  430. * RETURNS:
  431. * Zero on success, negative errno on error.
  432. */
  433. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  434. {
  435. int rc = 0;
  436. u8 scsi_cmd[MAX_COMMAND_SIZE];
  437. u8 args[4], *argbuf = NULL, *sensebuf = NULL;
  438. int argsize = 0;
  439. enum dma_data_direction data_dir;
  440. int cmd_result;
  441. if (arg == NULL)
  442. return -EINVAL;
  443. if (copy_from_user(args, arg, sizeof(args)))
  444. return -EFAULT;
  445. sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  446. if (!sensebuf)
  447. return -ENOMEM;
  448. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  449. if (args[3]) {
  450. argsize = ATA_SECT_SIZE * args[3];
  451. argbuf = kmalloc(argsize, GFP_KERNEL);
  452. if (argbuf == NULL) {
  453. rc = -ENOMEM;
  454. goto error;
  455. }
  456. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  457. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  458. block count in sector count field */
  459. data_dir = DMA_FROM_DEVICE;
  460. } else {
  461. scsi_cmd[1] = (3 << 1); /* Non-data */
  462. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  463. data_dir = DMA_NONE;
  464. }
  465. scsi_cmd[0] = ATA_16;
  466. scsi_cmd[4] = args[2];
  467. if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
  468. scsi_cmd[6] = args[3];
  469. scsi_cmd[8] = args[1];
  470. scsi_cmd[10] = 0x4f;
  471. scsi_cmd[12] = 0xc2;
  472. } else {
  473. scsi_cmd[6] = args[1];
  474. }
  475. scsi_cmd[14] = args[0];
  476. /* Good values for timeout and retries? Values below
  477. from scsi_ioctl_send_command() for default case... */
  478. cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
  479. sensebuf, (10*HZ), 5, 0, NULL);
  480. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  481. u8 *desc = sensebuf + 8;
  482. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  483. /* If we set cc then ATA pass-through will cause a
  484. * check condition even if no error. Filter that. */
  485. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  486. struct scsi_sense_hdr sshdr;
  487. scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
  488. &sshdr);
  489. if (sshdr.sense_key == RECOVERED_ERROR &&
  490. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  491. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  492. }
  493. /* Send userspace a few ATA registers (same as drivers/ide) */
  494. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  495. desc[0] == 0x09) { /* code is "ATA Descriptor" */
  496. args[0] = desc[13]; /* status */
  497. args[1] = desc[3]; /* error */
  498. args[2] = desc[5]; /* sector count (0:7) */
  499. if (copy_to_user(arg, args, sizeof(args)))
  500. rc = -EFAULT;
  501. }
  502. }
  503. if (cmd_result) {
  504. rc = -EIO;
  505. goto error;
  506. }
  507. if ((argbuf)
  508. && copy_to_user(arg + sizeof(args), argbuf, argsize))
  509. rc = -EFAULT;
  510. error:
  511. kfree(sensebuf);
  512. kfree(argbuf);
  513. return rc;
  514. }
  515. /**
  516. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  517. * @scsidev: Device to which we are issuing command
  518. * @arg: User provided data for issuing command
  519. *
  520. * LOCKING:
  521. * Defined by the SCSI layer. We don't really care.
  522. *
  523. * RETURNS:
  524. * Zero on success, negative errno on error.
  525. */
  526. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  527. {
  528. int rc = 0;
  529. u8 scsi_cmd[MAX_COMMAND_SIZE];
  530. u8 args[7], *sensebuf = NULL;
  531. int cmd_result;
  532. if (arg == NULL)
  533. return -EINVAL;
  534. if (copy_from_user(args, arg, sizeof(args)))
  535. return -EFAULT;
  536. sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  537. if (!sensebuf)
  538. return -ENOMEM;
  539. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  540. scsi_cmd[0] = ATA_16;
  541. scsi_cmd[1] = (3 << 1); /* Non-data */
  542. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  543. scsi_cmd[4] = args[1];
  544. scsi_cmd[6] = args[2];
  545. scsi_cmd[8] = args[3];
  546. scsi_cmd[10] = args[4];
  547. scsi_cmd[12] = args[5];
  548. scsi_cmd[13] = args[6] & 0x4f;
  549. scsi_cmd[14] = args[0];
  550. /* Good values for timeout and retries? Values below
  551. from scsi_ioctl_send_command() for default case... */
  552. cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
  553. sensebuf, (10*HZ), 5, 0, NULL);
  554. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  555. u8 *desc = sensebuf + 8;
  556. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  557. /* If we set cc then ATA pass-through will cause a
  558. * check condition even if no error. Filter that. */
  559. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  560. struct scsi_sense_hdr sshdr;
  561. scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
  562. &sshdr);
  563. if (sshdr.sense_key == RECOVERED_ERROR &&
  564. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  565. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  566. }
  567. /* Send userspace ATA registers */
  568. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  569. desc[0] == 0x09) {/* code is "ATA Descriptor" */
  570. args[0] = desc[13]; /* status */
  571. args[1] = desc[3]; /* error */
  572. args[2] = desc[5]; /* sector count (0:7) */
  573. args[3] = desc[7]; /* lbal */
  574. args[4] = desc[9]; /* lbam */
  575. args[5] = desc[11]; /* lbah */
  576. args[6] = desc[12]; /* select */
  577. if (copy_to_user(arg, args, sizeof(args)))
  578. rc = -EFAULT;
  579. }
  580. }
  581. if (cmd_result) {
  582. rc = -EIO;
  583. goto error;
  584. }
  585. error:
  586. kfree(sensebuf);
  587. return rc;
  588. }
  589. static int ata_ioc32(struct ata_port *ap)
  590. {
  591. if (ap->flags & ATA_FLAG_PIO_DMA)
  592. return 1;
  593. if (ap->pflags & ATA_PFLAG_PIO32)
  594. return 1;
  595. return 0;
  596. }
  597. int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
  598. int cmd, void __user *arg)
  599. {
  600. int val = -EINVAL, rc = -EINVAL;
  601. unsigned long flags;
  602. switch (cmd) {
  603. case ATA_IOC_GET_IO32:
  604. spin_lock_irqsave(ap->lock, flags);
  605. val = ata_ioc32(ap);
  606. spin_unlock_irqrestore(ap->lock, flags);
  607. if (copy_to_user(arg, &val, 1))
  608. return -EFAULT;
  609. return 0;
  610. case ATA_IOC_SET_IO32:
  611. val = (unsigned long) arg;
  612. rc = 0;
  613. spin_lock_irqsave(ap->lock, flags);
  614. if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
  615. if (val)
  616. ap->pflags |= ATA_PFLAG_PIO32;
  617. else
  618. ap->pflags &= ~ATA_PFLAG_PIO32;
  619. } else {
  620. if (val != ata_ioc32(ap))
  621. rc = -EINVAL;
  622. }
  623. spin_unlock_irqrestore(ap->lock, flags);
  624. return rc;
  625. case HDIO_GET_IDENTITY:
  626. return ata_get_identity(ap, scsidev, arg);
  627. case HDIO_DRIVE_CMD:
  628. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  629. return -EACCES;
  630. return ata_cmd_ioctl(scsidev, arg);
  631. case HDIO_DRIVE_TASK:
  632. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  633. return -EACCES;
  634. return ata_task_ioctl(scsidev, arg);
  635. default:
  636. rc = -ENOTTY;
  637. break;
  638. }
  639. return rc;
  640. }
  641. EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
  642. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  643. {
  644. return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
  645. scsidev, cmd, arg);
  646. }
  647. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  648. /**
  649. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  650. * @dev: ATA device to which the new command is attached
  651. * @cmd: SCSI command that originated this ATA command
  652. *
  653. * Obtain a reference to an unused ata_queued_cmd structure,
  654. * which is the basic libata structure representing a single
  655. * ATA command sent to the hardware.
  656. *
  657. * If a command was available, fill in the SCSI-specific
  658. * portions of the structure with information on the
  659. * current command.
  660. *
  661. * LOCKING:
  662. * spin_lock_irqsave(host lock)
  663. *
  664. * RETURNS:
  665. * Command allocated, or %NULL if none available.
  666. */
  667. static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
  668. struct scsi_cmnd *cmd)
  669. {
  670. struct ata_queued_cmd *qc;
  671. qc = ata_qc_new_init(dev, cmd->request->tag);
  672. if (qc) {
  673. qc->scsicmd = cmd;
  674. qc->scsidone = cmd->scsi_done;
  675. qc->sg = scsi_sglist(cmd);
  676. qc->n_elem = scsi_sg_count(cmd);
  677. } else {
  678. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  679. cmd->scsi_done(cmd);
  680. }
  681. return qc;
  682. }
  683. static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
  684. {
  685. struct scsi_cmnd *scmd = qc->scsicmd;
  686. qc->extrabytes = scmd->request->extra_len;
  687. qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
  688. }
  689. /**
  690. * ata_dump_status - user friendly display of error info
  691. * @id: id of the port in question
  692. * @tf: ptr to filled out taskfile
  693. *
  694. * Decode and dump the ATA error/status registers for the user so
  695. * that they have some idea what really happened at the non
  696. * make-believe layer.
  697. *
  698. * LOCKING:
  699. * inherited from caller
  700. */
  701. static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  702. {
  703. u8 stat = tf->command, err = tf->feature;
  704. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  705. if (stat & ATA_BUSY) {
  706. printk("Busy }\n"); /* Data is not valid in this case */
  707. } else {
  708. if (stat & ATA_DRDY) printk("DriveReady ");
  709. if (stat & ATA_DF) printk("DeviceFault ");
  710. if (stat & ATA_DSC) printk("SeekComplete ");
  711. if (stat & ATA_DRQ) printk("DataRequest ");
  712. if (stat & ATA_CORR) printk("CorrectedError ");
  713. if (stat & ATA_SENSE) printk("Sense ");
  714. if (stat & ATA_ERR) printk("Error ");
  715. printk("}\n");
  716. if (err) {
  717. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  718. if (err & ATA_ABORTED) printk("DriveStatusError ");
  719. if (err & ATA_ICRC) {
  720. if (err & ATA_ABORTED)
  721. printk("BadCRC ");
  722. else printk("Sector ");
  723. }
  724. if (err & ATA_UNC) printk("UncorrectableError ");
  725. if (err & ATA_IDNF) printk("SectorIdNotFound ");
  726. if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
  727. if (err & ATA_AMNF) printk("AddrMarkNotFound ");
  728. printk("}\n");
  729. }
  730. }
  731. }
  732. /**
  733. * ata_to_sense_error - convert ATA error to SCSI error
  734. * @id: ATA device number
  735. * @drv_stat: value contained in ATA status register
  736. * @drv_err: value contained in ATA error register
  737. * @sk: the sense key we'll fill out
  738. * @asc: the additional sense code we'll fill out
  739. * @ascq: the additional sense code qualifier we'll fill out
  740. * @verbose: be verbose
  741. *
  742. * Converts an ATA error into a SCSI error. Fill out pointers to
  743. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  744. * format sense blocks.
  745. *
  746. * LOCKING:
  747. * spin_lock_irqsave(host lock)
  748. */
  749. static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
  750. u8 *asc, u8 *ascq, int verbose)
  751. {
  752. int i;
  753. /* Based on the 3ware driver translation table */
  754. static const unsigned char sense_table[][4] = {
  755. /* BBD|ECC|ID|MAR */
  756. {0xd1, ABORTED_COMMAND, 0x00, 0x00},
  757. // Device busy Aborted command
  758. /* BBD|ECC|ID */
  759. {0xd0, ABORTED_COMMAND, 0x00, 0x00},
  760. // Device busy Aborted command
  761. /* ECC|MC|MARK */
  762. {0x61, HARDWARE_ERROR, 0x00, 0x00},
  763. // Device fault Hardware error
  764. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  765. {0x84, ABORTED_COMMAND, 0x47, 0x00},
  766. // Data CRC error SCSI parity error
  767. /* MC|ID|ABRT|TRK0|MARK */
  768. {0x37, NOT_READY, 0x04, 0x00},
  769. // Unit offline Not ready
  770. /* MCR|MARK */
  771. {0x09, NOT_READY, 0x04, 0x00},
  772. // Unrecovered disk error Not ready
  773. /* Bad address mark */
  774. {0x01, MEDIUM_ERROR, 0x13, 0x00},
  775. // Address mark not found for data field
  776. /* TRK0 - Track 0 not found */
  777. {0x02, HARDWARE_ERROR, 0x00, 0x00},
  778. // Hardware error
  779. /* Abort: 0x04 is not translated here, see below */
  780. /* Media change request */
  781. {0x08, NOT_READY, 0x04, 0x00},
  782. // FIXME: faking offline
  783. /* SRV/IDNF - ID not found */
  784. {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
  785. // Logical address out of range
  786. /* MC - Media Changed */
  787. {0x20, UNIT_ATTENTION, 0x28, 0x00},
  788. // Not ready to ready change, medium may have changed
  789. /* ECC - Uncorrectable ECC error */
  790. {0x40, MEDIUM_ERROR, 0x11, 0x04},
  791. // Unrecovered read error
  792. /* BBD - block marked bad */
  793. {0x80, MEDIUM_ERROR, 0x11, 0x04},
  794. // Block marked bad Medium error, unrecovered read error
  795. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  796. };
  797. static const unsigned char stat_table[][4] = {
  798. /* Must be first because BUSY means no other bits valid */
  799. {0x80, ABORTED_COMMAND, 0x47, 0x00},
  800. // Busy, fake parity for now
  801. {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
  802. // Device ready, unaligned write command
  803. {0x20, HARDWARE_ERROR, 0x44, 0x00},
  804. // Device fault, internal target failure
  805. {0x08, ABORTED_COMMAND, 0x47, 0x00},
  806. // Timed out in xfer, fake parity for now
  807. {0x04, RECOVERED_ERROR, 0x11, 0x00},
  808. // Recovered ECC error Medium error, recovered
  809. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  810. };
  811. /*
  812. * Is this an error we can process/parse
  813. */
  814. if (drv_stat & ATA_BUSY) {
  815. drv_err = 0; /* Ignore the err bits, they're invalid */
  816. }
  817. if (drv_err) {
  818. /* Look for drv_err */
  819. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  820. /* Look for best matches first */
  821. if ((sense_table[i][0] & drv_err) ==
  822. sense_table[i][0]) {
  823. *sk = sense_table[i][1];
  824. *asc = sense_table[i][2];
  825. *ascq = sense_table[i][3];
  826. goto translate_done;
  827. }
  828. }
  829. }
  830. /*
  831. * Fall back to interpreting status bits. Note that if the drv_err
  832. * has only the ABRT bit set, we decode drv_stat. ABRT by itself
  833. * is not descriptive enough.
  834. */
  835. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  836. if (stat_table[i][0] & drv_stat) {
  837. *sk = stat_table[i][1];
  838. *asc = stat_table[i][2];
  839. *ascq = stat_table[i][3];
  840. goto translate_done;
  841. }
  842. }
  843. /*
  844. * We need a sensible error return here, which is tricky, and one
  845. * that won't cause people to do things like return a disk wrongly.
  846. */
  847. *sk = ABORTED_COMMAND;
  848. *asc = 0x00;
  849. *ascq = 0x00;
  850. translate_done:
  851. if (verbose)
  852. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
  853. "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
  854. id, drv_stat, drv_err, *sk, *asc, *ascq);
  855. return;
  856. }
  857. /*
  858. * ata_gen_passthru_sense - Generate check condition sense block.
  859. * @qc: Command that completed.
  860. *
  861. * This function is specific to the ATA descriptor format sense
  862. * block specified for the ATA pass through commands. Regardless
  863. * of whether the command errored or not, return a sense
  864. * block. Copy all controller registers into the sense
  865. * block. If there was no error, we get the request from an ATA
  866. * passthrough command, so we use the following sense data:
  867. * sk = RECOVERED ERROR
  868. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  869. *
  870. *
  871. * LOCKING:
  872. * None.
  873. */
  874. static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
  875. {
  876. struct scsi_cmnd *cmd = qc->scsicmd;
  877. struct ata_taskfile *tf = &qc->result_tf;
  878. unsigned char *sb = cmd->sense_buffer;
  879. unsigned char *desc = sb + 8;
  880. int verbose = qc->ap->ops->error_handler == NULL;
  881. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  882. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  883. /*
  884. * Use ata_to_sense_error() to map status register bits
  885. * onto sense key, asc & ascq.
  886. */
  887. if (qc->err_mask ||
  888. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  889. ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
  890. &sb[1], &sb[2], &sb[3], verbose);
  891. sb[1] &= 0x0f;
  892. } else {
  893. sb[1] = RECOVERED_ERROR;
  894. sb[2] = 0;
  895. sb[3] = 0x1D;
  896. }
  897. /*
  898. * Sense data is current and format is descriptor.
  899. */
  900. sb[0] = 0x72;
  901. desc[0] = 0x09;
  902. /* set length of additional sense data */
  903. sb[7] = 14;
  904. desc[1] = 12;
  905. /*
  906. * Copy registers into sense buffer.
  907. */
  908. desc[2] = 0x00;
  909. desc[3] = tf->feature; /* == error reg */
  910. desc[5] = tf->nsect;
  911. desc[7] = tf->lbal;
  912. desc[9] = tf->lbam;
  913. desc[11] = tf->lbah;
  914. desc[12] = tf->device;
  915. desc[13] = tf->command; /* == status reg */
  916. /*
  917. * Fill in Extend bit, and the high order bytes
  918. * if applicable.
  919. */
  920. if (tf->flags & ATA_TFLAG_LBA48) {
  921. desc[2] |= 0x01;
  922. desc[4] = tf->hob_nsect;
  923. desc[6] = tf->hob_lbal;
  924. desc[8] = tf->hob_lbam;
  925. desc[10] = tf->hob_lbah;
  926. }
  927. }
  928. /**
  929. * ata_gen_ata_sense - generate a SCSI fixed sense block
  930. * @qc: Command that we are erroring out
  931. *
  932. * Generate sense block for a failed ATA command @qc. Descriptor
  933. * format is used to accommodate LBA48 block address.
  934. *
  935. * LOCKING:
  936. * None.
  937. */
  938. static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
  939. {
  940. struct ata_device *dev = qc->dev;
  941. struct scsi_cmnd *cmd = qc->scsicmd;
  942. struct ata_taskfile *tf = &qc->result_tf;
  943. unsigned char *sb = cmd->sense_buffer;
  944. unsigned char *desc = sb + 8;
  945. int verbose = qc->ap->ops->error_handler == NULL;
  946. u64 block;
  947. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  948. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  949. /* sense data is current and format is descriptor */
  950. sb[0] = 0x72;
  951. /* Use ata_to_sense_error() to map status register bits
  952. * onto sense key, asc & ascq.
  953. */
  954. if (qc->err_mask ||
  955. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  956. ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
  957. &sb[1], &sb[2], &sb[3], verbose);
  958. sb[1] &= 0x0f;
  959. }
  960. block = ata_tf_read_block(&qc->result_tf, dev);
  961. /* information sense data descriptor */
  962. sb[7] = 12;
  963. desc[0] = 0x00;
  964. desc[1] = 10;
  965. desc[2] |= 0x80; /* valid */
  966. desc[6] = block >> 40;
  967. desc[7] = block >> 32;
  968. desc[8] = block >> 24;
  969. desc[9] = block >> 16;
  970. desc[10] = block >> 8;
  971. desc[11] = block;
  972. }
  973. static void ata_scsi_sdev_config(struct scsi_device *sdev)
  974. {
  975. sdev->use_10_for_rw = 1;
  976. sdev->use_10_for_ms = 1;
  977. sdev->no_report_opcodes = 1;
  978. sdev->no_write_same = 1;
  979. /* Schedule policy is determined by ->qc_defer() callback and
  980. * it needs to see every deferred qc. Set dev_blocked to 1 to
  981. * prevent SCSI midlayer from automatically deferring
  982. * requests.
  983. */
  984. sdev->max_device_blocked = 1;
  985. }
  986. /**
  987. * atapi_drain_needed - Check whether data transfer may overflow
  988. * @rq: request to be checked
  989. *
  990. * ATAPI commands which transfer variable length data to host
  991. * might overflow due to application error or hardare bug. This
  992. * function checks whether overflow should be drained and ignored
  993. * for @request.
  994. *
  995. * LOCKING:
  996. * None.
  997. *
  998. * RETURNS:
  999. * 1 if ; otherwise, 0.
  1000. */
  1001. static int atapi_drain_needed(struct request *rq)
  1002. {
  1003. if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
  1004. return 0;
  1005. if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
  1006. return 0;
  1007. return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
  1008. }
  1009. static int ata_scsi_dev_config(struct scsi_device *sdev,
  1010. struct ata_device *dev)
  1011. {
  1012. struct request_queue *q = sdev->request_queue;
  1013. if (!ata_id_has_unload(dev->id))
  1014. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  1015. /* configure max sectors */
  1016. blk_queue_max_hw_sectors(q, dev->max_sectors);
  1017. if (dev->class == ATA_DEV_ATAPI) {
  1018. void *buf;
  1019. sdev->sector_size = ATA_SECT_SIZE;
  1020. /* set DMA padding */
  1021. blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
  1022. /* configure draining */
  1023. buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
  1024. if (!buf) {
  1025. ata_dev_err(dev, "drain buffer allocation failed\n");
  1026. return -ENOMEM;
  1027. }
  1028. blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
  1029. } else {
  1030. sdev->sector_size = ata_id_logical_sector_size(dev->id);
  1031. sdev->manage_start_stop = 1;
  1032. }
  1033. /*
  1034. * ata_pio_sectors() expects buffer for each sector to not cross
  1035. * page boundary. Enforce it by requiring buffers to be sector
  1036. * aligned, which works iff sector_size is not larger than
  1037. * PAGE_SIZE. ATAPI devices also need the alignment as
  1038. * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
  1039. */
  1040. if (sdev->sector_size > PAGE_SIZE)
  1041. ata_dev_warn(dev,
  1042. "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
  1043. sdev->sector_size);
  1044. blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
  1045. if (dev->flags & ATA_DFLAG_AN)
  1046. set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
  1047. if (dev->flags & ATA_DFLAG_NCQ) {
  1048. int depth;
  1049. depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  1050. depth = min(ATA_MAX_QUEUE - 1, depth);
  1051. scsi_change_queue_depth(sdev, depth);
  1052. }
  1053. blk_queue_flush_queueable(q, false);
  1054. dev->sdev = sdev;
  1055. return 0;
  1056. }
  1057. /**
  1058. * ata_scsi_slave_config - Set SCSI device attributes
  1059. * @sdev: SCSI device to examine
  1060. *
  1061. * This is called before we actually start reading
  1062. * and writing to the device, to configure certain
  1063. * SCSI mid-layer behaviors.
  1064. *
  1065. * LOCKING:
  1066. * Defined by SCSI layer. We don't really care.
  1067. */
  1068. int ata_scsi_slave_config(struct scsi_device *sdev)
  1069. {
  1070. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1071. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  1072. int rc = 0;
  1073. ata_scsi_sdev_config(sdev);
  1074. if (dev)
  1075. rc = ata_scsi_dev_config(sdev, dev);
  1076. return rc;
  1077. }
  1078. /**
  1079. * ata_scsi_slave_destroy - SCSI device is about to be destroyed
  1080. * @sdev: SCSI device to be destroyed
  1081. *
  1082. * @sdev is about to be destroyed for hot/warm unplugging. If
  1083. * this unplugging was initiated by libata as indicated by NULL
  1084. * dev->sdev, this function doesn't have to do anything.
  1085. * Otherwise, SCSI layer initiated warm-unplug is in progress.
  1086. * Clear dev->sdev, schedule the device for ATA detach and invoke
  1087. * EH.
  1088. *
  1089. * LOCKING:
  1090. * Defined by SCSI layer. We don't really care.
  1091. */
  1092. void ata_scsi_slave_destroy(struct scsi_device *sdev)
  1093. {
  1094. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1095. struct request_queue *q = sdev->request_queue;
  1096. unsigned long flags;
  1097. struct ata_device *dev;
  1098. if (!ap->ops->error_handler)
  1099. return;
  1100. spin_lock_irqsave(ap->lock, flags);
  1101. dev = __ata_scsi_find_dev(ap, sdev);
  1102. if (dev && dev->sdev) {
  1103. /* SCSI device already in CANCEL state, no need to offline it */
  1104. dev->sdev = NULL;
  1105. dev->flags |= ATA_DFLAG_DETACH;
  1106. ata_port_schedule_eh(ap);
  1107. }
  1108. spin_unlock_irqrestore(ap->lock, flags);
  1109. kfree(q->dma_drain_buffer);
  1110. q->dma_drain_buffer = NULL;
  1111. q->dma_drain_size = 0;
  1112. }
  1113. /**
  1114. * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
  1115. * @ap: ATA port to which the device change the queue depth
  1116. * @sdev: SCSI device to configure queue depth for
  1117. * @queue_depth: new queue depth
  1118. *
  1119. * libsas and libata have different approaches for associating a sdev to
  1120. * its ata_port.
  1121. *
  1122. */
  1123. int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
  1124. int queue_depth)
  1125. {
  1126. struct ata_device *dev;
  1127. unsigned long flags;
  1128. if (queue_depth < 1 || queue_depth == sdev->queue_depth)
  1129. return sdev->queue_depth;
  1130. dev = ata_scsi_find_dev(ap, sdev);
  1131. if (!dev || !ata_dev_enabled(dev))
  1132. return sdev->queue_depth;
  1133. /* NCQ enabled? */
  1134. spin_lock_irqsave(ap->lock, flags);
  1135. dev->flags &= ~ATA_DFLAG_NCQ_OFF;
  1136. if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
  1137. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1138. queue_depth = 1;
  1139. }
  1140. spin_unlock_irqrestore(ap->lock, flags);
  1141. /* limit and apply queue depth */
  1142. queue_depth = min(queue_depth, sdev->host->can_queue);
  1143. queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
  1144. queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
  1145. if (sdev->queue_depth == queue_depth)
  1146. return -EINVAL;
  1147. return scsi_change_queue_depth(sdev, queue_depth);
  1148. }
  1149. /**
  1150. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  1151. * @sdev: SCSI device to configure queue depth for
  1152. * @queue_depth: new queue depth
  1153. *
  1154. * This is libata standard hostt->change_queue_depth callback.
  1155. * SCSI will call into this callback when user tries to set queue
  1156. * depth via sysfs.
  1157. *
  1158. * LOCKING:
  1159. * SCSI layer (we don't care)
  1160. *
  1161. * RETURNS:
  1162. * Newly configured queue depth.
  1163. */
  1164. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  1165. {
  1166. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1167. return __ata_change_queue_depth(ap, sdev, queue_depth);
  1168. }
  1169. /**
  1170. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  1171. * @qc: Storage for translated ATA taskfile
  1172. *
  1173. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  1174. * (to start). Perhaps these commands should be preceded by
  1175. * CHECK POWER MODE to see what power mode the device is already in.
  1176. * [See SAT revision 5 at www.t10.org]
  1177. *
  1178. * LOCKING:
  1179. * spin_lock_irqsave(host lock)
  1180. *
  1181. * RETURNS:
  1182. * Zero on success, non-zero on error.
  1183. */
  1184. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
  1185. {
  1186. struct scsi_cmnd *scmd = qc->scsicmd;
  1187. struct ata_taskfile *tf = &qc->tf;
  1188. const u8 *cdb = scmd->cmnd;
  1189. if (scmd->cmd_len < 5)
  1190. goto invalid_fld;
  1191. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1192. tf->protocol = ATA_PROT_NODATA;
  1193. if (cdb[1] & 0x1) {
  1194. ; /* ignore IMMED bit, violates sat-r05 */
  1195. }
  1196. if (cdb[4] & 0x2)
  1197. goto invalid_fld; /* LOEJ bit set not supported */
  1198. if (((cdb[4] >> 4) & 0xf) != 0)
  1199. goto invalid_fld; /* power conditions not supported */
  1200. if (cdb[4] & 0x1) {
  1201. tf->nsect = 1; /* 1 sector, lba=0 */
  1202. if (qc->dev->flags & ATA_DFLAG_LBA) {
  1203. tf->flags |= ATA_TFLAG_LBA;
  1204. tf->lbah = 0x0;
  1205. tf->lbam = 0x0;
  1206. tf->lbal = 0x0;
  1207. tf->device |= ATA_LBA;
  1208. } else {
  1209. /* CHS */
  1210. tf->lbal = 0x1; /* sect */
  1211. tf->lbam = 0x0; /* cyl low */
  1212. tf->lbah = 0x0; /* cyl high */
  1213. }
  1214. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  1215. } else {
  1216. /* Some odd clown BIOSen issue spindown on power off (ACPI S4
  1217. * or S5) causing some drives to spin up and down again.
  1218. */
  1219. if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
  1220. system_state == SYSTEM_POWER_OFF)
  1221. goto skip;
  1222. if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
  1223. system_entering_hibernation())
  1224. goto skip;
  1225. /* Issue ATA STANDBY IMMEDIATE command */
  1226. tf->command = ATA_CMD_STANDBYNOW1;
  1227. }
  1228. /*
  1229. * Standby and Idle condition timers could be implemented but that
  1230. * would require libata to implement the Power condition mode page
  1231. * and allow the user to change it. Changing mode pages requires
  1232. * MODE SELECT to be implemented.
  1233. */
  1234. return 0;
  1235. invalid_fld:
  1236. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1237. /* "Invalid field in cbd" */
  1238. return 1;
  1239. skip:
  1240. scmd->result = SAM_STAT_GOOD;
  1241. return 1;
  1242. }
  1243. /**
  1244. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  1245. * @qc: Storage for translated ATA taskfile
  1246. *
  1247. * Sets up an ATA taskfile to issue FLUSH CACHE or
  1248. * FLUSH CACHE EXT.
  1249. *
  1250. * LOCKING:
  1251. * spin_lock_irqsave(host lock)
  1252. *
  1253. * RETURNS:
  1254. * Zero on success, non-zero on error.
  1255. */
  1256. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
  1257. {
  1258. struct ata_taskfile *tf = &qc->tf;
  1259. tf->flags |= ATA_TFLAG_DEVICE;
  1260. tf->protocol = ATA_PROT_NODATA;
  1261. if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
  1262. tf->command = ATA_CMD_FLUSH_EXT;
  1263. else
  1264. tf->command = ATA_CMD_FLUSH;
  1265. /* flush is critical for IO integrity, consider it an IO command */
  1266. qc->flags |= ATA_QCFLAG_IO;
  1267. return 0;
  1268. }
  1269. /**
  1270. * scsi_6_lba_len - Get LBA and transfer length
  1271. * @cdb: SCSI command to translate
  1272. *
  1273. * Calculate LBA and transfer length for 6-byte commands.
  1274. *
  1275. * RETURNS:
  1276. * @plba: the LBA
  1277. * @plen: the transfer length
  1278. */
  1279. static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1280. {
  1281. u64 lba = 0;
  1282. u32 len;
  1283. VPRINTK("six-byte command\n");
  1284. lba |= ((u64)(cdb[1] & 0x1f)) << 16;
  1285. lba |= ((u64)cdb[2]) << 8;
  1286. lba |= ((u64)cdb[3]);
  1287. len = cdb[4];
  1288. *plba = lba;
  1289. *plen = len;
  1290. }
  1291. /**
  1292. * scsi_10_lba_len - Get LBA and transfer length
  1293. * @cdb: SCSI command to translate
  1294. *
  1295. * Calculate LBA and transfer length for 10-byte commands.
  1296. *
  1297. * RETURNS:
  1298. * @plba: the LBA
  1299. * @plen: the transfer length
  1300. */
  1301. static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1302. {
  1303. u64 lba = 0;
  1304. u32 len = 0;
  1305. VPRINTK("ten-byte command\n");
  1306. lba |= ((u64)cdb[2]) << 24;
  1307. lba |= ((u64)cdb[3]) << 16;
  1308. lba |= ((u64)cdb[4]) << 8;
  1309. lba |= ((u64)cdb[5]);
  1310. len |= ((u32)cdb[7]) << 8;
  1311. len |= ((u32)cdb[8]);
  1312. *plba = lba;
  1313. *plen = len;
  1314. }
  1315. /**
  1316. * scsi_16_lba_len - Get LBA and transfer length
  1317. * @cdb: SCSI command to translate
  1318. *
  1319. * Calculate LBA and transfer length for 16-byte commands.
  1320. *
  1321. * RETURNS:
  1322. * @plba: the LBA
  1323. * @plen: the transfer length
  1324. */
  1325. static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1326. {
  1327. u64 lba = 0;
  1328. u32 len = 0;
  1329. VPRINTK("sixteen-byte command\n");
  1330. lba |= ((u64)cdb[2]) << 56;
  1331. lba |= ((u64)cdb[3]) << 48;
  1332. lba |= ((u64)cdb[4]) << 40;
  1333. lba |= ((u64)cdb[5]) << 32;
  1334. lba |= ((u64)cdb[6]) << 24;
  1335. lba |= ((u64)cdb[7]) << 16;
  1336. lba |= ((u64)cdb[8]) << 8;
  1337. lba |= ((u64)cdb[9]);
  1338. len |= ((u32)cdb[10]) << 24;
  1339. len |= ((u32)cdb[11]) << 16;
  1340. len |= ((u32)cdb[12]) << 8;
  1341. len |= ((u32)cdb[13]);
  1342. *plba = lba;
  1343. *plen = len;
  1344. }
  1345. /**
  1346. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  1347. * @qc: Storage for translated ATA taskfile
  1348. *
  1349. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  1350. *
  1351. * LOCKING:
  1352. * spin_lock_irqsave(host lock)
  1353. *
  1354. * RETURNS:
  1355. * Zero on success, non-zero on error.
  1356. */
  1357. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
  1358. {
  1359. struct scsi_cmnd *scmd = qc->scsicmd;
  1360. struct ata_taskfile *tf = &qc->tf;
  1361. struct ata_device *dev = qc->dev;
  1362. u64 dev_sectors = qc->dev->n_sectors;
  1363. const u8 *cdb = scmd->cmnd;
  1364. u64 block;
  1365. u32 n_block;
  1366. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1367. tf->protocol = ATA_PROT_NODATA;
  1368. if (cdb[0] == VERIFY) {
  1369. if (scmd->cmd_len < 10)
  1370. goto invalid_fld;
  1371. scsi_10_lba_len(cdb, &block, &n_block);
  1372. } else if (cdb[0] == VERIFY_16) {
  1373. if (scmd->cmd_len < 16)
  1374. goto invalid_fld;
  1375. scsi_16_lba_len(cdb, &block, &n_block);
  1376. } else
  1377. goto invalid_fld;
  1378. if (!n_block)
  1379. goto nothing_to_do;
  1380. if (block >= dev_sectors)
  1381. goto out_of_range;
  1382. if ((block + n_block) > dev_sectors)
  1383. goto out_of_range;
  1384. if (dev->flags & ATA_DFLAG_LBA) {
  1385. tf->flags |= ATA_TFLAG_LBA;
  1386. if (lba_28_ok(block, n_block)) {
  1387. /* use LBA28 */
  1388. tf->command = ATA_CMD_VERIFY;
  1389. tf->device |= (block >> 24) & 0xf;
  1390. } else if (lba_48_ok(block, n_block)) {
  1391. if (!(dev->flags & ATA_DFLAG_LBA48))
  1392. goto out_of_range;
  1393. /* use LBA48 */
  1394. tf->flags |= ATA_TFLAG_LBA48;
  1395. tf->command = ATA_CMD_VERIFY_EXT;
  1396. tf->hob_nsect = (n_block >> 8) & 0xff;
  1397. tf->hob_lbah = (block >> 40) & 0xff;
  1398. tf->hob_lbam = (block >> 32) & 0xff;
  1399. tf->hob_lbal = (block >> 24) & 0xff;
  1400. } else
  1401. /* request too large even for LBA48 */
  1402. goto out_of_range;
  1403. tf->nsect = n_block & 0xff;
  1404. tf->lbah = (block >> 16) & 0xff;
  1405. tf->lbam = (block >> 8) & 0xff;
  1406. tf->lbal = block & 0xff;
  1407. tf->device |= ATA_LBA;
  1408. } else {
  1409. /* CHS */
  1410. u32 sect, head, cyl, track;
  1411. if (!lba_28_ok(block, n_block))
  1412. goto out_of_range;
  1413. /* Convert LBA to CHS */
  1414. track = (u32)block / dev->sectors;
  1415. cyl = track / dev->heads;
  1416. head = track % dev->heads;
  1417. sect = (u32)block % dev->sectors + 1;
  1418. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1419. (u32)block, track, cyl, head, sect);
  1420. /* Check whether the converted CHS can fit.
  1421. Cylinder: 0-65535
  1422. Head: 0-15
  1423. Sector: 1-255*/
  1424. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1425. goto out_of_range;
  1426. tf->command = ATA_CMD_VERIFY;
  1427. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1428. tf->lbal = sect;
  1429. tf->lbam = cyl;
  1430. tf->lbah = cyl >> 8;
  1431. tf->device |= head;
  1432. }
  1433. return 0;
  1434. invalid_fld:
  1435. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1436. /* "Invalid field in cbd" */
  1437. return 1;
  1438. out_of_range:
  1439. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1440. /* "Logical Block Address out of range" */
  1441. return 1;
  1442. nothing_to_do:
  1443. scmd->result = SAM_STAT_GOOD;
  1444. return 1;
  1445. }
  1446. /**
  1447. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  1448. * @qc: Storage for translated ATA taskfile
  1449. *
  1450. * Converts any of six SCSI read/write commands into the
  1451. * ATA counterpart, including starting sector (LBA),
  1452. * sector count, and taking into account the device's LBA48
  1453. * support.
  1454. *
  1455. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  1456. * %WRITE_16 are currently supported.
  1457. *
  1458. * LOCKING:
  1459. * spin_lock_irqsave(host lock)
  1460. *
  1461. * RETURNS:
  1462. * Zero on success, non-zero on error.
  1463. */
  1464. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
  1465. {
  1466. struct scsi_cmnd *scmd = qc->scsicmd;
  1467. const u8 *cdb = scmd->cmnd;
  1468. unsigned int tf_flags = 0;
  1469. u64 block;
  1470. u32 n_block;
  1471. int rc;
  1472. if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
  1473. tf_flags |= ATA_TFLAG_WRITE;
  1474. /* Calculate the SCSI LBA, transfer length and FUA. */
  1475. switch (cdb[0]) {
  1476. case READ_10:
  1477. case WRITE_10:
  1478. if (unlikely(scmd->cmd_len < 10))
  1479. goto invalid_fld;
  1480. scsi_10_lba_len(cdb, &block, &n_block);
  1481. if (cdb[1] & (1 << 3))
  1482. tf_flags |= ATA_TFLAG_FUA;
  1483. break;
  1484. case READ_6:
  1485. case WRITE_6:
  1486. if (unlikely(scmd->cmd_len < 6))
  1487. goto invalid_fld;
  1488. scsi_6_lba_len(cdb, &block, &n_block);
  1489. /* for 6-byte r/w commands, transfer length 0
  1490. * means 256 blocks of data, not 0 block.
  1491. */
  1492. if (!n_block)
  1493. n_block = 256;
  1494. break;
  1495. case READ_16:
  1496. case WRITE_16:
  1497. if (unlikely(scmd->cmd_len < 16))
  1498. goto invalid_fld;
  1499. scsi_16_lba_len(cdb, &block, &n_block);
  1500. if (cdb[1] & (1 << 3))
  1501. tf_flags |= ATA_TFLAG_FUA;
  1502. break;
  1503. default:
  1504. DPRINTK("no-byte command\n");
  1505. goto invalid_fld;
  1506. }
  1507. /* Check and compose ATA command */
  1508. if (!n_block)
  1509. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1510. * length 0 means transfer 0 block of data.
  1511. * However, for ATA R/W commands, sector count 0 means
  1512. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1513. *
  1514. * WARNING: one or two older ATA drives treat 0 as 0...
  1515. */
  1516. goto nothing_to_do;
  1517. qc->flags |= ATA_QCFLAG_IO;
  1518. qc->nbytes = n_block * scmd->device->sector_size;
  1519. rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
  1520. qc->tag);
  1521. if (likely(rc == 0))
  1522. return 0;
  1523. if (rc == -ERANGE)
  1524. goto out_of_range;
  1525. /* treat all other errors as -EINVAL, fall through */
  1526. invalid_fld:
  1527. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1528. /* "Invalid field in cbd" */
  1529. return 1;
  1530. out_of_range:
  1531. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1532. /* "Logical Block Address out of range" */
  1533. return 1;
  1534. nothing_to_do:
  1535. scmd->result = SAM_STAT_GOOD;
  1536. return 1;
  1537. }
  1538. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1539. {
  1540. struct ata_port *ap = qc->ap;
  1541. struct scsi_cmnd *cmd = qc->scsicmd;
  1542. u8 *cdb = cmd->cmnd;
  1543. int need_sense = (qc->err_mask != 0);
  1544. /* For ATA pass thru (SAT) commands, generate a sense block if
  1545. * user mandated it or if there's an error. Note that if we
  1546. * generate because the user forced us to [CK_COND =1], a check
  1547. * condition is generated and the ATA register values are returned
  1548. * whether the command completed successfully or not. If there
  1549. * was no error, we use the following sense data:
  1550. * sk = RECOVERED ERROR
  1551. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  1552. */
  1553. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1554. ((cdb[2] & 0x20) || need_sense)) {
  1555. ata_gen_passthru_sense(qc);
  1556. } else {
  1557. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1558. cmd->result = SAM_STAT_CHECK_CONDITION;
  1559. } else if (!need_sense) {
  1560. cmd->result = SAM_STAT_GOOD;
  1561. } else {
  1562. /* TODO: decide which descriptor format to use
  1563. * for 48b LBA devices and call that here
  1564. * instead of the fixed desc, which is only
  1565. * good for smaller LBA (and maybe CHS?)
  1566. * devices.
  1567. */
  1568. ata_gen_ata_sense(qc);
  1569. }
  1570. }
  1571. if (need_sense && !ap->ops->error_handler)
  1572. ata_dump_status(ap->print_id, &qc->result_tf);
  1573. qc->scsidone(cmd);
  1574. ata_qc_free(qc);
  1575. }
  1576. /**
  1577. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1578. * @dev: ATA device to which the command is addressed
  1579. * @cmd: SCSI command to execute
  1580. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1581. *
  1582. * Our ->queuecommand() function has decided that the SCSI
  1583. * command issued can be directly translated into an ATA
  1584. * command, rather than handled internally.
  1585. *
  1586. * This function sets up an ata_queued_cmd structure for the
  1587. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1588. *
  1589. * The xlat_func argument (actor) returns 0 if ready to execute
  1590. * ATA command, else 1 to finish translation. If 1 is returned
  1591. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1592. * to be set reflecting an error condition or clean (early)
  1593. * termination.
  1594. *
  1595. * LOCKING:
  1596. * spin_lock_irqsave(host lock)
  1597. *
  1598. * RETURNS:
  1599. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1600. * needs to be deferred.
  1601. */
  1602. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1603. ata_xlat_func_t xlat_func)
  1604. {
  1605. struct ata_port *ap = dev->link->ap;
  1606. struct ata_queued_cmd *qc;
  1607. int rc;
  1608. VPRINTK("ENTER\n");
  1609. qc = ata_scsi_qc_new(dev, cmd);
  1610. if (!qc)
  1611. goto err_mem;
  1612. /* data is present; dma-map it */
  1613. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1614. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1615. if (unlikely(scsi_bufflen(cmd) < 1)) {
  1616. ata_dev_warn(dev, "WARNING: zero len r/w req\n");
  1617. goto err_did;
  1618. }
  1619. ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
  1620. qc->dma_dir = cmd->sc_data_direction;
  1621. }
  1622. qc->complete_fn = ata_scsi_qc_complete;
  1623. if (xlat_func(qc))
  1624. goto early_finish;
  1625. if (ap->ops->qc_defer) {
  1626. if ((rc = ap->ops->qc_defer(qc)))
  1627. goto defer;
  1628. }
  1629. /* select device, send command to hardware */
  1630. ata_qc_issue(qc);
  1631. VPRINTK("EXIT\n");
  1632. return 0;
  1633. early_finish:
  1634. ata_qc_free(qc);
  1635. cmd->scsi_done(cmd);
  1636. DPRINTK("EXIT - early finish (good or error)\n");
  1637. return 0;
  1638. err_did:
  1639. ata_qc_free(qc);
  1640. cmd->result = (DID_ERROR << 16);
  1641. cmd->scsi_done(cmd);
  1642. err_mem:
  1643. DPRINTK("EXIT - internal\n");
  1644. return 0;
  1645. defer:
  1646. ata_qc_free(qc);
  1647. DPRINTK("EXIT - defer\n");
  1648. if (rc == ATA_DEFER_LINK)
  1649. return SCSI_MLQUEUE_DEVICE_BUSY;
  1650. else
  1651. return SCSI_MLQUEUE_HOST_BUSY;
  1652. }
  1653. /**
  1654. * ata_scsi_rbuf_get - Map response buffer.
  1655. * @cmd: SCSI command containing buffer to be mapped.
  1656. * @flags: unsigned long variable to store irq enable status
  1657. * @copy_in: copy in from user buffer
  1658. *
  1659. * Prepare buffer for simulated SCSI commands.
  1660. *
  1661. * LOCKING:
  1662. * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  1663. *
  1664. * RETURNS:
  1665. * Pointer to response buffer.
  1666. */
  1667. static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
  1668. unsigned long *flags)
  1669. {
  1670. spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
  1671. memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
  1672. if (copy_in)
  1673. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1674. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1675. return ata_scsi_rbuf;
  1676. }
  1677. /**
  1678. * ata_scsi_rbuf_put - Unmap response buffer.
  1679. * @cmd: SCSI command containing buffer to be unmapped.
  1680. * @copy_out: copy out result
  1681. * @flags: @flags passed to ata_scsi_rbuf_get()
  1682. *
  1683. * Returns rbuf buffer. The result is copied to @cmd's buffer if
  1684. * @copy_back is true.
  1685. *
  1686. * LOCKING:
  1687. * Unlocks ata_scsi_rbuf_lock.
  1688. */
  1689. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
  1690. unsigned long *flags)
  1691. {
  1692. if (copy_out)
  1693. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1694. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1695. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
  1696. }
  1697. /**
  1698. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1699. * @args: device IDENTIFY data / SCSI command of interest.
  1700. * @actor: Callback hook for desired SCSI command simulator
  1701. *
  1702. * Takes care of the hard work of simulating a SCSI command...
  1703. * Mapping the response buffer, calling the command's handler,
  1704. * and handling the handler's return value. This return value
  1705. * indicates whether the handler wishes the SCSI command to be
  1706. * completed successfully (0), or not (in which case cmd->result
  1707. * and sense buffer are assumed to be set).
  1708. *
  1709. * LOCKING:
  1710. * spin_lock_irqsave(host lock)
  1711. */
  1712. static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1713. unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
  1714. {
  1715. u8 *rbuf;
  1716. unsigned int rc;
  1717. struct scsi_cmnd *cmd = args->cmd;
  1718. unsigned long flags;
  1719. rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
  1720. rc = actor(args, rbuf);
  1721. ata_scsi_rbuf_put(cmd, rc == 0, &flags);
  1722. if (rc == 0)
  1723. cmd->result = SAM_STAT_GOOD;
  1724. args->done(cmd);
  1725. }
  1726. /**
  1727. * ata_scsiop_inq_std - Simulate INQUIRY command
  1728. * @args: device IDENTIFY data / SCSI command of interest.
  1729. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1730. *
  1731. * Returns standard device identification data associated
  1732. * with non-VPD INQUIRY command output.
  1733. *
  1734. * LOCKING:
  1735. * spin_lock_irqsave(host lock)
  1736. */
  1737. static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
  1738. {
  1739. const u8 versions[] = {
  1740. 0x00,
  1741. 0x60, /* SAM-3 (no version claimed) */
  1742. 0x03,
  1743. 0x20, /* SBC-2 (no version claimed) */
  1744. 0x02,
  1745. 0x60 /* SPC-3 (no version claimed) */
  1746. };
  1747. const u8 versions_zbc[] = {
  1748. 0x00,
  1749. 0xA0, /* SAM-5 (no version claimed) */
  1750. 0x04,
  1751. 0xC0, /* SBC-3 (no version claimed) */
  1752. 0x04,
  1753. 0x60, /* SPC-4 (no version claimed) */
  1754. 0x60,
  1755. 0x20, /* ZBC (no version claimed) */
  1756. };
  1757. u8 hdr[] = {
  1758. TYPE_DISK,
  1759. 0,
  1760. 0x5, /* claim SPC-3 version compatibility */
  1761. 2,
  1762. 95 - 4
  1763. };
  1764. VPRINTK("ENTER\n");
  1765. /* set scsi removable (RMB) bit per ata bit */
  1766. if (ata_id_removable(args->id))
  1767. hdr[1] |= (1 << 7);
  1768. if (args->dev->class == ATA_DEV_ZAC) {
  1769. hdr[0] = TYPE_ZBC;
  1770. hdr[2] = 0x6; /* ZBC is defined in SPC-4 */
  1771. }
  1772. memcpy(rbuf, hdr, sizeof(hdr));
  1773. memcpy(&rbuf[8], "ATA ", 8);
  1774. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
  1775. /* From SAT, use last 2 words from fw rev unless they are spaces */
  1776. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
  1777. if (strncmp(&rbuf[32], " ", 4) == 0)
  1778. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1779. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1780. memcpy(&rbuf[32], "n/a ", 4);
  1781. if (args->dev->class == ATA_DEV_ZAC)
  1782. memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
  1783. else
  1784. memcpy(rbuf + 58, versions, sizeof(versions));
  1785. return 0;
  1786. }
  1787. /**
  1788. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1789. * @args: device IDENTIFY data / SCSI command of interest.
  1790. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1791. *
  1792. * Returns list of inquiry VPD pages available.
  1793. *
  1794. * LOCKING:
  1795. * spin_lock_irqsave(host lock)
  1796. */
  1797. static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
  1798. {
  1799. const u8 pages[] = {
  1800. 0x00, /* page 0x00, this page */
  1801. 0x80, /* page 0x80, unit serial no page */
  1802. 0x83, /* page 0x83, device ident page */
  1803. 0x89, /* page 0x89, ata info page */
  1804. 0xb0, /* page 0xb0, block limits page */
  1805. 0xb1, /* page 0xb1, block device characteristics page */
  1806. 0xb2, /* page 0xb2, thin provisioning page */
  1807. };
  1808. rbuf[3] = sizeof(pages); /* number of supported VPD pages */
  1809. memcpy(rbuf + 4, pages, sizeof(pages));
  1810. return 0;
  1811. }
  1812. /**
  1813. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1814. * @args: device IDENTIFY data / SCSI command of interest.
  1815. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1816. *
  1817. * Returns ATA device serial number.
  1818. *
  1819. * LOCKING:
  1820. * spin_lock_irqsave(host lock)
  1821. */
  1822. static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
  1823. {
  1824. const u8 hdr[] = {
  1825. 0,
  1826. 0x80, /* this page code */
  1827. 0,
  1828. ATA_ID_SERNO_LEN, /* page len */
  1829. };
  1830. memcpy(rbuf, hdr, sizeof(hdr));
  1831. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1832. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1833. return 0;
  1834. }
  1835. /**
  1836. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1837. * @args: device IDENTIFY data / SCSI command of interest.
  1838. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1839. *
  1840. * Yields two logical unit device identification designators:
  1841. * - vendor specific ASCII containing the ATA serial number
  1842. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1843. * name ("ATA "), model and serial numbers.
  1844. *
  1845. * LOCKING:
  1846. * spin_lock_irqsave(host lock)
  1847. */
  1848. static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
  1849. {
  1850. const int sat_model_serial_desc_len = 68;
  1851. int num;
  1852. rbuf[1] = 0x83; /* this page code */
  1853. num = 4;
  1854. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1855. rbuf[num + 0] = 2;
  1856. rbuf[num + 3] = ATA_ID_SERNO_LEN;
  1857. num += 4;
  1858. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1859. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1860. num += ATA_ID_SERNO_LEN;
  1861. /* SAT defined lu model and serial numbers descriptor */
  1862. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  1863. rbuf[num + 0] = 2;
  1864. rbuf[num + 1] = 1;
  1865. rbuf[num + 3] = sat_model_serial_desc_len;
  1866. num += 4;
  1867. memcpy(rbuf + num, "ATA ", 8);
  1868. num += 8;
  1869. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
  1870. ATA_ID_PROD_LEN);
  1871. num += ATA_ID_PROD_LEN;
  1872. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
  1873. ATA_ID_SERNO_LEN);
  1874. num += ATA_ID_SERNO_LEN;
  1875. if (ata_id_has_wwn(args->id)) {
  1876. /* SAT defined lu world wide name */
  1877. /* piv=0, assoc=lu, code_set=binary, designator=NAA */
  1878. rbuf[num + 0] = 1;
  1879. rbuf[num + 1] = 3;
  1880. rbuf[num + 3] = ATA_ID_WWN_LEN;
  1881. num += 4;
  1882. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1883. ATA_ID_WWN, ATA_ID_WWN_LEN);
  1884. num += ATA_ID_WWN_LEN;
  1885. }
  1886. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  1887. return 0;
  1888. }
  1889. /**
  1890. * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  1891. * @args: device IDENTIFY data / SCSI command of interest.
  1892. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1893. *
  1894. * Yields SAT-specified ATA VPD page.
  1895. *
  1896. * LOCKING:
  1897. * spin_lock_irqsave(host lock)
  1898. */
  1899. static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
  1900. {
  1901. struct ata_taskfile tf;
  1902. memset(&tf, 0, sizeof(tf));
  1903. rbuf[1] = 0x89; /* our page code */
  1904. rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
  1905. rbuf[3] = (0x238 & 0xff);
  1906. memcpy(&rbuf[8], "linux ", 8);
  1907. memcpy(&rbuf[16], "libata ", 16);
  1908. memcpy(&rbuf[32], DRV_VERSION, 4);
  1909. /* we don't store the ATA device signature, so we fake it */
  1910. tf.command = ATA_DRDY; /* really, this is Status reg */
  1911. tf.lbal = 0x1;
  1912. tf.nsect = 0x1;
  1913. ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
  1914. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  1915. rbuf[56] = ATA_CMD_ID_ATA;
  1916. memcpy(&rbuf[60], &args->id[0], 512);
  1917. return 0;
  1918. }
  1919. static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
  1920. {
  1921. u16 min_io_sectors;
  1922. rbuf[1] = 0xb0;
  1923. rbuf[3] = 0x3c; /* required VPD size with unmap support */
  1924. /*
  1925. * Optimal transfer length granularity.
  1926. *
  1927. * This is always one physical block, but for disks with a smaller
  1928. * logical than physical sector size we need to figure out what the
  1929. * latter is.
  1930. */
  1931. min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
  1932. put_unaligned_be16(min_io_sectors, &rbuf[6]);
  1933. /*
  1934. * Optimal unmap granularity.
  1935. *
  1936. * The ATA spec doesn't even know about a granularity or alignment
  1937. * for the TRIM command. We can leave away most of the unmap related
  1938. * VPD page entries, but we have specifify a granularity to signal
  1939. * that we support some form of unmap - in thise case via WRITE SAME
  1940. * with the unmap bit set.
  1941. */
  1942. if (ata_id_has_trim(args->id)) {
  1943. put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
  1944. put_unaligned_be32(1, &rbuf[28]);
  1945. }
  1946. return 0;
  1947. }
  1948. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  1949. {
  1950. int form_factor = ata_id_form_factor(args->id);
  1951. int media_rotation_rate = ata_id_rotation_rate(args->id);
  1952. rbuf[1] = 0xb1;
  1953. rbuf[3] = 0x3c;
  1954. rbuf[4] = media_rotation_rate >> 8;
  1955. rbuf[5] = media_rotation_rate;
  1956. rbuf[7] = form_factor;
  1957. return 0;
  1958. }
  1959. static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
  1960. {
  1961. /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
  1962. rbuf[1] = 0xb2;
  1963. rbuf[3] = 0x4;
  1964. rbuf[5] = 1 << 6; /* TPWS */
  1965. return 0;
  1966. }
  1967. /**
  1968. * ata_scsiop_noop - Command handler that simply returns success.
  1969. * @args: device IDENTIFY data / SCSI command of interest.
  1970. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1971. *
  1972. * No operation. Simply returns success to caller, to indicate
  1973. * that the caller should successfully complete this SCSI command.
  1974. *
  1975. * LOCKING:
  1976. * spin_lock_irqsave(host lock)
  1977. */
  1978. static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
  1979. {
  1980. VPRINTK("ENTER\n");
  1981. return 0;
  1982. }
  1983. /**
  1984. * modecpy - Prepare response for MODE SENSE
  1985. * @dest: output buffer
  1986. * @src: data being copied
  1987. * @n: length of mode page
  1988. * @changeable: whether changeable parameters are requested
  1989. *
  1990. * Generate a generic MODE SENSE page for either current or changeable
  1991. * parameters.
  1992. *
  1993. * LOCKING:
  1994. * None.
  1995. */
  1996. static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
  1997. {
  1998. if (changeable) {
  1999. memcpy(dest, src, 2);
  2000. memset(dest + 2, 0, n - 2);
  2001. } else {
  2002. memcpy(dest, src, n);
  2003. }
  2004. }
  2005. /**
  2006. * ata_msense_caching - Simulate MODE SENSE caching info page
  2007. * @id: device IDENTIFY data
  2008. * @buf: output buffer
  2009. * @changeable: whether changeable parameters are requested
  2010. *
  2011. * Generate a caching info page, which conditionally indicates
  2012. * write caching to the SCSI layer, depending on device
  2013. * capabilities.
  2014. *
  2015. * LOCKING:
  2016. * None.
  2017. */
  2018. static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
  2019. {
  2020. modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
  2021. if (changeable || ata_id_wcache_enabled(id))
  2022. buf[2] |= (1 << 2); /* write cache enable */
  2023. if (!changeable && !ata_id_rahead_enabled(id))
  2024. buf[12] |= (1 << 5); /* disable read ahead */
  2025. return sizeof(def_cache_mpage);
  2026. }
  2027. /**
  2028. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  2029. * @buf: output buffer
  2030. * @changeable: whether changeable parameters are requested
  2031. *
  2032. * Generate a generic MODE SENSE control mode page.
  2033. *
  2034. * LOCKING:
  2035. * None.
  2036. */
  2037. static unsigned int ata_msense_ctl_mode(u8 *buf, bool changeable)
  2038. {
  2039. modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
  2040. return sizeof(def_control_mpage);
  2041. }
  2042. /**
  2043. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  2044. * @buf: output buffer
  2045. * @changeable: whether changeable parameters are requested
  2046. *
  2047. * Generate a generic MODE SENSE r/w error recovery page.
  2048. *
  2049. * LOCKING:
  2050. * None.
  2051. */
  2052. static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
  2053. {
  2054. modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
  2055. changeable);
  2056. return sizeof(def_rw_recovery_mpage);
  2057. }
  2058. /*
  2059. * We can turn this into a real blacklist if it's needed, for now just
  2060. * blacklist any Maxtor BANC1G10 revision firmware
  2061. */
  2062. static int ata_dev_supports_fua(u16 *id)
  2063. {
  2064. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  2065. if (!libata_fua)
  2066. return 0;
  2067. if (!ata_id_has_fua(id))
  2068. return 0;
  2069. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  2070. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  2071. if (strcmp(model, "Maxtor"))
  2072. return 1;
  2073. if (strcmp(fw, "BANC1G10"))
  2074. return 1;
  2075. return 0; /* blacklisted */
  2076. }
  2077. /**
  2078. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  2079. * @args: device IDENTIFY data / SCSI command of interest.
  2080. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2081. *
  2082. * Simulate MODE SENSE commands. Assume this is invoked for direct
  2083. * access devices (e.g. disks) only. There should be no block
  2084. * descriptor for other device types.
  2085. *
  2086. * LOCKING:
  2087. * spin_lock_irqsave(host lock)
  2088. */
  2089. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  2090. {
  2091. struct ata_device *dev = args->dev;
  2092. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  2093. const u8 sat_blk_desc[] = {
  2094. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  2095. 0,
  2096. 0, 0x2, 0x0 /* block length: 512 bytes */
  2097. };
  2098. u8 pg, spg;
  2099. unsigned int ebd, page_control, six_byte;
  2100. u8 dpofua;
  2101. VPRINTK("ENTER\n");
  2102. six_byte = (scsicmd[0] == MODE_SENSE);
  2103. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  2104. /*
  2105. * LLBA bit in msense(10) ignored (compliant)
  2106. */
  2107. page_control = scsicmd[2] >> 6;
  2108. switch (page_control) {
  2109. case 0: /* current */
  2110. case 1: /* changeable */
  2111. case 2: /* defaults */
  2112. break; /* supported */
  2113. case 3: /* saved */
  2114. goto saving_not_supp;
  2115. default:
  2116. goto invalid_fld;
  2117. }
  2118. if (six_byte)
  2119. p += 4 + (ebd ? 8 : 0);
  2120. else
  2121. p += 8 + (ebd ? 8 : 0);
  2122. pg = scsicmd[2] & 0x3f;
  2123. spg = scsicmd[3];
  2124. /*
  2125. * No mode subpages supported (yet) but asking for _all_
  2126. * subpages may be valid
  2127. */
  2128. if (spg && (spg != ALL_SUB_MPAGES))
  2129. goto invalid_fld;
  2130. switch(pg) {
  2131. case RW_RECOVERY_MPAGE:
  2132. p += ata_msense_rw_recovery(p, page_control == 1);
  2133. break;
  2134. case CACHE_MPAGE:
  2135. p += ata_msense_caching(args->id, p, page_control == 1);
  2136. break;
  2137. case CONTROL_MPAGE:
  2138. p += ata_msense_ctl_mode(p, page_control == 1);
  2139. break;
  2140. case ALL_MPAGES:
  2141. p += ata_msense_rw_recovery(p, page_control == 1);
  2142. p += ata_msense_caching(args->id, p, page_control == 1);
  2143. p += ata_msense_ctl_mode(p, page_control == 1);
  2144. break;
  2145. default: /* invalid page code */
  2146. goto invalid_fld;
  2147. }
  2148. dpofua = 0;
  2149. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2150. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2151. dpofua = 1 << 4;
  2152. if (six_byte) {
  2153. rbuf[0] = p - rbuf - 1;
  2154. rbuf[2] |= dpofua;
  2155. if (ebd) {
  2156. rbuf[3] = sizeof(sat_blk_desc);
  2157. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2158. }
  2159. } else {
  2160. unsigned int output_len = p - rbuf - 2;
  2161. rbuf[0] = output_len >> 8;
  2162. rbuf[1] = output_len;
  2163. rbuf[3] |= dpofua;
  2164. if (ebd) {
  2165. rbuf[7] = sizeof(sat_blk_desc);
  2166. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2167. }
  2168. }
  2169. return 0;
  2170. invalid_fld:
  2171. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2172. /* "Invalid field in cbd" */
  2173. return 1;
  2174. saving_not_supp:
  2175. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2176. /* "Saving parameters not supported" */
  2177. return 1;
  2178. }
  2179. /**
  2180. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2181. * @args: device IDENTIFY data / SCSI command of interest.
  2182. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2183. *
  2184. * Simulate READ CAPACITY commands.
  2185. *
  2186. * LOCKING:
  2187. * None.
  2188. */
  2189. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2190. {
  2191. struct ata_device *dev = args->dev;
  2192. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2193. u32 sector_size; /* physical sector size in bytes */
  2194. u8 log2_per_phys;
  2195. u16 lowest_aligned;
  2196. sector_size = ata_id_logical_sector_size(dev->id);
  2197. log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
  2198. lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
  2199. VPRINTK("ENTER\n");
  2200. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2201. if (last_lba >= 0xffffffffULL)
  2202. last_lba = 0xffffffff;
  2203. /* sector count, 32-bit */
  2204. rbuf[0] = last_lba >> (8 * 3);
  2205. rbuf[1] = last_lba >> (8 * 2);
  2206. rbuf[2] = last_lba >> (8 * 1);
  2207. rbuf[3] = last_lba;
  2208. /* sector size */
  2209. rbuf[4] = sector_size >> (8 * 3);
  2210. rbuf[5] = sector_size >> (8 * 2);
  2211. rbuf[6] = sector_size >> (8 * 1);
  2212. rbuf[7] = sector_size;
  2213. } else {
  2214. /* sector count, 64-bit */
  2215. rbuf[0] = last_lba >> (8 * 7);
  2216. rbuf[1] = last_lba >> (8 * 6);
  2217. rbuf[2] = last_lba >> (8 * 5);
  2218. rbuf[3] = last_lba >> (8 * 4);
  2219. rbuf[4] = last_lba >> (8 * 3);
  2220. rbuf[5] = last_lba >> (8 * 2);
  2221. rbuf[6] = last_lba >> (8 * 1);
  2222. rbuf[7] = last_lba;
  2223. /* sector size */
  2224. rbuf[ 8] = sector_size >> (8 * 3);
  2225. rbuf[ 9] = sector_size >> (8 * 2);
  2226. rbuf[10] = sector_size >> (8 * 1);
  2227. rbuf[11] = sector_size;
  2228. rbuf[12] = 0;
  2229. rbuf[13] = log2_per_phys;
  2230. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2231. rbuf[15] = lowest_aligned;
  2232. if (ata_id_has_trim(args->id)) {
  2233. rbuf[14] |= 0x80; /* LBPME */
  2234. if (ata_id_has_zero_after_trim(args->id) &&
  2235. dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
  2236. ata_dev_info(dev, "Enabling discard_zeroes_data\n");
  2237. rbuf[14] |= 0x40; /* LBPRZ */
  2238. }
  2239. }
  2240. }
  2241. return 0;
  2242. }
  2243. /**
  2244. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2245. * @args: device IDENTIFY data / SCSI command of interest.
  2246. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2247. *
  2248. * Simulate REPORT LUNS command.
  2249. *
  2250. * LOCKING:
  2251. * spin_lock_irqsave(host lock)
  2252. */
  2253. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2254. {
  2255. VPRINTK("ENTER\n");
  2256. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2257. return 0;
  2258. }
  2259. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2260. {
  2261. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2262. /* FIXME: not quite right; we don't want the
  2263. * translation of taskfile registers into
  2264. * a sense descriptors, since that's only
  2265. * correct for ATA, not ATAPI
  2266. */
  2267. ata_gen_passthru_sense(qc);
  2268. }
  2269. qc->scsidone(qc->scsicmd);
  2270. ata_qc_free(qc);
  2271. }
  2272. /* is it pointless to prefer PIO for "safety reasons"? */
  2273. static inline int ata_pio_use_silly(struct ata_port *ap)
  2274. {
  2275. return (ap->flags & ATA_FLAG_PIO_DMA);
  2276. }
  2277. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2278. {
  2279. struct ata_port *ap = qc->ap;
  2280. struct scsi_cmnd *cmd = qc->scsicmd;
  2281. DPRINTK("ATAPI request sense\n");
  2282. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2283. #ifdef CONFIG_ATA_SFF
  2284. if (ap->ops->sff_tf_read)
  2285. ap->ops->sff_tf_read(ap, &qc->tf);
  2286. #endif
  2287. /* fill these in, for the case where they are -not- overwritten */
  2288. cmd->sense_buffer[0] = 0x70;
  2289. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2290. ata_qc_reinit(qc);
  2291. /* setup sg table and init transfer direction */
  2292. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2293. ata_sg_init(qc, &qc->sgent, 1);
  2294. qc->dma_dir = DMA_FROM_DEVICE;
  2295. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2296. qc->cdb[0] = REQUEST_SENSE;
  2297. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2298. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2299. qc->tf.command = ATA_CMD_PACKET;
  2300. if (ata_pio_use_silly(ap)) {
  2301. qc->tf.protocol = ATAPI_PROT_DMA;
  2302. qc->tf.feature |= ATAPI_PKT_DMA;
  2303. } else {
  2304. qc->tf.protocol = ATAPI_PROT_PIO;
  2305. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2306. qc->tf.lbah = 0;
  2307. }
  2308. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2309. qc->complete_fn = atapi_sense_complete;
  2310. ata_qc_issue(qc);
  2311. DPRINTK("EXIT\n");
  2312. }
  2313. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2314. {
  2315. struct scsi_cmnd *cmd = qc->scsicmd;
  2316. unsigned int err_mask = qc->err_mask;
  2317. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2318. /* handle completion from new EH */
  2319. if (unlikely(qc->ap->ops->error_handler &&
  2320. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2321. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2322. /* FIXME: not quite right; we don't want the
  2323. * translation of taskfile registers into a
  2324. * sense descriptors, since that's only
  2325. * correct for ATA, not ATAPI
  2326. */
  2327. ata_gen_passthru_sense(qc);
  2328. }
  2329. /* SCSI EH automatically locks door if sdev->locked is
  2330. * set. Sometimes door lock request continues to
  2331. * fail, for example, when no media is present. This
  2332. * creates a loop - SCSI EH issues door lock which
  2333. * fails and gets invoked again to acquire sense data
  2334. * for the failed command.
  2335. *
  2336. * If door lock fails, always clear sdev->locked to
  2337. * avoid this infinite loop.
  2338. *
  2339. * This may happen before SCSI scan is complete. Make
  2340. * sure qc->dev->sdev isn't NULL before dereferencing.
  2341. */
  2342. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
  2343. qc->dev->sdev->locked = 0;
  2344. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2345. qc->scsidone(cmd);
  2346. ata_qc_free(qc);
  2347. return;
  2348. }
  2349. /* successful completion or old EH failure path */
  2350. if (unlikely(err_mask & AC_ERR_DEV)) {
  2351. cmd->result = SAM_STAT_CHECK_CONDITION;
  2352. atapi_request_sense(qc);
  2353. return;
  2354. } else if (unlikely(err_mask)) {
  2355. /* FIXME: not quite right; we don't want the
  2356. * translation of taskfile registers into
  2357. * a sense descriptors, since that's only
  2358. * correct for ATA, not ATAPI
  2359. */
  2360. ata_gen_passthru_sense(qc);
  2361. } else {
  2362. u8 *scsicmd = cmd->cmnd;
  2363. if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
  2364. unsigned long flags;
  2365. u8 *buf;
  2366. buf = ata_scsi_rbuf_get(cmd, true, &flags);
  2367. /* ATAPI devices typically report zero for their SCSI version,
  2368. * and sometimes deviate from the spec WRT response data
  2369. * format. If SCSI version is reported as zero like normal,
  2370. * then we make the following fixups: 1) Fake MMC-5 version,
  2371. * to indicate to the Linux scsi midlayer this is a modern
  2372. * device. 2) Ensure response data format / ATAPI information
  2373. * are always correct.
  2374. */
  2375. if (buf[2] == 0) {
  2376. buf[2] = 0x5;
  2377. buf[3] = 0x32;
  2378. }
  2379. ata_scsi_rbuf_put(cmd, true, &flags);
  2380. }
  2381. cmd->result = SAM_STAT_GOOD;
  2382. }
  2383. qc->scsidone(cmd);
  2384. ata_qc_free(qc);
  2385. }
  2386. /**
  2387. * atapi_xlat - Initialize PACKET taskfile
  2388. * @qc: command structure to be initialized
  2389. *
  2390. * LOCKING:
  2391. * spin_lock_irqsave(host lock)
  2392. *
  2393. * RETURNS:
  2394. * Zero on success, non-zero on failure.
  2395. */
  2396. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2397. {
  2398. struct scsi_cmnd *scmd = qc->scsicmd;
  2399. struct ata_device *dev = qc->dev;
  2400. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2401. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2402. unsigned int nbytes;
  2403. memset(qc->cdb, 0, dev->cdb_len);
  2404. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2405. qc->complete_fn = atapi_qc_complete;
  2406. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2407. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2408. qc->tf.flags |= ATA_TFLAG_WRITE;
  2409. DPRINTK("direction: write\n");
  2410. }
  2411. qc->tf.command = ATA_CMD_PACKET;
  2412. ata_qc_set_pc_nbytes(qc);
  2413. /* check whether ATAPI DMA is safe */
  2414. if (!nodata && !using_pio && atapi_check_dma(qc))
  2415. using_pio = 1;
  2416. /* Some controller variants snoop this value for Packet
  2417. * transfers to do state machine and FIFO management. Thus we
  2418. * want to set it properly, and for DMA where it is
  2419. * effectively meaningless.
  2420. */
  2421. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2422. /* Most ATAPI devices which honor transfer chunk size don't
  2423. * behave according to the spec when odd chunk size which
  2424. * matches the transfer length is specified. If the number of
  2425. * bytes to transfer is 2n+1. According to the spec, what
  2426. * should happen is to indicate that 2n+1 is going to be
  2427. * transferred and transfer 2n+2 bytes where the last byte is
  2428. * padding.
  2429. *
  2430. * In practice, this doesn't happen. ATAPI devices first
  2431. * indicate and transfer 2n bytes and then indicate and
  2432. * transfer 2 bytes where the last byte is padding.
  2433. *
  2434. * This inconsistency confuses several controllers which
  2435. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2436. * These controllers use actual number of transferred bytes to
  2437. * update DMA poitner and transfer of 4n+2 bytes make those
  2438. * controller push DMA pointer by 4n+4 bytes because SATA data
  2439. * FISes are aligned to 4 bytes. This causes data corruption
  2440. * and buffer overrun.
  2441. *
  2442. * Always setting nbytes to even number solves this problem
  2443. * because then ATAPI devices don't have to split data at 2n
  2444. * boundaries.
  2445. */
  2446. if (nbytes & 0x1)
  2447. nbytes++;
  2448. qc->tf.lbam = (nbytes & 0xFF);
  2449. qc->tf.lbah = (nbytes >> 8);
  2450. if (nodata)
  2451. qc->tf.protocol = ATAPI_PROT_NODATA;
  2452. else if (using_pio)
  2453. qc->tf.protocol = ATAPI_PROT_PIO;
  2454. else {
  2455. /* DMA data xfer */
  2456. qc->tf.protocol = ATAPI_PROT_DMA;
  2457. qc->tf.feature |= ATAPI_PKT_DMA;
  2458. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2459. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2460. /* some SATA bridges need us to indicate data xfer direction */
  2461. qc->tf.feature |= ATAPI_DMADIR;
  2462. }
  2463. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2464. as ATAPI tape drives don't get this right otherwise */
  2465. return 0;
  2466. }
  2467. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2468. {
  2469. if (!sata_pmp_attached(ap)) {
  2470. if (likely(devno < ata_link_max_devices(&ap->link)))
  2471. return &ap->link.device[devno];
  2472. } else {
  2473. if (likely(devno < ap->nr_pmp_links))
  2474. return &ap->pmp_link[devno].device[0];
  2475. }
  2476. return NULL;
  2477. }
  2478. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2479. const struct scsi_device *scsidev)
  2480. {
  2481. int devno;
  2482. /* skip commands not addressed to targets we simulate */
  2483. if (!sata_pmp_attached(ap)) {
  2484. if (unlikely(scsidev->channel || scsidev->lun))
  2485. return NULL;
  2486. devno = scsidev->id;
  2487. } else {
  2488. if (unlikely(scsidev->id || scsidev->lun))
  2489. return NULL;
  2490. devno = scsidev->channel;
  2491. }
  2492. return ata_find_dev(ap, devno);
  2493. }
  2494. /**
  2495. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2496. * @ap: ATA port to which the device is attached
  2497. * @scsidev: SCSI device from which we derive the ATA device
  2498. *
  2499. * Given various information provided in struct scsi_cmnd,
  2500. * map that onto an ATA bus, and using that mapping
  2501. * determine which ata_device is associated with the
  2502. * SCSI command to be sent.
  2503. *
  2504. * LOCKING:
  2505. * spin_lock_irqsave(host lock)
  2506. *
  2507. * RETURNS:
  2508. * Associated ATA device, or %NULL if not found.
  2509. */
  2510. static struct ata_device *
  2511. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2512. {
  2513. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2514. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2515. return NULL;
  2516. return dev;
  2517. }
  2518. /*
  2519. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2520. * @byte1: Byte 1 from pass-thru CDB.
  2521. *
  2522. * RETURNS:
  2523. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2524. */
  2525. static u8
  2526. ata_scsi_map_proto(u8 byte1)
  2527. {
  2528. switch((byte1 & 0x1e) >> 1) {
  2529. case 3: /* Non-data */
  2530. return ATA_PROT_NODATA;
  2531. case 6: /* DMA */
  2532. case 10: /* UDMA Data-in */
  2533. case 11: /* UDMA Data-Out */
  2534. return ATA_PROT_DMA;
  2535. case 4: /* PIO Data-in */
  2536. case 5: /* PIO Data-out */
  2537. return ATA_PROT_PIO;
  2538. case 0: /* Hard Reset */
  2539. case 1: /* SRST */
  2540. case 8: /* Device Diagnostic */
  2541. case 9: /* Device Reset */
  2542. case 7: /* DMA Queued */
  2543. case 12: /* FPDMA */
  2544. case 15: /* Return Response Info */
  2545. default: /* Reserved */
  2546. break;
  2547. }
  2548. return ATA_PROT_UNKNOWN;
  2549. }
  2550. /**
  2551. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2552. * @qc: command structure to be initialized
  2553. *
  2554. * Handles either 12 or 16-byte versions of the CDB.
  2555. *
  2556. * RETURNS:
  2557. * Zero on success, non-zero on failure.
  2558. */
  2559. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2560. {
  2561. struct ata_taskfile *tf = &(qc->tf);
  2562. struct scsi_cmnd *scmd = qc->scsicmd;
  2563. struct ata_device *dev = qc->dev;
  2564. const u8 *cdb = scmd->cmnd;
  2565. if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
  2566. goto invalid_fld;
  2567. /*
  2568. * 12 and 16 byte CDBs use different offsets to
  2569. * provide the various register values.
  2570. */
  2571. if (cdb[0] == ATA_16) {
  2572. /*
  2573. * 16-byte CDB - may contain extended commands.
  2574. *
  2575. * If that is the case, copy the upper byte register values.
  2576. */
  2577. if (cdb[1] & 0x01) {
  2578. tf->hob_feature = cdb[3];
  2579. tf->hob_nsect = cdb[5];
  2580. tf->hob_lbal = cdb[7];
  2581. tf->hob_lbam = cdb[9];
  2582. tf->hob_lbah = cdb[11];
  2583. tf->flags |= ATA_TFLAG_LBA48;
  2584. } else
  2585. tf->flags &= ~ATA_TFLAG_LBA48;
  2586. /*
  2587. * Always copy low byte, device and command registers.
  2588. */
  2589. tf->feature = cdb[4];
  2590. tf->nsect = cdb[6];
  2591. tf->lbal = cdb[8];
  2592. tf->lbam = cdb[10];
  2593. tf->lbah = cdb[12];
  2594. tf->device = cdb[13];
  2595. tf->command = cdb[14];
  2596. } else {
  2597. /*
  2598. * 12-byte CDB - incapable of extended commands.
  2599. */
  2600. tf->flags &= ~ATA_TFLAG_LBA48;
  2601. tf->feature = cdb[3];
  2602. tf->nsect = cdb[4];
  2603. tf->lbal = cdb[5];
  2604. tf->lbam = cdb[6];
  2605. tf->lbah = cdb[7];
  2606. tf->device = cdb[8];
  2607. tf->command = cdb[9];
  2608. }
  2609. /* enforce correct master/slave bit */
  2610. tf->device = dev->devno ?
  2611. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2612. switch (tf->command) {
  2613. /* READ/WRITE LONG use a non-standard sect_size */
  2614. case ATA_CMD_READ_LONG:
  2615. case ATA_CMD_READ_LONG_ONCE:
  2616. case ATA_CMD_WRITE_LONG:
  2617. case ATA_CMD_WRITE_LONG_ONCE:
  2618. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
  2619. goto invalid_fld;
  2620. qc->sect_size = scsi_bufflen(scmd);
  2621. break;
  2622. /* commands using reported Logical Block size (e.g. 512 or 4K) */
  2623. case ATA_CMD_CFA_WRITE_NE:
  2624. case ATA_CMD_CFA_TRANS_SECT:
  2625. case ATA_CMD_CFA_WRITE_MULT_NE:
  2626. /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
  2627. case ATA_CMD_READ:
  2628. case ATA_CMD_READ_EXT:
  2629. case ATA_CMD_READ_QUEUED:
  2630. /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
  2631. case ATA_CMD_FPDMA_READ:
  2632. case ATA_CMD_READ_MULTI:
  2633. case ATA_CMD_READ_MULTI_EXT:
  2634. case ATA_CMD_PIO_READ:
  2635. case ATA_CMD_PIO_READ_EXT:
  2636. case ATA_CMD_READ_STREAM_DMA_EXT:
  2637. case ATA_CMD_READ_STREAM_EXT:
  2638. case ATA_CMD_VERIFY:
  2639. case ATA_CMD_VERIFY_EXT:
  2640. case ATA_CMD_WRITE:
  2641. case ATA_CMD_WRITE_EXT:
  2642. case ATA_CMD_WRITE_FUA_EXT:
  2643. case ATA_CMD_WRITE_QUEUED:
  2644. case ATA_CMD_WRITE_QUEUED_FUA_EXT:
  2645. case ATA_CMD_FPDMA_WRITE:
  2646. case ATA_CMD_WRITE_MULTI:
  2647. case ATA_CMD_WRITE_MULTI_EXT:
  2648. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  2649. case ATA_CMD_PIO_WRITE:
  2650. case ATA_CMD_PIO_WRITE_EXT:
  2651. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  2652. case ATA_CMD_WRITE_STREAM_EXT:
  2653. qc->sect_size = scmd->device->sector_size;
  2654. break;
  2655. /* Everything else uses 512 byte "sectors" */
  2656. default:
  2657. qc->sect_size = ATA_SECT_SIZE;
  2658. }
  2659. /*
  2660. * Set flags so that all registers will be written, pass on
  2661. * write indication (used for PIO/DMA setup), result TF is
  2662. * copied back and we don't whine too much about its failure.
  2663. */
  2664. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2665. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2666. tf->flags |= ATA_TFLAG_WRITE;
  2667. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2668. /*
  2669. * Set transfer length.
  2670. *
  2671. * TODO: find out if we need to do more here to
  2672. * cover scatter/gather case.
  2673. */
  2674. ata_qc_set_pc_nbytes(qc);
  2675. /* We may not issue DMA commands if no DMA mode is set */
  2676. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
  2677. goto invalid_fld;
  2678. /* sanity check for pio multi commands */
  2679. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
  2680. goto invalid_fld;
  2681. if (is_multi_taskfile(tf)) {
  2682. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2683. /* compare the passed through multi_count
  2684. * with the cached multi_count of libata
  2685. */
  2686. if (multi_count != dev->multi_count)
  2687. ata_dev_warn(dev, "invalid multi_count %u ignored\n",
  2688. multi_count);
  2689. }
  2690. /*
  2691. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2692. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2693. * by an update to hardware-specific registers for each
  2694. * controller (i.e. the reason for ->set_piomode(),
  2695. * ->set_dmamode(), and ->post_set_mode() hooks).
  2696. */
  2697. if (tf->command == ATA_CMD_SET_FEATURES &&
  2698. tf->feature == SETFEATURES_XFER)
  2699. goto invalid_fld;
  2700. /*
  2701. * Filter TPM commands by default. These provide an
  2702. * essentially uncontrolled encrypted "back door" between
  2703. * applications and the disk. Set libata.allow_tpm=1 if you
  2704. * have a real reason for wanting to use them. This ensures
  2705. * that installed software cannot easily mess stuff up without
  2706. * user intent. DVR type users will probably ship with this enabled
  2707. * for movie content management.
  2708. *
  2709. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2710. * for this and should do in future but that it is not sufficient as
  2711. * DCS is an optional feature set. Thus we also do the software filter
  2712. * so that we comply with the TC consortium stated goal that the user
  2713. * can turn off TC features of their system.
  2714. */
  2715. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
  2716. goto invalid_fld;
  2717. return 0;
  2718. invalid_fld:
  2719. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2720. /* "Invalid field in cdb" */
  2721. return 1;
  2722. }
  2723. static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
  2724. {
  2725. struct ata_taskfile *tf = &qc->tf;
  2726. struct scsi_cmnd *scmd = qc->scsicmd;
  2727. struct ata_device *dev = qc->dev;
  2728. const u8 *cdb = scmd->cmnd;
  2729. u64 block;
  2730. u32 n_block;
  2731. u32 size;
  2732. void *buf;
  2733. /* we may not issue DMA commands if no DMA mode is set */
  2734. if (unlikely(!dev->dma_mode))
  2735. goto invalid_fld;
  2736. if (unlikely(scmd->cmd_len < 16))
  2737. goto invalid_fld;
  2738. scsi_16_lba_len(cdb, &block, &n_block);
  2739. /* for now we only support WRITE SAME with the unmap bit set */
  2740. if (unlikely(!(cdb[1] & 0x8)))
  2741. goto invalid_fld;
  2742. /*
  2743. * WRITE SAME always has a sector sized buffer as payload, this
  2744. * should never be a multiple entry S/G list.
  2745. */
  2746. if (!scsi_sg_count(scmd))
  2747. goto invalid_fld;
  2748. buf = page_address(sg_page(scsi_sglist(scmd)));
  2749. size = ata_set_lba_range_entries(buf, 512, block, n_block);
  2750. if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
  2751. /* Newer devices support queued TRIM commands */
  2752. tf->protocol = ATA_PROT_NCQ;
  2753. tf->command = ATA_CMD_FPDMA_SEND;
  2754. tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
  2755. tf->nsect = qc->tag << 3;
  2756. tf->hob_feature = (size / 512) >> 8;
  2757. tf->feature = size / 512;
  2758. tf->auxiliary = 1;
  2759. } else {
  2760. tf->protocol = ATA_PROT_DMA;
  2761. tf->hob_feature = 0;
  2762. tf->feature = ATA_DSM_TRIM;
  2763. tf->hob_nsect = (size / 512) >> 8;
  2764. tf->nsect = size / 512;
  2765. tf->command = ATA_CMD_DSM;
  2766. }
  2767. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
  2768. ATA_TFLAG_WRITE;
  2769. ata_qc_set_pc_nbytes(qc);
  2770. return 0;
  2771. invalid_fld:
  2772. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2773. /* "Invalid field in cdb" */
  2774. return 1;
  2775. }
  2776. /**
  2777. * ata_mselect_caching - Simulate MODE SELECT for caching info page
  2778. * @qc: Storage for translated ATA taskfile
  2779. * @buf: input buffer
  2780. * @len: number of valid bytes in the input buffer
  2781. *
  2782. * Prepare a taskfile to modify caching information for the device.
  2783. *
  2784. * LOCKING:
  2785. * None.
  2786. */
  2787. static int ata_mselect_caching(struct ata_queued_cmd *qc,
  2788. const u8 *buf, int len)
  2789. {
  2790. struct ata_taskfile *tf = &qc->tf;
  2791. struct ata_device *dev = qc->dev;
  2792. char mpage[CACHE_MPAGE_LEN];
  2793. u8 wce;
  2794. /*
  2795. * The first two bytes of def_cache_mpage are a header, so offsets
  2796. * in mpage are off by 2 compared to buf. Same for len.
  2797. */
  2798. if (len != CACHE_MPAGE_LEN - 2)
  2799. return -EINVAL;
  2800. wce = buf[0] & (1 << 2);
  2801. /*
  2802. * Check that read-only bits are not modified.
  2803. */
  2804. ata_msense_caching(dev->id, mpage, false);
  2805. mpage[2] &= ~(1 << 2);
  2806. mpage[2] |= wce;
  2807. if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0)
  2808. return -EINVAL;
  2809. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  2810. tf->protocol = ATA_PROT_NODATA;
  2811. tf->nsect = 0;
  2812. tf->command = ATA_CMD_SET_FEATURES;
  2813. tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
  2814. return 0;
  2815. }
  2816. /**
  2817. * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
  2818. * @qc: Storage for translated ATA taskfile
  2819. *
  2820. * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
  2821. * Assume this is invoked for direct access devices (e.g. disks) only.
  2822. * There should be no block descriptor for other device types.
  2823. *
  2824. * LOCKING:
  2825. * spin_lock_irqsave(host lock)
  2826. */
  2827. static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
  2828. {
  2829. struct scsi_cmnd *scmd = qc->scsicmd;
  2830. const u8 *cdb = scmd->cmnd;
  2831. const u8 *p;
  2832. u8 pg, spg;
  2833. unsigned six_byte, pg_len, hdr_len, bd_len;
  2834. int len;
  2835. VPRINTK("ENTER\n");
  2836. six_byte = (cdb[0] == MODE_SELECT);
  2837. if (six_byte) {
  2838. if (scmd->cmd_len < 5)
  2839. goto invalid_fld;
  2840. len = cdb[4];
  2841. hdr_len = 4;
  2842. } else {
  2843. if (scmd->cmd_len < 9)
  2844. goto invalid_fld;
  2845. len = (cdb[7] << 8) + cdb[8];
  2846. hdr_len = 8;
  2847. }
  2848. /* We only support PF=1, SP=0. */
  2849. if ((cdb[1] & 0x11) != 0x10)
  2850. goto invalid_fld;
  2851. /* Test early for possible overrun. */
  2852. if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
  2853. goto invalid_param_len;
  2854. p = page_address(sg_page(scsi_sglist(scmd)));
  2855. /* Move past header and block descriptors. */
  2856. if (len < hdr_len)
  2857. goto invalid_param_len;
  2858. if (six_byte)
  2859. bd_len = p[3];
  2860. else
  2861. bd_len = (p[6] << 8) + p[7];
  2862. len -= hdr_len;
  2863. p += hdr_len;
  2864. if (len < bd_len)
  2865. goto invalid_param_len;
  2866. if (bd_len != 0 && bd_len != 8)
  2867. goto invalid_param;
  2868. len -= bd_len;
  2869. p += bd_len;
  2870. if (len == 0)
  2871. goto skip;
  2872. /* Parse both possible formats for the mode page headers. */
  2873. pg = p[0] & 0x3f;
  2874. if (p[0] & 0x40) {
  2875. if (len < 4)
  2876. goto invalid_param_len;
  2877. spg = p[1];
  2878. pg_len = (p[2] << 8) | p[3];
  2879. p += 4;
  2880. len -= 4;
  2881. } else {
  2882. if (len < 2)
  2883. goto invalid_param_len;
  2884. spg = 0;
  2885. pg_len = p[1];
  2886. p += 2;
  2887. len -= 2;
  2888. }
  2889. /*
  2890. * No mode subpages supported (yet) but asking for _all_
  2891. * subpages may be valid
  2892. */
  2893. if (spg && (spg != ALL_SUB_MPAGES))
  2894. goto invalid_param;
  2895. if (pg_len > len)
  2896. goto invalid_param_len;
  2897. switch (pg) {
  2898. case CACHE_MPAGE:
  2899. if (ata_mselect_caching(qc, p, pg_len) < 0)
  2900. goto invalid_param;
  2901. break;
  2902. default: /* invalid page code */
  2903. goto invalid_param;
  2904. }
  2905. /*
  2906. * Only one page has changeable data, so we only support setting one
  2907. * page at a time.
  2908. */
  2909. if (len > pg_len)
  2910. goto invalid_param;
  2911. return 0;
  2912. invalid_fld:
  2913. /* "Invalid field in CDB" */
  2914. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2915. return 1;
  2916. invalid_param:
  2917. /* "Invalid field in parameter list" */
  2918. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x26, 0x0);
  2919. return 1;
  2920. invalid_param_len:
  2921. /* "Parameter list length error" */
  2922. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  2923. return 1;
  2924. skip:
  2925. scmd->result = SAM_STAT_GOOD;
  2926. return 1;
  2927. }
  2928. /**
  2929. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  2930. * @dev: ATA device
  2931. * @cmd: SCSI command opcode to consider
  2932. *
  2933. * Look up the SCSI command given, and determine whether the
  2934. * SCSI command is to be translated or simulated.
  2935. *
  2936. * RETURNS:
  2937. * Pointer to translation function if possible, %NULL if not.
  2938. */
  2939. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2940. {
  2941. switch (cmd) {
  2942. case READ_6:
  2943. case READ_10:
  2944. case READ_16:
  2945. case WRITE_6:
  2946. case WRITE_10:
  2947. case WRITE_16:
  2948. return ata_scsi_rw_xlat;
  2949. case WRITE_SAME_16:
  2950. return ata_scsi_write_same_xlat;
  2951. case SYNCHRONIZE_CACHE:
  2952. if (ata_try_flush_cache(dev))
  2953. return ata_scsi_flush_xlat;
  2954. break;
  2955. case VERIFY:
  2956. case VERIFY_16:
  2957. return ata_scsi_verify_xlat;
  2958. case ATA_12:
  2959. case ATA_16:
  2960. return ata_scsi_pass_thru;
  2961. case MODE_SELECT:
  2962. case MODE_SELECT_10:
  2963. return ata_scsi_mode_select_xlat;
  2964. break;
  2965. case START_STOP:
  2966. return ata_scsi_start_stop_xlat;
  2967. }
  2968. return NULL;
  2969. }
  2970. /**
  2971. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2972. * @ap: ATA port to which the command was being sent
  2973. * @cmd: SCSI command to dump
  2974. *
  2975. * Prints the contents of a SCSI command via printk().
  2976. */
  2977. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2978. struct scsi_cmnd *cmd)
  2979. {
  2980. #ifdef ATA_DEBUG
  2981. struct scsi_device *scsidev = cmd->device;
  2982. u8 *scsicmd = cmd->cmnd;
  2983. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2984. ap->print_id,
  2985. scsidev->channel, scsidev->id, scsidev->lun,
  2986. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2987. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2988. scsicmd[8]);
  2989. #endif
  2990. }
  2991. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
  2992. struct ata_device *dev)
  2993. {
  2994. u8 scsi_op = scmd->cmnd[0];
  2995. ata_xlat_func_t xlat_func;
  2996. int rc = 0;
  2997. if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
  2998. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  2999. goto bad_cdb_len;
  3000. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3001. } else {
  3002. if (unlikely(!scmd->cmd_len))
  3003. goto bad_cdb_len;
  3004. xlat_func = NULL;
  3005. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  3006. /* relay SCSI command to ATAPI device */
  3007. int len = COMMAND_SIZE(scsi_op);
  3008. if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
  3009. goto bad_cdb_len;
  3010. xlat_func = atapi_xlat;
  3011. } else {
  3012. /* ATA_16 passthru, treat as an ATA command */
  3013. if (unlikely(scmd->cmd_len > 16))
  3014. goto bad_cdb_len;
  3015. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3016. }
  3017. }
  3018. if (xlat_func)
  3019. rc = ata_scsi_translate(dev, scmd, xlat_func);
  3020. else
  3021. ata_scsi_simulate(dev, scmd);
  3022. return rc;
  3023. bad_cdb_len:
  3024. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  3025. scmd->cmd_len, scsi_op, dev->cdb_len);
  3026. scmd->result = DID_ERROR << 16;
  3027. scmd->scsi_done(scmd);
  3028. return 0;
  3029. }
  3030. /**
  3031. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  3032. * @shost: SCSI host of command to be sent
  3033. * @cmd: SCSI command to be sent
  3034. *
  3035. * In some cases, this function translates SCSI commands into
  3036. * ATA taskfiles, and queues the taskfiles to be sent to
  3037. * hardware. In other cases, this function simulates a
  3038. * SCSI device by evaluating and responding to certain
  3039. * SCSI commands. This creates the overall effect of
  3040. * ATA and ATAPI devices appearing as SCSI devices.
  3041. *
  3042. * LOCKING:
  3043. * ATA host lock
  3044. *
  3045. * RETURNS:
  3046. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3047. * 0 otherwise.
  3048. */
  3049. int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  3050. {
  3051. struct ata_port *ap;
  3052. struct ata_device *dev;
  3053. struct scsi_device *scsidev = cmd->device;
  3054. int rc = 0;
  3055. unsigned long irq_flags;
  3056. ap = ata_shost_to_port(shost);
  3057. spin_lock_irqsave(ap->lock, irq_flags);
  3058. ata_scsi_dump_cdb(ap, cmd);
  3059. dev = ata_scsi_find_dev(ap, scsidev);
  3060. if (likely(dev))
  3061. rc = __ata_scsi_queuecmd(cmd, dev);
  3062. else {
  3063. cmd->result = (DID_BAD_TARGET << 16);
  3064. cmd->scsi_done(cmd);
  3065. }
  3066. spin_unlock_irqrestore(ap->lock, irq_flags);
  3067. return rc;
  3068. }
  3069. /**
  3070. * ata_scsi_simulate - simulate SCSI command on ATA device
  3071. * @dev: the target device
  3072. * @cmd: SCSI command being sent to device.
  3073. *
  3074. * Interprets and directly executes a select list of SCSI commands
  3075. * that can be handled internally.
  3076. *
  3077. * LOCKING:
  3078. * spin_lock_irqsave(host lock)
  3079. */
  3080. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
  3081. {
  3082. struct ata_scsi_args args;
  3083. const u8 *scsicmd = cmd->cmnd;
  3084. u8 tmp8;
  3085. args.dev = dev;
  3086. args.id = dev->id;
  3087. args.cmd = cmd;
  3088. args.done = cmd->scsi_done;
  3089. switch(scsicmd[0]) {
  3090. /* TODO: worth improving? */
  3091. case FORMAT_UNIT:
  3092. ata_scsi_invalid_field(cmd);
  3093. break;
  3094. case INQUIRY:
  3095. if (scsicmd[1] & 2) /* is CmdDt set? */
  3096. ata_scsi_invalid_field(cmd);
  3097. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  3098. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  3099. else switch (scsicmd[2]) {
  3100. case 0x00:
  3101. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  3102. break;
  3103. case 0x80:
  3104. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  3105. break;
  3106. case 0x83:
  3107. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  3108. break;
  3109. case 0x89:
  3110. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  3111. break;
  3112. case 0xb0:
  3113. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
  3114. break;
  3115. case 0xb1:
  3116. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  3117. break;
  3118. case 0xb2:
  3119. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
  3120. break;
  3121. default:
  3122. ata_scsi_invalid_field(cmd);
  3123. break;
  3124. }
  3125. break;
  3126. case MODE_SENSE:
  3127. case MODE_SENSE_10:
  3128. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  3129. break;
  3130. case READ_CAPACITY:
  3131. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3132. break;
  3133. case SERVICE_ACTION_IN_16:
  3134. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  3135. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3136. else
  3137. ata_scsi_invalid_field(cmd);
  3138. break;
  3139. case REPORT_LUNS:
  3140. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  3141. break;
  3142. case REQUEST_SENSE:
  3143. ata_scsi_set_sense(cmd, 0, 0, 0);
  3144. cmd->result = (DRIVER_SENSE << 24);
  3145. cmd->scsi_done(cmd);
  3146. break;
  3147. /* if we reach this, then writeback caching is disabled,
  3148. * turning this into a no-op.
  3149. */
  3150. case SYNCHRONIZE_CACHE:
  3151. /* fall through */
  3152. /* no-op's, complete with success */
  3153. case REZERO_UNIT:
  3154. case SEEK_6:
  3155. case SEEK_10:
  3156. case TEST_UNIT_READY:
  3157. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  3158. break;
  3159. case SEND_DIAGNOSTIC:
  3160. tmp8 = scsicmd[1] & ~(1 << 3);
  3161. if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
  3162. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  3163. else
  3164. ata_scsi_invalid_field(cmd);
  3165. break;
  3166. /* all other commands */
  3167. default:
  3168. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  3169. /* "Invalid command operation code" */
  3170. cmd->scsi_done(cmd);
  3171. break;
  3172. }
  3173. }
  3174. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  3175. {
  3176. int i, rc;
  3177. for (i = 0; i < host->n_ports; i++) {
  3178. struct ata_port *ap = host->ports[i];
  3179. struct Scsi_Host *shost;
  3180. rc = -ENOMEM;
  3181. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  3182. if (!shost)
  3183. goto err_alloc;
  3184. shost->eh_noresume = 1;
  3185. *(struct ata_port **)&shost->hostdata[0] = ap;
  3186. ap->scsi_host = shost;
  3187. shost->transportt = ata_scsi_transport_template;
  3188. shost->unique_id = ap->print_id;
  3189. shost->max_id = 16;
  3190. shost->max_lun = 1;
  3191. shost->max_channel = 1;
  3192. shost->max_cmd_len = 16;
  3193. shost->no_write_same = 1;
  3194. /* Schedule policy is determined by ->qc_defer()
  3195. * callback and it needs to see every deferred qc.
  3196. * Set host_blocked to 1 to prevent SCSI midlayer from
  3197. * automatically deferring requests.
  3198. */
  3199. shost->max_host_blocked = 1;
  3200. if (scsi_init_shared_tag_map(shost, host->n_tags))
  3201. goto err_add;
  3202. rc = scsi_add_host_with_dma(ap->scsi_host,
  3203. &ap->tdev, ap->host->dev);
  3204. if (rc)
  3205. goto err_add;
  3206. }
  3207. return 0;
  3208. err_add:
  3209. scsi_host_put(host->ports[i]->scsi_host);
  3210. err_alloc:
  3211. while (--i >= 0) {
  3212. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  3213. scsi_remove_host(shost);
  3214. scsi_host_put(shost);
  3215. }
  3216. return rc;
  3217. }
  3218. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  3219. {
  3220. int tries = 5;
  3221. struct ata_device *last_failed_dev = NULL;
  3222. struct ata_link *link;
  3223. struct ata_device *dev;
  3224. repeat:
  3225. ata_for_each_link(link, ap, EDGE) {
  3226. ata_for_each_dev(dev, link, ENABLED) {
  3227. struct scsi_device *sdev;
  3228. int channel = 0, id = 0;
  3229. if (dev->sdev)
  3230. continue;
  3231. if (ata_is_host_link(link))
  3232. id = dev->devno;
  3233. else
  3234. channel = link->pmp;
  3235. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  3236. NULL);
  3237. if (!IS_ERR(sdev)) {
  3238. dev->sdev = sdev;
  3239. scsi_device_put(sdev);
  3240. } else {
  3241. dev->sdev = NULL;
  3242. }
  3243. }
  3244. }
  3245. /* If we scanned while EH was in progress or allocation
  3246. * failure occurred, scan would have failed silently. Check
  3247. * whether all devices are attached.
  3248. */
  3249. ata_for_each_link(link, ap, EDGE) {
  3250. ata_for_each_dev(dev, link, ENABLED) {
  3251. if (!dev->sdev)
  3252. goto exit_loop;
  3253. }
  3254. }
  3255. exit_loop:
  3256. if (!link)
  3257. return;
  3258. /* we're missing some SCSI devices */
  3259. if (sync) {
  3260. /* If caller requested synchrnous scan && we've made
  3261. * any progress, sleep briefly and repeat.
  3262. */
  3263. if (dev != last_failed_dev) {
  3264. msleep(100);
  3265. last_failed_dev = dev;
  3266. goto repeat;
  3267. }
  3268. /* We might be failing to detect boot device, give it
  3269. * a few more chances.
  3270. */
  3271. if (--tries) {
  3272. msleep(100);
  3273. goto repeat;
  3274. }
  3275. ata_port_err(ap,
  3276. "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
  3277. }
  3278. queue_delayed_work(system_long_wq, &ap->hotplug_task,
  3279. round_jiffies_relative(HZ));
  3280. }
  3281. /**
  3282. * ata_scsi_offline_dev - offline attached SCSI device
  3283. * @dev: ATA device to offline attached SCSI device for
  3284. *
  3285. * This function is called from ata_eh_hotplug() and responsible
  3286. * for taking the SCSI device attached to @dev offline. This
  3287. * function is called with host lock which protects dev->sdev
  3288. * against clearing.
  3289. *
  3290. * LOCKING:
  3291. * spin_lock_irqsave(host lock)
  3292. *
  3293. * RETURNS:
  3294. * 1 if attached SCSI device exists, 0 otherwise.
  3295. */
  3296. int ata_scsi_offline_dev(struct ata_device *dev)
  3297. {
  3298. if (dev->sdev) {
  3299. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  3300. return 1;
  3301. }
  3302. return 0;
  3303. }
  3304. /**
  3305. * ata_scsi_remove_dev - remove attached SCSI device
  3306. * @dev: ATA device to remove attached SCSI device for
  3307. *
  3308. * This function is called from ata_eh_scsi_hotplug() and
  3309. * responsible for removing the SCSI device attached to @dev.
  3310. *
  3311. * LOCKING:
  3312. * Kernel thread context (may sleep).
  3313. */
  3314. static void ata_scsi_remove_dev(struct ata_device *dev)
  3315. {
  3316. struct ata_port *ap = dev->link->ap;
  3317. struct scsi_device *sdev;
  3318. unsigned long flags;
  3319. /* Alas, we need to grab scan_mutex to ensure SCSI device
  3320. * state doesn't change underneath us and thus
  3321. * scsi_device_get() always succeeds. The mutex locking can
  3322. * be removed if there is __scsi_device_get() interface which
  3323. * increments reference counts regardless of device state.
  3324. */
  3325. mutex_lock(&ap->scsi_host->scan_mutex);
  3326. spin_lock_irqsave(ap->lock, flags);
  3327. /* clearing dev->sdev is protected by host lock */
  3328. sdev = dev->sdev;
  3329. dev->sdev = NULL;
  3330. if (sdev) {
  3331. /* If user initiated unplug races with us, sdev can go
  3332. * away underneath us after the host lock and
  3333. * scan_mutex are released. Hold onto it.
  3334. */
  3335. if (scsi_device_get(sdev) == 0) {
  3336. /* The following ensures the attached sdev is
  3337. * offline on return from ata_scsi_offline_dev()
  3338. * regardless it wins or loses the race
  3339. * against this function.
  3340. */
  3341. scsi_device_set_state(sdev, SDEV_OFFLINE);
  3342. } else {
  3343. WARN_ON(1);
  3344. sdev = NULL;
  3345. }
  3346. }
  3347. spin_unlock_irqrestore(ap->lock, flags);
  3348. mutex_unlock(&ap->scsi_host->scan_mutex);
  3349. if (sdev) {
  3350. ata_dev_info(dev, "detaching (SCSI %s)\n",
  3351. dev_name(&sdev->sdev_gendev));
  3352. scsi_remove_device(sdev);
  3353. scsi_device_put(sdev);
  3354. }
  3355. }
  3356. static void ata_scsi_handle_link_detach(struct ata_link *link)
  3357. {
  3358. struct ata_port *ap = link->ap;
  3359. struct ata_device *dev;
  3360. ata_for_each_dev(dev, link, ALL) {
  3361. unsigned long flags;
  3362. if (!(dev->flags & ATA_DFLAG_DETACHED))
  3363. continue;
  3364. spin_lock_irqsave(ap->lock, flags);
  3365. dev->flags &= ~ATA_DFLAG_DETACHED;
  3366. spin_unlock_irqrestore(ap->lock, flags);
  3367. if (zpodd_dev_enabled(dev))
  3368. zpodd_exit(dev);
  3369. ata_scsi_remove_dev(dev);
  3370. }
  3371. }
  3372. /**
  3373. * ata_scsi_media_change_notify - send media change event
  3374. * @dev: Pointer to the disk device with media change event
  3375. *
  3376. * Tell the block layer to send a media change notification
  3377. * event.
  3378. *
  3379. * LOCKING:
  3380. * spin_lock_irqsave(host lock)
  3381. */
  3382. void ata_scsi_media_change_notify(struct ata_device *dev)
  3383. {
  3384. if (dev->sdev)
  3385. sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
  3386. GFP_ATOMIC);
  3387. }
  3388. /**
  3389. * ata_scsi_hotplug - SCSI part of hotplug
  3390. * @work: Pointer to ATA port to perform SCSI hotplug on
  3391. *
  3392. * Perform SCSI part of hotplug. It's executed from a separate
  3393. * workqueue after EH completes. This is necessary because SCSI
  3394. * hot plugging requires working EH and hot unplugging is
  3395. * synchronized with hot plugging with a mutex.
  3396. *
  3397. * LOCKING:
  3398. * Kernel thread context (may sleep).
  3399. */
  3400. void ata_scsi_hotplug(struct work_struct *work)
  3401. {
  3402. struct ata_port *ap =
  3403. container_of(work, struct ata_port, hotplug_task.work);
  3404. int i;
  3405. if (ap->pflags & ATA_PFLAG_UNLOADING) {
  3406. DPRINTK("ENTER/EXIT - unloading\n");
  3407. return;
  3408. }
  3409. /*
  3410. * XXX - UGLY HACK
  3411. *
  3412. * The block layer suspend/resume path is fundamentally broken due
  3413. * to freezable kthreads and workqueue and may deadlock if a block
  3414. * device gets removed while resume is in progress. I don't know
  3415. * what the solution is short of removing freezable kthreads and
  3416. * workqueues altogether.
  3417. *
  3418. * The following is an ugly hack to avoid kicking off device
  3419. * removal while freezer is active. This is a joke but does avoid
  3420. * this particular deadlock scenario.
  3421. *
  3422. * https://bugzilla.kernel.org/show_bug.cgi?id=62801
  3423. * http://marc.info/?l=linux-kernel&m=138695698516487
  3424. */
  3425. #ifdef CONFIG_FREEZER
  3426. while (pm_freezing)
  3427. msleep(10);
  3428. #endif
  3429. DPRINTK("ENTER\n");
  3430. mutex_lock(&ap->scsi_scan_mutex);
  3431. /* Unplug detached devices. We cannot use link iterator here
  3432. * because PMP links have to be scanned even if PMP is
  3433. * currently not attached. Iterate manually.
  3434. */
  3435. ata_scsi_handle_link_detach(&ap->link);
  3436. if (ap->pmp_link)
  3437. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  3438. ata_scsi_handle_link_detach(&ap->pmp_link[i]);
  3439. /* scan for new ones */
  3440. ata_scsi_scan_host(ap, 0);
  3441. mutex_unlock(&ap->scsi_scan_mutex);
  3442. DPRINTK("EXIT\n");
  3443. }
  3444. /**
  3445. * ata_scsi_user_scan - indication for user-initiated bus scan
  3446. * @shost: SCSI host to scan
  3447. * @channel: Channel to scan
  3448. * @id: ID to scan
  3449. * @lun: LUN to scan
  3450. *
  3451. * This function is called when user explicitly requests bus
  3452. * scan. Set probe pending flag and invoke EH.
  3453. *
  3454. * LOCKING:
  3455. * SCSI layer (we don't care)
  3456. *
  3457. * RETURNS:
  3458. * Zero.
  3459. */
  3460. int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  3461. unsigned int id, u64 lun)
  3462. {
  3463. struct ata_port *ap = ata_shost_to_port(shost);
  3464. unsigned long flags;
  3465. int devno, rc = 0;
  3466. if (!ap->ops->error_handler)
  3467. return -EOPNOTSUPP;
  3468. if (lun != SCAN_WILD_CARD && lun)
  3469. return -EINVAL;
  3470. if (!sata_pmp_attached(ap)) {
  3471. if (channel != SCAN_WILD_CARD && channel)
  3472. return -EINVAL;
  3473. devno = id;
  3474. } else {
  3475. if (id != SCAN_WILD_CARD && id)
  3476. return -EINVAL;
  3477. devno = channel;
  3478. }
  3479. spin_lock_irqsave(ap->lock, flags);
  3480. if (devno == SCAN_WILD_CARD) {
  3481. struct ata_link *link;
  3482. ata_for_each_link(link, ap, EDGE) {
  3483. struct ata_eh_info *ehi = &link->eh_info;
  3484. ehi->probe_mask |= ATA_ALL_DEVICES;
  3485. ehi->action |= ATA_EH_RESET;
  3486. }
  3487. } else {
  3488. struct ata_device *dev = ata_find_dev(ap, devno);
  3489. if (dev) {
  3490. struct ata_eh_info *ehi = &dev->link->eh_info;
  3491. ehi->probe_mask |= 1 << dev->devno;
  3492. ehi->action |= ATA_EH_RESET;
  3493. } else
  3494. rc = -EINVAL;
  3495. }
  3496. if (rc == 0) {
  3497. ata_port_schedule_eh(ap);
  3498. spin_unlock_irqrestore(ap->lock, flags);
  3499. ata_port_wait_eh(ap);
  3500. } else
  3501. spin_unlock_irqrestore(ap->lock, flags);
  3502. return rc;
  3503. }
  3504. /**
  3505. * ata_scsi_dev_rescan - initiate scsi_rescan_device()
  3506. * @work: Pointer to ATA port to perform scsi_rescan_device()
  3507. *
  3508. * After ATA pass thru (SAT) commands are executed successfully,
  3509. * libata need to propagate the changes to SCSI layer.
  3510. *
  3511. * LOCKING:
  3512. * Kernel thread context (may sleep).
  3513. */
  3514. void ata_scsi_dev_rescan(struct work_struct *work)
  3515. {
  3516. struct ata_port *ap =
  3517. container_of(work, struct ata_port, scsi_rescan_task);
  3518. struct ata_link *link;
  3519. struct ata_device *dev;
  3520. unsigned long flags;
  3521. mutex_lock(&ap->scsi_scan_mutex);
  3522. spin_lock_irqsave(ap->lock, flags);
  3523. ata_for_each_link(link, ap, EDGE) {
  3524. ata_for_each_dev(dev, link, ENABLED) {
  3525. struct scsi_device *sdev = dev->sdev;
  3526. if (!sdev)
  3527. continue;
  3528. if (scsi_device_get(sdev))
  3529. continue;
  3530. spin_unlock_irqrestore(ap->lock, flags);
  3531. scsi_rescan_device(&(sdev->sdev_gendev));
  3532. scsi_device_put(sdev);
  3533. spin_lock_irqsave(ap->lock, flags);
  3534. }
  3535. }
  3536. spin_unlock_irqrestore(ap->lock, flags);
  3537. mutex_unlock(&ap->scsi_scan_mutex);
  3538. }
  3539. /**
  3540. * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
  3541. * @host: ATA host container for all SAS ports
  3542. * @port_info: Information from low-level host driver
  3543. * @shost: SCSI host that the scsi device is attached to
  3544. *
  3545. * LOCKING:
  3546. * PCI/etc. bus probe sem.
  3547. *
  3548. * RETURNS:
  3549. * ata_port pointer on success / NULL on failure.
  3550. */
  3551. struct ata_port *ata_sas_port_alloc(struct ata_host *host,
  3552. struct ata_port_info *port_info,
  3553. struct Scsi_Host *shost)
  3554. {
  3555. struct ata_port *ap;
  3556. ap = ata_port_alloc(host);
  3557. if (!ap)
  3558. return NULL;
  3559. ap->port_no = 0;
  3560. ap->lock = &host->lock;
  3561. ap->pio_mask = port_info->pio_mask;
  3562. ap->mwdma_mask = port_info->mwdma_mask;
  3563. ap->udma_mask = port_info->udma_mask;
  3564. ap->flags |= port_info->flags;
  3565. ap->ops = port_info->port_ops;
  3566. ap->cbl = ATA_CBL_SATA;
  3567. return ap;
  3568. }
  3569. EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  3570. /**
  3571. * ata_sas_port_start - Set port up for dma.
  3572. * @ap: Port to initialize
  3573. *
  3574. * Called just after data structures for each port are
  3575. * initialized.
  3576. *
  3577. * May be used as the port_start() entry in ata_port_operations.
  3578. *
  3579. * LOCKING:
  3580. * Inherited from caller.
  3581. */
  3582. int ata_sas_port_start(struct ata_port *ap)
  3583. {
  3584. /*
  3585. * the port is marked as frozen at allocation time, but if we don't
  3586. * have new eh, we won't thaw it
  3587. */
  3588. if (!ap->ops->error_handler)
  3589. ap->pflags &= ~ATA_PFLAG_FROZEN;
  3590. return 0;
  3591. }
  3592. EXPORT_SYMBOL_GPL(ata_sas_port_start);
  3593. /**
  3594. * ata_port_stop - Undo ata_sas_port_start()
  3595. * @ap: Port to shut down
  3596. *
  3597. * May be used as the port_stop() entry in ata_port_operations.
  3598. *
  3599. * LOCKING:
  3600. * Inherited from caller.
  3601. */
  3602. void ata_sas_port_stop(struct ata_port *ap)
  3603. {
  3604. }
  3605. EXPORT_SYMBOL_GPL(ata_sas_port_stop);
  3606. /**
  3607. * ata_sas_async_probe - simply schedule probing and return
  3608. * @ap: Port to probe
  3609. *
  3610. * For batch scheduling of probe for sas attached ata devices, assumes
  3611. * the port has already been through ata_sas_port_init()
  3612. */
  3613. void ata_sas_async_probe(struct ata_port *ap)
  3614. {
  3615. __ata_port_probe(ap);
  3616. }
  3617. EXPORT_SYMBOL_GPL(ata_sas_async_probe);
  3618. int ata_sas_sync_probe(struct ata_port *ap)
  3619. {
  3620. return ata_port_probe(ap);
  3621. }
  3622. EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
  3623. /**
  3624. * ata_sas_port_init - Initialize a SATA device
  3625. * @ap: SATA port to initialize
  3626. *
  3627. * LOCKING:
  3628. * PCI/etc. bus probe sem.
  3629. *
  3630. * RETURNS:
  3631. * Zero on success, non-zero on error.
  3632. */
  3633. int ata_sas_port_init(struct ata_port *ap)
  3634. {
  3635. int rc = ap->ops->port_start(ap);
  3636. if (rc)
  3637. return rc;
  3638. ap->print_id = atomic_inc_return(&ata_print_id);
  3639. return 0;
  3640. }
  3641. EXPORT_SYMBOL_GPL(ata_sas_port_init);
  3642. /**
  3643. * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
  3644. * @ap: SATA port to destroy
  3645. *
  3646. */
  3647. void ata_sas_port_destroy(struct ata_port *ap)
  3648. {
  3649. if (ap->ops->port_stop)
  3650. ap->ops->port_stop(ap);
  3651. kfree(ap);
  3652. }
  3653. EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
  3654. /**
  3655. * ata_sas_slave_configure - Default slave_config routine for libata devices
  3656. * @sdev: SCSI device to configure
  3657. * @ap: ATA port to which SCSI device is attached
  3658. *
  3659. * RETURNS:
  3660. * Zero.
  3661. */
  3662. int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
  3663. {
  3664. ata_scsi_sdev_config(sdev);
  3665. ata_scsi_dev_config(sdev, ap->link.device);
  3666. return 0;
  3667. }
  3668. EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
  3669. /**
  3670. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  3671. * @cmd: SCSI command to be sent
  3672. * @ap: ATA port to which the command is being sent
  3673. *
  3674. * RETURNS:
  3675. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3676. * 0 otherwise.
  3677. */
  3678. int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
  3679. {
  3680. int rc = 0;
  3681. ata_scsi_dump_cdb(ap, cmd);
  3682. if (likely(ata_dev_enabled(ap->link.device)))
  3683. rc = __ata_scsi_queuecmd(cmd, ap->link.device);
  3684. else {
  3685. cmd->result = (DID_BAD_TARGET << 16);
  3686. cmd->scsi_done(cmd);
  3687. }
  3688. return rc;
  3689. }
  3690. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
  3691. int ata_sas_allocate_tag(struct ata_port *ap)
  3692. {
  3693. unsigned int max_queue = ap->host->n_tags;
  3694. unsigned int i, tag;
  3695. for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
  3696. tag = tag < max_queue ? tag : 0;
  3697. /* the last tag is reserved for internal command. */
  3698. if (tag == ATA_TAG_INTERNAL)
  3699. continue;
  3700. if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
  3701. ap->sas_last_tag = tag;
  3702. return tag;
  3703. }
  3704. }
  3705. return -1;
  3706. }
  3707. void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
  3708. {
  3709. clear_bit(tag, &ap->sas_tag_allocated);
  3710. }