libata-scsi.c 96 KB

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