libata-scsi.c 94 KB

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