libata-scsi.c 92 KB

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