megaraid_mbox.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * FILE : megaraid_mbox.c
  13. * Version : v2.20.4.6 (Mar 07 2005)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsil.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
  18. * Manoj Jose <Manoj.Jose@lsil.com>
  19. *
  20. * List of supported controllers
  21. *
  22. * OEM Product Name VID DID SSVID SSID
  23. * --- ------------ --- --- ---- ----
  24. * Dell PERC3/QC 101E 1960 1028 0471
  25. * Dell PERC3/DC 101E 1960 1028 0493
  26. * Dell PERC3/SC 101E 1960 1028 0475
  27. * Dell PERC3/Di 1028 1960 1028 0123
  28. * Dell PERC4/SC 1000 1960 1028 0520
  29. * Dell PERC4/DC 1000 1960 1028 0518
  30. * Dell PERC4/QC 1000 0407 1028 0531
  31. * Dell PERC4/Di 1028 000F 1028 014A
  32. * Dell PERC 4e/Si 1028 0013 1028 016c
  33. * Dell PERC 4e/Di 1028 0013 1028 016d
  34. * Dell PERC 4e/Di 1028 0013 1028 016e
  35. * Dell PERC 4e/Di 1028 0013 1028 016f
  36. * Dell PERC 4e/Di 1028 0013 1028 0170
  37. * Dell PERC 4e/DC 1000 0408 1028 0002
  38. * Dell PERC 4e/SC 1000 0408 1028 0001
  39. *
  40. *
  41. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  42. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  43. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  44. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  45. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  46. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  47. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  48. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  49. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  50. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  51. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  52. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  53. *
  54. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  55. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  56. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  57. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  58. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  59. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  61. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  62. *
  63. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  64. *
  65. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  66. *
  67. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  68. *
  69. * For history of changes, see Documentation/ChangeLog.megaraid
  70. */
  71. #include "megaraid_mbox.h"
  72. static int megaraid_init(void);
  73. static void megaraid_exit(void);
  74. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  75. static void megaraid_detach_one(struct pci_dev *);
  76. static void megaraid_mbox_shutdown(struct pci_dev *);
  77. static int megaraid_io_attach(adapter_t *);
  78. static void megaraid_io_detach(adapter_t *);
  79. static int megaraid_init_mbox(adapter_t *);
  80. static void megaraid_fini_mbox(adapter_t *);
  81. static int megaraid_alloc_cmd_packets(adapter_t *);
  82. static void megaraid_free_cmd_packets(adapter_t *);
  83. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  84. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  85. static int megaraid_sysfs_alloc_resources(adapter_t *);
  86. static void megaraid_sysfs_free_resources(adapter_t *);
  87. static int megaraid_abort_handler(struct scsi_cmnd *);
  88. static int megaraid_reset_handler(struct scsi_cmnd *);
  89. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  90. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  91. static int megaraid_busywait_mbox(mraid_device_t *);
  92. static int megaraid_mbox_product_info(adapter_t *);
  93. static int megaraid_mbox_extended_cdb(adapter_t *);
  94. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  95. static int megaraid_mbox_support_random_del(adapter_t *);
  96. static int megaraid_mbox_get_max_sg(adapter_t *);
  97. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  98. static void megaraid_mbox_flush_cache(adapter_t *);
  99. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  100. static void megaraid_mbox_setup_device_map(adapter_t *);
  101. static int megaraid_queue_command(struct scsi_cmnd *,
  102. void (*)(struct scsi_cmnd *));
  103. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  104. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  105. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  106. struct scsi_cmnd *);
  107. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  108. struct scsi_cmnd *);
  109. static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
  110. static void megaraid_mbox_dpc(unsigned long);
  111. static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
  112. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  113. static int megaraid_cmm_register(adapter_t *);
  114. static int megaraid_cmm_unregister(adapter_t *);
  115. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  116. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  117. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  118. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  119. static int wait_till_fw_empty(adapter_t *);
  120. MODULE_AUTHOR("LSI Logic Corporation");
  121. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  122. MODULE_LICENSE("GPL");
  123. MODULE_VERSION(MEGARAID_VERSION);
  124. /*
  125. * ### modules parameters for driver ###
  126. */
  127. /**
  128. * Set to enable driver to expose unconfigured disk to kernel
  129. */
  130. static int megaraid_expose_unconf_disks = 0;
  131. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  132. MODULE_PARM_DESC(unconf_disks,
  133. "Set to expose unconfigured disks to kernel (default=0)");
  134. /**
  135. * driver wait time if the adapter's mailbox is busy
  136. */
  137. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  138. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  139. MODULE_PARM_DESC(busy_wait,
  140. "Max wait for mailbox in microseconds if busy (default=10)");
  141. /**
  142. * number of sectors per IO command
  143. */
  144. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  145. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  146. MODULE_PARM_DESC(max_sectors,
  147. "Maximum number of sectors per IO command (default=128)");
  148. /**
  149. * number of commands per logical unit
  150. */
  151. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  152. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  153. MODULE_PARM_DESC(cmd_per_lun,
  154. "Maximum number of commands per logical unit (default=64)");
  155. /**
  156. * Fast driver load option, skip scanning for physical devices during load.
  157. * This would result in non-disk devices being skipped during driver load
  158. * time. These can be later added though, using /proc/scsi/scsi
  159. */
  160. static unsigned int megaraid_fast_load = 0;
  161. module_param_named(fast_load, megaraid_fast_load, int, 0);
  162. MODULE_PARM_DESC(fast_load,
  163. "Faster loading of the driver, skips physical devices! (default=0)");
  164. /**
  165. * mraid_debug level - threshold for amount of information to be displayed by
  166. * the driver. This level can be changed through modules parameters, ioctl or
  167. * sysfs/proc interface. By default, print the announcement messages only.
  168. */
  169. int mraid_debug_level = CL_ANN;
  170. module_param_named(debug_level, mraid_debug_level, int, 0);
  171. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  172. /*
  173. * ### global data ###
  174. */
  175. static uint8_t megaraid_mbox_version[8] =
  176. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  177. /*
  178. * PCI table for all supported controllers.
  179. */
  180. static struct pci_device_id pci_id_table_g[] = {
  181. {
  182. PCI_VENDOR_ID_DELL,
  183. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  184. PCI_VENDOR_ID_DELL,
  185. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  186. },
  187. {
  188. PCI_VENDOR_ID_LSI_LOGIC,
  189. PCI_DEVICE_ID_PERC4_SC,
  190. PCI_VENDOR_ID_DELL,
  191. PCI_SUBSYS_ID_PERC4_SC,
  192. },
  193. {
  194. PCI_VENDOR_ID_LSI_LOGIC,
  195. PCI_DEVICE_ID_PERC4_DC,
  196. PCI_VENDOR_ID_DELL,
  197. PCI_SUBSYS_ID_PERC4_DC,
  198. },
  199. {
  200. PCI_VENDOR_ID_LSI_LOGIC,
  201. PCI_DEVICE_ID_VERDE,
  202. PCI_ANY_ID,
  203. PCI_ANY_ID,
  204. },
  205. {
  206. PCI_VENDOR_ID_DELL,
  207. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  208. PCI_VENDOR_ID_DELL,
  209. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  210. },
  211. {
  212. PCI_VENDOR_ID_DELL,
  213. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  214. PCI_VENDOR_ID_DELL,
  215. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  216. },
  217. {
  218. PCI_VENDOR_ID_DELL,
  219. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  220. PCI_VENDOR_ID_DELL,
  221. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  222. },
  223. {
  224. PCI_VENDOR_ID_DELL,
  225. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  226. PCI_VENDOR_ID_DELL,
  227. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  228. },
  229. {
  230. PCI_VENDOR_ID_DELL,
  231. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  232. PCI_VENDOR_ID_DELL,
  233. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  234. },
  235. {
  236. PCI_VENDOR_ID_DELL,
  237. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  238. PCI_VENDOR_ID_DELL,
  239. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  240. },
  241. {
  242. PCI_VENDOR_ID_LSI_LOGIC,
  243. PCI_DEVICE_ID_DOBSON,
  244. PCI_ANY_ID,
  245. PCI_ANY_ID,
  246. },
  247. {
  248. PCI_VENDOR_ID_AMI,
  249. PCI_DEVICE_ID_AMI_MEGARAID3,
  250. PCI_VENDOR_ID_DELL,
  251. PCI_SUBSYS_ID_PERC3_QC,
  252. },
  253. {
  254. PCI_VENDOR_ID_AMI,
  255. PCI_DEVICE_ID_AMI_MEGARAID3,
  256. PCI_VENDOR_ID_DELL,
  257. PCI_SUBSYS_ID_PERC3_DC,
  258. },
  259. {
  260. PCI_VENDOR_ID_AMI,
  261. PCI_DEVICE_ID_AMI_MEGARAID3,
  262. PCI_VENDOR_ID_DELL,
  263. PCI_SUBSYS_ID_PERC3_SC,
  264. },
  265. {
  266. PCI_VENDOR_ID_AMI,
  267. PCI_DEVICE_ID_AMI_MEGARAID3,
  268. PCI_VENDOR_ID_AMI,
  269. PCI_SUBSYS_ID_PERC3_SC,
  270. },
  271. {
  272. PCI_VENDOR_ID_AMI,
  273. PCI_DEVICE_ID_AMI_MEGARAID3,
  274. PCI_VENDOR_ID_AMI,
  275. PCI_SUBSYS_ID_PERC3_DC,
  276. },
  277. {
  278. PCI_VENDOR_ID_LSI_LOGIC,
  279. PCI_DEVICE_ID_MEGARAID_SCSI_320_0,
  280. PCI_VENDOR_ID_LSI_LOGIC,
  281. PCI_SUBSYS_ID_MEGARAID_SCSI_320_0,
  282. },
  283. {
  284. PCI_VENDOR_ID_LSI_LOGIC,
  285. PCI_DEVICE_ID_MEGARAID_SCSI_320_1,
  286. PCI_VENDOR_ID_LSI_LOGIC,
  287. PCI_SUBSYS_ID_MEGARAID_SCSI_320_1,
  288. },
  289. {
  290. PCI_VENDOR_ID_LSI_LOGIC,
  291. PCI_DEVICE_ID_MEGARAID_SCSI_320_2,
  292. PCI_VENDOR_ID_LSI_LOGIC,
  293. PCI_SUBSYS_ID_MEGARAID_SCSI_320_2,
  294. },
  295. {
  296. PCI_VENDOR_ID_LSI_LOGIC,
  297. PCI_DEVICE_ID_MEGARAID_I4_133_RAID,
  298. PCI_VENDOR_ID_LSI_LOGIC,
  299. PCI_SUBSYS_ID_MEGARAID_I4_133_RAID,
  300. },
  301. {
  302. PCI_VENDOR_ID_LSI_LOGIC,
  303. PCI_DEVICE_ID_MEGARAID_SATA_150_4,
  304. PCI_VENDOR_ID_LSI_LOGIC,
  305. PCI_SUBSYS_ID_MEGARAID_SATA_150_4,
  306. },
  307. {
  308. PCI_VENDOR_ID_LSI_LOGIC,
  309. PCI_DEVICE_ID_MEGARAID_SATA_150_6,
  310. PCI_VENDOR_ID_LSI_LOGIC,
  311. PCI_SUBSYS_ID_MEGARAID_SATA_150_6,
  312. },
  313. {
  314. PCI_VENDOR_ID_LSI_LOGIC,
  315. PCI_DEVICE_ID_LINDSAY,
  316. PCI_ANY_ID,
  317. PCI_ANY_ID,
  318. },
  319. {
  320. PCI_VENDOR_ID_LSI_LOGIC,
  321. PCI_DEVICE_ID_INTEL_RAID_SRCS16,
  322. PCI_VENDOR_ID_INTEL,
  323. PCI_SUBSYS_ID_INTEL_RAID_SRCS16,
  324. },
  325. {
  326. PCI_VENDOR_ID_LSI_LOGIC,
  327. PCI_DEVICE_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
  328. PCI_VENDOR_ID_INTEL,
  329. PCI_SUBSYS_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
  330. },
  331. {0} /* Terminating entry */
  332. };
  333. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  334. static struct pci_driver megaraid_pci_driver_g = {
  335. .name = "megaraid",
  336. .id_table = pci_id_table_g,
  337. .probe = megaraid_probe_one,
  338. .remove = __devexit_p(megaraid_detach_one),
  339. .shutdown = megaraid_mbox_shutdown,
  340. };
  341. // definitions for the device attributes for exporting logical drive number
  342. // for a scsi address (Host, Channel, Id, Lun)
  343. CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  344. NULL);
  345. // Host template initializer for megaraid mbox sysfs device attributes
  346. static struct class_device_attribute *megaraid_shost_attrs[] = {
  347. &class_device_attr_megaraid_mbox_app_hndl,
  348. NULL,
  349. };
  350. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  351. // Host template initializer for megaraid mbox sysfs device attributes
  352. static struct device_attribute *megaraid_sdev_attrs[] = {
  353. &dev_attr_megaraid_mbox_ld,
  354. NULL,
  355. };
  356. /*
  357. * Scsi host template for megaraid unified driver
  358. */
  359. static struct scsi_host_template megaraid_template_g = {
  360. .module = THIS_MODULE,
  361. .name = "LSI Logic MegaRAID driver",
  362. .proc_name = "megaraid",
  363. .queuecommand = megaraid_queue_command,
  364. .eh_abort_handler = megaraid_abort_handler,
  365. .eh_device_reset_handler = megaraid_reset_handler,
  366. .eh_bus_reset_handler = megaraid_reset_handler,
  367. .eh_host_reset_handler = megaraid_reset_handler,
  368. .use_clustering = ENABLE_CLUSTERING,
  369. .sdev_attrs = megaraid_sdev_attrs,
  370. .shost_attrs = megaraid_shost_attrs,
  371. };
  372. /**
  373. * megaraid_init - module load hook
  374. *
  375. * We register ourselves as hotplug enabled module and let PCI subsystem
  376. * discover our adaters
  377. **/
  378. static int __init
  379. megaraid_init(void)
  380. {
  381. int rval;
  382. // Announce the driver version
  383. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  384. MEGARAID_EXT_VERSION));
  385. // check validity of module parameters
  386. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  387. con_log(CL_ANN, (KERN_WARNING
  388. "megaraid mailbox: max commands per lun reset to %d\n",
  389. MBOX_MAX_SCSI_CMDS));
  390. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  391. }
  392. // register as a PCI hot-plug driver module
  393. rval = pci_register_driver(&megaraid_pci_driver_g);
  394. if (rval < 0) {
  395. con_log(CL_ANN, (KERN_WARNING
  396. "megaraid: could not register hotplug support.\n"));
  397. }
  398. return rval;
  399. }
  400. /**
  401. * megaraid_exit - driver unload entry point
  402. *
  403. * We simply unwrap the megaraid_init routine here
  404. */
  405. static void __exit
  406. megaraid_exit(void)
  407. {
  408. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  409. // unregister as PCI hotplug driver
  410. pci_unregister_driver(&megaraid_pci_driver_g);
  411. return;
  412. }
  413. /**
  414. * megaraid_probe_one - PCI hotplug entry point
  415. * @param pdev : handle to this controller's PCI configuration space
  416. * @param id : pci device id of the class of controllers
  417. *
  418. * This routine should be called whenever a new adapter is detected by the
  419. * PCI hotplug susbsytem.
  420. **/
  421. static int __devinit
  422. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  423. {
  424. adapter_t *adapter;
  425. // detected a new controller
  426. con_log(CL_ANN, (KERN_INFO
  427. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  428. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  429. pdev->subsystem_device));
  430. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  431. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  432. if (pci_enable_device(pdev)) {
  433. con_log(CL_ANN, (KERN_WARNING
  434. "megaraid: pci_enable_device failed\n"));
  435. return -ENODEV;
  436. }
  437. // Enable bus-mastering on this controller
  438. pci_set_master(pdev);
  439. // Allocate the per driver initialization structure
  440. adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
  441. if (adapter == NULL) {
  442. con_log(CL_ANN, (KERN_WARNING
  443. "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
  444. goto out_probe_one;
  445. }
  446. memset(adapter, 0, sizeof(adapter_t));
  447. // set up PCI related soft state and other pre-known parameters
  448. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  449. adapter->irq = pdev->irq;
  450. adapter->pdev = pdev;
  451. atomic_set(&adapter->being_detached, 0);
  452. // Setup the default DMA mask. This would be changed later on
  453. // depending on hardware capabilities
  454. if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
  455. con_log(CL_ANN, (KERN_WARNING
  456. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  457. goto out_free_adapter;
  458. }
  459. // Initialize the synchronization lock for kernel and LLD
  460. spin_lock_init(&adapter->lock);
  461. adapter->host_lock = &adapter->lock;
  462. // Initialize the command queues: the list of free SCBs and the list
  463. // of pending SCBs.
  464. INIT_LIST_HEAD(&adapter->kscb_pool);
  465. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  466. INIT_LIST_HEAD(&adapter->pend_list);
  467. spin_lock_init(PENDING_LIST_LOCK(adapter));
  468. INIT_LIST_HEAD(&adapter->completed_list);
  469. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  470. // Start the mailbox based controller
  471. if (megaraid_init_mbox(adapter) != 0) {
  472. con_log(CL_ANN, (KERN_WARNING
  473. "megaraid: maibox adapter did not initialize\n"));
  474. goto out_free_adapter;
  475. }
  476. // Register with LSI Common Management Module
  477. if (megaraid_cmm_register(adapter) != 0) {
  478. con_log(CL_ANN, (KERN_WARNING
  479. "megaraid: could not register with management module\n"));
  480. goto out_fini_mbox;
  481. }
  482. // setup adapter handle in PCI soft state
  483. pci_set_drvdata(pdev, adapter);
  484. // attach with scsi mid-layer
  485. if (megaraid_io_attach(adapter) != 0) {
  486. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  487. goto out_cmm_unreg;
  488. }
  489. return 0;
  490. out_cmm_unreg:
  491. pci_set_drvdata(pdev, NULL);
  492. megaraid_cmm_unregister(adapter);
  493. out_fini_mbox:
  494. megaraid_fini_mbox(adapter);
  495. out_free_adapter:
  496. kfree(adapter);
  497. out_probe_one:
  498. pci_disable_device(pdev);
  499. return -ENODEV;
  500. }
  501. /**
  502. * megaraid_detach_one - release the framework resources and call LLD release
  503. * routine
  504. * @param pdev : handle for our PCI cofiguration space
  505. *
  506. * This routine is called during driver unload. We free all the allocated
  507. * resources and call the corresponding LLD so that it can also release all
  508. * its resources.
  509. *
  510. * This routine is also called from the PCI hotplug system
  511. **/
  512. static void
  513. megaraid_detach_one(struct pci_dev *pdev)
  514. {
  515. adapter_t *adapter;
  516. struct Scsi_Host *host;
  517. // Start a rollback on this adapter
  518. adapter = pci_get_drvdata(pdev);
  519. if (!adapter) {
  520. con_log(CL_ANN, (KERN_CRIT
  521. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  522. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  523. pdev->subsystem_device));
  524. return;
  525. }
  526. else {
  527. con_log(CL_ANN, (KERN_NOTICE
  528. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  529. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  530. pdev->subsystem_device));
  531. }
  532. host = adapter->host;
  533. // do not allow any more requests from the management module for this
  534. // adapter.
  535. // FIXME: How do we account for the request which might still be
  536. // pending with us?
  537. atomic_set(&adapter->being_detached, 1);
  538. // detach from the IO sub-system
  539. megaraid_io_detach(adapter);
  540. // reset the device state in the PCI structure. We check this
  541. // condition when we enter here. If the device state is NULL,
  542. // that would mean the device has already been removed
  543. pci_set_drvdata(pdev, NULL);
  544. // Unregister from common management module
  545. //
  546. // FIXME: this must return success or failure for conditions if there
  547. // is a command pending with LLD or not.
  548. megaraid_cmm_unregister(adapter);
  549. // finalize the mailbox based controller and release all resources
  550. megaraid_fini_mbox(adapter);
  551. kfree(adapter);
  552. scsi_host_put(host);
  553. pci_disable_device(pdev);
  554. return;
  555. }
  556. /**
  557. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  558. * @param device : generice driver model device
  559. *
  560. * Shutdown notification, perform flush cache
  561. */
  562. static void
  563. megaraid_mbox_shutdown(struct pci_dev *pdev)
  564. {
  565. adapter_t *adapter = pci_get_drvdata(pdev);
  566. static int counter;
  567. if (!adapter) {
  568. con_log(CL_ANN, (KERN_WARNING
  569. "megaraid: null device in shutdown\n"));
  570. return;
  571. }
  572. // flush caches now
  573. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  574. counter++));
  575. megaraid_mbox_flush_cache(adapter);
  576. con_log(CL_ANN, ("done\n"));
  577. }
  578. /**
  579. * megaraid_io_attach - attach a device with the IO subsystem
  580. * @param adapter : controller's soft state
  581. *
  582. * Attach this device with the IO subsystem
  583. **/
  584. static int
  585. megaraid_io_attach(adapter_t *adapter)
  586. {
  587. struct Scsi_Host *host;
  588. // Initialize SCSI Host structure
  589. host = scsi_host_alloc(&megaraid_template_g, 8);
  590. if (!host) {
  591. con_log(CL_ANN, (KERN_WARNING
  592. "megaraid mbox: scsi_register failed\n"));
  593. return -1;
  594. }
  595. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  596. adapter->host = host;
  597. // export the parameters required by the mid-layer
  598. scsi_assign_lock(host, adapter->host_lock);
  599. host->irq = adapter->irq;
  600. host->unique_id = adapter->unique_id;
  601. host->can_queue = adapter->max_cmds;
  602. host->this_id = adapter->init_id;
  603. host->sg_tablesize = adapter->sglen;
  604. host->max_sectors = adapter->max_sectors;
  605. host->cmd_per_lun = adapter->cmd_per_lun;
  606. host->max_channel = adapter->max_channel;
  607. host->max_id = adapter->max_target;
  608. host->max_lun = adapter->max_lun;
  609. // notify mid-layer about the new controller
  610. if (scsi_add_host(host, &adapter->pdev->dev)) {
  611. con_log(CL_ANN, (KERN_WARNING
  612. "megaraid mbox: scsi_add_host failed\n"));
  613. scsi_host_put(host);
  614. return -1;
  615. }
  616. scsi_scan_host(host);
  617. return 0;
  618. }
  619. /**
  620. * megaraid_io_detach - detach a device from the IO subsystem
  621. * @param adapter : controller's soft state
  622. *
  623. * Detach this device from the IO subsystem
  624. **/
  625. static void
  626. megaraid_io_detach(adapter_t *adapter)
  627. {
  628. struct Scsi_Host *host;
  629. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  630. host = adapter->host;
  631. scsi_remove_host(host);
  632. return;
  633. }
  634. /*
  635. * START: Mailbox Low Level Driver
  636. *
  637. * This is section specific to the single mailbox based controllers
  638. */
  639. /**
  640. * megaraid_init_mbox - initialize controller
  641. * @param adapter - our soft state
  642. *
  643. * . Allocate 16-byte aligned mailbox memory for firmware handshake
  644. * . Allocate controller's memory resources
  645. * . Find out all initialization data
  646. * . Allocate memory required for all the commands
  647. * . Use internal library of FW routines, build up complete soft state
  648. */
  649. static int __init
  650. megaraid_init_mbox(adapter_t *adapter)
  651. {
  652. struct pci_dev *pdev;
  653. mraid_device_t *raid_dev;
  654. int i;
  655. adapter->ito = MBOX_TIMEOUT;
  656. pdev = adapter->pdev;
  657. /*
  658. * Allocate and initialize the init data structure for mailbox
  659. * controllers
  660. */
  661. raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
  662. if (raid_dev == NULL) return -1;
  663. memset(raid_dev, 0, sizeof(mraid_device_t));
  664. /*
  665. * Attach the adapter soft state to raid device soft state
  666. */
  667. adapter->raid_device = (caddr_t)raid_dev;
  668. raid_dev->fast_load = megaraid_fast_load;
  669. // our baseport
  670. raid_dev->baseport = pci_resource_start(pdev, 0);
  671. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  672. con_log(CL_ANN, (KERN_WARNING
  673. "megaraid: mem region busy\n"));
  674. goto out_free_raid_dev;
  675. }
  676. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  677. if (!raid_dev->baseaddr) {
  678. con_log(CL_ANN, (KERN_WARNING
  679. "megaraid: could not map hba memory\n") );
  680. goto out_release_regions;
  681. }
  682. //
  683. // Setup the rest of the soft state using the library of FW routines
  684. //
  685. // request IRQ and register the interrupt service routine
  686. if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
  687. adapter)) {
  688. con_log(CL_ANN, (KERN_WARNING
  689. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  690. goto out_iounmap;
  691. }
  692. // initialize the mutual exclusion lock for the mailbox
  693. spin_lock_init(&raid_dev->mailbox_lock);
  694. // allocate memory required for commands
  695. if (megaraid_alloc_cmd_packets(adapter) != 0) {
  696. goto out_free_irq;
  697. }
  698. // Product info
  699. if (megaraid_mbox_product_info(adapter) != 0) {
  700. goto out_alloc_cmds;
  701. }
  702. // Do we support extended CDBs
  703. adapter->max_cdb_sz = 10;
  704. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  705. adapter->max_cdb_sz = 16;
  706. }
  707. /*
  708. * Do we support cluster environment, if we do, what is the initiator
  709. * id.
  710. * NOTE: In a non-cluster aware firmware environment, the LLD should
  711. * return 7 as initiator id.
  712. */
  713. adapter->ha = 0;
  714. adapter->init_id = -1;
  715. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  716. adapter->ha = 1;
  717. }
  718. /*
  719. * Prepare the device ids array to have the mapping between the kernel
  720. * device address and megaraid device address.
  721. * We export the physical devices on their actual addresses. The
  722. * logical drives are exported on a virtual SCSI channel
  723. */
  724. megaraid_mbox_setup_device_map(adapter);
  725. // If the firmware supports random deletion, update the device id map
  726. if (megaraid_mbox_support_random_del(adapter)) {
  727. // Change the logical drives numbers in device_ids array one
  728. // slot in device_ids is reserved for target id, that's why
  729. // "<=" below
  730. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  731. adapter->device_ids[adapter->max_channel][i] += 0x80;
  732. }
  733. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  734. 0xFF;
  735. raid_dev->random_del_supported = 1;
  736. }
  737. /*
  738. * find out the maximum number of scatter-gather elements supported by
  739. * this firmware
  740. */
  741. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  742. // enumerate RAID and SCSI channels so that all devices on SCSI
  743. // channels can later be exported, including disk devices
  744. megaraid_mbox_enum_raid_scsi(adapter);
  745. /*
  746. * Other parameters required by upper layer
  747. *
  748. * maximum number of sectors per IO command
  749. */
  750. adapter->max_sectors = megaraid_max_sectors;
  751. /*
  752. * number of queued commands per LUN.
  753. */
  754. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  755. /*
  756. * Allocate resources required to issue FW calls, when sysfs is
  757. * accessed
  758. */
  759. if (megaraid_sysfs_alloc_resources(adapter) != 0) {
  760. goto out_alloc_cmds;
  761. }
  762. // Set the DMA mask to 64-bit. All supported controllers as capable of
  763. // DMA in this range
  764. if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
  765. con_log(CL_ANN, (KERN_WARNING
  766. "megaraid: could not set DMA mask for 64-bit.\n"));
  767. goto out_free_sysfs_res;
  768. }
  769. // setup tasklet for DPC
  770. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  771. (unsigned long)adapter);
  772. con_log(CL_DLEVEL1, (KERN_INFO
  773. "megaraid mbox hba successfully initialized\n"));
  774. return 0;
  775. out_free_sysfs_res:
  776. megaraid_sysfs_free_resources(adapter);
  777. out_alloc_cmds:
  778. megaraid_free_cmd_packets(adapter);
  779. out_free_irq:
  780. free_irq(adapter->irq, adapter);
  781. out_iounmap:
  782. iounmap(raid_dev->baseaddr);
  783. out_release_regions:
  784. pci_release_regions(pdev);
  785. out_free_raid_dev:
  786. kfree(raid_dev);
  787. return -1;
  788. }
  789. /**
  790. * megaraid_fini_mbox - undo controller initialization
  791. * @param adapter : our soft state
  792. */
  793. static void
  794. megaraid_fini_mbox(adapter_t *adapter)
  795. {
  796. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  797. // flush all caches
  798. megaraid_mbox_flush_cache(adapter);
  799. tasklet_kill(&adapter->dpc_h);
  800. megaraid_sysfs_free_resources(adapter);
  801. megaraid_free_cmd_packets(adapter);
  802. free_irq(adapter->irq, adapter);
  803. iounmap(raid_dev->baseaddr);
  804. pci_release_regions(adapter->pdev);
  805. kfree(raid_dev);
  806. return;
  807. }
  808. /**
  809. * megaraid_alloc_cmd_packets - allocate shared mailbox
  810. * @param adapter : soft state of the raid controller
  811. *
  812. * Allocate and align the shared mailbox. This maibox is used to issue
  813. * all the commands. For IO based controllers, the mailbox is also regsitered
  814. * with the FW. Allocate memory for all commands as well.
  815. * This is our big allocator
  816. */
  817. static int
  818. megaraid_alloc_cmd_packets(adapter_t *adapter)
  819. {
  820. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  821. struct pci_dev *pdev;
  822. unsigned long align;
  823. scb_t *scb;
  824. mbox_ccb_t *ccb;
  825. struct mraid_pci_blk *epthru_pci_blk;
  826. struct mraid_pci_blk *sg_pci_blk;
  827. struct mraid_pci_blk *mbox_pci_blk;
  828. int i;
  829. pdev = adapter->pdev;
  830. /*
  831. * Setup the mailbox
  832. * Allocate the common 16-byte aligned memory for the handshake
  833. * mailbox.
  834. */
  835. raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
  836. sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
  837. if (!raid_dev->una_mbox64) {
  838. con_log(CL_ANN, (KERN_WARNING
  839. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  840. __LINE__));
  841. return -1;
  842. }
  843. memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
  844. /*
  845. * Align the mailbox at 16-byte boundary
  846. */
  847. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  848. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  849. (~0UL ^ 0xFUL));
  850. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  851. align = ((void *)raid_dev->mbox -
  852. ((void *)&raid_dev->una_mbox64->mbox32));
  853. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  854. align;
  855. // Allocate memory for commands issued internally
  856. adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
  857. &adapter->ibuf_dma_h);
  858. if (!adapter->ibuf) {
  859. con_log(CL_ANN, (KERN_WARNING
  860. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  861. __LINE__));
  862. goto out_free_common_mbox;
  863. }
  864. memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
  865. // Allocate memory for our SCSI Command Blocks and their associated
  866. // memory
  867. /*
  868. * Allocate memory for the base list of scb. Later allocate memory for
  869. * CCBs and embedded components of each CCB and point the pointers in
  870. * scb to the allocated components
  871. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  872. * since the calling routine does not yet know the number of available
  873. * commands.
  874. */
  875. adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
  876. GFP_KERNEL);
  877. if (adapter->kscb_list == NULL) {
  878. con_log(CL_ANN, (KERN_WARNING
  879. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  880. __LINE__));
  881. goto out_free_ibuf;
  882. }
  883. memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
  884. // memory allocation for our command packets
  885. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  886. con_log(CL_ANN, (KERN_WARNING
  887. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  888. __LINE__));
  889. goto out_free_scb_list;
  890. }
  891. // Adjust the scb pointers and link in the free pool
  892. epthru_pci_blk = raid_dev->epthru_pool;
  893. sg_pci_blk = raid_dev->sg_pool;
  894. mbox_pci_blk = raid_dev->mbox_pool;
  895. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  896. scb = adapter->kscb_list + i;
  897. ccb = raid_dev->ccb_list + i;
  898. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  899. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  900. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  901. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  902. // make sure the mailbox is aligned properly
  903. if (ccb->mbox_dma_h & 0x0F) {
  904. con_log(CL_ANN, (KERN_CRIT
  905. "megaraid mbox: not aligned on 16-bytes\n"));
  906. goto out_teardown_dma_pools;
  907. }
  908. ccb->epthru = (mraid_epassthru_t *)
  909. epthru_pci_blk[i].vaddr;
  910. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  911. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  912. ccb->pthru_dma_h = ccb->epthru_dma_h;
  913. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  914. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  915. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  916. scb->ccb = (caddr_t)ccb;
  917. scb->gp = 0;
  918. scb->sno = i; // command index
  919. scb->scp = NULL;
  920. scb->state = SCB_FREE;
  921. scb->dma_direction = PCI_DMA_NONE;
  922. scb->dma_type = MRAID_DMA_NONE;
  923. scb->dev_channel = -1;
  924. scb->dev_target = -1;
  925. // put scb in the free pool
  926. list_add_tail(&scb->list, &adapter->kscb_pool);
  927. }
  928. return 0;
  929. out_teardown_dma_pools:
  930. megaraid_mbox_teardown_dma_pools(adapter);
  931. out_free_scb_list:
  932. kfree(adapter->kscb_list);
  933. out_free_ibuf:
  934. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  935. adapter->ibuf_dma_h);
  936. out_free_common_mbox:
  937. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  938. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  939. return -1;
  940. }
  941. /**
  942. * megaraid_free_cmd_packets - free memory
  943. * @param adapter : soft state of the raid controller
  944. *
  945. * Release memory resources allocated for commands
  946. */
  947. static void
  948. megaraid_free_cmd_packets(adapter_t *adapter)
  949. {
  950. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  951. megaraid_mbox_teardown_dma_pools(adapter);
  952. kfree(adapter->kscb_list);
  953. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  954. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  955. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  956. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  957. return;
  958. }
  959. /**
  960. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  961. * @param adapter : HBA soft state
  962. *
  963. * setup the dma pools for mailbox, passthru and extended passthru structures,
  964. * and scatter-gather lists
  965. */
  966. static int
  967. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  968. {
  969. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  970. struct mraid_pci_blk *epthru_pci_blk;
  971. struct mraid_pci_blk *sg_pci_blk;
  972. struct mraid_pci_blk *mbox_pci_blk;
  973. int i;
  974. // Allocate memory for 16-bytes aligned mailboxes
  975. raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
  976. adapter->pdev,
  977. sizeof(mbox64_t) + 16,
  978. 16, 0);
  979. if (raid_dev->mbox_pool_handle == NULL) {
  980. goto fail_setup_dma_pool;
  981. }
  982. mbox_pci_blk = raid_dev->mbox_pool;
  983. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  984. mbox_pci_blk[i].vaddr = pci_pool_alloc(
  985. raid_dev->mbox_pool_handle,
  986. GFP_KERNEL,
  987. &mbox_pci_blk[i].dma_addr);
  988. if (!mbox_pci_blk[i].vaddr) {
  989. goto fail_setup_dma_pool;
  990. }
  991. }
  992. /*
  993. * Allocate memory for each embedded passthru strucuture pointer
  994. * Request for a 128 bytes aligned structure for each passthru command
  995. * structure
  996. * Since passthru and extended passthru commands are exclusive, they
  997. * share common memory pool. Passthru structures piggyback on memory
  998. * allocted to extended passthru since passthru is smaller of the two
  999. */
  1000. raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
  1001. adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
  1002. if (raid_dev->epthru_pool_handle == NULL) {
  1003. goto fail_setup_dma_pool;
  1004. }
  1005. epthru_pci_blk = raid_dev->epthru_pool;
  1006. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1007. epthru_pci_blk[i].vaddr = pci_pool_alloc(
  1008. raid_dev->epthru_pool_handle,
  1009. GFP_KERNEL,
  1010. &epthru_pci_blk[i].dma_addr);
  1011. if (!epthru_pci_blk[i].vaddr) {
  1012. goto fail_setup_dma_pool;
  1013. }
  1014. }
  1015. // Allocate memory for each scatter-gather list. Request for 512 bytes
  1016. // alignment for each sg list
  1017. raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
  1018. adapter->pdev,
  1019. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  1020. 512, 0);
  1021. if (raid_dev->sg_pool_handle == NULL) {
  1022. goto fail_setup_dma_pool;
  1023. }
  1024. sg_pci_blk = raid_dev->sg_pool;
  1025. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1026. sg_pci_blk[i].vaddr = pci_pool_alloc(
  1027. raid_dev->sg_pool_handle,
  1028. GFP_KERNEL,
  1029. &sg_pci_blk[i].dma_addr);
  1030. if (!sg_pci_blk[i].vaddr) {
  1031. goto fail_setup_dma_pool;
  1032. }
  1033. }
  1034. return 0;
  1035. fail_setup_dma_pool:
  1036. megaraid_mbox_teardown_dma_pools(adapter);
  1037. return -1;
  1038. }
  1039. /**
  1040. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  1041. * @param adapter : HBA soft state
  1042. *
  1043. * teardown the dma pool for mailbox, passthru and extended passthru
  1044. * structures, and scatter-gather lists
  1045. */
  1046. static void
  1047. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  1048. {
  1049. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1050. struct mraid_pci_blk *epthru_pci_blk;
  1051. struct mraid_pci_blk *sg_pci_blk;
  1052. struct mraid_pci_blk *mbox_pci_blk;
  1053. int i;
  1054. sg_pci_blk = raid_dev->sg_pool;
  1055. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1056. pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1057. sg_pci_blk[i].dma_addr);
  1058. }
  1059. if (raid_dev->sg_pool_handle)
  1060. pci_pool_destroy(raid_dev->sg_pool_handle);
  1061. epthru_pci_blk = raid_dev->epthru_pool;
  1062. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1063. pci_pool_free(raid_dev->epthru_pool_handle,
  1064. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1065. }
  1066. if (raid_dev->epthru_pool_handle)
  1067. pci_pool_destroy(raid_dev->epthru_pool_handle);
  1068. mbox_pci_blk = raid_dev->mbox_pool;
  1069. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1070. pci_pool_free(raid_dev->mbox_pool_handle,
  1071. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1072. }
  1073. if (raid_dev->mbox_pool_handle)
  1074. pci_pool_destroy(raid_dev->mbox_pool_handle);
  1075. return;
  1076. }
  1077. /**
  1078. * megaraid_alloc_scb - detach and return a scb from the free list
  1079. * @adapter : controller's soft state
  1080. *
  1081. * return the scb from the head of the free list. NULL if there are none
  1082. * available
  1083. **/
  1084. static inline scb_t *
  1085. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1086. {
  1087. struct list_head *head = &adapter->kscb_pool;
  1088. scb_t *scb = NULL;
  1089. unsigned long flags;
  1090. // detach scb from free pool
  1091. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1092. if (list_empty(head)) {
  1093. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1094. return NULL;
  1095. }
  1096. scb = list_entry(head->next, scb_t, list);
  1097. list_del_init(&scb->list);
  1098. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1099. scb->state = SCB_ACTIVE;
  1100. scb->scp = scp;
  1101. scb->dma_type = MRAID_DMA_NONE;
  1102. return scb;
  1103. }
  1104. /**
  1105. * megaraid_dealloc_scb - return the scb to the free pool
  1106. * @adapter : controller's soft state
  1107. * @scb : scb to be freed
  1108. *
  1109. * return the scb back to the free list of scbs. The caller must 'flush' the
  1110. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1111. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1112. * routine.
  1113. **/
  1114. static inline void
  1115. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1116. {
  1117. unsigned long flags;
  1118. // put scb in the free pool
  1119. scb->state = SCB_FREE;
  1120. scb->scp = NULL;
  1121. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1122. list_add(&scb->list, &adapter->kscb_pool);
  1123. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1124. return;
  1125. }
  1126. /**
  1127. * megaraid_mbox_mksgl - make the scatter-gather list
  1128. * @adapter - controller's soft state
  1129. * @scb - scsi control block
  1130. *
  1131. * prepare the scatter-gather list
  1132. */
  1133. static inline int
  1134. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1135. {
  1136. struct scatterlist *sgl;
  1137. mbox_ccb_t *ccb;
  1138. struct page *page;
  1139. unsigned long offset;
  1140. struct scsi_cmnd *scp;
  1141. int sgcnt;
  1142. int i;
  1143. scp = scb->scp;
  1144. ccb = (mbox_ccb_t *)scb->ccb;
  1145. // no mapping required if no data to be transferred
  1146. if (!scp->request_buffer || !scp->request_bufflen)
  1147. return 0;
  1148. if (!scp->use_sg) { /* scatter-gather list not used */
  1149. page = virt_to_page(scp->request_buffer);
  1150. offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
  1151. ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
  1152. scp->request_bufflen,
  1153. scb->dma_direction);
  1154. scb->dma_type = MRAID_DMA_WBUF;
  1155. /*
  1156. * We need to handle special 64-bit commands that need a
  1157. * minimum of 1 SG
  1158. */
  1159. sgcnt = 1;
  1160. ccb->sgl64[0].address = ccb->buf_dma_h;
  1161. ccb->sgl64[0].length = scp->request_bufflen;
  1162. return sgcnt;
  1163. }
  1164. sgl = (struct scatterlist *)scp->request_buffer;
  1165. // The number of sg elements returned must not exceed our limit
  1166. sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
  1167. scb->dma_direction);
  1168. if (sgcnt > adapter->sglen) {
  1169. con_log(CL_ANN, (KERN_CRIT
  1170. "megaraid critical: too many sg elements:%d\n",
  1171. sgcnt));
  1172. BUG();
  1173. }
  1174. scb->dma_type = MRAID_DMA_WSG;
  1175. for (i = 0; i < sgcnt; i++, sgl++) {
  1176. ccb->sgl64[i].address = sg_dma_address(sgl);
  1177. ccb->sgl64[i].length = sg_dma_len(sgl);
  1178. }
  1179. // Return count of SG nodes
  1180. return sgcnt;
  1181. }
  1182. /**
  1183. * mbox_post_cmd - issue a mailbox command
  1184. * @adapter - controller's soft state
  1185. * @scb - command to be issued
  1186. *
  1187. * post the command to the controller if mailbox is availble.
  1188. */
  1189. static inline int
  1190. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1191. {
  1192. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1193. mbox64_t *mbox64;
  1194. mbox_t *mbox;
  1195. mbox_ccb_t *ccb;
  1196. unsigned long flags;
  1197. unsigned int i = 0;
  1198. ccb = (mbox_ccb_t *)scb->ccb;
  1199. mbox = raid_dev->mbox;
  1200. mbox64 = raid_dev->mbox64;
  1201. /*
  1202. * Check for busy mailbox. If it is, return failure - the caller
  1203. * should retry later.
  1204. */
  1205. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1206. if (unlikely(mbox->busy)) {
  1207. do {
  1208. udelay(1);
  1209. i++;
  1210. rmb();
  1211. } while(mbox->busy && (i < max_mbox_busy_wait));
  1212. if (mbox->busy) {
  1213. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1214. return -1;
  1215. }
  1216. }
  1217. // Copy this command's mailbox data into "adapter's" mailbox
  1218. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1219. mbox->cmdid = scb->sno;
  1220. adapter->outstanding_cmds++;
  1221. if (scb->dma_direction == PCI_DMA_TODEVICE) {
  1222. if (!scb->scp->use_sg) { // sg list not used
  1223. pci_dma_sync_single_for_device(adapter->pdev,
  1224. ccb->buf_dma_h,
  1225. scb->scp->request_bufflen,
  1226. PCI_DMA_TODEVICE);
  1227. }
  1228. else {
  1229. pci_dma_sync_sg_for_device(adapter->pdev,
  1230. scb->scp->request_buffer,
  1231. scb->scp->use_sg, PCI_DMA_TODEVICE);
  1232. }
  1233. }
  1234. mbox->busy = 1; // Set busy
  1235. mbox->poll = 0;
  1236. mbox->ack = 0;
  1237. wmb();
  1238. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1239. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1240. return 0;
  1241. }
  1242. /**
  1243. * megaraid_queue_command - generic queue entry point for all LLDs
  1244. * @scp : pointer to the scsi command to be executed
  1245. * @done : callback routine to be called after the cmd has be completed
  1246. *
  1247. * Queue entry point for mailbox based controllers.
  1248. */
  1249. static int
  1250. megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
  1251. {
  1252. adapter_t *adapter;
  1253. scb_t *scb;
  1254. int if_busy;
  1255. adapter = SCP2ADAPTER(scp);
  1256. scp->scsi_done = done;
  1257. scp->result = 0;
  1258. assert_spin_locked(adapter->host_lock);
  1259. spin_unlock(adapter->host_lock);
  1260. /*
  1261. * Allocate and build a SCB request
  1262. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1263. * not allocate scb. We will return non-zero status in that case.
  1264. * NOTE: scb can be null even though certain commands completed
  1265. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1266. * return 0 in that case, and we would do the callback right away.
  1267. */
  1268. if_busy = 0;
  1269. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1270. if (scb) {
  1271. megaraid_mbox_runpendq(adapter, scb);
  1272. }
  1273. spin_lock(adapter->host_lock);
  1274. if (!scb) { // command already completed
  1275. done(scp);
  1276. return 0;
  1277. }
  1278. return if_busy;
  1279. }
  1280. /**
  1281. * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
  1282. * firmware lingua
  1283. * @adapter - controller's soft state
  1284. * @scp - mid-layer scsi command pointer
  1285. * @busy - set if request could not be completed because of lack of
  1286. * resources
  1287. *
  1288. * convert the command issued by mid-layer to format understood by megaraid
  1289. * firmware. We also complete certain command without sending them to firmware
  1290. */
  1291. static scb_t *
  1292. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1293. {
  1294. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1295. int channel;
  1296. int target;
  1297. int islogical;
  1298. mbox_ccb_t *ccb;
  1299. mraid_passthru_t *pthru;
  1300. mbox64_t *mbox64;
  1301. mbox_t *mbox;
  1302. scb_t *scb;
  1303. char skip[] = "skipping";
  1304. char scan[] = "scanning";
  1305. char *ss;
  1306. /*
  1307. * Get the appropriate device map for the device this command is
  1308. * intended for
  1309. */
  1310. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1311. /*
  1312. * Logical drive commands
  1313. */
  1314. if (islogical) {
  1315. switch (scp->cmnd[0]) {
  1316. case TEST_UNIT_READY:
  1317. /*
  1318. * Do we support clustering and is the support enabled
  1319. * If no, return success always
  1320. */
  1321. if (!adapter->ha) {
  1322. scp->result = (DID_OK << 16);
  1323. return NULL;
  1324. }
  1325. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1326. scp->result = (DID_ERROR << 16);
  1327. *busy = 1;
  1328. return NULL;
  1329. }
  1330. scb->dma_direction = scp->sc_data_direction;
  1331. scb->dev_channel = 0xFF;
  1332. scb->dev_target = target;
  1333. ccb = (mbox_ccb_t *)scb->ccb;
  1334. /*
  1335. * The command id will be provided by the command
  1336. * issuance routine
  1337. */
  1338. ccb->raw_mbox[0] = CLUSTER_CMD;
  1339. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1340. ccb->raw_mbox[3] = target;
  1341. return scb;
  1342. case MODE_SENSE:
  1343. if (scp->use_sg) {
  1344. struct scatterlist *sgl;
  1345. caddr_t vaddr;
  1346. sgl = (struct scatterlist *)scp->request_buffer;
  1347. if (sgl->page) {
  1348. vaddr = (caddr_t)
  1349. (page_address((&sgl[0])->page)
  1350. + (&sgl[0])->offset);
  1351. memset(vaddr, 0, scp->cmnd[4]);
  1352. }
  1353. else {
  1354. con_log(CL_ANN, (KERN_WARNING
  1355. "megaraid mailbox: invalid sg:%d\n",
  1356. __LINE__));
  1357. }
  1358. }
  1359. else {
  1360. memset(scp->request_buffer, 0, scp->cmnd[4]);
  1361. }
  1362. scp->result = (DID_OK << 16);
  1363. return NULL;
  1364. case INQUIRY:
  1365. /*
  1366. * Display the channel scan for logical drives
  1367. * Do not display scan for a channel if already done.
  1368. */
  1369. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1370. con_log(CL_ANN, (KERN_INFO
  1371. "scsi[%d]: scanning scsi channel %d",
  1372. adapter->host->host_no,
  1373. SCP2CHANNEL(scp)));
  1374. con_log(CL_ANN, (
  1375. " [virtual] for logical drives\n"));
  1376. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1377. }
  1378. /* Fall through */
  1379. case READ_CAPACITY:
  1380. /*
  1381. * Do not allow LUN > 0 for logical drives and
  1382. * requests for more than 40 logical drives
  1383. */
  1384. if (SCP2LUN(scp)) {
  1385. scp->result = (DID_BAD_TARGET << 16);
  1386. return NULL;
  1387. }
  1388. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1389. scp->result = (DID_BAD_TARGET << 16);
  1390. return NULL;
  1391. }
  1392. /* Allocate a SCB and initialize passthru */
  1393. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1394. scp->result = (DID_ERROR << 16);
  1395. *busy = 1;
  1396. return NULL;
  1397. }
  1398. ccb = (mbox_ccb_t *)scb->ccb;
  1399. scb->dev_channel = 0xFF;
  1400. scb->dev_target = target;
  1401. pthru = ccb->pthru;
  1402. mbox = ccb->mbox;
  1403. mbox64 = ccb->mbox64;
  1404. pthru->timeout = 0;
  1405. pthru->ars = 1;
  1406. pthru->reqsenselen = 14;
  1407. pthru->islogical = 1;
  1408. pthru->logdrv = target;
  1409. pthru->cdblen = scp->cmd_len;
  1410. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1411. mbox->cmd = MBOXCMD_PASSTHRU64;
  1412. scb->dma_direction = scp->sc_data_direction;
  1413. pthru->dataxferlen = scp->request_bufflen;
  1414. pthru->dataxferaddr = ccb->sgl_dma_h;
  1415. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1416. scb);
  1417. mbox->xferaddr = 0xFFFFFFFF;
  1418. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1419. mbox64->xferaddr_hi = 0;
  1420. return scb;
  1421. case READ_6:
  1422. case WRITE_6:
  1423. case READ_10:
  1424. case WRITE_10:
  1425. case READ_12:
  1426. case WRITE_12:
  1427. /*
  1428. * Allocate a SCB and initialize mailbox
  1429. */
  1430. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1431. scp->result = (DID_ERROR << 16);
  1432. *busy = 1;
  1433. return NULL;
  1434. }
  1435. ccb = (mbox_ccb_t *)scb->ccb;
  1436. scb->dev_channel = 0xFF;
  1437. scb->dev_target = target;
  1438. mbox = ccb->mbox;
  1439. mbox64 = ccb->mbox64;
  1440. mbox->logdrv = target;
  1441. /*
  1442. * A little HACK: 2nd bit is zero for all scsi read
  1443. * commands and is set for all scsi write commands
  1444. */
  1445. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1446. MBOXCMD_LREAD64 ;
  1447. /*
  1448. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1449. */
  1450. if (scp->cmd_len == 6) {
  1451. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1452. mbox->lba =
  1453. ((uint32_t)scp->cmnd[1] << 16) |
  1454. ((uint32_t)scp->cmnd[2] << 8) |
  1455. (uint32_t)scp->cmnd[3];
  1456. mbox->lba &= 0x1FFFFF;
  1457. }
  1458. /*
  1459. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1460. */
  1461. else if (scp->cmd_len == 10) {
  1462. mbox->numsectors =
  1463. (uint32_t)scp->cmnd[8] |
  1464. ((uint32_t)scp->cmnd[7] << 8);
  1465. mbox->lba =
  1466. ((uint32_t)scp->cmnd[2] << 24) |
  1467. ((uint32_t)scp->cmnd[3] << 16) |
  1468. ((uint32_t)scp->cmnd[4] << 8) |
  1469. (uint32_t)scp->cmnd[5];
  1470. }
  1471. /*
  1472. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1473. */
  1474. else if (scp->cmd_len == 12) {
  1475. mbox->lba =
  1476. ((uint32_t)scp->cmnd[2] << 24) |
  1477. ((uint32_t)scp->cmnd[3] << 16) |
  1478. ((uint32_t)scp->cmnd[4] << 8) |
  1479. (uint32_t)scp->cmnd[5];
  1480. mbox->numsectors =
  1481. ((uint32_t)scp->cmnd[6] << 24) |
  1482. ((uint32_t)scp->cmnd[7] << 16) |
  1483. ((uint32_t)scp->cmnd[8] << 8) |
  1484. (uint32_t)scp->cmnd[9];
  1485. }
  1486. else {
  1487. con_log(CL_ANN, (KERN_WARNING
  1488. "megaraid: unsupported CDB length\n"));
  1489. megaraid_dealloc_scb(adapter, scb);
  1490. scp->result = (DID_ERROR << 16);
  1491. return NULL;
  1492. }
  1493. scb->dma_direction = scp->sc_data_direction;
  1494. // Calculate Scatter-Gather info
  1495. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1496. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1497. scb);
  1498. mbox->xferaddr = 0xFFFFFFFF;
  1499. mbox64->xferaddr_hi = 0;
  1500. return scb;
  1501. case RESERVE:
  1502. case RELEASE:
  1503. /*
  1504. * Do we support clustering and is the support enabled
  1505. */
  1506. if (!adapter->ha) {
  1507. scp->result = (DID_BAD_TARGET << 16);
  1508. return NULL;
  1509. }
  1510. /*
  1511. * Allocate a SCB and initialize mailbox
  1512. */
  1513. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1514. scp->result = (DID_ERROR << 16);
  1515. *busy = 1;
  1516. return NULL;
  1517. }
  1518. ccb = (mbox_ccb_t *)scb->ccb;
  1519. scb->dev_channel = 0xFF;
  1520. scb->dev_target = target;
  1521. ccb->raw_mbox[0] = CLUSTER_CMD;
  1522. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1523. RESERVE_LD : RELEASE_LD;
  1524. ccb->raw_mbox[3] = target;
  1525. scb->dma_direction = scp->sc_data_direction;
  1526. return scb;
  1527. default:
  1528. scp->result = (DID_BAD_TARGET << 16);
  1529. return NULL;
  1530. }
  1531. }
  1532. else { // Passthru device commands
  1533. // Do not allow access to target id > 15 or LUN > 7
  1534. if (target > 15 || SCP2LUN(scp) > 7) {
  1535. scp->result = (DID_BAD_TARGET << 16);
  1536. return NULL;
  1537. }
  1538. // if fast load option was set and scan for last device is
  1539. // over, reset the fast_load flag so that during a possible
  1540. // next scan, devices can be made available
  1541. if (rdev->fast_load && (target == 15) &&
  1542. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1543. con_log(CL_ANN, (KERN_INFO
  1544. "megaraid[%d]: physical device scan re-enabled\n",
  1545. adapter->host->host_no));
  1546. rdev->fast_load = 0;
  1547. }
  1548. /*
  1549. * Display the channel scan for physical devices
  1550. */
  1551. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1552. ss = rdev->fast_load ? skip : scan;
  1553. con_log(CL_ANN, (KERN_INFO
  1554. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1555. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1556. channel));
  1557. con_log(CL_ANN, (
  1558. " for non-raid devices\n"));
  1559. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1560. }
  1561. // disable channel sweep if fast load option given
  1562. if (rdev->fast_load) {
  1563. scp->result = (DID_BAD_TARGET << 16);
  1564. return NULL;
  1565. }
  1566. // Allocate a SCB and initialize passthru
  1567. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1568. scp->result = (DID_ERROR << 16);
  1569. *busy = 1;
  1570. return NULL;
  1571. }
  1572. ccb = (mbox_ccb_t *)scb->ccb;
  1573. scb->dev_channel = channel;
  1574. scb->dev_target = target;
  1575. scb->dma_direction = scp->sc_data_direction;
  1576. mbox = ccb->mbox;
  1577. mbox64 = ccb->mbox64;
  1578. // Does this firmware support extended CDBs
  1579. if (adapter->max_cdb_sz == 16) {
  1580. mbox->cmd = MBOXCMD_EXTPTHRU;
  1581. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1582. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1583. mbox64->xferaddr_hi = 0;
  1584. mbox->xferaddr = 0xFFFFFFFF;
  1585. }
  1586. else {
  1587. mbox->cmd = MBOXCMD_PASSTHRU64;
  1588. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1589. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1590. mbox64->xferaddr_hi = 0;
  1591. mbox->xferaddr = 0xFFFFFFFF;
  1592. }
  1593. return scb;
  1594. }
  1595. // NOT REACHED
  1596. }
  1597. /**
  1598. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1599. * @adapter : controller's soft state
  1600. * @scb : SCB to be queued in the pending list
  1601. *
  1602. * scan the pending list for commands which are not yet issued and try to
  1603. * post to the controller. The SCB can be a null pointer, which would indicate
  1604. * no SCB to be queue, just try to execute the ones in the pending list.
  1605. *
  1606. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1607. * out from the head of the pending list. If it is successfully issued, the
  1608. * next SCB is at the head now.
  1609. */
  1610. static void
  1611. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1612. {
  1613. scb_t *scb;
  1614. unsigned long flags;
  1615. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1616. if (scb_q) {
  1617. scb_q->state = SCB_PENDQ;
  1618. list_add_tail(&scb_q->list, &adapter->pend_list);
  1619. }
  1620. // if the adapter in not in quiescent mode, post the commands to FW
  1621. if (adapter->quiescent) {
  1622. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1623. return;
  1624. }
  1625. while (!list_empty(&adapter->pend_list)) {
  1626. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1627. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1628. // remove the scb from the pending list and try to
  1629. // issue. If we are unable to issue it, put back in
  1630. // the pending list and return
  1631. list_del_init(&scb->list);
  1632. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1633. // if mailbox was busy, return SCB back to pending
  1634. // list. Make sure to add at the head, since that's
  1635. // where it would have been removed from
  1636. scb->state = SCB_ISSUED;
  1637. if (mbox_post_cmd(adapter, scb) != 0) {
  1638. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1639. scb->state = SCB_PENDQ;
  1640. list_add(&scb->list, &adapter->pend_list);
  1641. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1642. flags);
  1643. return;
  1644. }
  1645. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1646. }
  1647. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1648. return;
  1649. }
  1650. /**
  1651. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1652. * @adapter - pointer to controller's soft state
  1653. * @scb - scsi control block
  1654. * @scp - scsi command from the mid-layer
  1655. *
  1656. * prepare a command for the scsi physical devices
  1657. */
  1658. static void
  1659. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1660. struct scsi_cmnd *scp)
  1661. {
  1662. mbox_ccb_t *ccb;
  1663. mraid_passthru_t *pthru;
  1664. uint8_t channel;
  1665. uint8_t target;
  1666. ccb = (mbox_ccb_t *)scb->ccb;
  1667. pthru = ccb->pthru;
  1668. channel = scb->dev_channel;
  1669. target = scb->dev_target;
  1670. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1671. pthru->timeout = 4;
  1672. pthru->ars = 1;
  1673. pthru->islogical = 0;
  1674. pthru->channel = 0;
  1675. pthru->target = (channel << 4) | target;
  1676. pthru->logdrv = SCP2LUN(scp);
  1677. pthru->reqsenselen = 14;
  1678. pthru->cdblen = scp->cmd_len;
  1679. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1680. if (scp->request_bufflen) {
  1681. pthru->dataxferlen = scp->request_bufflen;
  1682. pthru->dataxferaddr = ccb->sgl_dma_h;
  1683. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1684. }
  1685. else {
  1686. pthru->dataxferaddr = 0;
  1687. pthru->dataxferlen = 0;
  1688. pthru->numsge = 0;
  1689. }
  1690. return;
  1691. }
  1692. /**
  1693. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1694. * @adapter - pointer to controller's soft state
  1695. * @scb - scsi control block
  1696. * @scp - scsi command from the mid-layer
  1697. *
  1698. * prepare a command for the scsi physical devices. This rountine prepares
  1699. * commands for devices which can take extended CDBs (>10 bytes)
  1700. */
  1701. static void
  1702. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1703. struct scsi_cmnd *scp)
  1704. {
  1705. mbox_ccb_t *ccb;
  1706. mraid_epassthru_t *epthru;
  1707. uint8_t channel;
  1708. uint8_t target;
  1709. ccb = (mbox_ccb_t *)scb->ccb;
  1710. epthru = ccb->epthru;
  1711. channel = scb->dev_channel;
  1712. target = scb->dev_target;
  1713. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1714. epthru->timeout = 4;
  1715. epthru->ars = 1;
  1716. epthru->islogical = 0;
  1717. epthru->channel = 0;
  1718. epthru->target = (channel << 4) | target;
  1719. epthru->logdrv = SCP2LUN(scp);
  1720. epthru->reqsenselen = 14;
  1721. epthru->cdblen = scp->cmd_len;
  1722. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1723. if (scp->request_bufflen) {
  1724. epthru->dataxferlen = scp->request_bufflen;
  1725. epthru->dataxferaddr = ccb->sgl_dma_h;
  1726. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1727. }
  1728. else {
  1729. epthru->dataxferaddr = 0;
  1730. epthru->dataxferlen = 0;
  1731. epthru->numsge = 0;
  1732. }
  1733. return;
  1734. }
  1735. /**
  1736. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1737. * @adapter - controller's soft state
  1738. *
  1739. * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
  1740. * completed command and put them on the completed list for later processing.
  1741. *
  1742. * Returns: 1 if the interrupt is valid, 0 otherwise
  1743. */
  1744. static inline int
  1745. megaraid_ack_sequence(adapter_t *adapter)
  1746. {
  1747. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1748. mbox_t *mbox;
  1749. scb_t *scb;
  1750. uint8_t nstatus;
  1751. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1752. struct list_head clist;
  1753. int handled;
  1754. uint32_t dword;
  1755. unsigned long flags;
  1756. int i, j;
  1757. mbox = raid_dev->mbox;
  1758. // move the SCBs from the firmware completed array to our local list
  1759. INIT_LIST_HEAD(&clist);
  1760. // loop till F/W has more commands for us to complete
  1761. handled = 0;
  1762. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1763. do {
  1764. /*
  1765. * Check if a valid interrupt is pending. If found, force the
  1766. * interrupt line low.
  1767. */
  1768. dword = RDOUTDOOR(raid_dev);
  1769. if (dword != 0x10001234) break;
  1770. handled = 1;
  1771. WROUTDOOR(raid_dev, 0x10001234);
  1772. nstatus = 0;
  1773. // wait for valid numstatus to post
  1774. for (i = 0; i < 0xFFFFF; i++) {
  1775. if (mbox->numstatus != 0xFF) {
  1776. nstatus = mbox->numstatus;
  1777. break;
  1778. }
  1779. rmb();
  1780. }
  1781. mbox->numstatus = 0xFF;
  1782. adapter->outstanding_cmds -= nstatus;
  1783. for (i = 0; i < nstatus; i++) {
  1784. // wait for valid command index to post
  1785. for (j = 0; j < 0xFFFFF; j++) {
  1786. if (mbox->completed[i] != 0xFF) break;
  1787. rmb();
  1788. }
  1789. completed[i] = mbox->completed[i];
  1790. mbox->completed[i] = 0xFF;
  1791. if (completed[i] == 0xFF) {
  1792. con_log(CL_ANN, (KERN_CRIT
  1793. "megaraid: command posting timed out\n"));
  1794. BUG();
  1795. continue;
  1796. }
  1797. // Get SCB associated with this command id
  1798. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1799. // a cmm command
  1800. scb = adapter->uscb_list + (completed[i] -
  1801. MBOX_MAX_SCSI_CMDS);
  1802. }
  1803. else {
  1804. // an os command
  1805. scb = adapter->kscb_list + completed[i];
  1806. }
  1807. scb->status = mbox->status;
  1808. list_add_tail(&scb->list, &clist);
  1809. }
  1810. // Acknowledge interrupt
  1811. WRINDOOR(raid_dev, 0x02);
  1812. } while(1);
  1813. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1814. // put the completed commands in the completed list. DPC would
  1815. // complete these commands later
  1816. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1817. list_splice(&clist, &adapter->completed_list);
  1818. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1819. // schedule the DPC if there is some work for it
  1820. if (handled)
  1821. tasklet_schedule(&adapter->dpc_h);
  1822. return handled;
  1823. }
  1824. /**
  1825. * megaraid_isr - isr for memory based mailbox based controllers
  1826. * @irq - irq
  1827. * @devp - pointer to our soft state
  1828. * @regs - unused
  1829. *
  1830. * Interrupt service routine for memory-mapped mailbox controllers.
  1831. */
  1832. static irqreturn_t
  1833. megaraid_isr(int irq, void *devp, struct pt_regs *regs)
  1834. {
  1835. adapter_t *adapter = devp;
  1836. int handled;
  1837. handled = megaraid_ack_sequence(adapter);
  1838. /* Loop through any pending requests */
  1839. if (!adapter->quiescent) {
  1840. megaraid_mbox_runpendq(adapter, NULL);
  1841. }
  1842. return IRQ_RETVAL(handled);
  1843. }
  1844. /**
  1845. * megaraid_mbox_sync_scb - sync kernel buffers
  1846. * @adapter : controller's soft state
  1847. * @scb : pointer to the resource packet
  1848. *
  1849. * DMA sync if required.
  1850. */
  1851. static inline void
  1852. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1853. {
  1854. mbox_ccb_t *ccb;
  1855. ccb = (mbox_ccb_t *)scb->ccb;
  1856. switch (scb->dma_type) {
  1857. case MRAID_DMA_WBUF:
  1858. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1859. pci_dma_sync_single_for_cpu(adapter->pdev,
  1860. ccb->buf_dma_h,
  1861. scb->scp->request_bufflen,
  1862. PCI_DMA_FROMDEVICE);
  1863. }
  1864. pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
  1865. scb->scp->request_bufflen, scb->dma_direction);
  1866. break;
  1867. case MRAID_DMA_WSG:
  1868. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1869. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1870. scb->scp->request_buffer,
  1871. scb->scp->use_sg, PCI_DMA_FROMDEVICE);
  1872. }
  1873. pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
  1874. scb->scp->use_sg, scb->dma_direction);
  1875. break;
  1876. default:
  1877. break;
  1878. }
  1879. return;
  1880. }
  1881. /**
  1882. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1883. * @devp : pointer to HBA soft state
  1884. *
  1885. * Pick up the commands from the completed list and send back to the owners.
  1886. * This is a reentrant function and does not assume any locks are held while
  1887. * it is being called.
  1888. */
  1889. static void
  1890. megaraid_mbox_dpc(unsigned long devp)
  1891. {
  1892. adapter_t *adapter = (adapter_t *)devp;
  1893. mraid_device_t *raid_dev;
  1894. struct list_head clist;
  1895. struct scatterlist *sgl;
  1896. scb_t *scb;
  1897. scb_t *tmp;
  1898. struct scsi_cmnd *scp;
  1899. mraid_passthru_t *pthru;
  1900. mraid_epassthru_t *epthru;
  1901. mbox_ccb_t *ccb;
  1902. int islogical;
  1903. int pdev_index;
  1904. int pdev_state;
  1905. mbox_t *mbox;
  1906. unsigned long flags;
  1907. uint8_t c;
  1908. int status;
  1909. if (!adapter) return;
  1910. raid_dev = ADAP2RAIDDEV(adapter);
  1911. // move the SCBs from the completed list to our local list
  1912. INIT_LIST_HEAD(&clist);
  1913. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1914. list_splice_init(&adapter->completed_list, &clist);
  1915. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1916. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1917. status = scb->status;
  1918. scp = scb->scp;
  1919. ccb = (mbox_ccb_t *)scb->ccb;
  1920. pthru = ccb->pthru;
  1921. epthru = ccb->epthru;
  1922. mbox = ccb->mbox;
  1923. // Make sure f/w has completed a valid command
  1924. if (scb->state != SCB_ISSUED) {
  1925. con_log(CL_ANN, (KERN_CRIT
  1926. "megaraid critical err: invalid command %d:%d:%p\n",
  1927. scb->sno, scb->state, scp));
  1928. BUG();
  1929. continue; // Must never happen!
  1930. }
  1931. // check for the management command and complete it right away
  1932. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1933. scb->state = SCB_FREE;
  1934. scb->status = status;
  1935. // remove from local clist
  1936. list_del_init(&scb->list);
  1937. megaraid_mbox_mm_done(adapter, scb);
  1938. continue;
  1939. }
  1940. // Was an abort issued for this command earlier
  1941. if (scb->state & SCB_ABORT) {
  1942. con_log(CL_ANN, (KERN_NOTICE
  1943. "megaraid: aborted cmd %lx[%x] completed\n",
  1944. scp->serial_number, scb->sno));
  1945. }
  1946. /*
  1947. * If the inquiry came of a disk drive which is not part of
  1948. * any RAID array, expose it to the kernel. For this to be
  1949. * enabled, user must set the "megaraid_expose_unconf_disks"
  1950. * flag to 1 by specifying it on module parameter list.
  1951. * This would enable data migration off drives from other
  1952. * configurations.
  1953. */
  1954. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1955. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1956. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1957. if (scp->use_sg) {
  1958. sgl = (struct scatterlist *)
  1959. scp->request_buffer;
  1960. if (sgl->page) {
  1961. c = *(unsigned char *)
  1962. (page_address((&sgl[0])->page) +
  1963. (&sgl[0])->offset);
  1964. }
  1965. else {
  1966. con_log(CL_ANN, (KERN_WARNING
  1967. "megaraid mailbox: invalid sg:%d\n",
  1968. __LINE__));
  1969. c = 0;
  1970. }
  1971. }
  1972. else {
  1973. c = *(uint8_t *)scp->request_buffer;
  1974. }
  1975. if ((c & 0x1F ) == TYPE_DISK) {
  1976. pdev_index = (scb->dev_channel * 16) +
  1977. scb->dev_target;
  1978. pdev_state =
  1979. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1980. if (pdev_state == PDRV_ONLINE ||
  1981. pdev_state == PDRV_FAILED ||
  1982. pdev_state == PDRV_RBLD ||
  1983. pdev_state == PDRV_HOTSPARE ||
  1984. megaraid_expose_unconf_disks == 0) {
  1985. status = 0xF0;
  1986. }
  1987. }
  1988. }
  1989. // Convert MegaRAID status to Linux error code
  1990. switch (status) {
  1991. case 0x00:
  1992. scp->result = (DID_OK << 16);
  1993. break;
  1994. case 0x02:
  1995. /* set sense_buffer and result fields */
  1996. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  1997. mbox->cmd == MBOXCMD_PASSTHRU64) {
  1998. memcpy(scp->sense_buffer, pthru->reqsensearea,
  1999. 14);
  2000. scp->result = DRIVER_SENSE << 24 |
  2001. DID_OK << 16 | CHECK_CONDITION << 1;
  2002. }
  2003. else {
  2004. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  2005. memcpy(scp->sense_buffer,
  2006. epthru->reqsensearea, 14);
  2007. scp->result = DRIVER_SENSE << 24 |
  2008. DID_OK << 16 |
  2009. CHECK_CONDITION << 1;
  2010. } else {
  2011. scp->sense_buffer[0] = 0x70;
  2012. scp->sense_buffer[2] = ABORTED_COMMAND;
  2013. scp->result = CHECK_CONDITION << 1;
  2014. }
  2015. }
  2016. break;
  2017. case 0x08:
  2018. scp->result = DID_BUS_BUSY << 16 | status;
  2019. break;
  2020. default:
  2021. /*
  2022. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  2023. * failed
  2024. */
  2025. if (scp->cmnd[0] == TEST_UNIT_READY) {
  2026. scp->result = DID_ERROR << 16 |
  2027. RESERVATION_CONFLICT << 1;
  2028. }
  2029. else
  2030. /*
  2031. * Error code returned is 1 if Reserve or Release
  2032. * failed or the input parameter is invalid
  2033. */
  2034. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  2035. scp->cmnd[0] == RELEASE)) {
  2036. scp->result = DID_ERROR << 16 |
  2037. RESERVATION_CONFLICT << 1;
  2038. }
  2039. else {
  2040. scp->result = DID_BAD_TARGET << 16 | status;
  2041. }
  2042. }
  2043. // print a debug message for all failed commands
  2044. if (status) {
  2045. megaraid_mbox_display_scb(adapter, scb);
  2046. }
  2047. // Free our internal resources and call the mid-layer callback
  2048. // routine
  2049. megaraid_mbox_sync_scb(adapter, scb);
  2050. // remove from local clist
  2051. list_del_init(&scb->list);
  2052. // put back in free list
  2053. megaraid_dealloc_scb(adapter, scb);
  2054. // send the scsi packet back to kernel
  2055. spin_lock(adapter->host_lock);
  2056. scp->scsi_done(scp);
  2057. spin_unlock(adapter->host_lock);
  2058. }
  2059. return;
  2060. }
  2061. /**
  2062. * megaraid_abort_handler - abort the scsi command
  2063. * @scp : command to be aborted
  2064. *
  2065. * Abort a previous SCSI request. Only commands on the pending list can be
  2066. * aborted. All the commands issued to the F/W must complete.
  2067. **/
  2068. static int
  2069. __megaraid_abort_handler(struct scsi_cmnd *scp)
  2070. {
  2071. adapter_t *adapter;
  2072. mraid_device_t *raid_dev;
  2073. scb_t *scb;
  2074. scb_t *tmp;
  2075. int found;
  2076. unsigned long flags;
  2077. int i;
  2078. adapter = SCP2ADAPTER(scp);
  2079. raid_dev = ADAP2RAIDDEV(adapter);
  2080. assert_spin_locked(adapter->host_lock);
  2081. con_log(CL_ANN, (KERN_WARNING
  2082. "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
  2083. scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
  2084. SCP2TARGET(scp), SCP2LUN(scp)));
  2085. // If FW has stopped responding, simply return failure
  2086. if (raid_dev->hw_error) {
  2087. con_log(CL_ANN, (KERN_NOTICE
  2088. "megaraid: hw error, not aborting\n"));
  2089. return FAILED;
  2090. }
  2091. // There might a race here, where the command was completed by the
  2092. // firmware and now it is on the completed list. Before we could
  2093. // complete the command to the kernel in dpc, the abort came.
  2094. // Find out if this is the case to avoid the race.
  2095. scb = NULL;
  2096. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  2097. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  2098. if (scb->scp == scp) { // Found command
  2099. list_del_init(&scb->list); // from completed list
  2100. con_log(CL_ANN, (KERN_WARNING
  2101. "megaraid: %ld:%d[%d:%d], abort from completed list\n",
  2102. scp->serial_number, scb->sno,
  2103. scb->dev_channel, scb->dev_target));
  2104. scp->result = (DID_ABORT << 16);
  2105. scp->scsi_done(scp);
  2106. megaraid_dealloc_scb(adapter, scb);
  2107. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  2108. flags);
  2109. return SUCCESS;
  2110. }
  2111. }
  2112. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  2113. // Find out if this command is still on the pending list. If it is and
  2114. // was never issued, abort and return success. If the command is owned
  2115. // by the firmware, we must wait for it to complete by the FW.
  2116. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2117. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2118. if (scb->scp == scp) { // Found command
  2119. list_del_init(&scb->list); // from pending list
  2120. ASSERT(!(scb->state & SCB_ISSUED));
  2121. con_log(CL_ANN, (KERN_WARNING
  2122. "megaraid abort: %ld[%d:%d], driver owner\n",
  2123. scp->serial_number, scb->dev_channel,
  2124. scb->dev_target));
  2125. scp->result = (DID_ABORT << 16);
  2126. scp->scsi_done(scp);
  2127. megaraid_dealloc_scb(adapter, scb);
  2128. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2129. flags);
  2130. return SUCCESS;
  2131. }
  2132. }
  2133. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2134. // Check do we even own this command, in which case this would be
  2135. // owned by the firmware. The only way to locate the FW scb is to
  2136. // traverse through the list of all SCB, since driver does not
  2137. // maintain these SCBs on any list
  2138. found = 0;
  2139. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2140. scb = adapter->kscb_list + i;
  2141. if (scb->scp == scp) {
  2142. found = 1;
  2143. if (!(scb->state & SCB_ISSUED)) {
  2144. con_log(CL_ANN, (KERN_WARNING
  2145. "megaraid abort: %ld%d[%d:%d], invalid state\n",
  2146. scp->serial_number, scb->sno, scb->dev_channel,
  2147. scb->dev_target));
  2148. BUG();
  2149. }
  2150. else {
  2151. con_log(CL_ANN, (KERN_WARNING
  2152. "megaraid abort: %ld:%d[%d:%d], fw owner\n",
  2153. scp->serial_number, scb->sno, scb->dev_channel,
  2154. scb->dev_target));
  2155. }
  2156. }
  2157. }
  2158. if (!found) {
  2159. con_log(CL_ANN, (KERN_WARNING
  2160. "megaraid abort: scsi cmd:%ld, do now own\n",
  2161. scp->serial_number));
  2162. // FIXME: Should there be a callback for this command?
  2163. return SUCCESS;
  2164. }
  2165. // We cannot actually abort a command owned by firmware, return
  2166. // failure and wait for reset. In host reset handler, we will find out
  2167. // if the HBA is still live
  2168. return FAILED;
  2169. }
  2170. static int
  2171. megaraid_abort_handler(struct scsi_cmnd *scp)
  2172. {
  2173. adapter_t *adapter;
  2174. int rc;
  2175. adapter = SCP2ADAPTER(scp);
  2176. spin_lock_irq(adapter->host_lock);
  2177. rc = __megaraid_abort_handler(scp);
  2178. spin_unlock_irq(adapter->host_lock);
  2179. return rc;
  2180. }
  2181. /**
  2182. * megaraid_reset_handler - device reset hadler for mailbox based driver
  2183. * @scp : reference command
  2184. *
  2185. * Reset handler for the mailbox based controller. First try to find out if
  2186. * the FW is still live, in which case the outstanding commands counter mut go
  2187. * down to 0. If that happens, also issue the reservation reset command to
  2188. * relinquish (possible) reservations on the logical drives connected to this
  2189. * host
  2190. **/
  2191. static int
  2192. __megaraid_reset_handler(struct scsi_cmnd *scp)
  2193. {
  2194. adapter_t *adapter;
  2195. scb_t *scb;
  2196. scb_t *tmp;
  2197. mraid_device_t *raid_dev;
  2198. unsigned long flags;
  2199. uint8_t raw_mbox[sizeof(mbox_t)];
  2200. int rval;
  2201. int recovery_window;
  2202. int recovering;
  2203. int i;
  2204. adapter = SCP2ADAPTER(scp);
  2205. raid_dev = ADAP2RAIDDEV(adapter);
  2206. assert_spin_locked(adapter->host_lock);
  2207. con_log(CL_ANN, (KERN_WARNING "megaraid: reseting the host...\n"));
  2208. // return failure if adapter is not responding
  2209. if (raid_dev->hw_error) {
  2210. con_log(CL_ANN, (KERN_NOTICE
  2211. "megaraid: hw error, cannot reset\n"));
  2212. return FAILED;
  2213. }
  2214. // Under exceptional conditions, FW can take up to 3 minutes to
  2215. // complete command processing. Wait for additional 2 minutes for the
  2216. // pending commands counter to go down to 0. If it doesn't, let the
  2217. // controller be marked offline
  2218. // Also, reset all the commands currently owned by the driver
  2219. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2220. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2221. list_del_init(&scb->list); // from pending list
  2222. con_log(CL_ANN, (KERN_WARNING
  2223. "megaraid: %ld:%d[%d:%d], reset from pending list\n",
  2224. scp->serial_number, scb->sno,
  2225. scb->dev_channel, scb->dev_target));
  2226. scp->result = (DID_RESET << 16);
  2227. scp->scsi_done(scp);
  2228. megaraid_dealloc_scb(adapter, scb);
  2229. }
  2230. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2231. if (adapter->outstanding_cmds) {
  2232. con_log(CL_ANN, (KERN_NOTICE
  2233. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2234. adapter->outstanding_cmds, MBOX_RESET_WAIT));
  2235. }
  2236. spin_unlock(adapter->host_lock);
  2237. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2238. recovering = adapter->outstanding_cmds;
  2239. for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
  2240. megaraid_ack_sequence(adapter);
  2241. // print a message once every 5 seconds only
  2242. if (!(i % 5)) {
  2243. con_log(CL_ANN, (
  2244. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2245. adapter->outstanding_cmds,
  2246. MBOX_RESET_WAIT - i));
  2247. }
  2248. // bailout if no recovery happended in reset time
  2249. if ((i == MBOX_RESET_WAIT) &&
  2250. (recovering == adapter->outstanding_cmds)) {
  2251. break;
  2252. }
  2253. msleep(1000);
  2254. }
  2255. spin_lock(adapter->host_lock);
  2256. // If still outstanding commands, bail out
  2257. if (adapter->outstanding_cmds) {
  2258. con_log(CL_ANN, (KERN_WARNING
  2259. "megaraid mbox: critical hardware error!\n"));
  2260. raid_dev->hw_error = 1;
  2261. return FAILED;
  2262. }
  2263. else {
  2264. con_log(CL_ANN, (KERN_NOTICE
  2265. "megaraid mbox: reset sequence completed sucessfully\n"));
  2266. }
  2267. // If the controller supports clustering, reset reservations
  2268. if (!adapter->ha) return SUCCESS;
  2269. // clear reservations if any
  2270. raw_mbox[0] = CLUSTER_CMD;
  2271. raw_mbox[2] = RESET_RESERVATIONS;
  2272. rval = SUCCESS;
  2273. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2274. con_log(CL_ANN,
  2275. (KERN_INFO "megaraid: reservation reset\n"));
  2276. }
  2277. else {
  2278. rval = FAILED;
  2279. con_log(CL_ANN, (KERN_WARNING
  2280. "megaraid: reservation reset failed\n"));
  2281. }
  2282. return rval;
  2283. }
  2284. static int
  2285. megaraid_reset_handler(struct scsi_cmnd *cmd)
  2286. {
  2287. int rc;
  2288. spin_lock_irq(cmd->device->host->host_lock);
  2289. rc = __megaraid_reset_handler(cmd);
  2290. spin_unlock_irq(cmd->device->host->host_lock);
  2291. return rc;
  2292. }
  2293. /*
  2294. * START: internal commands library
  2295. *
  2296. * This section of the driver has the common routine used by the driver and
  2297. * also has all the FW routines
  2298. */
  2299. /**
  2300. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2301. * @adapter - controller's soft state
  2302. * @raw_mbox - the mailbox
  2303. *
  2304. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2305. * controllers
  2306. */
  2307. static int
  2308. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2309. {
  2310. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2311. mbox64_t *mbox64;
  2312. mbox_t *mbox;
  2313. uint8_t status;
  2314. int i;
  2315. mbox64 = raid_dev->mbox64;
  2316. mbox = raid_dev->mbox;
  2317. /*
  2318. * Wait until mailbox is free
  2319. */
  2320. if (megaraid_busywait_mbox(raid_dev) != 0)
  2321. goto blocked_mailbox;
  2322. /*
  2323. * Copy mailbox data into host structure
  2324. */
  2325. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2326. mbox->cmdid = 0xFE;
  2327. mbox->busy = 1;
  2328. mbox->poll = 0;
  2329. mbox->ack = 0;
  2330. mbox->numstatus = 0xFF;
  2331. mbox->status = 0xFF;
  2332. wmb();
  2333. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2334. // wait for maximum 1 second for status to post. If the status is not
  2335. // available within 1 second, assume FW is initializing and wait
  2336. // for an extended amount of time
  2337. if (mbox->numstatus == 0xFF) { // status not yet available
  2338. udelay(25);;
  2339. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2340. rmb();
  2341. msleep(1);
  2342. }
  2343. if (i == 1000) {
  2344. con_log(CL_ANN, (KERN_NOTICE
  2345. "megaraid mailbox: wait for FW to boot "));
  2346. for (i = 0; (mbox->numstatus == 0xFF) &&
  2347. (i < MBOX_RESET_WAIT); i++) {
  2348. rmb();
  2349. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2350. MBOX_RESET_WAIT - i));
  2351. msleep(1000);
  2352. }
  2353. if (i == MBOX_RESET_WAIT) {
  2354. con_log(CL_ANN, (
  2355. "\nmegaraid mailbox: status not available\n"));
  2356. return -1;
  2357. }
  2358. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2359. }
  2360. }
  2361. // wait for maximum 1 second for poll semaphore
  2362. if (mbox->poll != 0x77) {
  2363. udelay(25);
  2364. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2365. rmb();
  2366. msleep(1);
  2367. }
  2368. if (i == 1000) {
  2369. con_log(CL_ANN, (KERN_WARNING
  2370. "megaraid mailbox: could not get poll semaphore\n"));
  2371. return -1;
  2372. }
  2373. }
  2374. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2375. wmb();
  2376. // wait for maximum 1 second for acknowledgement
  2377. if (RDINDOOR(raid_dev) & 0x2) {
  2378. udelay(25);
  2379. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2380. rmb();
  2381. msleep(1);
  2382. }
  2383. if (i == 1000) {
  2384. con_log(CL_ANN, (KERN_WARNING
  2385. "megaraid mailbox: could not acknowledge\n"));
  2386. return -1;
  2387. }
  2388. }
  2389. mbox->poll = 0;
  2390. mbox->ack = 0x77;
  2391. status = mbox->status;
  2392. // invalidate the completed command id array. After command
  2393. // completion, firmware would write the valid id.
  2394. mbox->numstatus = 0xFF;
  2395. mbox->status = 0xFF;
  2396. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2397. mbox->completed[i] = 0xFF;
  2398. }
  2399. return status;
  2400. blocked_mailbox:
  2401. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2402. return -1;
  2403. }
  2404. /**
  2405. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2406. * @adapter - controller's soft state
  2407. * @raw_mbox - the mailbox
  2408. *
  2409. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2410. * controllers. This is a faster version of the synchronous command and
  2411. * therefore can be called in interrupt-context as well
  2412. */
  2413. static int
  2414. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2415. {
  2416. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2417. mbox_t *mbox;
  2418. long i;
  2419. mbox = raid_dev->mbox;
  2420. // return immediately if the mailbox is busy
  2421. if (mbox->busy) return -1;
  2422. // Copy mailbox data into host structure
  2423. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2424. mbox->cmdid = 0xFE;
  2425. mbox->busy = 1;
  2426. mbox->poll = 0;
  2427. mbox->ack = 0;
  2428. mbox->numstatus = 0xFF;
  2429. mbox->status = 0xFF;
  2430. wmb();
  2431. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2432. for (i = 0; i < 0xFFFFF; i++) {
  2433. if (mbox->numstatus != 0xFF) break;
  2434. }
  2435. if (i == 0xFFFFF) {
  2436. // We may need to re-calibrate the counter
  2437. con_log(CL_ANN, (KERN_CRIT
  2438. "megaraid: fast sync command timed out\n"));
  2439. }
  2440. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2441. wmb();
  2442. return mbox->status;
  2443. }
  2444. /**
  2445. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2446. * @raid_dev - RAID device (HBA) soft state
  2447. *
  2448. * wait until the controller's mailbox is available to accept more commands.
  2449. * wait for at most 1 second
  2450. */
  2451. static int
  2452. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2453. {
  2454. mbox_t *mbox = raid_dev->mbox;
  2455. int i = 0;
  2456. if (mbox->busy) {
  2457. udelay(25);
  2458. for (i = 0; mbox->busy && i < 1000; i++)
  2459. msleep(1);
  2460. }
  2461. if (i < 1000) return 0;
  2462. else return -1;
  2463. }
  2464. /**
  2465. * megaraid_mbox_product_info - some static information about the controller
  2466. * @adapter - our soft state
  2467. *
  2468. * issue commands to the controller to grab some parameters required by our
  2469. * caller.
  2470. */
  2471. static int
  2472. megaraid_mbox_product_info(adapter_t *adapter)
  2473. {
  2474. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2475. mbox_t *mbox;
  2476. uint8_t raw_mbox[sizeof(mbox_t)];
  2477. mraid_pinfo_t *pinfo;
  2478. dma_addr_t pinfo_dma_h;
  2479. mraid_inquiry3_t *mraid_inq3;
  2480. int i;
  2481. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2482. mbox = (mbox_t *)raw_mbox;
  2483. /*
  2484. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2485. * e.g., max channels, max commands etc.
  2486. */
  2487. pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2488. &pinfo_dma_h);
  2489. if (pinfo == NULL) {
  2490. con_log(CL_ANN, (KERN_WARNING
  2491. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2492. __LINE__));
  2493. return -1;
  2494. }
  2495. memset(pinfo, 0, sizeof(mraid_pinfo_t));
  2496. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2497. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2498. raw_mbox[0] = FC_NEW_CONFIG;
  2499. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2500. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2501. // Issue the command
  2502. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2503. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2504. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2505. pinfo, pinfo_dma_h);
  2506. return -1;
  2507. }
  2508. /*
  2509. * Collect information about state of each physical drive
  2510. * attached to the controller. We will expose all the disks
  2511. * which are not part of RAID
  2512. */
  2513. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2514. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2515. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2516. }
  2517. /*
  2518. * Get product info for information like number of channels,
  2519. * maximum commands supported.
  2520. */
  2521. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2522. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2523. raw_mbox[0] = FC_NEW_CONFIG;
  2524. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2525. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2526. con_log(CL_ANN, (KERN_WARNING
  2527. "megaraid: product info failed\n"));
  2528. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2529. pinfo, pinfo_dma_h);
  2530. return -1;
  2531. }
  2532. /*
  2533. * Setup some parameters for host, as required by our caller
  2534. */
  2535. adapter->max_channel = pinfo->nchannels;
  2536. /*
  2537. * we will export all the logical drives on a single channel.
  2538. * Add 1 since inquires do not come for inititor ID
  2539. */
  2540. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2541. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2542. /*
  2543. * These are the maximum outstanding commands for the scsi-layer
  2544. */
  2545. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2546. memset(adapter->fw_version, 0, VERSION_SIZE);
  2547. memset(adapter->bios_version, 0, VERSION_SIZE);
  2548. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2549. adapter->fw_version[4] = 0;
  2550. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2551. adapter->bios_version[4] = 0;
  2552. con_log(CL_ANN, (KERN_NOTICE
  2553. "megaraid: fw version:[%s] bios version:[%s]\n",
  2554. adapter->fw_version, adapter->bios_version));
  2555. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2556. pinfo_dma_h);
  2557. return 0;
  2558. }
  2559. /**
  2560. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2561. * @adapter - soft state for the controller
  2562. *
  2563. * this routine check whether the controller in question supports extended
  2564. * ( > 10 bytes ) CDBs
  2565. */
  2566. static int
  2567. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2568. {
  2569. mbox_t *mbox;
  2570. uint8_t raw_mbox[sizeof(mbox_t)];
  2571. int rval;
  2572. mbox = (mbox_t *)raw_mbox;
  2573. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2574. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2575. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2576. raw_mbox[0] = MAIN_MISC_OPCODE;
  2577. raw_mbox[2] = SUPPORT_EXT_CDB;
  2578. /*
  2579. * Issue the command
  2580. */
  2581. rval = 0;
  2582. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2583. rval = -1;
  2584. }
  2585. return rval;
  2586. }
  2587. /**
  2588. * megaraid_mbox_support_ha - Do we support clustering
  2589. * @adapter - soft state for the controller
  2590. * @init_id - ID of the initiator
  2591. *
  2592. * Determine if the firmware supports clustering and the ID of the initiator.
  2593. */
  2594. static int
  2595. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2596. {
  2597. mbox_t *mbox;
  2598. uint8_t raw_mbox[sizeof(mbox_t)];
  2599. int rval;
  2600. mbox = (mbox_t *)raw_mbox;
  2601. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2602. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2603. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2604. raw_mbox[0] = GET_TARGET_ID;
  2605. // Issue the command
  2606. *init_id = 7;
  2607. rval = -1;
  2608. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2609. *init_id = *(uint8_t *)adapter->ibuf;
  2610. con_log(CL_ANN, (KERN_INFO
  2611. "megaraid: cluster firmware, initiator ID: %d\n",
  2612. *init_id));
  2613. rval = 0;
  2614. }
  2615. return rval;
  2616. }
  2617. /**
  2618. * megaraid_mbox_support_random_del - Do we support random deletion
  2619. * @adapter - soft state for the controller
  2620. *
  2621. * Determine if the firmware supports random deletion
  2622. * Return: 1 is operation supported, 0 otherwise
  2623. */
  2624. static int
  2625. megaraid_mbox_support_random_del(adapter_t *adapter)
  2626. {
  2627. mbox_t *mbox;
  2628. uint8_t raw_mbox[sizeof(mbox_t)];
  2629. int rval;
  2630. mbox = (mbox_t *)raw_mbox;
  2631. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2632. raw_mbox[0] = FC_DEL_LOGDRV;
  2633. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2634. // Issue the command
  2635. rval = 0;
  2636. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2637. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2638. rval = 1;
  2639. }
  2640. return rval;
  2641. }
  2642. /**
  2643. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2644. * @adapter - soft state for the controller
  2645. *
  2646. * Find out the maximum number of scatter-gather elements supported by the
  2647. * firmware
  2648. */
  2649. static int
  2650. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2651. {
  2652. mbox_t *mbox;
  2653. uint8_t raw_mbox[sizeof(mbox_t)];
  2654. int nsg;
  2655. mbox = (mbox_t *)raw_mbox;
  2656. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2657. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2658. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2659. raw_mbox[0] = MAIN_MISC_OPCODE;
  2660. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2661. // Issue the command
  2662. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2663. nsg = *(uint8_t *)adapter->ibuf;
  2664. }
  2665. else {
  2666. nsg = MBOX_DEFAULT_SG_SIZE;
  2667. }
  2668. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2669. return nsg;
  2670. }
  2671. /**
  2672. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2673. * @adapter - soft state for the controller
  2674. *
  2675. * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
  2676. * can be exported as regular SCSI channels
  2677. */
  2678. static void
  2679. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2680. {
  2681. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2682. mbox_t *mbox;
  2683. uint8_t raw_mbox[sizeof(mbox_t)];
  2684. mbox = (mbox_t *)raw_mbox;
  2685. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2686. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2687. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2688. raw_mbox[0] = CHNL_CLASS;
  2689. raw_mbox[2] = GET_CHNL_CLASS;
  2690. // Issue the command. If the command fails, all channels are RAID
  2691. // channels
  2692. raid_dev->channel_class = 0xFF;
  2693. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2694. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2695. }
  2696. return;
  2697. }
  2698. /**
  2699. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2700. * @param adapter : soft state for the controller
  2701. *
  2702. * Flush adapter cache followed by disks cache
  2703. */
  2704. static void
  2705. megaraid_mbox_flush_cache(adapter_t *adapter)
  2706. {
  2707. mbox_t *mbox;
  2708. uint8_t raw_mbox[sizeof(mbox_t)];
  2709. mbox = (mbox_t *)raw_mbox;
  2710. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2711. raw_mbox[0] = FLUSH_ADAPTER;
  2712. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2713. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2714. }
  2715. raw_mbox[0] = FLUSH_SYSTEM;
  2716. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2717. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2718. }
  2719. return;
  2720. }
  2721. /**
  2722. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2723. * @param adapter : controllers' soft state
  2724. * @param scb : SCB to be displayed
  2725. * @param level : debug level for console print
  2726. *
  2727. * Diplay information about the given SCB iff the current debug level is
  2728. * verbose
  2729. */
  2730. static void
  2731. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2732. {
  2733. mbox_ccb_t *ccb;
  2734. struct scsi_cmnd *scp;
  2735. mbox_t *mbox;
  2736. int level;
  2737. int i;
  2738. ccb = (mbox_ccb_t *)scb->ccb;
  2739. scp = scb->scp;
  2740. mbox = ccb->mbox;
  2741. level = CL_DLEVEL3;
  2742. con_log(level, (KERN_NOTICE
  2743. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2744. mbox->cmd, scb->sno));
  2745. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2746. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2747. mbox->numsge));
  2748. if (!scp) return;
  2749. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2750. for (i = 0; i < scp->cmd_len; i++) {
  2751. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2752. }
  2753. con_log(level, ("\n"));
  2754. return;
  2755. }
  2756. /**
  2757. * megaraid_mbox_setup_device_map - manage device ids
  2758. * @adapter : Driver's soft state
  2759. *
  2760. * Manange the device ids to have an appropraite mapping between the kernel
  2761. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2762. * scsi devices on their actual addresses, whereas the logical drives are
  2763. * exported on a virtual scsi channel.
  2764. **/
  2765. static void
  2766. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2767. {
  2768. uint8_t c;
  2769. uint8_t t;
  2770. /*
  2771. * First fill the values on the logical drive channel
  2772. */
  2773. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2774. adapter->device_ids[adapter->max_channel][t] =
  2775. (t < adapter->init_id) ? t : t - 1;
  2776. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2777. /*
  2778. * Fill the values on the physical devices channels
  2779. */
  2780. for (c = 0; c < adapter->max_channel; c++)
  2781. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2782. adapter->device_ids[c][t] = (c << 8) | t;
  2783. }
  2784. /*
  2785. * END: internal commands library
  2786. */
  2787. /*
  2788. * START: Interface for the common management module
  2789. *
  2790. * This is the module, which interfaces with the common mangement module to
  2791. * provide support for ioctl and sysfs
  2792. */
  2793. /**
  2794. * megaraid_cmm_register - register with the mangement module
  2795. * @param adapter : HBA soft state
  2796. *
  2797. * Register with the management module, which allows applications to issue
  2798. * ioctl calls to the drivers. This interface is used by the management module
  2799. * to setup sysfs support as well.
  2800. */
  2801. static int
  2802. megaraid_cmm_register(adapter_t *adapter)
  2803. {
  2804. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2805. mraid_mmadp_t adp;
  2806. scb_t *scb;
  2807. mbox_ccb_t *ccb;
  2808. int rval;
  2809. int i;
  2810. // Allocate memory for the base list of scb for management module.
  2811. adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
  2812. GFP_KERNEL);
  2813. if (adapter->uscb_list == NULL) {
  2814. con_log(CL_ANN, (KERN_WARNING
  2815. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2816. __LINE__));
  2817. return -1;
  2818. }
  2819. memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
  2820. // Initialize the synchronization parameters for resources for
  2821. // commands for management module
  2822. INIT_LIST_HEAD(&adapter->uscb_pool);
  2823. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2824. // link all the packets. Note, CCB for commands, coming from the
  2825. // commom management module, mailbox physical address are already
  2826. // setup by it. We just need placeholder for that in our local command
  2827. // control blocks
  2828. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2829. scb = adapter->uscb_list + i;
  2830. ccb = raid_dev->uccb_list + i;
  2831. scb->ccb = (caddr_t)ccb;
  2832. ccb->mbox64 = raid_dev->umbox64 + i;
  2833. ccb->mbox = &ccb->mbox64->mbox32;
  2834. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2835. scb->gp = 0;
  2836. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2837. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2838. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2839. scb->scp = NULL;
  2840. scb->state = SCB_FREE;
  2841. scb->dma_direction = PCI_DMA_NONE;
  2842. scb->dma_type = MRAID_DMA_NONE;
  2843. scb->dev_channel = -1;
  2844. scb->dev_target = -1;
  2845. // put scb in the free pool
  2846. list_add_tail(&scb->list, &adapter->uscb_pool);
  2847. }
  2848. adp.unique_id = adapter->unique_id;
  2849. adp.drvr_type = DRVRTYPE_MBOX;
  2850. adp.drvr_data = (unsigned long)adapter;
  2851. adp.pdev = adapter->pdev;
  2852. adp.issue_uioc = megaraid_mbox_mm_handler;
  2853. adp.timeout = 300;
  2854. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2855. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2856. con_log(CL_ANN, (KERN_WARNING
  2857. "megaraid mbox: did not register with CMM\n"));
  2858. kfree(adapter->uscb_list);
  2859. }
  2860. return rval;
  2861. }
  2862. /**
  2863. * megaraid_cmm_unregister - un-register with the mangement module
  2864. * @param adapter : HBA soft state
  2865. *
  2866. * Un-register with the management module.
  2867. * FIXME: mgmt module must return failure for unregister if it has pending
  2868. * commands in LLD
  2869. */
  2870. static int
  2871. megaraid_cmm_unregister(adapter_t *adapter)
  2872. {
  2873. kfree(adapter->uscb_list);
  2874. mraid_mm_unregister_adp(adapter->unique_id);
  2875. return 0;
  2876. }
  2877. /**
  2878. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2879. * @param drvr_data : LLD specific data
  2880. * @param kioc : CMM interface packet
  2881. * @param action : command action
  2882. *
  2883. * This routine is invoked whenever the Common Mangement Module (CMM) has a
  2884. * command for us. The 'action' parameter specifies if this is a new command
  2885. * or otherwise.
  2886. */
  2887. static int
  2888. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2889. {
  2890. adapter_t *adapter;
  2891. if (action != IOCTL_ISSUE) {
  2892. con_log(CL_ANN, (KERN_WARNING
  2893. "megaraid: unsupported management action:%#2x\n",
  2894. action));
  2895. return (-ENOTSUPP);
  2896. }
  2897. adapter = (adapter_t *)drvr_data;
  2898. // make sure this adapter is not being detached right now.
  2899. if (atomic_read(&adapter->being_detached)) {
  2900. con_log(CL_ANN, (KERN_WARNING
  2901. "megaraid: reject management request, detaching\n"));
  2902. return (-ENODEV);
  2903. }
  2904. switch (kioc->opcode) {
  2905. case GET_ADAP_INFO:
  2906. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2907. (unsigned long)kioc->buf_vaddr);
  2908. kioc->done(kioc);
  2909. return kioc->status;
  2910. case MBOX_CMD:
  2911. return megaraid_mbox_mm_command(adapter, kioc);
  2912. default:
  2913. kioc->status = (-EINVAL);
  2914. kioc->done(kioc);
  2915. return (-EINVAL);
  2916. }
  2917. return 0; // not reached
  2918. }
  2919. /**
  2920. * megaraid_mbox_mm_command - issues commands routed through CMM
  2921. * @param adapter : HBA soft state
  2922. * @param kioc : management command packet
  2923. *
  2924. * Issues commands, which are routed through the management module.
  2925. */
  2926. static int
  2927. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2928. {
  2929. struct list_head *head = &adapter->uscb_pool;
  2930. mbox64_t *mbox64;
  2931. uint8_t *raw_mbox;
  2932. scb_t *scb;
  2933. mbox_ccb_t *ccb;
  2934. unsigned long flags;
  2935. // detach one scb from free pool
  2936. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2937. if (list_empty(head)) { // should never happen because of CMM
  2938. con_log(CL_ANN, (KERN_WARNING
  2939. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2940. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2941. return (-EINVAL);
  2942. }
  2943. scb = list_entry(head->next, scb_t, list);
  2944. list_del_init(&scb->list);
  2945. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2946. scb->state = SCB_ACTIVE;
  2947. scb->dma_type = MRAID_DMA_NONE;
  2948. scb->dma_direction = PCI_DMA_NONE;
  2949. ccb = (mbox_ccb_t *)scb->ccb;
  2950. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2951. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2952. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2953. scb->gp = (unsigned long)kioc;
  2954. /*
  2955. * If it is a logdrv random delete operation, we have to wait till
  2956. * there are no outstanding cmds at the fw and then issue it directly
  2957. */
  2958. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2959. if (wait_till_fw_empty(adapter)) {
  2960. con_log(CL_ANN, (KERN_NOTICE
  2961. "megaraid mbox: LD delete, timed out\n"));
  2962. kioc->status = -ETIME;
  2963. scb->status = -1;
  2964. megaraid_mbox_mm_done(adapter, scb);
  2965. return (-ETIME);
  2966. }
  2967. INIT_LIST_HEAD(&scb->list);
  2968. scb->state = SCB_ISSUED;
  2969. if (mbox_post_cmd(adapter, scb) != 0) {
  2970. con_log(CL_ANN, (KERN_NOTICE
  2971. "megaraid mbox: LD delete, mailbox busy\n"));
  2972. kioc->status = -EBUSY;
  2973. scb->status = -1;
  2974. megaraid_mbox_mm_done(adapter, scb);
  2975. return (-EBUSY);
  2976. }
  2977. return 0;
  2978. }
  2979. // put the command on the pending list and execute
  2980. megaraid_mbox_runpendq(adapter, scb);
  2981. return 0;
  2982. }
  2983. static int
  2984. wait_till_fw_empty(adapter_t *adapter)
  2985. {
  2986. unsigned long flags = 0;
  2987. int i;
  2988. /*
  2989. * Set the quiescent flag to stop issuing cmds to FW.
  2990. */
  2991. spin_lock_irqsave(adapter->host_lock, flags);
  2992. adapter->quiescent++;
  2993. spin_unlock_irqrestore(adapter->host_lock, flags);
  2994. /*
  2995. * Wait till there are no more cmds outstanding at FW. Try for at most
  2996. * 60 seconds
  2997. */
  2998. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  2999. con_log(CL_DLEVEL1, (KERN_INFO
  3000. "megaraid: FW has %d pending commands\n",
  3001. adapter->outstanding_cmds));
  3002. msleep(1000);
  3003. }
  3004. return adapter->outstanding_cmds;
  3005. }
  3006. /**
  3007. * megaraid_mbox_mm_done - callback for CMM commands
  3008. * @adapter : HBA soft state
  3009. * @scb : completed command
  3010. *
  3011. * Callback routine for internal commands originated from the management
  3012. * module.
  3013. */
  3014. static void
  3015. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  3016. {
  3017. uioc_t *kioc;
  3018. mbox64_t *mbox64;
  3019. uint8_t *raw_mbox;
  3020. unsigned long flags;
  3021. kioc = (uioc_t *)scb->gp;
  3022. kioc->status = 0;
  3023. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  3024. mbox64->mbox32.status = scb->status;
  3025. raw_mbox = (uint8_t *)&mbox64->mbox32;
  3026. // put scb in the free pool
  3027. scb->state = SCB_FREE;
  3028. scb->scp = NULL;
  3029. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  3030. list_add(&scb->list, &adapter->uscb_pool);
  3031. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  3032. // if a delete logical drive operation succeeded, restart the
  3033. // controller
  3034. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  3035. adapter->quiescent--;
  3036. megaraid_mbox_runpendq(adapter, NULL);
  3037. }
  3038. kioc->done(kioc);
  3039. return;
  3040. }
  3041. /**
  3042. * gather_hbainfo - HBA characteristics for the applications
  3043. * @param adapter : HBA soft state
  3044. * @param hinfo : pointer to the caller's host info strucuture
  3045. */
  3046. static int
  3047. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  3048. {
  3049. uint8_t dmajor;
  3050. dmajor = megaraid_mbox_version[0];
  3051. hinfo->pci_vendor_id = adapter->pdev->vendor;
  3052. hinfo->pci_device_id = adapter->pdev->device;
  3053. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3054. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3055. hinfo->pci_bus = adapter->pdev->bus->number;
  3056. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3057. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3058. hinfo->irq = adapter->host->irq;
  3059. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3060. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3061. hinfo->host_no = adapter->host->host_no;
  3062. return 0;
  3063. }
  3064. /*
  3065. * END: Interface for the common management module
  3066. */
  3067. /**
  3068. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3069. *
  3070. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3071. * are read. These attributes would require up-to-date information from the
  3072. * FW. Also set up resources for mutual exclusion to share these resources and
  3073. * the wait queue.
  3074. *
  3075. * @param adapter : controller's soft state
  3076. *
  3077. * @return 0 on success
  3078. * @return -ERROR_CODE on failure
  3079. */
  3080. static int
  3081. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3082. {
  3083. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3084. int rval = 0;
  3085. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3086. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3087. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3088. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3089. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3090. !raid_dev->sysfs_buffer) {
  3091. con_log(CL_ANN, (KERN_WARNING
  3092. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  3093. __LINE__));
  3094. rval = -ENOMEM;
  3095. megaraid_sysfs_free_resources(adapter);
  3096. }
  3097. sema_init(&raid_dev->sysfs_sem, 1);
  3098. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3099. return rval;
  3100. }
  3101. /**
  3102. * megaraid_sysfs_free_resources - free sysfs related resources
  3103. *
  3104. * Free packets allocated for sysfs FW commands
  3105. *
  3106. * @param adapter : controller's soft state
  3107. */
  3108. static void
  3109. megaraid_sysfs_free_resources(adapter_t *adapter)
  3110. {
  3111. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3112. kfree(raid_dev->sysfs_uioc);
  3113. kfree(raid_dev->sysfs_mbox64);
  3114. if (raid_dev->sysfs_buffer) {
  3115. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3116. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3117. }
  3118. }
  3119. /**
  3120. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3121. *
  3122. * Callback routine called in the ISR/tasklet context for get ldmap call
  3123. *
  3124. * @param uioc : completed packet
  3125. */
  3126. static void
  3127. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3128. {
  3129. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3130. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3131. uioc->status = 0;
  3132. wake_up(&raid_dev->sysfs_wait_q);
  3133. }
  3134. /**
  3135. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3136. *
  3137. * Timeout routine to recover and return to application, in case the adapter
  3138. * has stopped responding. A timeout of 60 seconds for this command seem like
  3139. * a good value
  3140. *
  3141. * @param uioc : timed out packet
  3142. */
  3143. static void
  3144. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3145. {
  3146. uioc_t *uioc = (uioc_t *)data;
  3147. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3148. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3149. uioc->status = -ETIME;
  3150. wake_up(&raid_dev->sysfs_wait_q);
  3151. }
  3152. /**
  3153. * megaraid_sysfs_get_ldmap - get update logical drive map
  3154. *
  3155. * This routine will be called whenever user reads the logical drive
  3156. * attributes, go get the current logical drive mapping table from the
  3157. * firmware. We use the managment API's to issue commands to the controller.
  3158. *
  3159. * NOTE: The commands issuance functionality is not generalized and
  3160. * implemented in context of "get ld map" command only. If required, the
  3161. * command issuance logical can be trivially pulled out and implemented as a
  3162. * standalone libary. For now, this should suffice since there is no other
  3163. * user of this interface.
  3164. *
  3165. * @param adapter : controller's soft state
  3166. *
  3167. * @return 0 on success
  3168. * @return -1 on failure
  3169. */
  3170. static int
  3171. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3172. {
  3173. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3174. uioc_t *uioc;
  3175. mbox64_t *mbox64;
  3176. mbox_t *mbox;
  3177. char *raw_mbox;
  3178. struct timer_list sysfs_timer;
  3179. struct timer_list *timerp;
  3180. caddr_t ldmap;
  3181. int rval = 0;
  3182. /*
  3183. * Allow only one read at a time to go through the sysfs attributes
  3184. */
  3185. down(&raid_dev->sysfs_sem);
  3186. uioc = raid_dev->sysfs_uioc;
  3187. mbox64 = raid_dev->sysfs_mbox64;
  3188. ldmap = raid_dev->sysfs_buffer;
  3189. memset(uioc, 0, sizeof(uioc_t));
  3190. memset(mbox64, 0, sizeof(mbox64_t));
  3191. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3192. mbox = &mbox64->mbox32;
  3193. raw_mbox = (char *)mbox;
  3194. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3195. uioc->buf_vaddr = (caddr_t)adapter;
  3196. uioc->status = -ENODATA;
  3197. uioc->done = megaraid_sysfs_get_ldmap_done;
  3198. /*
  3199. * Prepare the mailbox packet to get the current logical drive mapping
  3200. * table
  3201. */
  3202. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3203. raw_mbox[0] = FC_DEL_LOGDRV;
  3204. raw_mbox[2] = OP_GET_LDID_MAP;
  3205. /*
  3206. * Setup a timer to recover from a non-responding controller
  3207. */
  3208. timerp = &sysfs_timer;
  3209. init_timer(timerp);
  3210. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3211. timerp->data = (unsigned long)uioc;
  3212. timerp->expires = jiffies + 60 * HZ;
  3213. add_timer(timerp);
  3214. /*
  3215. * Send the command to the firmware
  3216. */
  3217. rval = megaraid_mbox_mm_command(adapter, uioc);
  3218. if (rval == 0) { // command successfully issued
  3219. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3220. /*
  3221. * Check if the command timed out
  3222. */
  3223. if (uioc->status == -ETIME) {
  3224. con_log(CL_ANN, (KERN_NOTICE
  3225. "megaraid: sysfs get ld map timed out\n"));
  3226. rval = -ETIME;
  3227. }
  3228. else {
  3229. rval = mbox->status;
  3230. }
  3231. if (rval == 0) {
  3232. memcpy(raid_dev->curr_ldmap, ldmap,
  3233. sizeof(raid_dev->curr_ldmap));
  3234. }
  3235. else {
  3236. con_log(CL_ANN, (KERN_NOTICE
  3237. "megaraid: get ld map failed with %x\n", rval));
  3238. }
  3239. }
  3240. else {
  3241. con_log(CL_ANN, (KERN_NOTICE
  3242. "megaraid: could not issue ldmap command:%x\n", rval));
  3243. }
  3244. del_timer_sync(timerp);
  3245. up(&raid_dev->sysfs_sem);
  3246. return rval;
  3247. }
  3248. /**
  3249. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3250. *
  3251. * Display the handle used by the applications while executing management
  3252. * tasks on the adapter. We invoke a management module API to get the adapter
  3253. * handle, since we do not interface with applications directly.
  3254. *
  3255. * @param cdev : class device object representation for the host
  3256. * @param buf : buffer to send data to
  3257. */
  3258. static ssize_t
  3259. megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
  3260. {
  3261. struct Scsi_Host *shost = class_to_shost(cdev);
  3262. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3263. uint32_t app_hndl;
  3264. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3265. return snprintf(buf, 8, "%u\n", app_hndl);
  3266. }
  3267. /**
  3268. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3269. *
  3270. * Display the logical drive number for the device in question, if it a valid
  3271. * logical drive. For physical devices, "-1" is returned
  3272. * The logical drive number is displayed in following format
  3273. *
  3274. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3275. * <int> <int> <int> <int>
  3276. *
  3277. * @param dev : device object representation for the scsi device
  3278. * @param buf : buffer to send data to
  3279. */
  3280. static ssize_t
  3281. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3282. {
  3283. struct scsi_device *sdev = to_scsi_device(dev);
  3284. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3285. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3286. int scsi_id = -1;
  3287. int logical_drv = -1;
  3288. int ldid_map = -1;
  3289. uint32_t app_hndl = 0;
  3290. int mapped_sdev_id;
  3291. int rval;
  3292. int i;
  3293. if (raid_dev->random_del_supported &&
  3294. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3295. rval = megaraid_sysfs_get_ldmap(adapter);
  3296. if (rval == 0) {
  3297. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3298. mapped_sdev_id = sdev->id;
  3299. if (sdev->id > adapter->init_id) {
  3300. mapped_sdev_id -= 1;
  3301. }
  3302. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3303. scsi_id = sdev->id;
  3304. logical_drv = i;
  3305. ldid_map = raid_dev->curr_ldmap[i];
  3306. app_hndl = mraid_mm_adapter_app_handle(
  3307. adapter->unique_id);
  3308. break;
  3309. }
  3310. }
  3311. }
  3312. else {
  3313. con_log(CL_ANN, (KERN_NOTICE
  3314. "megaraid: sysfs get ld map failed: %x\n",
  3315. rval));
  3316. }
  3317. }
  3318. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3319. ldid_map, app_hndl);
  3320. }
  3321. /*
  3322. * END: Mailbox Low Level Driver
  3323. */
  3324. module_init(megaraid_init);
  3325. module_exit(megaraid_exit);
  3326. /* vim: set ts=8 sw=8 tw=78 ai si: */