mtip32xx.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686
  1. /*
  2. * Driver for the Micron P320 SSD
  3. * Copyright (C) 2011 Micron Technology, Inc.
  4. *
  5. * Portions of this code were derived from works subjected to the
  6. * following copyright:
  7. * Copyright (C) 2009 Integrated Device Technology, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/ata.h>
  23. #include <linux/delay.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/random.h>
  27. #include <linux/smp.h>
  28. #include <linux/compat.h>
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/genhd.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/blk-mq.h>
  34. #include <linux/bio.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/idr.h>
  37. #include <linux/kthread.h>
  38. #include <../drivers/ata/ahci.h>
  39. #include <linux/export.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/prefetch.h>
  42. #include "mtip32xx.h"
  43. #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
  44. /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
  45. #define AHCI_RX_FIS_SZ 0x100
  46. #define AHCI_RX_FIS_OFFSET 0x0
  47. #define AHCI_IDFY_SZ ATA_SECT_SIZE
  48. #define AHCI_IDFY_OFFSET 0x400
  49. #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
  50. #define AHCI_SECTBUF_OFFSET 0x800
  51. #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
  52. #define AHCI_SMARTBUF_OFFSET 0xC00
  53. /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
  54. #define BLOCK_DMA_ALLOC_SZ 4096
  55. /* DMA region containing command table (should be 8192 bytes) */
  56. #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
  57. #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
  58. #define AHCI_CMD_TBL_OFFSET 0x0
  59. /* DMA region per command (contains header and SGL) */
  60. #define AHCI_CMD_TBL_HDR_SZ 0x80
  61. #define AHCI_CMD_TBL_HDR_OFFSET 0x0
  62. #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
  63. #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
  64. #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
  65. #define HOST_CAP_NZDMA (1 << 19)
  66. #define HOST_HSORG 0xFC
  67. #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
  68. #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
  69. #define HSORG_HWREV 0xFF00
  70. #define HSORG_STYLE 0x8
  71. #define HSORG_SLOTGROUPS 0x7
  72. #define PORT_COMMAND_ISSUE 0x38
  73. #define PORT_SDBV 0x7C
  74. #define PORT_OFFSET 0x100
  75. #define PORT_MEM_SIZE 0x80
  76. #define PORT_IRQ_ERR \
  77. (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
  78. PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
  79. PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
  80. PORT_IRQ_OVERFLOW)
  81. #define PORT_IRQ_LEGACY \
  82. (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  83. #define PORT_IRQ_HANDLED \
  84. (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
  85. PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
  86. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
  87. #define DEF_PORT_IRQ \
  88. (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
  89. /* product numbers */
  90. #define MTIP_PRODUCT_UNKNOWN 0x00
  91. #define MTIP_PRODUCT_ASICFPGA 0x11
  92. /* Device instance number, incremented each time a device is probed. */
  93. static int instance;
  94. struct list_head online_list;
  95. struct list_head removing_list;
  96. spinlock_t dev_lock;
  97. /*
  98. * Global variable used to hold the major block device number
  99. * allocated in mtip_init().
  100. */
  101. static int mtip_major;
  102. static struct dentry *dfs_parent;
  103. static struct dentry *dfs_device_status;
  104. static u32 cpu_use[NR_CPUS];
  105. static DEFINE_SPINLOCK(rssd_index_lock);
  106. static DEFINE_IDA(rssd_index_ida);
  107. static int mtip_block_initialize(struct driver_data *dd);
  108. #ifdef CONFIG_COMPAT
  109. struct mtip_compat_ide_task_request_s {
  110. __u8 io_ports[8];
  111. __u8 hob_ports[8];
  112. ide_reg_valid_t out_flags;
  113. ide_reg_valid_t in_flags;
  114. int data_phase;
  115. int req_cmd;
  116. compat_ulong_t out_size;
  117. compat_ulong_t in_size;
  118. };
  119. #endif
  120. /*
  121. * This function check_for_surprise_removal is called
  122. * while card is removed from the system and it will
  123. * read the vendor id from the configration space
  124. *
  125. * @pdev Pointer to the pci_dev structure.
  126. *
  127. * return value
  128. * true if device removed, else false
  129. */
  130. static bool mtip_check_surprise_removal(struct pci_dev *pdev)
  131. {
  132. u16 vendor_id = 0;
  133. struct driver_data *dd = pci_get_drvdata(pdev);
  134. if (dd->sr)
  135. return true;
  136. /* Read the vendorID from the configuration space */
  137. pci_read_config_word(pdev, 0x00, &vendor_id);
  138. if (vendor_id == 0xFFFF) {
  139. dd->sr = true;
  140. if (dd->queue)
  141. set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
  142. else
  143. dev_warn(&dd->pdev->dev,
  144. "%s: dd->queue is NULL\n", __func__);
  145. return true; /* device removed */
  146. }
  147. return false; /* device present */
  148. }
  149. static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
  150. {
  151. struct request *rq;
  152. rq = blk_mq_alloc_request(dd->queue, 0, __GFP_WAIT, true);
  153. return blk_mq_rq_to_pdu(rq);
  154. }
  155. static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
  156. {
  157. blk_put_request(blk_mq_rq_from_pdu(cmd));
  158. }
  159. /*
  160. * Once we add support for one hctx per mtip group, this will change a bit
  161. */
  162. static struct request *mtip_rq_from_tag(struct driver_data *dd,
  163. unsigned int tag)
  164. {
  165. struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
  166. return blk_mq_tag_to_rq(hctx->tags, tag);
  167. }
  168. static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
  169. unsigned int tag)
  170. {
  171. struct request *rq = mtip_rq_from_tag(dd, tag);
  172. return blk_mq_rq_to_pdu(rq);
  173. }
  174. /*
  175. * IO completion function.
  176. *
  177. * This completion function is called by the driver ISR when a
  178. * command that was issued by the kernel completes. It first calls the
  179. * asynchronous completion function which normally calls back into the block
  180. * layer passing the asynchronous callback data, then unmaps the
  181. * scatter list associated with the completed command, and finally
  182. * clears the allocated bit associated with the completed command.
  183. *
  184. * @port Pointer to the port data structure.
  185. * @tag Tag of the command.
  186. * @data Pointer to driver_data.
  187. * @status Completion status.
  188. *
  189. * return value
  190. * None
  191. */
  192. static void mtip_async_complete(struct mtip_port *port,
  193. int tag, struct mtip_cmd *cmd, int status)
  194. {
  195. struct driver_data *dd = port->dd;
  196. struct request *rq;
  197. if (unlikely(!dd) || unlikely(!port))
  198. return;
  199. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  200. dev_warn(&port->dd->pdev->dev,
  201. "Command tag %d failed due to TFE\n", tag);
  202. }
  203. /* Unmap the DMA scatter list entries */
  204. dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, cmd->direction);
  205. rq = mtip_rq_from_tag(dd, tag);
  206. if (unlikely(cmd->unaligned))
  207. up(&port->cmd_slot_unal);
  208. blk_mq_end_request(rq, status ? -EIO : 0);
  209. }
  210. /*
  211. * Reset the HBA (without sleeping)
  212. *
  213. * @dd Pointer to the driver data structure.
  214. *
  215. * return value
  216. * 0 The reset was successful.
  217. * -1 The HBA Reset bit did not clear.
  218. */
  219. static int mtip_hba_reset(struct driver_data *dd)
  220. {
  221. unsigned long timeout;
  222. /* Set the reset bit */
  223. writel(HOST_RESET, dd->mmio + HOST_CTL);
  224. /* Flush */
  225. readl(dd->mmio + HOST_CTL);
  226. /*
  227. * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
  228. * is 1 sec but in LUN failure conditions, up to 10 secs are required
  229. */
  230. timeout = jiffies + msecs_to_jiffies(10000);
  231. do {
  232. mdelay(10);
  233. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
  234. return -1;
  235. } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
  236. && time_before(jiffies, timeout));
  237. if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
  238. return -1;
  239. return 0;
  240. }
  241. /*
  242. * Issue a command to the hardware.
  243. *
  244. * Set the appropriate bit in the s_active and Command Issue hardware
  245. * registers, causing hardware command processing to begin.
  246. *
  247. * @port Pointer to the port structure.
  248. * @tag The tag of the command to be issued.
  249. *
  250. * return value
  251. * None
  252. */
  253. static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
  254. {
  255. int group = tag >> 5;
  256. /* guard SACT and CI registers */
  257. spin_lock(&port->cmd_issue_lock[group]);
  258. writel((1 << MTIP_TAG_BIT(tag)),
  259. port->s_active[MTIP_TAG_INDEX(tag)]);
  260. writel((1 << MTIP_TAG_BIT(tag)),
  261. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  262. spin_unlock(&port->cmd_issue_lock[group]);
  263. }
  264. /*
  265. * Enable/disable the reception of FIS
  266. *
  267. * @port Pointer to the port data structure
  268. * @enable 1 to enable, 0 to disable
  269. *
  270. * return value
  271. * Previous state: 1 enabled, 0 disabled
  272. */
  273. static int mtip_enable_fis(struct mtip_port *port, int enable)
  274. {
  275. u32 tmp;
  276. /* enable FIS reception */
  277. tmp = readl(port->mmio + PORT_CMD);
  278. if (enable)
  279. writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  280. else
  281. writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  282. /* Flush */
  283. readl(port->mmio + PORT_CMD);
  284. return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
  285. }
  286. /*
  287. * Enable/disable the DMA engine
  288. *
  289. * @port Pointer to the port data structure
  290. * @enable 1 to enable, 0 to disable
  291. *
  292. * return value
  293. * Previous state: 1 enabled, 0 disabled.
  294. */
  295. static int mtip_enable_engine(struct mtip_port *port, int enable)
  296. {
  297. u32 tmp;
  298. /* enable FIS reception */
  299. tmp = readl(port->mmio + PORT_CMD);
  300. if (enable)
  301. writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
  302. else
  303. writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
  304. readl(port->mmio + PORT_CMD);
  305. return (((tmp & PORT_CMD_START) == PORT_CMD_START));
  306. }
  307. /*
  308. * Enables the port DMA engine and FIS reception.
  309. *
  310. * return value
  311. * None
  312. */
  313. static inline void mtip_start_port(struct mtip_port *port)
  314. {
  315. /* Enable FIS reception */
  316. mtip_enable_fis(port, 1);
  317. /* Enable the DMA engine */
  318. mtip_enable_engine(port, 1);
  319. }
  320. /*
  321. * Deinitialize a port by disabling port interrupts, the DMA engine,
  322. * and FIS reception.
  323. *
  324. * @port Pointer to the port structure
  325. *
  326. * return value
  327. * None
  328. */
  329. static inline void mtip_deinit_port(struct mtip_port *port)
  330. {
  331. /* Disable interrupts on this port */
  332. writel(0, port->mmio + PORT_IRQ_MASK);
  333. /* Disable the DMA engine */
  334. mtip_enable_engine(port, 0);
  335. /* Disable FIS reception */
  336. mtip_enable_fis(port, 0);
  337. }
  338. /*
  339. * Initialize a port.
  340. *
  341. * This function deinitializes the port by calling mtip_deinit_port() and
  342. * then initializes it by setting the command header and RX FIS addresses,
  343. * clearing the SError register and any pending port interrupts before
  344. * re-enabling the default set of port interrupts.
  345. *
  346. * @port Pointer to the port structure.
  347. *
  348. * return value
  349. * None
  350. */
  351. static void mtip_init_port(struct mtip_port *port)
  352. {
  353. int i;
  354. mtip_deinit_port(port);
  355. /* Program the command list base and FIS base addresses */
  356. if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
  357. writel((port->command_list_dma >> 16) >> 16,
  358. port->mmio + PORT_LST_ADDR_HI);
  359. writel((port->rxfis_dma >> 16) >> 16,
  360. port->mmio + PORT_FIS_ADDR_HI);
  361. }
  362. writel(port->command_list_dma & 0xFFFFFFFF,
  363. port->mmio + PORT_LST_ADDR);
  364. writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
  365. /* Clear SError */
  366. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  367. /* reset the completed registers.*/
  368. for (i = 0; i < port->dd->slot_groups; i++)
  369. writel(0xFFFFFFFF, port->completed[i]);
  370. /* Clear any pending interrupts for this port */
  371. writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  372. /* Clear any pending interrupts on the HBA. */
  373. writel(readl(port->dd->mmio + HOST_IRQ_STAT),
  374. port->dd->mmio + HOST_IRQ_STAT);
  375. /* Enable port interrupts */
  376. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  377. }
  378. /*
  379. * Restart a port
  380. *
  381. * @port Pointer to the port data structure.
  382. *
  383. * return value
  384. * None
  385. */
  386. static void mtip_restart_port(struct mtip_port *port)
  387. {
  388. unsigned long timeout;
  389. /* Disable the DMA engine */
  390. mtip_enable_engine(port, 0);
  391. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  392. timeout = jiffies + msecs_to_jiffies(500);
  393. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  394. && time_before(jiffies, timeout))
  395. ;
  396. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  397. return;
  398. /*
  399. * Chip quirk: escalate to hba reset if
  400. * PxCMD.CR not clear after 500 ms
  401. */
  402. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  403. dev_warn(&port->dd->pdev->dev,
  404. "PxCMD.CR not clear, escalating reset\n");
  405. if (mtip_hba_reset(port->dd))
  406. dev_err(&port->dd->pdev->dev,
  407. "HBA reset escalation failed.\n");
  408. /* 30 ms delay before com reset to quiesce chip */
  409. mdelay(30);
  410. }
  411. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  412. /* Set PxSCTL.DET */
  413. writel(readl(port->mmio + PORT_SCR_CTL) |
  414. 1, port->mmio + PORT_SCR_CTL);
  415. readl(port->mmio + PORT_SCR_CTL);
  416. /* Wait 1 ms to quiesce chip function */
  417. timeout = jiffies + msecs_to_jiffies(1);
  418. while (time_before(jiffies, timeout))
  419. ;
  420. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  421. return;
  422. /* Clear PxSCTL.DET */
  423. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  424. port->mmio + PORT_SCR_CTL);
  425. readl(port->mmio + PORT_SCR_CTL);
  426. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  427. timeout = jiffies + msecs_to_jiffies(500);
  428. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  429. && time_before(jiffies, timeout))
  430. ;
  431. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  432. return;
  433. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  434. dev_warn(&port->dd->pdev->dev,
  435. "COM reset failed\n");
  436. mtip_init_port(port);
  437. mtip_start_port(port);
  438. }
  439. static int mtip_device_reset(struct driver_data *dd)
  440. {
  441. int rv = 0;
  442. if (mtip_check_surprise_removal(dd->pdev))
  443. return 0;
  444. if (mtip_hba_reset(dd) < 0)
  445. rv = -EFAULT;
  446. mdelay(1);
  447. mtip_init_port(dd->port);
  448. mtip_start_port(dd->port);
  449. /* Enable interrupts on the HBA. */
  450. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  451. dd->mmio + HOST_CTL);
  452. return rv;
  453. }
  454. /*
  455. * Helper function for tag logging
  456. */
  457. static void print_tags(struct driver_data *dd,
  458. char *msg,
  459. unsigned long *tagbits,
  460. int cnt)
  461. {
  462. unsigned char tagmap[128];
  463. int group, tagmap_len = 0;
  464. memset(tagmap, 0, sizeof(tagmap));
  465. for (group = SLOTBITS_IN_LONGS; group > 0; group--)
  466. tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
  467. tagbits[group-1]);
  468. dev_warn(&dd->pdev->dev,
  469. "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
  470. }
  471. /*
  472. * Internal command completion callback function.
  473. *
  474. * This function is normally called by the driver ISR when an internal
  475. * command completed. This function signals the command completion by
  476. * calling complete().
  477. *
  478. * @port Pointer to the port data structure.
  479. * @tag Tag of the command that has completed.
  480. * @data Pointer to a completion structure.
  481. * @status Completion status.
  482. *
  483. * return value
  484. * None
  485. */
  486. static void mtip_completion(struct mtip_port *port,
  487. int tag, struct mtip_cmd *command, int status)
  488. {
  489. struct completion *waiting = command->comp_data;
  490. if (unlikely(status == PORT_IRQ_TF_ERR))
  491. dev_warn(&port->dd->pdev->dev,
  492. "Internal command %d completed with TFE\n", tag);
  493. complete(waiting);
  494. }
  495. static void mtip_null_completion(struct mtip_port *port,
  496. int tag, struct mtip_cmd *command, int status)
  497. {
  498. }
  499. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  500. dma_addr_t buffer_dma, unsigned int sectors);
  501. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  502. struct smart_attr *attrib);
  503. /*
  504. * Handle an error.
  505. *
  506. * @dd Pointer to the DRIVER_DATA structure.
  507. *
  508. * return value
  509. * None
  510. */
  511. static void mtip_handle_tfe(struct driver_data *dd)
  512. {
  513. int group, tag, bit, reissue, rv;
  514. struct mtip_port *port;
  515. struct mtip_cmd *cmd;
  516. u32 completed;
  517. struct host_to_dev_fis *fis;
  518. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  519. unsigned int cmd_cnt = 0;
  520. unsigned char *buf;
  521. char *fail_reason = NULL;
  522. int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
  523. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  524. port = dd->port;
  525. set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  526. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
  527. cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
  528. dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
  529. if (cmd->comp_data && cmd->comp_func) {
  530. cmd->comp_func(port, MTIP_TAG_INTERNAL,
  531. cmd, PORT_IRQ_TF_ERR);
  532. }
  533. goto handle_tfe_exit;
  534. }
  535. /* clear the tag accumulator */
  536. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  537. /* Loop through all the groups */
  538. for (group = 0; group < dd->slot_groups; group++) {
  539. completed = readl(port->completed[group]);
  540. dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
  541. /* clear completed status register in the hardware.*/
  542. writel(completed, port->completed[group]);
  543. /* Process successfully completed commands */
  544. for (bit = 0; bit < 32 && completed; bit++) {
  545. if (!(completed & (1<<bit)))
  546. continue;
  547. tag = (group << 5) + bit;
  548. /* Skip the internal command slot */
  549. if (tag == MTIP_TAG_INTERNAL)
  550. continue;
  551. cmd = mtip_cmd_from_tag(dd, tag);
  552. if (likely(cmd->comp_func)) {
  553. set_bit(tag, tagaccum);
  554. cmd_cnt++;
  555. cmd->comp_func(port, tag, cmd, 0);
  556. } else {
  557. dev_err(&port->dd->pdev->dev,
  558. "Missing completion func for tag %d",
  559. tag);
  560. if (mtip_check_surprise_removal(dd->pdev)) {
  561. /* don't proceed further */
  562. return;
  563. }
  564. }
  565. }
  566. }
  567. print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
  568. /* Restart the port */
  569. mdelay(20);
  570. mtip_restart_port(port);
  571. /* Trying to determine the cause of the error */
  572. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  573. dd->port->log_buf,
  574. dd->port->log_buf_dma, 1);
  575. if (rv) {
  576. dev_warn(&dd->pdev->dev,
  577. "Error in READ LOG EXT (10h) command\n");
  578. /* non-critical error, don't fail the load */
  579. } else {
  580. buf = (unsigned char *)dd->port->log_buf;
  581. if (buf[259] & 0x1) {
  582. dev_info(&dd->pdev->dev,
  583. "Write protect bit is set.\n");
  584. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  585. fail_all_ncq_write = 1;
  586. fail_reason = "write protect";
  587. }
  588. if (buf[288] == 0xF7) {
  589. dev_info(&dd->pdev->dev,
  590. "Exceeded Tmax, drive in thermal shutdown.\n");
  591. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  592. fail_all_ncq_cmds = 1;
  593. fail_reason = "thermal shutdown";
  594. }
  595. if (buf[288] == 0xBF) {
  596. set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
  597. dev_info(&dd->pdev->dev,
  598. "Drive indicates rebuild has failed. Secure erase required.\n");
  599. fail_all_ncq_cmds = 1;
  600. fail_reason = "rebuild failed";
  601. }
  602. }
  603. /* clear the tag accumulator */
  604. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  605. /* Loop through all the groups */
  606. for (group = 0; group < dd->slot_groups; group++) {
  607. for (bit = 0; bit < 32; bit++) {
  608. reissue = 1;
  609. tag = (group << 5) + bit;
  610. cmd = mtip_cmd_from_tag(dd, tag);
  611. fis = (struct host_to_dev_fis *)cmd->command;
  612. /* Should re-issue? */
  613. if (tag == MTIP_TAG_INTERNAL ||
  614. fis->command == ATA_CMD_SET_FEATURES)
  615. reissue = 0;
  616. else {
  617. if (fail_all_ncq_cmds ||
  618. (fail_all_ncq_write &&
  619. fis->command == ATA_CMD_FPDMA_WRITE)) {
  620. dev_warn(&dd->pdev->dev,
  621. " Fail: %s w/tag %d [%s].\n",
  622. fis->command == ATA_CMD_FPDMA_WRITE ?
  623. "write" : "read",
  624. tag,
  625. fail_reason != NULL ?
  626. fail_reason : "unknown");
  627. if (cmd->comp_func) {
  628. cmd->comp_func(port, tag,
  629. cmd, -ENODATA);
  630. }
  631. continue;
  632. }
  633. }
  634. /*
  635. * First check if this command has
  636. * exceeded its retries.
  637. */
  638. if (reissue && (cmd->retries-- > 0)) {
  639. set_bit(tag, tagaccum);
  640. /* Re-issue the command. */
  641. mtip_issue_ncq_command(port, tag);
  642. continue;
  643. }
  644. /* Retire a command that will not be reissued */
  645. dev_warn(&port->dd->pdev->dev,
  646. "retiring tag %d\n", tag);
  647. if (cmd->comp_func)
  648. cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
  649. else
  650. dev_warn(&port->dd->pdev->dev,
  651. "Bad completion for tag %d\n",
  652. tag);
  653. }
  654. }
  655. print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
  656. handle_tfe_exit:
  657. /* clear eh_active */
  658. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  659. wake_up_interruptible(&port->svc_wait);
  660. }
  661. /*
  662. * Handle a set device bits interrupt
  663. */
  664. static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
  665. u32 completed)
  666. {
  667. struct driver_data *dd = port->dd;
  668. int tag, bit;
  669. struct mtip_cmd *command;
  670. if (!completed) {
  671. WARN_ON_ONCE(!completed);
  672. return;
  673. }
  674. /* clear completed status register in the hardware.*/
  675. writel(completed, port->completed[group]);
  676. /* Process completed commands. */
  677. for (bit = 0; (bit < 32) && completed; bit++) {
  678. if (completed & 0x01) {
  679. tag = (group << 5) | bit;
  680. /* skip internal command slot. */
  681. if (unlikely(tag == MTIP_TAG_INTERNAL))
  682. continue;
  683. command = mtip_cmd_from_tag(dd, tag);
  684. if (likely(command->comp_func))
  685. command->comp_func(port, tag, command, 0);
  686. else {
  687. dev_dbg(&dd->pdev->dev,
  688. "Null completion for tag %d",
  689. tag);
  690. if (mtip_check_surprise_removal(
  691. dd->pdev)) {
  692. return;
  693. }
  694. }
  695. }
  696. completed >>= 1;
  697. }
  698. /* If last, re-enable interrupts */
  699. if (atomic_dec_return(&dd->irq_workers_active) == 0)
  700. writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
  701. }
  702. /*
  703. * Process legacy pio and d2h interrupts
  704. */
  705. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  706. {
  707. struct mtip_port *port = dd->port;
  708. struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
  709. if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
  710. (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  711. & (1 << MTIP_TAG_INTERNAL))) {
  712. if (cmd->comp_func) {
  713. cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
  714. return;
  715. }
  716. }
  717. return;
  718. }
  719. /*
  720. * Demux and handle errors
  721. */
  722. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  723. {
  724. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  725. dev_warn(&dd->pdev->dev,
  726. "Clearing PxSERR.DIAG.x\n");
  727. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  728. }
  729. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  730. dev_warn(&dd->pdev->dev,
  731. "Clearing PxSERR.DIAG.n\n");
  732. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  733. }
  734. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  735. dev_warn(&dd->pdev->dev,
  736. "Port stat errors %x unhandled\n",
  737. (port_stat & ~PORT_IRQ_HANDLED));
  738. if (mtip_check_surprise_removal(dd->pdev))
  739. return;
  740. }
  741. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
  742. set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
  743. wake_up_interruptible(&dd->port->svc_wait);
  744. }
  745. }
  746. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  747. {
  748. struct driver_data *dd = (struct driver_data *) data;
  749. struct mtip_port *port = dd->port;
  750. u32 hba_stat, port_stat;
  751. int rv = IRQ_NONE;
  752. int do_irq_enable = 1, i, workers;
  753. struct mtip_work *twork;
  754. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  755. if (hba_stat) {
  756. rv = IRQ_HANDLED;
  757. /* Acknowledge the interrupt status on the port.*/
  758. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  759. if (unlikely(port_stat == 0xFFFFFFFF)) {
  760. mtip_check_surprise_removal(dd->pdev);
  761. return IRQ_HANDLED;
  762. }
  763. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  764. /* Demux port status */
  765. if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
  766. do_irq_enable = 0;
  767. WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
  768. /* Start at 1: group zero is always local? */
  769. for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
  770. i++) {
  771. twork = &dd->work[i];
  772. twork->completed = readl(port->completed[i]);
  773. if (twork->completed)
  774. workers++;
  775. }
  776. atomic_set(&dd->irq_workers_active, workers);
  777. if (workers) {
  778. for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
  779. twork = &dd->work[i];
  780. if (twork->completed)
  781. queue_work_on(
  782. twork->cpu_binding,
  783. dd->isr_workq,
  784. &twork->work);
  785. }
  786. if (likely(dd->work[0].completed))
  787. mtip_workq_sdbfx(port, 0,
  788. dd->work[0].completed);
  789. } else {
  790. /*
  791. * Chip quirk: SDB interrupt but nothing
  792. * to complete
  793. */
  794. do_irq_enable = 1;
  795. }
  796. }
  797. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  798. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  799. /* don't proceed further */
  800. return IRQ_HANDLED;
  801. }
  802. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  803. &dd->dd_flag))
  804. return rv;
  805. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  806. }
  807. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  808. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  809. }
  810. /* acknowledge interrupt */
  811. if (unlikely(do_irq_enable))
  812. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  813. return rv;
  814. }
  815. /*
  816. * HBA interrupt subroutine.
  817. *
  818. * @irq IRQ number.
  819. * @instance Pointer to the driver data structure.
  820. *
  821. * return value
  822. * IRQ_HANDLED A HBA interrupt was pending and handled.
  823. * IRQ_NONE This interrupt was not for the HBA.
  824. */
  825. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  826. {
  827. struct driver_data *dd = instance;
  828. return mtip_handle_irq(dd);
  829. }
  830. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  831. {
  832. writel(1 << MTIP_TAG_BIT(tag),
  833. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  834. }
  835. static bool mtip_pause_ncq(struct mtip_port *port,
  836. struct host_to_dev_fis *fis)
  837. {
  838. struct host_to_dev_fis *reply;
  839. unsigned long task_file_data;
  840. reply = port->rxfis + RX_FIS_D2H_REG;
  841. task_file_data = readl(port->mmio+PORT_TFDATA);
  842. if ((task_file_data & 1))
  843. return false;
  844. if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
  845. port->ic_pause_timer = jiffies;
  846. return true;
  847. } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
  848. (fis->features == 0x03)) {
  849. set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  850. port->ic_pause_timer = jiffies;
  851. return true;
  852. } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
  853. ((fis->command == 0xFC) &&
  854. (fis->features == 0x27 || fis->features == 0x72 ||
  855. fis->features == 0x62 || fis->features == 0x26))) {
  856. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  857. /* Com reset after secure erase or lowlevel format */
  858. mtip_restart_port(port);
  859. clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  860. return false;
  861. }
  862. return false;
  863. }
  864. /*
  865. * Wait for port to quiesce
  866. *
  867. * @port Pointer to port data structure
  868. * @timeout Max duration to wait (ms)
  869. *
  870. * return value
  871. * 0 Success
  872. * -EBUSY Commands still active
  873. */
  874. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  875. {
  876. unsigned long to;
  877. unsigned int n;
  878. unsigned int active = 1;
  879. blk_mq_stop_hw_queues(port->dd->queue);
  880. to = jiffies + msecs_to_jiffies(timeout);
  881. do {
  882. if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
  883. test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  884. msleep(20);
  885. continue; /* svc thd is actively issuing commands */
  886. }
  887. msleep(100);
  888. if (mtip_check_surprise_removal(port->dd->pdev))
  889. goto err_fault;
  890. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  891. goto err_fault;
  892. /*
  893. * Ignore s_active bit 0 of array element 0.
  894. * This bit will always be set
  895. */
  896. active = readl(port->s_active[0]) & 0xFFFFFFFE;
  897. for (n = 1; n < port->dd->slot_groups; n++)
  898. active |= readl(port->s_active[n]);
  899. if (!active)
  900. break;
  901. } while (time_before(jiffies, to));
  902. blk_mq_start_stopped_hw_queues(port->dd->queue, true);
  903. return active ? -EBUSY : 0;
  904. err_fault:
  905. blk_mq_start_stopped_hw_queues(port->dd->queue, true);
  906. return -EFAULT;
  907. }
  908. /*
  909. * Execute an internal command and wait for the completion.
  910. *
  911. * @port Pointer to the port data structure.
  912. * @fis Pointer to the FIS that describes the command.
  913. * @fis_len Length in WORDS of the FIS.
  914. * @buffer DMA accessible for command data.
  915. * @buf_len Length, in bytes, of the data buffer.
  916. * @opts Command header options, excluding the FIS length
  917. * and the number of PRD entries.
  918. * @timeout Time in ms to wait for the command to complete.
  919. *
  920. * return value
  921. * 0 Command completed successfully.
  922. * -EFAULT The buffer address is not correctly aligned.
  923. * -EBUSY Internal command or other IO in progress.
  924. * -EAGAIN Time out waiting for command to complete.
  925. */
  926. static int mtip_exec_internal_command(struct mtip_port *port,
  927. struct host_to_dev_fis *fis,
  928. int fis_len,
  929. dma_addr_t buffer,
  930. int buf_len,
  931. u32 opts,
  932. gfp_t atomic,
  933. unsigned long timeout)
  934. {
  935. struct mtip_cmd_sg *command_sg;
  936. DECLARE_COMPLETION_ONSTACK(wait);
  937. struct mtip_cmd *int_cmd;
  938. struct driver_data *dd = port->dd;
  939. int rv = 0;
  940. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  941. if (buffer & 0x00000007) {
  942. dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
  943. return -EFAULT;
  944. }
  945. int_cmd = mtip_get_int_command(dd);
  946. set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  947. if (fis->command == ATA_CMD_SEC_ERASE_PREP)
  948. set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
  949. clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
  950. if (atomic == GFP_KERNEL) {
  951. if (fis->command != ATA_CMD_STANDBYNOW1) {
  952. /* wait for io to complete if non atomic */
  953. if (mtip_quiesce_io(port,
  954. MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
  955. dev_warn(&dd->pdev->dev,
  956. "Failed to quiesce IO\n");
  957. mtip_put_int_command(dd, int_cmd);
  958. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  959. wake_up_interruptible(&port->svc_wait);
  960. return -EBUSY;
  961. }
  962. }
  963. /* Set the completion function and data for the command. */
  964. int_cmd->comp_data = &wait;
  965. int_cmd->comp_func = mtip_completion;
  966. } else {
  967. /* Clear completion - we're going to poll */
  968. int_cmd->comp_data = NULL;
  969. int_cmd->comp_func = mtip_null_completion;
  970. }
  971. /* Copy the command to the command table */
  972. memcpy(int_cmd->command, fis, fis_len*4);
  973. /* Populate the SG list */
  974. int_cmd->command_header->opts =
  975. __force_bit2int cpu_to_le32(opts | fis_len);
  976. if (buf_len) {
  977. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  978. command_sg->info =
  979. __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
  980. command_sg->dba =
  981. __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
  982. command_sg->dba_upper =
  983. __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
  984. int_cmd->command_header->opts |=
  985. __force_bit2int cpu_to_le32((1 << 16));
  986. }
  987. /* Populate the command header */
  988. int_cmd->command_header->byte_count = 0;
  989. /* Issue the command to the hardware */
  990. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  991. if (atomic == GFP_KERNEL) {
  992. /* Wait for the command to complete or timeout. */
  993. if ((rv = wait_for_completion_interruptible_timeout(
  994. &wait,
  995. msecs_to_jiffies(timeout))) <= 0) {
  996. if (rv == -ERESTARTSYS) { /* interrupted */
  997. dev_err(&dd->pdev->dev,
  998. "Internal command [%02X] was interrupted after %lu ms\n",
  999. fis->command, timeout);
  1000. rv = -EINTR;
  1001. goto exec_ic_exit;
  1002. } else if (rv == 0) /* timeout */
  1003. dev_err(&dd->pdev->dev,
  1004. "Internal command did not complete [%02X] within timeout of %lu ms\n",
  1005. fis->command, timeout);
  1006. else
  1007. dev_err(&dd->pdev->dev,
  1008. "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
  1009. fis->command, rv, timeout);
  1010. if (mtip_check_surprise_removal(dd->pdev) ||
  1011. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1012. &dd->dd_flag)) {
  1013. dev_err(&dd->pdev->dev,
  1014. "Internal command [%02X] wait returned due to SR\n",
  1015. fis->command);
  1016. rv = -ENXIO;
  1017. goto exec_ic_exit;
  1018. }
  1019. mtip_device_reset(dd); /* recover from timeout issue */
  1020. rv = -EAGAIN;
  1021. goto exec_ic_exit;
  1022. }
  1023. } else {
  1024. u32 hba_stat, port_stat;
  1025. /* Spin for <timeout> checking if command still outstanding */
  1026. timeout = jiffies + msecs_to_jiffies(timeout);
  1027. while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1028. & (1 << MTIP_TAG_INTERNAL))
  1029. && time_before(jiffies, timeout)) {
  1030. if (mtip_check_surprise_removal(dd->pdev)) {
  1031. rv = -ENXIO;
  1032. goto exec_ic_exit;
  1033. }
  1034. if ((fis->command != ATA_CMD_STANDBYNOW1) &&
  1035. test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  1036. &dd->dd_flag)) {
  1037. rv = -ENXIO;
  1038. goto exec_ic_exit;
  1039. }
  1040. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  1041. if (!port_stat)
  1042. continue;
  1043. if (port_stat & PORT_IRQ_ERR) {
  1044. dev_err(&dd->pdev->dev,
  1045. "Internal command [%02X] failed\n",
  1046. fis->command);
  1047. mtip_device_reset(dd);
  1048. rv = -EIO;
  1049. goto exec_ic_exit;
  1050. } else {
  1051. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  1052. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  1053. if (hba_stat)
  1054. writel(hba_stat,
  1055. dd->mmio + HOST_IRQ_STAT);
  1056. }
  1057. break;
  1058. }
  1059. }
  1060. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1061. & (1 << MTIP_TAG_INTERNAL)) {
  1062. rv = -ENXIO;
  1063. if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  1064. mtip_device_reset(dd);
  1065. rv = -EAGAIN;
  1066. }
  1067. }
  1068. exec_ic_exit:
  1069. /* Clear the allocated and active bits for the internal command. */
  1070. mtip_put_int_command(dd, int_cmd);
  1071. clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
  1072. if (rv >= 0 && mtip_pause_ncq(port, fis)) {
  1073. /* NCQ paused */
  1074. return rv;
  1075. }
  1076. wake_up_interruptible(&port->svc_wait);
  1077. return rv;
  1078. }
  1079. /*
  1080. * Byte-swap ATA ID strings.
  1081. *
  1082. * ATA identify data contains strings in byte-swapped 16-bit words.
  1083. * They must be swapped (on all architectures) to be usable as C strings.
  1084. * This function swaps bytes in-place.
  1085. *
  1086. * @buf The buffer location of the string
  1087. * @len The number of bytes to swap
  1088. *
  1089. * return value
  1090. * None
  1091. */
  1092. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1093. {
  1094. int i;
  1095. for (i = 0; i < (len/2); i++)
  1096. be16_to_cpus(&buf[i]);
  1097. }
  1098. static void mtip_set_timeout(struct driver_data *dd,
  1099. struct host_to_dev_fis *fis,
  1100. unsigned int *timeout, u8 erasemode)
  1101. {
  1102. switch (fis->command) {
  1103. case ATA_CMD_DOWNLOAD_MICRO:
  1104. *timeout = 120000; /* 2 minutes */
  1105. break;
  1106. case ATA_CMD_SEC_ERASE_UNIT:
  1107. case 0xFC:
  1108. if (erasemode)
  1109. *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
  1110. else
  1111. *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
  1112. break;
  1113. case ATA_CMD_STANDBYNOW1:
  1114. *timeout = 120000; /* 2 minutes */
  1115. break;
  1116. case 0xF7:
  1117. case 0xFA:
  1118. *timeout = 60000; /* 60 seconds */
  1119. break;
  1120. case ATA_CMD_SMART:
  1121. *timeout = 15000; /* 15 seconds */
  1122. break;
  1123. default:
  1124. *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
  1125. break;
  1126. }
  1127. }
  1128. /*
  1129. * Request the device identity information.
  1130. *
  1131. * If a user space buffer is not specified, i.e. is NULL, the
  1132. * identify information is still read from the drive and placed
  1133. * into the identify data buffer (@e port->identify) in the
  1134. * port data structure.
  1135. * When the identify buffer contains valid identify information @e
  1136. * port->identify_valid is non-zero.
  1137. *
  1138. * @port Pointer to the port structure.
  1139. * @user_buffer A user space buffer where the identify data should be
  1140. * copied.
  1141. *
  1142. * return value
  1143. * 0 Command completed successfully.
  1144. * -EFAULT An error occurred while coping data to the user buffer.
  1145. * -1 Command failed.
  1146. */
  1147. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1148. {
  1149. int rv = 0;
  1150. struct host_to_dev_fis fis;
  1151. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
  1152. return -EFAULT;
  1153. /* Build the FIS. */
  1154. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1155. fis.type = 0x27;
  1156. fis.opts = 1 << 7;
  1157. fis.command = ATA_CMD_ID_ATA;
  1158. /* Set the identify information as invalid. */
  1159. port->identify_valid = 0;
  1160. /* Clear the identify information. */
  1161. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1162. /* Execute the command. */
  1163. if (mtip_exec_internal_command(port,
  1164. &fis,
  1165. 5,
  1166. port->identify_dma,
  1167. sizeof(u16) * ATA_ID_WORDS,
  1168. 0,
  1169. GFP_KERNEL,
  1170. MTIP_INT_CMD_TIMEOUT_MS)
  1171. < 0) {
  1172. rv = -1;
  1173. goto out;
  1174. }
  1175. /*
  1176. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1177. * perform field-sensitive swapping on the string fields.
  1178. * See the kernel use of ata_id_string() for proof of this.
  1179. */
  1180. #ifdef __LITTLE_ENDIAN
  1181. ata_swap_string(port->identify + 27, 40); /* model string*/
  1182. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1183. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1184. #else
  1185. {
  1186. int i;
  1187. for (i = 0; i < ATA_ID_WORDS; i++)
  1188. port->identify[i] = le16_to_cpu(port->identify[i]);
  1189. }
  1190. #endif
  1191. /* Check security locked state */
  1192. if (port->identify[128] & 0x4)
  1193. set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  1194. else
  1195. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
  1196. #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
  1197. /* Demux ID.DRAT & ID.RZAT to determine trim support */
  1198. if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
  1199. port->dd->trim_supp = true;
  1200. else
  1201. #endif
  1202. port->dd->trim_supp = false;
  1203. /* Set the identify buffer as valid. */
  1204. port->identify_valid = 1;
  1205. if (user_buffer) {
  1206. if (copy_to_user(
  1207. user_buffer,
  1208. port->identify,
  1209. ATA_ID_WORDS * sizeof(u16))) {
  1210. rv = -EFAULT;
  1211. goto out;
  1212. }
  1213. }
  1214. out:
  1215. return rv;
  1216. }
  1217. /*
  1218. * Issue a standby immediate command to the device.
  1219. *
  1220. * @port Pointer to the port structure.
  1221. *
  1222. * return value
  1223. * 0 Command was executed successfully.
  1224. * -1 An error occurred while executing the command.
  1225. */
  1226. static int mtip_standby_immediate(struct mtip_port *port)
  1227. {
  1228. int rv;
  1229. struct host_to_dev_fis fis;
  1230. unsigned long start;
  1231. unsigned int timeout;
  1232. /* Build the FIS. */
  1233. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1234. fis.type = 0x27;
  1235. fis.opts = 1 << 7;
  1236. fis.command = ATA_CMD_STANDBYNOW1;
  1237. mtip_set_timeout(port->dd, &fis, &timeout, 0);
  1238. start = jiffies;
  1239. rv = mtip_exec_internal_command(port,
  1240. &fis,
  1241. 5,
  1242. 0,
  1243. 0,
  1244. 0,
  1245. GFP_ATOMIC,
  1246. timeout);
  1247. dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
  1248. jiffies_to_msecs(jiffies - start));
  1249. if (rv)
  1250. dev_warn(&port->dd->pdev->dev,
  1251. "STANDBY IMMEDIATE command failed.\n");
  1252. return rv;
  1253. }
  1254. /*
  1255. * Issue a READ LOG EXT command to the device.
  1256. *
  1257. * @port pointer to the port structure.
  1258. * @page page number to fetch
  1259. * @buffer pointer to buffer
  1260. * @buffer_dma dma address corresponding to @buffer
  1261. * @sectors page length to fetch, in sectors
  1262. *
  1263. * return value
  1264. * @rv return value from mtip_exec_internal_command()
  1265. */
  1266. static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
  1267. dma_addr_t buffer_dma, unsigned int sectors)
  1268. {
  1269. struct host_to_dev_fis fis;
  1270. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1271. fis.type = 0x27;
  1272. fis.opts = 1 << 7;
  1273. fis.command = ATA_CMD_READ_LOG_EXT;
  1274. fis.sect_count = sectors & 0xFF;
  1275. fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
  1276. fis.lba_low = page;
  1277. fis.lba_mid = 0;
  1278. fis.device = ATA_DEVICE_OBS;
  1279. memset(buffer, 0, sectors * ATA_SECT_SIZE);
  1280. return mtip_exec_internal_command(port,
  1281. &fis,
  1282. 5,
  1283. buffer_dma,
  1284. sectors * ATA_SECT_SIZE,
  1285. 0,
  1286. GFP_ATOMIC,
  1287. MTIP_INT_CMD_TIMEOUT_MS);
  1288. }
  1289. /*
  1290. * Issue a SMART READ DATA command to the device.
  1291. *
  1292. * @port pointer to the port structure.
  1293. * @buffer pointer to buffer
  1294. * @buffer_dma dma address corresponding to @buffer
  1295. *
  1296. * return value
  1297. * @rv return value from mtip_exec_internal_command()
  1298. */
  1299. static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
  1300. dma_addr_t buffer_dma)
  1301. {
  1302. struct host_to_dev_fis fis;
  1303. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1304. fis.type = 0x27;
  1305. fis.opts = 1 << 7;
  1306. fis.command = ATA_CMD_SMART;
  1307. fis.features = 0xD0;
  1308. fis.sect_count = 1;
  1309. fis.lba_mid = 0x4F;
  1310. fis.lba_hi = 0xC2;
  1311. fis.device = ATA_DEVICE_OBS;
  1312. return mtip_exec_internal_command(port,
  1313. &fis,
  1314. 5,
  1315. buffer_dma,
  1316. ATA_SECT_SIZE,
  1317. 0,
  1318. GFP_ATOMIC,
  1319. 15000);
  1320. }
  1321. /*
  1322. * Get the value of a smart attribute
  1323. *
  1324. * @port pointer to the port structure
  1325. * @id attribute number
  1326. * @attrib pointer to return attrib information corresponding to @id
  1327. *
  1328. * return value
  1329. * -EINVAL NULL buffer passed or unsupported attribute @id.
  1330. * -EPERM Identify data not valid, SMART not supported or not enabled
  1331. */
  1332. static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
  1333. struct smart_attr *attrib)
  1334. {
  1335. int rv, i;
  1336. struct smart_attr *pattr;
  1337. if (!attrib)
  1338. return -EINVAL;
  1339. if (!port->identify_valid) {
  1340. dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
  1341. return -EPERM;
  1342. }
  1343. if (!(port->identify[82] & 0x1)) {
  1344. dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
  1345. return -EPERM;
  1346. }
  1347. if (!(port->identify[85] & 0x1)) {
  1348. dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
  1349. return -EPERM;
  1350. }
  1351. memset(port->smart_buf, 0, ATA_SECT_SIZE);
  1352. rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
  1353. if (rv) {
  1354. dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
  1355. return rv;
  1356. }
  1357. pattr = (struct smart_attr *)(port->smart_buf + 2);
  1358. for (i = 0; i < 29; i++, pattr++)
  1359. if (pattr->attr_id == id) {
  1360. memcpy(attrib, pattr, sizeof(struct smart_attr));
  1361. break;
  1362. }
  1363. if (i == 29) {
  1364. dev_warn(&port->dd->pdev->dev,
  1365. "Query for invalid SMART attribute ID\n");
  1366. rv = -EINVAL;
  1367. }
  1368. return rv;
  1369. }
  1370. /*
  1371. * Trim unused sectors
  1372. *
  1373. * @dd pointer to driver_data structure
  1374. * @lba starting lba
  1375. * @len # of 512b sectors to trim
  1376. *
  1377. * return value
  1378. * -ENOMEM Out of dma memory
  1379. * -EINVAL Invalid parameters passed in, trim not supported
  1380. * -EIO Error submitting trim request to hw
  1381. */
  1382. static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
  1383. unsigned int len)
  1384. {
  1385. int i, rv = 0;
  1386. u64 tlba, tlen, sect_left;
  1387. struct mtip_trim_entry *buf;
  1388. dma_addr_t dma_addr;
  1389. struct host_to_dev_fis fis;
  1390. if (!len || dd->trim_supp == false)
  1391. return -EINVAL;
  1392. /* Trim request too big */
  1393. WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
  1394. /* Trim request not aligned on 4k boundary */
  1395. WARN_ON(len % 8 != 0);
  1396. /* Warn if vu_trim structure is too big */
  1397. WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
  1398. /* Allocate a DMA buffer for the trim structure */
  1399. buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
  1400. GFP_KERNEL);
  1401. if (!buf)
  1402. return -ENOMEM;
  1403. memset(buf, 0, ATA_SECT_SIZE);
  1404. for (i = 0, sect_left = len, tlba = lba;
  1405. i < MTIP_MAX_TRIM_ENTRIES && sect_left;
  1406. i++) {
  1407. tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
  1408. MTIP_MAX_TRIM_ENTRY_LEN :
  1409. sect_left);
  1410. buf[i].lba = __force_bit2int cpu_to_le32(tlba);
  1411. buf[i].range = __force_bit2int cpu_to_le16(tlen);
  1412. tlba += tlen;
  1413. sect_left -= tlen;
  1414. }
  1415. WARN_ON(sect_left != 0);
  1416. /* Build the fis */
  1417. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1418. fis.type = 0x27;
  1419. fis.opts = 1 << 7;
  1420. fis.command = 0xfb;
  1421. fis.features = 0x60;
  1422. fis.sect_count = 1;
  1423. fis.device = ATA_DEVICE_OBS;
  1424. if (mtip_exec_internal_command(dd->port,
  1425. &fis,
  1426. 5,
  1427. dma_addr,
  1428. ATA_SECT_SIZE,
  1429. 0,
  1430. GFP_KERNEL,
  1431. MTIP_TRIM_TIMEOUT_MS) < 0)
  1432. rv = -EIO;
  1433. dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
  1434. return rv;
  1435. }
  1436. /*
  1437. * Get the drive capacity.
  1438. *
  1439. * @dd Pointer to the device data structure.
  1440. * @sectors Pointer to the variable that will receive the sector count.
  1441. *
  1442. * return value
  1443. * 1 Capacity was returned successfully.
  1444. * 0 The identify information is invalid.
  1445. */
  1446. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1447. {
  1448. struct mtip_port *port = dd->port;
  1449. u64 total, raw0, raw1, raw2, raw3;
  1450. raw0 = port->identify[100];
  1451. raw1 = port->identify[101];
  1452. raw2 = port->identify[102];
  1453. raw3 = port->identify[103];
  1454. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1455. *sectors = total;
  1456. return (bool) !!port->identify_valid;
  1457. }
  1458. /*
  1459. * Display the identify command data.
  1460. *
  1461. * @port Pointer to the port data structure.
  1462. *
  1463. * return value
  1464. * None
  1465. */
  1466. static void mtip_dump_identify(struct mtip_port *port)
  1467. {
  1468. sector_t sectors;
  1469. unsigned short revid;
  1470. char cbuf[42];
  1471. if (!port->identify_valid)
  1472. return;
  1473. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1474. dev_info(&port->dd->pdev->dev,
  1475. "Serial No.: %s\n", cbuf);
  1476. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1477. dev_info(&port->dd->pdev->dev,
  1478. "Firmware Ver.: %s\n", cbuf);
  1479. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1480. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1481. dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
  1482. port->identify[128],
  1483. port->identify[128] & 0x4 ? "(LOCKED)" : "");
  1484. if (mtip_hw_get_capacity(port->dd, &sectors))
  1485. dev_info(&port->dd->pdev->dev,
  1486. "Capacity: %llu sectors (%llu MB)\n",
  1487. (u64)sectors,
  1488. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1489. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1490. switch (revid & 0xFF) {
  1491. case 0x1:
  1492. strlcpy(cbuf, "A0", 3);
  1493. break;
  1494. case 0x3:
  1495. strlcpy(cbuf, "A2", 3);
  1496. break;
  1497. default:
  1498. strlcpy(cbuf, "?", 2);
  1499. break;
  1500. }
  1501. dev_info(&port->dd->pdev->dev,
  1502. "Card Type: %s\n", cbuf);
  1503. }
  1504. /*
  1505. * Map the commands scatter list into the command table.
  1506. *
  1507. * @command Pointer to the command.
  1508. * @nents Number of scatter list entries.
  1509. *
  1510. * return value
  1511. * None
  1512. */
  1513. static inline void fill_command_sg(struct driver_data *dd,
  1514. struct mtip_cmd *command,
  1515. int nents)
  1516. {
  1517. int n;
  1518. unsigned int dma_len;
  1519. struct mtip_cmd_sg *command_sg;
  1520. struct scatterlist *sg = command->sg;
  1521. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1522. for (n = 0; n < nents; n++) {
  1523. dma_len = sg_dma_len(sg);
  1524. if (dma_len > 0x400000)
  1525. dev_err(&dd->pdev->dev,
  1526. "DMA segment length truncated\n");
  1527. command_sg->info = __force_bit2int
  1528. cpu_to_le32((dma_len-1) & 0x3FFFFF);
  1529. command_sg->dba = __force_bit2int
  1530. cpu_to_le32(sg_dma_address(sg));
  1531. command_sg->dba_upper = __force_bit2int
  1532. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1533. command_sg++;
  1534. sg++;
  1535. }
  1536. }
  1537. /*
  1538. * @brief Execute a drive command.
  1539. *
  1540. * return value 0 The command completed successfully.
  1541. * return value -1 An error occurred while executing the command.
  1542. */
  1543. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1544. {
  1545. struct host_to_dev_fis fis;
  1546. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1547. unsigned int to;
  1548. /* Build the FIS. */
  1549. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1550. fis.type = 0x27;
  1551. fis.opts = 1 << 7;
  1552. fis.command = command[0];
  1553. fis.features = command[1];
  1554. fis.sect_count = command[2];
  1555. fis.sector = command[3];
  1556. fis.cyl_low = command[4];
  1557. fis.cyl_hi = command[5];
  1558. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1559. mtip_set_timeout(port->dd, &fis, &to, 0);
  1560. dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
  1561. __func__,
  1562. command[0],
  1563. command[1],
  1564. command[2],
  1565. command[3],
  1566. command[4],
  1567. command[5],
  1568. command[6]);
  1569. /* Execute the command. */
  1570. if (mtip_exec_internal_command(port,
  1571. &fis,
  1572. 5,
  1573. 0,
  1574. 0,
  1575. 0,
  1576. GFP_KERNEL,
  1577. to) < 0) {
  1578. return -1;
  1579. }
  1580. command[0] = reply->command; /* Status*/
  1581. command[1] = reply->features; /* Error*/
  1582. command[4] = reply->cyl_low;
  1583. command[5] = reply->cyl_hi;
  1584. dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
  1585. __func__,
  1586. command[0],
  1587. command[1],
  1588. command[4],
  1589. command[5]);
  1590. return 0;
  1591. }
  1592. /*
  1593. * @brief Execute a drive command.
  1594. *
  1595. * @param port Pointer to the port data structure.
  1596. * @param command Pointer to the user specified command parameters.
  1597. * @param user_buffer Pointer to the user space buffer where read sector
  1598. * data should be copied.
  1599. *
  1600. * return value 0 The command completed successfully.
  1601. * return value -EFAULT An error occurred while copying the completion
  1602. * data to the user space buffer.
  1603. * return value -1 An error occurred while executing the command.
  1604. */
  1605. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1606. void __user *user_buffer)
  1607. {
  1608. struct host_to_dev_fis fis;
  1609. struct host_to_dev_fis *reply;
  1610. u8 *buf = NULL;
  1611. dma_addr_t dma_addr = 0;
  1612. int rv = 0, xfer_sz = command[3];
  1613. unsigned int to;
  1614. if (xfer_sz) {
  1615. if (!user_buffer)
  1616. return -EFAULT;
  1617. buf = dmam_alloc_coherent(&port->dd->pdev->dev,
  1618. ATA_SECT_SIZE * xfer_sz,
  1619. &dma_addr,
  1620. GFP_KERNEL);
  1621. if (!buf) {
  1622. dev_err(&port->dd->pdev->dev,
  1623. "Memory allocation failed (%d bytes)\n",
  1624. ATA_SECT_SIZE * xfer_sz);
  1625. return -ENOMEM;
  1626. }
  1627. memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
  1628. }
  1629. /* Build the FIS. */
  1630. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1631. fis.type = 0x27;
  1632. fis.opts = 1 << 7;
  1633. fis.command = command[0];
  1634. fis.features = command[2];
  1635. fis.sect_count = command[3];
  1636. if (fis.command == ATA_CMD_SMART) {
  1637. fis.sector = command[1];
  1638. fis.cyl_low = 0x4F;
  1639. fis.cyl_hi = 0xC2;
  1640. }
  1641. mtip_set_timeout(port->dd, &fis, &to, 0);
  1642. if (xfer_sz)
  1643. reply = (port->rxfis + RX_FIS_PIO_SETUP);
  1644. else
  1645. reply = (port->rxfis + RX_FIS_D2H_REG);
  1646. dbg_printk(MTIP_DRV_NAME
  1647. " %s: User Command: cmd %x, sect %x, "
  1648. "feat %x, sectcnt %x\n",
  1649. __func__,
  1650. command[0],
  1651. command[1],
  1652. command[2],
  1653. command[3]);
  1654. /* Execute the command. */
  1655. if (mtip_exec_internal_command(port,
  1656. &fis,
  1657. 5,
  1658. (xfer_sz ? dma_addr : 0),
  1659. (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
  1660. 0,
  1661. GFP_KERNEL,
  1662. to)
  1663. < 0) {
  1664. rv = -EFAULT;
  1665. goto exit_drive_command;
  1666. }
  1667. /* Collect the completion status. */
  1668. command[0] = reply->command; /* Status*/
  1669. command[1] = reply->features; /* Error*/
  1670. command[2] = reply->sect_count;
  1671. dbg_printk(MTIP_DRV_NAME
  1672. " %s: Completion Status: stat %x, "
  1673. "err %x, nsect %x\n",
  1674. __func__,
  1675. command[0],
  1676. command[1],
  1677. command[2]);
  1678. if (xfer_sz) {
  1679. if (copy_to_user(user_buffer,
  1680. buf,
  1681. ATA_SECT_SIZE * command[3])) {
  1682. rv = -EFAULT;
  1683. goto exit_drive_command;
  1684. }
  1685. }
  1686. exit_drive_command:
  1687. if (buf)
  1688. dmam_free_coherent(&port->dd->pdev->dev,
  1689. ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
  1690. return rv;
  1691. }
  1692. /*
  1693. * Indicates whether a command has a single sector payload.
  1694. *
  1695. * @command passed to the device to perform the certain event.
  1696. * @features passed to the device to perform the certain event.
  1697. *
  1698. * return value
  1699. * 1 command is one that always has a single sector payload,
  1700. * regardless of the value in the Sector Count field.
  1701. * 0 otherwise
  1702. *
  1703. */
  1704. static unsigned int implicit_sector(unsigned char command,
  1705. unsigned char features)
  1706. {
  1707. unsigned int rv = 0;
  1708. /* list of commands that have an implicit sector count of 1 */
  1709. switch (command) {
  1710. case ATA_CMD_SEC_SET_PASS:
  1711. case ATA_CMD_SEC_UNLOCK:
  1712. case ATA_CMD_SEC_ERASE_PREP:
  1713. case ATA_CMD_SEC_ERASE_UNIT:
  1714. case ATA_CMD_SEC_FREEZE_LOCK:
  1715. case ATA_CMD_SEC_DISABLE_PASS:
  1716. case ATA_CMD_PMP_READ:
  1717. case ATA_CMD_PMP_WRITE:
  1718. rv = 1;
  1719. break;
  1720. case ATA_CMD_SET_MAX:
  1721. if (features == ATA_SET_MAX_UNLOCK)
  1722. rv = 1;
  1723. break;
  1724. case ATA_CMD_SMART:
  1725. if ((features == ATA_SMART_READ_VALUES) ||
  1726. (features == ATA_SMART_READ_THRESHOLDS))
  1727. rv = 1;
  1728. break;
  1729. case ATA_CMD_CONF_OVERLAY:
  1730. if ((features == ATA_DCO_IDENTIFY) ||
  1731. (features == ATA_DCO_SET))
  1732. rv = 1;
  1733. break;
  1734. }
  1735. return rv;
  1736. }
  1737. /*
  1738. * Executes a taskfile
  1739. * See ide_taskfile_ioctl() for derivation
  1740. */
  1741. static int exec_drive_taskfile(struct driver_data *dd,
  1742. void __user *buf,
  1743. ide_task_request_t *req_task,
  1744. int outtotal)
  1745. {
  1746. struct host_to_dev_fis fis;
  1747. struct host_to_dev_fis *reply;
  1748. u8 *outbuf = NULL;
  1749. u8 *inbuf = NULL;
  1750. dma_addr_t outbuf_dma = 0;
  1751. dma_addr_t inbuf_dma = 0;
  1752. dma_addr_t dma_buffer = 0;
  1753. int err = 0;
  1754. unsigned int taskin = 0;
  1755. unsigned int taskout = 0;
  1756. u8 nsect = 0;
  1757. unsigned int timeout;
  1758. unsigned int force_single_sector;
  1759. unsigned int transfer_size;
  1760. unsigned long task_file_data;
  1761. int intotal = outtotal + req_task->out_size;
  1762. int erasemode = 0;
  1763. taskout = req_task->out_size;
  1764. taskin = req_task->in_size;
  1765. /* 130560 = 512 * 0xFF*/
  1766. if (taskin > 130560 || taskout > 130560) {
  1767. err = -EINVAL;
  1768. goto abort;
  1769. }
  1770. if (taskout) {
  1771. outbuf = kzalloc(taskout, GFP_KERNEL);
  1772. if (outbuf == NULL) {
  1773. err = -ENOMEM;
  1774. goto abort;
  1775. }
  1776. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1777. err = -EFAULT;
  1778. goto abort;
  1779. }
  1780. outbuf_dma = pci_map_single(dd->pdev,
  1781. outbuf,
  1782. taskout,
  1783. DMA_TO_DEVICE);
  1784. if (outbuf_dma == 0) {
  1785. err = -ENOMEM;
  1786. goto abort;
  1787. }
  1788. dma_buffer = outbuf_dma;
  1789. }
  1790. if (taskin) {
  1791. inbuf = kzalloc(taskin, GFP_KERNEL);
  1792. if (inbuf == NULL) {
  1793. err = -ENOMEM;
  1794. goto abort;
  1795. }
  1796. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1797. err = -EFAULT;
  1798. goto abort;
  1799. }
  1800. inbuf_dma = pci_map_single(dd->pdev,
  1801. inbuf,
  1802. taskin, DMA_FROM_DEVICE);
  1803. if (inbuf_dma == 0) {
  1804. err = -ENOMEM;
  1805. goto abort;
  1806. }
  1807. dma_buffer = inbuf_dma;
  1808. }
  1809. /* only supports PIO and non-data commands from this ioctl. */
  1810. switch (req_task->data_phase) {
  1811. case TASKFILE_OUT:
  1812. nsect = taskout / ATA_SECT_SIZE;
  1813. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1814. break;
  1815. case TASKFILE_IN:
  1816. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1817. break;
  1818. case TASKFILE_NO_DATA:
  1819. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1820. break;
  1821. default:
  1822. err = -EINVAL;
  1823. goto abort;
  1824. }
  1825. /* Build the FIS. */
  1826. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1827. fis.type = 0x27;
  1828. fis.opts = 1 << 7;
  1829. fis.command = req_task->io_ports[7];
  1830. fis.features = req_task->io_ports[1];
  1831. fis.sect_count = req_task->io_ports[2];
  1832. fis.lba_low = req_task->io_ports[3];
  1833. fis.lba_mid = req_task->io_ports[4];
  1834. fis.lba_hi = req_task->io_ports[5];
  1835. /* Clear the dev bit*/
  1836. fis.device = req_task->io_ports[6] & ~0x10;
  1837. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1838. req_task->in_flags.all =
  1839. IDE_TASKFILE_STD_IN_FLAGS |
  1840. (IDE_HOB_STD_IN_FLAGS << 8);
  1841. fis.lba_low_ex = req_task->hob_ports[3];
  1842. fis.lba_mid_ex = req_task->hob_ports[4];
  1843. fis.lba_hi_ex = req_task->hob_ports[5];
  1844. fis.features_ex = req_task->hob_ports[1];
  1845. fis.sect_cnt_ex = req_task->hob_ports[2];
  1846. } else {
  1847. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1848. }
  1849. force_single_sector = implicit_sector(fis.command, fis.features);
  1850. if ((taskin || taskout) && (!fis.sect_count)) {
  1851. if (nsect)
  1852. fis.sect_count = nsect;
  1853. else {
  1854. if (!force_single_sector) {
  1855. dev_warn(&dd->pdev->dev,
  1856. "data movement but "
  1857. "sect_count is 0\n");
  1858. err = -EINVAL;
  1859. goto abort;
  1860. }
  1861. }
  1862. }
  1863. dbg_printk(MTIP_DRV_NAME
  1864. " %s: cmd %x, feat %x, nsect %x,"
  1865. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1866. " head/dev %x\n",
  1867. __func__,
  1868. fis.command,
  1869. fis.features,
  1870. fis.sect_count,
  1871. fis.lba_low,
  1872. fis.lba_mid,
  1873. fis.lba_hi,
  1874. fis.device);
  1875. /* check for erase mode support during secure erase.*/
  1876. if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
  1877. (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
  1878. erasemode = 1;
  1879. }
  1880. mtip_set_timeout(dd, &fis, &timeout, erasemode);
  1881. /* Determine the correct transfer size.*/
  1882. if (force_single_sector)
  1883. transfer_size = ATA_SECT_SIZE;
  1884. else
  1885. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1886. /* Execute the command.*/
  1887. if (mtip_exec_internal_command(dd->port,
  1888. &fis,
  1889. 5,
  1890. dma_buffer,
  1891. transfer_size,
  1892. 0,
  1893. GFP_KERNEL,
  1894. timeout) < 0) {
  1895. err = -EIO;
  1896. goto abort;
  1897. }
  1898. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  1899. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  1900. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  1901. req_task->io_ports[7] = reply->control;
  1902. } else {
  1903. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  1904. req_task->io_ports[7] = reply->command;
  1905. }
  1906. /* reclaim the DMA buffers.*/
  1907. if (inbuf_dma)
  1908. pci_unmap_single(dd->pdev, inbuf_dma,
  1909. taskin, DMA_FROM_DEVICE);
  1910. if (outbuf_dma)
  1911. pci_unmap_single(dd->pdev, outbuf_dma,
  1912. taskout, DMA_TO_DEVICE);
  1913. inbuf_dma = 0;
  1914. outbuf_dma = 0;
  1915. /* return the ATA registers to the caller.*/
  1916. req_task->io_ports[1] = reply->features;
  1917. req_task->io_ports[2] = reply->sect_count;
  1918. req_task->io_ports[3] = reply->lba_low;
  1919. req_task->io_ports[4] = reply->lba_mid;
  1920. req_task->io_ports[5] = reply->lba_hi;
  1921. req_task->io_ports[6] = reply->device;
  1922. if (req_task->out_flags.all & 1) {
  1923. req_task->hob_ports[3] = reply->lba_low_ex;
  1924. req_task->hob_ports[4] = reply->lba_mid_ex;
  1925. req_task->hob_ports[5] = reply->lba_hi_ex;
  1926. req_task->hob_ports[1] = reply->features_ex;
  1927. req_task->hob_ports[2] = reply->sect_cnt_ex;
  1928. }
  1929. dbg_printk(MTIP_DRV_NAME
  1930. " %s: Completion: stat %x,"
  1931. "err %x, sect_cnt %x, lbalo %x,"
  1932. "lbamid %x, lbahi %x, dev %x\n",
  1933. __func__,
  1934. req_task->io_ports[7],
  1935. req_task->io_ports[1],
  1936. req_task->io_ports[2],
  1937. req_task->io_ports[3],
  1938. req_task->io_ports[4],
  1939. req_task->io_ports[5],
  1940. req_task->io_ports[6]);
  1941. if (taskout) {
  1942. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  1943. err = -EFAULT;
  1944. goto abort;
  1945. }
  1946. }
  1947. if (taskin) {
  1948. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  1949. err = -EFAULT;
  1950. goto abort;
  1951. }
  1952. }
  1953. abort:
  1954. if (inbuf_dma)
  1955. pci_unmap_single(dd->pdev, inbuf_dma,
  1956. taskin, DMA_FROM_DEVICE);
  1957. if (outbuf_dma)
  1958. pci_unmap_single(dd->pdev, outbuf_dma,
  1959. taskout, DMA_TO_DEVICE);
  1960. kfree(outbuf);
  1961. kfree(inbuf);
  1962. return err;
  1963. }
  1964. /*
  1965. * Handle IOCTL calls from the Block Layer.
  1966. *
  1967. * This function is called by the Block Layer when it receives an IOCTL
  1968. * command that it does not understand. If the IOCTL command is not supported
  1969. * this function returns -ENOTTY.
  1970. *
  1971. * @dd Pointer to the driver data structure.
  1972. * @cmd IOCTL command passed from the Block Layer.
  1973. * @arg IOCTL argument passed from the Block Layer.
  1974. *
  1975. * return value
  1976. * 0 The IOCTL completed successfully.
  1977. * -ENOTTY The specified command is not supported.
  1978. * -EFAULT An error occurred copying data to a user space buffer.
  1979. * -EIO An error occurred while executing the command.
  1980. */
  1981. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  1982. unsigned long arg)
  1983. {
  1984. switch (cmd) {
  1985. case HDIO_GET_IDENTITY:
  1986. {
  1987. if (copy_to_user((void __user *)arg, dd->port->identify,
  1988. sizeof(u16) * ATA_ID_WORDS))
  1989. return -EFAULT;
  1990. break;
  1991. }
  1992. case HDIO_DRIVE_CMD:
  1993. {
  1994. u8 drive_command[4];
  1995. /* Copy the user command info to our buffer. */
  1996. if (copy_from_user(drive_command,
  1997. (void __user *) arg,
  1998. sizeof(drive_command)))
  1999. return -EFAULT;
  2000. /* Execute the drive command. */
  2001. if (exec_drive_command(dd->port,
  2002. drive_command,
  2003. (void __user *) (arg+4)))
  2004. return -EIO;
  2005. /* Copy the status back to the users buffer. */
  2006. if (copy_to_user((void __user *) arg,
  2007. drive_command,
  2008. sizeof(drive_command)))
  2009. return -EFAULT;
  2010. break;
  2011. }
  2012. case HDIO_DRIVE_TASK:
  2013. {
  2014. u8 drive_command[7];
  2015. /* Copy the user command info to our buffer. */
  2016. if (copy_from_user(drive_command,
  2017. (void __user *) arg,
  2018. sizeof(drive_command)))
  2019. return -EFAULT;
  2020. /* Execute the drive command. */
  2021. if (exec_drive_task(dd->port, drive_command))
  2022. return -EIO;
  2023. /* Copy the status back to the users buffer. */
  2024. if (copy_to_user((void __user *) arg,
  2025. drive_command,
  2026. sizeof(drive_command)))
  2027. return -EFAULT;
  2028. break;
  2029. }
  2030. case HDIO_DRIVE_TASKFILE: {
  2031. ide_task_request_t req_task;
  2032. int ret, outtotal;
  2033. if (copy_from_user(&req_task, (void __user *) arg,
  2034. sizeof(req_task)))
  2035. return -EFAULT;
  2036. outtotal = sizeof(req_task);
  2037. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2038. &req_task, outtotal);
  2039. if (copy_to_user((void __user *) arg, &req_task,
  2040. sizeof(req_task)))
  2041. return -EFAULT;
  2042. return ret;
  2043. }
  2044. default:
  2045. return -EINVAL;
  2046. }
  2047. return 0;
  2048. }
  2049. /*
  2050. * Submit an IO to the hw
  2051. *
  2052. * This function is called by the block layer to issue an io
  2053. * to the device. Upon completion, the callback function will
  2054. * be called with the data parameter passed as the callback data.
  2055. *
  2056. * @dd Pointer to the driver data structure.
  2057. * @start First sector to read.
  2058. * @nsect Number of sectors to read.
  2059. * @nents Number of entries in scatter list for the read command.
  2060. * @tag The tag of this read command.
  2061. * @callback Pointer to the function that should be called
  2062. * when the read completes.
  2063. * @data Callback data passed to the callback function
  2064. * when the read completes.
  2065. * @dir Direction (read or write)
  2066. *
  2067. * return value
  2068. * None
  2069. */
  2070. static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
  2071. struct mtip_cmd *command, int nents,
  2072. struct blk_mq_hw_ctx *hctx)
  2073. {
  2074. struct host_to_dev_fis *fis;
  2075. struct mtip_port *port = dd->port;
  2076. int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2077. u64 start = blk_rq_pos(rq);
  2078. unsigned int nsect = blk_rq_sectors(rq);
  2079. /* Map the scatter list for DMA access */
  2080. nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
  2081. prefetch(&port->flags);
  2082. command->scatter_ents = nents;
  2083. /*
  2084. * The number of retries for this command before it is
  2085. * reported as a failure to the upper layers.
  2086. */
  2087. command->retries = MTIP_MAX_RETRIES;
  2088. /* Fill out fis */
  2089. fis = command->command;
  2090. fis->type = 0x27;
  2091. fis->opts = 1 << 7;
  2092. if (dma_dir == DMA_FROM_DEVICE)
  2093. fis->command = ATA_CMD_FPDMA_READ;
  2094. else
  2095. fis->command = ATA_CMD_FPDMA_WRITE;
  2096. fis->lba_low = start & 0xFF;
  2097. fis->lba_mid = (start >> 8) & 0xFF;
  2098. fis->lba_hi = (start >> 16) & 0xFF;
  2099. fis->lba_low_ex = (start >> 24) & 0xFF;
  2100. fis->lba_mid_ex = (start >> 32) & 0xFF;
  2101. fis->lba_hi_ex = (start >> 40) & 0xFF;
  2102. fis->device = 1 << 6;
  2103. fis->features = nsect & 0xFF;
  2104. fis->features_ex = (nsect >> 8) & 0xFF;
  2105. fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
  2106. fis->sect_cnt_ex = 0;
  2107. fis->control = 0;
  2108. fis->res2 = 0;
  2109. fis->res3 = 0;
  2110. fill_command_sg(dd, command, nents);
  2111. if (unlikely(command->unaligned))
  2112. fis->device |= 1 << 7;
  2113. /* Populate the command header */
  2114. command->command_header->opts =
  2115. __force_bit2int cpu_to_le32(
  2116. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  2117. command->command_header->byte_count = 0;
  2118. /*
  2119. * Set the completion function and data for the command
  2120. * within this layer.
  2121. */
  2122. command->comp_data = dd;
  2123. command->comp_func = mtip_async_complete;
  2124. command->direction = dma_dir;
  2125. /*
  2126. * To prevent this command from being issued
  2127. * if an internal command is in progress or error handling is active.
  2128. */
  2129. if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
  2130. set_bit(rq->tag, port->cmds_to_issue);
  2131. set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2132. return;
  2133. }
  2134. /* Issue the command to the hardware */
  2135. mtip_issue_ncq_command(port, rq->tag);
  2136. }
  2137. /*
  2138. * Sysfs status dump.
  2139. *
  2140. * @dev Pointer to the device structure, passed by the kernrel.
  2141. * @attr Pointer to the device_attribute structure passed by the kernel.
  2142. * @buf Pointer to the char buffer that will receive the stats info.
  2143. *
  2144. * return value
  2145. * The size, in bytes, of the data copied into buf.
  2146. */
  2147. static ssize_t mtip_hw_show_status(struct device *dev,
  2148. struct device_attribute *attr,
  2149. char *buf)
  2150. {
  2151. struct driver_data *dd = dev_to_disk(dev)->private_data;
  2152. int size = 0;
  2153. if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
  2154. size += sprintf(buf, "%s", "thermal_shutdown\n");
  2155. else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
  2156. size += sprintf(buf, "%s", "write_protect\n");
  2157. else
  2158. size += sprintf(buf, "%s", "online\n");
  2159. return size;
  2160. }
  2161. static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
  2162. /* debugsfs entries */
  2163. static ssize_t show_device_status(struct device_driver *drv, char *buf)
  2164. {
  2165. int size = 0;
  2166. struct driver_data *dd, *tmp;
  2167. unsigned long flags;
  2168. char id_buf[42];
  2169. u16 status = 0;
  2170. spin_lock_irqsave(&dev_lock, flags);
  2171. size += sprintf(&buf[size], "Devices Present:\n");
  2172. list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
  2173. if (dd->pdev) {
  2174. if (dd->port &&
  2175. dd->port->identify &&
  2176. dd->port->identify_valid) {
  2177. strlcpy(id_buf,
  2178. (char *) (dd->port->identify + 10), 21);
  2179. status = *(dd->port->identify + 141);
  2180. } else {
  2181. memset(id_buf, 0, 42);
  2182. status = 0;
  2183. }
  2184. if (dd->port &&
  2185. test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
  2186. size += sprintf(&buf[size],
  2187. " device %s %s (ftl rebuild %d %%)\n",
  2188. dev_name(&dd->pdev->dev),
  2189. id_buf,
  2190. status);
  2191. } else {
  2192. size += sprintf(&buf[size],
  2193. " device %s %s\n",
  2194. dev_name(&dd->pdev->dev),
  2195. id_buf);
  2196. }
  2197. }
  2198. }
  2199. size += sprintf(&buf[size], "Devices Being Removed:\n");
  2200. list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
  2201. if (dd->pdev) {
  2202. if (dd->port &&
  2203. dd->port->identify &&
  2204. dd->port->identify_valid) {
  2205. strlcpy(id_buf,
  2206. (char *) (dd->port->identify+10), 21);
  2207. status = *(dd->port->identify + 141);
  2208. } else {
  2209. memset(id_buf, 0, 42);
  2210. status = 0;
  2211. }
  2212. if (dd->port &&
  2213. test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
  2214. size += sprintf(&buf[size],
  2215. " device %s %s (ftl rebuild %d %%)\n",
  2216. dev_name(&dd->pdev->dev),
  2217. id_buf,
  2218. status);
  2219. } else {
  2220. size += sprintf(&buf[size],
  2221. " device %s %s\n",
  2222. dev_name(&dd->pdev->dev),
  2223. id_buf);
  2224. }
  2225. }
  2226. }
  2227. spin_unlock_irqrestore(&dev_lock, flags);
  2228. return size;
  2229. }
  2230. static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
  2231. size_t len, loff_t *offset)
  2232. {
  2233. struct driver_data *dd = (struct driver_data *)f->private_data;
  2234. int size = *offset;
  2235. char *buf;
  2236. int rv = 0;
  2237. if (!len || *offset)
  2238. return 0;
  2239. buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
  2240. if (!buf) {
  2241. dev_err(&dd->pdev->dev,
  2242. "Memory allocation: status buffer\n");
  2243. return -ENOMEM;
  2244. }
  2245. size += show_device_status(NULL, buf);
  2246. *offset = size <= len ? size : len;
  2247. size = copy_to_user(ubuf, buf, *offset);
  2248. if (size)
  2249. rv = -EFAULT;
  2250. kfree(buf);
  2251. return rv ? rv : *offset;
  2252. }
  2253. static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
  2254. size_t len, loff_t *offset)
  2255. {
  2256. struct driver_data *dd = (struct driver_data *)f->private_data;
  2257. char *buf;
  2258. u32 group_allocated;
  2259. int size = *offset;
  2260. int n, rv = 0;
  2261. if (!len || size)
  2262. return 0;
  2263. buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
  2264. if (!buf) {
  2265. dev_err(&dd->pdev->dev,
  2266. "Memory allocation: register buffer\n");
  2267. return -ENOMEM;
  2268. }
  2269. size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
  2270. for (n = dd->slot_groups-1; n >= 0; n--)
  2271. size += sprintf(&buf[size], "%08X ",
  2272. readl(dd->port->s_active[n]));
  2273. size += sprintf(&buf[size], "]\n");
  2274. size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
  2275. for (n = dd->slot_groups-1; n >= 0; n--)
  2276. size += sprintf(&buf[size], "%08X ",
  2277. readl(dd->port->cmd_issue[n]));
  2278. size += sprintf(&buf[size], "]\n");
  2279. size += sprintf(&buf[size], "H/ Completed : [ 0x");
  2280. for (n = dd->slot_groups-1; n >= 0; n--)
  2281. size += sprintf(&buf[size], "%08X ",
  2282. readl(dd->port->completed[n]));
  2283. size += sprintf(&buf[size], "]\n");
  2284. size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
  2285. readl(dd->port->mmio + PORT_IRQ_STAT));
  2286. size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
  2287. readl(dd->mmio + HOST_IRQ_STAT));
  2288. size += sprintf(&buf[size], "\n");
  2289. size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
  2290. for (n = dd->slot_groups-1; n >= 0; n--) {
  2291. if (sizeof(long) > sizeof(u32))
  2292. group_allocated =
  2293. dd->port->cmds_to_issue[n/2] >> (32*(n&1));
  2294. else
  2295. group_allocated = dd->port->cmds_to_issue[n];
  2296. size += sprintf(&buf[size], "%08X ", group_allocated);
  2297. }
  2298. size += sprintf(&buf[size], "]\n");
  2299. *offset = size <= len ? size : len;
  2300. size = copy_to_user(ubuf, buf, *offset);
  2301. if (size)
  2302. rv = -EFAULT;
  2303. kfree(buf);
  2304. return rv ? rv : *offset;
  2305. }
  2306. static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
  2307. size_t len, loff_t *offset)
  2308. {
  2309. struct driver_data *dd = (struct driver_data *)f->private_data;
  2310. char *buf;
  2311. int size = *offset;
  2312. int rv = 0;
  2313. if (!len || size)
  2314. return 0;
  2315. buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
  2316. if (!buf) {
  2317. dev_err(&dd->pdev->dev,
  2318. "Memory allocation: flag buffer\n");
  2319. return -ENOMEM;
  2320. }
  2321. size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
  2322. dd->port->flags);
  2323. size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
  2324. dd->dd_flag);
  2325. *offset = size <= len ? size : len;
  2326. size = copy_to_user(ubuf, buf, *offset);
  2327. if (size)
  2328. rv = -EFAULT;
  2329. kfree(buf);
  2330. return rv ? rv : *offset;
  2331. }
  2332. static const struct file_operations mtip_device_status_fops = {
  2333. .owner = THIS_MODULE,
  2334. .open = simple_open,
  2335. .read = mtip_hw_read_device_status,
  2336. .llseek = no_llseek,
  2337. };
  2338. static const struct file_operations mtip_regs_fops = {
  2339. .owner = THIS_MODULE,
  2340. .open = simple_open,
  2341. .read = mtip_hw_read_registers,
  2342. .llseek = no_llseek,
  2343. };
  2344. static const struct file_operations mtip_flags_fops = {
  2345. .owner = THIS_MODULE,
  2346. .open = simple_open,
  2347. .read = mtip_hw_read_flags,
  2348. .llseek = no_llseek,
  2349. };
  2350. /*
  2351. * Create the sysfs related attributes.
  2352. *
  2353. * @dd Pointer to the driver data structure.
  2354. * @kobj Pointer to the kobj for the block device.
  2355. *
  2356. * return value
  2357. * 0 Operation completed successfully.
  2358. * -EINVAL Invalid parameter.
  2359. */
  2360. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  2361. {
  2362. if (!kobj || !dd)
  2363. return -EINVAL;
  2364. if (sysfs_create_file(kobj, &dev_attr_status.attr))
  2365. dev_warn(&dd->pdev->dev,
  2366. "Error creating 'status' sysfs entry\n");
  2367. return 0;
  2368. }
  2369. /*
  2370. * Remove the sysfs related attributes.
  2371. *
  2372. * @dd Pointer to the driver data structure.
  2373. * @kobj Pointer to the kobj for the block device.
  2374. *
  2375. * return value
  2376. * 0 Operation completed successfully.
  2377. * -EINVAL Invalid parameter.
  2378. */
  2379. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  2380. {
  2381. if (!kobj || !dd)
  2382. return -EINVAL;
  2383. sysfs_remove_file(kobj, &dev_attr_status.attr);
  2384. return 0;
  2385. }
  2386. static int mtip_hw_debugfs_init(struct driver_data *dd)
  2387. {
  2388. if (!dfs_parent)
  2389. return -1;
  2390. dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
  2391. if (IS_ERR_OR_NULL(dd->dfs_node)) {
  2392. dev_warn(&dd->pdev->dev,
  2393. "Error creating node %s under debugfs\n",
  2394. dd->disk->disk_name);
  2395. dd->dfs_node = NULL;
  2396. return -1;
  2397. }
  2398. debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
  2399. &mtip_flags_fops);
  2400. debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
  2401. &mtip_regs_fops);
  2402. return 0;
  2403. }
  2404. static void mtip_hw_debugfs_exit(struct driver_data *dd)
  2405. {
  2406. if (dd->dfs_node)
  2407. debugfs_remove_recursive(dd->dfs_node);
  2408. }
  2409. /*
  2410. * Perform any init/resume time hardware setup
  2411. *
  2412. * @dd Pointer to the driver data structure.
  2413. *
  2414. * return value
  2415. * None
  2416. */
  2417. static inline void hba_setup(struct driver_data *dd)
  2418. {
  2419. u32 hwdata;
  2420. hwdata = readl(dd->mmio + HOST_HSORG);
  2421. /* interrupt bug workaround: use only 1 IS bit.*/
  2422. writel(hwdata |
  2423. HSORG_DISABLE_SLOTGRP_INTR |
  2424. HSORG_DISABLE_SLOTGRP_PXIS,
  2425. dd->mmio + HOST_HSORG);
  2426. }
  2427. static int mtip_device_unaligned_constrained(struct driver_data *dd)
  2428. {
  2429. return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
  2430. }
  2431. /*
  2432. * Detect the details of the product, and store anything needed
  2433. * into the driver data structure. This includes product type and
  2434. * version and number of slot groups.
  2435. *
  2436. * @dd Pointer to the driver data structure.
  2437. *
  2438. * return value
  2439. * None
  2440. */
  2441. static void mtip_detect_product(struct driver_data *dd)
  2442. {
  2443. u32 hwdata;
  2444. unsigned int rev, slotgroups;
  2445. /*
  2446. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2447. * info register:
  2448. * [15:8] hardware/software interface rev#
  2449. * [ 3] asic-style interface
  2450. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2451. */
  2452. hwdata = readl(dd->mmio + HOST_HSORG);
  2453. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2454. dd->slot_groups = 1;
  2455. if (hwdata & 0x8) {
  2456. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2457. rev = (hwdata & HSORG_HWREV) >> 8;
  2458. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2459. dev_info(&dd->pdev->dev,
  2460. "ASIC-FPGA design, HS rev 0x%x, "
  2461. "%i slot groups [%i slots]\n",
  2462. rev,
  2463. slotgroups,
  2464. slotgroups * 32);
  2465. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2466. dev_warn(&dd->pdev->dev,
  2467. "Warning: driver only supports "
  2468. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2469. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2470. }
  2471. dd->slot_groups = slotgroups;
  2472. return;
  2473. }
  2474. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2475. }
  2476. /*
  2477. * Blocking wait for FTL rebuild to complete
  2478. *
  2479. * @dd Pointer to the DRIVER_DATA structure.
  2480. *
  2481. * return value
  2482. * 0 FTL rebuild completed successfully
  2483. * -EFAULT FTL rebuild error/timeout/interruption
  2484. */
  2485. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2486. {
  2487. unsigned long timeout, cnt = 0, start;
  2488. dev_warn(&dd->pdev->dev,
  2489. "FTL rebuild in progress. Polling for completion.\n");
  2490. start = jiffies;
  2491. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2492. do {
  2493. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2494. &dd->dd_flag)))
  2495. return -EFAULT;
  2496. if (mtip_check_surprise_removal(dd->pdev))
  2497. return -EFAULT;
  2498. if (mtip_get_identify(dd->port, NULL) < 0)
  2499. return -EFAULT;
  2500. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2501. MTIP_FTL_REBUILD_MAGIC) {
  2502. ssleep(1);
  2503. /* Print message every 3 minutes */
  2504. if (cnt++ >= 180) {
  2505. dev_warn(&dd->pdev->dev,
  2506. "FTL rebuild in progress (%d secs).\n",
  2507. jiffies_to_msecs(jiffies - start) / 1000);
  2508. cnt = 0;
  2509. }
  2510. } else {
  2511. dev_warn(&dd->pdev->dev,
  2512. "FTL rebuild complete (%d secs).\n",
  2513. jiffies_to_msecs(jiffies - start) / 1000);
  2514. mtip_block_initialize(dd);
  2515. return 0;
  2516. }
  2517. } while (time_before(jiffies, timeout));
  2518. /* Check for timeout */
  2519. dev_err(&dd->pdev->dev,
  2520. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2521. jiffies_to_msecs(jiffies - start) / 1000);
  2522. return -EFAULT;
  2523. }
  2524. /*
  2525. * service thread to issue queued commands
  2526. *
  2527. * @data Pointer to the driver data structure.
  2528. *
  2529. * return value
  2530. * 0
  2531. */
  2532. static int mtip_service_thread(void *data)
  2533. {
  2534. struct driver_data *dd = (struct driver_data *)data;
  2535. unsigned long slot, slot_start, slot_wrap;
  2536. unsigned int num_cmd_slots = dd->slot_groups * 32;
  2537. struct mtip_port *port = dd->port;
  2538. while (1) {
  2539. if (kthread_should_stop() ||
  2540. test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2541. goto st_out;
  2542. clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2543. /*
  2544. * the condition is to check neither an internal command is
  2545. * is in progress nor error handling is active
  2546. */
  2547. wait_event_interruptible(port->svc_wait, (port->flags) &&
  2548. !(port->flags & MTIP_PF_PAUSE_IO));
  2549. set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
  2550. if (kthread_should_stop() ||
  2551. test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
  2552. goto st_out;
  2553. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  2554. &dd->dd_flag)))
  2555. goto st_out;
  2556. restart_eh:
  2557. /* Demux bits: start with error handling */
  2558. if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
  2559. mtip_handle_tfe(dd);
  2560. clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
  2561. }
  2562. if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
  2563. goto restart_eh;
  2564. if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
  2565. slot = 1;
  2566. /* used to restrict the loop to one iteration */
  2567. slot_start = num_cmd_slots;
  2568. slot_wrap = 0;
  2569. while (1) {
  2570. slot = find_next_bit(port->cmds_to_issue,
  2571. num_cmd_slots, slot);
  2572. if (slot_wrap == 1) {
  2573. if ((slot_start >= slot) ||
  2574. (slot >= num_cmd_slots))
  2575. break;
  2576. }
  2577. if (unlikely(slot_start == num_cmd_slots))
  2578. slot_start = slot;
  2579. if (unlikely(slot == num_cmd_slots)) {
  2580. slot = 1;
  2581. slot_wrap = 1;
  2582. continue;
  2583. }
  2584. /* Issue the command to the hardware */
  2585. mtip_issue_ncq_command(port, slot);
  2586. clear_bit(slot, port->cmds_to_issue);
  2587. }
  2588. clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
  2589. }
  2590. if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
  2591. if (mtip_ftl_rebuild_poll(dd) < 0)
  2592. set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
  2593. &dd->dd_flag);
  2594. clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
  2595. }
  2596. }
  2597. st_out:
  2598. return 0;
  2599. }
  2600. /*
  2601. * DMA region teardown
  2602. *
  2603. * @dd Pointer to driver_data structure
  2604. *
  2605. * return value
  2606. * None
  2607. */
  2608. static void mtip_dma_free(struct driver_data *dd)
  2609. {
  2610. struct mtip_port *port = dd->port;
  2611. if (port->block1)
  2612. dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
  2613. port->block1, port->block1_dma);
  2614. if (port->command_list) {
  2615. dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
  2616. port->command_list, port->command_list_dma);
  2617. }
  2618. }
  2619. /*
  2620. * DMA region setup
  2621. *
  2622. * @dd Pointer to driver_data structure
  2623. *
  2624. * return value
  2625. * -ENOMEM Not enough free DMA region space to initialize driver
  2626. */
  2627. static int mtip_dma_alloc(struct driver_data *dd)
  2628. {
  2629. struct mtip_port *port = dd->port;
  2630. /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
  2631. port->block1 =
  2632. dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
  2633. &port->block1_dma, GFP_KERNEL);
  2634. if (!port->block1)
  2635. return -ENOMEM;
  2636. memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
  2637. /* Allocate dma memory for command list */
  2638. port->command_list =
  2639. dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
  2640. &port->command_list_dma, GFP_KERNEL);
  2641. if (!port->command_list) {
  2642. dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
  2643. port->block1, port->block1_dma);
  2644. port->block1 = NULL;
  2645. port->block1_dma = 0;
  2646. return -ENOMEM;
  2647. }
  2648. memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
  2649. /* Setup all pointers into first DMA region */
  2650. port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
  2651. port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
  2652. port->identify = port->block1 + AHCI_IDFY_OFFSET;
  2653. port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
  2654. port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
  2655. port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
  2656. port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
  2657. port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
  2658. return 0;
  2659. }
  2660. static int mtip_hw_get_identify(struct driver_data *dd)
  2661. {
  2662. struct smart_attr attr242;
  2663. unsigned char *buf;
  2664. int rv;
  2665. if (mtip_get_identify(dd->port, NULL) < 0)
  2666. return -EFAULT;
  2667. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2668. MTIP_FTL_REBUILD_MAGIC) {
  2669. set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
  2670. return MTIP_FTL_REBUILD_MAGIC;
  2671. }
  2672. mtip_dump_identify(dd->port);
  2673. /* check write protect, over temp and rebuild statuses */
  2674. rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
  2675. dd->port->log_buf,
  2676. dd->port->log_buf_dma, 1);
  2677. if (rv) {
  2678. dev_warn(&dd->pdev->dev,
  2679. "Error in READ LOG EXT (10h) command\n");
  2680. /* non-critical error, don't fail the load */
  2681. } else {
  2682. buf = (unsigned char *)dd->port->log_buf;
  2683. if (buf[259] & 0x1) {
  2684. dev_info(&dd->pdev->dev,
  2685. "Write protect bit is set.\n");
  2686. set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
  2687. }
  2688. if (buf[288] == 0xF7) {
  2689. dev_info(&dd->pdev->dev,
  2690. "Exceeded Tmax, drive in thermal shutdown.\n");
  2691. set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
  2692. }
  2693. if (buf[288] == 0xBF) {
  2694. dev_info(&dd->pdev->dev,
  2695. "Drive indicates rebuild has failed.\n");
  2696. /* TODO */
  2697. }
  2698. }
  2699. /* get write protect progess */
  2700. memset(&attr242, 0, sizeof(struct smart_attr));
  2701. if (mtip_get_smart_attr(dd->port, 242, &attr242))
  2702. dev_warn(&dd->pdev->dev,
  2703. "Unable to check write protect progress\n");
  2704. else
  2705. dev_info(&dd->pdev->dev,
  2706. "Write protect progress: %u%% (%u blocks)\n",
  2707. attr242.cur, le32_to_cpu(attr242.data));
  2708. return rv;
  2709. }
  2710. /*
  2711. * Called once for each card.
  2712. *
  2713. * @dd Pointer to the driver data structure.
  2714. *
  2715. * return value
  2716. * 0 on success, else an error code.
  2717. */
  2718. static int mtip_hw_init(struct driver_data *dd)
  2719. {
  2720. int i;
  2721. int rv;
  2722. unsigned int num_command_slots;
  2723. unsigned long timeout, timetaken;
  2724. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2725. mtip_detect_product(dd);
  2726. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2727. rv = -EIO;
  2728. goto out1;
  2729. }
  2730. num_command_slots = dd->slot_groups * 32;
  2731. hba_setup(dd);
  2732. dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
  2733. dd->numa_node);
  2734. if (!dd->port) {
  2735. dev_err(&dd->pdev->dev,
  2736. "Memory allocation: port structure\n");
  2737. return -ENOMEM;
  2738. }
  2739. /* Continue workqueue setup */
  2740. for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
  2741. dd->work[i].port = dd->port;
  2742. /* Enable unaligned IO constraints for some devices */
  2743. if (mtip_device_unaligned_constrained(dd))
  2744. dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
  2745. else
  2746. dd->unal_qdepth = 0;
  2747. sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
  2748. /* Spinlock to prevent concurrent issue */
  2749. for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
  2750. spin_lock_init(&dd->port->cmd_issue_lock[i]);
  2751. /* Set the port mmio base address. */
  2752. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2753. dd->port->dd = dd;
  2754. /* DMA allocations */
  2755. rv = mtip_dma_alloc(dd);
  2756. if (rv < 0)
  2757. goto out1;
  2758. /* Setup the pointers to the extended s_active and CI registers. */
  2759. for (i = 0; i < dd->slot_groups; i++) {
  2760. dd->port->s_active[i] =
  2761. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2762. dd->port->cmd_issue[i] =
  2763. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2764. dd->port->completed[i] =
  2765. dd->port->mmio + i*0x80 + PORT_SDBV;
  2766. }
  2767. timetaken = jiffies;
  2768. timeout = jiffies + msecs_to_jiffies(30000);
  2769. while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
  2770. time_before(jiffies, timeout)) {
  2771. mdelay(100);
  2772. }
  2773. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  2774. timetaken = jiffies - timetaken;
  2775. dev_warn(&dd->pdev->dev,
  2776. "Surprise removal detected at %u ms\n",
  2777. jiffies_to_msecs(timetaken));
  2778. rv = -ENODEV;
  2779. goto out2 ;
  2780. }
  2781. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
  2782. timetaken = jiffies - timetaken;
  2783. dev_warn(&dd->pdev->dev,
  2784. "Removal detected at %u ms\n",
  2785. jiffies_to_msecs(timetaken));
  2786. rv = -EFAULT;
  2787. goto out2;
  2788. }
  2789. /* Conditionally reset the HBA. */
  2790. if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
  2791. if (mtip_hba_reset(dd) < 0) {
  2792. dev_err(&dd->pdev->dev,
  2793. "Card did not reset within timeout\n");
  2794. rv = -EIO;
  2795. goto out2;
  2796. }
  2797. } else {
  2798. /* Clear any pending interrupts on the HBA */
  2799. writel(readl(dd->mmio + HOST_IRQ_STAT),
  2800. dd->mmio + HOST_IRQ_STAT);
  2801. }
  2802. mtip_init_port(dd->port);
  2803. mtip_start_port(dd->port);
  2804. /* Setup the ISR and enable interrupts. */
  2805. rv = devm_request_irq(&dd->pdev->dev,
  2806. dd->pdev->irq,
  2807. mtip_irq_handler,
  2808. IRQF_SHARED,
  2809. dev_driver_string(&dd->pdev->dev),
  2810. dd);
  2811. if (rv) {
  2812. dev_err(&dd->pdev->dev,
  2813. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2814. goto out2;
  2815. }
  2816. irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
  2817. /* Enable interrupts on the HBA. */
  2818. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2819. dd->mmio + HOST_CTL);
  2820. init_waitqueue_head(&dd->port->svc_wait);
  2821. if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
  2822. rv = -EFAULT;
  2823. goto out3;
  2824. }
  2825. return rv;
  2826. out3:
  2827. /* Disable interrupts on the HBA. */
  2828. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2829. dd->mmio + HOST_CTL);
  2830. /* Release the IRQ. */
  2831. irq_set_affinity_hint(dd->pdev->irq, NULL);
  2832. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2833. out2:
  2834. mtip_deinit_port(dd->port);
  2835. mtip_dma_free(dd);
  2836. out1:
  2837. /* Free the memory allocated for the for structure. */
  2838. kfree(dd->port);
  2839. return rv;
  2840. }
  2841. static void mtip_standby_drive(struct driver_data *dd)
  2842. {
  2843. if (dd->sr)
  2844. return;
  2845. /*
  2846. * Send standby immediate (E0h) to the drive so that it
  2847. * saves its state.
  2848. */
  2849. if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
  2850. !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
  2851. if (mtip_standby_immediate(dd->port))
  2852. dev_warn(&dd->pdev->dev,
  2853. "STANDBY IMMEDIATE failed\n");
  2854. }
  2855. /*
  2856. * Called to deinitialize an interface.
  2857. *
  2858. * @dd Pointer to the driver data structure.
  2859. *
  2860. * return value
  2861. * 0
  2862. */
  2863. static int mtip_hw_exit(struct driver_data *dd)
  2864. {
  2865. /*
  2866. * Send standby immediate (E0h) to the drive so that it
  2867. * saves its state.
  2868. */
  2869. if (!dd->sr) {
  2870. /* de-initialize the port. */
  2871. mtip_deinit_port(dd->port);
  2872. /* Disable interrupts on the HBA. */
  2873. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2874. dd->mmio + HOST_CTL);
  2875. }
  2876. /* Release the IRQ. */
  2877. irq_set_affinity_hint(dd->pdev->irq, NULL);
  2878. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2879. msleep(1000);
  2880. /* Free dma regions */
  2881. mtip_dma_free(dd);
  2882. /* Free the memory allocated for the for structure. */
  2883. kfree(dd->port);
  2884. dd->port = NULL;
  2885. return 0;
  2886. }
  2887. /*
  2888. * Issue a Standby Immediate command to the device.
  2889. *
  2890. * This function is called by the Block Layer just before the
  2891. * system powers off during a shutdown.
  2892. *
  2893. * @dd Pointer to the driver data structure.
  2894. *
  2895. * return value
  2896. * 0
  2897. */
  2898. static int mtip_hw_shutdown(struct driver_data *dd)
  2899. {
  2900. /*
  2901. * Send standby immediate (E0h) to the drive so that it
  2902. * saves its state.
  2903. */
  2904. if (!dd->sr && dd->port)
  2905. mtip_standby_immediate(dd->port);
  2906. return 0;
  2907. }
  2908. /*
  2909. * Suspend function
  2910. *
  2911. * This function is called by the Block Layer just before the
  2912. * system hibernates.
  2913. *
  2914. * @dd Pointer to the driver data structure.
  2915. *
  2916. * return value
  2917. * 0 Suspend was successful
  2918. * -EFAULT Suspend was not successful
  2919. */
  2920. static int mtip_hw_suspend(struct driver_data *dd)
  2921. {
  2922. /*
  2923. * Send standby immediate (E0h) to the drive
  2924. * so that it saves its state.
  2925. */
  2926. if (mtip_standby_immediate(dd->port) != 0) {
  2927. dev_err(&dd->pdev->dev,
  2928. "Failed standby-immediate command\n");
  2929. return -EFAULT;
  2930. }
  2931. /* Disable interrupts on the HBA.*/
  2932. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2933. dd->mmio + HOST_CTL);
  2934. mtip_deinit_port(dd->port);
  2935. return 0;
  2936. }
  2937. /*
  2938. * Resume function
  2939. *
  2940. * This function is called by the Block Layer as the
  2941. * system resumes.
  2942. *
  2943. * @dd Pointer to the driver data structure.
  2944. *
  2945. * return value
  2946. * 0 Resume was successful
  2947. * -EFAULT Resume was not successful
  2948. */
  2949. static int mtip_hw_resume(struct driver_data *dd)
  2950. {
  2951. /* Perform any needed hardware setup steps */
  2952. hba_setup(dd);
  2953. /* Reset the HBA */
  2954. if (mtip_hba_reset(dd) != 0) {
  2955. dev_err(&dd->pdev->dev,
  2956. "Unable to reset the HBA\n");
  2957. return -EFAULT;
  2958. }
  2959. /*
  2960. * Enable the port, DMA engine, and FIS reception specific
  2961. * h/w in controller.
  2962. */
  2963. mtip_init_port(dd->port);
  2964. mtip_start_port(dd->port);
  2965. /* Enable interrupts on the HBA.*/
  2966. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2967. dd->mmio + HOST_CTL);
  2968. return 0;
  2969. }
  2970. /*
  2971. * Helper function for reusing disk name
  2972. * upon hot insertion.
  2973. */
  2974. static int rssd_disk_name_format(char *prefix,
  2975. int index,
  2976. char *buf,
  2977. int buflen)
  2978. {
  2979. const int base = 'z' - 'a' + 1;
  2980. char *begin = buf + strlen(prefix);
  2981. char *end = buf + buflen;
  2982. char *p;
  2983. int unit;
  2984. p = end - 1;
  2985. *p = '\0';
  2986. unit = base;
  2987. do {
  2988. if (p == begin)
  2989. return -EINVAL;
  2990. *--p = 'a' + (index % unit);
  2991. index = (index / unit) - 1;
  2992. } while (index >= 0);
  2993. memmove(begin, p, end - p);
  2994. memcpy(buf, prefix, strlen(prefix));
  2995. return 0;
  2996. }
  2997. /*
  2998. * Block layer IOCTL handler.
  2999. *
  3000. * @dev Pointer to the block_device structure.
  3001. * @mode ignored
  3002. * @cmd IOCTL command passed from the user application.
  3003. * @arg Argument passed from the user application.
  3004. *
  3005. * return value
  3006. * 0 IOCTL completed successfully.
  3007. * -ENOTTY IOCTL not supported or invalid driver data
  3008. * structure pointer.
  3009. */
  3010. static int mtip_block_ioctl(struct block_device *dev,
  3011. fmode_t mode,
  3012. unsigned cmd,
  3013. unsigned long arg)
  3014. {
  3015. struct driver_data *dd = dev->bd_disk->private_data;
  3016. if (!capable(CAP_SYS_ADMIN))
  3017. return -EACCES;
  3018. if (!dd)
  3019. return -ENOTTY;
  3020. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3021. return -ENOTTY;
  3022. switch (cmd) {
  3023. case BLKFLSBUF:
  3024. return -ENOTTY;
  3025. default:
  3026. return mtip_hw_ioctl(dd, cmd, arg);
  3027. }
  3028. }
  3029. #ifdef CONFIG_COMPAT
  3030. /*
  3031. * Block layer compat IOCTL handler.
  3032. *
  3033. * @dev Pointer to the block_device structure.
  3034. * @mode ignored
  3035. * @cmd IOCTL command passed from the user application.
  3036. * @arg Argument passed from the user application.
  3037. *
  3038. * return value
  3039. * 0 IOCTL completed successfully.
  3040. * -ENOTTY IOCTL not supported or invalid driver data
  3041. * structure pointer.
  3042. */
  3043. static int mtip_block_compat_ioctl(struct block_device *dev,
  3044. fmode_t mode,
  3045. unsigned cmd,
  3046. unsigned long arg)
  3047. {
  3048. struct driver_data *dd = dev->bd_disk->private_data;
  3049. if (!capable(CAP_SYS_ADMIN))
  3050. return -EACCES;
  3051. if (!dd)
  3052. return -ENOTTY;
  3053. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
  3054. return -ENOTTY;
  3055. switch (cmd) {
  3056. case BLKFLSBUF:
  3057. return -ENOTTY;
  3058. case HDIO_DRIVE_TASKFILE: {
  3059. struct mtip_compat_ide_task_request_s __user *compat_req_task;
  3060. ide_task_request_t req_task;
  3061. int compat_tasksize, outtotal, ret;
  3062. compat_tasksize =
  3063. sizeof(struct mtip_compat_ide_task_request_s);
  3064. compat_req_task =
  3065. (struct mtip_compat_ide_task_request_s __user *) arg;
  3066. if (copy_from_user(&req_task, (void __user *) arg,
  3067. compat_tasksize - (2 * sizeof(compat_long_t))))
  3068. return -EFAULT;
  3069. if (get_user(req_task.out_size, &compat_req_task->out_size))
  3070. return -EFAULT;
  3071. if (get_user(req_task.in_size, &compat_req_task->in_size))
  3072. return -EFAULT;
  3073. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  3074. ret = exec_drive_taskfile(dd, (void __user *) arg,
  3075. &req_task, outtotal);
  3076. if (copy_to_user((void __user *) arg, &req_task,
  3077. compat_tasksize -
  3078. (2 * sizeof(compat_long_t))))
  3079. return -EFAULT;
  3080. if (put_user(req_task.out_size, &compat_req_task->out_size))
  3081. return -EFAULT;
  3082. if (put_user(req_task.in_size, &compat_req_task->in_size))
  3083. return -EFAULT;
  3084. return ret;
  3085. }
  3086. default:
  3087. return mtip_hw_ioctl(dd, cmd, arg);
  3088. }
  3089. }
  3090. #endif
  3091. /*
  3092. * Obtain the geometry of the device.
  3093. *
  3094. * You may think that this function is obsolete, but some applications,
  3095. * fdisk for example still used CHS values. This function describes the
  3096. * device as having 224 heads and 56 sectors per cylinder. These values are
  3097. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  3098. * partition is described in terms of a start and end cylinder this means
  3099. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  3100. * affects performance.
  3101. *
  3102. * @dev Pointer to the block_device strucutre.
  3103. * @geo Pointer to a hd_geometry structure.
  3104. *
  3105. * return value
  3106. * 0 Operation completed successfully.
  3107. * -ENOTTY An error occurred while reading the drive capacity.
  3108. */
  3109. static int mtip_block_getgeo(struct block_device *dev,
  3110. struct hd_geometry *geo)
  3111. {
  3112. struct driver_data *dd = dev->bd_disk->private_data;
  3113. sector_t capacity;
  3114. if (!dd)
  3115. return -ENOTTY;
  3116. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3117. dev_warn(&dd->pdev->dev,
  3118. "Could not get drive capacity.\n");
  3119. return -ENOTTY;
  3120. }
  3121. geo->heads = 224;
  3122. geo->sectors = 56;
  3123. sector_div(capacity, (geo->heads * geo->sectors));
  3124. geo->cylinders = capacity;
  3125. return 0;
  3126. }
  3127. /*
  3128. * Block device operation function.
  3129. *
  3130. * This structure contains pointers to the functions required by the block
  3131. * layer.
  3132. */
  3133. static const struct block_device_operations mtip_block_ops = {
  3134. .ioctl = mtip_block_ioctl,
  3135. #ifdef CONFIG_COMPAT
  3136. .compat_ioctl = mtip_block_compat_ioctl,
  3137. #endif
  3138. .getgeo = mtip_block_getgeo,
  3139. .owner = THIS_MODULE
  3140. };
  3141. static inline bool is_se_active(struct driver_data *dd)
  3142. {
  3143. if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
  3144. if (dd->port->ic_pause_timer) {
  3145. unsigned long to = dd->port->ic_pause_timer +
  3146. msecs_to_jiffies(1000);
  3147. if (time_after(jiffies, to)) {
  3148. clear_bit(MTIP_PF_SE_ACTIVE_BIT,
  3149. &dd->port->flags);
  3150. clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
  3151. dd->port->ic_pause_timer = 0;
  3152. wake_up_interruptible(&dd->port->svc_wait);
  3153. return false;
  3154. }
  3155. }
  3156. return true;
  3157. }
  3158. return false;
  3159. }
  3160. /*
  3161. * Block layer make request function.
  3162. *
  3163. * This function is called by the kernel to process a BIO for
  3164. * the P320 device.
  3165. *
  3166. * @queue Pointer to the request queue. Unused other than to obtain
  3167. * the driver data structure.
  3168. * @rq Pointer to the request.
  3169. *
  3170. */
  3171. static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
  3172. {
  3173. struct driver_data *dd = hctx->queue->queuedata;
  3174. struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
  3175. unsigned int nents;
  3176. if (is_se_active(dd))
  3177. return -ENODATA;
  3178. if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
  3179. if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
  3180. &dd->dd_flag))) {
  3181. return -ENXIO;
  3182. }
  3183. if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
  3184. return -ENODATA;
  3185. }
  3186. if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
  3187. &dd->dd_flag) &&
  3188. rq_data_dir(rq))) {
  3189. return -ENODATA;
  3190. }
  3191. if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
  3192. return -ENODATA;
  3193. if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
  3194. return -ENXIO;
  3195. }
  3196. if (rq->cmd_flags & REQ_DISCARD) {
  3197. int err;
  3198. err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
  3199. blk_mq_end_request(rq, err);
  3200. return 0;
  3201. }
  3202. /* Create the scatter list for this request. */
  3203. nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
  3204. /* Issue the read/write. */
  3205. mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
  3206. return 0;
  3207. }
  3208. static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
  3209. struct request *rq)
  3210. {
  3211. struct driver_data *dd = hctx->queue->queuedata;
  3212. struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
  3213. if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
  3214. return false;
  3215. /*
  3216. * If unaligned depth must be limited on this controller, mark it
  3217. * as unaligned if the IO isn't on a 4k boundary (start of length).
  3218. */
  3219. if (blk_rq_sectors(rq) <= 64) {
  3220. if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
  3221. cmd->unaligned = 1;
  3222. }
  3223. if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
  3224. return true;
  3225. return false;
  3226. }
  3227. static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
  3228. const struct blk_mq_queue_data *bd)
  3229. {
  3230. struct request *rq = bd->rq;
  3231. int ret;
  3232. if (unlikely(mtip_check_unal_depth(hctx, rq)))
  3233. return BLK_MQ_RQ_QUEUE_BUSY;
  3234. blk_mq_start_request(rq);
  3235. ret = mtip_submit_request(hctx, rq);
  3236. if (likely(!ret))
  3237. return BLK_MQ_RQ_QUEUE_OK;
  3238. rq->errors = ret;
  3239. return BLK_MQ_RQ_QUEUE_ERROR;
  3240. }
  3241. static void mtip_free_cmd(void *data, struct request *rq,
  3242. unsigned int hctx_idx, unsigned int request_idx)
  3243. {
  3244. struct driver_data *dd = data;
  3245. struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
  3246. if (!cmd->command)
  3247. return;
  3248. dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
  3249. cmd->command, cmd->command_dma);
  3250. }
  3251. static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
  3252. unsigned int request_idx, unsigned int numa_node)
  3253. {
  3254. struct driver_data *dd = data;
  3255. struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
  3256. u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
  3257. cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
  3258. &cmd->command_dma, GFP_KERNEL);
  3259. if (!cmd->command)
  3260. return -ENOMEM;
  3261. memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
  3262. /* Point the command headers at the command tables. */
  3263. cmd->command_header = dd->port->command_list +
  3264. (sizeof(struct mtip_cmd_hdr) * request_idx);
  3265. cmd->command_header_dma = dd->port->command_list_dma +
  3266. (sizeof(struct mtip_cmd_hdr) * request_idx);
  3267. if (host_cap_64)
  3268. cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
  3269. cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
  3270. sg_init_table(cmd->sg, MTIP_MAX_SG);
  3271. return 0;
  3272. }
  3273. static struct blk_mq_ops mtip_mq_ops = {
  3274. .queue_rq = mtip_queue_rq,
  3275. .map_queue = blk_mq_map_queue,
  3276. .init_request = mtip_init_cmd,
  3277. .exit_request = mtip_free_cmd,
  3278. };
  3279. /*
  3280. * Block layer initialization function.
  3281. *
  3282. * This function is called once by the PCI layer for each P320
  3283. * device that is connected to the system.
  3284. *
  3285. * @dd Pointer to the driver data structure.
  3286. *
  3287. * return value
  3288. * 0 on success else an error code.
  3289. */
  3290. static int mtip_block_initialize(struct driver_data *dd)
  3291. {
  3292. int rv = 0, wait_for_rebuild = 0;
  3293. sector_t capacity;
  3294. unsigned int index = 0;
  3295. struct kobject *kobj;
  3296. unsigned char thd_name[16];
  3297. if (dd->disk)
  3298. goto skip_create_disk; /* hw init done, before rebuild */
  3299. if (mtip_hw_init(dd)) {
  3300. rv = -EINVAL;
  3301. goto protocol_init_error;
  3302. }
  3303. dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
  3304. if (dd->disk == NULL) {
  3305. dev_err(&dd->pdev->dev,
  3306. "Unable to allocate gendisk structure\n");
  3307. rv = -EINVAL;
  3308. goto alloc_disk_error;
  3309. }
  3310. /* Generate the disk name, implemented same as in sd.c */
  3311. do {
  3312. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  3313. goto ida_get_error;
  3314. spin_lock(&rssd_index_lock);
  3315. rv = ida_get_new(&rssd_index_ida, &index);
  3316. spin_unlock(&rssd_index_lock);
  3317. } while (rv == -EAGAIN);
  3318. if (rv)
  3319. goto ida_get_error;
  3320. rv = rssd_disk_name_format("rssd",
  3321. index,
  3322. dd->disk->disk_name,
  3323. DISK_NAME_LEN);
  3324. if (rv)
  3325. goto disk_index_error;
  3326. dd->disk->driverfs_dev = &dd->pdev->dev;
  3327. dd->disk->major = dd->major;
  3328. dd->disk->first_minor = index * MTIP_MAX_MINORS;
  3329. dd->disk->minors = MTIP_MAX_MINORS;
  3330. dd->disk->fops = &mtip_block_ops;
  3331. dd->disk->private_data = dd;
  3332. dd->index = index;
  3333. mtip_hw_debugfs_init(dd);
  3334. skip_create_disk:
  3335. memset(&dd->tags, 0, sizeof(dd->tags));
  3336. dd->tags.ops = &mtip_mq_ops;
  3337. dd->tags.nr_hw_queues = 1;
  3338. dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
  3339. dd->tags.reserved_tags = 1;
  3340. dd->tags.cmd_size = sizeof(struct mtip_cmd);
  3341. dd->tags.numa_node = dd->numa_node;
  3342. dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
  3343. dd->tags.driver_data = dd;
  3344. rv = blk_mq_alloc_tag_set(&dd->tags);
  3345. if (rv) {
  3346. dev_err(&dd->pdev->dev,
  3347. "Unable to allocate request queue\n");
  3348. goto block_queue_alloc_init_error;
  3349. }
  3350. /* Allocate the request queue. */
  3351. dd->queue = blk_mq_init_queue(&dd->tags);
  3352. if (IS_ERR(dd->queue)) {
  3353. dev_err(&dd->pdev->dev,
  3354. "Unable to allocate request queue\n");
  3355. rv = -ENOMEM;
  3356. goto block_queue_alloc_init_error;
  3357. }
  3358. dd->disk->queue = dd->queue;
  3359. dd->queue->queuedata = dd;
  3360. /* Initialize the protocol layer. */
  3361. wait_for_rebuild = mtip_hw_get_identify(dd);
  3362. if (wait_for_rebuild < 0) {
  3363. dev_err(&dd->pdev->dev,
  3364. "Protocol layer initialization failed\n");
  3365. rv = -EINVAL;
  3366. goto init_hw_cmds_error;
  3367. }
  3368. /*
  3369. * if rebuild pending, start the service thread, and delay the block
  3370. * queue creation and add_disk()
  3371. */
  3372. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3373. goto start_service_thread;
  3374. /* Set device limits. */
  3375. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  3376. clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
  3377. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  3378. blk_queue_physical_block_size(dd->queue, 4096);
  3379. blk_queue_max_hw_sectors(dd->queue, 0xffff);
  3380. blk_queue_max_segment_size(dd->queue, 0x400000);
  3381. blk_queue_io_min(dd->queue, 4096);
  3382. blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
  3383. /*
  3384. * write back cache is not supported in the device. FUA depends on
  3385. * write back cache support, hence setting flush support to zero.
  3386. */
  3387. blk_queue_flush(dd->queue, 0);
  3388. /* Signal trim support */
  3389. if (dd->trim_supp == true) {
  3390. set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
  3391. dd->queue->limits.discard_granularity = 4096;
  3392. blk_queue_max_discard_sectors(dd->queue,
  3393. MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
  3394. dd->queue->limits.discard_zeroes_data = 0;
  3395. }
  3396. /* Set the capacity of the device in 512 byte sectors. */
  3397. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  3398. dev_warn(&dd->pdev->dev,
  3399. "Could not read drive capacity\n");
  3400. rv = -EIO;
  3401. goto read_capacity_error;
  3402. }
  3403. set_capacity(dd->disk, capacity);
  3404. /* Enable the block device and add it to /dev */
  3405. add_disk(dd->disk);
  3406. dd->bdev = bdget_disk(dd->disk, 0);
  3407. /*
  3408. * Now that the disk is active, initialize any sysfs attributes
  3409. * managed by the protocol layer.
  3410. */
  3411. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3412. if (kobj) {
  3413. mtip_hw_sysfs_init(dd, kobj);
  3414. kobject_put(kobj);
  3415. }
  3416. if (dd->mtip_svc_handler) {
  3417. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3418. return rv; /* service thread created for handling rebuild */
  3419. }
  3420. start_service_thread:
  3421. sprintf(thd_name, "mtip_svc_thd_%02d", index);
  3422. dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
  3423. dd, dd->numa_node, "%s",
  3424. thd_name);
  3425. if (IS_ERR(dd->mtip_svc_handler)) {
  3426. dev_err(&dd->pdev->dev, "service thread failed to start\n");
  3427. dd->mtip_svc_handler = NULL;
  3428. rv = -EFAULT;
  3429. goto kthread_run_error;
  3430. }
  3431. wake_up_process(dd->mtip_svc_handler);
  3432. if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
  3433. rv = wait_for_rebuild;
  3434. return rv;
  3435. kthread_run_error:
  3436. bdput(dd->bdev);
  3437. dd->bdev = NULL;
  3438. /* Delete our gendisk. This also removes the device from /dev */
  3439. del_gendisk(dd->disk);
  3440. read_capacity_error:
  3441. init_hw_cmds_error:
  3442. blk_cleanup_queue(dd->queue);
  3443. blk_mq_free_tag_set(&dd->tags);
  3444. block_queue_alloc_init_error:
  3445. mtip_hw_debugfs_exit(dd);
  3446. disk_index_error:
  3447. spin_lock(&rssd_index_lock);
  3448. ida_remove(&rssd_index_ida, index);
  3449. spin_unlock(&rssd_index_lock);
  3450. ida_get_error:
  3451. put_disk(dd->disk);
  3452. alloc_disk_error:
  3453. mtip_hw_exit(dd); /* De-initialize the protocol layer. */
  3454. protocol_init_error:
  3455. return rv;
  3456. }
  3457. /*
  3458. * Block layer deinitialization function.
  3459. *
  3460. * Called by the PCI layer as each P320 device is removed.
  3461. *
  3462. * @dd Pointer to the driver data structure.
  3463. *
  3464. * return value
  3465. * 0
  3466. */
  3467. static int mtip_block_remove(struct driver_data *dd)
  3468. {
  3469. struct kobject *kobj;
  3470. mtip_hw_debugfs_exit(dd);
  3471. if (dd->mtip_svc_handler) {
  3472. set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
  3473. wake_up_interruptible(&dd->port->svc_wait);
  3474. kthread_stop(dd->mtip_svc_handler);
  3475. }
  3476. /* Clean up the sysfs attributes, if created */
  3477. if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
  3478. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  3479. if (kobj) {
  3480. mtip_hw_sysfs_exit(dd, kobj);
  3481. kobject_put(kobj);
  3482. }
  3483. }
  3484. if (!dd->sr)
  3485. mtip_standby_drive(dd);
  3486. else
  3487. dev_info(&dd->pdev->dev, "device %s surprise removal\n",
  3488. dd->disk->disk_name);
  3489. /*
  3490. * Delete our gendisk structure. This also removes the device
  3491. * from /dev
  3492. */
  3493. if (dd->bdev) {
  3494. bdput(dd->bdev);
  3495. dd->bdev = NULL;
  3496. }
  3497. if (dd->disk) {
  3498. del_gendisk(dd->disk);
  3499. if (dd->disk->queue) {
  3500. blk_cleanup_queue(dd->queue);
  3501. blk_mq_free_tag_set(&dd->tags);
  3502. dd->queue = NULL;
  3503. }
  3504. put_disk(dd->disk);
  3505. }
  3506. dd->disk = NULL;
  3507. spin_lock(&rssd_index_lock);
  3508. ida_remove(&rssd_index_ida, dd->index);
  3509. spin_unlock(&rssd_index_lock);
  3510. /* De-initialize the protocol layer. */
  3511. mtip_hw_exit(dd);
  3512. return 0;
  3513. }
  3514. /*
  3515. * Function called by the PCI layer when just before the
  3516. * machine shuts down.
  3517. *
  3518. * If a protocol layer shutdown function is present it will be called
  3519. * by this function.
  3520. *
  3521. * @dd Pointer to the driver data structure.
  3522. *
  3523. * return value
  3524. * 0
  3525. */
  3526. static int mtip_block_shutdown(struct driver_data *dd)
  3527. {
  3528. mtip_hw_shutdown(dd);
  3529. /* Delete our gendisk structure, and cleanup the blk queue. */
  3530. if (dd->disk) {
  3531. dev_info(&dd->pdev->dev,
  3532. "Shutting down %s ...\n", dd->disk->disk_name);
  3533. del_gendisk(dd->disk);
  3534. if (dd->disk->queue) {
  3535. blk_cleanup_queue(dd->queue);
  3536. blk_mq_free_tag_set(&dd->tags);
  3537. }
  3538. put_disk(dd->disk);
  3539. dd->disk = NULL;
  3540. dd->queue = NULL;
  3541. }
  3542. spin_lock(&rssd_index_lock);
  3543. ida_remove(&rssd_index_ida, dd->index);
  3544. spin_unlock(&rssd_index_lock);
  3545. return 0;
  3546. }
  3547. static int mtip_block_suspend(struct driver_data *dd)
  3548. {
  3549. dev_info(&dd->pdev->dev,
  3550. "Suspending %s ...\n", dd->disk->disk_name);
  3551. mtip_hw_suspend(dd);
  3552. return 0;
  3553. }
  3554. static int mtip_block_resume(struct driver_data *dd)
  3555. {
  3556. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  3557. dd->disk->disk_name);
  3558. mtip_hw_resume(dd);
  3559. return 0;
  3560. }
  3561. static void drop_cpu(int cpu)
  3562. {
  3563. cpu_use[cpu]--;
  3564. }
  3565. static int get_least_used_cpu_on_node(int node)
  3566. {
  3567. int cpu, least_used_cpu, least_cnt;
  3568. const struct cpumask *node_mask;
  3569. node_mask = cpumask_of_node(node);
  3570. least_used_cpu = cpumask_first(node_mask);
  3571. least_cnt = cpu_use[least_used_cpu];
  3572. cpu = least_used_cpu;
  3573. for_each_cpu(cpu, node_mask) {
  3574. if (cpu_use[cpu] < least_cnt) {
  3575. least_used_cpu = cpu;
  3576. least_cnt = cpu_use[cpu];
  3577. }
  3578. }
  3579. cpu_use[least_used_cpu]++;
  3580. return least_used_cpu;
  3581. }
  3582. /* Helper for selecting a node in round robin mode */
  3583. static inline int mtip_get_next_rr_node(void)
  3584. {
  3585. static int next_node = -1;
  3586. if (next_node == -1) {
  3587. next_node = first_online_node;
  3588. return next_node;
  3589. }
  3590. next_node = next_online_node(next_node);
  3591. if (next_node == MAX_NUMNODES)
  3592. next_node = first_online_node;
  3593. return next_node;
  3594. }
  3595. static DEFINE_HANDLER(0);
  3596. static DEFINE_HANDLER(1);
  3597. static DEFINE_HANDLER(2);
  3598. static DEFINE_HANDLER(3);
  3599. static DEFINE_HANDLER(4);
  3600. static DEFINE_HANDLER(5);
  3601. static DEFINE_HANDLER(6);
  3602. static DEFINE_HANDLER(7);
  3603. static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
  3604. {
  3605. int pos;
  3606. unsigned short pcie_dev_ctrl;
  3607. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  3608. if (pos) {
  3609. pci_read_config_word(pdev,
  3610. pos + PCI_EXP_DEVCTL,
  3611. &pcie_dev_ctrl);
  3612. if (pcie_dev_ctrl & (1 << 11) ||
  3613. pcie_dev_ctrl & (1 << 4)) {
  3614. dev_info(&dd->pdev->dev,
  3615. "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
  3616. pdev->vendor, pdev->device);
  3617. pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
  3618. PCI_EXP_DEVCTL_RELAX_EN);
  3619. pci_write_config_word(pdev,
  3620. pos + PCI_EXP_DEVCTL,
  3621. pcie_dev_ctrl);
  3622. }
  3623. }
  3624. }
  3625. static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
  3626. {
  3627. /*
  3628. * This workaround is specific to AMD/ATI chipset with a PCI upstream
  3629. * device with device id 0x5aXX
  3630. */
  3631. if (pdev->bus && pdev->bus->self) {
  3632. if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
  3633. ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
  3634. mtip_disable_link_opts(dd, pdev->bus->self);
  3635. } else {
  3636. /* Check further up the topology */
  3637. struct pci_dev *parent_dev = pdev->bus->self;
  3638. if (parent_dev->bus &&
  3639. parent_dev->bus->parent &&
  3640. parent_dev->bus->parent->self &&
  3641. parent_dev->bus->parent->self->vendor ==
  3642. PCI_VENDOR_ID_ATI &&
  3643. (parent_dev->bus->parent->self->device &
  3644. 0xff00) == 0x5a00) {
  3645. mtip_disable_link_opts(dd,
  3646. parent_dev->bus->parent->self);
  3647. }
  3648. }
  3649. }
  3650. }
  3651. /*
  3652. * Called for each supported PCI device detected.
  3653. *
  3654. * This function allocates the private data structure, enables the
  3655. * PCI device and then calls the block layer initialization function.
  3656. *
  3657. * return value
  3658. * 0 on success else an error code.
  3659. */
  3660. static int mtip_pci_probe(struct pci_dev *pdev,
  3661. const struct pci_device_id *ent)
  3662. {
  3663. int rv = 0;
  3664. struct driver_data *dd = NULL;
  3665. char cpu_list[256];
  3666. const struct cpumask *node_mask;
  3667. int cpu, i = 0, j = 0;
  3668. int my_node = NUMA_NO_NODE;
  3669. unsigned long flags;
  3670. /* Allocate memory for this devices private data. */
  3671. my_node = pcibus_to_node(pdev->bus);
  3672. if (my_node != NUMA_NO_NODE) {
  3673. if (!node_online(my_node))
  3674. my_node = mtip_get_next_rr_node();
  3675. } else {
  3676. dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
  3677. my_node = mtip_get_next_rr_node();
  3678. }
  3679. dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
  3680. my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
  3681. cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
  3682. dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
  3683. if (dd == NULL) {
  3684. dev_err(&pdev->dev,
  3685. "Unable to allocate memory for driver data\n");
  3686. return -ENOMEM;
  3687. }
  3688. /* Attach the private data to this PCI device. */
  3689. pci_set_drvdata(pdev, dd);
  3690. rv = pcim_enable_device(pdev);
  3691. if (rv < 0) {
  3692. dev_err(&pdev->dev, "Unable to enable device\n");
  3693. goto iomap_err;
  3694. }
  3695. /* Map BAR5 to memory. */
  3696. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  3697. if (rv < 0) {
  3698. dev_err(&pdev->dev, "Unable to map regions\n");
  3699. goto iomap_err;
  3700. }
  3701. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3702. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3703. if (rv) {
  3704. rv = pci_set_consistent_dma_mask(pdev,
  3705. DMA_BIT_MASK(32));
  3706. if (rv) {
  3707. dev_warn(&pdev->dev,
  3708. "64-bit DMA enable failed\n");
  3709. goto setmask_err;
  3710. }
  3711. }
  3712. }
  3713. /* Copy the info we may need later into the private data structure. */
  3714. dd->major = mtip_major;
  3715. dd->instance = instance;
  3716. dd->pdev = pdev;
  3717. dd->numa_node = my_node;
  3718. INIT_LIST_HEAD(&dd->online_list);
  3719. INIT_LIST_HEAD(&dd->remove_list);
  3720. memset(dd->workq_name, 0, 32);
  3721. snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
  3722. dd->isr_workq = create_workqueue(dd->workq_name);
  3723. if (!dd->isr_workq) {
  3724. dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
  3725. rv = -ENOMEM;
  3726. goto block_initialize_err;
  3727. }
  3728. memset(cpu_list, 0, sizeof(cpu_list));
  3729. node_mask = cpumask_of_node(dd->numa_node);
  3730. if (!cpumask_empty(node_mask)) {
  3731. for_each_cpu(cpu, node_mask)
  3732. {
  3733. snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
  3734. j = strlen(cpu_list);
  3735. }
  3736. dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
  3737. dd->numa_node,
  3738. topology_physical_package_id(cpumask_first(node_mask)),
  3739. nr_cpus_node(dd->numa_node),
  3740. cpu_list);
  3741. } else
  3742. dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
  3743. dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
  3744. dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
  3745. cpu_to_node(dd->isr_binding), dd->isr_binding);
  3746. /* first worker context always runs in ISR */
  3747. dd->work[0].cpu_binding = dd->isr_binding;
  3748. dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
  3749. dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
  3750. dd->work[3].cpu_binding = dd->work[0].cpu_binding;
  3751. dd->work[4].cpu_binding = dd->work[1].cpu_binding;
  3752. dd->work[5].cpu_binding = dd->work[2].cpu_binding;
  3753. dd->work[6].cpu_binding = dd->work[2].cpu_binding;
  3754. dd->work[7].cpu_binding = dd->work[1].cpu_binding;
  3755. /* Log the bindings */
  3756. for_each_present_cpu(cpu) {
  3757. memset(cpu_list, 0, sizeof(cpu_list));
  3758. for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
  3759. if (dd->work[i].cpu_binding == cpu) {
  3760. snprintf(&cpu_list[j], 256 - j, "%d ", i);
  3761. j = strlen(cpu_list);
  3762. }
  3763. }
  3764. if (j)
  3765. dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
  3766. }
  3767. INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
  3768. INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
  3769. INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
  3770. INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
  3771. INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
  3772. INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
  3773. INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
  3774. INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
  3775. pci_set_master(pdev);
  3776. rv = pci_enable_msi(pdev);
  3777. if (rv) {
  3778. dev_warn(&pdev->dev,
  3779. "Unable to enable MSI interrupt.\n");
  3780. goto msi_initialize_err;
  3781. }
  3782. mtip_fix_ero_nosnoop(dd, pdev);
  3783. /* Initialize the block layer. */
  3784. rv = mtip_block_initialize(dd);
  3785. if (rv < 0) {
  3786. dev_err(&pdev->dev,
  3787. "Unable to initialize block layer\n");
  3788. goto block_initialize_err;
  3789. }
  3790. /*
  3791. * Increment the instance count so that each device has a unique
  3792. * instance number.
  3793. */
  3794. instance++;
  3795. if (rv != MTIP_FTL_REBUILD_MAGIC)
  3796. set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
  3797. else
  3798. rv = 0; /* device in rebuild state, return 0 from probe */
  3799. /* Add to online list even if in ftl rebuild */
  3800. spin_lock_irqsave(&dev_lock, flags);
  3801. list_add(&dd->online_list, &online_list);
  3802. spin_unlock_irqrestore(&dev_lock, flags);
  3803. goto done;
  3804. block_initialize_err:
  3805. pci_disable_msi(pdev);
  3806. msi_initialize_err:
  3807. if (dd->isr_workq) {
  3808. flush_workqueue(dd->isr_workq);
  3809. destroy_workqueue(dd->isr_workq);
  3810. drop_cpu(dd->work[0].cpu_binding);
  3811. drop_cpu(dd->work[1].cpu_binding);
  3812. drop_cpu(dd->work[2].cpu_binding);
  3813. }
  3814. setmask_err:
  3815. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3816. iomap_err:
  3817. kfree(dd);
  3818. pci_set_drvdata(pdev, NULL);
  3819. return rv;
  3820. done:
  3821. return rv;
  3822. }
  3823. /*
  3824. * Called for each probed device when the device is removed or the
  3825. * driver is unloaded.
  3826. *
  3827. * return value
  3828. * None
  3829. */
  3830. static void mtip_pci_remove(struct pci_dev *pdev)
  3831. {
  3832. struct driver_data *dd = pci_get_drvdata(pdev);
  3833. unsigned long flags, to;
  3834. set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
  3835. spin_lock_irqsave(&dev_lock, flags);
  3836. list_del_init(&dd->online_list);
  3837. list_add(&dd->remove_list, &removing_list);
  3838. spin_unlock_irqrestore(&dev_lock, flags);
  3839. mtip_check_surprise_removal(pdev);
  3840. synchronize_irq(dd->pdev->irq);
  3841. /* Spin until workers are done */
  3842. to = jiffies + msecs_to_jiffies(4000);
  3843. do {
  3844. msleep(20);
  3845. } while (atomic_read(&dd->irq_workers_active) != 0 &&
  3846. time_before(jiffies, to));
  3847. if (atomic_read(&dd->irq_workers_active) != 0) {
  3848. dev_warn(&dd->pdev->dev,
  3849. "Completion workers still active!\n");
  3850. }
  3851. blk_mq_stop_hw_queues(dd->queue);
  3852. /* Clean up the block layer. */
  3853. mtip_block_remove(dd);
  3854. if (dd->isr_workq) {
  3855. flush_workqueue(dd->isr_workq);
  3856. destroy_workqueue(dd->isr_workq);
  3857. drop_cpu(dd->work[0].cpu_binding);
  3858. drop_cpu(dd->work[1].cpu_binding);
  3859. drop_cpu(dd->work[2].cpu_binding);
  3860. }
  3861. pci_disable_msi(pdev);
  3862. spin_lock_irqsave(&dev_lock, flags);
  3863. list_del_init(&dd->remove_list);
  3864. spin_unlock_irqrestore(&dev_lock, flags);
  3865. kfree(dd);
  3866. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  3867. pci_set_drvdata(pdev, NULL);
  3868. }
  3869. /*
  3870. * Called for each probed device when the device is suspended.
  3871. *
  3872. * return value
  3873. * 0 Success
  3874. * <0 Error
  3875. */
  3876. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  3877. {
  3878. int rv = 0;
  3879. struct driver_data *dd = pci_get_drvdata(pdev);
  3880. if (!dd) {
  3881. dev_err(&pdev->dev,
  3882. "Driver private datastructure is NULL\n");
  3883. return -EFAULT;
  3884. }
  3885. set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3886. /* Disable ports & interrupts then send standby immediate */
  3887. rv = mtip_block_suspend(dd);
  3888. if (rv < 0) {
  3889. dev_err(&pdev->dev,
  3890. "Failed to suspend controller\n");
  3891. return rv;
  3892. }
  3893. /*
  3894. * Save the pci config space to pdev structure &
  3895. * disable the device
  3896. */
  3897. pci_save_state(pdev);
  3898. pci_disable_device(pdev);
  3899. /* Move to Low power state*/
  3900. pci_set_power_state(pdev, PCI_D3hot);
  3901. return rv;
  3902. }
  3903. /*
  3904. * Called for each probed device when the device is resumed.
  3905. *
  3906. * return value
  3907. * 0 Success
  3908. * <0 Error
  3909. */
  3910. static int mtip_pci_resume(struct pci_dev *pdev)
  3911. {
  3912. int rv = 0;
  3913. struct driver_data *dd;
  3914. dd = pci_get_drvdata(pdev);
  3915. if (!dd) {
  3916. dev_err(&pdev->dev,
  3917. "Driver private datastructure is NULL\n");
  3918. return -EFAULT;
  3919. }
  3920. /* Move the device to active State */
  3921. pci_set_power_state(pdev, PCI_D0);
  3922. /* Restore PCI configuration space */
  3923. pci_restore_state(pdev);
  3924. /* Enable the PCI device*/
  3925. rv = pcim_enable_device(pdev);
  3926. if (rv < 0) {
  3927. dev_err(&pdev->dev,
  3928. "Failed to enable card during resume\n");
  3929. goto err;
  3930. }
  3931. pci_set_master(pdev);
  3932. /*
  3933. * Calls hbaReset, initPort, & startPort function
  3934. * then enables interrupts
  3935. */
  3936. rv = mtip_block_resume(dd);
  3937. if (rv < 0)
  3938. dev_err(&pdev->dev, "Unable to resume\n");
  3939. err:
  3940. clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
  3941. return rv;
  3942. }
  3943. /*
  3944. * Shutdown routine
  3945. *
  3946. * return value
  3947. * None
  3948. */
  3949. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3950. {
  3951. struct driver_data *dd = pci_get_drvdata(pdev);
  3952. if (dd)
  3953. mtip_block_shutdown(dd);
  3954. }
  3955. /* Table of device ids supported by this driver. */
  3956. static const struct pci_device_id mtip_pci_tbl[] = {
  3957. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
  3958. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
  3959. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
  3960. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
  3961. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
  3962. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
  3963. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
  3964. { 0 }
  3965. };
  3966. /* Structure that describes the PCI driver functions. */
  3967. static struct pci_driver mtip_pci_driver = {
  3968. .name = MTIP_DRV_NAME,
  3969. .id_table = mtip_pci_tbl,
  3970. .probe = mtip_pci_probe,
  3971. .remove = mtip_pci_remove,
  3972. .suspend = mtip_pci_suspend,
  3973. .resume = mtip_pci_resume,
  3974. .shutdown = mtip_pci_shutdown,
  3975. };
  3976. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3977. /*
  3978. * Module initialization function.
  3979. *
  3980. * Called once when the module is loaded. This function allocates a major
  3981. * block device number to the Cyclone devices and registers the PCI layer
  3982. * of the driver.
  3983. *
  3984. * Return value
  3985. * 0 on success else error code.
  3986. */
  3987. static int __init mtip_init(void)
  3988. {
  3989. int error;
  3990. pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3991. spin_lock_init(&dev_lock);
  3992. INIT_LIST_HEAD(&online_list);
  3993. INIT_LIST_HEAD(&removing_list);
  3994. /* Allocate a major block device number to use with this driver. */
  3995. error = register_blkdev(0, MTIP_DRV_NAME);
  3996. if (error <= 0) {
  3997. pr_err("Unable to register block device (%d)\n",
  3998. error);
  3999. return -EBUSY;
  4000. }
  4001. mtip_major = error;
  4002. dfs_parent = debugfs_create_dir("rssd", NULL);
  4003. if (IS_ERR_OR_NULL(dfs_parent)) {
  4004. pr_warn("Error creating debugfs parent\n");
  4005. dfs_parent = NULL;
  4006. }
  4007. if (dfs_parent) {
  4008. dfs_device_status = debugfs_create_file("device_status",
  4009. S_IRUGO, dfs_parent, NULL,
  4010. &mtip_device_status_fops);
  4011. if (IS_ERR_OR_NULL(dfs_device_status)) {
  4012. pr_err("Error creating device_status node\n");
  4013. dfs_device_status = NULL;
  4014. }
  4015. }
  4016. /* Register our PCI operations. */
  4017. error = pci_register_driver(&mtip_pci_driver);
  4018. if (error) {
  4019. debugfs_remove(dfs_parent);
  4020. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  4021. }
  4022. return error;
  4023. }
  4024. /*
  4025. * Module de-initialization function.
  4026. *
  4027. * Called once when the module is unloaded. This function deallocates
  4028. * the major block device number allocated by mtip_init() and
  4029. * unregisters the PCI layer of the driver.
  4030. *
  4031. * Return value
  4032. * none
  4033. */
  4034. static void __exit mtip_exit(void)
  4035. {
  4036. /* Release the allocated major block device number. */
  4037. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  4038. /* Unregister the PCI driver. */
  4039. pci_unregister_driver(&mtip_pci_driver);
  4040. debugfs_remove_recursive(dfs_parent);
  4041. }
  4042. MODULE_AUTHOR("Micron Technology, Inc");
  4043. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  4044. MODULE_LICENSE("GPL");
  4045. MODULE_VERSION(MTIP_DRV_VERSION);
  4046. module_init(mtip_init);
  4047. module_exit(mtip_exit);