libata-scsi.c 93 KB

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