mtip32xx.c 84 KB

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