mtip32xx.c 114 KB

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