megaraid_mbox.c 100 KB

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