mtip32xx.c 90 KB

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