libata-scsi.c 107 KB

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