mtip32xx.c 100 KB

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