mtip32xx.c 112 KB

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