libata-scsi.c 98 KB

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