libata-scsi.c 94 KB

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