qla_init.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <linux/vmalloc.h>
  10. #include "qla_devtbl.h"
  11. /* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
  12. #ifndef EXT_IS_LUN_BIT_SET
  13. #define EXT_IS_LUN_BIT_SET(P,L) \
  14. (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
  15. #define EXT_SET_LUN_BIT(P,L) \
  16. ((P)->mask[L/8] |= (0x80 >> (L%8)))
  17. #endif
  18. /*
  19. * QLogic ISP2x00 Hardware Support Function Prototypes.
  20. */
  21. static int qla2x00_isp_firmware(scsi_qla_host_t *);
  22. static void qla2x00_resize_request_q(scsi_qla_host_t *);
  23. static int qla2x00_setup_chip(scsi_qla_host_t *);
  24. static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
  25. static int qla2x00_init_rings(scsi_qla_host_t *);
  26. static int qla2x00_fw_ready(scsi_qla_host_t *);
  27. static int qla2x00_configure_hba(scsi_qla_host_t *);
  28. static int qla2x00_configure_loop(scsi_qla_host_t *);
  29. static int qla2x00_configure_local_loop(scsi_qla_host_t *);
  30. static int qla2x00_configure_fabric(scsi_qla_host_t *);
  31. static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
  32. static int qla2x00_device_resync(scsi_qla_host_t *);
  33. static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
  34. uint16_t *);
  35. static int qla2x00_restart_isp(scsi_qla_host_t *);
  36. static int qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev);
  37. /****************************************************************************/
  38. /* QLogic ISP2x00 Hardware Support Functions. */
  39. /****************************************************************************/
  40. /*
  41. * qla2x00_initialize_adapter
  42. * Initialize board.
  43. *
  44. * Input:
  45. * ha = adapter block pointer.
  46. *
  47. * Returns:
  48. * 0 = success
  49. */
  50. int
  51. qla2x00_initialize_adapter(scsi_qla_host_t *ha)
  52. {
  53. int rval;
  54. uint8_t restart_risc = 0;
  55. uint8_t retry;
  56. uint32_t wait_time;
  57. /* Clear adapter flags. */
  58. ha->flags.online = 0;
  59. ha->flags.reset_active = 0;
  60. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  61. atomic_set(&ha->loop_state, LOOP_DOWN);
  62. ha->device_flags = 0;
  63. ha->dpc_flags = 0;
  64. ha->flags.management_server_logged_in = 0;
  65. ha->marker_needed = 0;
  66. ha->mbx_flags = 0;
  67. ha->isp_abort_cnt = 0;
  68. ha->beacon_blink_led = 0;
  69. set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
  70. qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
  71. rval = ha->isp_ops.pci_config(ha);
  72. if (rval) {
  73. DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
  74. ha->host_no));
  75. return (rval);
  76. }
  77. ha->isp_ops.reset_chip(ha);
  78. qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
  79. ha->isp_ops.nvram_config(ha);
  80. if (ha->flags.disable_serdes) {
  81. /* Mask HBA via NVRAM settings? */
  82. qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
  83. "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
  84. ha->port_name[0], ha->port_name[1],
  85. ha->port_name[2], ha->port_name[3],
  86. ha->port_name[4], ha->port_name[5],
  87. ha->port_name[6], ha->port_name[7]);
  88. return QLA_FUNCTION_FAILED;
  89. }
  90. qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
  91. retry = 10;
  92. /*
  93. * Try to configure the loop.
  94. */
  95. do {
  96. restart_risc = 0;
  97. /* If firmware needs to be loaded */
  98. if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
  99. if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
  100. rval = qla2x00_setup_chip(ha);
  101. }
  102. }
  103. if (rval == QLA_SUCCESS &&
  104. (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
  105. check_fw_ready_again:
  106. /*
  107. * Wait for a successful LIP up to a maximum
  108. * of (in seconds): RISC login timeout value,
  109. * RISC retry count value, and port down retry
  110. * value OR a minimum of 4 seconds OR If no
  111. * cable, only 5 seconds.
  112. */
  113. rval = qla2x00_fw_ready(ha);
  114. if (rval == QLA_SUCCESS) {
  115. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  116. /* Issue a marker after FW becomes ready. */
  117. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  118. /*
  119. * Wait at most MAX_TARGET RSCNs for a stable
  120. * link.
  121. */
  122. wait_time = 256;
  123. do {
  124. clear_bit(LOOP_RESYNC_NEEDED,
  125. &ha->dpc_flags);
  126. rval = qla2x00_configure_loop(ha);
  127. if (test_and_clear_bit(ISP_ABORT_NEEDED,
  128. &ha->dpc_flags)) {
  129. restart_risc = 1;
  130. break;
  131. }
  132. /*
  133. * If loop state change while we were
  134. * discoverying devices then wait for
  135. * LIP to complete
  136. */
  137. if (atomic_read(&ha->loop_state) !=
  138. LOOP_READY && retry--) {
  139. goto check_fw_ready_again;
  140. }
  141. wait_time--;
  142. } while (!atomic_read(&ha->loop_down_timer) &&
  143. retry &&
  144. wait_time &&
  145. (test_bit(LOOP_RESYNC_NEEDED,
  146. &ha->dpc_flags)));
  147. if (wait_time == 0)
  148. rval = QLA_FUNCTION_FAILED;
  149. } else if (ha->device_flags & DFLG_NO_CABLE)
  150. /* If no cable, then all is good. */
  151. rval = QLA_SUCCESS;
  152. }
  153. } while (restart_risc && retry--);
  154. if (rval == QLA_SUCCESS) {
  155. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  156. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  157. ha->marker_needed = 0;
  158. ha->flags.online = 1;
  159. } else {
  160. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  161. }
  162. return (rval);
  163. }
  164. /**
  165. * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
  166. * @ha: HA context
  167. *
  168. * Returns 0 on success.
  169. */
  170. int
  171. qla2100_pci_config(scsi_qla_host_t *ha)
  172. {
  173. uint16_t w, mwi;
  174. uint32_t d;
  175. unsigned long flags;
  176. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  177. pci_set_master(ha->pdev);
  178. mwi = 0;
  179. if (pci_set_mwi(ha->pdev))
  180. mwi = PCI_COMMAND_INVALIDATE;
  181. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  182. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  183. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  184. /* Reset expansion ROM address decode enable */
  185. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  186. d &= ~PCI_ROM_ADDRESS_ENABLE;
  187. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  188. /* Get PCI bus information. */
  189. spin_lock_irqsave(&ha->hardware_lock, flags);
  190. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  191. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  192. return QLA_SUCCESS;
  193. }
  194. /**
  195. * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
  196. * @ha: HA context
  197. *
  198. * Returns 0 on success.
  199. */
  200. int
  201. qla2300_pci_config(scsi_qla_host_t *ha)
  202. {
  203. uint16_t w, mwi;
  204. uint32_t d;
  205. unsigned long flags = 0;
  206. uint32_t cnt;
  207. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  208. pci_set_master(ha->pdev);
  209. mwi = 0;
  210. if (pci_set_mwi(ha->pdev))
  211. mwi = PCI_COMMAND_INVALIDATE;
  212. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  213. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  214. if (IS_QLA2322(ha) || IS_QLA6322(ha))
  215. w &= ~PCI_COMMAND_INTX_DISABLE;
  216. /*
  217. * If this is a 2300 card and not 2312, reset the
  218. * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
  219. * the 2310 also reports itself as a 2300 so we need to get the
  220. * fb revision level -- a 6 indicates it really is a 2300 and
  221. * not a 2310.
  222. */
  223. if (IS_QLA2300(ha)) {
  224. spin_lock_irqsave(&ha->hardware_lock, flags);
  225. /* Pause RISC. */
  226. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  227. for (cnt = 0; cnt < 30000; cnt++) {
  228. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  229. break;
  230. udelay(10);
  231. }
  232. /* Select FPM registers. */
  233. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  234. RD_REG_WORD(&reg->ctrl_status);
  235. /* Get the fb rev level */
  236. ha->fb_rev = RD_FB_CMD_REG(ha, reg);
  237. if (ha->fb_rev == FPM_2300)
  238. w &= ~PCI_COMMAND_INVALIDATE;
  239. /* Deselect FPM registers. */
  240. WRT_REG_WORD(&reg->ctrl_status, 0x0);
  241. RD_REG_WORD(&reg->ctrl_status);
  242. /* Release RISC module. */
  243. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  244. for (cnt = 0; cnt < 30000; cnt++) {
  245. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
  246. break;
  247. udelay(10);
  248. }
  249. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  250. }
  251. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  252. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  253. /* Reset expansion ROM address decode enable */
  254. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  255. d &= ~PCI_ROM_ADDRESS_ENABLE;
  256. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  257. /* Get PCI bus information. */
  258. spin_lock_irqsave(&ha->hardware_lock, flags);
  259. ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
  260. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  261. return QLA_SUCCESS;
  262. }
  263. /**
  264. * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
  265. * @ha: HA context
  266. *
  267. * Returns 0 on success.
  268. */
  269. int
  270. qla24xx_pci_config(scsi_qla_host_t *ha)
  271. {
  272. uint16_t w, mwi;
  273. uint32_t d;
  274. unsigned long flags = 0;
  275. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  276. int pcix_cmd_reg, pcie_dctl_reg;
  277. pci_set_master(ha->pdev);
  278. mwi = 0;
  279. if (pci_set_mwi(ha->pdev))
  280. mwi = PCI_COMMAND_INVALIDATE;
  281. pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
  282. w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
  283. w &= ~PCI_COMMAND_INTX_DISABLE;
  284. pci_write_config_word(ha->pdev, PCI_COMMAND, w);
  285. pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
  286. /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
  287. pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
  288. if (pcix_cmd_reg) {
  289. uint16_t pcix_cmd;
  290. pcix_cmd_reg += PCI_X_CMD;
  291. pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
  292. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  293. pcix_cmd |= 0x0008;
  294. pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
  295. }
  296. /* PCIe -- adjust Maximum Read Request Size (2048). */
  297. pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  298. if (pcie_dctl_reg) {
  299. uint16_t pcie_dctl;
  300. pcie_dctl_reg += PCI_EXP_DEVCTL;
  301. pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
  302. pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
  303. pcie_dctl |= 0x4000;
  304. pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
  305. }
  306. /* Reset expansion ROM address decode enable */
  307. pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
  308. d &= ~PCI_ROM_ADDRESS_ENABLE;
  309. pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
  310. /* Get PCI bus information. */
  311. spin_lock_irqsave(&ha->hardware_lock, flags);
  312. ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
  313. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  314. return QLA_SUCCESS;
  315. }
  316. /**
  317. * qla2x00_isp_firmware() - Choose firmware image.
  318. * @ha: HA context
  319. *
  320. * Returns 0 on success.
  321. */
  322. static int
  323. qla2x00_isp_firmware(scsi_qla_host_t *ha)
  324. {
  325. int rval;
  326. /* Assume loading risc code */
  327. rval = QLA_FUNCTION_FAILED;
  328. if (ha->flags.disable_risc_code_load) {
  329. DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
  330. ha->host_no));
  331. qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
  332. /* Verify checksum of loaded RISC code. */
  333. rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
  334. }
  335. if (rval) {
  336. DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
  337. ha->host_no));
  338. }
  339. return (rval);
  340. }
  341. /**
  342. * qla2x00_reset_chip() - Reset ISP chip.
  343. * @ha: HA context
  344. *
  345. * Returns 0 on success.
  346. */
  347. void
  348. qla2x00_reset_chip(scsi_qla_host_t *ha)
  349. {
  350. unsigned long flags = 0;
  351. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  352. uint32_t cnt;
  353. uint16_t cmd;
  354. ha->isp_ops.disable_intrs(ha);
  355. spin_lock_irqsave(&ha->hardware_lock, flags);
  356. /* Turn off master enable */
  357. cmd = 0;
  358. pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
  359. cmd &= ~PCI_COMMAND_MASTER;
  360. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  361. if (!IS_QLA2100(ha)) {
  362. /* Pause RISC. */
  363. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  364. if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
  365. for (cnt = 0; cnt < 30000; cnt++) {
  366. if ((RD_REG_WORD(&reg->hccr) &
  367. HCCR_RISC_PAUSE) != 0)
  368. break;
  369. udelay(100);
  370. }
  371. } else {
  372. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  373. udelay(10);
  374. }
  375. /* Select FPM registers. */
  376. WRT_REG_WORD(&reg->ctrl_status, 0x20);
  377. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  378. /* FPM Soft Reset. */
  379. WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
  380. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  381. /* Toggle Fpm Reset. */
  382. if (!IS_QLA2200(ha)) {
  383. WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
  384. RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
  385. }
  386. /* Select frame buffer registers. */
  387. WRT_REG_WORD(&reg->ctrl_status, 0x10);
  388. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  389. /* Reset frame buffer FIFOs. */
  390. if (IS_QLA2200(ha)) {
  391. WRT_FB_CMD_REG(ha, reg, 0xa000);
  392. RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
  393. } else {
  394. WRT_FB_CMD_REG(ha, reg, 0x00fc);
  395. /* Read back fb_cmd until zero or 3 seconds max */
  396. for (cnt = 0; cnt < 3000; cnt++) {
  397. if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
  398. break;
  399. udelay(100);
  400. }
  401. }
  402. /* Select RISC module registers. */
  403. WRT_REG_WORD(&reg->ctrl_status, 0);
  404. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  405. /* Reset RISC processor. */
  406. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  407. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  408. /* Release RISC processor. */
  409. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  410. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  411. }
  412. WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
  413. WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
  414. /* Reset ISP chip. */
  415. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  416. /* Wait for RISC to recover from reset. */
  417. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  418. /*
  419. * It is necessary to for a delay here since the card doesn't
  420. * respond to PCI reads during a reset. On some architectures
  421. * this will result in an MCA.
  422. */
  423. udelay(20);
  424. for (cnt = 30000; cnt; cnt--) {
  425. if ((RD_REG_WORD(&reg->ctrl_status) &
  426. CSR_ISP_SOFT_RESET) == 0)
  427. break;
  428. udelay(100);
  429. }
  430. } else
  431. udelay(10);
  432. /* Reset RISC processor. */
  433. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  434. WRT_REG_WORD(&reg->semaphore, 0);
  435. /* Release RISC processor. */
  436. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  437. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  438. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  439. for (cnt = 0; cnt < 30000; cnt++) {
  440. if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
  441. break;
  442. udelay(100);
  443. }
  444. } else
  445. udelay(100);
  446. /* Turn on master enable */
  447. cmd |= PCI_COMMAND_MASTER;
  448. pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
  449. /* Disable RISC pause on FPM parity error. */
  450. if (!IS_QLA2100(ha)) {
  451. WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
  452. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  453. }
  454. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  455. }
  456. /**
  457. * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
  458. * @ha: HA context
  459. *
  460. * Returns 0 on success.
  461. */
  462. static inline void
  463. qla24xx_reset_risc(scsi_qla_host_t *ha)
  464. {
  465. unsigned long flags = 0;
  466. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  467. uint32_t cnt, d2;
  468. uint16_t wd;
  469. spin_lock_irqsave(&ha->hardware_lock, flags);
  470. /* Reset RISC. */
  471. WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  472. for (cnt = 0; cnt < 30000; cnt++) {
  473. if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
  474. break;
  475. udelay(10);
  476. }
  477. WRT_REG_DWORD(&reg->ctrl_status,
  478. CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
  479. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  480. udelay(100);
  481. /* Wait for firmware to complete NVRAM accesses. */
  482. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  483. for (cnt = 10000 ; cnt && d2; cnt--) {
  484. udelay(5);
  485. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  486. barrier();
  487. }
  488. /* Wait for soft-reset to complete. */
  489. d2 = RD_REG_DWORD(&reg->ctrl_status);
  490. for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
  491. udelay(5);
  492. d2 = RD_REG_DWORD(&reg->ctrl_status);
  493. barrier();
  494. }
  495. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  496. RD_REG_DWORD(&reg->hccr);
  497. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  498. RD_REG_DWORD(&reg->hccr);
  499. WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
  500. RD_REG_DWORD(&reg->hccr);
  501. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  502. for (cnt = 6000000 ; cnt && d2; cnt--) {
  503. udelay(5);
  504. d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
  505. barrier();
  506. }
  507. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  508. }
  509. /**
  510. * qla24xx_reset_chip() - Reset ISP24xx chip.
  511. * @ha: HA context
  512. *
  513. * Returns 0 on success.
  514. */
  515. void
  516. qla24xx_reset_chip(scsi_qla_host_t *ha)
  517. {
  518. ha->isp_ops.disable_intrs(ha);
  519. /* Perform RISC reset. */
  520. qla24xx_reset_risc(ha);
  521. }
  522. /**
  523. * qla2x00_chip_diag() - Test chip for proper operation.
  524. * @ha: HA context
  525. *
  526. * Returns 0 on success.
  527. */
  528. int
  529. qla2x00_chip_diag(scsi_qla_host_t *ha)
  530. {
  531. int rval;
  532. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  533. unsigned long flags = 0;
  534. uint16_t data;
  535. uint32_t cnt;
  536. uint16_t mb[5];
  537. /* Assume a failed state */
  538. rval = QLA_FUNCTION_FAILED;
  539. DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
  540. ha->host_no, (u_long)&reg->flash_address));
  541. spin_lock_irqsave(&ha->hardware_lock, flags);
  542. /* Reset ISP chip. */
  543. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  544. /*
  545. * We need to have a delay here since the card will not respond while
  546. * in reset causing an MCA on some architectures.
  547. */
  548. udelay(20);
  549. data = qla2x00_debounce_register(&reg->ctrl_status);
  550. for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
  551. udelay(5);
  552. data = RD_REG_WORD(&reg->ctrl_status);
  553. barrier();
  554. }
  555. if (!cnt)
  556. goto chip_diag_failed;
  557. DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
  558. ha->host_no));
  559. /* Reset RISC processor. */
  560. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  561. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  562. /* Workaround for QLA2312 PCI parity error */
  563. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  564. data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
  565. for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
  566. udelay(5);
  567. data = RD_MAILBOX_REG(ha, reg, 0);
  568. barrier();
  569. }
  570. } else
  571. udelay(10);
  572. if (!cnt)
  573. goto chip_diag_failed;
  574. /* Check product ID of chip */
  575. DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
  576. mb[1] = RD_MAILBOX_REG(ha, reg, 1);
  577. mb[2] = RD_MAILBOX_REG(ha, reg, 2);
  578. mb[3] = RD_MAILBOX_REG(ha, reg, 3);
  579. mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
  580. if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
  581. mb[3] != PROD_ID_3) {
  582. qla_printk(KERN_WARNING, ha,
  583. "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
  584. goto chip_diag_failed;
  585. }
  586. ha->product_id[0] = mb[1];
  587. ha->product_id[1] = mb[2];
  588. ha->product_id[2] = mb[3];
  589. ha->product_id[3] = mb[4];
  590. /* Adjust fw RISC transfer size */
  591. if (ha->request_q_length > 1024)
  592. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  593. else
  594. ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
  595. ha->request_q_length;
  596. if (IS_QLA2200(ha) &&
  597. RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
  598. /* Limit firmware transfer size with a 2200A */
  599. DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
  600. ha->host_no));
  601. ha->device_type |= DT_ISP2200A;
  602. ha->fw_transfer_size = 128;
  603. }
  604. /* Wrap Incoming Mailboxes Test. */
  605. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  606. DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
  607. rval = qla2x00_mbx_reg_test(ha);
  608. if (rval) {
  609. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  610. ha->host_no));
  611. qla_printk(KERN_WARNING, ha,
  612. "Failed mailbox send register test\n");
  613. }
  614. else {
  615. /* Flag a successful rval */
  616. rval = QLA_SUCCESS;
  617. }
  618. spin_lock_irqsave(&ha->hardware_lock, flags);
  619. chip_diag_failed:
  620. if (rval)
  621. DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
  622. "****\n", ha->host_no));
  623. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  624. return (rval);
  625. }
  626. /**
  627. * qla24xx_chip_diag() - Test ISP24xx for proper operation.
  628. * @ha: HA context
  629. *
  630. * Returns 0 on success.
  631. */
  632. int
  633. qla24xx_chip_diag(scsi_qla_host_t *ha)
  634. {
  635. int rval;
  636. /* Perform RISC reset. */
  637. qla24xx_reset_risc(ha);
  638. ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
  639. rval = qla2x00_mbx_reg_test(ha);
  640. if (rval) {
  641. DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
  642. ha->host_no));
  643. qla_printk(KERN_WARNING, ha,
  644. "Failed mailbox send register test\n");
  645. } else {
  646. /* Flag a successful rval */
  647. rval = QLA_SUCCESS;
  648. }
  649. return rval;
  650. }
  651. void
  652. qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
  653. {
  654. int rval;
  655. uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
  656. eft_size;
  657. dma_addr_t eft_dma;
  658. void *eft;
  659. if (ha->fw_dump) {
  660. qla_printk(KERN_WARNING, ha,
  661. "Firmware dump previously allocated.\n");
  662. return;
  663. }
  664. ha->fw_dumped = 0;
  665. fixed_size = mem_size = eft_size = 0;
  666. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  667. fixed_size = sizeof(struct qla2100_fw_dump);
  668. } else if (IS_QLA23XX(ha)) {
  669. fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
  670. mem_size = (ha->fw_memory_size - 0x11000 + 1) *
  671. sizeof(uint16_t);
  672. } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
  673. fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
  674. mem_size = (ha->fw_memory_size - 0x100000 + 1) *
  675. sizeof(uint32_t);
  676. /* Allocate memory for Extended Trace Buffer. */
  677. eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
  678. GFP_KERNEL);
  679. if (!eft) {
  680. qla_printk(KERN_WARNING, ha, "Unable to allocate "
  681. "(%d KB) for EFT.\n", EFT_SIZE / 1024);
  682. goto cont_alloc;
  683. }
  684. rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
  685. EFT_NUM_BUFFERS);
  686. if (rval) {
  687. qla_printk(KERN_WARNING, ha, "Unable to initialize "
  688. "EFT (%d).\n", rval);
  689. dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
  690. eft_dma);
  691. goto cont_alloc;
  692. }
  693. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
  694. EFT_SIZE / 1024);
  695. eft_size = EFT_SIZE;
  696. memset(eft, 0, eft_size);
  697. ha->eft_dma = eft_dma;
  698. ha->eft = eft;
  699. }
  700. cont_alloc:
  701. req_q_size = ha->request_q_length * sizeof(request_t);
  702. rsp_q_size = ha->response_q_length * sizeof(response_t);
  703. dump_size = offsetof(struct qla2xxx_fw_dump, isp);
  704. dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
  705. eft_size;
  706. ha->fw_dump = vmalloc(dump_size);
  707. if (!ha->fw_dump) {
  708. qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
  709. "firmware dump!!!\n", dump_size / 1024);
  710. if (ha->eft) {
  711. dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
  712. ha->eft_dma);
  713. ha->eft = NULL;
  714. ha->eft_dma = 0;
  715. }
  716. return;
  717. }
  718. qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
  719. dump_size / 1024);
  720. ha->fw_dump_len = dump_size;
  721. ha->fw_dump->signature[0] = 'Q';
  722. ha->fw_dump->signature[1] = 'L';
  723. ha->fw_dump->signature[2] = 'G';
  724. ha->fw_dump->signature[3] = 'C';
  725. ha->fw_dump->version = __constant_htonl(1);
  726. ha->fw_dump->fixed_size = htonl(fixed_size);
  727. ha->fw_dump->mem_size = htonl(mem_size);
  728. ha->fw_dump->req_q_size = htonl(req_q_size);
  729. ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
  730. ha->fw_dump->eft_size = htonl(eft_size);
  731. ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
  732. ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
  733. ha->fw_dump->header_size =
  734. htonl(offsetof(struct qla2xxx_fw_dump, isp));
  735. }
  736. /**
  737. * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
  738. * @ha: HA context
  739. *
  740. * Returns 0 on success.
  741. */
  742. static void
  743. qla2x00_resize_request_q(scsi_qla_host_t *ha)
  744. {
  745. int rval;
  746. uint16_t fw_iocb_cnt = 0;
  747. uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
  748. dma_addr_t request_dma;
  749. request_t *request_ring;
  750. /* Valid only on recent ISPs. */
  751. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  752. return;
  753. /* Retrieve IOCB counts available to the firmware. */
  754. rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
  755. if (rval)
  756. return;
  757. /* No point in continuing if current settings are sufficient. */
  758. if (fw_iocb_cnt < 1024)
  759. return;
  760. if (ha->request_q_length >= request_q_length)
  761. return;
  762. /* Attempt to claim larger area for request queue. */
  763. request_ring = dma_alloc_coherent(&ha->pdev->dev,
  764. (request_q_length + 1) * sizeof(request_t), &request_dma,
  765. GFP_KERNEL);
  766. if (request_ring == NULL)
  767. return;
  768. /* Resize successful, report extensions. */
  769. qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
  770. (ha->fw_memory_size + 1) / 1024);
  771. qla_printk(KERN_INFO, ha, "Resizing request queue depth "
  772. "(%d -> %d)...\n", ha->request_q_length, request_q_length);
  773. /* Clear old allocations. */
  774. dma_free_coherent(&ha->pdev->dev,
  775. (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
  776. ha->request_dma);
  777. /* Begin using larger queue. */
  778. ha->request_q_length = request_q_length;
  779. ha->request_ring = request_ring;
  780. ha->request_dma = request_dma;
  781. }
  782. /**
  783. * qla2x00_setup_chip() - Load and start RISC firmware.
  784. * @ha: HA context
  785. *
  786. * Returns 0 on success.
  787. */
  788. static int
  789. qla2x00_setup_chip(scsi_qla_host_t *ha)
  790. {
  791. int rval;
  792. uint32_t srisc_address = 0;
  793. /* Load firmware sequences */
  794. rval = ha->isp_ops.load_risc(ha, &srisc_address);
  795. if (rval == QLA_SUCCESS) {
  796. DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
  797. "code.\n", ha->host_no));
  798. rval = qla2x00_verify_checksum(ha, srisc_address);
  799. if (rval == QLA_SUCCESS) {
  800. /* Start firmware execution. */
  801. DEBUG(printk("scsi(%ld): Checksum OK, start "
  802. "firmware.\n", ha->host_no));
  803. rval = qla2x00_execute_fw(ha, srisc_address);
  804. /* Retrieve firmware information. */
  805. if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
  806. qla2x00_get_fw_version(ha,
  807. &ha->fw_major_version,
  808. &ha->fw_minor_version,
  809. &ha->fw_subminor_version,
  810. &ha->fw_attributes, &ha->fw_memory_size);
  811. qla2x00_resize_request_q(ha);
  812. if (ql2xallocfwdump)
  813. qla2x00_alloc_fw_dump(ha);
  814. }
  815. } else {
  816. DEBUG2(printk(KERN_INFO
  817. "scsi(%ld): ISP Firmware failed checksum.\n",
  818. ha->host_no));
  819. }
  820. }
  821. if (rval) {
  822. DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
  823. ha->host_no));
  824. }
  825. return (rval);
  826. }
  827. /**
  828. * qla2x00_init_response_q_entries() - Initializes response queue entries.
  829. * @ha: HA context
  830. *
  831. * Beginning of request ring has initialization control block already built
  832. * by nvram config routine.
  833. *
  834. * Returns 0 on success.
  835. */
  836. static void
  837. qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
  838. {
  839. uint16_t cnt;
  840. response_t *pkt;
  841. pkt = ha->response_ring_ptr;
  842. for (cnt = 0; cnt < ha->response_q_length; cnt++) {
  843. pkt->signature = RESPONSE_PROCESSED;
  844. pkt++;
  845. }
  846. }
  847. /**
  848. * qla2x00_update_fw_options() - Read and process firmware options.
  849. * @ha: HA context
  850. *
  851. * Returns 0 on success.
  852. */
  853. void
  854. qla2x00_update_fw_options(scsi_qla_host_t *ha)
  855. {
  856. uint16_t swing, emphasis, tx_sens, rx_sens;
  857. memset(ha->fw_options, 0, sizeof(ha->fw_options));
  858. qla2x00_get_fw_options(ha, ha->fw_options);
  859. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  860. return;
  861. /* Serial Link options. */
  862. DEBUG3(printk("scsi(%ld): Serial link options:\n",
  863. ha->host_no));
  864. DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
  865. sizeof(ha->fw_seriallink_options)));
  866. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  867. if (ha->fw_seriallink_options[3] & BIT_2) {
  868. ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
  869. /* 1G settings */
  870. swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
  871. emphasis = (ha->fw_seriallink_options[2] &
  872. (BIT_4 | BIT_3)) >> 3;
  873. tx_sens = ha->fw_seriallink_options[0] &
  874. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  875. rx_sens = (ha->fw_seriallink_options[0] &
  876. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  877. ha->fw_options[10] = (emphasis << 14) | (swing << 8);
  878. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  879. if (rx_sens == 0x0)
  880. rx_sens = 0x3;
  881. ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
  882. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  883. ha->fw_options[10] |= BIT_5 |
  884. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  885. (tx_sens & (BIT_1 | BIT_0));
  886. /* 2G settings */
  887. swing = (ha->fw_seriallink_options[2] &
  888. (BIT_7 | BIT_6 | BIT_5)) >> 5;
  889. emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
  890. tx_sens = ha->fw_seriallink_options[1] &
  891. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  892. rx_sens = (ha->fw_seriallink_options[1] &
  893. (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
  894. ha->fw_options[11] = (emphasis << 14) | (swing << 8);
  895. if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
  896. if (rx_sens == 0x0)
  897. rx_sens = 0x3;
  898. ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
  899. } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
  900. ha->fw_options[11] |= BIT_5 |
  901. ((rx_sens & (BIT_1 | BIT_0)) << 2) |
  902. (tx_sens & (BIT_1 | BIT_0));
  903. }
  904. /* FCP2 options. */
  905. /* Return command IOCBs without waiting for an ABTS to complete. */
  906. ha->fw_options[3] |= BIT_13;
  907. /* LED scheme. */
  908. if (ha->flags.enable_led_scheme)
  909. ha->fw_options[2] |= BIT_12;
  910. /* Detect ISP6312. */
  911. if (IS_QLA6312(ha))
  912. ha->fw_options[2] |= BIT_13;
  913. /* Update firmware options. */
  914. qla2x00_set_fw_options(ha, ha->fw_options);
  915. }
  916. void
  917. qla24xx_update_fw_options(scsi_qla_host_t *ha)
  918. {
  919. int rval;
  920. /* Update Serial Link options. */
  921. if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
  922. return;
  923. rval = qla2x00_set_serdes_params(ha,
  924. le16_to_cpu(ha->fw_seriallink_options24[1]),
  925. le16_to_cpu(ha->fw_seriallink_options24[2]),
  926. le16_to_cpu(ha->fw_seriallink_options24[3]));
  927. if (rval != QLA_SUCCESS) {
  928. qla_printk(KERN_WARNING, ha,
  929. "Unable to update Serial Link options (%x).\n", rval);
  930. }
  931. }
  932. void
  933. qla2x00_config_rings(struct scsi_qla_host *ha)
  934. {
  935. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  936. /* Setup ring parameters in initialization control block. */
  937. ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
  938. ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
  939. ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
  940. ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
  941. ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  942. ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  943. ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  944. ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  945. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
  946. WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
  947. WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
  948. WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
  949. RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
  950. }
  951. void
  952. qla24xx_config_rings(struct scsi_qla_host *ha)
  953. {
  954. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  955. struct init_cb_24xx *icb;
  956. /* Setup ring parameters in initialization control block. */
  957. icb = (struct init_cb_24xx *)ha->init_cb;
  958. icb->request_q_outpointer = __constant_cpu_to_le16(0);
  959. icb->response_q_inpointer = __constant_cpu_to_le16(0);
  960. icb->request_q_length = cpu_to_le16(ha->request_q_length);
  961. icb->response_q_length = cpu_to_le16(ha->response_q_length);
  962. icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
  963. icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
  964. icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
  965. icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
  966. WRT_REG_DWORD(&reg->req_q_in, 0);
  967. WRT_REG_DWORD(&reg->req_q_out, 0);
  968. WRT_REG_DWORD(&reg->rsp_q_in, 0);
  969. WRT_REG_DWORD(&reg->rsp_q_out, 0);
  970. RD_REG_DWORD(&reg->rsp_q_out);
  971. }
  972. /**
  973. * qla2x00_init_rings() - Initializes firmware.
  974. * @ha: HA context
  975. *
  976. * Beginning of request ring has initialization control block already built
  977. * by nvram config routine.
  978. *
  979. * Returns 0 on success.
  980. */
  981. static int
  982. qla2x00_init_rings(scsi_qla_host_t *ha)
  983. {
  984. int rval;
  985. unsigned long flags = 0;
  986. int cnt;
  987. spin_lock_irqsave(&ha->hardware_lock, flags);
  988. /* Clear outstanding commands array. */
  989. for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
  990. ha->outstanding_cmds[cnt] = NULL;
  991. ha->current_outstanding_cmd = 0;
  992. /* Clear RSCN queue. */
  993. ha->rscn_in_ptr = 0;
  994. ha->rscn_out_ptr = 0;
  995. /* Initialize firmware. */
  996. ha->request_ring_ptr = ha->request_ring;
  997. ha->req_ring_index = 0;
  998. ha->req_q_cnt = ha->request_q_length;
  999. ha->response_ring_ptr = ha->response_ring;
  1000. ha->rsp_ring_index = 0;
  1001. /* Initialize response queue entries */
  1002. qla2x00_init_response_q_entries(ha);
  1003. ha->isp_ops.config_rings(ha);
  1004. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1005. /* Update any ISP specific firmware options before initialization. */
  1006. ha->isp_ops.update_fw_options(ha);
  1007. DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
  1008. rval = qla2x00_init_firmware(ha, ha->init_cb_size);
  1009. if (rval) {
  1010. DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
  1011. ha->host_no));
  1012. } else {
  1013. DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
  1014. ha->host_no));
  1015. }
  1016. return (rval);
  1017. }
  1018. /**
  1019. * qla2x00_fw_ready() - Waits for firmware ready.
  1020. * @ha: HA context
  1021. *
  1022. * Returns 0 on success.
  1023. */
  1024. static int
  1025. qla2x00_fw_ready(scsi_qla_host_t *ha)
  1026. {
  1027. int rval;
  1028. unsigned long wtime, mtime;
  1029. uint16_t min_wait; /* Minimum wait time if loop is down */
  1030. uint16_t wait_time; /* Wait time if loop is coming ready */
  1031. uint16_t fw_state;
  1032. rval = QLA_SUCCESS;
  1033. /* 20 seconds for loop down. */
  1034. min_wait = 20;
  1035. /*
  1036. * Firmware should take at most one RATOV to login, plus 5 seconds for
  1037. * our own processing.
  1038. */
  1039. if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
  1040. wait_time = min_wait;
  1041. }
  1042. /* Min wait time if loop down */
  1043. mtime = jiffies + (min_wait * HZ);
  1044. /* wait time before firmware ready */
  1045. wtime = jiffies + (wait_time * HZ);
  1046. /* Wait for ISP to finish LIP */
  1047. if (!ha->flags.init_done)
  1048. qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
  1049. DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
  1050. ha->host_no));
  1051. do {
  1052. rval = qla2x00_get_firmware_state(ha, &fw_state);
  1053. if (rval == QLA_SUCCESS) {
  1054. if (fw_state < FSTATE_LOSS_OF_SYNC) {
  1055. ha->device_flags &= ~DFLG_NO_CABLE;
  1056. }
  1057. if (fw_state == FSTATE_READY) {
  1058. DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
  1059. ha->host_no));
  1060. qla2x00_get_retry_cnt(ha, &ha->retry_count,
  1061. &ha->login_timeout, &ha->r_a_tov);
  1062. rval = QLA_SUCCESS;
  1063. break;
  1064. }
  1065. rval = QLA_FUNCTION_FAILED;
  1066. if (atomic_read(&ha->loop_down_timer) &&
  1067. fw_state != FSTATE_READY) {
  1068. /* Loop down. Timeout on min_wait for states
  1069. * other than Wait for Login.
  1070. */
  1071. if (time_after_eq(jiffies, mtime)) {
  1072. qla_printk(KERN_INFO, ha,
  1073. "Cable is unplugged...\n");
  1074. ha->device_flags |= DFLG_NO_CABLE;
  1075. break;
  1076. }
  1077. }
  1078. } else {
  1079. /* Mailbox cmd failed. Timeout on min_wait. */
  1080. if (time_after_eq(jiffies, mtime))
  1081. break;
  1082. }
  1083. if (time_after_eq(jiffies, wtime))
  1084. break;
  1085. /* Delay for a while */
  1086. msleep(500);
  1087. DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1088. ha->host_no, fw_state, jiffies));
  1089. } while (1);
  1090. DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
  1091. ha->host_no, fw_state, jiffies));
  1092. if (rval) {
  1093. DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
  1094. ha->host_no));
  1095. }
  1096. return (rval);
  1097. }
  1098. /*
  1099. * qla2x00_configure_hba
  1100. * Setup adapter context.
  1101. *
  1102. * Input:
  1103. * ha = adapter state pointer.
  1104. *
  1105. * Returns:
  1106. * 0 = success
  1107. *
  1108. * Context:
  1109. * Kernel context.
  1110. */
  1111. static int
  1112. qla2x00_configure_hba(scsi_qla_host_t *ha)
  1113. {
  1114. int rval;
  1115. uint16_t loop_id;
  1116. uint16_t topo;
  1117. uint8_t al_pa;
  1118. uint8_t area;
  1119. uint8_t domain;
  1120. char connect_type[22];
  1121. /* Get host addresses. */
  1122. rval = qla2x00_get_adapter_id(ha,
  1123. &loop_id, &al_pa, &area, &domain, &topo);
  1124. if (rval != QLA_SUCCESS) {
  1125. if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
  1126. (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
  1127. DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
  1128. __func__, ha->host_no));
  1129. } else {
  1130. qla_printk(KERN_WARNING, ha,
  1131. "ERROR -- Unable to get host loop ID.\n");
  1132. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1133. }
  1134. return (rval);
  1135. }
  1136. if (topo == 4) {
  1137. qla_printk(KERN_INFO, ha,
  1138. "Cannot get topology - retrying.\n");
  1139. return (QLA_FUNCTION_FAILED);
  1140. }
  1141. ha->loop_id = loop_id;
  1142. /* initialize */
  1143. ha->min_external_loopid = SNS_FIRST_LOOP_ID;
  1144. ha->operating_mode = LOOP;
  1145. switch (topo) {
  1146. case 0:
  1147. DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
  1148. ha->host_no));
  1149. ha->current_topology = ISP_CFG_NL;
  1150. strcpy(connect_type, "(Loop)");
  1151. break;
  1152. case 1:
  1153. DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
  1154. ha->host_no));
  1155. ha->current_topology = ISP_CFG_FL;
  1156. strcpy(connect_type, "(FL_Port)");
  1157. break;
  1158. case 2:
  1159. DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
  1160. ha->host_no));
  1161. ha->operating_mode = P2P;
  1162. ha->current_topology = ISP_CFG_N;
  1163. strcpy(connect_type, "(N_Port-to-N_Port)");
  1164. break;
  1165. case 3:
  1166. DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
  1167. ha->host_no));
  1168. ha->operating_mode = P2P;
  1169. ha->current_topology = ISP_CFG_F;
  1170. strcpy(connect_type, "(F_Port)");
  1171. break;
  1172. default:
  1173. DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
  1174. "Using NL.\n",
  1175. ha->host_no, topo));
  1176. ha->current_topology = ISP_CFG_NL;
  1177. strcpy(connect_type, "(Loop)");
  1178. break;
  1179. }
  1180. /* Save Host port and loop ID. */
  1181. /* byte order - Big Endian */
  1182. ha->d_id.b.domain = domain;
  1183. ha->d_id.b.area = area;
  1184. ha->d_id.b.al_pa = al_pa;
  1185. if (!ha->flags.init_done)
  1186. qla_printk(KERN_INFO, ha,
  1187. "Topology - %s, Host Loop address 0x%x\n",
  1188. connect_type, ha->loop_id);
  1189. if (rval) {
  1190. DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
  1191. } else {
  1192. DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
  1193. }
  1194. return(rval);
  1195. }
  1196. /*
  1197. * NVRAM configuration for ISP 2xxx
  1198. *
  1199. * Input:
  1200. * ha = adapter block pointer.
  1201. *
  1202. * Output:
  1203. * initialization control block in response_ring
  1204. * host adapters parameters in host adapter block
  1205. *
  1206. * Returns:
  1207. * 0 = success.
  1208. */
  1209. int
  1210. qla2x00_nvram_config(scsi_qla_host_t *ha)
  1211. {
  1212. int rval;
  1213. uint8_t chksum = 0;
  1214. uint16_t cnt;
  1215. uint8_t *dptr1, *dptr2;
  1216. init_cb_t *icb = ha->init_cb;
  1217. nvram_t *nv = (nvram_t *)ha->request_ring;
  1218. uint8_t *ptr = (uint8_t *)ha->request_ring;
  1219. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1220. rval = QLA_SUCCESS;
  1221. /* Determine NVRAM starting address. */
  1222. ha->nvram_size = sizeof(nvram_t);
  1223. ha->nvram_base = 0;
  1224. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
  1225. if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
  1226. ha->nvram_base = 0x80;
  1227. /* Get NVRAM data and calculate checksum. */
  1228. ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
  1229. for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
  1230. chksum += *ptr++;
  1231. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  1232. DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
  1233. ha->nvram_size));
  1234. /* Bad NVRAM data, set defaults parameters. */
  1235. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
  1236. nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
  1237. /* Reset NVRAM data. */
  1238. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  1239. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  1240. nv->nvram_version);
  1241. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  1242. "invalid -- WWPN) defaults.\n");
  1243. /*
  1244. * Set default initialization control block.
  1245. */
  1246. memset(nv, 0, ha->nvram_size);
  1247. nv->parameter_block_version = ICB_VERSION;
  1248. if (IS_QLA23XX(ha)) {
  1249. nv->firmware_options[0] = BIT_2 | BIT_1;
  1250. nv->firmware_options[1] = BIT_7 | BIT_5;
  1251. nv->add_firmware_options[0] = BIT_5;
  1252. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1253. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1254. nv->special_options[1] = BIT_7;
  1255. } else if (IS_QLA2200(ha)) {
  1256. nv->firmware_options[0] = BIT_2 | BIT_1;
  1257. nv->firmware_options[1] = BIT_7 | BIT_5;
  1258. nv->add_firmware_options[0] = BIT_5;
  1259. nv->add_firmware_options[1] = BIT_5 | BIT_4;
  1260. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1261. } else if (IS_QLA2100(ha)) {
  1262. nv->firmware_options[0] = BIT_3 | BIT_1;
  1263. nv->firmware_options[1] = BIT_5;
  1264. nv->frame_payload_size = __constant_cpu_to_le16(1024);
  1265. }
  1266. nv->max_iocb_allocation = __constant_cpu_to_le16(256);
  1267. nv->execution_throttle = __constant_cpu_to_le16(16);
  1268. nv->retry_count = 8;
  1269. nv->retry_delay = 1;
  1270. nv->port_name[0] = 33;
  1271. nv->port_name[3] = 224;
  1272. nv->port_name[4] = 139;
  1273. nv->login_timeout = 4;
  1274. /*
  1275. * Set default host adapter parameters
  1276. */
  1277. nv->host_p[1] = BIT_2;
  1278. nv->reset_delay = 5;
  1279. nv->port_down_retry_count = 8;
  1280. nv->max_luns_per_target = __constant_cpu_to_le16(8);
  1281. nv->link_down_timeout = 60;
  1282. rval = 1;
  1283. }
  1284. #if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
  1285. /*
  1286. * The SN2 does not provide BIOS emulation which means you can't change
  1287. * potentially bogus BIOS settings. Force the use of default settings
  1288. * for link rate and frame size. Hope that the rest of the settings
  1289. * are valid.
  1290. */
  1291. if (ia64_platform_is("sn2")) {
  1292. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  1293. if (IS_QLA23XX(ha))
  1294. nv->special_options[1] = BIT_7;
  1295. }
  1296. #endif
  1297. /* Reset Initialization control block */
  1298. memset(icb, 0, ha->init_cb_size);
  1299. /*
  1300. * Setup driver NVRAM options.
  1301. */
  1302. nv->firmware_options[0] |= (BIT_6 | BIT_1);
  1303. nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
  1304. nv->firmware_options[1] |= (BIT_5 | BIT_0);
  1305. nv->firmware_options[1] &= ~BIT_4;
  1306. if (IS_QLA23XX(ha)) {
  1307. nv->firmware_options[0] |= BIT_2;
  1308. nv->firmware_options[0] &= ~BIT_3;
  1309. nv->add_firmware_options[1] |= BIT_5 | BIT_4;
  1310. if (IS_QLA2300(ha)) {
  1311. if (ha->fb_rev == FPM_2310) {
  1312. strcpy(ha->model_number, "QLA2310");
  1313. } else {
  1314. strcpy(ha->model_number, "QLA2300");
  1315. }
  1316. } else {
  1317. if (rval == 0 &&
  1318. memcmp(nv->model_number, BINZERO,
  1319. sizeof(nv->model_number)) != 0) {
  1320. char *st, *en;
  1321. strncpy(ha->model_number, nv->model_number,
  1322. sizeof(nv->model_number));
  1323. st = en = ha->model_number;
  1324. en += sizeof(nv->model_number) - 1;
  1325. while (en > st) {
  1326. if (*en != 0x20 && *en != 0x00)
  1327. break;
  1328. *en-- = '\0';
  1329. }
  1330. } else {
  1331. uint16_t index;
  1332. index = (ha->pdev->subsystem_device & 0xff);
  1333. if (index < QLA_MODEL_NAMES) {
  1334. strcpy(ha->model_number,
  1335. qla2x00_model_name[index * 2]);
  1336. ha->model_desc =
  1337. qla2x00_model_name[index * 2 + 1];
  1338. } else {
  1339. strcpy(ha->model_number, "QLA23xx");
  1340. }
  1341. }
  1342. }
  1343. } else if (IS_QLA2200(ha)) {
  1344. nv->firmware_options[0] |= BIT_2;
  1345. /*
  1346. * 'Point-to-point preferred, else loop' is not a safe
  1347. * connection mode setting.
  1348. */
  1349. if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
  1350. (BIT_5 | BIT_4)) {
  1351. /* Force 'loop preferred, else point-to-point'. */
  1352. nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
  1353. nv->add_firmware_options[0] |= BIT_5;
  1354. }
  1355. strcpy(ha->model_number, "QLA22xx");
  1356. } else /*if (IS_QLA2100(ha))*/ {
  1357. strcpy(ha->model_number, "QLA2100");
  1358. }
  1359. /*
  1360. * Copy over NVRAM RISC parameter block to initialization control block.
  1361. */
  1362. dptr1 = (uint8_t *)icb;
  1363. dptr2 = (uint8_t *)&nv->parameter_block_version;
  1364. cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
  1365. while (cnt--)
  1366. *dptr1++ = *dptr2++;
  1367. /* Copy 2nd half. */
  1368. dptr1 = (uint8_t *)icb->add_firmware_options;
  1369. cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
  1370. while (cnt--)
  1371. *dptr1++ = *dptr2++;
  1372. /* Use alternate WWN? */
  1373. if (nv->host_p[1] & BIT_7) {
  1374. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  1375. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  1376. }
  1377. /* Prepare nodename */
  1378. if ((icb->firmware_options[1] & BIT_6) == 0) {
  1379. /*
  1380. * Firmware will apply the following mask if the nodename was
  1381. * not provided.
  1382. */
  1383. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  1384. icb->node_name[0] &= 0xF0;
  1385. }
  1386. /*
  1387. * Set host adapter parameters.
  1388. */
  1389. if (nv->host_p[0] & BIT_7)
  1390. extended_error_logging = 1;
  1391. ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
  1392. /* Always load RISC code on non ISP2[12]00 chips. */
  1393. if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
  1394. ha->flags.disable_risc_code_load = 0;
  1395. ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
  1396. ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
  1397. ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
  1398. ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
  1399. ha->flags.disable_serdes = 0;
  1400. ha->operating_mode =
  1401. (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
  1402. memcpy(ha->fw_seriallink_options, nv->seriallink_options,
  1403. sizeof(ha->fw_seriallink_options));
  1404. /* save HBA serial number */
  1405. ha->serial0 = icb->port_name[5];
  1406. ha->serial1 = icb->port_name[6];
  1407. ha->serial2 = icb->port_name[7];
  1408. ha->node_name = icb->node_name;
  1409. ha->port_name = icb->port_name;
  1410. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  1411. ha->retry_count = nv->retry_count;
  1412. /* Set minimum login_timeout to 4 seconds. */
  1413. if (nv->login_timeout < ql2xlogintimeout)
  1414. nv->login_timeout = ql2xlogintimeout;
  1415. if (nv->login_timeout < 4)
  1416. nv->login_timeout = 4;
  1417. ha->login_timeout = nv->login_timeout;
  1418. icb->login_timeout = nv->login_timeout;
  1419. /* Set minimum RATOV to 200 tenths of a second. */
  1420. ha->r_a_tov = 200;
  1421. ha->loop_reset_delay = nv->reset_delay;
  1422. /* Link Down Timeout = 0:
  1423. *
  1424. * When Port Down timer expires we will start returning
  1425. * I/O's to OS with "DID_NO_CONNECT".
  1426. *
  1427. * Link Down Timeout != 0:
  1428. *
  1429. * The driver waits for the link to come up after link down
  1430. * before returning I/Os to OS with "DID_NO_CONNECT".
  1431. */
  1432. if (nv->link_down_timeout == 0) {
  1433. ha->loop_down_abort_time =
  1434. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  1435. } else {
  1436. ha->link_down_timeout = nv->link_down_timeout;
  1437. ha->loop_down_abort_time =
  1438. (LOOP_DOWN_TIME - ha->link_down_timeout);
  1439. }
  1440. /*
  1441. * Need enough time to try and get the port back.
  1442. */
  1443. ha->port_down_retry_count = nv->port_down_retry_count;
  1444. if (qlport_down_retry)
  1445. ha->port_down_retry_count = qlport_down_retry;
  1446. /* Set login_retry_count */
  1447. ha->login_retry_count = nv->retry_count;
  1448. if (ha->port_down_retry_count == nv->port_down_retry_count &&
  1449. ha->port_down_retry_count > 3)
  1450. ha->login_retry_count = ha->port_down_retry_count;
  1451. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  1452. ha->login_retry_count = ha->port_down_retry_count;
  1453. if (ql2xloginretrycount)
  1454. ha->login_retry_count = ql2xloginretrycount;
  1455. icb->lun_enables = __constant_cpu_to_le16(0);
  1456. icb->command_resource_count = 0;
  1457. icb->immediate_notify_resource_count = 0;
  1458. icb->timeout = __constant_cpu_to_le16(0);
  1459. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  1460. /* Enable RIO */
  1461. icb->firmware_options[0] &= ~BIT_3;
  1462. icb->add_firmware_options[0] &=
  1463. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1464. icb->add_firmware_options[0] |= BIT_2;
  1465. icb->response_accumulation_timer = 3;
  1466. icb->interrupt_delay_timer = 5;
  1467. ha->flags.process_response_queue = 1;
  1468. } else {
  1469. /* Enable ZIO. */
  1470. if (!ha->flags.init_done) {
  1471. ha->zio_mode = icb->add_firmware_options[0] &
  1472. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1473. ha->zio_timer = icb->interrupt_delay_timer ?
  1474. icb->interrupt_delay_timer: 2;
  1475. }
  1476. icb->add_firmware_options[0] &=
  1477. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
  1478. ha->flags.process_response_queue = 0;
  1479. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  1480. ha->zio_mode = QLA_ZIO_MODE_6;
  1481. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
  1482. "delay (%d us).\n", ha->host_no, ha->zio_mode,
  1483. ha->zio_timer * 100));
  1484. qla_printk(KERN_INFO, ha,
  1485. "ZIO mode %d enabled; timer delay (%d us).\n",
  1486. ha->zio_mode, ha->zio_timer * 100);
  1487. icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
  1488. icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
  1489. ha->flags.process_response_queue = 1;
  1490. }
  1491. }
  1492. if (rval) {
  1493. DEBUG2_3(printk(KERN_WARNING
  1494. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  1495. }
  1496. return (rval);
  1497. }
  1498. static void
  1499. qla2x00_rport_del(void *data)
  1500. {
  1501. fc_port_t *fcport = data;
  1502. struct fc_rport *rport;
  1503. unsigned long flags;
  1504. spin_lock_irqsave(&fcport->rport_lock, flags);
  1505. rport = fcport->drport;
  1506. fcport->drport = NULL;
  1507. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1508. if (rport)
  1509. fc_remote_port_delete(rport);
  1510. }
  1511. /**
  1512. * qla2x00_alloc_fcport() - Allocate a generic fcport.
  1513. * @ha: HA context
  1514. * @flags: allocation flags
  1515. *
  1516. * Returns a pointer to the allocated fcport, or NULL, if none available.
  1517. */
  1518. static fc_port_t *
  1519. qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
  1520. {
  1521. fc_port_t *fcport;
  1522. fcport = kmalloc(sizeof(fc_port_t), flags);
  1523. if (fcport == NULL)
  1524. return (fcport);
  1525. /* Setup fcport template structure. */
  1526. memset(fcport, 0, sizeof (fc_port_t));
  1527. fcport->ha = ha;
  1528. fcport->port_type = FCT_UNKNOWN;
  1529. fcport->loop_id = FC_NO_LOOP_ID;
  1530. atomic_set(&fcport->state, FCS_UNCONFIGURED);
  1531. fcport->flags = FCF_RLC_SUPPORT;
  1532. fcport->supported_classes = FC_COS_UNSPECIFIED;
  1533. spin_lock_init(&fcport->rport_lock);
  1534. return (fcport);
  1535. }
  1536. /*
  1537. * qla2x00_configure_loop
  1538. * Updates Fibre Channel Device Database with what is actually on loop.
  1539. *
  1540. * Input:
  1541. * ha = adapter block pointer.
  1542. *
  1543. * Returns:
  1544. * 0 = success.
  1545. * 1 = error.
  1546. * 2 = database was full and device was not configured.
  1547. */
  1548. static int
  1549. qla2x00_configure_loop(scsi_qla_host_t *ha)
  1550. {
  1551. int rval;
  1552. unsigned long flags, save_flags;
  1553. rval = QLA_SUCCESS;
  1554. /* Get Initiator ID */
  1555. if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
  1556. rval = qla2x00_configure_hba(ha);
  1557. if (rval != QLA_SUCCESS) {
  1558. DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
  1559. ha->host_no));
  1560. return (rval);
  1561. }
  1562. }
  1563. save_flags = flags = ha->dpc_flags;
  1564. DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
  1565. ha->host_no, flags));
  1566. /*
  1567. * If we have both an RSCN and PORT UPDATE pending then handle them
  1568. * both at the same time.
  1569. */
  1570. clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1571. clear_bit(RSCN_UPDATE, &ha->dpc_flags);
  1572. /* Determine what we need to do */
  1573. if (ha->current_topology == ISP_CFG_FL &&
  1574. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1575. ha->flags.rscn_queue_overflow = 1;
  1576. set_bit(RSCN_UPDATE, &flags);
  1577. } else if (ha->current_topology == ISP_CFG_F &&
  1578. (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
  1579. ha->flags.rscn_queue_overflow = 1;
  1580. set_bit(RSCN_UPDATE, &flags);
  1581. clear_bit(LOCAL_LOOP_UPDATE, &flags);
  1582. } else if (ha->current_topology == ISP_CFG_N) {
  1583. clear_bit(RSCN_UPDATE, &flags);
  1584. } else if (!ha->flags.online ||
  1585. (test_bit(ABORT_ISP_ACTIVE, &flags))) {
  1586. ha->flags.rscn_queue_overflow = 1;
  1587. set_bit(RSCN_UPDATE, &flags);
  1588. set_bit(LOCAL_LOOP_UPDATE, &flags);
  1589. }
  1590. if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
  1591. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1592. rval = QLA_FUNCTION_FAILED;
  1593. } else {
  1594. rval = qla2x00_configure_local_loop(ha);
  1595. }
  1596. }
  1597. if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
  1598. if (LOOP_TRANSITION(ha)) {
  1599. rval = QLA_FUNCTION_FAILED;
  1600. } else {
  1601. rval = qla2x00_configure_fabric(ha);
  1602. }
  1603. }
  1604. if (rval == QLA_SUCCESS) {
  1605. if (atomic_read(&ha->loop_down_timer) ||
  1606. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1607. rval = QLA_FUNCTION_FAILED;
  1608. } else {
  1609. atomic_set(&ha->loop_state, LOOP_READY);
  1610. DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
  1611. }
  1612. }
  1613. if (rval) {
  1614. DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
  1615. __func__, ha->host_no));
  1616. } else {
  1617. DEBUG3(printk("%s: exiting normally\n", __func__));
  1618. }
  1619. /* Restore state if a resync event occured during processing */
  1620. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
  1621. if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
  1622. set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
  1623. if (test_bit(RSCN_UPDATE, &save_flags))
  1624. set_bit(RSCN_UPDATE, &ha->dpc_flags);
  1625. }
  1626. return (rval);
  1627. }
  1628. /*
  1629. * qla2x00_configure_local_loop
  1630. * Updates Fibre Channel Device Database with local loop devices.
  1631. *
  1632. * Input:
  1633. * ha = adapter block pointer.
  1634. *
  1635. * Returns:
  1636. * 0 = success.
  1637. */
  1638. static int
  1639. qla2x00_configure_local_loop(scsi_qla_host_t *ha)
  1640. {
  1641. int rval, rval2;
  1642. int found_devs;
  1643. int found;
  1644. fc_port_t *fcport, *new_fcport;
  1645. uint16_t index;
  1646. uint16_t entries;
  1647. char *id_iter;
  1648. uint16_t loop_id;
  1649. uint8_t domain, area, al_pa;
  1650. found_devs = 0;
  1651. new_fcport = NULL;
  1652. entries = MAX_FIBRE_DEVICES;
  1653. DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
  1654. DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
  1655. /* Get list of logged in devices. */
  1656. memset(ha->gid_list, 0, GID_LIST_SIZE);
  1657. rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
  1658. &entries);
  1659. if (rval != QLA_SUCCESS)
  1660. goto cleanup_allocation;
  1661. DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
  1662. ha->host_no, entries));
  1663. DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
  1664. entries * sizeof(struct gid_list_info)));
  1665. /* Allocate temporary fcport for any new fcports discovered. */
  1666. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1667. if (new_fcport == NULL) {
  1668. rval = QLA_MEMORY_ALLOC_FAILED;
  1669. goto cleanup_allocation;
  1670. }
  1671. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1672. /*
  1673. * Mark local devices that were present with FCF_DEVICE_LOST for now.
  1674. */
  1675. list_for_each_entry(fcport, &ha->fcports, list) {
  1676. if (atomic_read(&fcport->state) == FCS_ONLINE &&
  1677. fcport->port_type != FCT_BROADCAST &&
  1678. (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  1679. DEBUG(printk("scsi(%ld): Marking port lost, "
  1680. "loop_id=0x%04x\n",
  1681. ha->host_no, fcport->loop_id));
  1682. atomic_set(&fcport->state, FCS_DEVICE_LOST);
  1683. fcport->flags &= ~FCF_FARP_DONE;
  1684. }
  1685. }
  1686. /* Add devices to port list. */
  1687. id_iter = (char *)ha->gid_list;
  1688. for (index = 0; index < entries; index++) {
  1689. domain = ((struct gid_list_info *)id_iter)->domain;
  1690. area = ((struct gid_list_info *)id_iter)->area;
  1691. al_pa = ((struct gid_list_info *)id_iter)->al_pa;
  1692. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  1693. loop_id = (uint16_t)
  1694. ((struct gid_list_info *)id_iter)->loop_id_2100;
  1695. else
  1696. loop_id = le16_to_cpu(
  1697. ((struct gid_list_info *)id_iter)->loop_id);
  1698. id_iter += ha->gid_list_info_size;
  1699. /* Bypass reserved domain fields. */
  1700. if ((domain & 0xf0) == 0xf0)
  1701. continue;
  1702. /* Bypass if not same domain and area of adapter. */
  1703. if (area && domain &&
  1704. (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
  1705. continue;
  1706. /* Bypass invalid local loop ID. */
  1707. if (loop_id > LAST_LOCAL_LOOP_ID)
  1708. continue;
  1709. /* Fill in member data. */
  1710. new_fcport->d_id.b.domain = domain;
  1711. new_fcport->d_id.b.area = area;
  1712. new_fcport->d_id.b.al_pa = al_pa;
  1713. new_fcport->loop_id = loop_id;
  1714. rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
  1715. if (rval2 != QLA_SUCCESS) {
  1716. DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
  1717. "information -- get_port_database=%x, "
  1718. "loop_id=0x%04x\n",
  1719. ha->host_no, rval2, new_fcport->loop_id));
  1720. DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
  1721. ha->host_no));
  1722. set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  1723. continue;
  1724. }
  1725. /* Check for matching device in port list. */
  1726. found = 0;
  1727. fcport = NULL;
  1728. list_for_each_entry(fcport, &ha->fcports, list) {
  1729. if (memcmp(new_fcport->port_name, fcport->port_name,
  1730. WWN_SIZE))
  1731. continue;
  1732. fcport->flags &= ~(FCF_FABRIC_DEVICE |
  1733. FCF_PERSISTENT_BOUND);
  1734. fcport->loop_id = new_fcport->loop_id;
  1735. fcport->port_type = new_fcport->port_type;
  1736. fcport->d_id.b24 = new_fcport->d_id.b24;
  1737. memcpy(fcport->node_name, new_fcport->node_name,
  1738. WWN_SIZE);
  1739. found++;
  1740. break;
  1741. }
  1742. if (!found) {
  1743. /* New device, add to fcports list. */
  1744. new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
  1745. list_add_tail(&new_fcport->list, &ha->fcports);
  1746. /* Allocate a new replacement fcport. */
  1747. fcport = new_fcport;
  1748. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  1749. if (new_fcport == NULL) {
  1750. rval = QLA_MEMORY_ALLOC_FAILED;
  1751. goto cleanup_allocation;
  1752. }
  1753. new_fcport->flags &= ~FCF_FABRIC_DEVICE;
  1754. }
  1755. qla2x00_update_fcport(ha, fcport);
  1756. found_devs++;
  1757. }
  1758. cleanup_allocation:
  1759. kfree(new_fcport);
  1760. if (rval != QLA_SUCCESS) {
  1761. DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
  1762. "rval=%x\n", ha->host_no, rval));
  1763. }
  1764. if (found_devs) {
  1765. ha->device_flags |= DFLG_LOCAL_DEVICES;
  1766. ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
  1767. }
  1768. return (rval);
  1769. }
  1770. static void
  1771. qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
  1772. {
  1773. fc_port_t *fcport;
  1774. qla2x00_mark_all_devices_lost(ha, 0);
  1775. list_for_each_entry(fcport, &ha->fcports, list) {
  1776. if (fcport->port_type != FCT_TARGET)
  1777. continue;
  1778. qla2x00_update_fcport(ha, fcport);
  1779. }
  1780. }
  1781. /*
  1782. * qla2x00_update_fcport
  1783. * Updates device on list.
  1784. *
  1785. * Input:
  1786. * ha = adapter block pointer.
  1787. * fcport = port structure pointer.
  1788. *
  1789. * Return:
  1790. * 0 - Success
  1791. * BIT_0 - error
  1792. *
  1793. * Context:
  1794. * Kernel context.
  1795. */
  1796. void
  1797. qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
  1798. {
  1799. fcport->ha = ha;
  1800. fcport->login_retry = 0;
  1801. fcport->port_login_retry_count = ha->port_down_retry_count *
  1802. PORT_RETRY_TIME;
  1803. atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
  1804. PORT_RETRY_TIME);
  1805. fcport->flags &= ~FCF_LOGIN_NEEDED;
  1806. atomic_set(&fcport->state, FCS_ONLINE);
  1807. if (ha->flags.init_done)
  1808. qla2x00_reg_remote_port(ha, fcport);
  1809. }
  1810. void
  1811. qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
  1812. {
  1813. struct fc_rport_identifiers rport_ids;
  1814. struct fc_rport *rport;
  1815. unsigned long flags;
  1816. if (fcport->drport)
  1817. qla2x00_rport_del(fcport);
  1818. if (fcport->rport)
  1819. return;
  1820. rport_ids.node_name = wwn_to_u64(fcport->node_name);
  1821. rport_ids.port_name = wwn_to_u64(fcport->port_name);
  1822. rport_ids.port_id = fcport->d_id.b.domain << 16 |
  1823. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  1824. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1825. rport = fc_remote_port_add(ha->host, 0, &rport_ids);
  1826. if (!rport) {
  1827. qla_printk(KERN_WARNING, ha,
  1828. "Unable to allocate fc remote port!\n");
  1829. return;
  1830. }
  1831. spin_lock_irqsave(&fcport->rport_lock, flags);
  1832. fcport->rport = rport;
  1833. *((fc_port_t **)rport->dd_data) = fcport;
  1834. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1835. rport->supported_classes = fcport->supported_classes;
  1836. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1837. if (fcport->port_type == FCT_INITIATOR)
  1838. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1839. if (fcport->port_type == FCT_TARGET)
  1840. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1841. fc_remote_port_rolechg(rport, rport_ids.roles);
  1842. if (rport->scsi_target_id != -1 &&
  1843. rport->scsi_target_id < ha->host->max_id)
  1844. fcport->os_target_id = rport->scsi_target_id;
  1845. }
  1846. /*
  1847. * qla2x00_configure_fabric
  1848. * Setup SNS devices with loop ID's.
  1849. *
  1850. * Input:
  1851. * ha = adapter block pointer.
  1852. *
  1853. * Returns:
  1854. * 0 = success.
  1855. * BIT_0 = error
  1856. */
  1857. static int
  1858. qla2x00_configure_fabric(scsi_qla_host_t *ha)
  1859. {
  1860. int rval, rval2;
  1861. fc_port_t *fcport, *fcptemp;
  1862. uint16_t next_loopid;
  1863. uint16_t mb[MAILBOX_REGISTER_COUNT];
  1864. uint16_t loop_id;
  1865. LIST_HEAD(new_fcports);
  1866. /* If FL port exists, then SNS is present */
  1867. if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
  1868. loop_id = NPH_F_PORT;
  1869. else
  1870. loop_id = SNS_FL_PORT;
  1871. rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
  1872. if (rval != QLA_SUCCESS) {
  1873. DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
  1874. "Port\n", ha->host_no));
  1875. ha->device_flags &= ~SWITCH_FOUND;
  1876. return (QLA_SUCCESS);
  1877. }
  1878. /* Mark devices that need re-synchronization. */
  1879. rval2 = qla2x00_device_resync(ha);
  1880. if (rval2 == QLA_RSCNS_HANDLED) {
  1881. /* No point doing the scan, just continue. */
  1882. return (QLA_SUCCESS);
  1883. }
  1884. do {
  1885. /* FDMI support. */
  1886. if (ql2xfdmienable &&
  1887. test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
  1888. qla2x00_fdmi_register(ha);
  1889. /* Ensure we are logged into the SNS. */
  1890. if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
  1891. loop_id = NPH_SNS;
  1892. else
  1893. loop_id = SIMPLE_NAME_SERVER;
  1894. ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
  1895. 0xfc, mb, BIT_1 | BIT_0);
  1896. if (mb[0] != MBS_COMMAND_COMPLETE) {
  1897. DEBUG2(qla_printk(KERN_INFO, ha,
  1898. "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
  1899. "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
  1900. mb[0], mb[1], mb[2], mb[6], mb[7]));
  1901. return (QLA_SUCCESS);
  1902. }
  1903. if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
  1904. if (qla2x00_rft_id(ha)) {
  1905. /* EMPTY */
  1906. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1907. "TYPE failed.\n", ha->host_no));
  1908. }
  1909. if (qla2x00_rff_id(ha)) {
  1910. /* EMPTY */
  1911. DEBUG2(printk("scsi(%ld): Register FC-4 "
  1912. "Features failed.\n", ha->host_no));
  1913. }
  1914. if (qla2x00_rnn_id(ha)) {
  1915. /* EMPTY */
  1916. DEBUG2(printk("scsi(%ld): Register Node Name "
  1917. "failed.\n", ha->host_no));
  1918. } else if (qla2x00_rsnn_nn(ha)) {
  1919. /* EMPTY */
  1920. DEBUG2(printk("scsi(%ld): Register Symbolic "
  1921. "Node Name failed.\n", ha->host_no));
  1922. }
  1923. }
  1924. rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
  1925. if (rval != QLA_SUCCESS)
  1926. break;
  1927. /*
  1928. * Logout all previous fabric devices marked lost, except
  1929. * tape devices.
  1930. */
  1931. list_for_each_entry(fcport, &ha->fcports, list) {
  1932. if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1933. break;
  1934. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
  1935. continue;
  1936. if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
  1937. qla2x00_mark_device_lost(ha, fcport,
  1938. ql2xplogiabsentdevice, 0);
  1939. if (fcport->loop_id != FC_NO_LOOP_ID &&
  1940. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  1941. fcport->port_type != FCT_INITIATOR &&
  1942. fcport->port_type != FCT_BROADCAST) {
  1943. ha->isp_ops.fabric_logout(ha,
  1944. fcport->loop_id,
  1945. fcport->d_id.b.domain,
  1946. fcport->d_id.b.area,
  1947. fcport->d_id.b.al_pa);
  1948. fcport->loop_id = FC_NO_LOOP_ID;
  1949. }
  1950. }
  1951. }
  1952. /* Starting free loop ID. */
  1953. next_loopid = ha->min_external_loopid;
  1954. /*
  1955. * Scan through our port list and login entries that need to be
  1956. * logged in.
  1957. */
  1958. list_for_each_entry(fcport, &ha->fcports, list) {
  1959. if (atomic_read(&ha->loop_down_timer) ||
  1960. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1961. break;
  1962. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  1963. (fcport->flags & FCF_LOGIN_NEEDED) == 0)
  1964. continue;
  1965. if (fcport->loop_id == FC_NO_LOOP_ID) {
  1966. fcport->loop_id = next_loopid;
  1967. rval = qla2x00_find_new_loop_id(ha, fcport);
  1968. if (rval != QLA_SUCCESS) {
  1969. /* Ran out of IDs to use */
  1970. break;
  1971. }
  1972. }
  1973. /* Login and update database */
  1974. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1975. }
  1976. /* Exit if out of loop IDs. */
  1977. if (rval != QLA_SUCCESS) {
  1978. break;
  1979. }
  1980. /*
  1981. * Login and add the new devices to our port list.
  1982. */
  1983. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  1984. if (atomic_read(&ha->loop_down_timer) ||
  1985. test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
  1986. break;
  1987. /* Find a new loop ID to use. */
  1988. fcport->loop_id = next_loopid;
  1989. rval = qla2x00_find_new_loop_id(ha, fcport);
  1990. if (rval != QLA_SUCCESS) {
  1991. /* Ran out of IDs to use */
  1992. break;
  1993. }
  1994. /* Remove device from the new list and add it to DB */
  1995. list_move_tail(&fcport->list, &ha->fcports);
  1996. /* Login and update database */
  1997. qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
  1998. }
  1999. } while (0);
  2000. /* Free all new device structures not processed. */
  2001. list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
  2002. list_del(&fcport->list);
  2003. kfree(fcport);
  2004. }
  2005. if (rval) {
  2006. DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
  2007. "rval=%d\n", ha->host_no, rval));
  2008. }
  2009. return (rval);
  2010. }
  2011. /*
  2012. * qla2x00_find_all_fabric_devs
  2013. *
  2014. * Input:
  2015. * ha = adapter block pointer.
  2016. * dev = database device entry pointer.
  2017. *
  2018. * Returns:
  2019. * 0 = success.
  2020. *
  2021. * Context:
  2022. * Kernel context.
  2023. */
  2024. static int
  2025. qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
  2026. {
  2027. int rval;
  2028. uint16_t loop_id;
  2029. fc_port_t *fcport, *new_fcport, *fcptemp;
  2030. int found;
  2031. sw_info_t *swl;
  2032. int swl_idx;
  2033. int first_dev, last_dev;
  2034. port_id_t wrap, nxt_d_id;
  2035. rval = QLA_SUCCESS;
  2036. /* Try GID_PT to get device list, else GAN. */
  2037. swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
  2038. if (swl == NULL) {
  2039. /*EMPTY*/
  2040. DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
  2041. "on GA_NXT\n", ha->host_no));
  2042. } else {
  2043. memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
  2044. if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
  2045. kfree(swl);
  2046. swl = NULL;
  2047. } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
  2048. kfree(swl);
  2049. swl = NULL;
  2050. } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
  2051. kfree(swl);
  2052. swl = NULL;
  2053. }
  2054. }
  2055. swl_idx = 0;
  2056. /* Allocate temporary fcport for any new fcports discovered. */
  2057. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2058. if (new_fcport == NULL) {
  2059. kfree(swl);
  2060. return (QLA_MEMORY_ALLOC_FAILED);
  2061. }
  2062. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2063. /* Set start port ID scan at adapter ID. */
  2064. first_dev = 1;
  2065. last_dev = 0;
  2066. /* Starting free loop ID. */
  2067. loop_id = ha->min_external_loopid;
  2068. for (; loop_id <= ha->last_loop_id; loop_id++) {
  2069. if (qla2x00_is_reserved_id(ha, loop_id))
  2070. continue;
  2071. if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
  2072. break;
  2073. if (swl != NULL) {
  2074. if (last_dev) {
  2075. wrap.b24 = new_fcport->d_id.b24;
  2076. } else {
  2077. new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
  2078. memcpy(new_fcport->node_name,
  2079. swl[swl_idx].node_name, WWN_SIZE);
  2080. memcpy(new_fcport->port_name,
  2081. swl[swl_idx].port_name, WWN_SIZE);
  2082. if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
  2083. last_dev = 1;
  2084. }
  2085. swl_idx++;
  2086. }
  2087. } else {
  2088. /* Send GA_NXT to the switch */
  2089. rval = qla2x00_ga_nxt(ha, new_fcport);
  2090. if (rval != QLA_SUCCESS) {
  2091. qla_printk(KERN_WARNING, ha,
  2092. "SNS scan failed -- assuming zero-entry "
  2093. "result...\n");
  2094. list_for_each_entry_safe(fcport, fcptemp,
  2095. new_fcports, list) {
  2096. list_del(&fcport->list);
  2097. kfree(fcport);
  2098. }
  2099. rval = QLA_SUCCESS;
  2100. break;
  2101. }
  2102. }
  2103. /* If wrap on switch device list, exit. */
  2104. if (first_dev) {
  2105. wrap.b24 = new_fcport->d_id.b24;
  2106. first_dev = 0;
  2107. } else if (new_fcport->d_id.b24 == wrap.b24) {
  2108. DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
  2109. ha->host_no, new_fcport->d_id.b.domain,
  2110. new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
  2111. break;
  2112. }
  2113. /* Bypass if host adapter. */
  2114. if (new_fcport->d_id.b24 == ha->d_id.b24)
  2115. continue;
  2116. /* Bypass if same domain and area of adapter. */
  2117. if (((new_fcport->d_id.b24 & 0xffff00) ==
  2118. (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
  2119. ISP_CFG_FL)
  2120. continue;
  2121. /* Bypass reserved domain fields. */
  2122. if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
  2123. continue;
  2124. /* Locate matching device in database. */
  2125. found = 0;
  2126. list_for_each_entry(fcport, &ha->fcports, list) {
  2127. if (memcmp(new_fcport->port_name, fcport->port_name,
  2128. WWN_SIZE))
  2129. continue;
  2130. found++;
  2131. /*
  2132. * If address the same and state FCS_ONLINE, nothing
  2133. * changed.
  2134. */
  2135. if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
  2136. atomic_read(&fcport->state) == FCS_ONLINE) {
  2137. break;
  2138. }
  2139. /*
  2140. * If device was not a fabric device before.
  2141. */
  2142. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
  2143. fcport->d_id.b24 = new_fcport->d_id.b24;
  2144. fcport->loop_id = FC_NO_LOOP_ID;
  2145. fcport->flags |= (FCF_FABRIC_DEVICE |
  2146. FCF_LOGIN_NEEDED);
  2147. fcport->flags &= ~FCF_PERSISTENT_BOUND;
  2148. break;
  2149. }
  2150. /*
  2151. * Port ID changed or device was marked to be updated;
  2152. * Log it out if still logged in and mark it for
  2153. * relogin later.
  2154. */
  2155. fcport->d_id.b24 = new_fcport->d_id.b24;
  2156. fcport->flags |= FCF_LOGIN_NEEDED;
  2157. if (fcport->loop_id != FC_NO_LOOP_ID &&
  2158. (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
  2159. fcport->port_type != FCT_INITIATOR &&
  2160. fcport->port_type != FCT_BROADCAST) {
  2161. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2162. fcport->d_id.b.domain, fcport->d_id.b.area,
  2163. fcport->d_id.b.al_pa);
  2164. fcport->loop_id = FC_NO_LOOP_ID;
  2165. }
  2166. break;
  2167. }
  2168. if (found)
  2169. continue;
  2170. /* If device was not in our fcports list, then add it. */
  2171. list_add_tail(&new_fcport->list, new_fcports);
  2172. /* Allocate a new replacement fcport. */
  2173. nxt_d_id.b24 = new_fcport->d_id.b24;
  2174. new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
  2175. if (new_fcport == NULL) {
  2176. kfree(swl);
  2177. return (QLA_MEMORY_ALLOC_FAILED);
  2178. }
  2179. new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
  2180. new_fcport->d_id.b24 = nxt_d_id.b24;
  2181. }
  2182. kfree(swl);
  2183. kfree(new_fcport);
  2184. if (!list_empty(new_fcports))
  2185. ha->device_flags |= DFLG_FABRIC_DEVICES;
  2186. return (rval);
  2187. }
  2188. /*
  2189. * qla2x00_find_new_loop_id
  2190. * Scan through our port list and find a new usable loop ID.
  2191. *
  2192. * Input:
  2193. * ha: adapter state pointer.
  2194. * dev: port structure pointer.
  2195. *
  2196. * Returns:
  2197. * qla2x00 local function return status code.
  2198. *
  2199. * Context:
  2200. * Kernel context.
  2201. */
  2202. static int
  2203. qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
  2204. {
  2205. int rval;
  2206. int found;
  2207. fc_port_t *fcport;
  2208. uint16_t first_loop_id;
  2209. rval = QLA_SUCCESS;
  2210. /* Save starting loop ID. */
  2211. first_loop_id = dev->loop_id;
  2212. for (;;) {
  2213. /* Skip loop ID if already used by adapter. */
  2214. if (dev->loop_id == ha->loop_id) {
  2215. dev->loop_id++;
  2216. }
  2217. /* Skip reserved loop IDs. */
  2218. while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
  2219. dev->loop_id++;
  2220. }
  2221. /* Reset loop ID if passed the end. */
  2222. if (dev->loop_id > ha->last_loop_id) {
  2223. /* first loop ID. */
  2224. dev->loop_id = ha->min_external_loopid;
  2225. }
  2226. /* Check for loop ID being already in use. */
  2227. found = 0;
  2228. fcport = NULL;
  2229. list_for_each_entry(fcport, &ha->fcports, list) {
  2230. if (fcport->loop_id == dev->loop_id && fcport != dev) {
  2231. /* ID possibly in use */
  2232. found++;
  2233. break;
  2234. }
  2235. }
  2236. /* If not in use then it is free to use. */
  2237. if (!found) {
  2238. break;
  2239. }
  2240. /* ID in use. Try next value. */
  2241. dev->loop_id++;
  2242. /* If wrap around. No free ID to use. */
  2243. if (dev->loop_id == first_loop_id) {
  2244. dev->loop_id = FC_NO_LOOP_ID;
  2245. rval = QLA_FUNCTION_FAILED;
  2246. break;
  2247. }
  2248. }
  2249. return (rval);
  2250. }
  2251. /*
  2252. * qla2x00_device_resync
  2253. * Marks devices in the database that needs resynchronization.
  2254. *
  2255. * Input:
  2256. * ha = adapter block pointer.
  2257. *
  2258. * Context:
  2259. * Kernel context.
  2260. */
  2261. static int
  2262. qla2x00_device_resync(scsi_qla_host_t *ha)
  2263. {
  2264. int rval;
  2265. uint32_t mask;
  2266. fc_port_t *fcport;
  2267. uint32_t rscn_entry;
  2268. uint8_t rscn_out_iter;
  2269. uint8_t format;
  2270. port_id_t d_id;
  2271. rval = QLA_RSCNS_HANDLED;
  2272. while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
  2273. ha->flags.rscn_queue_overflow) {
  2274. rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
  2275. format = MSB(MSW(rscn_entry));
  2276. d_id.b.domain = LSB(MSW(rscn_entry));
  2277. d_id.b.area = MSB(LSW(rscn_entry));
  2278. d_id.b.al_pa = LSB(LSW(rscn_entry));
  2279. DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
  2280. "[%02x/%02x%02x%02x].\n",
  2281. ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
  2282. d_id.b.area, d_id.b.al_pa));
  2283. ha->rscn_out_ptr++;
  2284. if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
  2285. ha->rscn_out_ptr = 0;
  2286. /* Skip duplicate entries. */
  2287. for (rscn_out_iter = ha->rscn_out_ptr;
  2288. !ha->flags.rscn_queue_overflow &&
  2289. rscn_out_iter != ha->rscn_in_ptr;
  2290. rscn_out_iter = (rscn_out_iter ==
  2291. (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
  2292. if (rscn_entry != ha->rscn_queue[rscn_out_iter])
  2293. break;
  2294. DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
  2295. "entry found at [%d].\n", ha->host_no,
  2296. rscn_out_iter));
  2297. ha->rscn_out_ptr = rscn_out_iter;
  2298. }
  2299. /* Queue overflow, set switch default case. */
  2300. if (ha->flags.rscn_queue_overflow) {
  2301. DEBUG(printk("scsi(%ld): device_resync: rscn "
  2302. "overflow.\n", ha->host_no));
  2303. format = 3;
  2304. ha->flags.rscn_queue_overflow = 0;
  2305. }
  2306. switch (format) {
  2307. case 0:
  2308. mask = 0xffffff;
  2309. break;
  2310. case 1:
  2311. mask = 0xffff00;
  2312. break;
  2313. case 2:
  2314. mask = 0xff0000;
  2315. break;
  2316. default:
  2317. mask = 0x0;
  2318. d_id.b24 = 0;
  2319. ha->rscn_out_ptr = ha->rscn_in_ptr;
  2320. break;
  2321. }
  2322. rval = QLA_SUCCESS;
  2323. list_for_each_entry(fcport, &ha->fcports, list) {
  2324. if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
  2325. (fcport->d_id.b24 & mask) != d_id.b24 ||
  2326. fcport->port_type == FCT_BROADCAST)
  2327. continue;
  2328. if (atomic_read(&fcport->state) == FCS_ONLINE) {
  2329. if (format != 3 ||
  2330. fcport->port_type != FCT_INITIATOR) {
  2331. qla2x00_mark_device_lost(ha, fcport,
  2332. 0, 0);
  2333. }
  2334. }
  2335. fcport->flags &= ~FCF_FARP_DONE;
  2336. }
  2337. }
  2338. return (rval);
  2339. }
  2340. /*
  2341. * qla2x00_fabric_dev_login
  2342. * Login fabric target device and update FC port database.
  2343. *
  2344. * Input:
  2345. * ha: adapter state pointer.
  2346. * fcport: port structure list pointer.
  2347. * next_loopid: contains value of a new loop ID that can be used
  2348. * by the next login attempt.
  2349. *
  2350. * Returns:
  2351. * qla2x00 local function return status code.
  2352. *
  2353. * Context:
  2354. * Kernel context.
  2355. */
  2356. static int
  2357. qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2358. uint16_t *next_loopid)
  2359. {
  2360. int rval;
  2361. int retry;
  2362. uint8_t opts;
  2363. rval = QLA_SUCCESS;
  2364. retry = 0;
  2365. rval = qla2x00_fabric_login(ha, fcport, next_loopid);
  2366. if (rval == QLA_SUCCESS) {
  2367. /* Send an ADISC to tape devices.*/
  2368. opts = 0;
  2369. if (fcport->flags & FCF_TAPE_PRESENT)
  2370. opts |= BIT_1;
  2371. rval = qla2x00_get_port_database(ha, fcport, opts);
  2372. if (rval != QLA_SUCCESS) {
  2373. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2374. fcport->d_id.b.domain, fcport->d_id.b.area,
  2375. fcport->d_id.b.al_pa);
  2376. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2377. } else {
  2378. qla2x00_update_fcport(ha, fcport);
  2379. }
  2380. }
  2381. return (rval);
  2382. }
  2383. /*
  2384. * qla2x00_fabric_login
  2385. * Issue fabric login command.
  2386. *
  2387. * Input:
  2388. * ha = adapter block pointer.
  2389. * device = pointer to FC device type structure.
  2390. *
  2391. * Returns:
  2392. * 0 - Login successfully
  2393. * 1 - Login failed
  2394. * 2 - Initiator device
  2395. * 3 - Fatal error
  2396. */
  2397. int
  2398. qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
  2399. uint16_t *next_loopid)
  2400. {
  2401. int rval;
  2402. int retry;
  2403. uint16_t tmp_loopid;
  2404. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2405. retry = 0;
  2406. tmp_loopid = 0;
  2407. for (;;) {
  2408. DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
  2409. "for port %02x%02x%02x.\n",
  2410. ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
  2411. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2412. /* Login fcport on switch. */
  2413. ha->isp_ops.fabric_login(ha, fcport->loop_id,
  2414. fcport->d_id.b.domain, fcport->d_id.b.area,
  2415. fcport->d_id.b.al_pa, mb, BIT_0);
  2416. if (mb[0] == MBS_PORT_ID_USED) {
  2417. /*
  2418. * Device has another loop ID. The firmware team
  2419. * recommends the driver perform an implicit login with
  2420. * the specified ID again. The ID we just used is save
  2421. * here so we return with an ID that can be tried by
  2422. * the next login.
  2423. */
  2424. retry++;
  2425. tmp_loopid = fcport->loop_id;
  2426. fcport->loop_id = mb[1];
  2427. DEBUG(printk("Fabric Login: port in use - next "
  2428. "loop id=0x%04x, port Id=%02x%02x%02x.\n",
  2429. fcport->loop_id, fcport->d_id.b.domain,
  2430. fcport->d_id.b.area, fcport->d_id.b.al_pa));
  2431. } else if (mb[0] == MBS_COMMAND_COMPLETE) {
  2432. /*
  2433. * Login succeeded.
  2434. */
  2435. if (retry) {
  2436. /* A retry occurred before. */
  2437. *next_loopid = tmp_loopid;
  2438. } else {
  2439. /*
  2440. * No retry occurred before. Just increment the
  2441. * ID value for next login.
  2442. */
  2443. *next_loopid = (fcport->loop_id + 1);
  2444. }
  2445. if (mb[1] & BIT_0) {
  2446. fcport->port_type = FCT_INITIATOR;
  2447. } else {
  2448. fcport->port_type = FCT_TARGET;
  2449. if (mb[1] & BIT_1) {
  2450. fcport->flags |= FCF_TAPE_PRESENT;
  2451. }
  2452. }
  2453. if (mb[10] & BIT_0)
  2454. fcport->supported_classes |= FC_COS_CLASS2;
  2455. if (mb[10] & BIT_1)
  2456. fcport->supported_classes |= FC_COS_CLASS3;
  2457. rval = QLA_SUCCESS;
  2458. break;
  2459. } else if (mb[0] == MBS_LOOP_ID_USED) {
  2460. /*
  2461. * Loop ID already used, try next loop ID.
  2462. */
  2463. fcport->loop_id++;
  2464. rval = qla2x00_find_new_loop_id(ha, fcport);
  2465. if (rval != QLA_SUCCESS) {
  2466. /* Ran out of loop IDs to use */
  2467. break;
  2468. }
  2469. } else if (mb[0] == MBS_COMMAND_ERROR) {
  2470. /*
  2471. * Firmware possibly timed out during login. If NO
  2472. * retries are left to do then the device is declared
  2473. * dead.
  2474. */
  2475. *next_loopid = fcport->loop_id;
  2476. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2477. fcport->d_id.b.domain, fcport->d_id.b.area,
  2478. fcport->d_id.b.al_pa);
  2479. qla2x00_mark_device_lost(ha, fcport, 1, 0);
  2480. rval = 1;
  2481. break;
  2482. } else {
  2483. /*
  2484. * unrecoverable / not handled error
  2485. */
  2486. DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
  2487. "loop_id=%x jiffies=%lx.\n",
  2488. __func__, ha->host_no, mb[0],
  2489. fcport->d_id.b.domain, fcport->d_id.b.area,
  2490. fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
  2491. *next_loopid = fcport->loop_id;
  2492. ha->isp_ops.fabric_logout(ha, fcport->loop_id,
  2493. fcport->d_id.b.domain, fcport->d_id.b.area,
  2494. fcport->d_id.b.al_pa);
  2495. fcport->loop_id = FC_NO_LOOP_ID;
  2496. fcport->login_retry = 0;
  2497. rval = 3;
  2498. break;
  2499. }
  2500. }
  2501. return (rval);
  2502. }
  2503. /*
  2504. * qla2x00_local_device_login
  2505. * Issue local device login command.
  2506. *
  2507. * Input:
  2508. * ha = adapter block pointer.
  2509. * loop_id = loop id of device to login to.
  2510. *
  2511. * Returns (Where's the #define!!!!):
  2512. * 0 - Login successfully
  2513. * 1 - Login failed
  2514. * 3 - Fatal error
  2515. */
  2516. int
  2517. qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
  2518. {
  2519. int rval;
  2520. uint16_t mb[MAILBOX_REGISTER_COUNT];
  2521. memset(mb, 0, sizeof(mb));
  2522. rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
  2523. if (rval == QLA_SUCCESS) {
  2524. /* Interrogate mailbox registers for any errors */
  2525. if (mb[0] == MBS_COMMAND_ERROR)
  2526. rval = 1;
  2527. else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
  2528. /* device not in PCB table */
  2529. rval = 3;
  2530. }
  2531. return (rval);
  2532. }
  2533. /*
  2534. * qla2x00_loop_resync
  2535. * Resync with fibre channel devices.
  2536. *
  2537. * Input:
  2538. * ha = adapter block pointer.
  2539. *
  2540. * Returns:
  2541. * 0 = success
  2542. */
  2543. int
  2544. qla2x00_loop_resync(scsi_qla_host_t *ha)
  2545. {
  2546. int rval;
  2547. uint32_t wait_time;
  2548. rval = QLA_SUCCESS;
  2549. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2550. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2551. if (ha->flags.online) {
  2552. if (!(rval = qla2x00_fw_ready(ha))) {
  2553. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2554. wait_time = 256;
  2555. do {
  2556. atomic_set(&ha->loop_state, LOOP_UPDATE);
  2557. /* Issue a marker after FW becomes ready. */
  2558. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2559. ha->marker_needed = 0;
  2560. /* Remap devices on Loop. */
  2561. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2562. qla2x00_configure_loop(ha);
  2563. wait_time--;
  2564. } while (!atomic_read(&ha->loop_down_timer) &&
  2565. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2566. wait_time &&
  2567. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2568. }
  2569. }
  2570. if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
  2571. return (QLA_FUNCTION_FAILED);
  2572. }
  2573. if (rval) {
  2574. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  2575. }
  2576. return (rval);
  2577. }
  2578. void
  2579. qla2x00_rescan_fcports(scsi_qla_host_t *ha)
  2580. {
  2581. int rescan_done;
  2582. fc_port_t *fcport;
  2583. rescan_done = 0;
  2584. list_for_each_entry(fcport, &ha->fcports, list) {
  2585. if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
  2586. continue;
  2587. qla2x00_update_fcport(ha, fcport);
  2588. fcport->flags &= ~FCF_RESCAN_NEEDED;
  2589. rescan_done = 1;
  2590. }
  2591. qla2x00_probe_for_all_luns(ha);
  2592. }
  2593. void
  2594. qla2x00_update_fcports(scsi_qla_host_t *ha)
  2595. {
  2596. fc_port_t *fcport;
  2597. /* Go with deferred removal of rport references. */
  2598. list_for_each_entry(fcport, &ha->fcports, list)
  2599. if (fcport->drport)
  2600. qla2x00_rport_del(fcport);
  2601. }
  2602. /*
  2603. * qla2x00_abort_isp
  2604. * Resets ISP and aborts all outstanding commands.
  2605. *
  2606. * Input:
  2607. * ha = adapter block pointer.
  2608. *
  2609. * Returns:
  2610. * 0 = success
  2611. */
  2612. int
  2613. qla2x00_abort_isp(scsi_qla_host_t *ha)
  2614. {
  2615. int rval;
  2616. unsigned long flags = 0;
  2617. uint16_t cnt;
  2618. srb_t *sp;
  2619. uint8_t status = 0;
  2620. if (ha->flags.online) {
  2621. ha->flags.online = 0;
  2622. clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  2623. qla_printk(KERN_INFO, ha,
  2624. "Performing ISP error recovery - ha= %p.\n", ha);
  2625. ha->isp_ops.reset_chip(ha);
  2626. atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
  2627. if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
  2628. atomic_set(&ha->loop_state, LOOP_DOWN);
  2629. qla2x00_mark_all_devices_lost(ha, 0);
  2630. } else {
  2631. if (!atomic_read(&ha->loop_down_timer))
  2632. atomic_set(&ha->loop_down_timer,
  2633. LOOP_DOWN_TIME);
  2634. }
  2635. spin_lock_irqsave(&ha->hardware_lock, flags);
  2636. /* Requeue all commands in outstanding command list. */
  2637. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  2638. sp = ha->outstanding_cmds[cnt];
  2639. if (sp) {
  2640. ha->outstanding_cmds[cnt] = NULL;
  2641. sp->flags = 0;
  2642. sp->cmd->result = DID_RESET << 16;
  2643. sp->cmd->host_scribble = (unsigned char *)NULL;
  2644. qla2x00_sp_compl(ha, sp);
  2645. }
  2646. }
  2647. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2648. ha->isp_ops.nvram_config(ha);
  2649. if (!qla2x00_restart_isp(ha)) {
  2650. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2651. if (!atomic_read(&ha->loop_down_timer)) {
  2652. /*
  2653. * Issue marker command only when we are going
  2654. * to start the I/O .
  2655. */
  2656. ha->marker_needed = 1;
  2657. }
  2658. ha->flags.online = 1;
  2659. ha->isp_ops.enable_intrs(ha);
  2660. ha->isp_abort_cnt = 0;
  2661. clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2662. if (ha->eft) {
  2663. rval = qla2x00_trace_control(ha, TC_ENABLE,
  2664. ha->eft_dma, EFT_NUM_BUFFERS);
  2665. if (rval) {
  2666. qla_printk(KERN_WARNING, ha,
  2667. "Unable to reinitialize EFT "
  2668. "(%d).\n", rval);
  2669. }
  2670. }
  2671. } else { /* failed the ISP abort */
  2672. ha->flags.online = 1;
  2673. if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
  2674. if (ha->isp_abort_cnt == 0) {
  2675. qla_printk(KERN_WARNING, ha,
  2676. "ISP error recovery failed - "
  2677. "board disabled\n");
  2678. /*
  2679. * The next call disables the board
  2680. * completely.
  2681. */
  2682. ha->isp_ops.reset_adapter(ha);
  2683. ha->flags.online = 0;
  2684. clear_bit(ISP_ABORT_RETRY,
  2685. &ha->dpc_flags);
  2686. status = 0;
  2687. } else { /* schedule another ISP abort */
  2688. ha->isp_abort_cnt--;
  2689. DEBUG(printk("qla%ld: ISP abort - "
  2690. "retry remaining %d\n",
  2691. ha->host_no, ha->isp_abort_cnt));
  2692. status = 1;
  2693. }
  2694. } else {
  2695. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  2696. DEBUG(printk("qla2x00(%ld): ISP error recovery "
  2697. "- retrying (%d) more times\n",
  2698. ha->host_no, ha->isp_abort_cnt));
  2699. set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
  2700. status = 1;
  2701. }
  2702. }
  2703. }
  2704. if (status) {
  2705. qla_printk(KERN_INFO, ha,
  2706. "qla2x00_abort_isp: **** FAILED ****\n");
  2707. } else {
  2708. DEBUG(printk(KERN_INFO
  2709. "qla2x00_abort_isp(%ld): exiting.\n",
  2710. ha->host_no));
  2711. }
  2712. return(status);
  2713. }
  2714. /*
  2715. * qla2x00_restart_isp
  2716. * restarts the ISP after a reset
  2717. *
  2718. * Input:
  2719. * ha = adapter block pointer.
  2720. *
  2721. * Returns:
  2722. * 0 = success
  2723. */
  2724. static int
  2725. qla2x00_restart_isp(scsi_qla_host_t *ha)
  2726. {
  2727. uint8_t status = 0;
  2728. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2729. unsigned long flags = 0;
  2730. uint32_t wait_time;
  2731. /* If firmware needs to be loaded */
  2732. if (qla2x00_isp_firmware(ha)) {
  2733. ha->flags.online = 0;
  2734. if (!(status = ha->isp_ops.chip_diag(ha))) {
  2735. if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
  2736. status = qla2x00_setup_chip(ha);
  2737. goto done;
  2738. }
  2739. spin_lock_irqsave(&ha->hardware_lock, flags);
  2740. if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
  2741. /*
  2742. * Disable SRAM, Instruction RAM and GP RAM
  2743. * parity.
  2744. */
  2745. WRT_REG_WORD(&reg->hccr,
  2746. (HCCR_ENABLE_PARITY + 0x0));
  2747. RD_REG_WORD(&reg->hccr);
  2748. }
  2749. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2750. status = qla2x00_setup_chip(ha);
  2751. spin_lock_irqsave(&ha->hardware_lock, flags);
  2752. if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
  2753. /* Enable proper parity */
  2754. if (IS_QLA2300(ha))
  2755. /* SRAM parity */
  2756. WRT_REG_WORD(&reg->hccr,
  2757. (HCCR_ENABLE_PARITY + 0x1));
  2758. else
  2759. /*
  2760. * SRAM, Instruction RAM and GP RAM
  2761. * parity.
  2762. */
  2763. WRT_REG_WORD(&reg->hccr,
  2764. (HCCR_ENABLE_PARITY + 0x7));
  2765. RD_REG_WORD(&reg->hccr);
  2766. }
  2767. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2768. }
  2769. }
  2770. done:
  2771. if (!status && !(status = qla2x00_init_rings(ha))) {
  2772. clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
  2773. if (!(status = qla2x00_fw_ready(ha))) {
  2774. DEBUG(printk("%s(): Start configure loop, "
  2775. "status = %d\n", __func__, status));
  2776. /* Issue a marker after FW becomes ready. */
  2777. qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
  2778. ha->flags.online = 1;
  2779. /* Wait at most MAX_TARGET RSCNs for a stable link. */
  2780. wait_time = 256;
  2781. do {
  2782. clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
  2783. qla2x00_configure_loop(ha);
  2784. wait_time--;
  2785. } while (!atomic_read(&ha->loop_down_timer) &&
  2786. !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
  2787. wait_time &&
  2788. (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
  2789. }
  2790. /* if no cable then assume it's good */
  2791. if ((ha->device_flags & DFLG_NO_CABLE))
  2792. status = 0;
  2793. DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
  2794. __func__,
  2795. status));
  2796. }
  2797. return (status);
  2798. }
  2799. /*
  2800. * qla2x00_reset_adapter
  2801. * Reset adapter.
  2802. *
  2803. * Input:
  2804. * ha = adapter block pointer.
  2805. */
  2806. void
  2807. qla2x00_reset_adapter(scsi_qla_host_t *ha)
  2808. {
  2809. unsigned long flags = 0;
  2810. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2811. ha->flags.online = 0;
  2812. ha->isp_ops.disable_intrs(ha);
  2813. spin_lock_irqsave(&ha->hardware_lock, flags);
  2814. WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
  2815. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2816. WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
  2817. RD_REG_WORD(&reg->hccr); /* PCI Posting. */
  2818. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2819. }
  2820. void
  2821. qla24xx_reset_adapter(scsi_qla_host_t *ha)
  2822. {
  2823. unsigned long flags = 0;
  2824. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  2825. ha->flags.online = 0;
  2826. ha->isp_ops.disable_intrs(ha);
  2827. spin_lock_irqsave(&ha->hardware_lock, flags);
  2828. WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
  2829. RD_REG_DWORD(&reg->hccr);
  2830. WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
  2831. RD_REG_DWORD(&reg->hccr);
  2832. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2833. }
  2834. int
  2835. qla24xx_nvram_config(scsi_qla_host_t *ha)
  2836. {
  2837. int rval;
  2838. struct init_cb_24xx *icb;
  2839. struct nvram_24xx *nv;
  2840. uint32_t *dptr;
  2841. uint8_t *dptr1, *dptr2;
  2842. uint32_t chksum;
  2843. uint16_t cnt;
  2844. rval = QLA_SUCCESS;
  2845. icb = (struct init_cb_24xx *)ha->init_cb;
  2846. nv = (struct nvram_24xx *)ha->request_ring;
  2847. /* Determine NVRAM starting address. */
  2848. ha->nvram_size = sizeof(struct nvram_24xx);
  2849. ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
  2850. ha->vpd_size = FA_NVRAM_VPD_SIZE;
  2851. ha->vpd_base = FA_NVRAM_VPD0_ADDR;
  2852. if (PCI_FUNC(ha->pdev->devfn)) {
  2853. ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
  2854. ha->vpd_base = FA_NVRAM_VPD1_ADDR;
  2855. }
  2856. /* Get NVRAM data and calculate checksum. */
  2857. dptr = (uint32_t *)nv;
  2858. ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
  2859. ha->nvram_size);
  2860. for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
  2861. chksum += le32_to_cpu(*dptr++);
  2862. DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
  2863. DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
  2864. ha->nvram_size));
  2865. /* Bad NVRAM data, set defaults parameters. */
  2866. if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
  2867. || nv->id[3] != ' ' ||
  2868. nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
  2869. /* Reset NVRAM data. */
  2870. qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
  2871. "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
  2872. le16_to_cpu(nv->nvram_version));
  2873. qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
  2874. "invalid -- WWPN) defaults.\n");
  2875. /*
  2876. * Set default initialization control block.
  2877. */
  2878. memset(nv, 0, ha->nvram_size);
  2879. nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
  2880. nv->version = __constant_cpu_to_le16(ICB_VERSION);
  2881. nv->frame_payload_size = __constant_cpu_to_le16(2048);
  2882. nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2883. nv->exchange_count = __constant_cpu_to_le16(0);
  2884. nv->hard_address = __constant_cpu_to_le16(124);
  2885. nv->port_name[0] = 0x21;
  2886. nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
  2887. nv->port_name[2] = 0x00;
  2888. nv->port_name[3] = 0xe0;
  2889. nv->port_name[4] = 0x8b;
  2890. nv->port_name[5] = 0x1c;
  2891. nv->port_name[6] = 0x55;
  2892. nv->port_name[7] = 0x86;
  2893. nv->node_name[0] = 0x20;
  2894. nv->node_name[1] = 0x00;
  2895. nv->node_name[2] = 0x00;
  2896. nv->node_name[3] = 0xe0;
  2897. nv->node_name[4] = 0x8b;
  2898. nv->node_name[5] = 0x1c;
  2899. nv->node_name[6] = 0x55;
  2900. nv->node_name[7] = 0x86;
  2901. nv->login_retry_count = __constant_cpu_to_le16(8);
  2902. nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
  2903. nv->login_timeout = __constant_cpu_to_le16(0);
  2904. nv->firmware_options_1 =
  2905. __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
  2906. nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
  2907. nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
  2908. nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
  2909. nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
  2910. nv->efi_parameters = __constant_cpu_to_le32(0);
  2911. nv->reset_delay = 5;
  2912. nv->max_luns_per_target = __constant_cpu_to_le16(128);
  2913. nv->port_down_retry_count = __constant_cpu_to_le16(30);
  2914. nv->link_down_timeout = __constant_cpu_to_le16(30);
  2915. rval = 1;
  2916. }
  2917. /* Reset Initialization control block */
  2918. memset(icb, 0, sizeof(struct init_cb_24xx));
  2919. /* Copy 1st segment. */
  2920. dptr1 = (uint8_t *)icb;
  2921. dptr2 = (uint8_t *)&nv->version;
  2922. cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
  2923. while (cnt--)
  2924. *dptr1++ = *dptr2++;
  2925. icb->login_retry_count = nv->login_retry_count;
  2926. icb->link_down_on_nos = nv->link_down_on_nos;
  2927. /* Copy 2nd segment. */
  2928. dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
  2929. dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
  2930. cnt = (uint8_t *)&icb->reserved_3 -
  2931. (uint8_t *)&icb->interrupt_delay_timer;
  2932. while (cnt--)
  2933. *dptr1++ = *dptr2++;
  2934. /*
  2935. * Setup driver NVRAM options.
  2936. */
  2937. if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
  2938. char *st, *en;
  2939. uint16_t index;
  2940. strncpy(ha->model_number, nv->model_name,
  2941. sizeof(nv->model_name));
  2942. st = en = ha->model_number;
  2943. en += sizeof(nv->model_name) - 1;
  2944. while (en > st) {
  2945. if (*en != 0x20 && *en != 0x00)
  2946. break;
  2947. *en-- = '\0';
  2948. }
  2949. index = (ha->pdev->subsystem_device & 0xff);
  2950. if (index < QLA_MODEL_NAMES)
  2951. ha->model_desc = qla2x00_model_name[index * 2 + 1];
  2952. } else
  2953. strcpy(ha->model_number, "QLA2462");
  2954. /* Use alternate WWN? */
  2955. if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
  2956. memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
  2957. memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
  2958. }
  2959. /* Prepare nodename */
  2960. if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
  2961. /*
  2962. * Firmware will apply the following mask if the nodename was
  2963. * not provided.
  2964. */
  2965. memcpy(icb->node_name, icb->port_name, WWN_SIZE);
  2966. icb->node_name[0] &= 0xF0;
  2967. }
  2968. /* Set host adapter parameters. */
  2969. ha->flags.disable_risc_code_load = 0;
  2970. ha->flags.enable_lip_reset = 1;
  2971. ha->flags.enable_lip_full_login = 1;
  2972. ha->flags.enable_target_reset = 1;
  2973. ha->flags.enable_led_scheme = 0;
  2974. ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
  2975. ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
  2976. (BIT_6 | BIT_5 | BIT_4)) >> 4;
  2977. memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
  2978. sizeof(ha->fw_seriallink_options24));
  2979. /* save HBA serial number */
  2980. ha->serial0 = icb->port_name[5];
  2981. ha->serial1 = icb->port_name[6];
  2982. ha->serial2 = icb->port_name[7];
  2983. ha->node_name = icb->node_name;
  2984. ha->port_name = icb->port_name;
  2985. icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
  2986. ha->retry_count = le16_to_cpu(nv->login_retry_count);
  2987. /* Set minimum login_timeout to 4 seconds. */
  2988. if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
  2989. nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
  2990. if (le16_to_cpu(nv->login_timeout) < 4)
  2991. nv->login_timeout = __constant_cpu_to_le16(4);
  2992. ha->login_timeout = le16_to_cpu(nv->login_timeout);
  2993. icb->login_timeout = cpu_to_le16(nv->login_timeout);
  2994. /* Set minimum RATOV to 200 tenths of a second. */
  2995. ha->r_a_tov = 200;
  2996. ha->loop_reset_delay = nv->reset_delay;
  2997. /* Link Down Timeout = 0:
  2998. *
  2999. * When Port Down timer expires we will start returning
  3000. * I/O's to OS with "DID_NO_CONNECT".
  3001. *
  3002. * Link Down Timeout != 0:
  3003. *
  3004. * The driver waits for the link to come up after link down
  3005. * before returning I/Os to OS with "DID_NO_CONNECT".
  3006. */
  3007. if (le16_to_cpu(nv->link_down_timeout) == 0) {
  3008. ha->loop_down_abort_time =
  3009. (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
  3010. } else {
  3011. ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
  3012. ha->loop_down_abort_time =
  3013. (LOOP_DOWN_TIME - ha->link_down_timeout);
  3014. }
  3015. /* Need enough time to try and get the port back. */
  3016. ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
  3017. if (qlport_down_retry)
  3018. ha->port_down_retry_count = qlport_down_retry;
  3019. /* Set login_retry_count */
  3020. ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
  3021. if (ha->port_down_retry_count ==
  3022. le16_to_cpu(nv->port_down_retry_count) &&
  3023. ha->port_down_retry_count > 3)
  3024. ha->login_retry_count = ha->port_down_retry_count;
  3025. else if (ha->port_down_retry_count > (int)ha->login_retry_count)
  3026. ha->login_retry_count = ha->port_down_retry_count;
  3027. if (ql2xloginretrycount)
  3028. ha->login_retry_count = ql2xloginretrycount;
  3029. /* Enable ZIO. */
  3030. if (!ha->flags.init_done) {
  3031. ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
  3032. (BIT_3 | BIT_2 | BIT_1 | BIT_0);
  3033. ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
  3034. le16_to_cpu(icb->interrupt_delay_timer): 2;
  3035. }
  3036. icb->firmware_options_2 &= __constant_cpu_to_le32(
  3037. ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
  3038. ha->flags.process_response_queue = 0;
  3039. if (ha->zio_mode != QLA_ZIO_DISABLED) {
  3040. ha->zio_mode = QLA_ZIO_MODE_6;
  3041. DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
  3042. "(%d us).\n", ha->host_no, ha->zio_mode,
  3043. ha->zio_timer * 100));
  3044. qla_printk(KERN_INFO, ha,
  3045. "ZIO mode %d enabled; timer delay (%d us).\n",
  3046. ha->zio_mode, ha->zio_timer * 100);
  3047. icb->firmware_options_2 |= cpu_to_le32(
  3048. (uint32_t)ha->zio_mode);
  3049. icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
  3050. ha->flags.process_response_queue = 1;
  3051. }
  3052. if (rval) {
  3053. DEBUG2_3(printk(KERN_WARNING
  3054. "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
  3055. }
  3056. return (rval);
  3057. }
  3058. static int
  3059. qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3060. {
  3061. int rval;
  3062. int segments, fragment;
  3063. uint32_t faddr;
  3064. uint32_t *dcode, dlen;
  3065. uint32_t risc_addr;
  3066. uint32_t risc_size;
  3067. uint32_t i;
  3068. rval = QLA_SUCCESS;
  3069. segments = FA_RISC_CODE_SEGMENTS;
  3070. faddr = FA_RISC_CODE_ADDR;
  3071. dcode = (uint32_t *)ha->request_ring;
  3072. *srisc_addr = 0;
  3073. /* Validate firmware image by checking version. */
  3074. qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
  3075. for (i = 0; i < 4; i++)
  3076. dcode[i] = be32_to_cpu(dcode[i]);
  3077. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3078. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3079. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3080. dcode[3] == 0)) {
  3081. qla_printk(KERN_WARNING, ha,
  3082. "Unable to verify integrity of flash firmware image!\n");
  3083. qla_printk(KERN_WARNING, ha,
  3084. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3085. dcode[1], dcode[2], dcode[3]);
  3086. return QLA_FUNCTION_FAILED;
  3087. }
  3088. while (segments && rval == QLA_SUCCESS) {
  3089. /* Read segment's load information. */
  3090. qla24xx_read_flash_data(ha, dcode, faddr, 4);
  3091. risc_addr = be32_to_cpu(dcode[2]);
  3092. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3093. risc_size = be32_to_cpu(dcode[3]);
  3094. fragment = 0;
  3095. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3096. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3097. if (dlen > risc_size)
  3098. dlen = risc_size;
  3099. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3100. "addr %x, number of dwords 0x%x, offset 0x%x.\n",
  3101. ha->host_no, risc_addr, dlen, faddr));
  3102. qla24xx_read_flash_data(ha, dcode, faddr, dlen);
  3103. for (i = 0; i < dlen; i++)
  3104. dcode[i] = swab32(dcode[i]);
  3105. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3106. dlen);
  3107. if (rval) {
  3108. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3109. "segment %d of firmware\n", ha->host_no,
  3110. fragment));
  3111. qla_printk(KERN_WARNING, ha,
  3112. "[ERROR] Failed to load segment %d of "
  3113. "firmware\n", fragment);
  3114. break;
  3115. }
  3116. faddr += dlen;
  3117. risc_addr += dlen;
  3118. risc_size -= dlen;
  3119. fragment++;
  3120. }
  3121. /* Next segment. */
  3122. segments--;
  3123. }
  3124. return rval;
  3125. }
  3126. #define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
  3127. int
  3128. qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3129. {
  3130. int rval;
  3131. int i, fragment;
  3132. uint16_t *wcode, *fwcode;
  3133. uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
  3134. struct fw_blob *blob;
  3135. /* Load firmware blob. */
  3136. blob = qla2x00_request_firmware(ha);
  3137. if (!blob) {
  3138. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3139. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3140. "from: " QLA_FW_URL ".\n");
  3141. return QLA_FUNCTION_FAILED;
  3142. }
  3143. rval = QLA_SUCCESS;
  3144. wcode = (uint16_t *)ha->request_ring;
  3145. *srisc_addr = 0;
  3146. fwcode = (uint16_t *)blob->fw->data;
  3147. fwclen = 0;
  3148. /* Validate firmware image by checking version. */
  3149. if (blob->fw->size < 8 * sizeof(uint16_t)) {
  3150. qla_printk(KERN_WARNING, ha,
  3151. "Unable to verify integrity of firmware image (%Zd)!\n",
  3152. blob->fw->size);
  3153. goto fail_fw_integrity;
  3154. }
  3155. for (i = 0; i < 4; i++)
  3156. wcode[i] = be16_to_cpu(fwcode[i + 4]);
  3157. if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
  3158. wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
  3159. wcode[2] == 0 && wcode[3] == 0)) {
  3160. qla_printk(KERN_WARNING, ha,
  3161. "Unable to verify integrity of firmware image!\n");
  3162. qla_printk(KERN_WARNING, ha,
  3163. "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
  3164. wcode[1], wcode[2], wcode[3]);
  3165. goto fail_fw_integrity;
  3166. }
  3167. seg = blob->segs;
  3168. while (*seg && rval == QLA_SUCCESS) {
  3169. risc_addr = *seg;
  3170. *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
  3171. risc_size = be16_to_cpu(fwcode[3]);
  3172. /* Validate firmware image size. */
  3173. fwclen += risc_size * sizeof(uint16_t);
  3174. if (blob->fw->size < fwclen) {
  3175. qla_printk(KERN_WARNING, ha,
  3176. "Unable to verify integrity of firmware image "
  3177. "(%Zd)!\n", blob->fw->size);
  3178. goto fail_fw_integrity;
  3179. }
  3180. fragment = 0;
  3181. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3182. wlen = (uint16_t)(ha->fw_transfer_size >> 1);
  3183. if (wlen > risc_size)
  3184. wlen = risc_size;
  3185. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3186. "addr %x, number of words 0x%x.\n", ha->host_no,
  3187. risc_addr, wlen));
  3188. for (i = 0; i < wlen; i++)
  3189. wcode[i] = swab16(fwcode[i]);
  3190. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3191. wlen);
  3192. if (rval) {
  3193. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3194. "segment %d of firmware\n", ha->host_no,
  3195. fragment));
  3196. qla_printk(KERN_WARNING, ha,
  3197. "[ERROR] Failed to load segment %d of "
  3198. "firmware\n", fragment);
  3199. break;
  3200. }
  3201. fwcode += wlen;
  3202. risc_addr += wlen;
  3203. risc_size -= wlen;
  3204. fragment++;
  3205. }
  3206. /* Next segment. */
  3207. seg++;
  3208. }
  3209. return rval;
  3210. fail_fw_integrity:
  3211. return QLA_FUNCTION_FAILED;
  3212. }
  3213. int
  3214. qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
  3215. {
  3216. int rval;
  3217. int segments, fragment;
  3218. uint32_t *dcode, dlen;
  3219. uint32_t risc_addr;
  3220. uint32_t risc_size;
  3221. uint32_t i;
  3222. struct fw_blob *blob;
  3223. uint32_t *fwcode, fwclen;
  3224. /* Load firmware blob. */
  3225. blob = qla2x00_request_firmware(ha);
  3226. if (!blob) {
  3227. qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
  3228. qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
  3229. "from: " QLA_FW_URL ".\n");
  3230. /* Try to load RISC code from flash. */
  3231. qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
  3232. "outdated) firmware from flash.\n");
  3233. return qla24xx_load_risc_flash(ha, srisc_addr);
  3234. }
  3235. rval = QLA_SUCCESS;
  3236. segments = FA_RISC_CODE_SEGMENTS;
  3237. dcode = (uint32_t *)ha->request_ring;
  3238. *srisc_addr = 0;
  3239. fwcode = (uint32_t *)blob->fw->data;
  3240. fwclen = 0;
  3241. /* Validate firmware image by checking version. */
  3242. if (blob->fw->size < 8 * sizeof(uint32_t)) {
  3243. qla_printk(KERN_WARNING, ha,
  3244. "Unable to verify integrity of firmware image (%Zd)!\n",
  3245. blob->fw->size);
  3246. goto fail_fw_integrity;
  3247. }
  3248. for (i = 0; i < 4; i++)
  3249. dcode[i] = be32_to_cpu(fwcode[i + 4]);
  3250. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  3251. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  3252. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  3253. dcode[3] == 0)) {
  3254. qla_printk(KERN_WARNING, ha,
  3255. "Unable to verify integrity of firmware image!\n");
  3256. qla_printk(KERN_WARNING, ha,
  3257. "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
  3258. dcode[1], dcode[2], dcode[3]);
  3259. goto fail_fw_integrity;
  3260. }
  3261. while (segments && rval == QLA_SUCCESS) {
  3262. risc_addr = be32_to_cpu(fwcode[2]);
  3263. *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
  3264. risc_size = be32_to_cpu(fwcode[3]);
  3265. /* Validate firmware image size. */
  3266. fwclen += risc_size * sizeof(uint32_t);
  3267. if (blob->fw->size < fwclen) {
  3268. qla_printk(KERN_WARNING, ha,
  3269. "Unable to verify integrity of firmware image "
  3270. "(%Zd)!\n", blob->fw->size);
  3271. goto fail_fw_integrity;
  3272. }
  3273. fragment = 0;
  3274. while (risc_size > 0 && rval == QLA_SUCCESS) {
  3275. dlen = (uint32_t)(ha->fw_transfer_size >> 2);
  3276. if (dlen > risc_size)
  3277. dlen = risc_size;
  3278. DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
  3279. "addr %x, number of dwords 0x%x.\n", ha->host_no,
  3280. risc_addr, dlen));
  3281. for (i = 0; i < dlen; i++)
  3282. dcode[i] = swab32(fwcode[i]);
  3283. rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
  3284. dlen);
  3285. if (rval) {
  3286. DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
  3287. "segment %d of firmware\n", ha->host_no,
  3288. fragment));
  3289. qla_printk(KERN_WARNING, ha,
  3290. "[ERROR] Failed to load segment %d of "
  3291. "firmware\n", fragment);
  3292. break;
  3293. }
  3294. fwcode += dlen;
  3295. risc_addr += dlen;
  3296. risc_size -= dlen;
  3297. fragment++;
  3298. }
  3299. /* Next segment. */
  3300. segments--;
  3301. }
  3302. return rval;
  3303. fail_fw_integrity:
  3304. return QLA_FUNCTION_FAILED;
  3305. }