mtip32xx.c 112 KB

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