pmcraid.c 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
  4. *
  5. * Written By: Anil Ravindranath<[email protected]>
  6. * PMC-Sierra Inc
  7. *
  8. * Copyright (C) 2008, 2009 PMC Sierra Inc
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/ioport.h>
  16. #include <linux/delay.h>
  17. #include <linux/pci.h>
  18. #include <linux/wait.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/sched.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/firmware.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/hdreg.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <asm/irq.h>
  30. #include <asm/processor.h>
  31. #include <linux/libata.h>
  32. #include <linux/mutex.h>
  33. #include <linux/ktime.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/scsi_host.h>
  36. #include <scsi/scsi_device.h>
  37. #include <scsi/scsi_tcq.h>
  38. #include <scsi/scsi_eh.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <scsi/scsicam.h>
  41. #include "pmcraid.h"
  42. /*
  43. * Module configuration parameters
  44. */
  45. static unsigned int pmcraid_debug_log;
  46. static unsigned int pmcraid_disable_aen;
  47. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  48. static unsigned int pmcraid_enable_msix;
  49. /*
  50. * Data structures to support multiple adapters by the LLD.
  51. * pmcraid_adapter_count - count of configured adapters
  52. */
  53. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  54. /*
  55. * Supporting user-level control interface through IOCTL commands.
  56. * pmcraid_major - major number to use
  57. * pmcraid_minor - minor number(s) to use
  58. */
  59. static unsigned int pmcraid_major;
  60. static struct class *pmcraid_class;
  61. static DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  62. /*
  63. * Module parameters
  64. */
  65. MODULE_AUTHOR("Anil Ravindranath<[email protected]>");
  66. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  67. MODULE_LICENSE("GPL");
  68. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  69. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  70. MODULE_PARM_DESC(log_level,
  71. "Enables firmware error code logging, default :1 high-severity"
  72. " errors, 2: all errors including high-severity errors,"
  73. " 0: disables logging");
  74. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  75. MODULE_PARM_DESC(debug,
  76. "Enable driver verbose message logging. Set 1 to enable."
  77. "(default: 0)");
  78. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  79. MODULE_PARM_DESC(disable_aen,
  80. "Disable driver aen notifications to apps. Set 1 to disable."
  81. "(default: 0)");
  82. /* chip specific constants for PMC MaxRAID controllers (same for
  83. * 0x5220 and 0x8010
  84. */
  85. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  86. {
  87. .ioastatus = 0x0,
  88. .ioarrin = 0x00040,
  89. .mailbox = 0x7FC30,
  90. .global_intr_mask = 0x00034,
  91. .ioa_host_intr = 0x0009C,
  92. .ioa_host_intr_clr = 0x000A0,
  93. .ioa_host_msix_intr = 0x7FC40,
  94. .ioa_host_mask = 0x7FC28,
  95. .ioa_host_mask_clr = 0x7FC28,
  96. .host_ioa_intr = 0x00020,
  97. .host_ioa_intr_clr = 0x00020,
  98. .transop_timeout = 300
  99. }
  100. };
  101. /*
  102. * PCI device ids supported by pmcraid driver
  103. */
  104. static struct pci_device_id pmcraid_pci_table[] = {
  105. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  106. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  107. },
  108. {}
  109. };
  110. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  111. /**
  112. * pmcraid_slave_alloc - Prepare for commands to a device
  113. * @scsi_dev: scsi device struct
  114. *
  115. * This function is called by mid-layer prior to sending any command to the new
  116. * device. Stores resource entry details of the device in scsi_device struct.
  117. * Queuecommand uses the resource handle and other details to fill up IOARCB
  118. * while sending commands to the device.
  119. *
  120. * Return value:
  121. * 0 on success / -ENXIO if device does not exist
  122. */
  123. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  124. {
  125. struct pmcraid_resource_entry *temp, *res = NULL;
  126. struct pmcraid_instance *pinstance;
  127. u8 target, bus, lun;
  128. unsigned long lock_flags;
  129. int rc = -ENXIO;
  130. u16 fw_version;
  131. pinstance = shost_priv(scsi_dev->host);
  132. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  133. /* Driver exposes VSET and GSCSI resources only; all other device types
  134. * are not exposed. Resource list is synchronized using resource lock
  135. * so any traversal or modifications to the list should be done inside
  136. * this lock
  137. */
  138. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  139. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  140. /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
  141. if (RES_IS_VSET(temp->cfg_entry)) {
  142. if (fw_version <= PMCRAID_FW_VERSION_1)
  143. target = temp->cfg_entry.unique_flags1;
  144. else
  145. target = le16_to_cpu(temp->cfg_entry.array_id) & 0xFF;
  146. if (target > PMCRAID_MAX_VSET_TARGETS)
  147. continue;
  148. bus = PMCRAID_VSET_BUS_ID;
  149. lun = 0;
  150. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  151. target = RES_TARGET(temp->cfg_entry.resource_address);
  152. bus = PMCRAID_PHYS_BUS_ID;
  153. lun = RES_LUN(temp->cfg_entry.resource_address);
  154. } else {
  155. continue;
  156. }
  157. if (bus == scsi_dev->channel &&
  158. target == scsi_dev->id &&
  159. lun == scsi_dev->lun) {
  160. res = temp;
  161. break;
  162. }
  163. }
  164. if (res) {
  165. res->scsi_dev = scsi_dev;
  166. scsi_dev->hostdata = res;
  167. res->change_detected = 0;
  168. atomic_set(&res->read_failures, 0);
  169. atomic_set(&res->write_failures, 0);
  170. rc = 0;
  171. }
  172. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  173. return rc;
  174. }
  175. /**
  176. * pmcraid_slave_configure - Configures a SCSI device
  177. * @scsi_dev: scsi device struct
  178. *
  179. * This function is executed by SCSI mid layer just after a device is first
  180. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  181. * timeout value (default 30s) will be over-written to a higher value (60s)
  182. * and max_sectors value will be over-written to 512. It also sets queue depth
  183. * to host->cmd_per_lun value
  184. *
  185. * Return value:
  186. * 0 on success
  187. */
  188. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  189. {
  190. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  191. if (!res)
  192. return 0;
  193. /* LLD exposes VSETs and Enclosure devices only */
  194. if (RES_IS_GSCSI(res->cfg_entry) &&
  195. scsi_dev->type != TYPE_ENCLOSURE)
  196. return -ENXIO;
  197. pmcraid_info("configuring %x:%x:%x:%x\n",
  198. scsi_dev->host->unique_id,
  199. scsi_dev->channel,
  200. scsi_dev->id,
  201. (u8)scsi_dev->lun);
  202. if (RES_IS_GSCSI(res->cfg_entry)) {
  203. scsi_dev->allow_restart = 1;
  204. } else if (RES_IS_VSET(res->cfg_entry)) {
  205. scsi_dev->allow_restart = 1;
  206. blk_queue_rq_timeout(scsi_dev->request_queue,
  207. PMCRAID_VSET_IO_TIMEOUT);
  208. blk_queue_max_hw_sectors(scsi_dev->request_queue,
  209. PMCRAID_VSET_MAX_SECTORS);
  210. }
  211. /*
  212. * We never want to report TCQ support for these types of devices.
  213. */
  214. if (!RES_IS_GSCSI(res->cfg_entry) && !RES_IS_VSET(res->cfg_entry))
  215. scsi_dev->tagged_supported = 0;
  216. return 0;
  217. }
  218. /**
  219. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  220. *
  221. * @scsi_dev: scsi device struct
  222. *
  223. * This is called by mid-layer before removing a device. Pointer assignments
  224. * done in pmcraid_slave_alloc will be reset to NULL here.
  225. *
  226. * Return value
  227. * none
  228. */
  229. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  230. {
  231. struct pmcraid_resource_entry *res;
  232. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  233. if (res)
  234. res->scsi_dev = NULL;
  235. scsi_dev->hostdata = NULL;
  236. }
  237. /**
  238. * pmcraid_change_queue_depth - Change the device's queue depth
  239. * @scsi_dev: scsi device struct
  240. * @depth: depth to set
  241. *
  242. * Return value
  243. * actual depth set
  244. */
  245. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth)
  246. {
  247. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  248. depth = PMCRAID_MAX_CMD_PER_LUN;
  249. return scsi_change_queue_depth(scsi_dev, depth);
  250. }
  251. /**
  252. * pmcraid_init_cmdblk - initializes a command block
  253. *
  254. * @cmd: pointer to struct pmcraid_cmd to be initialized
  255. * @index: if >=0 first time initialization; otherwise reinitialization
  256. *
  257. * Return Value
  258. * None
  259. */
  260. static void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  261. {
  262. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  263. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  264. if (index >= 0) {
  265. /* first time initialization (called from probe) */
  266. u32 ioasa_offset =
  267. offsetof(struct pmcraid_control_block, ioasa);
  268. cmd->index = index;
  269. ioarcb->response_handle = cpu_to_le32(index << 2);
  270. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  271. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  272. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  273. } else {
  274. /* re-initialization of various lengths, called once command is
  275. * processed by IOA
  276. */
  277. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  278. ioarcb->hrrq_id = 0;
  279. ioarcb->request_flags0 = 0;
  280. ioarcb->request_flags1 = 0;
  281. ioarcb->cmd_timeout = 0;
  282. ioarcb->ioarcb_bus_addr &= cpu_to_le64(~0x1FULL);
  283. ioarcb->ioadl_bus_addr = 0;
  284. ioarcb->ioadl_length = 0;
  285. ioarcb->data_transfer_length = 0;
  286. ioarcb->add_cmd_param_length = 0;
  287. ioarcb->add_cmd_param_offset = 0;
  288. cmd->ioa_cb->ioasa.ioasc = 0;
  289. cmd->ioa_cb->ioasa.residual_data_length = 0;
  290. cmd->time_left = 0;
  291. }
  292. cmd->cmd_done = NULL;
  293. cmd->scsi_cmd = NULL;
  294. cmd->release = 0;
  295. cmd->completion_req = 0;
  296. cmd->sense_buffer = NULL;
  297. cmd->sense_buffer_dma = 0;
  298. cmd->dma_handle = 0;
  299. timer_setup(&cmd->timer, NULL, 0);
  300. }
  301. /**
  302. * pmcraid_reinit_cmdblk - reinitialize a command block
  303. *
  304. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  305. *
  306. * Return Value
  307. * None
  308. */
  309. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  310. {
  311. pmcraid_init_cmdblk(cmd, -1);
  312. }
  313. /**
  314. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  315. * @pinstance: adapter instance structure
  316. *
  317. * Return Value:
  318. * returns pointer to cmd block or NULL if no blocks are available
  319. */
  320. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  321. struct pmcraid_instance *pinstance
  322. )
  323. {
  324. struct pmcraid_cmd *cmd = NULL;
  325. unsigned long lock_flags;
  326. /* free cmd block list is protected by free_pool_lock */
  327. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  328. if (!list_empty(&pinstance->free_cmd_pool)) {
  329. cmd = list_entry(pinstance->free_cmd_pool.next,
  330. struct pmcraid_cmd, free_list);
  331. list_del(&cmd->free_list);
  332. }
  333. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  334. /* Initialize the command block before giving it the caller */
  335. if (cmd != NULL)
  336. pmcraid_reinit_cmdblk(cmd);
  337. return cmd;
  338. }
  339. /**
  340. * pmcraid_return_cmd - return a completed command block back into free pool
  341. * @cmd: pointer to the command block
  342. *
  343. * Return Value:
  344. * nothing
  345. */
  346. static void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  347. {
  348. struct pmcraid_instance *pinstance = cmd->drv_inst;
  349. unsigned long lock_flags;
  350. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  351. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  352. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  353. }
  354. /**
  355. * pmcraid_read_interrupts - reads IOA interrupts
  356. *
  357. * @pinstance: pointer to adapter instance structure
  358. *
  359. * Return value
  360. * interrupts read from IOA
  361. */
  362. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  363. {
  364. return (pinstance->interrupt_mode) ?
  365. ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
  366. ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  367. }
  368. /**
  369. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  370. *
  371. * @pinstance: pointer to per adapter instance structure
  372. * @intrs: interrupts to disable
  373. *
  374. * Return Value
  375. * None
  376. */
  377. static void pmcraid_disable_interrupts(
  378. struct pmcraid_instance *pinstance,
  379. u32 intrs
  380. )
  381. {
  382. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  383. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  384. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  385. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  386. ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  387. if (!pinstance->interrupt_mode) {
  388. iowrite32(intrs,
  389. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  390. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  391. }
  392. }
  393. /**
  394. * pmcraid_enable_interrupts - Enables specified interrupts
  395. *
  396. * @pinstance: pointer to per adapter instance structure
  397. * @intrs: interrupts to enable
  398. *
  399. * Return Value
  400. * None
  401. */
  402. static void pmcraid_enable_interrupts(
  403. struct pmcraid_instance *pinstance,
  404. u32 intrs)
  405. {
  406. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  407. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  408. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  409. if (!pinstance->interrupt_mode) {
  410. iowrite32(~intrs,
  411. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  412. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  413. }
  414. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  415. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  416. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  417. }
  418. /**
  419. * pmcraid_clr_trans_op - clear trans to op interrupt
  420. *
  421. * @pinstance: pointer to per adapter instance structure
  422. *
  423. * Return Value
  424. * None
  425. */
  426. static void pmcraid_clr_trans_op(
  427. struct pmcraid_instance *pinstance
  428. )
  429. {
  430. unsigned long lock_flags;
  431. if (!pinstance->interrupt_mode) {
  432. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  433. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  434. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  435. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  436. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  437. ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
  438. }
  439. if (pinstance->reset_cmd != NULL) {
  440. del_timer(&pinstance->reset_cmd->timer);
  441. spin_lock_irqsave(
  442. pinstance->host->host_lock, lock_flags);
  443. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  444. spin_unlock_irqrestore(
  445. pinstance->host->host_lock, lock_flags);
  446. }
  447. }
  448. /**
  449. * pmcraid_reset_type - Determine the required reset type
  450. * @pinstance: pointer to adapter instance structure
  451. *
  452. * IOA requires hard reset if any of the following conditions is true.
  453. * 1. If HRRQ valid interrupt is not masked
  454. * 2. IOA reset alert doorbell is set
  455. * 3. If there are any error interrupts
  456. */
  457. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  458. {
  459. u32 mask;
  460. u32 intrs;
  461. u32 alerts;
  462. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  463. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  464. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  465. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  466. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  467. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  468. pmcraid_info("IOA requires hard reset\n");
  469. pinstance->ioa_hard_reset = 1;
  470. }
  471. /* If unit check is active, trigger the dump */
  472. if (intrs & INTRS_IOA_UNIT_CHECK)
  473. pinstance->ioa_unit_check = 1;
  474. }
  475. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  476. /**
  477. * pmcraid_bist_done - completion function for PCI BIST
  478. * @t: pointer to reset command
  479. * Return Value
  480. * none
  481. */
  482. static void pmcraid_bist_done(struct timer_list *t)
  483. {
  484. struct pmcraid_cmd *cmd = from_timer(cmd, t, timer);
  485. struct pmcraid_instance *pinstance = cmd->drv_inst;
  486. unsigned long lock_flags;
  487. int rc;
  488. u16 pci_reg;
  489. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  490. /* If PCI config space can't be accessed wait for another two secs */
  491. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  492. cmd->time_left > 0) {
  493. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  494. cmd->timer.expires = jiffies + cmd->time_left;
  495. cmd->time_left = 0;
  496. add_timer(&cmd->timer);
  497. } else {
  498. cmd->time_left = 0;
  499. pmcraid_info("BIST is complete, proceeding with reset\n");
  500. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  501. pmcraid_ioa_reset(cmd);
  502. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  503. }
  504. }
  505. /**
  506. * pmcraid_start_bist - starts BIST
  507. * @cmd: pointer to reset cmd
  508. * Return Value
  509. * none
  510. */
  511. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  512. {
  513. struct pmcraid_instance *pinstance = cmd->drv_inst;
  514. u32 doorbells, intrs;
  515. /* proceed with bist and wait for 2 seconds */
  516. iowrite32(DOORBELL_IOA_START_BIST,
  517. pinstance->int_regs.host_ioa_interrupt_reg);
  518. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  519. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  520. pmcraid_info("doorbells after start bist: %x intrs: %x\n",
  521. doorbells, intrs);
  522. cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  523. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  524. cmd->timer.function = pmcraid_bist_done;
  525. add_timer(&cmd->timer);
  526. }
  527. /**
  528. * pmcraid_reset_alert_done - completion routine for reset_alert
  529. * @t: pointer to command block used in reset sequence
  530. * Return value
  531. * None
  532. */
  533. static void pmcraid_reset_alert_done(struct timer_list *t)
  534. {
  535. struct pmcraid_cmd *cmd = from_timer(cmd, t, timer);
  536. struct pmcraid_instance *pinstance = cmd->drv_inst;
  537. u32 status = ioread32(pinstance->ioa_status);
  538. unsigned long lock_flags;
  539. /* if the critical operation in progress bit is set or the wait times
  540. * out, invoke reset engine to proceed with hard reset. If there is
  541. * some more time to wait, restart the timer
  542. */
  543. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  544. cmd->time_left <= 0) {
  545. pmcraid_info("critical op is reset proceeding with reset\n");
  546. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  547. pmcraid_ioa_reset(cmd);
  548. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  549. } else {
  550. pmcraid_info("critical op is not yet reset waiting again\n");
  551. /* restart timer if some more time is available to wait */
  552. cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  553. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  554. cmd->timer.function = pmcraid_reset_alert_done;
  555. add_timer(&cmd->timer);
  556. }
  557. }
  558. static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
  559. /**
  560. * pmcraid_reset_alert - alerts IOA for a possible reset
  561. * @cmd: command block to be used for reset sequence.
  562. *
  563. * Return Value
  564. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  565. * successfully written to IOA. Returns non-zero in case pci_config_space
  566. * is not accessible
  567. */
  568. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  569. {
  570. struct pmcraid_instance *pinstance = cmd->drv_inst;
  571. u32 doorbells;
  572. int rc;
  573. u16 pci_reg;
  574. /* If we are able to access IOA PCI config space, alert IOA that we are
  575. * going to reset it soon. This enables IOA to preserv persistent error
  576. * data if any. In case memory space is not accessible, proceed with
  577. * BIST or slot_reset
  578. */
  579. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  580. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  581. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  582. * reset IOA doesn't generate any interrupts when CRITICAL
  583. * OPERATION bit is reset. A timer is started to wait for this
  584. * bit to be reset.
  585. */
  586. cmd->time_left = PMCRAID_RESET_TIMEOUT;
  587. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  588. cmd->timer.function = pmcraid_reset_alert_done;
  589. add_timer(&cmd->timer);
  590. iowrite32(DOORBELL_IOA_RESET_ALERT,
  591. pinstance->int_regs.host_ioa_interrupt_reg);
  592. doorbells =
  593. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  594. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  595. } else {
  596. pmcraid_info("PCI config is not accessible starting BIST\n");
  597. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  598. pmcraid_start_bist(cmd);
  599. }
  600. }
  601. /**
  602. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  603. *
  604. * @t: pointer to command structure, that got timedout
  605. *
  606. * This function blocks host requests and initiates an adapter reset.
  607. *
  608. * Return value:
  609. * None
  610. */
  611. static void pmcraid_timeout_handler(struct timer_list *t)
  612. {
  613. struct pmcraid_cmd *cmd = from_timer(cmd, t, timer);
  614. struct pmcraid_instance *pinstance = cmd->drv_inst;
  615. unsigned long lock_flags;
  616. dev_info(&pinstance->pdev->dev,
  617. "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
  618. cmd->ioa_cb->ioarcb.cdb[0]);
  619. /* Command timeouts result in hard reset sequence. The command that got
  620. * timed out may be the one used as part of reset sequence. In this
  621. * case restart reset sequence using the same command block even if
  622. * reset is in progress. Otherwise fail this command and get a free
  623. * command block to restart the reset sequence.
  624. */
  625. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  626. if (!pinstance->ioa_reset_in_progress) {
  627. pinstance->ioa_reset_attempts = 0;
  628. cmd = pmcraid_get_free_cmd(pinstance);
  629. /* If we are out of command blocks, just return here itself.
  630. * Some other command's timeout handler can do the reset job
  631. */
  632. if (cmd == NULL) {
  633. spin_unlock_irqrestore(pinstance->host->host_lock,
  634. lock_flags);
  635. pmcraid_err("no free cmnd block for timeout handler\n");
  636. return;
  637. }
  638. pinstance->reset_cmd = cmd;
  639. pinstance->ioa_reset_in_progress = 1;
  640. } else {
  641. pmcraid_info("reset is already in progress\n");
  642. if (pinstance->reset_cmd != cmd) {
  643. /* This command should have been given to IOA, this
  644. * command will be completed by fail_outstanding_cmds
  645. * anyway
  646. */
  647. pmcraid_err("cmd is pending but reset in progress\n");
  648. }
  649. /* If this command was being used as part of the reset
  650. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  651. * causes fail_outstanding_commands not to return the command
  652. * block back to free pool
  653. */
  654. if (cmd == pinstance->reset_cmd)
  655. cmd->cmd_done = pmcraid_ioa_reset;
  656. }
  657. /* Notify apps of important IOA bringup/bringdown sequences */
  658. if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
  659. pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
  660. pmcraid_notify_ioastate(pinstance,
  661. PMC_DEVICE_EVENT_RESET_START);
  662. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  663. scsi_block_requests(pinstance->host);
  664. pmcraid_reset_alert(cmd);
  665. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  666. }
  667. /**
  668. * pmcraid_internal_done - completion routine for internally generated cmds
  669. *
  670. * @cmd: command that got response from IOA
  671. *
  672. * Return Value:
  673. * none
  674. */
  675. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  676. {
  677. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  678. cmd->ioa_cb->ioarcb.cdb[0],
  679. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  680. /* Some of the internal commands are sent with callers blocking for the
  681. * response. Same will be indicated as part of cmd->completion_req
  682. * field. Response path needs to wake up any waiters waiting for cmd
  683. * completion if this flag is set.
  684. */
  685. if (cmd->completion_req) {
  686. cmd->completion_req = 0;
  687. complete(&cmd->wait_for_completion);
  688. }
  689. /* most of the internal commands are completed by caller itself, so
  690. * no need to return the command block back to free pool until we are
  691. * required to do so (e.g once done with initialization).
  692. */
  693. if (cmd->release) {
  694. cmd->release = 0;
  695. pmcraid_return_cmd(cmd);
  696. }
  697. }
  698. /**
  699. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  700. *
  701. * @cmd: command that got response from IOA
  702. *
  703. * This routine is called after driver re-reads configuration table due to a
  704. * lost CCN. It returns the command block back to free pool and schedules
  705. * worker thread to add/delete devices into the system.
  706. *
  707. * Return Value:
  708. * none
  709. */
  710. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  711. {
  712. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  713. cmd->ioa_cb->ioarcb.cdb[0],
  714. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  715. if (cmd->release) {
  716. cmd->release = 0;
  717. pmcraid_return_cmd(cmd);
  718. }
  719. pmcraid_info("scheduling worker for config table reinitialization\n");
  720. schedule_work(&cmd->drv_inst->worker_q);
  721. }
  722. /**
  723. * pmcraid_erp_done - Process completion of SCSI error response from device
  724. * @cmd: pmcraid_command
  725. *
  726. * This function copies the sense buffer into the scsi_cmd struct and completes
  727. * scsi_cmd by calling scsi_done function.
  728. *
  729. * Return value:
  730. * none
  731. */
  732. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  733. {
  734. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  735. struct pmcraid_instance *pinstance = cmd->drv_inst;
  736. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  737. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  738. scsi_cmd->result |= (DID_ERROR << 16);
  739. scmd_printk(KERN_INFO, scsi_cmd,
  740. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  741. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  742. }
  743. if (cmd->sense_buffer) {
  744. dma_unmap_single(&pinstance->pdev->dev, cmd->sense_buffer_dma,
  745. SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  746. cmd->sense_buffer = NULL;
  747. cmd->sense_buffer_dma = 0;
  748. }
  749. scsi_dma_unmap(scsi_cmd);
  750. pmcraid_return_cmd(cmd);
  751. scsi_done(scsi_cmd);
  752. }
  753. /**
  754. * _pmcraid_fire_command - sends an IOA command to adapter
  755. *
  756. * This function adds the given block into pending command list
  757. * and returns without waiting
  758. *
  759. * @cmd : command to be sent to the device
  760. *
  761. * Return Value
  762. * None
  763. */
  764. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  765. {
  766. struct pmcraid_instance *pinstance = cmd->drv_inst;
  767. unsigned long lock_flags;
  768. /* Add this command block to pending cmd pool. We do this prior to
  769. * writting IOARCB to ioarrin because IOA might complete the command
  770. * by the time we are about to add it to the list. Response handler
  771. * (isr/tasklet) looks for cmd block in the pending pending list.
  772. */
  773. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  774. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  775. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  776. atomic_inc(&pinstance->outstanding_cmds);
  777. /* driver writes lower 32-bit value of IOARCB address only */
  778. mb();
  779. iowrite32(le64_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr), pinstance->ioarrin);
  780. }
  781. /**
  782. * pmcraid_send_cmd - fires a command to IOA
  783. *
  784. * This function also sets up timeout function, and command completion
  785. * function
  786. *
  787. * @cmd: pointer to the command block to be fired to IOA
  788. * @cmd_done: command completion function, called once IOA responds
  789. * @timeout: timeout to wait for this command completion
  790. * @timeout_func: timeout handler
  791. *
  792. * Return value
  793. * none
  794. */
  795. static void pmcraid_send_cmd(
  796. struct pmcraid_cmd *cmd,
  797. void (*cmd_done) (struct pmcraid_cmd *),
  798. unsigned long timeout,
  799. void (*timeout_func) (struct timer_list *)
  800. )
  801. {
  802. /* initialize done function */
  803. cmd->cmd_done = cmd_done;
  804. if (timeout_func) {
  805. /* setup timeout handler */
  806. cmd->timer.expires = jiffies + timeout;
  807. cmd->timer.function = timeout_func;
  808. add_timer(&cmd->timer);
  809. }
  810. /* fire the command to IOA */
  811. _pmcraid_fire_command(cmd);
  812. }
  813. /**
  814. * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
  815. * @cmd: pointer to the command block used for sending IOA shutdown command
  816. *
  817. * Return value
  818. * None
  819. */
  820. static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
  821. {
  822. struct pmcraid_instance *pinstance = cmd->drv_inst;
  823. unsigned long lock_flags;
  824. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  825. pmcraid_ioa_reset(cmd);
  826. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  827. }
  828. /**
  829. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  830. *
  831. * @cmd: pointer to the command block used as part of reset sequence
  832. *
  833. * Return Value
  834. * None
  835. */
  836. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  837. {
  838. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  839. cmd->ioa_cb->ioarcb.cdb[0],
  840. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  841. /* Note that commands sent during reset require next command to be sent
  842. * to IOA. Hence reinit the done function as well as timeout function
  843. */
  844. pmcraid_reinit_cmdblk(cmd);
  845. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  846. cmd->ioa_cb->ioarcb.resource_handle =
  847. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  848. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  849. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  850. /* fire shutdown command to hardware. */
  851. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  852. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  853. pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
  854. pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
  855. PMCRAID_SHUTDOWN_TIMEOUT,
  856. pmcraid_timeout_handler);
  857. }
  858. static void pmcraid_querycfg(struct pmcraid_cmd *);
  859. /**
  860. * pmcraid_get_fwversion_done - completion function for get_fwversion
  861. *
  862. * @cmd: pointer to command block used to send INQUIRY command
  863. *
  864. * Return Value
  865. * none
  866. */
  867. static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
  868. {
  869. struct pmcraid_instance *pinstance = cmd->drv_inst;
  870. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  871. unsigned long lock_flags;
  872. /* configuration table entry size depends on firmware version. If fw
  873. * version is not known, it is not possible to interpret IOA config
  874. * table
  875. */
  876. if (ioasc) {
  877. pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
  878. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  879. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  880. pmcraid_reset_alert(cmd);
  881. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  882. } else {
  883. pmcraid_querycfg(cmd);
  884. }
  885. }
  886. /**
  887. * pmcraid_get_fwversion - reads firmware version information
  888. *
  889. * @cmd: pointer to command block used to send INQUIRY command
  890. *
  891. * Return Value
  892. * none
  893. */
  894. static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
  895. {
  896. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  897. struct pmcraid_ioadl_desc *ioadl;
  898. struct pmcraid_instance *pinstance = cmd->drv_inst;
  899. u16 data_size = sizeof(struct pmcraid_inquiry_data);
  900. pmcraid_reinit_cmdblk(cmd);
  901. ioarcb->request_type = REQ_TYPE_SCSI;
  902. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  903. ioarcb->cdb[0] = INQUIRY;
  904. ioarcb->cdb[1] = 1;
  905. ioarcb->cdb[2] = 0xD0;
  906. ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
  907. ioarcb->cdb[4] = data_size & 0xFF;
  908. /* Since entire inquiry data it can be part of IOARCB itself
  909. */
  910. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  911. offsetof(struct pmcraid_ioarcb,
  912. add_data.u.ioadl[0]));
  913. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  914. ioarcb->ioarcb_bus_addr &= cpu_to_le64(~(0x1FULL));
  915. ioarcb->request_flags0 |= NO_LINK_DESCS;
  916. ioarcb->data_transfer_length = cpu_to_le32(data_size);
  917. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  918. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  919. ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
  920. ioadl->data_len = cpu_to_le32(data_size);
  921. pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
  922. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  923. }
  924. /**
  925. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  926. * @cmd: pointer to command block to be used for identify hrrq
  927. *
  928. * Return Value
  929. * none
  930. */
  931. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  932. {
  933. struct pmcraid_instance *pinstance = cmd->drv_inst;
  934. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  935. int index = cmd->hrrq_index;
  936. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  937. __be32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  938. void (*done_function)(struct pmcraid_cmd *);
  939. pmcraid_reinit_cmdblk(cmd);
  940. cmd->hrrq_index = index + 1;
  941. if (cmd->hrrq_index < pinstance->num_hrrq) {
  942. done_function = pmcraid_identify_hrrq;
  943. } else {
  944. cmd->hrrq_index = 0;
  945. done_function = pmcraid_get_fwversion;
  946. }
  947. /* Initialize ioarcb */
  948. ioarcb->request_type = REQ_TYPE_IOACMD;
  949. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  950. /* initialize the hrrq number where IOA will respond to this command */
  951. ioarcb->hrrq_id = index;
  952. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  953. ioarcb->cdb[1] = index;
  954. /* IOA expects 64-bit pci address to be written in B.E format
  955. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  956. */
  957. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
  958. hrrq_addr, ioarcb->ioarcb_bus_addr, index);
  959. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  960. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  961. /* Subsequent commands require HRRQ identification to be successful.
  962. * Note that this gets called even during reset from SCSI mid-layer
  963. * or tasklet
  964. */
  965. pmcraid_send_cmd(cmd, done_function,
  966. PMCRAID_INTERNAL_TIMEOUT,
  967. pmcraid_timeout_handler);
  968. }
  969. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  970. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  971. /**
  972. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  973. *
  974. * @cmd: initialized command block pointer
  975. *
  976. * Return Value
  977. * none
  978. */
  979. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  980. {
  981. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  982. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  983. else
  984. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  985. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  986. }
  987. /**
  988. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  989. *
  990. * @pinstance: pointer to adapter instance structure
  991. * @type: HCAM type
  992. *
  993. * Return Value
  994. * pointer to initialized pmcraid_cmd structure or NULL
  995. */
  996. static struct pmcraid_cmd *pmcraid_init_hcam
  997. (
  998. struct pmcraid_instance *pinstance,
  999. u8 type
  1000. )
  1001. {
  1002. struct pmcraid_cmd *cmd;
  1003. struct pmcraid_ioarcb *ioarcb;
  1004. struct pmcraid_ioadl_desc *ioadl;
  1005. struct pmcraid_hostrcb *hcam;
  1006. void (*cmd_done) (struct pmcraid_cmd *);
  1007. dma_addr_t dma;
  1008. int rcb_size;
  1009. cmd = pmcraid_get_free_cmd(pinstance);
  1010. if (!cmd) {
  1011. pmcraid_err("no free command blocks for hcam\n");
  1012. return cmd;
  1013. }
  1014. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  1015. rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
  1016. cmd_done = pmcraid_process_ccn;
  1017. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  1018. hcam = &pinstance->ccn;
  1019. } else {
  1020. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  1021. cmd_done = pmcraid_process_ldn;
  1022. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  1023. hcam = &pinstance->ldn;
  1024. }
  1025. /* initialize command pointer used for HCAM registration */
  1026. hcam->cmd = cmd;
  1027. ioarcb = &cmd->ioa_cb->ioarcb;
  1028. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1029. offsetof(struct pmcraid_ioarcb,
  1030. add_data.u.ioadl[0]));
  1031. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1032. ioadl = ioarcb->add_data.u.ioadl;
  1033. /* Initialize ioarcb */
  1034. ioarcb->request_type = REQ_TYPE_HCAM;
  1035. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1036. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  1037. ioarcb->cdb[1] = type;
  1038. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  1039. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  1040. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  1041. ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
  1042. ioadl[0].data_len = cpu_to_le32(rcb_size);
  1043. ioadl[0].address = cpu_to_le64(dma);
  1044. cmd->cmd_done = cmd_done;
  1045. return cmd;
  1046. }
  1047. /**
  1048. * pmcraid_send_hcam - Send an HCAM to IOA
  1049. * @pinstance: ioa config struct
  1050. * @type: HCAM type
  1051. *
  1052. * This function will send a Host Controlled Async command to IOA.
  1053. *
  1054. * Return value:
  1055. * none
  1056. */
  1057. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  1058. {
  1059. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  1060. pmcraid_send_hcam_cmd(cmd);
  1061. }
  1062. /**
  1063. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  1064. *
  1065. * @cmd: pointer to cmd that is used as cancelling command
  1066. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  1067. */
  1068. static void pmcraid_prepare_cancel_cmd(
  1069. struct pmcraid_cmd *cmd,
  1070. struct pmcraid_cmd *cmd_to_cancel
  1071. )
  1072. {
  1073. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1074. __be64 ioarcb_addr;
  1075. /* IOARCB address of the command to be cancelled is given in
  1076. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  1077. * IOARCB address are not masked.
  1078. */
  1079. ioarcb_addr = cpu_to_be64(le64_to_cpu(cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr));
  1080. /* Get the resource handle to where the command to be aborted has been
  1081. * sent.
  1082. */
  1083. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  1084. ioarcb->request_type = REQ_TYPE_IOACMD;
  1085. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1086. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  1087. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  1088. }
  1089. /**
  1090. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1091. *
  1092. * @cmd: command to be used as cancelling command
  1093. * @type: HCAM type
  1094. * @cmd_done: op done function for the cancelling command
  1095. */
  1096. static void pmcraid_cancel_hcam(
  1097. struct pmcraid_cmd *cmd,
  1098. u8 type,
  1099. void (*cmd_done) (struct pmcraid_cmd *)
  1100. )
  1101. {
  1102. struct pmcraid_instance *pinstance;
  1103. struct pmcraid_hostrcb *hcam;
  1104. pinstance = cmd->drv_inst;
  1105. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1106. &pinstance->ldn : &pinstance->ccn;
  1107. /* prepare for cancelling previous hcam command. If the HCAM is
  1108. * currently not pending with IOA, we would have hcam->cmd as non-null
  1109. */
  1110. if (hcam->cmd == NULL)
  1111. return;
  1112. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1113. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1114. * schedule queuecommand while we are doing this
  1115. */
  1116. pmcraid_send_cmd(cmd, cmd_done,
  1117. PMCRAID_INTERNAL_TIMEOUT,
  1118. pmcraid_timeout_handler);
  1119. }
  1120. /**
  1121. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1122. *
  1123. * @cmd: command block to be used for cancelling the HCAM
  1124. */
  1125. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1126. {
  1127. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1128. cmd->ioa_cb->ioarcb.cdb[0],
  1129. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1130. pmcraid_reinit_cmdblk(cmd);
  1131. pmcraid_cancel_hcam(cmd,
  1132. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1133. pmcraid_ioa_shutdown);
  1134. }
  1135. /**
  1136. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1137. *
  1138. * @cmd: command block to be used for cancelling the HCAM
  1139. */
  1140. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1141. {
  1142. pmcraid_cancel_hcam(cmd,
  1143. PMCRAID_HCAM_CODE_LOG_DATA,
  1144. pmcraid_cancel_ccn);
  1145. }
  1146. /**
  1147. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1148. *
  1149. * @fw_version: firmware version code
  1150. * @cfgte: pointer to configuration table entry of the resource
  1151. *
  1152. * Return value:
  1153. * true if resource can be added to midlayer, false(0) otherwise
  1154. */
  1155. static int pmcraid_expose_resource(u16 fw_version,
  1156. struct pmcraid_config_table_entry *cfgte)
  1157. {
  1158. int retval = 0;
  1159. if (cfgte->resource_type == RES_TYPE_VSET) {
  1160. if (fw_version <= PMCRAID_FW_VERSION_1)
  1161. retval = ((cfgte->unique_flags1 & 0x80) == 0);
  1162. else
  1163. retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
  1164. (cfgte->unique_flags1 & 0x80) == 0);
  1165. } else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1166. retval = (RES_BUS(cfgte->resource_address) !=
  1167. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1168. return retval;
  1169. }
  1170. /* attributes supported by pmcraid_event_family */
  1171. enum {
  1172. PMCRAID_AEN_ATTR_UNSPEC,
  1173. PMCRAID_AEN_ATTR_EVENT,
  1174. __PMCRAID_AEN_ATTR_MAX,
  1175. };
  1176. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1177. /* commands supported by pmcraid_event_family */
  1178. enum {
  1179. PMCRAID_AEN_CMD_UNSPEC,
  1180. PMCRAID_AEN_CMD_EVENT,
  1181. __PMCRAID_AEN_CMD_MAX,
  1182. };
  1183. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1184. static struct genl_multicast_group pmcraid_mcgrps[] = {
  1185. { .name = "events", /* not really used - see ID discussion below */ },
  1186. };
  1187. static struct genl_family pmcraid_event_family __ro_after_init = {
  1188. .module = THIS_MODULE,
  1189. .name = "pmcraid",
  1190. .version = 1,
  1191. .maxattr = PMCRAID_AEN_ATTR_MAX,
  1192. .mcgrps = pmcraid_mcgrps,
  1193. .n_mcgrps = ARRAY_SIZE(pmcraid_mcgrps),
  1194. };
  1195. /**
  1196. * pmcraid_netlink_init - registers pmcraid_event_family
  1197. *
  1198. * Return value:
  1199. * 0 if the pmcraid_event_family is successfully registered
  1200. * with netlink generic, non-zero otherwise
  1201. */
  1202. static int __init pmcraid_netlink_init(void)
  1203. {
  1204. int result;
  1205. result = genl_register_family(&pmcraid_event_family);
  1206. if (result)
  1207. return result;
  1208. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1209. pmcraid_event_family.id);
  1210. return result;
  1211. }
  1212. /**
  1213. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1214. *
  1215. * Return value:
  1216. * none
  1217. */
  1218. static void pmcraid_netlink_release(void)
  1219. {
  1220. genl_unregister_family(&pmcraid_event_family);
  1221. }
  1222. /*
  1223. * pmcraid_notify_aen - sends event msg to user space application
  1224. * @pinstance: pointer to adapter instance structure
  1225. *
  1226. * Return value:
  1227. * 0 if success, error value in case of any failure.
  1228. */
  1229. static int pmcraid_notify_aen(
  1230. struct pmcraid_instance *pinstance,
  1231. struct pmcraid_aen_msg *aen_msg,
  1232. u32 data_size)
  1233. {
  1234. struct sk_buff *skb;
  1235. void *msg_header;
  1236. u32 total_size, nla_genl_hdr_total_size;
  1237. int result;
  1238. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1239. MINOR(pinstance->cdev.dev));
  1240. aen_msg->length = data_size;
  1241. data_size += sizeof(*aen_msg);
  1242. total_size = nla_total_size(data_size);
  1243. /* Add GENL_HDR to total_size */
  1244. nla_genl_hdr_total_size =
  1245. (total_size + (GENL_HDRLEN +
  1246. ((struct genl_family *)&pmcraid_event_family)->hdrsize)
  1247. + NLMSG_HDRLEN);
  1248. skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
  1249. if (!skb) {
  1250. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1251. total_size);
  1252. return -ENOMEM;
  1253. }
  1254. /* add the genetlink message header */
  1255. msg_header = genlmsg_put(skb, 0, 0,
  1256. &pmcraid_event_family, 0,
  1257. PMCRAID_AEN_CMD_EVENT);
  1258. if (!msg_header) {
  1259. pmcraid_err("failed to copy command details\n");
  1260. nlmsg_free(skb);
  1261. return -ENOMEM;
  1262. }
  1263. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1264. if (result) {
  1265. pmcraid_err("failed to copy AEN attribute data\n");
  1266. nlmsg_free(skb);
  1267. return -EINVAL;
  1268. }
  1269. /* send genetlink multicast message to notify applications */
  1270. genlmsg_end(skb, msg_header);
  1271. result = genlmsg_multicast(&pmcraid_event_family, skb,
  1272. 0, 0, GFP_ATOMIC);
  1273. /* If there are no listeners, genlmsg_multicast may return non-zero
  1274. * value.
  1275. */
  1276. if (result)
  1277. pmcraid_info("error (%x) sending aen event message\n", result);
  1278. return result;
  1279. }
  1280. /**
  1281. * pmcraid_notify_ccn - notifies about CCN event msg to user space
  1282. * @pinstance: pointer adapter instance structure
  1283. *
  1284. * Return value:
  1285. * 0 if success, error value in case of any failure
  1286. */
  1287. static int pmcraid_notify_ccn(struct pmcraid_instance *pinstance)
  1288. {
  1289. return pmcraid_notify_aen(pinstance,
  1290. pinstance->ccn.msg,
  1291. le32_to_cpu(pinstance->ccn.hcam->data_len) +
  1292. sizeof(struct pmcraid_hcam_hdr));
  1293. }
  1294. /**
  1295. * pmcraid_notify_ldn - notifies about CCN event msg to user space
  1296. * @pinstance: pointer adapter instance structure
  1297. *
  1298. * Return value:
  1299. * 0 if success, error value in case of any failure
  1300. */
  1301. static int pmcraid_notify_ldn(struct pmcraid_instance *pinstance)
  1302. {
  1303. return pmcraid_notify_aen(pinstance,
  1304. pinstance->ldn.msg,
  1305. le32_to_cpu(pinstance->ldn.hcam->data_len) +
  1306. sizeof(struct pmcraid_hcam_hdr));
  1307. }
  1308. /**
  1309. * pmcraid_notify_ioastate - sends IOA state event msg to user space
  1310. * @pinstance: pointer adapter instance structure
  1311. * @evt: controller state event to be sent
  1312. *
  1313. * Return value:
  1314. * 0 if success, error value in case of any failure
  1315. */
  1316. static void pmcraid_notify_ioastate(struct pmcraid_instance *pinstance, u32 evt)
  1317. {
  1318. pinstance->scn.ioa_state = evt;
  1319. pmcraid_notify_aen(pinstance,
  1320. &pinstance->scn.msg,
  1321. sizeof(u32));
  1322. }
  1323. /**
  1324. * pmcraid_handle_config_change - Handle a config change from the adapter
  1325. * @pinstance: pointer to per adapter instance structure
  1326. *
  1327. * Return value:
  1328. * none
  1329. */
  1330. static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
  1331. {
  1332. struct pmcraid_config_table_entry *cfg_entry;
  1333. struct pmcraid_hcam_ccn *ccn_hcam;
  1334. struct pmcraid_cmd *cmd;
  1335. struct pmcraid_cmd *cfgcmd;
  1336. struct pmcraid_resource_entry *res = NULL;
  1337. unsigned long lock_flags;
  1338. unsigned long host_lock_flags;
  1339. u32 new_entry = 1;
  1340. u32 hidden_entry = 0;
  1341. u16 fw_version;
  1342. int rc;
  1343. ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
  1344. cfg_entry = &ccn_hcam->cfg_entry;
  1345. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  1346. pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
  1347. res: %x:%x:%x:%x\n",
  1348. le32_to_cpu(pinstance->ccn.hcam->ilid),
  1349. pinstance->ccn.hcam->op_code,
  1350. (le32_to_cpu(pinstance->ccn.hcam->timestamp1) |
  1351. ((le32_to_cpu(pinstance->ccn.hcam->timestamp2) & 0xffffffffLL) << 32)),
  1352. pinstance->ccn.hcam->notification_type,
  1353. pinstance->ccn.hcam->notification_lost,
  1354. pinstance->ccn.hcam->flags,
  1355. pinstance->host->unique_id,
  1356. RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
  1357. (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
  1358. RES_BUS(cfg_entry->resource_address)),
  1359. RES_IS_VSET(*cfg_entry) ?
  1360. (fw_version <= PMCRAID_FW_VERSION_1 ?
  1361. cfg_entry->unique_flags1 :
  1362. le16_to_cpu(cfg_entry->array_id) & 0xFF) :
  1363. RES_TARGET(cfg_entry->resource_address),
  1364. RES_LUN(cfg_entry->resource_address));
  1365. /* If this HCAM indicates a lost notification, read the config table */
  1366. if (pinstance->ccn.hcam->notification_lost) {
  1367. cfgcmd = pmcraid_get_free_cmd(pinstance);
  1368. if (cfgcmd) {
  1369. pmcraid_info("lost CCN, reading config table\b");
  1370. pinstance->reinit_cfg_table = 1;
  1371. pmcraid_querycfg(cfgcmd);
  1372. } else {
  1373. pmcraid_err("lost CCN, no free cmd for querycfg\n");
  1374. }
  1375. goto out_notify_apps;
  1376. }
  1377. /* If this resource is not going to be added to mid-layer, just notify
  1378. * applications and return. If this notification is about hiding a VSET
  1379. * resource, check if it was exposed already.
  1380. */
  1381. if (pinstance->ccn.hcam->notification_type ==
  1382. NOTIFICATION_TYPE_ENTRY_CHANGED &&
  1383. cfg_entry->resource_type == RES_TYPE_VSET) {
  1384. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1385. } else if (!pmcraid_expose_resource(fw_version, cfg_entry)) {
  1386. goto out_notify_apps;
  1387. }
  1388. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  1389. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  1390. rc = memcmp(&res->cfg_entry.resource_address,
  1391. &cfg_entry->resource_address,
  1392. sizeof(cfg_entry->resource_address));
  1393. if (!rc) {
  1394. new_entry = 0;
  1395. break;
  1396. }
  1397. }
  1398. if (new_entry) {
  1399. if (hidden_entry) {
  1400. spin_unlock_irqrestore(&pinstance->resource_lock,
  1401. lock_flags);
  1402. goto out_notify_apps;
  1403. }
  1404. /* If there are more number of resources than what driver can
  1405. * manage, do not notify the applications about the CCN. Just
  1406. * ignore this notifications and re-register the same HCAM
  1407. */
  1408. if (list_empty(&pinstance->free_res_q)) {
  1409. spin_unlock_irqrestore(&pinstance->resource_lock,
  1410. lock_flags);
  1411. pmcraid_err("too many resources attached\n");
  1412. spin_lock_irqsave(pinstance->host->host_lock,
  1413. host_lock_flags);
  1414. pmcraid_send_hcam(pinstance,
  1415. PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1416. spin_unlock_irqrestore(pinstance->host->host_lock,
  1417. host_lock_flags);
  1418. return;
  1419. }
  1420. res = list_entry(pinstance->free_res_q.next,
  1421. struct pmcraid_resource_entry, queue);
  1422. list_del(&res->queue);
  1423. res->scsi_dev = NULL;
  1424. res->reset_progress = 0;
  1425. list_add_tail(&res->queue, &pinstance->used_res_q);
  1426. }
  1427. memcpy(&res->cfg_entry, cfg_entry, pinstance->config_table_entry_size);
  1428. if (pinstance->ccn.hcam->notification_type ==
  1429. NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
  1430. if (res->scsi_dev) {
  1431. if (fw_version <= PMCRAID_FW_VERSION_1)
  1432. res->cfg_entry.unique_flags1 &= 0x7F;
  1433. else
  1434. res->cfg_entry.array_id &= cpu_to_le16(0xFF);
  1435. res->change_detected = RES_CHANGE_DEL;
  1436. res->cfg_entry.resource_handle =
  1437. PMCRAID_INVALID_RES_HANDLE;
  1438. schedule_work(&pinstance->worker_q);
  1439. } else {
  1440. /* This may be one of the non-exposed resources */
  1441. list_move_tail(&res->queue, &pinstance->free_res_q);
  1442. }
  1443. } else if (!res->scsi_dev) {
  1444. res->change_detected = RES_CHANGE_ADD;
  1445. schedule_work(&pinstance->worker_q);
  1446. }
  1447. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  1448. out_notify_apps:
  1449. /* Notify configuration changes to registered applications.*/
  1450. if (!pmcraid_disable_aen)
  1451. pmcraid_notify_ccn(pinstance);
  1452. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1453. if (cmd)
  1454. pmcraid_send_hcam_cmd(cmd);
  1455. }
  1456. /**
  1457. * pmcraid_get_error_info - return error string for an ioasc
  1458. * @ioasc: ioasc code
  1459. * Return Value
  1460. * none
  1461. */
  1462. static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
  1463. {
  1464. int i;
  1465. for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
  1466. if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
  1467. return &pmcraid_ioasc_error_table[i];
  1468. }
  1469. return NULL;
  1470. }
  1471. /**
  1472. * pmcraid_ioasc_logger - log IOASC information based user-settings
  1473. * @ioasc: ioasc code
  1474. * @cmd: pointer to command that resulted in 'ioasc'
  1475. */
  1476. static void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
  1477. {
  1478. struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
  1479. if (error_info == NULL ||
  1480. cmd->drv_inst->current_log_level < error_info->log_level)
  1481. return;
  1482. /* log the error string */
  1483. pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
  1484. cmd->ioa_cb->ioarcb.cdb[0],
  1485. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  1486. ioasc, error_info->error_string);
  1487. }
  1488. /**
  1489. * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
  1490. *
  1491. * @pinstance: pointer to per adapter instance structure
  1492. *
  1493. * Return value:
  1494. * none
  1495. */
  1496. static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
  1497. {
  1498. struct pmcraid_hcam_ldn *hcam_ldn;
  1499. u32 ioasc;
  1500. hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1501. pmcraid_info
  1502. ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
  1503. pinstance->ldn.hcam->ilid,
  1504. pinstance->ldn.hcam->op_code,
  1505. pinstance->ldn.hcam->notification_type,
  1506. pinstance->ldn.hcam->notification_lost,
  1507. pinstance->ldn.hcam->flags,
  1508. pinstance->ldn.hcam->overlay_id);
  1509. /* log only the errors, no need to log informational log entries */
  1510. if (pinstance->ldn.hcam->notification_type !=
  1511. NOTIFICATION_TYPE_ERROR_LOG)
  1512. return;
  1513. if (pinstance->ldn.hcam->notification_lost ==
  1514. HOSTRCB_NOTIFICATIONS_LOST)
  1515. dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
  1516. ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
  1517. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  1518. ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
  1519. dev_info(&pinstance->pdev->dev,
  1520. "UnitAttention due to IOA Bus Reset\n");
  1521. scsi_report_bus_reset(
  1522. pinstance->host,
  1523. RES_BUS(hcam_ldn->error_log.fd_ra));
  1524. }
  1525. return;
  1526. }
  1527. /**
  1528. * pmcraid_process_ccn - Op done function for a CCN.
  1529. * @cmd: pointer to command struct
  1530. *
  1531. * This function is the op done function for a configuration
  1532. * change notification
  1533. *
  1534. * Return value:
  1535. * none
  1536. */
  1537. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
  1538. {
  1539. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1540. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1541. unsigned long lock_flags;
  1542. pinstance->ccn.cmd = NULL;
  1543. pmcraid_return_cmd(cmd);
  1544. /* If driver initiated IOA reset happened while this hcam was pending
  1545. * with IOA, or IOA bringdown sequence is in progress, no need to
  1546. * re-register the hcam
  1547. */
  1548. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1549. atomic_read(&pinstance->ccn.ignore) == 1) {
  1550. return;
  1551. } else if (ioasc) {
  1552. dev_info(&pinstance->pdev->dev,
  1553. "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
  1554. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1555. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1556. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1557. } else {
  1558. pmcraid_handle_config_change(pinstance);
  1559. }
  1560. }
  1561. static void pmcraid_initiate_reset(struct pmcraid_instance *);
  1562. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
  1563. /**
  1564. * pmcraid_process_ldn - op done function for an LDN
  1565. * @cmd: pointer to command block
  1566. *
  1567. * Return value
  1568. * none
  1569. */
  1570. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
  1571. {
  1572. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1573. struct pmcraid_hcam_ldn *ldn_hcam =
  1574. (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1575. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1576. u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
  1577. unsigned long lock_flags;
  1578. /* return the command block back to freepool */
  1579. pinstance->ldn.cmd = NULL;
  1580. pmcraid_return_cmd(cmd);
  1581. /* If driver initiated IOA reset happened while this hcam was pending
  1582. * with IOA, no need to re-register the hcam as reset engine will do it
  1583. * once reset sequence is complete
  1584. */
  1585. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1586. atomic_read(&pinstance->ccn.ignore) == 1) {
  1587. return;
  1588. } else if (!ioasc) {
  1589. pmcraid_handle_error_log(pinstance);
  1590. if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
  1591. spin_lock_irqsave(pinstance->host->host_lock,
  1592. lock_flags);
  1593. pmcraid_initiate_reset(pinstance);
  1594. spin_unlock_irqrestore(pinstance->host->host_lock,
  1595. lock_flags);
  1596. return;
  1597. }
  1598. if (fd_ioasc == PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC) {
  1599. pinstance->timestamp_error = 1;
  1600. pmcraid_set_timestamp(cmd);
  1601. }
  1602. } else {
  1603. dev_info(&pinstance->pdev->dev,
  1604. "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
  1605. }
  1606. /* send netlink message for HCAM notification if enabled */
  1607. if (!pmcraid_disable_aen)
  1608. pmcraid_notify_ldn(pinstance);
  1609. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1610. if (cmd)
  1611. pmcraid_send_hcam_cmd(cmd);
  1612. }
  1613. /**
  1614. * pmcraid_register_hcams - register HCAMs for CCN and LDN
  1615. *
  1616. * @pinstance: pointer per adapter instance structure
  1617. *
  1618. * Return Value
  1619. * none
  1620. */
  1621. static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
  1622. {
  1623. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1624. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1625. }
  1626. /**
  1627. * pmcraid_unregister_hcams - cancel HCAMs registered already
  1628. * @cmd: pointer to command used as part of reset sequence
  1629. */
  1630. static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
  1631. {
  1632. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1633. /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
  1634. * handling hcam response though it is not necessary. In order to
  1635. * prevent this, set 'ignore', so that bring-down sequence doesn't
  1636. * re-send any more hcams
  1637. */
  1638. atomic_set(&pinstance->ccn.ignore, 1);
  1639. atomic_set(&pinstance->ldn.ignore, 1);
  1640. /* If adapter reset was forced as part of runtime reset sequence,
  1641. * start the reset sequence. Reset will be triggered even in case
  1642. * IOA unit_check.
  1643. */
  1644. if ((pinstance->force_ioa_reset && !pinstance->ioa_bringdown) ||
  1645. pinstance->ioa_unit_check) {
  1646. pinstance->force_ioa_reset = 0;
  1647. pinstance->ioa_unit_check = 0;
  1648. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1649. pmcraid_reset_alert(cmd);
  1650. return;
  1651. }
  1652. /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
  1653. * one after the other. So CCN cancellation will be triggered by
  1654. * pmcraid_cancel_ldn itself.
  1655. */
  1656. pmcraid_cancel_ldn(cmd);
  1657. }
  1658. static void pmcraid_reinit_buffers(struct pmcraid_instance *);
  1659. /**
  1660. * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
  1661. * @pinstance: pointer to adapter instance structure
  1662. * Return Value
  1663. * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
  1664. */
  1665. static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
  1666. {
  1667. u32 intrs;
  1668. pmcraid_reinit_buffers(pinstance);
  1669. intrs = pmcraid_read_interrupts(pinstance);
  1670. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  1671. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  1672. if (!pinstance->interrupt_mode) {
  1673. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1674. pinstance->int_regs.
  1675. ioa_host_interrupt_mask_reg);
  1676. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1677. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  1678. }
  1679. return 1;
  1680. } else {
  1681. return 0;
  1682. }
  1683. }
  1684. /**
  1685. * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
  1686. * @cmd : pointer to reset command block
  1687. *
  1688. * Return Value
  1689. * none
  1690. */
  1691. static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
  1692. {
  1693. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1694. u32 int_reg;
  1695. u32 doorbell;
  1696. /* There will be an interrupt when Transition to Operational bit is
  1697. * set so tasklet would execute next reset task. The timeout handler
  1698. * would re-initiate a reset
  1699. */
  1700. cmd->cmd_done = pmcraid_ioa_reset;
  1701. cmd->timer.expires = jiffies +
  1702. msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
  1703. cmd->timer.function = pmcraid_timeout_handler;
  1704. if (!timer_pending(&cmd->timer))
  1705. add_timer(&cmd->timer);
  1706. /* Enable destructive diagnostics on IOA if it is not yet in
  1707. * operational state
  1708. */
  1709. doorbell = DOORBELL_RUNTIME_RESET |
  1710. DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
  1711. /* Since we do RESET_ALERT and Start BIST we have to again write
  1712. * MSIX Doorbell to indicate the interrupt mode
  1713. */
  1714. if (pinstance->interrupt_mode) {
  1715. iowrite32(DOORBELL_INTR_MODE_MSIX,
  1716. pinstance->int_regs.host_ioa_interrupt_reg);
  1717. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  1718. }
  1719. iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
  1720. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1721. int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  1722. pmcraid_info("Waiting for IOA to become operational %x:%x\n",
  1723. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1724. int_reg);
  1725. }
  1726. /**
  1727. * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
  1728. *
  1729. * @pinstance: pointer to adapter instance structure
  1730. *
  1731. * Return Value
  1732. * none
  1733. */
  1734. static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
  1735. {
  1736. pmcraid_info("%s is not yet implemented\n", __func__);
  1737. }
  1738. /**
  1739. * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
  1740. * @pinstance: pointer to adapter instance structure
  1741. *
  1742. * This function fails all outstanding ops. If they are submitted to IOA
  1743. * already, it sends cancel all messages if IOA is still accepting IOARCBs,
  1744. * otherwise just completes the commands and returns the cmd blocks to free
  1745. * pool.
  1746. *
  1747. * Return value:
  1748. * none
  1749. */
  1750. static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
  1751. {
  1752. struct pmcraid_cmd *cmd, *temp;
  1753. unsigned long lock_flags;
  1754. /* pending command list is protected by pending_pool_lock. Its
  1755. * traversal must be done as within this lock
  1756. */
  1757. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1758. list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
  1759. free_list) {
  1760. list_del(&cmd->free_list);
  1761. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  1762. lock_flags);
  1763. cmd->ioa_cb->ioasa.ioasc =
  1764. cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
  1765. cmd->ioa_cb->ioasa.ilid =
  1766. cpu_to_le32(PMCRAID_DRIVER_ILID);
  1767. /* In case the command timer is still running */
  1768. del_timer(&cmd->timer);
  1769. /* If this is an IO command, complete it by invoking scsi_done
  1770. * function. If this is one of the internal commands other
  1771. * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
  1772. * complete it
  1773. */
  1774. if (cmd->scsi_cmd) {
  1775. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1776. __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
  1777. scsi_cmd->result |= DID_ERROR << 16;
  1778. scsi_dma_unmap(scsi_cmd);
  1779. pmcraid_return_cmd(cmd);
  1780. pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
  1781. le32_to_cpu(resp) >> 2,
  1782. cmd->ioa_cb->ioarcb.cdb[0],
  1783. scsi_cmd->result);
  1784. scsi_done(scsi_cmd);
  1785. } else if (cmd->cmd_done == pmcraid_internal_done ||
  1786. cmd->cmd_done == pmcraid_erp_done) {
  1787. cmd->cmd_done(cmd);
  1788. } else if (cmd->cmd_done != pmcraid_ioa_reset &&
  1789. cmd->cmd_done != pmcraid_ioa_shutdown_done) {
  1790. pmcraid_return_cmd(cmd);
  1791. }
  1792. atomic_dec(&pinstance->outstanding_cmds);
  1793. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1794. }
  1795. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  1796. }
  1797. /**
  1798. * pmcraid_ioa_reset - Implementation of IOA reset logic
  1799. *
  1800. * @cmd: pointer to the cmd block to be used for entire reset process
  1801. *
  1802. * This function executes most of the steps required for IOA reset. This gets
  1803. * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
  1804. * 'eh_' thread. Access to variables used for controlling the reset sequence is
  1805. * synchronized using host lock. Various functions called during reset process
  1806. * would make use of a single command block, pointer to which is also stored in
  1807. * adapter instance structure.
  1808. *
  1809. * Return Value
  1810. * None
  1811. */
  1812. static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
  1813. {
  1814. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1815. u8 reset_complete = 0;
  1816. pinstance->ioa_reset_in_progress = 1;
  1817. if (pinstance->reset_cmd != cmd) {
  1818. pmcraid_err("reset is called with different command block\n");
  1819. pinstance->reset_cmd = cmd;
  1820. }
  1821. pmcraid_info("reset_engine: state = %d, command = %p\n",
  1822. pinstance->ioa_state, cmd);
  1823. switch (pinstance->ioa_state) {
  1824. case IOA_STATE_DEAD:
  1825. /* If IOA is offline, whatever may be the reset reason, just
  1826. * return. callers might be waiting on the reset wait_q, wake
  1827. * up them
  1828. */
  1829. pmcraid_err("IOA is offline no reset is possible\n");
  1830. reset_complete = 1;
  1831. break;
  1832. case IOA_STATE_IN_BRINGDOWN:
  1833. /* we enter here, once ioa shutdown command is processed by IOA
  1834. * Alert IOA for a possible reset. If reset alert fails, IOA
  1835. * goes through hard-reset
  1836. */
  1837. pmcraid_disable_interrupts(pinstance, ~0);
  1838. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1839. pmcraid_reset_alert(cmd);
  1840. break;
  1841. case IOA_STATE_UNKNOWN:
  1842. /* We may be called during probe or resume. Some pre-processing
  1843. * is required for prior to reset
  1844. */
  1845. scsi_block_requests(pinstance->host);
  1846. /* If asked to reset while IOA was processing responses or
  1847. * there are any error responses then IOA may require
  1848. * hard-reset.
  1849. */
  1850. if (pinstance->ioa_hard_reset == 0) {
  1851. if (ioread32(pinstance->ioa_status) &
  1852. INTRS_TRANSITION_TO_OPERATIONAL) {
  1853. pmcraid_info("sticky bit set, bring-up\n");
  1854. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1855. pmcraid_reinit_cmdblk(cmd);
  1856. pmcraid_identify_hrrq(cmd);
  1857. } else {
  1858. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1859. pmcraid_soft_reset(cmd);
  1860. }
  1861. } else {
  1862. /* Alert IOA of a possible reset and wait for critical
  1863. * operation in progress bit to reset
  1864. */
  1865. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1866. pmcraid_reset_alert(cmd);
  1867. }
  1868. break;
  1869. case IOA_STATE_IN_RESET_ALERT:
  1870. /* If critical operation in progress bit is reset or wait gets
  1871. * timed out, reset proceeds with starting BIST on the IOA.
  1872. * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
  1873. * they are 3 or more, reset engine marks IOA dead and returns
  1874. */
  1875. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  1876. pmcraid_start_bist(cmd);
  1877. break;
  1878. case IOA_STATE_IN_HARD_RESET:
  1879. pinstance->ioa_reset_attempts++;
  1880. /* retry reset if we haven't reached maximum allowed limit */
  1881. if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
  1882. pinstance->ioa_reset_attempts = 0;
  1883. pmcraid_err("IOA didn't respond marking it as dead\n");
  1884. pinstance->ioa_state = IOA_STATE_DEAD;
  1885. if (pinstance->ioa_bringdown)
  1886. pmcraid_notify_ioastate(pinstance,
  1887. PMC_DEVICE_EVENT_SHUTDOWN_FAILED);
  1888. else
  1889. pmcraid_notify_ioastate(pinstance,
  1890. PMC_DEVICE_EVENT_RESET_FAILED);
  1891. reset_complete = 1;
  1892. break;
  1893. }
  1894. /* Once either bist or pci reset is done, restore PCI config
  1895. * space. If this fails, proceed with hard reset again
  1896. */
  1897. pci_restore_state(pinstance->pdev);
  1898. /* fail all pending commands */
  1899. pmcraid_fail_outstanding_cmds(pinstance);
  1900. /* check if unit check is active, if so extract dump */
  1901. if (pinstance->ioa_unit_check) {
  1902. pmcraid_info("unit check is active\n");
  1903. pinstance->ioa_unit_check = 0;
  1904. pmcraid_get_dump(pinstance);
  1905. pinstance->ioa_reset_attempts--;
  1906. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1907. pmcraid_reset_alert(cmd);
  1908. break;
  1909. }
  1910. /* if the reset reason is to bring-down the ioa, we might be
  1911. * done with the reset restore pci_config_space and complete
  1912. * the reset
  1913. */
  1914. if (pinstance->ioa_bringdown) {
  1915. pmcraid_info("bringing down the adapter\n");
  1916. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1917. pinstance->ioa_bringdown = 0;
  1918. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  1919. pmcraid_notify_ioastate(pinstance,
  1920. PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS);
  1921. reset_complete = 1;
  1922. } else {
  1923. /* bring-up IOA, so proceed with soft reset
  1924. * Reinitialize hrrq_buffers and their indices also
  1925. * enable interrupts after a pci_restore_state
  1926. */
  1927. if (pmcraid_reset_enable_ioa(pinstance)) {
  1928. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1929. pmcraid_info("bringing up the adapter\n");
  1930. pmcraid_reinit_cmdblk(cmd);
  1931. pmcraid_identify_hrrq(cmd);
  1932. } else {
  1933. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1934. pmcraid_soft_reset(cmd);
  1935. }
  1936. }
  1937. break;
  1938. case IOA_STATE_IN_SOFT_RESET:
  1939. /* TRANSITION TO OPERATIONAL is on so start initialization
  1940. * sequence
  1941. */
  1942. pmcraid_info("In softreset proceeding with bring-up\n");
  1943. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1944. /* Initialization commands start with HRRQ identification. From
  1945. * now on tasklet completes most of the commands as IOA is up
  1946. * and intrs are enabled
  1947. */
  1948. pmcraid_identify_hrrq(cmd);
  1949. break;
  1950. case IOA_STATE_IN_BRINGUP:
  1951. /* we are done with bringing up of IOA, change the ioa_state to
  1952. * operational and wake up any waiters
  1953. */
  1954. pinstance->ioa_state = IOA_STATE_OPERATIONAL;
  1955. reset_complete = 1;
  1956. break;
  1957. case IOA_STATE_OPERATIONAL:
  1958. default:
  1959. /* When IOA is operational and a reset is requested, check for
  1960. * the reset reason. If reset is to bring down IOA, unregister
  1961. * HCAMs and initiate shutdown; if adapter reset is forced then
  1962. * restart reset sequence again
  1963. */
  1964. if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
  1965. pinstance->force_ioa_reset == 0) {
  1966. pmcraid_notify_ioastate(pinstance,
  1967. PMC_DEVICE_EVENT_RESET_SUCCESS);
  1968. reset_complete = 1;
  1969. } else {
  1970. if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
  1971. pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
  1972. pmcraid_reinit_cmdblk(cmd);
  1973. pmcraid_unregister_hcams(cmd);
  1974. }
  1975. break;
  1976. }
  1977. /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
  1978. * OPERATIONAL. Reset all control variables used during reset, wake up
  1979. * any waiting threads and let the SCSI mid-layer send commands. Note
  1980. * that host_lock must be held before invoking scsi_report_bus_reset.
  1981. */
  1982. if (reset_complete) {
  1983. pinstance->ioa_reset_in_progress = 0;
  1984. pinstance->ioa_reset_attempts = 0;
  1985. pinstance->reset_cmd = NULL;
  1986. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1987. pinstance->ioa_bringdown = 0;
  1988. pmcraid_return_cmd(cmd);
  1989. /* If target state is to bring up the adapter, proceed with
  1990. * hcam registration and resource exposure to mid-layer.
  1991. */
  1992. if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
  1993. pmcraid_register_hcams(pinstance);
  1994. wake_up_all(&pinstance->reset_wait_q);
  1995. }
  1996. return;
  1997. }
  1998. /**
  1999. * pmcraid_initiate_reset - initiates reset sequence. This is called from
  2000. * ISR/tasklet during error interrupts including IOA unit check. If reset
  2001. * is already in progress, it just returns, otherwise initiates IOA reset
  2002. * to bring IOA up to operational state.
  2003. *
  2004. * @pinstance: pointer to adapter instance structure
  2005. *
  2006. * Return value
  2007. * none
  2008. */
  2009. static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
  2010. {
  2011. struct pmcraid_cmd *cmd;
  2012. /* If the reset is already in progress, just return, otherwise start
  2013. * reset sequence and return
  2014. */
  2015. if (!pinstance->ioa_reset_in_progress) {
  2016. scsi_block_requests(pinstance->host);
  2017. cmd = pmcraid_get_free_cmd(pinstance);
  2018. if (cmd == NULL) {
  2019. pmcraid_err("no cmnd blocks for initiate_reset\n");
  2020. return;
  2021. }
  2022. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2023. pinstance->reset_cmd = cmd;
  2024. pinstance->force_ioa_reset = 1;
  2025. pmcraid_notify_ioastate(pinstance,
  2026. PMC_DEVICE_EVENT_RESET_START);
  2027. pmcraid_ioa_reset(cmd);
  2028. }
  2029. }
  2030. /**
  2031. * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
  2032. * or bringdown IOA
  2033. * @pinstance: pointer adapter instance structure
  2034. * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
  2035. * @target_state: expected target state after reset
  2036. *
  2037. * Note: This command initiates reset and waits for its completion. Hence this
  2038. * should not be called from isr/timer/tasklet functions (timeout handlers,
  2039. * error response handlers and interrupt handlers).
  2040. *
  2041. * Return Value
  2042. * 1 in case ioa_state is not target_state, 0 otherwise.
  2043. */
  2044. static int pmcraid_reset_reload(
  2045. struct pmcraid_instance *pinstance,
  2046. u8 shutdown_type,
  2047. u8 target_state
  2048. )
  2049. {
  2050. struct pmcraid_cmd *reset_cmd = NULL;
  2051. unsigned long lock_flags;
  2052. int reset = 1;
  2053. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2054. if (pinstance->ioa_reset_in_progress) {
  2055. pmcraid_info("reset_reload: reset is already in progress\n");
  2056. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2057. wait_event(pinstance->reset_wait_q,
  2058. !pinstance->ioa_reset_in_progress);
  2059. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2060. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2061. pmcraid_info("reset_reload: IOA is dead\n");
  2062. goto out_unlock;
  2063. }
  2064. if (pinstance->ioa_state == target_state) {
  2065. reset = 0;
  2066. goto out_unlock;
  2067. }
  2068. }
  2069. pmcraid_info("reset_reload: proceeding with reset\n");
  2070. scsi_block_requests(pinstance->host);
  2071. reset_cmd = pmcraid_get_free_cmd(pinstance);
  2072. if (reset_cmd == NULL) {
  2073. pmcraid_err("no free cmnd for reset_reload\n");
  2074. goto out_unlock;
  2075. }
  2076. if (shutdown_type == SHUTDOWN_NORMAL)
  2077. pinstance->ioa_bringdown = 1;
  2078. pinstance->ioa_shutdown_type = shutdown_type;
  2079. pinstance->reset_cmd = reset_cmd;
  2080. pinstance->force_ioa_reset = reset;
  2081. pmcraid_info("reset_reload: initiating reset\n");
  2082. pmcraid_ioa_reset(reset_cmd);
  2083. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2084. pmcraid_info("reset_reload: waiting for reset to complete\n");
  2085. wait_event(pinstance->reset_wait_q,
  2086. !pinstance->ioa_reset_in_progress);
  2087. pmcraid_info("reset_reload: reset is complete !!\n");
  2088. scsi_unblock_requests(pinstance->host);
  2089. return pinstance->ioa_state != target_state;
  2090. out_unlock:
  2091. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2092. return reset;
  2093. }
  2094. /**
  2095. * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
  2096. *
  2097. * @pinstance: pointer to adapter instance structure
  2098. *
  2099. * Return Value
  2100. * whatever is returned from pmcraid_reset_reload
  2101. */
  2102. static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
  2103. {
  2104. return pmcraid_reset_reload(pinstance,
  2105. SHUTDOWN_NORMAL,
  2106. IOA_STATE_UNKNOWN);
  2107. }
  2108. /**
  2109. * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
  2110. *
  2111. * @pinstance: pointer to adapter instance structure
  2112. *
  2113. * Return Value
  2114. * whatever is returned from pmcraid_reset_reload
  2115. */
  2116. static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
  2117. {
  2118. pmcraid_notify_ioastate(pinstance, PMC_DEVICE_EVENT_RESET_START);
  2119. return pmcraid_reset_reload(pinstance,
  2120. SHUTDOWN_NONE,
  2121. IOA_STATE_OPERATIONAL);
  2122. }
  2123. /**
  2124. * pmcraid_request_sense - Send request sense to a device
  2125. * @cmd: pmcraid command struct
  2126. *
  2127. * This function sends a request sense to a device as a result of a check
  2128. * condition. This method re-uses the same command block that failed earlier.
  2129. */
  2130. static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
  2131. {
  2132. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2133. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2134. struct device *dev = &cmd->drv_inst->pdev->dev;
  2135. cmd->sense_buffer = cmd->scsi_cmd->sense_buffer;
  2136. cmd->sense_buffer_dma = dma_map_single(dev, cmd->sense_buffer,
  2137. SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  2138. if (dma_mapping_error(dev, cmd->sense_buffer_dma)) {
  2139. pmcraid_err
  2140. ("couldn't allocate sense buffer for request sense\n");
  2141. pmcraid_erp_done(cmd);
  2142. return;
  2143. }
  2144. /* re-use the command block */
  2145. memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
  2146. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2147. ioarcb->request_flags0 = (SYNC_COMPLETE |
  2148. NO_LINK_DESCS |
  2149. INHIBIT_UL_CHECK);
  2150. ioarcb->request_type = REQ_TYPE_SCSI;
  2151. ioarcb->cdb[0] = REQUEST_SENSE;
  2152. ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2153. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2154. offsetof(struct pmcraid_ioarcb,
  2155. add_data.u.ioadl[0]));
  2156. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  2157. ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2158. ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
  2159. ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2160. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  2161. /* request sense might be called as part of error response processing
  2162. * which runs in tasklets context. It is possible that mid-layer might
  2163. * schedule queuecommand during this time, hence, writting to IOARRIN
  2164. * must be protect by host_lock
  2165. */
  2166. pmcraid_send_cmd(cmd, pmcraid_erp_done,
  2167. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2168. pmcraid_timeout_handler);
  2169. }
  2170. /**
  2171. * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
  2172. * @cmd: command that failed
  2173. * @need_sense: true if request_sense is required after cancel all
  2174. *
  2175. * This function sends a cancel all to a device to clear the queue.
  2176. */
  2177. static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, bool need_sense)
  2178. {
  2179. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2180. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2181. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2182. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2183. ioarcb->request_flags0 = SYNC_OVERRIDE;
  2184. ioarcb->request_type = REQ_TYPE_IOACMD;
  2185. ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
  2186. if (RES_IS_GSCSI(res->cfg_entry))
  2187. ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
  2188. ioarcb->ioadl_bus_addr = 0;
  2189. ioarcb->ioadl_length = 0;
  2190. ioarcb->data_transfer_length = 0;
  2191. ioarcb->ioarcb_bus_addr &= cpu_to_le64((~0x1FULL));
  2192. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  2193. * schedule queuecommand while we are doing this
  2194. */
  2195. pmcraid_send_cmd(cmd, need_sense ?
  2196. pmcraid_erp_done : pmcraid_request_sense,
  2197. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2198. pmcraid_timeout_handler);
  2199. }
  2200. /**
  2201. * pmcraid_frame_auto_sense: frame fixed format sense information
  2202. *
  2203. * @cmd: pointer to failing command block
  2204. *
  2205. * Return value
  2206. * none
  2207. */
  2208. static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
  2209. {
  2210. u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
  2211. struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
  2212. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2213. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2214. u32 failing_lba = 0;
  2215. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  2216. cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  2217. if (RES_IS_VSET(res->cfg_entry) &&
  2218. ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
  2219. ioasa->u.vset.failing_lba_hi != 0) {
  2220. sense_buf[0] = 0x72;
  2221. sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2222. sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2223. sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2224. sense_buf[7] = 12;
  2225. sense_buf[8] = 0;
  2226. sense_buf[9] = 0x0A;
  2227. sense_buf[10] = 0x80;
  2228. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
  2229. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  2230. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  2231. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  2232. sense_buf[15] = failing_lba & 0x000000ff;
  2233. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
  2234. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  2235. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  2236. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  2237. sense_buf[19] = failing_lba & 0x000000ff;
  2238. } else {
  2239. sense_buf[0] = 0x70;
  2240. sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2241. sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2242. sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2243. if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
  2244. if (RES_IS_VSET(res->cfg_entry))
  2245. failing_lba =
  2246. le32_to_cpu(ioasa->u.
  2247. vset.failing_lba_lo);
  2248. sense_buf[0] |= 0x80;
  2249. sense_buf[3] = (failing_lba >> 24) & 0xff;
  2250. sense_buf[4] = (failing_lba >> 16) & 0xff;
  2251. sense_buf[5] = (failing_lba >> 8) & 0xff;
  2252. sense_buf[6] = failing_lba & 0xff;
  2253. }
  2254. sense_buf[7] = 6; /* additional length */
  2255. }
  2256. }
  2257. /**
  2258. * pmcraid_error_handler - Error response handlers for a SCSI op
  2259. * @cmd: pointer to pmcraid_cmd that has failed
  2260. *
  2261. * This function determines whether or not to initiate ERP on the affected
  2262. * device. This is called from a tasklet, which doesn't hold any locks.
  2263. *
  2264. * Return value:
  2265. * 0 it caller can complete the request, otherwise 1 where in error
  2266. * handler itself completes the request and returns the command block
  2267. * back to free-pool
  2268. */
  2269. static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
  2270. {
  2271. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2272. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2273. struct pmcraid_instance *pinstance = cmd->drv_inst;
  2274. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2275. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2276. u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
  2277. bool sense_copied = false;
  2278. if (!res) {
  2279. pmcraid_info("resource pointer is NULL\n");
  2280. return 0;
  2281. }
  2282. /* If this was a SCSI read/write command keep count of errors */
  2283. if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
  2284. atomic_inc(&res->read_failures);
  2285. else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
  2286. atomic_inc(&res->write_failures);
  2287. if (!RES_IS_GSCSI(res->cfg_entry) &&
  2288. masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
  2289. pmcraid_frame_auto_sense(cmd);
  2290. }
  2291. /* Log IOASC/IOASA information based on user settings */
  2292. pmcraid_ioasc_logger(ioasc, cmd);
  2293. switch (masked_ioasc) {
  2294. case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
  2295. scsi_cmd->result |= (DID_ABORT << 16);
  2296. break;
  2297. case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
  2298. case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
  2299. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  2300. break;
  2301. case PMCRAID_IOASC_NR_SYNC_REQUIRED:
  2302. res->sync_reqd = 1;
  2303. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  2304. break;
  2305. case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
  2306. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  2307. break;
  2308. case PMCRAID_IOASC_UA_BUS_WAS_RESET:
  2309. case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
  2310. if (!res->reset_progress)
  2311. scsi_report_bus_reset(pinstance->host,
  2312. scsi_cmd->device->channel);
  2313. scsi_cmd->result |= (DID_ERROR << 16);
  2314. break;
  2315. case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
  2316. scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
  2317. res->sync_reqd = 1;
  2318. /* if check_condition is not active return with error otherwise
  2319. * get/frame the sense buffer
  2320. */
  2321. if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
  2322. SAM_STAT_CHECK_CONDITION &&
  2323. PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
  2324. return 0;
  2325. /* If we have auto sense data as part of IOASA pass it to
  2326. * mid-layer
  2327. */
  2328. if (ioasa->auto_sense_length != 0) {
  2329. short sense_len = le16_to_cpu(ioasa->auto_sense_length);
  2330. int data_size = min_t(u16, sense_len,
  2331. SCSI_SENSE_BUFFERSIZE);
  2332. memcpy(scsi_cmd->sense_buffer,
  2333. ioasa->sense_data,
  2334. data_size);
  2335. sense_copied = true;
  2336. }
  2337. if (RES_IS_GSCSI(res->cfg_entry))
  2338. pmcraid_cancel_all(cmd, sense_copied);
  2339. else if (sense_copied)
  2340. pmcraid_erp_done(cmd);
  2341. else
  2342. pmcraid_request_sense(cmd);
  2343. return 1;
  2344. case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
  2345. break;
  2346. default:
  2347. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  2348. scsi_cmd->result |= (DID_ERROR << 16);
  2349. break;
  2350. }
  2351. return 0;
  2352. }
  2353. /**
  2354. * pmcraid_reset_device - device reset handler functions
  2355. *
  2356. * @scsi_cmd: scsi command struct
  2357. * @timeout: command timeout
  2358. * @modifier: reset modifier indicating the reset sequence to be performed
  2359. *
  2360. * This function issues a device reset to the affected device.
  2361. * A LUN reset will be sent to the device first. If that does
  2362. * not work, a target reset will be sent.
  2363. *
  2364. * Return value:
  2365. * SUCCESS / FAILED
  2366. */
  2367. static int pmcraid_reset_device(
  2368. struct scsi_cmnd *scsi_cmd,
  2369. unsigned long timeout,
  2370. u8 modifier)
  2371. {
  2372. struct pmcraid_cmd *cmd;
  2373. struct pmcraid_instance *pinstance;
  2374. struct pmcraid_resource_entry *res;
  2375. struct pmcraid_ioarcb *ioarcb;
  2376. unsigned long lock_flags;
  2377. u32 ioasc;
  2378. pinstance =
  2379. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2380. res = scsi_cmd->device->hostdata;
  2381. if (!res) {
  2382. sdev_printk(KERN_ERR, scsi_cmd->device,
  2383. "reset_device: NULL resource pointer\n");
  2384. return FAILED;
  2385. }
  2386. /* If adapter is currently going through reset/reload, return failed.
  2387. * This will force the mid-layer to call _eh_bus/host reset, which
  2388. * will then go to sleep and wait for the reset to complete
  2389. */
  2390. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2391. if (pinstance->ioa_reset_in_progress ||
  2392. pinstance->ioa_state == IOA_STATE_DEAD) {
  2393. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2394. return FAILED;
  2395. }
  2396. res->reset_progress = 1;
  2397. pmcraid_info("Resetting %s resource with addr %x\n",
  2398. ((modifier & RESET_DEVICE_LUN) ? "LUN" :
  2399. ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
  2400. le32_to_cpu(res->cfg_entry.resource_address));
  2401. /* get a free cmd block */
  2402. cmd = pmcraid_get_free_cmd(pinstance);
  2403. if (cmd == NULL) {
  2404. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2405. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2406. return FAILED;
  2407. }
  2408. ioarcb = &cmd->ioa_cb->ioarcb;
  2409. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2410. ioarcb->request_type = REQ_TYPE_IOACMD;
  2411. ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
  2412. /* Initialize reset modifier bits */
  2413. if (modifier)
  2414. modifier = ENABLE_RESET_MODIFIER | modifier;
  2415. ioarcb->cdb[1] = modifier;
  2416. init_completion(&cmd->wait_for_completion);
  2417. cmd->completion_req = 1;
  2418. pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
  2419. cmd->ioa_cb->ioarcb.cdb[0],
  2420. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  2421. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2422. pmcraid_send_cmd(cmd,
  2423. pmcraid_internal_done,
  2424. timeout,
  2425. pmcraid_timeout_handler);
  2426. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2427. /* RESET_DEVICE command completes after all pending IOARCBs are
  2428. * completed. Once this command is completed, pmcraind_internal_done
  2429. * will wake up the 'completion' queue.
  2430. */
  2431. wait_for_completion(&cmd->wait_for_completion);
  2432. /* complete the command here itself and return the command block
  2433. * to free list
  2434. */
  2435. pmcraid_return_cmd(cmd);
  2436. res->reset_progress = 0;
  2437. ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2438. /* set the return value based on the returned ioasc */
  2439. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2440. }
  2441. /**
  2442. * _pmcraid_io_done - helper for pmcraid_io_done function
  2443. *
  2444. * @cmd: pointer to pmcraid command struct
  2445. * @reslen: residual data length to be set in the ioasa
  2446. * @ioasc: ioasc either returned by IOA or set by driver itself.
  2447. *
  2448. * This function is invoked by pmcraid_io_done to complete mid-layer
  2449. * scsi ops.
  2450. *
  2451. * Return value:
  2452. * 0 if caller is required to return it to free_pool. Returns 1 if
  2453. * caller need not worry about freeing command block as error handler
  2454. * will take care of that.
  2455. */
  2456. static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
  2457. {
  2458. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2459. int rc = 0;
  2460. scsi_set_resid(scsi_cmd, reslen);
  2461. pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
  2462. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2463. cmd->ioa_cb->ioarcb.cdb[0],
  2464. ioasc, scsi_cmd->result);
  2465. if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
  2466. rc = pmcraid_error_handler(cmd);
  2467. if (rc == 0) {
  2468. scsi_dma_unmap(scsi_cmd);
  2469. scsi_done(scsi_cmd);
  2470. }
  2471. return rc;
  2472. }
  2473. /**
  2474. * pmcraid_io_done - SCSI completion function
  2475. *
  2476. * @cmd: pointer to pmcraid command struct
  2477. *
  2478. * This function is invoked by tasklet/mid-layer error handler to completing
  2479. * the SCSI ops sent from mid-layer.
  2480. *
  2481. * Return value
  2482. * none
  2483. */
  2484. static void pmcraid_io_done(struct pmcraid_cmd *cmd)
  2485. {
  2486. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2487. u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
  2488. if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
  2489. pmcraid_return_cmd(cmd);
  2490. }
  2491. /**
  2492. * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
  2493. *
  2494. * @cmd: command block of the command to be aborted
  2495. *
  2496. * Return Value:
  2497. * returns pointer to command structure used as cancelling cmd
  2498. */
  2499. static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
  2500. {
  2501. struct pmcraid_cmd *cancel_cmd;
  2502. struct pmcraid_instance *pinstance;
  2503. pinstance = (struct pmcraid_instance *)cmd->drv_inst;
  2504. cancel_cmd = pmcraid_get_free_cmd(pinstance);
  2505. if (cancel_cmd == NULL) {
  2506. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2507. return NULL;
  2508. }
  2509. pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
  2510. pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
  2511. cmd->ioa_cb->ioarcb.cdb[0],
  2512. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2513. init_completion(&cancel_cmd->wait_for_completion);
  2514. cancel_cmd->completion_req = 1;
  2515. pmcraid_info("command (%d) CDB[0] = %x for %x\n",
  2516. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2517. cancel_cmd->ioa_cb->ioarcb.cdb[0],
  2518. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
  2519. pmcraid_send_cmd(cancel_cmd,
  2520. pmcraid_internal_done,
  2521. PMCRAID_INTERNAL_TIMEOUT,
  2522. pmcraid_timeout_handler);
  2523. return cancel_cmd;
  2524. }
  2525. /**
  2526. * pmcraid_abort_complete - Waits for ABORT TASK completion
  2527. *
  2528. * @cancel_cmd: command block use as cancelling command
  2529. *
  2530. * Return Value:
  2531. * returns SUCCESS if ABORT TASK has good completion
  2532. * otherwise FAILED
  2533. */
  2534. static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
  2535. {
  2536. struct pmcraid_resource_entry *res;
  2537. u32 ioasc;
  2538. wait_for_completion(&cancel_cmd->wait_for_completion);
  2539. res = cancel_cmd->res;
  2540. cancel_cmd->res = NULL;
  2541. ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
  2542. /* If the abort task is not timed out we will get a Good completion
  2543. * as sense_key, otherwise we may get one the following responses
  2544. * due to subsequent bus reset or device reset. In case IOASC is
  2545. * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
  2546. */
  2547. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  2548. ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
  2549. if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
  2550. res->sync_reqd = 1;
  2551. ioasc = 0;
  2552. }
  2553. /* complete the command here itself */
  2554. pmcraid_return_cmd(cancel_cmd);
  2555. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2556. }
  2557. /**
  2558. * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
  2559. *
  2560. * @scsi_cmd: scsi command struct given by mid-layer. When this is called
  2561. * mid-layer ensures that no other commands are queued. This
  2562. * never gets called under interrupt, but a separate eh thread.
  2563. *
  2564. * Return value:
  2565. * SUCCESS / FAILED
  2566. */
  2567. static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
  2568. {
  2569. struct pmcraid_instance *pinstance;
  2570. struct pmcraid_cmd *cmd;
  2571. struct pmcraid_resource_entry *res;
  2572. unsigned long host_lock_flags;
  2573. unsigned long pending_lock_flags;
  2574. struct pmcraid_cmd *cancel_cmd = NULL;
  2575. int cmd_found = 0;
  2576. int rc = FAILED;
  2577. pinstance =
  2578. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2579. scmd_printk(KERN_INFO, scsi_cmd,
  2580. "I/O command timed out, aborting it.\n");
  2581. res = scsi_cmd->device->hostdata;
  2582. if (res == NULL)
  2583. return rc;
  2584. /* If we are currently going through reset/reload, return failed.
  2585. * This will force the mid-layer to eventually call
  2586. * pmcraid_eh_host_reset which will then go to sleep and wait for the
  2587. * reset to complete
  2588. */
  2589. spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
  2590. if (pinstance->ioa_reset_in_progress ||
  2591. pinstance->ioa_state == IOA_STATE_DEAD) {
  2592. spin_unlock_irqrestore(pinstance->host->host_lock,
  2593. host_lock_flags);
  2594. return rc;
  2595. }
  2596. /* loop over pending cmd list to find cmd corresponding to this
  2597. * scsi_cmd. Note that this command might not have been completed
  2598. * already. locking: all pending commands are protected with
  2599. * pending_pool_lock.
  2600. */
  2601. spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
  2602. list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
  2603. if (cmd->scsi_cmd == scsi_cmd) {
  2604. cmd_found = 1;
  2605. break;
  2606. }
  2607. }
  2608. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  2609. pending_lock_flags);
  2610. /* If the command to be aborted was given to IOA and still pending with
  2611. * it, send ABORT_TASK to abort this and wait for its completion
  2612. */
  2613. if (cmd_found)
  2614. cancel_cmd = pmcraid_abort_cmd(cmd);
  2615. spin_unlock_irqrestore(pinstance->host->host_lock,
  2616. host_lock_flags);
  2617. if (cancel_cmd) {
  2618. cancel_cmd->res = cmd->scsi_cmd->device->hostdata;
  2619. rc = pmcraid_abort_complete(cancel_cmd);
  2620. }
  2621. return cmd_found ? rc : SUCCESS;
  2622. }
  2623. /**
  2624. * pmcraid_eh_device_reset_handler - bus/target/device reset handler callbacks
  2625. *
  2626. * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
  2627. *
  2628. * All these routines invokve pmcraid_reset_device with appropriate parameters.
  2629. * Since these are called from mid-layer EH thread, no other IO will be queued
  2630. * to the resource being reset. However, control path (IOCTL) may be active so
  2631. * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
  2632. * takes care by locking/unlocking host_lock.
  2633. *
  2634. * Return value
  2635. * SUCCESS or FAILED
  2636. */
  2637. static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
  2638. {
  2639. scmd_printk(KERN_INFO, scmd,
  2640. "resetting device due to an I/O command timeout.\n");
  2641. return pmcraid_reset_device(scmd,
  2642. PMCRAID_INTERNAL_TIMEOUT,
  2643. RESET_DEVICE_LUN);
  2644. }
  2645. static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
  2646. {
  2647. scmd_printk(KERN_INFO, scmd,
  2648. "Doing bus reset due to an I/O command timeout.\n");
  2649. return pmcraid_reset_device(scmd,
  2650. PMCRAID_RESET_BUS_TIMEOUT,
  2651. RESET_DEVICE_BUS);
  2652. }
  2653. static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
  2654. {
  2655. scmd_printk(KERN_INFO, scmd,
  2656. "Doing target reset due to an I/O command timeout.\n");
  2657. return pmcraid_reset_device(scmd,
  2658. PMCRAID_INTERNAL_TIMEOUT,
  2659. RESET_DEVICE_TARGET);
  2660. }
  2661. /**
  2662. * pmcraid_eh_host_reset_handler - adapter reset handler callback
  2663. *
  2664. * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
  2665. *
  2666. * Initiates adapter reset to bring it up to operational state
  2667. *
  2668. * Return value
  2669. * SUCCESS or FAILED
  2670. */
  2671. static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
  2672. {
  2673. unsigned long interval = 10000; /* 10 seconds interval */
  2674. int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
  2675. struct pmcraid_instance *pinstance =
  2676. (struct pmcraid_instance *)(scmd->device->host->hostdata);
  2677. /* wait for an additional 150 seconds just in case firmware could come
  2678. * up and if it could complete all the pending commands excluding the
  2679. * two HCAM (CCN and LDN).
  2680. */
  2681. while (waits--) {
  2682. if (atomic_read(&pinstance->outstanding_cmds) <=
  2683. PMCRAID_MAX_HCAM_CMD)
  2684. return SUCCESS;
  2685. msleep(interval);
  2686. }
  2687. dev_err(&pinstance->pdev->dev,
  2688. "Adapter being reset due to an I/O command timeout.\n");
  2689. return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
  2690. }
  2691. /**
  2692. * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
  2693. * @cmd: pmcraid command struct
  2694. * @sgcount: count of scatter-gather elements
  2695. *
  2696. * Return value
  2697. * returns pointer pmcraid_ioadl_desc, initialized to point to internal
  2698. * or external IOADLs
  2699. */
  2700. static struct pmcraid_ioadl_desc *
  2701. pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
  2702. {
  2703. struct pmcraid_ioadl_desc *ioadl;
  2704. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2705. int ioadl_count = 0;
  2706. if (ioarcb->add_cmd_param_length)
  2707. ioadl_count = DIV_ROUND_UP(le16_to_cpu(ioarcb->add_cmd_param_length), 16);
  2708. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc) * sgcount);
  2709. if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
  2710. /* external ioadls start at offset 0x80 from control_block
  2711. * structure, re-using 24 out of 27 ioadls part of IOARCB.
  2712. * It is necessary to indicate to firmware that driver is
  2713. * using ioadls to be treated as external to IOARCB.
  2714. */
  2715. ioarcb->ioarcb_bus_addr &= cpu_to_le64(~(0x1FULL));
  2716. ioarcb->ioadl_bus_addr =
  2717. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2718. offsetof(struct pmcraid_ioarcb,
  2719. add_data.u.ioadl[3]));
  2720. ioadl = &ioarcb->add_data.u.ioadl[3];
  2721. } else {
  2722. ioarcb->ioadl_bus_addr =
  2723. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2724. offsetof(struct pmcraid_ioarcb,
  2725. add_data.u.ioadl[ioadl_count]));
  2726. ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
  2727. ioarcb->ioarcb_bus_addr |=
  2728. cpu_to_le64(DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8));
  2729. }
  2730. return ioadl;
  2731. }
  2732. /**
  2733. * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
  2734. * @pinstance: pointer to adapter instance structure
  2735. * @cmd: pmcraid command struct
  2736. *
  2737. * This function is invoked by queuecommand entry point while sending a command
  2738. * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
  2739. *
  2740. * Return value:
  2741. * 0 on success or -1 on failure
  2742. */
  2743. static int pmcraid_build_ioadl(
  2744. struct pmcraid_instance *pinstance,
  2745. struct pmcraid_cmd *cmd
  2746. )
  2747. {
  2748. int i, nseg;
  2749. struct scatterlist *sglist;
  2750. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2751. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  2752. struct pmcraid_ioadl_desc *ioadl;
  2753. u32 length = scsi_bufflen(scsi_cmd);
  2754. if (!length)
  2755. return 0;
  2756. nseg = scsi_dma_map(scsi_cmd);
  2757. if (nseg < 0) {
  2758. scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
  2759. return -1;
  2760. } else if (nseg > PMCRAID_MAX_IOADLS) {
  2761. scsi_dma_unmap(scsi_cmd);
  2762. scmd_printk(KERN_ERR, scsi_cmd,
  2763. "sg count is (%d) more than allowed!\n", nseg);
  2764. return -1;
  2765. }
  2766. /* Initialize IOARCB data transfer length fields */
  2767. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
  2768. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  2769. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2770. ioarcb->data_transfer_length = cpu_to_le32(length);
  2771. ioadl = pmcraid_init_ioadls(cmd, nseg);
  2772. /* Initialize IOADL descriptor addresses */
  2773. scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
  2774. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
  2775. ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
  2776. ioadl[i].flags = 0;
  2777. }
  2778. /* setup last descriptor */
  2779. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  2780. return 0;
  2781. }
  2782. /**
  2783. * pmcraid_queuecommand_lck - Queue a mid-layer request
  2784. * @scsi_cmd: scsi command struct
  2785. *
  2786. * This function queues a request generated by the mid-layer. Midlayer calls
  2787. * this routine within host->lock. Some of the functions called by queuecommand
  2788. * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
  2789. *
  2790. * Return value:
  2791. * 0 on success
  2792. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  2793. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  2794. */
  2795. static int pmcraid_queuecommand_lck(struct scsi_cmnd *scsi_cmd)
  2796. {
  2797. struct pmcraid_instance *pinstance;
  2798. struct pmcraid_resource_entry *res;
  2799. struct pmcraid_ioarcb *ioarcb;
  2800. struct pmcraid_cmd *cmd;
  2801. u32 fw_version;
  2802. int rc = 0;
  2803. pinstance =
  2804. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2805. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  2806. res = scsi_cmd->device->hostdata;
  2807. scsi_cmd->result = (DID_OK << 16);
  2808. /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
  2809. * the command
  2810. */
  2811. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2812. pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
  2813. scsi_cmd->result = (DID_NO_CONNECT << 16);
  2814. scsi_done(scsi_cmd);
  2815. return 0;
  2816. }
  2817. /* If IOA reset is in progress, can't queue the commands */
  2818. if (pinstance->ioa_reset_in_progress)
  2819. return SCSI_MLQUEUE_HOST_BUSY;
  2820. /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
  2821. * the command here itself with success return
  2822. */
  2823. if (scsi_cmd->cmnd[0] == SYNCHRONIZE_CACHE) {
  2824. pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
  2825. scsi_done(scsi_cmd);
  2826. return 0;
  2827. }
  2828. /* initialize the command and IOARCB to be sent to IOA */
  2829. cmd = pmcraid_get_free_cmd(pinstance);
  2830. if (cmd == NULL) {
  2831. pmcraid_err("free command block is not available\n");
  2832. return SCSI_MLQUEUE_HOST_BUSY;
  2833. }
  2834. cmd->scsi_cmd = scsi_cmd;
  2835. ioarcb = &(cmd->ioa_cb->ioarcb);
  2836. memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  2837. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2838. ioarcb->request_type = REQ_TYPE_SCSI;
  2839. /* set hrrq number where the IOA should respond to. Note that all cmds
  2840. * generated internally uses hrrq_id 0, exception to this is the cmd
  2841. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  2842. * hrrq_id assigned here in queuecommand
  2843. */
  2844. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  2845. pinstance->num_hrrq;
  2846. cmd->cmd_done = pmcraid_io_done;
  2847. if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
  2848. if (scsi_cmd->underflow == 0)
  2849. ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
  2850. if (res->sync_reqd) {
  2851. ioarcb->request_flags0 |= SYNC_COMPLETE;
  2852. res->sync_reqd = 0;
  2853. }
  2854. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2855. if (scsi_cmd->flags & SCMD_TAGGED)
  2856. ioarcb->request_flags1 |= TASK_TAG_SIMPLE;
  2857. if (RES_IS_GSCSI(res->cfg_entry))
  2858. ioarcb->request_flags1 |= DELAY_AFTER_RESET;
  2859. }
  2860. rc = pmcraid_build_ioadl(pinstance, cmd);
  2861. pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
  2862. le32_to_cpu(ioarcb->response_handle) >> 2,
  2863. scsi_cmd->cmnd[0], pinstance->host->unique_id,
  2864. RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
  2865. PMCRAID_PHYS_BUS_ID,
  2866. RES_IS_VSET(res->cfg_entry) ?
  2867. (fw_version <= PMCRAID_FW_VERSION_1 ?
  2868. res->cfg_entry.unique_flags1 :
  2869. le16_to_cpu(res->cfg_entry.array_id) & 0xFF) :
  2870. RES_TARGET(res->cfg_entry.resource_address),
  2871. RES_LUN(res->cfg_entry.resource_address));
  2872. if (likely(rc == 0)) {
  2873. _pmcraid_fire_command(cmd);
  2874. } else {
  2875. pmcraid_err("queuecommand could not build ioadl\n");
  2876. pmcraid_return_cmd(cmd);
  2877. rc = SCSI_MLQUEUE_HOST_BUSY;
  2878. }
  2879. return rc;
  2880. }
  2881. static DEF_SCSI_QCMD(pmcraid_queuecommand)
  2882. /*
  2883. * pmcraid_open -char node "open" entry, allowed only users with admin access
  2884. */
  2885. static int pmcraid_chr_open(struct inode *inode, struct file *filep)
  2886. {
  2887. struct pmcraid_instance *pinstance;
  2888. if (!capable(CAP_SYS_ADMIN))
  2889. return -EACCES;
  2890. /* Populate adapter instance * pointer for use by ioctl */
  2891. pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
  2892. filep->private_data = pinstance;
  2893. return 0;
  2894. }
  2895. /*
  2896. * pmcraid_fasync - Async notifier registration from applications
  2897. *
  2898. * This function adds the calling process to a driver global queue. When an
  2899. * event occurs, SIGIO will be sent to all processes in this queue.
  2900. */
  2901. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  2902. {
  2903. struct pmcraid_instance *pinstance;
  2904. int rc;
  2905. pinstance = filep->private_data;
  2906. mutex_lock(&pinstance->aen_queue_lock);
  2907. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  2908. mutex_unlock(&pinstance->aen_queue_lock);
  2909. return rc;
  2910. }
  2911. /**
  2912. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  2913. *
  2914. * @pinstance: pointer to adapter instance structure
  2915. * @cmd: ioctl command passed in
  2916. * @buflen: length of user_buffer
  2917. * @user_buffer: user buffer pointer
  2918. *
  2919. * Return Value
  2920. * 0 in case of success, otherwise appropriate error code
  2921. */
  2922. static long pmcraid_ioctl_driver(
  2923. struct pmcraid_instance *pinstance,
  2924. unsigned int cmd,
  2925. unsigned int buflen,
  2926. void __user *user_buffer
  2927. )
  2928. {
  2929. int rc = -ENOSYS;
  2930. switch (cmd) {
  2931. case PMCRAID_IOCTL_RESET_ADAPTER:
  2932. pmcraid_reset_bringup(pinstance);
  2933. rc = 0;
  2934. break;
  2935. default:
  2936. break;
  2937. }
  2938. return rc;
  2939. }
  2940. /**
  2941. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  2942. *
  2943. * @cmd: ioctl command
  2944. * @arg: user buffer
  2945. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  2946. *
  2947. * Return Value
  2948. * negetive error code if there are access issues, otherwise zero.
  2949. * Upon success, returns ioctl header copied out of user buffer.
  2950. */
  2951. static int pmcraid_check_ioctl_buffer(
  2952. int cmd,
  2953. void __user *arg,
  2954. struct pmcraid_ioctl_header *hdr
  2955. )
  2956. {
  2957. int rc;
  2958. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  2959. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  2960. return -EFAULT;
  2961. }
  2962. /* check for valid driver signature */
  2963. rc = memcmp(hdr->signature,
  2964. PMCRAID_IOCTL_SIGNATURE,
  2965. sizeof(hdr->signature));
  2966. if (rc) {
  2967. pmcraid_err("signature verification failed\n");
  2968. return -EINVAL;
  2969. }
  2970. return 0;
  2971. }
  2972. /*
  2973. * pmcraid_ioctl - char node ioctl entry point
  2974. */
  2975. static long pmcraid_chr_ioctl(
  2976. struct file *filep,
  2977. unsigned int cmd,
  2978. unsigned long arg
  2979. )
  2980. {
  2981. struct pmcraid_instance *pinstance = NULL;
  2982. struct pmcraid_ioctl_header *hdr = NULL;
  2983. void __user *argp = (void __user *)arg;
  2984. int retval = -ENOTTY;
  2985. hdr = kmalloc(sizeof(struct pmcraid_ioctl_header), GFP_KERNEL);
  2986. if (!hdr) {
  2987. pmcraid_err("failed to allocate memory for ioctl header\n");
  2988. return -ENOMEM;
  2989. }
  2990. retval = pmcraid_check_ioctl_buffer(cmd, argp, hdr);
  2991. if (retval) {
  2992. pmcraid_info("chr_ioctl: header check failed\n");
  2993. kfree(hdr);
  2994. return retval;
  2995. }
  2996. pinstance = filep->private_data;
  2997. if (!pinstance) {
  2998. pmcraid_info("adapter instance is not found\n");
  2999. kfree(hdr);
  3000. return -ENOTTY;
  3001. }
  3002. switch (_IOC_TYPE(cmd)) {
  3003. case PMCRAID_DRIVER_IOCTL:
  3004. arg += sizeof(struct pmcraid_ioctl_header);
  3005. retval = pmcraid_ioctl_driver(pinstance, cmd,
  3006. hdr->buffer_length, argp);
  3007. break;
  3008. default:
  3009. retval = -ENOTTY;
  3010. break;
  3011. }
  3012. kfree(hdr);
  3013. return retval;
  3014. }
  3015. /*
  3016. * File operations structure for management interface
  3017. */
  3018. static const struct file_operations pmcraid_fops = {
  3019. .owner = THIS_MODULE,
  3020. .open = pmcraid_chr_open,
  3021. .fasync = pmcraid_chr_fasync,
  3022. .unlocked_ioctl = pmcraid_chr_ioctl,
  3023. .compat_ioctl = compat_ptr_ioctl,
  3024. .llseek = noop_llseek,
  3025. };
  3026. /**
  3027. * pmcraid_show_log_level - Display adapter's error logging level
  3028. * @dev: class device struct
  3029. * @attr: unused
  3030. * @buf: buffer
  3031. *
  3032. * Return value:
  3033. * number of bytes printed to buffer
  3034. */
  3035. static ssize_t pmcraid_show_log_level(
  3036. struct device *dev,
  3037. struct device_attribute *attr,
  3038. char *buf)
  3039. {
  3040. struct Scsi_Host *shost = class_to_shost(dev);
  3041. struct pmcraid_instance *pinstance =
  3042. (struct pmcraid_instance *)shost->hostdata;
  3043. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3044. }
  3045. /**
  3046. * pmcraid_store_log_level - Change the adapter's error logging level
  3047. * @dev: class device struct
  3048. * @attr: unused
  3049. * @buf: buffer
  3050. * @count: not used
  3051. *
  3052. * Return value:
  3053. * number of bytes printed to buffer
  3054. */
  3055. static ssize_t pmcraid_store_log_level(
  3056. struct device *dev,
  3057. struct device_attribute *attr,
  3058. const char *buf,
  3059. size_t count
  3060. )
  3061. {
  3062. struct Scsi_Host *shost;
  3063. struct pmcraid_instance *pinstance;
  3064. u8 val;
  3065. if (kstrtou8(buf, 10, &val))
  3066. return -EINVAL;
  3067. /* log-level should be from 0 to 2 */
  3068. if (val > 2)
  3069. return -EINVAL;
  3070. shost = class_to_shost(dev);
  3071. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3072. pinstance->current_log_level = val;
  3073. return strlen(buf);
  3074. }
  3075. static struct device_attribute pmcraid_log_level_attr = {
  3076. .attr = {
  3077. .name = "log_level",
  3078. .mode = S_IRUGO | S_IWUSR,
  3079. },
  3080. .show = pmcraid_show_log_level,
  3081. .store = pmcraid_store_log_level,
  3082. };
  3083. /**
  3084. * pmcraid_show_drv_version - Display driver version
  3085. * @dev: class device struct
  3086. * @attr: unused
  3087. * @buf: buffer
  3088. *
  3089. * Return value:
  3090. * number of bytes printed to buffer
  3091. */
  3092. static ssize_t pmcraid_show_drv_version(
  3093. struct device *dev,
  3094. struct device_attribute *attr,
  3095. char *buf
  3096. )
  3097. {
  3098. return snprintf(buf, PAGE_SIZE, "version: %s\n",
  3099. PMCRAID_DRIVER_VERSION);
  3100. }
  3101. static struct device_attribute pmcraid_driver_version_attr = {
  3102. .attr = {
  3103. .name = "drv_version",
  3104. .mode = S_IRUGO,
  3105. },
  3106. .show = pmcraid_show_drv_version,
  3107. };
  3108. /**
  3109. * pmcraid_show_adapter_id - Display driver assigned adapter id
  3110. * @dev: class device struct
  3111. * @attr: unused
  3112. * @buf: buffer
  3113. *
  3114. * Return value:
  3115. * number of bytes printed to buffer
  3116. */
  3117. static ssize_t pmcraid_show_adapter_id(
  3118. struct device *dev,
  3119. struct device_attribute *attr,
  3120. char *buf
  3121. )
  3122. {
  3123. struct Scsi_Host *shost = class_to_shost(dev);
  3124. struct pmcraid_instance *pinstance =
  3125. (struct pmcraid_instance *)shost->hostdata;
  3126. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3127. pinstance->pdev->devfn;
  3128. u32 aen_group = pmcraid_event_family.id;
  3129. return snprintf(buf, PAGE_SIZE,
  3130. "adapter id: %d\nminor: %d\naen group: %d\n",
  3131. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3132. }
  3133. static struct device_attribute pmcraid_adapter_id_attr = {
  3134. .attr = {
  3135. .name = "adapter_id",
  3136. .mode = S_IRUGO,
  3137. },
  3138. .show = pmcraid_show_adapter_id,
  3139. };
  3140. static struct attribute *pmcraid_host_attrs[] = {
  3141. &pmcraid_log_level_attr.attr,
  3142. &pmcraid_driver_version_attr.attr,
  3143. &pmcraid_adapter_id_attr.attr,
  3144. NULL,
  3145. };
  3146. ATTRIBUTE_GROUPS(pmcraid_host);
  3147. /* host template structure for pmcraid driver */
  3148. static struct scsi_host_template pmcraid_host_template = {
  3149. .module = THIS_MODULE,
  3150. .name = PMCRAID_DRIVER_NAME,
  3151. .queuecommand = pmcraid_queuecommand,
  3152. .eh_abort_handler = pmcraid_eh_abort_handler,
  3153. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3154. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3155. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3156. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3157. .slave_alloc = pmcraid_slave_alloc,
  3158. .slave_configure = pmcraid_slave_configure,
  3159. .slave_destroy = pmcraid_slave_destroy,
  3160. .change_queue_depth = pmcraid_change_queue_depth,
  3161. .can_queue = PMCRAID_MAX_IO_CMD,
  3162. .this_id = -1,
  3163. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3164. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3165. .no_write_same = 1,
  3166. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3167. .shost_groups = pmcraid_host_groups,
  3168. .proc_name = PMCRAID_DRIVER_NAME,
  3169. };
  3170. /*
  3171. * pmcraid_isr_msix - implements MSI-X interrupt handling routine
  3172. * @irq: interrupt vector number
  3173. * @dev_id: pointer hrrq_vector
  3174. *
  3175. * Return Value
  3176. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3177. */
  3178. static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
  3179. {
  3180. struct pmcraid_isr_param *hrrq_vector;
  3181. struct pmcraid_instance *pinstance;
  3182. unsigned long lock_flags;
  3183. u32 intrs_val;
  3184. int hrrq_id;
  3185. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3186. hrrq_id = hrrq_vector->hrrq_id;
  3187. pinstance = hrrq_vector->drv_inst;
  3188. if (!hrrq_id) {
  3189. /* Read the interrupt */
  3190. intrs_val = pmcraid_read_interrupts(pinstance);
  3191. if (intrs_val &&
  3192. ((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
  3193. & DOORBELL_INTR_MSIX_CLR) == 0)) {
  3194. /* Any error interrupts including unit_check,
  3195. * initiate IOA reset.In case of unit check indicate
  3196. * to reset_sequence that IOA unit checked and prepare
  3197. * for a dump during reset sequence
  3198. */
  3199. if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
  3200. if (intrs_val & INTRS_IOA_UNIT_CHECK)
  3201. pinstance->ioa_unit_check = 1;
  3202. pmcraid_err("ISR: error interrupts: %x \
  3203. initiating reset\n", intrs_val);
  3204. spin_lock_irqsave(pinstance->host->host_lock,
  3205. lock_flags);
  3206. pmcraid_initiate_reset(pinstance);
  3207. spin_unlock_irqrestore(
  3208. pinstance->host->host_lock,
  3209. lock_flags);
  3210. }
  3211. /* If interrupt was as part of the ioa initialization,
  3212. * clear it. Delete the timer and wakeup the
  3213. * reset engine to proceed with reset sequence
  3214. */
  3215. if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
  3216. pmcraid_clr_trans_op(pinstance);
  3217. /* Clear the interrupt register by writing
  3218. * to host to ioa doorbell. Once done
  3219. * FW will clear the interrupt.
  3220. */
  3221. iowrite32(DOORBELL_INTR_MSIX_CLR,
  3222. pinstance->int_regs.host_ioa_interrupt_reg);
  3223. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3224. }
  3225. }
  3226. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3227. return IRQ_HANDLED;
  3228. }
  3229. /**
  3230. * pmcraid_isr - implements legacy interrupt handling routine
  3231. *
  3232. * @irq: interrupt vector number
  3233. * @dev_id: pointer hrrq_vector
  3234. *
  3235. * Return Value
  3236. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3237. */
  3238. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3239. {
  3240. struct pmcraid_isr_param *hrrq_vector;
  3241. struct pmcraid_instance *pinstance;
  3242. u32 intrs;
  3243. unsigned long lock_flags;
  3244. int hrrq_id = 0;
  3245. /* In case of legacy interrupt mode where interrupts are shared across
  3246. * isrs, it may be possible that the current interrupt is not from IOA
  3247. */
  3248. if (!dev_id) {
  3249. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3250. return IRQ_NONE;
  3251. }
  3252. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3253. pinstance = hrrq_vector->drv_inst;
  3254. intrs = pmcraid_read_interrupts(pinstance);
  3255. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
  3256. return IRQ_NONE;
  3257. /* Any error interrupts including unit_check, initiate IOA reset.
  3258. * In case of unit check indicate to reset_sequence that IOA unit
  3259. * checked and prepare for a dump during reset sequence
  3260. */
  3261. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3262. if (intrs & INTRS_IOA_UNIT_CHECK)
  3263. pinstance->ioa_unit_check = 1;
  3264. iowrite32(intrs,
  3265. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3266. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3267. intrs);
  3268. intrs = ioread32(
  3269. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3270. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3271. pmcraid_initiate_reset(pinstance);
  3272. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3273. } else {
  3274. /* If interrupt was as part of the ioa initialization,
  3275. * clear. Delete the timer and wakeup the
  3276. * reset engine to proceed with reset sequence
  3277. */
  3278. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3279. pmcraid_clr_trans_op(pinstance);
  3280. } else {
  3281. iowrite32(intrs,
  3282. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3283. ioread32(
  3284. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3285. tasklet_schedule(
  3286. &(pinstance->isr_tasklet[hrrq_id]));
  3287. }
  3288. }
  3289. return IRQ_HANDLED;
  3290. }
  3291. /**
  3292. * pmcraid_worker_function - worker thread function
  3293. *
  3294. * @workp: pointer to struct work queue
  3295. *
  3296. * Return Value
  3297. * None
  3298. */
  3299. static void pmcraid_worker_function(struct work_struct *workp)
  3300. {
  3301. struct pmcraid_instance *pinstance;
  3302. struct pmcraid_resource_entry *res;
  3303. struct pmcraid_resource_entry *temp;
  3304. struct scsi_device *sdev;
  3305. unsigned long lock_flags;
  3306. unsigned long host_lock_flags;
  3307. u16 fw_version;
  3308. u8 bus, target, lun;
  3309. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3310. /* add resources only after host is added into system */
  3311. if (!atomic_read(&pinstance->expose_resources))
  3312. return;
  3313. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3314. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3315. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3316. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3317. sdev = res->scsi_dev;
  3318. /* host_lock must be held before calling
  3319. * scsi_device_get
  3320. */
  3321. spin_lock_irqsave(pinstance->host->host_lock,
  3322. host_lock_flags);
  3323. if (!scsi_device_get(sdev)) {
  3324. spin_unlock_irqrestore(
  3325. pinstance->host->host_lock,
  3326. host_lock_flags);
  3327. pmcraid_info("deleting %x from midlayer\n",
  3328. res->cfg_entry.resource_address);
  3329. list_move_tail(&res->queue,
  3330. &pinstance->free_res_q);
  3331. spin_unlock_irqrestore(
  3332. &pinstance->resource_lock,
  3333. lock_flags);
  3334. scsi_remove_device(sdev);
  3335. scsi_device_put(sdev);
  3336. spin_lock_irqsave(&pinstance->resource_lock,
  3337. lock_flags);
  3338. res->change_detected = 0;
  3339. } else {
  3340. spin_unlock_irqrestore(
  3341. pinstance->host->host_lock,
  3342. host_lock_flags);
  3343. }
  3344. }
  3345. }
  3346. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3347. if (res->change_detected == RES_CHANGE_ADD) {
  3348. if (!pmcraid_expose_resource(fw_version,
  3349. &res->cfg_entry))
  3350. continue;
  3351. if (RES_IS_VSET(res->cfg_entry)) {
  3352. bus = PMCRAID_VSET_BUS_ID;
  3353. if (fw_version <= PMCRAID_FW_VERSION_1)
  3354. target = res->cfg_entry.unique_flags1;
  3355. else
  3356. target = le16_to_cpu(res->cfg_entry.array_id) & 0xFF;
  3357. lun = PMCRAID_VSET_LUN_ID;
  3358. } else {
  3359. bus = PMCRAID_PHYS_BUS_ID;
  3360. target =
  3361. RES_TARGET(
  3362. res->cfg_entry.resource_address);
  3363. lun = RES_LUN(res->cfg_entry.resource_address);
  3364. }
  3365. res->change_detected = 0;
  3366. spin_unlock_irqrestore(&pinstance->resource_lock,
  3367. lock_flags);
  3368. scsi_add_device(pinstance->host, bus, target, lun);
  3369. spin_lock_irqsave(&pinstance->resource_lock,
  3370. lock_flags);
  3371. }
  3372. }
  3373. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3374. }
  3375. /**
  3376. * pmcraid_tasklet_function - Tasklet function
  3377. *
  3378. * @instance: pointer to msix param structure
  3379. *
  3380. * Return Value
  3381. * None
  3382. */
  3383. static void pmcraid_tasklet_function(unsigned long instance)
  3384. {
  3385. struct pmcraid_isr_param *hrrq_vector;
  3386. struct pmcraid_instance *pinstance;
  3387. unsigned long hrrq_lock_flags;
  3388. unsigned long pending_lock_flags;
  3389. unsigned long host_lock_flags;
  3390. spinlock_t *lockp; /* hrrq buffer lock */
  3391. int id;
  3392. u32 resp;
  3393. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3394. pinstance = hrrq_vector->drv_inst;
  3395. id = hrrq_vector->hrrq_id;
  3396. lockp = &(pinstance->hrrq_lock[id]);
  3397. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3398. * protected by its own lock. Traversals must be done within this lock
  3399. * as there may be multiple tasklets running on multiple CPUs. Note
  3400. * that the lock is held just for picking up the response handle and
  3401. * manipulating hrrq_curr/toggle_bit values.
  3402. */
  3403. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3404. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3405. while ((resp & HRRQ_TOGGLE_BIT) ==
  3406. pinstance->host_toggle_bit[id]) {
  3407. int cmd_index = resp >> 2;
  3408. struct pmcraid_cmd *cmd = NULL;
  3409. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  3410. pinstance->hrrq_curr[id]++;
  3411. } else {
  3412. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  3413. pinstance->host_toggle_bit[id] ^= 1u;
  3414. }
  3415. if (cmd_index >= PMCRAID_MAX_CMD) {
  3416. /* In case of invalid response handle, log message */
  3417. pmcraid_err("Invalid response handle %d\n", cmd_index);
  3418. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3419. continue;
  3420. }
  3421. cmd = pinstance->cmd_list[cmd_index];
  3422. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3423. spin_lock_irqsave(&pinstance->pending_pool_lock,
  3424. pending_lock_flags);
  3425. list_del(&cmd->free_list);
  3426. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  3427. pending_lock_flags);
  3428. del_timer(&cmd->timer);
  3429. atomic_dec(&pinstance->outstanding_cmds);
  3430. if (cmd->cmd_done == pmcraid_ioa_reset) {
  3431. spin_lock_irqsave(pinstance->host->host_lock,
  3432. host_lock_flags);
  3433. cmd->cmd_done(cmd);
  3434. spin_unlock_irqrestore(pinstance->host->host_lock,
  3435. host_lock_flags);
  3436. } else if (cmd->cmd_done != NULL) {
  3437. cmd->cmd_done(cmd);
  3438. }
  3439. /* loop over until we are done with all responses */
  3440. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3441. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3442. }
  3443. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3444. }
  3445. /**
  3446. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  3447. * @pinstance: pointer to adapter instance structure
  3448. *
  3449. * This routine un-registers registered interrupt handler and
  3450. * also frees irqs/vectors.
  3451. *
  3452. * Retun Value
  3453. * None
  3454. */
  3455. static
  3456. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  3457. {
  3458. struct pci_dev *pdev = pinstance->pdev;
  3459. int i;
  3460. for (i = 0; i < pinstance->num_hrrq; i++)
  3461. free_irq(pci_irq_vector(pdev, i), &pinstance->hrrq_vector[i]);
  3462. pinstance->interrupt_mode = 0;
  3463. pci_free_irq_vectors(pdev);
  3464. }
  3465. /**
  3466. * pmcraid_register_interrupt_handler - registers interrupt handler
  3467. * @pinstance: pointer to per-adapter instance structure
  3468. *
  3469. * Return Value
  3470. * 0 on success, non-zero error code otherwise.
  3471. */
  3472. static int
  3473. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  3474. {
  3475. struct pci_dev *pdev = pinstance->pdev;
  3476. unsigned int irq_flag = PCI_IRQ_LEGACY, flag;
  3477. int num_hrrq, rc, i;
  3478. irq_handler_t isr;
  3479. if (pmcraid_enable_msix)
  3480. irq_flag |= PCI_IRQ_MSIX;
  3481. num_hrrq = pci_alloc_irq_vectors(pdev, 1, PMCRAID_NUM_MSIX_VECTORS,
  3482. irq_flag);
  3483. if (num_hrrq < 0)
  3484. return num_hrrq;
  3485. if (pdev->msix_enabled) {
  3486. flag = 0;
  3487. isr = pmcraid_isr_msix;
  3488. } else {
  3489. flag = IRQF_SHARED;
  3490. isr = pmcraid_isr;
  3491. }
  3492. for (i = 0; i < num_hrrq; i++) {
  3493. struct pmcraid_isr_param *vec = &pinstance->hrrq_vector[i];
  3494. vec->hrrq_id = i;
  3495. vec->drv_inst = pinstance;
  3496. rc = request_irq(pci_irq_vector(pdev, i), isr, flag,
  3497. PMCRAID_DRIVER_NAME, vec);
  3498. if (rc)
  3499. goto out_unwind;
  3500. }
  3501. pinstance->num_hrrq = num_hrrq;
  3502. if (pdev->msix_enabled) {
  3503. pinstance->interrupt_mode = 1;
  3504. iowrite32(DOORBELL_INTR_MODE_MSIX,
  3505. pinstance->int_regs.host_ioa_interrupt_reg);
  3506. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3507. }
  3508. return 0;
  3509. out_unwind:
  3510. while (--i >= 0)
  3511. free_irq(pci_irq_vector(pdev, i), &pinstance->hrrq_vector[i]);
  3512. pci_free_irq_vectors(pdev);
  3513. return rc;
  3514. }
  3515. /**
  3516. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  3517. * @pinstance: per adapter instance structure pointer
  3518. * @max_index: number of buffer blocks to release
  3519. *
  3520. * Return Value
  3521. * None
  3522. */
  3523. static void
  3524. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  3525. {
  3526. int i;
  3527. for (i = 0; i < max_index; i++) {
  3528. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  3529. pinstance->cmd_list[i] = NULL;
  3530. }
  3531. kmem_cache_destroy(pinstance->cmd_cachep);
  3532. pinstance->cmd_cachep = NULL;
  3533. }
  3534. /**
  3535. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  3536. * @pinstance: pointer to per adapter instance structure
  3537. * @max_index: number of buffers (from 0 onwards) to release
  3538. *
  3539. * This function assumes that the command blocks for which control blocks are
  3540. * linked are not released.
  3541. *
  3542. * Return Value
  3543. * None
  3544. */
  3545. static void
  3546. pmcraid_release_control_blocks(
  3547. struct pmcraid_instance *pinstance,
  3548. int max_index
  3549. )
  3550. {
  3551. int i;
  3552. if (pinstance->control_pool == NULL)
  3553. return;
  3554. for (i = 0; i < max_index; i++) {
  3555. dma_pool_free(pinstance->control_pool,
  3556. pinstance->cmd_list[i]->ioa_cb,
  3557. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  3558. pinstance->cmd_list[i]->ioa_cb = NULL;
  3559. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  3560. }
  3561. dma_pool_destroy(pinstance->control_pool);
  3562. pinstance->control_pool = NULL;
  3563. }
  3564. /**
  3565. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  3566. * @pinstance: pointer to per adapter instance structure
  3567. *
  3568. * Allocates memory for command blocks using kernel slab allocator.
  3569. *
  3570. * Return Value
  3571. * 0 in case of success; -ENOMEM in case of failure
  3572. */
  3573. static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  3574. {
  3575. int i;
  3576. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  3577. pinstance->host->unique_id);
  3578. pinstance->cmd_cachep = kmem_cache_create(
  3579. pinstance->cmd_pool_name,
  3580. sizeof(struct pmcraid_cmd), 0,
  3581. SLAB_HWCACHE_ALIGN, NULL);
  3582. if (!pinstance->cmd_cachep)
  3583. return -ENOMEM;
  3584. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  3585. pinstance->cmd_list[i] =
  3586. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  3587. if (!pinstance->cmd_list[i]) {
  3588. pmcraid_release_cmd_blocks(pinstance, i);
  3589. return -ENOMEM;
  3590. }
  3591. }
  3592. return 0;
  3593. }
  3594. /**
  3595. * pmcraid_allocate_control_blocks - allocates memory control blocks
  3596. * @pinstance : pointer to per adapter instance structure
  3597. *
  3598. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  3599. * and IOASAs. This is called after command blocks are already allocated.
  3600. *
  3601. * Return Value
  3602. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  3603. */
  3604. static int pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  3605. {
  3606. int i;
  3607. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  3608. pinstance->host->unique_id);
  3609. pinstance->control_pool =
  3610. dma_pool_create(pinstance->ctl_pool_name,
  3611. &pinstance->pdev->dev,
  3612. sizeof(struct pmcraid_control_block),
  3613. PMCRAID_IOARCB_ALIGNMENT, 0);
  3614. if (!pinstance->control_pool)
  3615. return -ENOMEM;
  3616. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  3617. pinstance->cmd_list[i]->ioa_cb =
  3618. dma_pool_zalloc(
  3619. pinstance->control_pool,
  3620. GFP_KERNEL,
  3621. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  3622. if (!pinstance->cmd_list[i]->ioa_cb) {
  3623. pmcraid_release_control_blocks(pinstance, i);
  3624. return -ENOMEM;
  3625. }
  3626. }
  3627. return 0;
  3628. }
  3629. /**
  3630. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  3631. * @pinstance: pointer to per adapter instance structure
  3632. * @maxindex: size of hrrq buffer pointer array
  3633. *
  3634. * Return Value
  3635. * None
  3636. */
  3637. static void
  3638. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  3639. {
  3640. int i;
  3641. for (i = 0; i < maxindex; i++) {
  3642. dma_free_coherent(&pinstance->pdev->dev,
  3643. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  3644. pinstance->hrrq_start[i],
  3645. pinstance->hrrq_start_bus_addr[i]);
  3646. /* reset pointers and toggle bit to zeros */
  3647. pinstance->hrrq_start[i] = NULL;
  3648. pinstance->hrrq_start_bus_addr[i] = 0;
  3649. pinstance->host_toggle_bit[i] = 0;
  3650. }
  3651. }
  3652. /**
  3653. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  3654. * @pinstance: pointer to per adapter instance structure
  3655. *
  3656. * Return value
  3657. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  3658. */
  3659. static int pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  3660. {
  3661. int i, buffer_size;
  3662. buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  3663. for (i = 0; i < pinstance->num_hrrq; i++) {
  3664. pinstance->hrrq_start[i] =
  3665. dma_alloc_coherent(&pinstance->pdev->dev, buffer_size,
  3666. &pinstance->hrrq_start_bus_addr[i],
  3667. GFP_KERNEL);
  3668. if (!pinstance->hrrq_start[i]) {
  3669. pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
  3670. i);
  3671. pmcraid_release_host_rrqs(pinstance, i);
  3672. return -ENOMEM;
  3673. }
  3674. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  3675. pinstance->hrrq_end[i] =
  3676. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  3677. pinstance->host_toggle_bit[i] = 1;
  3678. spin_lock_init(&pinstance->hrrq_lock[i]);
  3679. }
  3680. return 0;
  3681. }
  3682. /**
  3683. * pmcraid_release_hcams - release HCAM buffers
  3684. *
  3685. * @pinstance: pointer to per adapter instance structure
  3686. *
  3687. * Return value
  3688. * none
  3689. */
  3690. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  3691. {
  3692. if (pinstance->ccn.msg != NULL) {
  3693. dma_free_coherent(&pinstance->pdev->dev,
  3694. PMCRAID_AEN_HDR_SIZE +
  3695. sizeof(struct pmcraid_hcam_ccn_ext),
  3696. pinstance->ccn.msg,
  3697. pinstance->ccn.baddr);
  3698. pinstance->ccn.msg = NULL;
  3699. pinstance->ccn.hcam = NULL;
  3700. pinstance->ccn.baddr = 0;
  3701. }
  3702. if (pinstance->ldn.msg != NULL) {
  3703. dma_free_coherent(&pinstance->pdev->dev,
  3704. PMCRAID_AEN_HDR_SIZE +
  3705. sizeof(struct pmcraid_hcam_ldn),
  3706. pinstance->ldn.msg,
  3707. pinstance->ldn.baddr);
  3708. pinstance->ldn.msg = NULL;
  3709. pinstance->ldn.hcam = NULL;
  3710. pinstance->ldn.baddr = 0;
  3711. }
  3712. }
  3713. /**
  3714. * pmcraid_allocate_hcams - allocates HCAM buffers
  3715. * @pinstance : pointer to per adapter instance structure
  3716. *
  3717. * Return Value:
  3718. * 0 in case of successful allocation, non-zero otherwise
  3719. */
  3720. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  3721. {
  3722. pinstance->ccn.msg = dma_alloc_coherent(&pinstance->pdev->dev,
  3723. PMCRAID_AEN_HDR_SIZE +
  3724. sizeof(struct pmcraid_hcam_ccn_ext),
  3725. &pinstance->ccn.baddr, GFP_KERNEL);
  3726. pinstance->ldn.msg = dma_alloc_coherent(&pinstance->pdev->dev,
  3727. PMCRAID_AEN_HDR_SIZE +
  3728. sizeof(struct pmcraid_hcam_ldn),
  3729. &pinstance->ldn.baddr, GFP_KERNEL);
  3730. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  3731. pmcraid_release_hcams(pinstance);
  3732. } else {
  3733. pinstance->ccn.hcam =
  3734. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  3735. pinstance->ldn.hcam =
  3736. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  3737. atomic_set(&pinstance->ccn.ignore, 0);
  3738. atomic_set(&pinstance->ldn.ignore, 0);
  3739. }
  3740. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  3741. }
  3742. /**
  3743. * pmcraid_release_config_buffers - release config.table buffers
  3744. * @pinstance: pointer to per adapter instance structure
  3745. *
  3746. * Return Value
  3747. * none
  3748. */
  3749. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  3750. {
  3751. if (pinstance->cfg_table != NULL &&
  3752. pinstance->cfg_table_bus_addr != 0) {
  3753. dma_free_coherent(&pinstance->pdev->dev,
  3754. sizeof(struct pmcraid_config_table),
  3755. pinstance->cfg_table,
  3756. pinstance->cfg_table_bus_addr);
  3757. pinstance->cfg_table = NULL;
  3758. pinstance->cfg_table_bus_addr = 0;
  3759. }
  3760. if (pinstance->res_entries != NULL) {
  3761. int i;
  3762. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  3763. list_del(&pinstance->res_entries[i].queue);
  3764. kfree(pinstance->res_entries);
  3765. pinstance->res_entries = NULL;
  3766. }
  3767. pmcraid_release_hcams(pinstance);
  3768. }
  3769. /**
  3770. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  3771. * @pinstance : pointer to per adapter instance structure
  3772. *
  3773. * Return Value
  3774. * 0 for successful allocation, -ENOMEM for any failure
  3775. */
  3776. static int pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  3777. {
  3778. int i;
  3779. pinstance->res_entries =
  3780. kcalloc(PMCRAID_MAX_RESOURCES,
  3781. sizeof(struct pmcraid_resource_entry),
  3782. GFP_KERNEL);
  3783. if (NULL == pinstance->res_entries) {
  3784. pmcraid_err("failed to allocate memory for resource table\n");
  3785. return -ENOMEM;
  3786. }
  3787. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  3788. list_add_tail(&pinstance->res_entries[i].queue,
  3789. &pinstance->free_res_q);
  3790. pinstance->cfg_table = dma_alloc_coherent(&pinstance->pdev->dev,
  3791. sizeof(struct pmcraid_config_table),
  3792. &pinstance->cfg_table_bus_addr,
  3793. GFP_KERNEL);
  3794. if (NULL == pinstance->cfg_table) {
  3795. pmcraid_err("couldn't alloc DMA memory for config table\n");
  3796. pmcraid_release_config_buffers(pinstance);
  3797. return -ENOMEM;
  3798. }
  3799. if (pmcraid_allocate_hcams(pinstance)) {
  3800. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  3801. pmcraid_release_config_buffers(pinstance);
  3802. return -ENOMEM;
  3803. }
  3804. return 0;
  3805. }
  3806. /**
  3807. * pmcraid_init_tasklets - registers tasklets for response handling
  3808. *
  3809. * @pinstance: pointer adapter instance structure
  3810. *
  3811. * Return value
  3812. * none
  3813. */
  3814. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  3815. {
  3816. int i;
  3817. for (i = 0; i < pinstance->num_hrrq; i++)
  3818. tasklet_init(&pinstance->isr_tasklet[i],
  3819. pmcraid_tasklet_function,
  3820. (unsigned long)&pinstance->hrrq_vector[i]);
  3821. }
  3822. /**
  3823. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  3824. *
  3825. * @pinstance: pointer to adapter instance structure
  3826. *
  3827. * Return value
  3828. * none
  3829. */
  3830. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  3831. {
  3832. int i;
  3833. for (i = 0; i < pinstance->num_hrrq; i++)
  3834. tasklet_kill(&pinstance->isr_tasklet[i]);
  3835. }
  3836. /**
  3837. * pmcraid_release_buffers - release per-adapter buffers allocated
  3838. *
  3839. * @pinstance: pointer to adapter soft state
  3840. *
  3841. * Return Value
  3842. * none
  3843. */
  3844. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  3845. {
  3846. pmcraid_release_config_buffers(pinstance);
  3847. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  3848. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  3849. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  3850. if (pinstance->inq_data != NULL) {
  3851. dma_free_coherent(&pinstance->pdev->dev,
  3852. sizeof(struct pmcraid_inquiry_data),
  3853. pinstance->inq_data,
  3854. pinstance->inq_data_baddr);
  3855. pinstance->inq_data = NULL;
  3856. pinstance->inq_data_baddr = 0;
  3857. }
  3858. if (pinstance->timestamp_data != NULL) {
  3859. dma_free_coherent(&pinstance->pdev->dev,
  3860. sizeof(struct pmcraid_timestamp_data),
  3861. pinstance->timestamp_data,
  3862. pinstance->timestamp_data_baddr);
  3863. pinstance->timestamp_data = NULL;
  3864. pinstance->timestamp_data_baddr = 0;
  3865. }
  3866. }
  3867. /**
  3868. * pmcraid_init_buffers - allocates memory and initializes various structures
  3869. * @pinstance: pointer to per adapter instance structure
  3870. *
  3871. * This routine pre-allocates memory based on the type of block as below:
  3872. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  3873. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  3874. * config-table entries : DMAable memory using dma_alloc_coherent
  3875. * HostRRQs : DMAable memory, using dma_alloc_coherent
  3876. *
  3877. * Return Value
  3878. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  3879. */
  3880. static int pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  3881. {
  3882. int i;
  3883. if (pmcraid_allocate_host_rrqs(pinstance)) {
  3884. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  3885. pinstance->num_hrrq);
  3886. return -ENOMEM;
  3887. }
  3888. if (pmcraid_allocate_config_buffers(pinstance)) {
  3889. pmcraid_err("couldn't allocate memory for config buffers\n");
  3890. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  3891. return -ENOMEM;
  3892. }
  3893. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  3894. pmcraid_err("couldn't allocate memory for cmd blocks\n");
  3895. pmcraid_release_config_buffers(pinstance);
  3896. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  3897. return -ENOMEM;
  3898. }
  3899. if (pmcraid_allocate_control_blocks(pinstance)) {
  3900. pmcraid_err("couldn't allocate memory control blocks\n");
  3901. pmcraid_release_config_buffers(pinstance);
  3902. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  3903. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  3904. return -ENOMEM;
  3905. }
  3906. /* allocate DMAable memory for page D0 INQUIRY buffer */
  3907. pinstance->inq_data = dma_alloc_coherent(&pinstance->pdev->dev,
  3908. sizeof(struct pmcraid_inquiry_data),
  3909. &pinstance->inq_data_baddr, GFP_KERNEL);
  3910. if (pinstance->inq_data == NULL) {
  3911. pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
  3912. pmcraid_release_buffers(pinstance);
  3913. return -ENOMEM;
  3914. }
  3915. /* allocate DMAable memory for set timestamp data buffer */
  3916. pinstance->timestamp_data = dma_alloc_coherent(&pinstance->pdev->dev,
  3917. sizeof(struct pmcraid_timestamp_data),
  3918. &pinstance->timestamp_data_baddr,
  3919. GFP_KERNEL);
  3920. if (pinstance->timestamp_data == NULL) {
  3921. pmcraid_err("couldn't allocate DMA memory for \
  3922. set time_stamp \n");
  3923. pmcraid_release_buffers(pinstance);
  3924. return -ENOMEM;
  3925. }
  3926. /* Initialize all the command blocks and add them to free pool. No
  3927. * need to lock (free_pool_lock) as this is done in initialization
  3928. * itself
  3929. */
  3930. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  3931. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  3932. pmcraid_init_cmdblk(cmdp, i);
  3933. cmdp->drv_inst = pinstance;
  3934. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  3935. }
  3936. return 0;
  3937. }
  3938. /**
  3939. * pmcraid_reinit_buffers - resets various buffer pointers
  3940. * @pinstance: pointer to adapter instance
  3941. * Return value
  3942. * none
  3943. */
  3944. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  3945. {
  3946. int i;
  3947. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  3948. for (i = 0; i < pinstance->num_hrrq; i++) {
  3949. memset(pinstance->hrrq_start[i], 0, buffer_size);
  3950. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  3951. pinstance->hrrq_end[i] =
  3952. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  3953. pinstance->host_toggle_bit[i] = 1;
  3954. }
  3955. }
  3956. /**
  3957. * pmcraid_init_instance - initialize per instance data structure
  3958. * @pdev: pointer to pci device structure
  3959. * @host: pointer to Scsi_Host structure
  3960. * @mapped_pci_addr: memory mapped IOA configuration registers
  3961. *
  3962. * Return Value
  3963. * 0 on success, non-zero in case of any failure
  3964. */
  3965. static int pmcraid_init_instance(struct pci_dev *pdev, struct Scsi_Host *host,
  3966. void __iomem *mapped_pci_addr)
  3967. {
  3968. struct pmcraid_instance *pinstance =
  3969. (struct pmcraid_instance *)host->hostdata;
  3970. pinstance->host = host;
  3971. pinstance->pdev = pdev;
  3972. /* Initialize register addresses */
  3973. pinstance->mapped_dma_addr = mapped_pci_addr;
  3974. /* Initialize chip-specific details */
  3975. {
  3976. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  3977. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  3978. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  3979. pint_regs->ioa_host_interrupt_reg =
  3980. mapped_pci_addr + chip_cfg->ioa_host_intr;
  3981. pint_regs->ioa_host_interrupt_clr_reg =
  3982. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  3983. pint_regs->ioa_host_msix_interrupt_reg =
  3984. mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
  3985. pint_regs->host_ioa_interrupt_reg =
  3986. mapped_pci_addr + chip_cfg->host_ioa_intr;
  3987. pint_regs->host_ioa_interrupt_clr_reg =
  3988. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  3989. /* Current version of firmware exposes interrupt mask set
  3990. * and mask clr registers through memory mapped bar0.
  3991. */
  3992. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  3993. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  3994. pint_regs->ioa_host_interrupt_mask_reg =
  3995. mapped_pci_addr + chip_cfg->ioa_host_mask;
  3996. pint_regs->ioa_host_interrupt_mask_clr_reg =
  3997. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  3998. pint_regs->global_interrupt_mask_reg =
  3999. mapped_pci_addr + chip_cfg->global_intr_mask;
  4000. }
  4001. pinstance->ioa_reset_attempts = 0;
  4002. init_waitqueue_head(&pinstance->reset_wait_q);
  4003. atomic_set(&pinstance->outstanding_cmds, 0);
  4004. atomic_set(&pinstance->last_message_id, 0);
  4005. atomic_set(&pinstance->expose_resources, 0);
  4006. INIT_LIST_HEAD(&pinstance->free_res_q);
  4007. INIT_LIST_HEAD(&pinstance->used_res_q);
  4008. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4009. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4010. spin_lock_init(&pinstance->free_pool_lock);
  4011. spin_lock_init(&pinstance->pending_pool_lock);
  4012. spin_lock_init(&pinstance->resource_lock);
  4013. mutex_init(&pinstance->aen_queue_lock);
  4014. /* Work-queue (Shared) for deferred processing error handling */
  4015. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4016. /* Initialize the default log_level */
  4017. pinstance->current_log_level = pmcraid_log_level;
  4018. /* Setup variables required for reset engine */
  4019. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4020. pinstance->reset_cmd = NULL;
  4021. return 0;
  4022. }
  4023. /**
  4024. * pmcraid_shutdown - shutdown adapter controller.
  4025. * @pdev: pci device struct
  4026. *
  4027. * Issues an adapter shutdown to the card waits for its completion
  4028. *
  4029. * Return value
  4030. * none
  4031. */
  4032. static void pmcraid_shutdown(struct pci_dev *pdev)
  4033. {
  4034. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4035. pmcraid_reset_bringdown(pinstance);
  4036. }
  4037. /*
  4038. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4039. */
  4040. static unsigned short pmcraid_get_minor(void)
  4041. {
  4042. int minor;
  4043. minor = find_first_zero_bit(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  4044. __set_bit(minor, pmcraid_minor);
  4045. return minor;
  4046. }
  4047. /*
  4048. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4049. */
  4050. static void pmcraid_release_minor(unsigned short minor)
  4051. {
  4052. __clear_bit(minor, pmcraid_minor);
  4053. }
  4054. /**
  4055. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4056. *
  4057. * @pinstance: pointer to adapter instance for which to register device
  4058. *
  4059. * Return value
  4060. * 0 in case of success, otherwise non-zero
  4061. */
  4062. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4063. {
  4064. int minor;
  4065. int error;
  4066. minor = pmcraid_get_minor();
  4067. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4068. pinstance->cdev.owner = THIS_MODULE;
  4069. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4070. if (error)
  4071. pmcraid_release_minor(minor);
  4072. else
  4073. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4074. NULL, "%s%u", PMCRAID_DEVFILE, minor);
  4075. return error;
  4076. }
  4077. /**
  4078. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4079. *
  4080. * @pinstance: pointer to adapter instance structure
  4081. *
  4082. * Return value
  4083. * none
  4084. */
  4085. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4086. {
  4087. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4088. device_destroy(pmcraid_class,
  4089. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4090. cdev_del(&pinstance->cdev);
  4091. }
  4092. /**
  4093. * pmcraid_remove - IOA hot plug remove entry point
  4094. * @pdev: pci device struct
  4095. *
  4096. * Return value
  4097. * none
  4098. */
  4099. static void pmcraid_remove(struct pci_dev *pdev)
  4100. {
  4101. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4102. /* remove the management interface (/dev file) for this device */
  4103. pmcraid_release_chrdev(pinstance);
  4104. /* remove host template from scsi midlayer */
  4105. scsi_remove_host(pinstance->host);
  4106. /* block requests from mid-layer */
  4107. scsi_block_requests(pinstance->host);
  4108. /* initiate shutdown adapter */
  4109. pmcraid_shutdown(pdev);
  4110. pmcraid_disable_interrupts(pinstance, ~0);
  4111. flush_work(&pinstance->worker_q);
  4112. pmcraid_kill_tasklets(pinstance);
  4113. pmcraid_unregister_interrupt_handler(pinstance);
  4114. pmcraid_release_buffers(pinstance);
  4115. iounmap(pinstance->mapped_dma_addr);
  4116. pci_release_regions(pdev);
  4117. scsi_host_put(pinstance->host);
  4118. pci_disable_device(pdev);
  4119. return;
  4120. }
  4121. /**
  4122. * pmcraid_suspend - driver suspend entry point for power management
  4123. * @dev: Device structure
  4124. *
  4125. * Return Value - 0 always
  4126. */
  4127. static int __maybe_unused pmcraid_suspend(struct device *dev)
  4128. {
  4129. struct pci_dev *pdev = to_pci_dev(dev);
  4130. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4131. pmcraid_shutdown(pdev);
  4132. pmcraid_disable_interrupts(pinstance, ~0);
  4133. pmcraid_kill_tasklets(pinstance);
  4134. pmcraid_unregister_interrupt_handler(pinstance);
  4135. return 0;
  4136. }
  4137. /**
  4138. * pmcraid_resume - driver resume entry point PCI power management
  4139. * @dev: Device structure
  4140. *
  4141. * Return Value - 0 in case of success. Error code in case of any failure
  4142. */
  4143. static int __maybe_unused pmcraid_resume(struct device *dev)
  4144. {
  4145. struct pci_dev *pdev = to_pci_dev(dev);
  4146. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4147. struct Scsi_Host *host = pinstance->host;
  4148. int rc = 0;
  4149. if (sizeof(dma_addr_t) == 4 ||
  4150. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)))
  4151. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  4152. if (rc == 0)
  4153. rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  4154. if (rc != 0) {
  4155. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4156. goto disable_device;
  4157. }
  4158. pmcraid_disable_interrupts(pinstance, ~0);
  4159. atomic_set(&pinstance->outstanding_cmds, 0);
  4160. rc = pmcraid_register_interrupt_handler(pinstance);
  4161. if (rc) {
  4162. dev_err(&pdev->dev,
  4163. "resume: couldn't register interrupt handlers\n");
  4164. rc = -ENODEV;
  4165. goto release_host;
  4166. }
  4167. pmcraid_init_tasklets(pinstance);
  4168. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4169. /* Start with hard reset sequence which brings up IOA to operational
  4170. * state as well as completes the reset sequence.
  4171. */
  4172. pinstance->ioa_hard_reset = 1;
  4173. /* Start IOA firmware initialization and bring card to Operational
  4174. * state.
  4175. */
  4176. if (pmcraid_reset_bringup(pinstance)) {
  4177. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4178. rc = -ENODEV;
  4179. goto release_tasklets;
  4180. }
  4181. return 0;
  4182. release_tasklets:
  4183. pmcraid_disable_interrupts(pinstance, ~0);
  4184. pmcraid_kill_tasklets(pinstance);
  4185. pmcraid_unregister_interrupt_handler(pinstance);
  4186. release_host:
  4187. scsi_host_put(host);
  4188. disable_device:
  4189. return rc;
  4190. }
  4191. /**
  4192. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4193. * completion of the ioa reset
  4194. * @cmd: pointer to reset command block
  4195. */
  4196. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4197. {
  4198. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4199. unsigned long flags;
  4200. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4201. pmcraid_ioa_reset(cmd);
  4202. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4203. scsi_unblock_requests(pinstance->host);
  4204. schedule_work(&pinstance->worker_q);
  4205. }
  4206. /**
  4207. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4208. *
  4209. * @cmd: pointer to pmcraid_cmd structure
  4210. *
  4211. * Return Value
  4212. * 0 for success or non-zero for failure cases
  4213. */
  4214. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4215. {
  4216. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4217. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4218. pmcraid_reinit_cmdblk(cmd);
  4219. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4220. ioarcb->request_type = REQ_TYPE_IOACMD;
  4221. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4222. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4223. /* If this was called as part of resource table reinitialization due to
  4224. * lost CCN, it is enough to return the command block back to free pool
  4225. * as part of set_supported_devs completion function.
  4226. */
  4227. if (cmd->drv_inst->reinit_cfg_table) {
  4228. cmd->drv_inst->reinit_cfg_table = 0;
  4229. cmd->release = 1;
  4230. cmd_done = pmcraid_reinit_cfgtable_done;
  4231. }
  4232. /* we will be done with the reset sequence after set supported devices,
  4233. * setup the done function to return the command block back to free
  4234. * pool
  4235. */
  4236. pmcraid_send_cmd(cmd,
  4237. cmd_done,
  4238. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4239. pmcraid_timeout_handler);
  4240. return;
  4241. }
  4242. /**
  4243. * pmcraid_set_timestamp - set the timestamp to IOAFP
  4244. *
  4245. * @cmd: pointer to pmcraid_cmd structure
  4246. *
  4247. * Return Value
  4248. * 0 for success or non-zero for failure cases
  4249. */
  4250. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
  4251. {
  4252. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4253. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4254. __be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
  4255. struct pmcraid_ioadl_desc *ioadl;
  4256. u64 timestamp;
  4257. timestamp = ktime_get_real_seconds() * 1000;
  4258. pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
  4259. pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
  4260. pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
  4261. pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
  4262. pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
  4263. pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp) >> 40);
  4264. pmcraid_reinit_cmdblk(cmd);
  4265. ioarcb->request_type = REQ_TYPE_SCSI;
  4266. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4267. ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
  4268. ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
  4269. memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
  4270. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4271. offsetof(struct pmcraid_ioarcb,
  4272. add_data.u.ioadl[0]));
  4273. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4274. ioarcb->ioarcb_bus_addr &= cpu_to_le64(~(0x1FULL));
  4275. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4276. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  4277. ioarcb->data_transfer_length =
  4278. cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4279. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4280. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4281. ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
  4282. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4283. if (!pinstance->timestamp_error) {
  4284. pinstance->timestamp_error = 0;
  4285. pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
  4286. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4287. } else {
  4288. pmcraid_send_cmd(cmd, pmcraid_return_cmd,
  4289. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4290. return;
  4291. }
  4292. }
  4293. /**
  4294. * pmcraid_init_res_table - Initialize the resource table
  4295. * @cmd: pointer to pmcraid command struct
  4296. *
  4297. * This function looks through the existing resource table, comparing
  4298. * it with the config table. This function will take care of old/new
  4299. * devices and schedule adding/removing them from the mid-layer
  4300. * as appropriate.
  4301. *
  4302. * Return value
  4303. * None
  4304. */
  4305. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4306. {
  4307. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4308. struct pmcraid_resource_entry *res, *temp;
  4309. struct pmcraid_config_table_entry *cfgte;
  4310. unsigned long lock_flags;
  4311. int found, rc, i;
  4312. u16 fw_version;
  4313. LIST_HEAD(old_res);
  4314. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4315. pmcraid_err("IOA requires microcode download\n");
  4316. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  4317. /* resource list is protected by pinstance->resource_lock.
  4318. * init_res_table can be called from probe (user-thread) or runtime
  4319. * reset (timer/tasklet)
  4320. */
  4321. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4322. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4323. list_move_tail(&res->queue, &old_res);
  4324. for (i = 0; i < le16_to_cpu(pinstance->cfg_table->num_entries); i++) {
  4325. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4326. PMCRAID_FW_VERSION_1)
  4327. cfgte = &pinstance->cfg_table->entries[i];
  4328. else
  4329. cfgte = (struct pmcraid_config_table_entry *)
  4330. &pinstance->cfg_table->entries_ext[i];
  4331. if (!pmcraid_expose_resource(fw_version, cfgte))
  4332. continue;
  4333. found = 0;
  4334. /* If this entry was already detected and initialized */
  4335. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4336. rc = memcmp(&res->cfg_entry.resource_address,
  4337. &cfgte->resource_address,
  4338. sizeof(cfgte->resource_address));
  4339. if (!rc) {
  4340. list_move_tail(&res->queue,
  4341. &pinstance->used_res_q);
  4342. found = 1;
  4343. break;
  4344. }
  4345. }
  4346. /* If this is new entry, initialize it and add it the queue */
  4347. if (!found) {
  4348. if (list_empty(&pinstance->free_res_q)) {
  4349. pmcraid_err("Too many devices attached\n");
  4350. break;
  4351. }
  4352. found = 1;
  4353. res = list_entry(pinstance->free_res_q.next,
  4354. struct pmcraid_resource_entry, queue);
  4355. res->scsi_dev = NULL;
  4356. res->change_detected = RES_CHANGE_ADD;
  4357. res->reset_progress = 0;
  4358. list_move_tail(&res->queue, &pinstance->used_res_q);
  4359. }
  4360. /* copy new configuration table entry details into driver
  4361. * maintained resource entry
  4362. */
  4363. if (found) {
  4364. memcpy(&res->cfg_entry, cfgte,
  4365. pinstance->config_table_entry_size);
  4366. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  4367. res->cfg_entry.resource_type,
  4368. (fw_version <= PMCRAID_FW_VERSION_1 ?
  4369. res->cfg_entry.unique_flags1 :
  4370. le16_to_cpu(res->cfg_entry.array_id) & 0xFF),
  4371. le32_to_cpu(res->cfg_entry.resource_address));
  4372. }
  4373. }
  4374. /* Detect any deleted entries, mark them for deletion from mid-layer */
  4375. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4376. if (res->scsi_dev) {
  4377. res->change_detected = RES_CHANGE_DEL;
  4378. res->cfg_entry.resource_handle =
  4379. PMCRAID_INVALID_RES_HANDLE;
  4380. list_move_tail(&res->queue, &pinstance->used_res_q);
  4381. } else {
  4382. list_move_tail(&res->queue, &pinstance->free_res_q);
  4383. }
  4384. }
  4385. /* release the resource list lock */
  4386. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  4387. pmcraid_set_timestamp(cmd);
  4388. }
  4389. /**
  4390. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  4391. * @cmd: pointer pmcraid_cmd struct
  4392. *
  4393. * This function sends a Query IOA Configuration command to the adapter to
  4394. * retrieve the IOA configuration table.
  4395. *
  4396. * Return value:
  4397. * none
  4398. */
  4399. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  4400. {
  4401. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4402. struct pmcraid_ioadl_desc *ioadl;
  4403. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4404. __be32 cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  4405. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4406. PMCRAID_FW_VERSION_1)
  4407. pinstance->config_table_entry_size =
  4408. sizeof(struct pmcraid_config_table_entry);
  4409. else
  4410. pinstance->config_table_entry_size =
  4411. sizeof(struct pmcraid_config_table_entry_ext);
  4412. ioarcb->request_type = REQ_TYPE_IOACMD;
  4413. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4414. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  4415. /* firmware requires 4-byte length field, specified in B.E format */
  4416. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  4417. /* Since entire config table can be described by single IOADL, it can
  4418. * be part of IOARCB itself
  4419. */
  4420. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4421. offsetof(struct pmcraid_ioarcb,
  4422. add_data.u.ioadl[0]));
  4423. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4424. ioarcb->ioarcb_bus_addr &= cpu_to_le64(~0x1FULL);
  4425. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4426. ioarcb->data_transfer_length =
  4427. cpu_to_le32(sizeof(struct pmcraid_config_table));
  4428. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4429. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4430. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  4431. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  4432. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  4433. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4434. }
  4435. /**
  4436. * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
  4437. * @pdev: pointer to pci device structure
  4438. * @dev_id: pointer to device ids structure
  4439. *
  4440. * Return Value
  4441. * returns 0 if the device is claimed and successfully configured.
  4442. * returns non-zero error code in case of any failure
  4443. */
  4444. static int pmcraid_probe(struct pci_dev *pdev,
  4445. const struct pci_device_id *dev_id)
  4446. {
  4447. struct pmcraid_instance *pinstance;
  4448. struct Scsi_Host *host;
  4449. void __iomem *mapped_pci_addr;
  4450. int rc = PCIBIOS_SUCCESSFUL;
  4451. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  4452. pmcraid_err
  4453. ("maximum number(%d) of supported adapters reached\n",
  4454. atomic_read(&pmcraid_adapter_count));
  4455. return -ENOMEM;
  4456. }
  4457. atomic_inc(&pmcraid_adapter_count);
  4458. rc = pci_enable_device(pdev);
  4459. if (rc) {
  4460. dev_err(&pdev->dev, "Cannot enable adapter\n");
  4461. atomic_dec(&pmcraid_adapter_count);
  4462. return rc;
  4463. }
  4464. dev_info(&pdev->dev,
  4465. "Found new IOA(%x:%x), Total IOA count: %d\n",
  4466. pdev->vendor, pdev->device,
  4467. atomic_read(&pmcraid_adapter_count));
  4468. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  4469. if (rc < 0) {
  4470. dev_err(&pdev->dev,
  4471. "Couldn't register memory range of registers\n");
  4472. goto out_disable_device;
  4473. }
  4474. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  4475. if (!mapped_pci_addr) {
  4476. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  4477. rc = -ENOMEM;
  4478. goto out_release_regions;
  4479. }
  4480. pci_set_master(pdev);
  4481. /* Firmware requires the system bus address of IOARCB to be within
  4482. * 32-bit addressable range though it has 64-bit IOARRIN register.
  4483. * However, firmware supports 64-bit streaming DMA buffers, whereas
  4484. * coherent buffers are to be 32-bit. Since dma_alloc_coherent always
  4485. * returns memory within 4GB (if not, change this logic), coherent
  4486. * buffers are within firmware acceptable address ranges.
  4487. */
  4488. if (sizeof(dma_addr_t) == 4 ||
  4489. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)))
  4490. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  4491. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  4492. * bit mask for dma_alloc_coherent to return addresses within 4GB
  4493. */
  4494. if (rc == 0)
  4495. rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  4496. if (rc != 0) {
  4497. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  4498. goto cleanup_nomem;
  4499. }
  4500. host = scsi_host_alloc(&pmcraid_host_template,
  4501. sizeof(struct pmcraid_instance));
  4502. if (!host) {
  4503. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  4504. rc = -ENOMEM;
  4505. goto cleanup_nomem;
  4506. }
  4507. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  4508. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  4509. host->unique_id = host->host_no;
  4510. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  4511. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  4512. /* zero out entire instance structure */
  4513. pinstance = (struct pmcraid_instance *)host->hostdata;
  4514. memset(pinstance, 0, sizeof(*pinstance));
  4515. pinstance->chip_cfg =
  4516. (struct pmcraid_chip_details *)(dev_id->driver_data);
  4517. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  4518. if (rc < 0) {
  4519. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  4520. goto out_scsi_host_put;
  4521. }
  4522. pci_set_drvdata(pdev, pinstance);
  4523. /* Save PCI config-space for use following the reset */
  4524. rc = pci_save_state(pinstance->pdev);
  4525. if (rc != 0) {
  4526. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  4527. goto out_scsi_host_put;
  4528. }
  4529. pmcraid_disable_interrupts(pinstance, ~0);
  4530. rc = pmcraid_register_interrupt_handler(pinstance);
  4531. if (rc) {
  4532. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  4533. goto out_scsi_host_put;
  4534. }
  4535. pmcraid_init_tasklets(pinstance);
  4536. /* allocate verious buffers used by LLD.*/
  4537. rc = pmcraid_init_buffers(pinstance);
  4538. if (rc) {
  4539. pmcraid_err("couldn't allocate memory blocks\n");
  4540. goto out_unregister_isr;
  4541. }
  4542. /* check the reset type required */
  4543. pmcraid_reset_type(pinstance);
  4544. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4545. /* Start IOA firmware initialization and bring card to Operational
  4546. * state.
  4547. */
  4548. pmcraid_info("starting IOA initialization sequence\n");
  4549. if (pmcraid_reset_bringup(pinstance)) {
  4550. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4551. rc = 1;
  4552. goto out_release_bufs;
  4553. }
  4554. /* Add adapter instance into mid-layer list */
  4555. rc = scsi_add_host(pinstance->host, &pdev->dev);
  4556. if (rc != 0) {
  4557. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  4558. goto out_release_bufs;
  4559. }
  4560. scsi_scan_host(pinstance->host);
  4561. rc = pmcraid_setup_chrdev(pinstance);
  4562. if (rc != 0) {
  4563. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  4564. rc);
  4565. goto out_remove_host;
  4566. }
  4567. /* Schedule worker thread to handle CCN and take care of adding and
  4568. * removing devices to OS
  4569. */
  4570. atomic_set(&pinstance->expose_resources, 1);
  4571. schedule_work(&pinstance->worker_q);
  4572. return rc;
  4573. out_remove_host:
  4574. scsi_remove_host(host);
  4575. out_release_bufs:
  4576. pmcraid_release_buffers(pinstance);
  4577. out_unregister_isr:
  4578. pmcraid_kill_tasklets(pinstance);
  4579. pmcraid_unregister_interrupt_handler(pinstance);
  4580. out_scsi_host_put:
  4581. scsi_host_put(host);
  4582. cleanup_nomem:
  4583. iounmap(mapped_pci_addr);
  4584. out_release_regions:
  4585. pci_release_regions(pdev);
  4586. out_disable_device:
  4587. atomic_dec(&pmcraid_adapter_count);
  4588. pci_disable_device(pdev);
  4589. return -ENODEV;
  4590. }
  4591. static SIMPLE_DEV_PM_OPS(pmcraid_pm_ops, pmcraid_suspend, pmcraid_resume);
  4592. /*
  4593. * PCI driver structure of pmcraid driver
  4594. */
  4595. static struct pci_driver pmcraid_driver = {
  4596. .name = PMCRAID_DRIVER_NAME,
  4597. .id_table = pmcraid_pci_table,
  4598. .probe = pmcraid_probe,
  4599. .remove = pmcraid_remove,
  4600. .driver.pm = &pmcraid_pm_ops,
  4601. .shutdown = pmcraid_shutdown
  4602. };
  4603. /**
  4604. * pmcraid_init - module load entry point
  4605. */
  4606. static int __init pmcraid_init(void)
  4607. {
  4608. dev_t dev;
  4609. int error;
  4610. pmcraid_info("%s Device Driver version: %s\n",
  4611. PMCRAID_DRIVER_NAME, PMCRAID_DRIVER_VERSION);
  4612. error = alloc_chrdev_region(&dev, 0,
  4613. PMCRAID_MAX_ADAPTERS,
  4614. PMCRAID_DEVFILE);
  4615. if (error) {
  4616. pmcraid_err("failed to get a major number for adapters\n");
  4617. goto out_init;
  4618. }
  4619. pmcraid_major = MAJOR(dev);
  4620. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  4621. if (IS_ERR(pmcraid_class)) {
  4622. error = PTR_ERR(pmcraid_class);
  4623. pmcraid_err("failed to register with sysfs, error = %x\n",
  4624. error);
  4625. goto out_unreg_chrdev;
  4626. }
  4627. error = pmcraid_netlink_init();
  4628. if (error) {
  4629. class_destroy(pmcraid_class);
  4630. goto out_unreg_chrdev;
  4631. }
  4632. error = pci_register_driver(&pmcraid_driver);
  4633. if (error == 0)
  4634. goto out_init;
  4635. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  4636. error);
  4637. class_destroy(pmcraid_class);
  4638. pmcraid_netlink_release();
  4639. out_unreg_chrdev:
  4640. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  4641. out_init:
  4642. return error;
  4643. }
  4644. /**
  4645. * pmcraid_exit - module unload entry point
  4646. */
  4647. static void __exit pmcraid_exit(void)
  4648. {
  4649. pmcraid_netlink_release();
  4650. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  4651. PMCRAID_MAX_ADAPTERS);
  4652. pci_unregister_driver(&pmcraid_driver);
  4653. class_destroy(pmcraid_class);
  4654. }
  4655. module_init(pmcraid_init);
  4656. module_exit(pmcraid_exit);