lpfc_scsi.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/pci.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/export.h>
  25. #include <linux/delay.h>
  26. #include <asm/unaligned.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_tcq.h>
  32. #include <scsi/scsi_transport_fc.h>
  33. #include "lpfc_version.h"
  34. #include "lpfc_hw4.h"
  35. #include "lpfc_hw.h"
  36. #include "lpfc_sli.h"
  37. #include "lpfc_sli4.h"
  38. #include "lpfc_nl.h"
  39. #include "lpfc_disc.h"
  40. #include "lpfc_scsi.h"
  41. #include "lpfc.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_crtn.h"
  44. #include "lpfc_vport.h"
  45. #define LPFC_RESET_WAIT 2
  46. #define LPFC_ABORT_WAIT 2
  47. int _dump_buf_done;
  48. static char *dif_op_str[] = {
  49. "SCSI_PROT_NORMAL",
  50. "SCSI_PROT_READ_INSERT",
  51. "SCSI_PROT_WRITE_STRIP",
  52. "SCSI_PROT_READ_STRIP",
  53. "SCSI_PROT_WRITE_INSERT",
  54. "SCSI_PROT_READ_PASS",
  55. "SCSI_PROT_WRITE_PASS",
  56. };
  57. struct scsi_dif_tuple {
  58. __be16 guard_tag; /* Checksum */
  59. __be16 app_tag; /* Opaque storage */
  60. __be32 ref_tag; /* Target LBA or indirect LBA */
  61. };
  62. static void
  63. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  64. static void
  65. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  66. static void
  67. lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  68. {
  69. void *src, *dst;
  70. struct scatterlist *sgde = scsi_sglist(cmnd);
  71. if (!_dump_buf_data) {
  72. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  73. "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  74. __func__);
  75. return;
  76. }
  77. if (!sgde) {
  78. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  79. "9051 BLKGRD: ERROR: data scatterlist is null\n");
  80. return;
  81. }
  82. dst = (void *) _dump_buf_data;
  83. while (sgde) {
  84. src = sg_virt(sgde);
  85. memcpy(dst, src, sgde->length);
  86. dst += sgde->length;
  87. sgde = sg_next(sgde);
  88. }
  89. }
  90. static void
  91. lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  92. {
  93. void *src, *dst;
  94. struct scatterlist *sgde = scsi_prot_sglist(cmnd);
  95. if (!_dump_buf_dif) {
  96. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  97. "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  98. __func__);
  99. return;
  100. }
  101. if (!sgde) {
  102. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  103. "9053 BLKGRD: ERROR: prot scatterlist is null\n");
  104. return;
  105. }
  106. dst = _dump_buf_dif;
  107. while (sgde) {
  108. src = sg_virt(sgde);
  109. memcpy(dst, src, sgde->length);
  110. dst += sgde->length;
  111. sgde = sg_next(sgde);
  112. }
  113. }
  114. /**
  115. * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
  116. * @phba: Pointer to HBA object.
  117. * @lpfc_cmd: lpfc scsi command object pointer.
  118. *
  119. * This function is called from the lpfc_prep_task_mgmt_cmd function to
  120. * set the last bit in the response sge entry.
  121. **/
  122. static void
  123. lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
  124. struct lpfc_scsi_buf *lpfc_cmd)
  125. {
  126. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  127. if (sgl) {
  128. sgl += 1;
  129. sgl->word2 = le32_to_cpu(sgl->word2);
  130. bf_set(lpfc_sli4_sge_last, sgl, 1);
  131. sgl->word2 = cpu_to_le32(sgl->word2);
  132. }
  133. }
  134. /**
  135. * lpfc_update_stats - Update statistical data for the command completion
  136. * @phba: Pointer to HBA object.
  137. * @lpfc_cmd: lpfc scsi command object pointer.
  138. *
  139. * This function is called when there is a command completion and this
  140. * function updates the statistical data for the command completion.
  141. **/
  142. static void
  143. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  144. {
  145. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  146. struct lpfc_nodelist *pnode = rdata->pnode;
  147. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  148. unsigned long flags;
  149. struct Scsi_Host *shost = cmd->device->host;
  150. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  151. unsigned long latency;
  152. int i;
  153. if (cmd->result)
  154. return;
  155. latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
  156. spin_lock_irqsave(shost->host_lock, flags);
  157. if (!vport->stat_data_enabled ||
  158. vport->stat_data_blocked ||
  159. !pnode ||
  160. !pnode->lat_data ||
  161. (phba->bucket_type == LPFC_NO_BUCKET)) {
  162. spin_unlock_irqrestore(shost->host_lock, flags);
  163. return;
  164. }
  165. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  166. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  167. phba->bucket_step;
  168. /* check array subscript bounds */
  169. if (i < 0)
  170. i = 0;
  171. else if (i >= LPFC_MAX_BUCKET_COUNT)
  172. i = LPFC_MAX_BUCKET_COUNT - 1;
  173. } else {
  174. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  175. if (latency <= (phba->bucket_base +
  176. ((1<<i)*phba->bucket_step)))
  177. break;
  178. }
  179. pnode->lat_data[i].cmd_count++;
  180. spin_unlock_irqrestore(shost->host_lock, flags);
  181. }
  182. /**
  183. * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
  184. * @phba: Pointer to HBA context object.
  185. * @vport: Pointer to vport object.
  186. * @ndlp: Pointer to FC node associated with the target.
  187. * @lun: Lun number of the scsi device.
  188. * @old_val: Old value of the queue depth.
  189. * @new_val: New value of the queue depth.
  190. *
  191. * This function sends an event to the mgmt application indicating
  192. * there is a change in the scsi device queue depth.
  193. **/
  194. static void
  195. lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
  196. struct lpfc_vport *vport,
  197. struct lpfc_nodelist *ndlp,
  198. uint32_t lun,
  199. uint32_t old_val,
  200. uint32_t new_val)
  201. {
  202. struct lpfc_fast_path_event *fast_path_evt;
  203. unsigned long flags;
  204. fast_path_evt = lpfc_alloc_fast_evt(phba);
  205. if (!fast_path_evt)
  206. return;
  207. fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
  208. FC_REG_SCSI_EVENT;
  209. fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
  210. LPFC_EVENT_VARQUEDEPTH;
  211. /* Report all luns with change in queue depth */
  212. fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
  213. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  214. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
  215. &ndlp->nlp_portname, sizeof(struct lpfc_name));
  216. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
  217. &ndlp->nlp_nodename, sizeof(struct lpfc_name));
  218. }
  219. fast_path_evt->un.queue_depth_evt.oldval = old_val;
  220. fast_path_evt->un.queue_depth_evt.newval = new_val;
  221. fast_path_evt->vport = vport;
  222. fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  223. spin_lock_irqsave(&phba->hbalock, flags);
  224. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  225. spin_unlock_irqrestore(&phba->hbalock, flags);
  226. lpfc_worker_wake_up(phba);
  227. return;
  228. }
  229. /**
  230. * lpfc_change_queue_depth - Alter scsi device queue depth
  231. * @sdev: Pointer the scsi device on which to change the queue depth.
  232. * @qdepth: New queue depth to set the sdev to.
  233. * @reason: The reason for the queue depth change.
  234. *
  235. * This function is called by the midlayer and the LLD to alter the queue
  236. * depth for a scsi device. This function sets the queue depth to the new
  237. * value and sends an event out to log the queue depth change.
  238. **/
  239. int
  240. lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  241. {
  242. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  243. struct lpfc_hba *phba = vport->phba;
  244. struct lpfc_rport_data *rdata;
  245. unsigned long new_queue_depth, old_queue_depth;
  246. old_queue_depth = sdev->queue_depth;
  247. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  248. new_queue_depth = sdev->queue_depth;
  249. rdata = sdev->hostdata;
  250. if (rdata)
  251. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  252. rdata->pnode, sdev->lun,
  253. old_queue_depth,
  254. new_queue_depth);
  255. return sdev->queue_depth;
  256. }
  257. /**
  258. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  259. * @phba: The Hba for which this call is being executed.
  260. *
  261. * This routine is called when there is resource error in driver or firmware.
  262. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  263. * posts at most 1 event each second. This routine wakes up worker thread of
  264. * @phba to process WORKER_RAM_DOWN_EVENT event.
  265. *
  266. * This routine should be called with no lock held.
  267. **/
  268. void
  269. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  270. {
  271. unsigned long flags;
  272. uint32_t evt_posted;
  273. spin_lock_irqsave(&phba->hbalock, flags);
  274. atomic_inc(&phba->num_rsrc_err);
  275. phba->last_rsrc_error_time = jiffies;
  276. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  277. spin_unlock_irqrestore(&phba->hbalock, flags);
  278. return;
  279. }
  280. phba->last_ramp_down_time = jiffies;
  281. spin_unlock_irqrestore(&phba->hbalock, flags);
  282. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  283. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  284. if (!evt_posted)
  285. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  286. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  287. if (!evt_posted)
  288. lpfc_worker_wake_up(phba);
  289. return;
  290. }
  291. /**
  292. * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
  293. * @phba: The Hba for which this call is being executed.
  294. *
  295. * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
  296. * post at most 1 event every 5 minute after last_ramp_up_time or
  297. * last_rsrc_error_time. This routine wakes up worker thread of @phba
  298. * to process WORKER_RAM_DOWN_EVENT event.
  299. *
  300. * This routine should be called with no lock held.
  301. **/
  302. static inline void
  303. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  304. uint32_t queue_depth)
  305. {
  306. unsigned long flags;
  307. struct lpfc_hba *phba = vport->phba;
  308. uint32_t evt_posted;
  309. atomic_inc(&phba->num_cmd_success);
  310. if (vport->cfg_lun_queue_depth <= queue_depth)
  311. return;
  312. spin_lock_irqsave(&phba->hbalock, flags);
  313. if (time_before(jiffies,
  314. phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
  315. time_before(jiffies,
  316. phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
  317. spin_unlock_irqrestore(&phba->hbalock, flags);
  318. return;
  319. }
  320. phba->last_ramp_up_time = jiffies;
  321. spin_unlock_irqrestore(&phba->hbalock, flags);
  322. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  323. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  324. if (!evt_posted)
  325. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  326. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  327. if (!evt_posted)
  328. lpfc_worker_wake_up(phba);
  329. return;
  330. }
  331. /**
  332. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  333. * @phba: The Hba for which this call is being executed.
  334. *
  335. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  336. * thread.This routine reduces queue depth for all scsi device on each vport
  337. * associated with @phba.
  338. **/
  339. void
  340. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  341. {
  342. struct lpfc_vport **vports;
  343. struct Scsi_Host *shost;
  344. struct scsi_device *sdev;
  345. unsigned long new_queue_depth;
  346. unsigned long num_rsrc_err, num_cmd_success;
  347. int i;
  348. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  349. num_cmd_success = atomic_read(&phba->num_cmd_success);
  350. vports = lpfc_create_vport_work_array(phba);
  351. if (vports != NULL)
  352. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  353. shost = lpfc_shost_from_vport(vports[i]);
  354. shost_for_each_device(sdev, shost) {
  355. new_queue_depth =
  356. sdev->queue_depth * num_rsrc_err /
  357. (num_rsrc_err + num_cmd_success);
  358. if (!new_queue_depth)
  359. new_queue_depth = sdev->queue_depth - 1;
  360. else
  361. new_queue_depth = sdev->queue_depth -
  362. new_queue_depth;
  363. lpfc_change_queue_depth(sdev, new_queue_depth,
  364. SCSI_QDEPTH_DEFAULT);
  365. }
  366. }
  367. lpfc_destroy_vport_work_array(phba, vports);
  368. atomic_set(&phba->num_rsrc_err, 0);
  369. atomic_set(&phba->num_cmd_success, 0);
  370. }
  371. /**
  372. * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
  373. * @phba: The Hba for which this call is being executed.
  374. *
  375. * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
  376. * thread.This routine increases queue depth for all scsi device on each vport
  377. * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
  378. * num_cmd_success to zero.
  379. **/
  380. void
  381. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  382. {
  383. struct lpfc_vport **vports;
  384. struct Scsi_Host *shost;
  385. struct scsi_device *sdev;
  386. int i;
  387. vports = lpfc_create_vport_work_array(phba);
  388. if (vports != NULL)
  389. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  390. shost = lpfc_shost_from_vport(vports[i]);
  391. shost_for_each_device(sdev, shost) {
  392. if (vports[i]->cfg_lun_queue_depth <=
  393. sdev->queue_depth)
  394. continue;
  395. lpfc_change_queue_depth(sdev,
  396. sdev->queue_depth+1,
  397. SCSI_QDEPTH_RAMP_UP);
  398. }
  399. }
  400. lpfc_destroy_vport_work_array(phba, vports);
  401. atomic_set(&phba->num_rsrc_err, 0);
  402. atomic_set(&phba->num_cmd_success, 0);
  403. }
  404. /**
  405. * lpfc_scsi_dev_block - set all scsi hosts to block state
  406. * @phba: Pointer to HBA context object.
  407. *
  408. * This function walks vport list and set each SCSI host to block state
  409. * by invoking fc_remote_port_delete() routine. This function is invoked
  410. * with EEH when device's PCI slot has been permanently disabled.
  411. **/
  412. void
  413. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  414. {
  415. struct lpfc_vport **vports;
  416. struct Scsi_Host *shost;
  417. struct scsi_device *sdev;
  418. struct fc_rport *rport;
  419. int i;
  420. vports = lpfc_create_vport_work_array(phba);
  421. if (vports != NULL)
  422. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  423. shost = lpfc_shost_from_vport(vports[i]);
  424. shost_for_each_device(sdev, shost) {
  425. rport = starget_to_rport(scsi_target(sdev));
  426. fc_remote_port_delete(rport);
  427. }
  428. }
  429. lpfc_destroy_vport_work_array(phba, vports);
  430. }
  431. /**
  432. * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
  433. * @vport: The virtual port for which this call being executed.
  434. * @num_to_allocate: The requested number of buffers to allocate.
  435. *
  436. * This routine allocates a scsi buffer for device with SLI-3 interface spec,
  437. * the scsi buffer contains all the necessary information needed to initiate
  438. * a SCSI I/O. The non-DMAable buffer region contains information to build
  439. * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
  440. * and the initial BPL. In addition to allocating memory, the FCP CMND and
  441. * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
  442. *
  443. * Return codes:
  444. * int - number of scsi buffers that were allocated.
  445. * 0 = failure, less than num_to_alloc is a partial failure.
  446. **/
  447. static int
  448. lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
  449. {
  450. struct lpfc_hba *phba = vport->phba;
  451. struct lpfc_scsi_buf *psb;
  452. struct ulp_bde64 *bpl;
  453. IOCB_t *iocb;
  454. dma_addr_t pdma_phys_fcp_cmd;
  455. dma_addr_t pdma_phys_fcp_rsp;
  456. dma_addr_t pdma_phys_bpl;
  457. uint16_t iotag;
  458. int bcnt;
  459. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  460. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  461. if (!psb)
  462. break;
  463. /*
  464. * Get memory from the pci pool to map the virt space to pci
  465. * bus space for an I/O. The DMA buffer includes space for the
  466. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  467. * necessary to support the sg_tablesize.
  468. */
  469. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  470. GFP_KERNEL, &psb->dma_handle);
  471. if (!psb->data) {
  472. kfree(psb);
  473. break;
  474. }
  475. /* Initialize virtual ptrs to dma_buf region. */
  476. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  477. /* Allocate iotag for psb->cur_iocbq. */
  478. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  479. if (iotag == 0) {
  480. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  481. psb->data, psb->dma_handle);
  482. kfree(psb);
  483. break;
  484. }
  485. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  486. psb->fcp_cmnd = psb->data;
  487. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  488. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  489. sizeof(struct fcp_rsp);
  490. /* Initialize local short-hand pointers. */
  491. bpl = psb->fcp_bpl;
  492. pdma_phys_fcp_cmd = psb->dma_handle;
  493. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  494. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  495. sizeof(struct fcp_rsp);
  496. /*
  497. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  498. * are sg list bdes. Initialize the first two and leave the
  499. * rest for queuecommand.
  500. */
  501. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  502. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  503. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  504. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  505. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  506. /* Setup the physical region for the FCP RSP */
  507. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  508. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  509. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  510. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  511. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  512. /*
  513. * Since the IOCB for the FCP I/O is built into this
  514. * lpfc_scsi_buf, initialize it with all known data now.
  515. */
  516. iocb = &psb->cur_iocbq.iocb;
  517. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  518. if ((phba->sli_rev == 3) &&
  519. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  520. /* fill in immediate fcp command BDE */
  521. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  522. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  523. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  524. unsli3.fcp_ext.icd);
  525. iocb->un.fcpi64.bdl.addrHigh = 0;
  526. iocb->ulpBdeCount = 0;
  527. iocb->ulpLe = 0;
  528. /* fill in response BDE */
  529. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
  530. BUFF_TYPE_BDE_64;
  531. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  532. sizeof(struct fcp_rsp);
  533. iocb->unsli3.fcp_ext.rbde.addrLow =
  534. putPaddrLow(pdma_phys_fcp_rsp);
  535. iocb->unsli3.fcp_ext.rbde.addrHigh =
  536. putPaddrHigh(pdma_phys_fcp_rsp);
  537. } else {
  538. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  539. iocb->un.fcpi64.bdl.bdeSize =
  540. (2 * sizeof(struct ulp_bde64));
  541. iocb->un.fcpi64.bdl.addrLow =
  542. putPaddrLow(pdma_phys_bpl);
  543. iocb->un.fcpi64.bdl.addrHigh =
  544. putPaddrHigh(pdma_phys_bpl);
  545. iocb->ulpBdeCount = 1;
  546. iocb->ulpLe = 1;
  547. }
  548. iocb->ulpClass = CLASS3;
  549. psb->status = IOSTAT_SUCCESS;
  550. /* Put it back into the SCSI buffer list */
  551. psb->cur_iocbq.context1 = psb;
  552. lpfc_release_scsi_buf_s3(phba, psb);
  553. }
  554. return bcnt;
  555. }
  556. /**
  557. * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
  558. * @vport: pointer to lpfc vport data structure.
  559. *
  560. * This routine is invoked by the vport cleanup for deletions and the cleanup
  561. * for an ndlp on removal.
  562. **/
  563. void
  564. lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
  565. {
  566. struct lpfc_hba *phba = vport->phba;
  567. struct lpfc_scsi_buf *psb, *next_psb;
  568. unsigned long iflag = 0;
  569. spin_lock_irqsave(&phba->hbalock, iflag);
  570. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  571. list_for_each_entry_safe(psb, next_psb,
  572. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  573. if (psb->rdata && psb->rdata->pnode
  574. && psb->rdata->pnode->vport == vport)
  575. psb->rdata = NULL;
  576. }
  577. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  578. spin_unlock_irqrestore(&phba->hbalock, iflag);
  579. }
  580. /**
  581. * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
  582. * @phba: pointer to lpfc hba data structure.
  583. * @axri: pointer to the fcp xri abort wcqe structure.
  584. *
  585. * This routine is invoked by the worker thread to process a SLI4 fast-path
  586. * FCP aborted xri.
  587. **/
  588. void
  589. lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
  590. struct sli4_wcqe_xri_aborted *axri)
  591. {
  592. uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
  593. uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
  594. struct lpfc_scsi_buf *psb, *next_psb;
  595. unsigned long iflag = 0;
  596. struct lpfc_iocbq *iocbq;
  597. int i;
  598. struct lpfc_nodelist *ndlp;
  599. int rrq_empty = 0;
  600. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  601. spin_lock_irqsave(&phba->hbalock, iflag);
  602. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  603. list_for_each_entry_safe(psb, next_psb,
  604. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  605. if (psb->cur_iocbq.sli4_xritag == xri) {
  606. list_del(&psb->list);
  607. psb->exch_busy = 0;
  608. psb->status = IOSTAT_SUCCESS;
  609. spin_unlock(
  610. &phba->sli4_hba.abts_scsi_buf_list_lock);
  611. if (psb->rdata && psb->rdata->pnode)
  612. ndlp = psb->rdata->pnode;
  613. else
  614. ndlp = NULL;
  615. rrq_empty = list_empty(&phba->active_rrq_list);
  616. spin_unlock_irqrestore(&phba->hbalock, iflag);
  617. if (ndlp)
  618. lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
  619. lpfc_release_scsi_buf_s4(phba, psb);
  620. if (rrq_empty)
  621. lpfc_worker_wake_up(phba);
  622. return;
  623. }
  624. }
  625. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  626. for (i = 1; i <= phba->sli.last_iotag; i++) {
  627. iocbq = phba->sli.iocbq_lookup[i];
  628. if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
  629. (iocbq->iocb_flag & LPFC_IO_LIBDFC))
  630. continue;
  631. if (iocbq->sli4_xritag != xri)
  632. continue;
  633. psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  634. psb->exch_busy = 0;
  635. spin_unlock_irqrestore(&phba->hbalock, iflag);
  636. if (pring->txq_cnt)
  637. lpfc_worker_wake_up(phba);
  638. return;
  639. }
  640. spin_unlock_irqrestore(&phba->hbalock, iflag);
  641. }
  642. /**
  643. * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
  644. * @phba: pointer to lpfc hba data structure.
  645. *
  646. * This routine walks the list of scsi buffers that have been allocated and
  647. * repost them to the HBA by using SGL block post. This is needed after a
  648. * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
  649. * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
  650. * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
  651. *
  652. * Returns: 0 = success, non-zero failure.
  653. **/
  654. int
  655. lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
  656. {
  657. struct lpfc_scsi_buf *psb;
  658. int index, status, bcnt = 0, rcnt = 0, rc = 0;
  659. LIST_HEAD(sblist);
  660. for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
  661. psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
  662. if (psb) {
  663. /* Remove from SCSI buffer list */
  664. list_del(&psb->list);
  665. /* Add it to a local SCSI buffer list */
  666. list_add_tail(&psb->list, &sblist);
  667. if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  668. bcnt = rcnt;
  669. rcnt = 0;
  670. }
  671. } else
  672. /* A hole present in the XRI array, need to skip */
  673. bcnt = rcnt;
  674. if (index == phba->sli4_hba.scsi_xri_cnt - 1)
  675. /* End of XRI array for SCSI buffer, complete */
  676. bcnt = rcnt;
  677. /* Continue until collect up to a nembed page worth of sgls */
  678. if (bcnt == 0)
  679. continue;
  680. /* Now, post the SCSI buffer list sgls as a block */
  681. if (!phba->sli4_hba.extents_in_use)
  682. status = lpfc_sli4_post_scsi_sgl_block(phba,
  683. &sblist,
  684. bcnt);
  685. else
  686. status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
  687. &sblist,
  688. bcnt);
  689. /* Reset SCSI buffer count for next round of posting */
  690. bcnt = 0;
  691. while (!list_empty(&sblist)) {
  692. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  693. list);
  694. if (status) {
  695. /* Put this back on the abort scsi list */
  696. psb->exch_busy = 1;
  697. rc++;
  698. } else {
  699. psb->exch_busy = 0;
  700. psb->status = IOSTAT_SUCCESS;
  701. }
  702. /* Put it back into the SCSI buffer list */
  703. lpfc_release_scsi_buf_s4(phba, psb);
  704. }
  705. }
  706. return rc;
  707. }
  708. /**
  709. * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
  710. * @vport: The virtual port for which this call being executed.
  711. * @num_to_allocate: The requested number of buffers to allocate.
  712. *
  713. * This routine allocates a scsi buffer for device with SLI-4 interface spec,
  714. * the scsi buffer contains all the necessary information needed to initiate
  715. * a SCSI I/O.
  716. *
  717. * Return codes:
  718. * int - number of scsi buffers that were allocated.
  719. * 0 = failure, less than num_to_alloc is a partial failure.
  720. **/
  721. static int
  722. lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
  723. {
  724. struct lpfc_hba *phba = vport->phba;
  725. struct lpfc_scsi_buf *psb;
  726. struct sli4_sge *sgl;
  727. IOCB_t *iocb;
  728. dma_addr_t pdma_phys_fcp_cmd;
  729. dma_addr_t pdma_phys_fcp_rsp;
  730. dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
  731. uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
  732. int status = 0, index;
  733. int bcnt;
  734. int non_sequential_xri = 0;
  735. LIST_HEAD(sblist);
  736. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  737. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  738. if (!psb)
  739. break;
  740. /*
  741. * Get memory from the pci pool to map the virt space to pci bus
  742. * space for an I/O. The DMA buffer includes space for the
  743. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  744. * necessary to support the sg_tablesize.
  745. */
  746. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  747. GFP_KERNEL, &psb->dma_handle);
  748. if (!psb->data) {
  749. kfree(psb);
  750. break;
  751. }
  752. /* Initialize virtual ptrs to dma_buf region. */
  753. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  754. /* Allocate iotag for psb->cur_iocbq. */
  755. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  756. if (iotag == 0) {
  757. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  758. psb->data, psb->dma_handle);
  759. kfree(psb);
  760. break;
  761. }
  762. lxri = lpfc_sli4_next_xritag(phba);
  763. if (lxri == NO_XRI) {
  764. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  765. psb->data, psb->dma_handle);
  766. kfree(psb);
  767. break;
  768. }
  769. psb->cur_iocbq.sli4_lxritag = lxri;
  770. psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  771. if (last_xritag != NO_XRI
  772. && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
  773. non_sequential_xri = 1;
  774. } else
  775. list_add_tail(&psb->list, &sblist);
  776. last_xritag = psb->cur_iocbq.sli4_xritag;
  777. index = phba->sli4_hba.scsi_xri_cnt++;
  778. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  779. psb->fcp_bpl = psb->data;
  780. psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
  781. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  782. psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
  783. sizeof(struct fcp_cmnd));
  784. /* Initialize local short-hand pointers. */
  785. sgl = (struct sli4_sge *)psb->fcp_bpl;
  786. pdma_phys_bpl = psb->dma_handle;
  787. pdma_phys_fcp_cmd =
  788. (psb->dma_handle + phba->cfg_sg_dma_buf_size)
  789. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  790. pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
  791. /*
  792. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  793. * are sg list bdes. Initialize the first two and leave the
  794. * rest for queuecommand.
  795. */
  796. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
  797. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
  798. sgl->word2 = le32_to_cpu(sgl->word2);
  799. bf_set(lpfc_sli4_sge_last, sgl, 0);
  800. sgl->word2 = cpu_to_le32(sgl->word2);
  801. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
  802. sgl++;
  803. /* Setup the physical region for the FCP RSP */
  804. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
  805. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
  806. sgl->word2 = le32_to_cpu(sgl->word2);
  807. bf_set(lpfc_sli4_sge_last, sgl, 1);
  808. sgl->word2 = cpu_to_le32(sgl->word2);
  809. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
  810. /*
  811. * Since the IOCB for the FCP I/O is built into this
  812. * lpfc_scsi_buf, initialize it with all known data now.
  813. */
  814. iocb = &psb->cur_iocbq.iocb;
  815. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  816. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
  817. /* setting the BLP size to 2 * sizeof BDE may not be correct.
  818. * We are setting the bpl to point to out sgl. An sgl's
  819. * entries are 16 bytes, a bpl entries are 12 bytes.
  820. */
  821. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  822. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
  823. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
  824. iocb->ulpBdeCount = 1;
  825. iocb->ulpLe = 1;
  826. iocb->ulpClass = CLASS3;
  827. psb->cur_iocbq.context1 = psb;
  828. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  829. pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
  830. else
  831. pdma_phys_bpl1 = 0;
  832. psb->dma_phys_bpl = pdma_phys_bpl;
  833. phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
  834. if (non_sequential_xri) {
  835. status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
  836. pdma_phys_bpl1,
  837. psb->cur_iocbq.sli4_xritag);
  838. if (status) {
  839. /* Put this back on the abort scsi list */
  840. psb->exch_busy = 1;
  841. } else {
  842. psb->exch_busy = 0;
  843. psb->status = IOSTAT_SUCCESS;
  844. }
  845. /* Put it back into the SCSI buffer list */
  846. lpfc_release_scsi_buf_s4(phba, psb);
  847. break;
  848. }
  849. }
  850. if (bcnt) {
  851. if (!phba->sli4_hba.extents_in_use)
  852. status = lpfc_sli4_post_scsi_sgl_block(phba,
  853. &sblist,
  854. bcnt);
  855. else
  856. status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
  857. &sblist,
  858. bcnt);
  859. if (status) {
  860. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  861. "3021 SCSI SGL post error %d\n",
  862. status);
  863. bcnt = 0;
  864. }
  865. /* Reset SCSI buffer count for next round of posting */
  866. while (!list_empty(&sblist)) {
  867. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  868. list);
  869. if (status) {
  870. /* Put this back on the abort scsi list */
  871. psb->exch_busy = 1;
  872. } else {
  873. psb->exch_busy = 0;
  874. psb->status = IOSTAT_SUCCESS;
  875. }
  876. /* Put it back into the SCSI buffer list */
  877. lpfc_release_scsi_buf_s4(phba, psb);
  878. }
  879. }
  880. return bcnt + non_sequential_xri;
  881. }
  882. /**
  883. * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
  884. * @vport: The virtual port for which this call being executed.
  885. * @num_to_allocate: The requested number of buffers to allocate.
  886. *
  887. * This routine wraps the actual SCSI buffer allocator function pointer from
  888. * the lpfc_hba struct.
  889. *
  890. * Return codes:
  891. * int - number of scsi buffers that were allocated.
  892. * 0 = failure, less than num_to_alloc is a partial failure.
  893. **/
  894. static inline int
  895. lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
  896. {
  897. return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
  898. }
  899. /**
  900. * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  901. * @phba: The HBA for which this call is being executed.
  902. *
  903. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  904. * and returns to caller.
  905. *
  906. * Return codes:
  907. * NULL - Error
  908. * Pointer to lpfc_scsi_buf - Success
  909. **/
  910. static struct lpfc_scsi_buf*
  911. lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  912. {
  913. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  914. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  915. unsigned long iflag = 0;
  916. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  917. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  918. if (lpfc_cmd) {
  919. lpfc_cmd->seg_cnt = 0;
  920. lpfc_cmd->nonsg_phys = 0;
  921. lpfc_cmd->prot_seg_cnt = 0;
  922. }
  923. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  924. return lpfc_cmd;
  925. }
  926. /**
  927. * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  928. * @phba: The HBA for which this call is being executed.
  929. *
  930. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  931. * and returns to caller.
  932. *
  933. * Return codes:
  934. * NULL - Error
  935. * Pointer to lpfc_scsi_buf - Success
  936. **/
  937. static struct lpfc_scsi_buf*
  938. lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  939. {
  940. struct lpfc_scsi_buf *lpfc_cmd ;
  941. unsigned long iflag = 0;
  942. int found = 0;
  943. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  944. list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
  945. list) {
  946. if (lpfc_test_rrq_active(phba, ndlp,
  947. lpfc_cmd->cur_iocbq.sli4_xritag))
  948. continue;
  949. list_del(&lpfc_cmd->list);
  950. found = 1;
  951. lpfc_cmd->seg_cnt = 0;
  952. lpfc_cmd->nonsg_phys = 0;
  953. lpfc_cmd->prot_seg_cnt = 0;
  954. break;
  955. }
  956. spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
  957. iflag);
  958. if (!found)
  959. return NULL;
  960. else
  961. return lpfc_cmd;
  962. }
  963. /**
  964. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  965. * @phba: The HBA for which this call is being executed.
  966. *
  967. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  968. * and returns to caller.
  969. *
  970. * Return codes:
  971. * NULL - Error
  972. * Pointer to lpfc_scsi_buf - Success
  973. **/
  974. static struct lpfc_scsi_buf*
  975. lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  976. {
  977. return phba->lpfc_get_scsi_buf(phba, ndlp);
  978. }
  979. /**
  980. * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
  981. * @phba: The Hba for which this call is being executed.
  982. * @psb: The scsi buffer which is being released.
  983. *
  984. * This routine releases @psb scsi buffer by adding it to tail of @phba
  985. * lpfc_scsi_buf_list list.
  986. **/
  987. static void
  988. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  989. {
  990. unsigned long iflag = 0;
  991. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  992. psb->pCmd = NULL;
  993. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  994. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  995. }
  996. /**
  997. * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
  998. * @phba: The Hba for which this call is being executed.
  999. * @psb: The scsi buffer which is being released.
  1000. *
  1001. * This routine releases @psb scsi buffer by adding it to tail of @phba
  1002. * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
  1003. * and cannot be reused for at least RA_TOV amount of time if it was
  1004. * aborted.
  1005. **/
  1006. static void
  1007. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  1008. {
  1009. unsigned long iflag = 0;
  1010. if (psb->exch_busy) {
  1011. spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
  1012. iflag);
  1013. psb->pCmd = NULL;
  1014. list_add_tail(&psb->list,
  1015. &phba->sli4_hba.lpfc_abts_scsi_buf_list);
  1016. spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
  1017. iflag);
  1018. } else {
  1019. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  1020. psb->pCmd = NULL;
  1021. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  1022. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  1023. }
  1024. }
  1025. /**
  1026. * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
  1027. * @phba: The Hba for which this call is being executed.
  1028. * @psb: The scsi buffer which is being released.
  1029. *
  1030. * This routine releases @psb scsi buffer by adding it to tail of @phba
  1031. * lpfc_scsi_buf_list list.
  1032. **/
  1033. static void
  1034. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  1035. {
  1036. phba->lpfc_release_scsi_buf(phba, psb);
  1037. }
  1038. /**
  1039. * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  1040. * @phba: The Hba for which this call is being executed.
  1041. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1042. *
  1043. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1044. * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
  1045. * through sg elements and format the bdea. This routine also initializes all
  1046. * IOCB fields which are dependent on scsi command request buffer.
  1047. *
  1048. * Return codes:
  1049. * 1 - Error
  1050. * 0 - Success
  1051. **/
  1052. static int
  1053. lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1054. {
  1055. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1056. struct scatterlist *sgel = NULL;
  1057. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1058. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1059. struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
  1060. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1061. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  1062. dma_addr_t physaddr;
  1063. uint32_t num_bde = 0;
  1064. int nseg, datadir = scsi_cmnd->sc_data_direction;
  1065. /*
  1066. * There are three possibilities here - use scatter-gather segment, use
  1067. * the single mapping, or neither. Start the lpfc command prep by
  1068. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1069. * data bde entry.
  1070. */
  1071. bpl += 2;
  1072. if (scsi_sg_count(scsi_cmnd)) {
  1073. /*
  1074. * The driver stores the segment count returned from pci_map_sg
  1075. * because this a count of dma-mappings used to map the use_sg
  1076. * pages. They are not guaranteed to be the same for those
  1077. * architectures that implement an IOMMU.
  1078. */
  1079. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  1080. scsi_sg_count(scsi_cmnd), datadir);
  1081. if (unlikely(!nseg))
  1082. return 1;
  1083. lpfc_cmd->seg_cnt = nseg;
  1084. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1085. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1086. "9064 BLKGRD: %s: Too many sg segments from "
  1087. "dma_map_sg. Config %d, seg_cnt %d\n",
  1088. __func__, phba->cfg_sg_seg_cnt,
  1089. lpfc_cmd->seg_cnt);
  1090. scsi_dma_unmap(scsi_cmnd);
  1091. return 1;
  1092. }
  1093. /*
  1094. * The driver established a maximum scatter-gather segment count
  1095. * during probe that limits the number of sg elements in any
  1096. * single scsi command. Just run through the seg_cnt and format
  1097. * the bde's.
  1098. * When using SLI-3 the driver will try to fit all the BDEs into
  1099. * the IOCB. If it can't then the BDEs get added to a BPL as it
  1100. * does for SLI-2 mode.
  1101. */
  1102. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  1103. physaddr = sg_dma_address(sgel);
  1104. if (phba->sli_rev == 3 &&
  1105. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1106. !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
  1107. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  1108. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1109. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  1110. data_bde->addrLow = putPaddrLow(physaddr);
  1111. data_bde->addrHigh = putPaddrHigh(physaddr);
  1112. data_bde++;
  1113. } else {
  1114. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1115. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  1116. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1117. bpl->addrLow =
  1118. le32_to_cpu(putPaddrLow(physaddr));
  1119. bpl->addrHigh =
  1120. le32_to_cpu(putPaddrHigh(physaddr));
  1121. bpl++;
  1122. }
  1123. }
  1124. }
  1125. /*
  1126. * Finish initializing those IOCB fields that are dependent on the
  1127. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  1128. * explicitly reinitialized and for SLI-3 the extended bde count is
  1129. * explicitly reinitialized since all iocb memory resources are reused.
  1130. */
  1131. if (phba->sli_rev == 3 &&
  1132. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1133. !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
  1134. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  1135. /*
  1136. * The extended IOCB format can only fit 3 BDE or a BPL.
  1137. * This I/O has more than 3 BDE so the 1st data bde will
  1138. * be a BPL that is filled in here.
  1139. */
  1140. physaddr = lpfc_cmd->dma_handle;
  1141. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  1142. data_bde->tus.f.bdeSize = (num_bde *
  1143. sizeof(struct ulp_bde64));
  1144. physaddr += (sizeof(struct fcp_cmnd) +
  1145. sizeof(struct fcp_rsp) +
  1146. (2 * sizeof(struct ulp_bde64)));
  1147. data_bde->addrHigh = putPaddrHigh(physaddr);
  1148. data_bde->addrLow = putPaddrLow(physaddr);
  1149. /* ebde count includes the response bde and data bpl */
  1150. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  1151. } else {
  1152. /* ebde count includes the response bde and data bdes */
  1153. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1154. }
  1155. } else {
  1156. iocb_cmd->un.fcpi64.bdl.bdeSize =
  1157. ((num_bde + 2) * sizeof(struct ulp_bde64));
  1158. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1159. }
  1160. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  1161. /*
  1162. * Due to difference in data length between DIF/non-DIF paths,
  1163. * we need to set word 4 of IOCB here
  1164. */
  1165. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1166. return 0;
  1167. }
  1168. static inline unsigned
  1169. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  1170. {
  1171. return sc->device->sector_size;
  1172. }
  1173. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1174. /*
  1175. * Given a scsi cmnd, determine the BlockGuard tags to be used with it
  1176. * @sc: The SCSI command to examine
  1177. * @reftag: (out) BlockGuard reference tag for transmitted data
  1178. * @apptag: (out) BlockGuard application tag for transmitted data
  1179. * @new_guard (in) Value to replace CRC with if needed
  1180. *
  1181. * Returns (1) if error injection was performed, (0) otherwise
  1182. */
  1183. static int
  1184. lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1185. uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
  1186. {
  1187. struct scatterlist *sgpe; /* s/g prot entry */
  1188. struct scatterlist *sgde; /* s/g data entry */
  1189. struct scsi_dif_tuple *src;
  1190. uint32_t op = scsi_get_prot_op(sc);
  1191. uint32_t blksize;
  1192. uint32_t numblks;
  1193. sector_t lba;
  1194. int rc = 0;
  1195. if (op == SCSI_PROT_NORMAL)
  1196. return 0;
  1197. lba = scsi_get_lba(sc);
  1198. if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
  1199. blksize = lpfc_cmd_blksize(sc);
  1200. numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
  1201. /* Make sure we have the right LBA if one is specified */
  1202. if ((phba->lpfc_injerr_lba < lba) ||
  1203. (phba->lpfc_injerr_lba >= (lba + numblks)))
  1204. return 0;
  1205. }
  1206. sgpe = scsi_prot_sglist(sc);
  1207. sgde = scsi_sglist(sc);
  1208. /* Should we change the Reference Tag */
  1209. if (reftag) {
  1210. /*
  1211. * If we are SCSI_PROT_WRITE_STRIP, the protection data is
  1212. * being stripped from the wire, thus it doesn't matter.
  1213. */
  1214. if ((op == SCSI_PROT_WRITE_PASS) ||
  1215. (op == SCSI_PROT_WRITE_INSERT)) {
  1216. if (phba->lpfc_injerr_wref_cnt) {
  1217. /* DEADBEEF will be the reftag on the wire */
  1218. *reftag = 0xDEADBEEF;
  1219. phba->lpfc_injerr_wref_cnt--;
  1220. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1221. rc = 1;
  1222. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1223. "9081 BLKGRD: Injecting reftag error: "
  1224. "write lba x%lx\n", (unsigned long)lba);
  1225. }
  1226. } else {
  1227. if (phba->lpfc_injerr_rref_cnt) {
  1228. *reftag = 0xDEADBEEF;
  1229. phba->lpfc_injerr_rref_cnt--;
  1230. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1231. rc = 1;
  1232. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1233. "9076 BLKGRD: Injecting reftag error: "
  1234. "read lba x%lx\n", (unsigned long)lba);
  1235. }
  1236. }
  1237. }
  1238. /* Should we change the Application Tag */
  1239. if (apptag) {
  1240. /*
  1241. * If we are SCSI_PROT_WRITE_STRIP, the protection data is
  1242. * being stripped from the wire, thus it doesn't matter.
  1243. */
  1244. if ((op == SCSI_PROT_WRITE_PASS) ||
  1245. (op == SCSI_PROT_WRITE_INSERT)) {
  1246. if (phba->lpfc_injerr_wapp_cnt) {
  1247. /* DEAD will be the apptag on the wire */
  1248. *apptag = 0xDEAD;
  1249. phba->lpfc_injerr_wapp_cnt--;
  1250. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1251. rc = 1;
  1252. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1253. "9077 BLKGRD: Injecting apptag error: "
  1254. "write lba x%lx\n", (unsigned long)lba);
  1255. }
  1256. } else {
  1257. if (phba->lpfc_injerr_rapp_cnt) {
  1258. *apptag = 0xDEAD;
  1259. phba->lpfc_injerr_rapp_cnt--;
  1260. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1261. rc = 1;
  1262. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1263. "9078 BLKGRD: Injecting apptag error: "
  1264. "read lba x%lx\n", (unsigned long)lba);
  1265. }
  1266. }
  1267. }
  1268. /* Should we change the Guard Tag */
  1269. /*
  1270. * If we are SCSI_PROT_WRITE_INSERT, the protection data is
  1271. * being on the wire is being fully generated on the HBA.
  1272. * The host cannot change it or force an error.
  1273. */
  1274. if (((op == SCSI_PROT_WRITE_STRIP) ||
  1275. (op == SCSI_PROT_WRITE_PASS)) &&
  1276. phba->lpfc_injerr_wgrd_cnt) {
  1277. if (sgpe) {
  1278. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  1279. /*
  1280. * Just inject an error in the first
  1281. * prot block.
  1282. */
  1283. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1284. "9079 BLKGRD: Injecting guard error: "
  1285. "write lba x%lx oldGuard x%x refTag x%x\n",
  1286. (unsigned long)lba, src->guard_tag,
  1287. src->ref_tag);
  1288. src->guard_tag = (uint16_t)new_guard;
  1289. phba->lpfc_injerr_wgrd_cnt--;
  1290. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1291. rc = 1;
  1292. } else {
  1293. blksize = lpfc_cmd_blksize(sc);
  1294. /*
  1295. * Jump past the first data block
  1296. * and inject an error in the
  1297. * prot data. The prot data is already
  1298. * embedded after the regular data.
  1299. */
  1300. src = (struct scsi_dif_tuple *)
  1301. (sg_virt(sgde) + blksize);
  1302. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1303. "9080 BLKGRD: Injecting guard error: "
  1304. "write lba x%lx oldGuard x%x refTag x%x\n",
  1305. (unsigned long)lba, src->guard_tag,
  1306. src->ref_tag);
  1307. src->guard_tag = (uint16_t)new_guard;
  1308. phba->lpfc_injerr_wgrd_cnt--;
  1309. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1310. rc = 1;
  1311. }
  1312. }
  1313. return rc;
  1314. }
  1315. #endif
  1316. /*
  1317. * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
  1318. * @sc: The SCSI command to examine
  1319. * @txopt: (out) BlockGuard operation for transmitted data
  1320. * @rxopt: (out) BlockGuard operation for received data
  1321. *
  1322. * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
  1323. *
  1324. */
  1325. static int
  1326. lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1327. uint8_t *txop, uint8_t *rxop)
  1328. {
  1329. uint8_t guard_type = scsi_host_get_guard(sc->device->host);
  1330. uint8_t ret = 0;
  1331. if (guard_type == SHOST_DIX_GUARD_IP) {
  1332. switch (scsi_get_prot_op(sc)) {
  1333. case SCSI_PROT_READ_INSERT:
  1334. case SCSI_PROT_WRITE_STRIP:
  1335. *txop = BG_OP_IN_CSUM_OUT_NODIF;
  1336. *rxop = BG_OP_IN_NODIF_OUT_CSUM;
  1337. break;
  1338. case SCSI_PROT_READ_STRIP:
  1339. case SCSI_PROT_WRITE_INSERT:
  1340. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1341. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1342. break;
  1343. case SCSI_PROT_READ_PASS:
  1344. case SCSI_PROT_WRITE_PASS:
  1345. *txop = BG_OP_IN_CSUM_OUT_CRC;
  1346. *rxop = BG_OP_IN_CRC_OUT_CSUM;
  1347. break;
  1348. case SCSI_PROT_NORMAL:
  1349. default:
  1350. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1351. "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
  1352. scsi_get_prot_op(sc));
  1353. ret = 1;
  1354. break;
  1355. }
  1356. } else {
  1357. switch (scsi_get_prot_op(sc)) {
  1358. case SCSI_PROT_READ_STRIP:
  1359. case SCSI_PROT_WRITE_INSERT:
  1360. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1361. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1362. break;
  1363. case SCSI_PROT_READ_PASS:
  1364. case SCSI_PROT_WRITE_PASS:
  1365. *txop = BG_OP_IN_CRC_OUT_CRC;
  1366. *rxop = BG_OP_IN_CRC_OUT_CRC;
  1367. break;
  1368. case SCSI_PROT_READ_INSERT:
  1369. case SCSI_PROT_WRITE_STRIP:
  1370. *txop = BG_OP_IN_CRC_OUT_NODIF;
  1371. *rxop = BG_OP_IN_NODIF_OUT_CRC;
  1372. break;
  1373. case SCSI_PROT_NORMAL:
  1374. default:
  1375. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1376. "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
  1377. scsi_get_prot_op(sc));
  1378. ret = 1;
  1379. break;
  1380. }
  1381. }
  1382. return ret;
  1383. }
  1384. /*
  1385. * This function sets up buffer list for protection groups of
  1386. * type LPFC_PG_TYPE_NO_DIF
  1387. *
  1388. * This is usually used when the HBA is instructed to generate
  1389. * DIFs and insert them into data stream (or strip DIF from
  1390. * incoming data stream)
  1391. *
  1392. * The buffer list consists of just one protection group described
  1393. * below:
  1394. * +-------------------------+
  1395. * start of prot group --> | PDE_5 |
  1396. * +-------------------------+
  1397. * | PDE_6 |
  1398. * +-------------------------+
  1399. * | Data BDE |
  1400. * +-------------------------+
  1401. * |more Data BDE's ... (opt)|
  1402. * +-------------------------+
  1403. *
  1404. * @sc: pointer to scsi command we're working on
  1405. * @bpl: pointer to buffer list for protection groups
  1406. * @datacnt: number of segments of data that have been dma mapped
  1407. *
  1408. * Note: Data s/g buffers have been dma mapped
  1409. */
  1410. static int
  1411. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1412. struct ulp_bde64 *bpl, int datasegcnt)
  1413. {
  1414. struct scatterlist *sgde = NULL; /* s/g data entry */
  1415. struct lpfc_pde5 *pde5 = NULL;
  1416. struct lpfc_pde6 *pde6 = NULL;
  1417. dma_addr_t physaddr;
  1418. int i = 0, num_bde = 0, status;
  1419. int datadir = sc->sc_data_direction;
  1420. uint32_t reftag;
  1421. unsigned blksize;
  1422. uint8_t txop, rxop;
  1423. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1424. if (status)
  1425. goto out;
  1426. /* extract some info from the scsi command for pde*/
  1427. blksize = lpfc_cmd_blksize(sc);
  1428. reftag = scsi_get_lba(sc) & 0xffffffff;
  1429. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1430. /* reftag is the only error we can inject here */
  1431. lpfc_bg_err_inject(phba, sc, &reftag, 0, 0);
  1432. #endif
  1433. /* setup PDE5 with what we have */
  1434. pde5 = (struct lpfc_pde5 *) bpl;
  1435. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1436. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1437. /* Endianness conversion if necessary for PDE5 */
  1438. pde5->word0 = cpu_to_le32(pde5->word0);
  1439. pde5->reftag = cpu_to_le32(reftag);
  1440. /* advance bpl and increment bde count */
  1441. num_bde++;
  1442. bpl++;
  1443. pde6 = (struct lpfc_pde6 *) bpl;
  1444. /* setup PDE6 with the rest of the info */
  1445. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1446. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1447. bf_set(pde6_optx, pde6, txop);
  1448. bf_set(pde6_oprx, pde6, rxop);
  1449. if (datadir == DMA_FROM_DEVICE) {
  1450. bf_set(pde6_ce, pde6, 1);
  1451. bf_set(pde6_re, pde6, 1);
  1452. }
  1453. bf_set(pde6_ai, pde6, 1);
  1454. bf_set(pde6_ae, pde6, 0);
  1455. bf_set(pde6_apptagval, pde6, 0);
  1456. /* Endianness conversion if necessary for PDE6 */
  1457. pde6->word0 = cpu_to_le32(pde6->word0);
  1458. pde6->word1 = cpu_to_le32(pde6->word1);
  1459. pde6->word2 = cpu_to_le32(pde6->word2);
  1460. /* advance bpl and increment bde count */
  1461. num_bde++;
  1462. bpl++;
  1463. /* assumption: caller has already run dma_map_sg on command data */
  1464. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  1465. physaddr = sg_dma_address(sgde);
  1466. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1467. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1468. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  1469. if (datadir == DMA_TO_DEVICE)
  1470. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1471. else
  1472. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1473. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1474. bpl++;
  1475. num_bde++;
  1476. }
  1477. out:
  1478. return num_bde;
  1479. }
  1480. /*
  1481. * This function sets up buffer list for protection groups of
  1482. * type LPFC_PG_TYPE_DIF_BUF
  1483. *
  1484. * This is usually used when DIFs are in their own buffers,
  1485. * separate from the data. The HBA can then by instructed
  1486. * to place the DIFs in the outgoing stream. For read operations,
  1487. * The HBA could extract the DIFs and place it in DIF buffers.
  1488. *
  1489. * The buffer list for this type consists of one or more of the
  1490. * protection groups described below:
  1491. * +-------------------------+
  1492. * start of first prot group --> | PDE_5 |
  1493. * +-------------------------+
  1494. * | PDE_6 |
  1495. * +-------------------------+
  1496. * | PDE_7 (Prot BDE) |
  1497. * +-------------------------+
  1498. * | Data BDE |
  1499. * +-------------------------+
  1500. * |more Data BDE's ... (opt)|
  1501. * +-------------------------+
  1502. * start of new prot group --> | PDE_5 |
  1503. * +-------------------------+
  1504. * | ... |
  1505. * +-------------------------+
  1506. *
  1507. * @sc: pointer to scsi command we're working on
  1508. * @bpl: pointer to buffer list for protection groups
  1509. * @datacnt: number of segments of data that have been dma mapped
  1510. * @protcnt: number of segment of protection data that have been dma mapped
  1511. *
  1512. * Note: It is assumed that both data and protection s/g buffers have been
  1513. * mapped for DMA
  1514. */
  1515. static int
  1516. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1517. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  1518. {
  1519. struct scatterlist *sgde = NULL; /* s/g data entry */
  1520. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1521. struct lpfc_pde5 *pde5 = NULL;
  1522. struct lpfc_pde6 *pde6 = NULL;
  1523. struct lpfc_pde7 *pde7 = NULL;
  1524. dma_addr_t dataphysaddr, protphysaddr;
  1525. unsigned short curr_data = 0, curr_prot = 0;
  1526. unsigned int split_offset;
  1527. unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
  1528. unsigned int protgrp_blks, protgrp_bytes;
  1529. unsigned int remainder, subtotal;
  1530. int status;
  1531. int datadir = sc->sc_data_direction;
  1532. unsigned char pgdone = 0, alldone = 0;
  1533. unsigned blksize;
  1534. uint32_t reftag;
  1535. uint8_t txop, rxop;
  1536. int num_bde = 0;
  1537. sgpe = scsi_prot_sglist(sc);
  1538. sgde = scsi_sglist(sc);
  1539. if (!sgpe || !sgde) {
  1540. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1541. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  1542. sgpe, sgde);
  1543. return 0;
  1544. }
  1545. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1546. if (status)
  1547. goto out;
  1548. /* extract some info from the scsi command */
  1549. blksize = lpfc_cmd_blksize(sc);
  1550. reftag = scsi_get_lba(sc) & 0xffffffff;
  1551. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1552. /* reftag / guard tag are the only errors we can inject here */
  1553. lpfc_bg_err_inject(phba, sc, &reftag, 0, 0xDEAD);
  1554. #endif
  1555. split_offset = 0;
  1556. do {
  1557. /* setup PDE5 with what we have */
  1558. pde5 = (struct lpfc_pde5 *) bpl;
  1559. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1560. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1561. /* Endianness conversion if necessary for PDE5 */
  1562. pde5->word0 = cpu_to_le32(pde5->word0);
  1563. pde5->reftag = cpu_to_le32(reftag);
  1564. /* advance bpl and increment bde count */
  1565. num_bde++;
  1566. bpl++;
  1567. pde6 = (struct lpfc_pde6 *) bpl;
  1568. /* setup PDE6 with the rest of the info */
  1569. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1570. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1571. bf_set(pde6_optx, pde6, txop);
  1572. bf_set(pde6_oprx, pde6, rxop);
  1573. bf_set(pde6_ce, pde6, 1);
  1574. bf_set(pde6_re, pde6, 1);
  1575. bf_set(pde6_ai, pde6, 1);
  1576. bf_set(pde6_ae, pde6, 0);
  1577. bf_set(pde6_apptagval, pde6, 0);
  1578. /* Endianness conversion if necessary for PDE6 */
  1579. pde6->word0 = cpu_to_le32(pde6->word0);
  1580. pde6->word1 = cpu_to_le32(pde6->word1);
  1581. pde6->word2 = cpu_to_le32(pde6->word2);
  1582. /* advance bpl and increment bde count */
  1583. num_bde++;
  1584. bpl++;
  1585. /* setup the first BDE that points to protection buffer */
  1586. protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
  1587. protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
  1588. /* must be integer multiple of the DIF block length */
  1589. BUG_ON(protgroup_len % 8);
  1590. pde7 = (struct lpfc_pde7 *) bpl;
  1591. memset(pde7, 0, sizeof(struct lpfc_pde7));
  1592. bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
  1593. pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  1594. pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  1595. protgrp_blks = protgroup_len / 8;
  1596. protgrp_bytes = protgrp_blks * blksize;
  1597. /* check if this pde is crossing the 4K boundary; if so split */
  1598. if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
  1599. protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
  1600. protgroup_offset += protgroup_remainder;
  1601. protgrp_blks = protgroup_remainder / 8;
  1602. protgrp_bytes = protgrp_blks * blksize;
  1603. } else {
  1604. protgroup_offset = 0;
  1605. curr_prot++;
  1606. }
  1607. num_bde++;
  1608. /* setup BDE's for data blocks associated with DIF data */
  1609. pgdone = 0;
  1610. subtotal = 0; /* total bytes processed for current prot grp */
  1611. while (!pgdone) {
  1612. if (!sgde) {
  1613. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1614. "9065 BLKGRD:%s Invalid data segment\n",
  1615. __func__);
  1616. return 0;
  1617. }
  1618. bpl++;
  1619. dataphysaddr = sg_dma_address(sgde) + split_offset;
  1620. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  1621. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  1622. remainder = sg_dma_len(sgde) - split_offset;
  1623. if ((subtotal + remainder) <= protgrp_bytes) {
  1624. /* we can use this whole buffer */
  1625. bpl->tus.f.bdeSize = remainder;
  1626. split_offset = 0;
  1627. if ((subtotal + remainder) == protgrp_bytes)
  1628. pgdone = 1;
  1629. } else {
  1630. /* must split this buffer with next prot grp */
  1631. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  1632. split_offset += bpl->tus.f.bdeSize;
  1633. }
  1634. subtotal += bpl->tus.f.bdeSize;
  1635. if (datadir == DMA_TO_DEVICE)
  1636. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1637. else
  1638. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1639. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1640. num_bde++;
  1641. curr_data++;
  1642. if (split_offset)
  1643. break;
  1644. /* Move to the next s/g segment if possible */
  1645. sgde = sg_next(sgde);
  1646. }
  1647. if (protgroup_offset) {
  1648. /* update the reference tag */
  1649. reftag += protgrp_blks;
  1650. bpl++;
  1651. continue;
  1652. }
  1653. /* are we done ? */
  1654. if (curr_prot == protcnt) {
  1655. alldone = 1;
  1656. } else if (curr_prot < protcnt) {
  1657. /* advance to next prot buffer */
  1658. sgpe = sg_next(sgpe);
  1659. bpl++;
  1660. /* update the reference tag */
  1661. reftag += protgrp_blks;
  1662. } else {
  1663. /* if we're here, we have a bug */
  1664. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1665. "9054 BLKGRD: bug in %s\n", __func__);
  1666. }
  1667. } while (!alldone);
  1668. out:
  1669. return num_bde;
  1670. }
  1671. /*
  1672. * Given a SCSI command that supports DIF, determine composition of protection
  1673. * groups involved in setting up buffer lists
  1674. *
  1675. * Returns:
  1676. * for DIF (for both read and write)
  1677. * */
  1678. static int
  1679. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1680. {
  1681. int ret = LPFC_PG_TYPE_INVALID;
  1682. unsigned char op = scsi_get_prot_op(sc);
  1683. switch (op) {
  1684. case SCSI_PROT_READ_STRIP:
  1685. case SCSI_PROT_WRITE_INSERT:
  1686. ret = LPFC_PG_TYPE_NO_DIF;
  1687. break;
  1688. case SCSI_PROT_READ_INSERT:
  1689. case SCSI_PROT_WRITE_STRIP:
  1690. case SCSI_PROT_READ_PASS:
  1691. case SCSI_PROT_WRITE_PASS:
  1692. ret = LPFC_PG_TYPE_DIF_BUF;
  1693. break;
  1694. default:
  1695. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1696. "9021 Unsupported protection op:%d\n", op);
  1697. break;
  1698. }
  1699. return ret;
  1700. }
  1701. /*
  1702. * This is the protection/DIF aware version of
  1703. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  1704. * two functions eventually, but for now, it's here
  1705. */
  1706. static int
  1707. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
  1708. struct lpfc_scsi_buf *lpfc_cmd)
  1709. {
  1710. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1711. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1712. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1713. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1714. uint32_t num_bde = 0;
  1715. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  1716. int prot_group_type = 0;
  1717. int diflen, fcpdl;
  1718. unsigned blksize;
  1719. /*
  1720. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  1721. * fcp_rsp regions to the first data bde entry
  1722. */
  1723. bpl += 2;
  1724. if (scsi_sg_count(scsi_cmnd)) {
  1725. /*
  1726. * The driver stores the segment count returned from pci_map_sg
  1727. * because this a count of dma-mappings used to map the use_sg
  1728. * pages. They are not guaranteed to be the same for those
  1729. * architectures that implement an IOMMU.
  1730. */
  1731. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  1732. scsi_sglist(scsi_cmnd),
  1733. scsi_sg_count(scsi_cmnd), datadir);
  1734. if (unlikely(!datasegcnt))
  1735. return 1;
  1736. lpfc_cmd->seg_cnt = datasegcnt;
  1737. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1738. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1739. "9067 BLKGRD: %s: Too many sg segments"
  1740. " from dma_map_sg. Config %d, seg_cnt"
  1741. " %d\n",
  1742. __func__, phba->cfg_sg_seg_cnt,
  1743. lpfc_cmd->seg_cnt);
  1744. scsi_dma_unmap(scsi_cmnd);
  1745. return 1;
  1746. }
  1747. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  1748. switch (prot_group_type) {
  1749. case LPFC_PG_TYPE_NO_DIF:
  1750. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  1751. datasegcnt);
  1752. /* we should have 2 or more entries in buffer list */
  1753. if (num_bde < 2)
  1754. goto err;
  1755. break;
  1756. case LPFC_PG_TYPE_DIF_BUF:{
  1757. /*
  1758. * This type indicates that protection buffers are
  1759. * passed to the driver, so that needs to be prepared
  1760. * for DMA
  1761. */
  1762. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  1763. scsi_prot_sglist(scsi_cmnd),
  1764. scsi_prot_sg_count(scsi_cmnd), datadir);
  1765. if (unlikely(!protsegcnt)) {
  1766. scsi_dma_unmap(scsi_cmnd);
  1767. return 1;
  1768. }
  1769. lpfc_cmd->prot_seg_cnt = protsegcnt;
  1770. if (lpfc_cmd->prot_seg_cnt
  1771. > phba->cfg_prot_sg_seg_cnt) {
  1772. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1773. "9068 BLKGRD: %s: Too many prot sg "
  1774. "segments from dma_map_sg. Config %d,"
  1775. "prot_seg_cnt %d\n", __func__,
  1776. phba->cfg_prot_sg_seg_cnt,
  1777. lpfc_cmd->prot_seg_cnt);
  1778. dma_unmap_sg(&phba->pcidev->dev,
  1779. scsi_prot_sglist(scsi_cmnd),
  1780. scsi_prot_sg_count(scsi_cmnd),
  1781. datadir);
  1782. scsi_dma_unmap(scsi_cmnd);
  1783. return 1;
  1784. }
  1785. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  1786. datasegcnt, protsegcnt);
  1787. /* we should have 3 or more entries in buffer list */
  1788. if (num_bde < 3)
  1789. goto err;
  1790. break;
  1791. }
  1792. case LPFC_PG_TYPE_INVALID:
  1793. default:
  1794. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1795. "9022 Unexpected protection group %i\n",
  1796. prot_group_type);
  1797. return 1;
  1798. }
  1799. }
  1800. /*
  1801. * Finish initializing those IOCB fields that are dependent on the
  1802. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  1803. * reinitialized since all iocb memory resources are used many times
  1804. * for transmit, receive, and continuation bpl's.
  1805. */
  1806. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  1807. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  1808. iocb_cmd->ulpBdeCount = 1;
  1809. iocb_cmd->ulpLe = 1;
  1810. fcpdl = scsi_bufflen(scsi_cmnd);
  1811. if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
  1812. /*
  1813. * We are in DIF Type 1 mode
  1814. * Every data block has a 8 byte DIF (trailer)
  1815. * attached to it. Must ajust FCP data length
  1816. */
  1817. blksize = lpfc_cmd_blksize(scsi_cmnd);
  1818. diflen = (fcpdl / blksize) * 8;
  1819. fcpdl += diflen;
  1820. }
  1821. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  1822. /*
  1823. * Due to difference in data length between DIF/non-DIF paths,
  1824. * we need to set word 4 of IOCB here
  1825. */
  1826. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  1827. return 0;
  1828. err:
  1829. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1830. "9023 Could not setup all needed BDE's"
  1831. "prot_group_type=%d, num_bde=%d\n",
  1832. prot_group_type, num_bde);
  1833. return 1;
  1834. }
  1835. /*
  1836. * This function checks for BlockGuard errors detected by
  1837. * the HBA. In case of errors, the ASC/ASCQ fields in the
  1838. * sense buffer will be set accordingly, paired with
  1839. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  1840. * detected corruption.
  1841. *
  1842. * Returns:
  1843. * 0 - No error found
  1844. * 1 - BlockGuard error found
  1845. * -1 - Internal error (bad profile, ...etc)
  1846. */
  1847. static int
  1848. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  1849. struct lpfc_iocbq *pIocbOut)
  1850. {
  1851. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1852. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  1853. int ret = 0;
  1854. uint32_t bghm = bgf->bghm;
  1855. uint32_t bgstat = bgf->bgstat;
  1856. uint64_t failing_sector = 0;
  1857. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
  1858. " 0x%x lba 0x%llx blk cnt 0x%x "
  1859. "bgstat=0x%x bghm=0x%x\n",
  1860. cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
  1861. blk_rq_sectors(cmd->request), bgstat, bghm);
  1862. spin_lock(&_dump_buf_lock);
  1863. if (!_dump_buf_done) {
  1864. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
  1865. " Data for %u blocks to debugfs\n",
  1866. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1867. lpfc_debug_save_data(phba, cmd);
  1868. /* If we have a prot sgl, save the DIF buffer */
  1869. if (lpfc_prot_group_type(phba, cmd) ==
  1870. LPFC_PG_TYPE_DIF_BUF) {
  1871. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
  1872. "Saving DIF for %u blocks to debugfs\n",
  1873. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1874. lpfc_debug_save_dif(phba, cmd);
  1875. }
  1876. _dump_buf_done = 1;
  1877. }
  1878. spin_unlock(&_dump_buf_lock);
  1879. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  1880. cmd->result = ScsiResult(DID_ERROR, 0);
  1881. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
  1882. " BlockGuard profile. bgstat:0x%x\n",
  1883. bgstat);
  1884. ret = (-1);
  1885. goto out;
  1886. }
  1887. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  1888. cmd->result = ScsiResult(DID_ERROR, 0);
  1889. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
  1890. "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
  1891. bgstat);
  1892. ret = (-1);
  1893. goto out;
  1894. }
  1895. if (lpfc_bgs_get_guard_err(bgstat)) {
  1896. ret = 1;
  1897. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1898. 0x10, 0x1);
  1899. cmd->result = DRIVER_SENSE << 24
  1900. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1901. phba->bg_guard_err_cnt++;
  1902. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1903. "9055 BLKGRD: guard_tag error\n");
  1904. }
  1905. if (lpfc_bgs_get_reftag_err(bgstat)) {
  1906. ret = 1;
  1907. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1908. 0x10, 0x3);
  1909. cmd->result = DRIVER_SENSE << 24
  1910. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1911. phba->bg_reftag_err_cnt++;
  1912. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1913. "9056 BLKGRD: ref_tag error\n");
  1914. }
  1915. if (lpfc_bgs_get_apptag_err(bgstat)) {
  1916. ret = 1;
  1917. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1918. 0x10, 0x2);
  1919. cmd->result = DRIVER_SENSE << 24
  1920. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1921. phba->bg_apptag_err_cnt++;
  1922. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1923. "9061 BLKGRD: app_tag error\n");
  1924. }
  1925. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  1926. /*
  1927. * setup sense data descriptor 0 per SPC-4 as an information
  1928. * field, and put the failing LBA in it.
  1929. * This code assumes there was also a guard/app/ref tag error
  1930. * indication.
  1931. */
  1932. cmd->sense_buffer[7] = 0xc; /* Additional sense length */
  1933. cmd->sense_buffer[8] = 0; /* Information descriptor type */
  1934. cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
  1935. cmd->sense_buffer[10] = 0x80; /* Validity bit */
  1936. bghm /= cmd->device->sector_size;
  1937. failing_sector = scsi_get_lba(cmd);
  1938. failing_sector += bghm;
  1939. /* Descriptor Information */
  1940. put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
  1941. }
  1942. if (!ret) {
  1943. /* No error was reported - problem in FW? */
  1944. cmd->result = ScsiResult(DID_ERROR, 0);
  1945. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1946. "9057 BLKGRD: no errors reported!\n");
  1947. }
  1948. out:
  1949. return ret;
  1950. }
  1951. /**
  1952. * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  1953. * @phba: The Hba for which this call is being executed.
  1954. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1955. *
  1956. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1957. * field of @lpfc_cmd for device with SLI-4 interface spec.
  1958. *
  1959. * Return codes:
  1960. * 1 - Error
  1961. * 0 - Success
  1962. **/
  1963. static int
  1964. lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1965. {
  1966. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1967. struct scatterlist *sgel = NULL;
  1968. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1969. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  1970. struct sli4_sge *first_data_sgl;
  1971. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1972. dma_addr_t physaddr;
  1973. uint32_t num_bde = 0;
  1974. uint32_t dma_len;
  1975. uint32_t dma_offset = 0;
  1976. int nseg;
  1977. struct ulp_bde64 *bde;
  1978. /*
  1979. * There are three possibilities here - use scatter-gather segment, use
  1980. * the single mapping, or neither. Start the lpfc command prep by
  1981. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1982. * data bde entry.
  1983. */
  1984. if (scsi_sg_count(scsi_cmnd)) {
  1985. /*
  1986. * The driver stores the segment count returned from pci_map_sg
  1987. * because this a count of dma-mappings used to map the use_sg
  1988. * pages. They are not guaranteed to be the same for those
  1989. * architectures that implement an IOMMU.
  1990. */
  1991. nseg = scsi_dma_map(scsi_cmnd);
  1992. if (unlikely(!nseg))
  1993. return 1;
  1994. sgl += 1;
  1995. /* clear the last flag in the fcp_rsp map entry */
  1996. sgl->word2 = le32_to_cpu(sgl->word2);
  1997. bf_set(lpfc_sli4_sge_last, sgl, 0);
  1998. sgl->word2 = cpu_to_le32(sgl->word2);
  1999. sgl += 1;
  2000. first_data_sgl = sgl;
  2001. lpfc_cmd->seg_cnt = nseg;
  2002. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  2003. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
  2004. " %s: Too many sg segments from "
  2005. "dma_map_sg. Config %d, seg_cnt %d\n",
  2006. __func__, phba->cfg_sg_seg_cnt,
  2007. lpfc_cmd->seg_cnt);
  2008. scsi_dma_unmap(scsi_cmnd);
  2009. return 1;
  2010. }
  2011. /*
  2012. * The driver established a maximum scatter-gather segment count
  2013. * during probe that limits the number of sg elements in any
  2014. * single scsi command. Just run through the seg_cnt and format
  2015. * the sge's.
  2016. * When using SLI-3 the driver will try to fit all the BDEs into
  2017. * the IOCB. If it can't then the BDEs get added to a BPL as it
  2018. * does for SLI-2 mode.
  2019. */
  2020. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  2021. physaddr = sg_dma_address(sgel);
  2022. dma_len = sg_dma_len(sgel);
  2023. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  2024. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  2025. sgl->word2 = le32_to_cpu(sgl->word2);
  2026. if ((num_bde + 1) == nseg)
  2027. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2028. else
  2029. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2030. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2031. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  2032. sgl->word2 = cpu_to_le32(sgl->word2);
  2033. sgl->sge_len = cpu_to_le32(dma_len);
  2034. dma_offset += dma_len;
  2035. sgl++;
  2036. }
  2037. /* setup the performance hint (first data BDE) if enabled */
  2038. if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
  2039. bde = (struct ulp_bde64 *)
  2040. &(iocb_cmd->unsli3.sli3Words[5]);
  2041. bde->addrLow = first_data_sgl->addr_lo;
  2042. bde->addrHigh = first_data_sgl->addr_hi;
  2043. bde->tus.f.bdeSize =
  2044. le32_to_cpu(first_data_sgl->sge_len);
  2045. bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  2046. bde->tus.w = cpu_to_le32(bde->tus.w);
  2047. }
  2048. } else {
  2049. sgl += 1;
  2050. /* clear the last flag in the fcp_rsp map entry */
  2051. sgl->word2 = le32_to_cpu(sgl->word2);
  2052. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2053. sgl->word2 = cpu_to_le32(sgl->word2);
  2054. }
  2055. /*
  2056. * Finish initializing those IOCB fields that are dependent on the
  2057. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  2058. * explicitly reinitialized.
  2059. * all iocb memory resources are reused.
  2060. */
  2061. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  2062. /*
  2063. * Due to difference in data length between DIF/non-DIF paths,
  2064. * we need to set word 4 of IOCB here
  2065. */
  2066. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  2067. return 0;
  2068. }
  2069. /**
  2070. * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  2071. * @phba: The Hba for which this call is being executed.
  2072. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  2073. *
  2074. * This routine wraps the actual DMA mapping function pointer from the
  2075. * lpfc_hba struct.
  2076. *
  2077. * Return codes:
  2078. * 1 - Error
  2079. * 0 - Success
  2080. **/
  2081. static inline int
  2082. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  2083. {
  2084. return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  2085. }
  2086. /**
  2087. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  2088. * @phba: Pointer to hba context object.
  2089. * @vport: Pointer to vport object.
  2090. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  2091. * @rsp_iocb: Pointer to response iocb object which reported error.
  2092. *
  2093. * This function posts an event when there is a SCSI command reporting
  2094. * error from the scsi device.
  2095. **/
  2096. static void
  2097. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  2098. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  2099. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  2100. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  2101. uint32_t resp_info = fcprsp->rspStatus2;
  2102. uint32_t scsi_status = fcprsp->rspStatus3;
  2103. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  2104. struct lpfc_fast_path_event *fast_path_evt = NULL;
  2105. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  2106. unsigned long flags;
  2107. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2108. return;
  2109. /* If there is queuefull or busy condition send a scsi event */
  2110. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  2111. (cmnd->result == SAM_STAT_BUSY)) {
  2112. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2113. if (!fast_path_evt)
  2114. return;
  2115. fast_path_evt->un.scsi_evt.event_type =
  2116. FC_REG_SCSI_EVENT;
  2117. fast_path_evt->un.scsi_evt.subcategory =
  2118. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  2119. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  2120. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  2121. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  2122. &pnode->nlp_portname, sizeof(struct lpfc_name));
  2123. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  2124. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2125. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  2126. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  2127. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2128. if (!fast_path_evt)
  2129. return;
  2130. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  2131. FC_REG_SCSI_EVENT;
  2132. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  2133. LPFC_EVENT_CHECK_COND;
  2134. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  2135. cmnd->device->lun;
  2136. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  2137. &pnode->nlp_portname, sizeof(struct lpfc_name));
  2138. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  2139. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2140. fast_path_evt->un.check_cond_evt.sense_key =
  2141. cmnd->sense_buffer[2] & 0xf;
  2142. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  2143. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  2144. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  2145. fcpi_parm &&
  2146. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  2147. ((scsi_status == SAM_STAT_GOOD) &&
  2148. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  2149. /*
  2150. * If status is good or resid does not match with fcp_param and
  2151. * there is valid fcpi_parm, then there is a read_check error
  2152. */
  2153. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2154. if (!fast_path_evt)
  2155. return;
  2156. fast_path_evt->un.read_check_error.header.event_type =
  2157. FC_REG_FABRIC_EVENT;
  2158. fast_path_evt->un.read_check_error.header.subcategory =
  2159. LPFC_EVENT_FCPRDCHKERR;
  2160. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  2161. &pnode->nlp_portname, sizeof(struct lpfc_name));
  2162. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  2163. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2164. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  2165. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  2166. fast_path_evt->un.read_check_error.fcpiparam =
  2167. fcpi_parm;
  2168. } else
  2169. return;
  2170. fast_path_evt->vport = vport;
  2171. spin_lock_irqsave(&phba->hbalock, flags);
  2172. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  2173. spin_unlock_irqrestore(&phba->hbalock, flags);
  2174. lpfc_worker_wake_up(phba);
  2175. return;
  2176. }
  2177. /**
  2178. * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
  2179. * @phba: The HBA for which this call is being executed.
  2180. * @psb: The scsi buffer which is going to be un-mapped.
  2181. *
  2182. * This routine does DMA un-mapping of scatter gather list of scsi command
  2183. * field of @lpfc_cmd for device with SLI-3 interface spec.
  2184. **/
  2185. static void
  2186. lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  2187. {
  2188. /*
  2189. * There are only two special cases to consider. (1) the scsi command
  2190. * requested scatter-gather usage or (2) the scsi command allocated
  2191. * a request buffer, but did not request use_sg. There is a third
  2192. * case, but it does not require resource deallocation.
  2193. */
  2194. if (psb->seg_cnt > 0)
  2195. scsi_dma_unmap(psb->pCmd);
  2196. if (psb->prot_seg_cnt > 0)
  2197. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  2198. scsi_prot_sg_count(psb->pCmd),
  2199. psb->pCmd->sc_data_direction);
  2200. }
  2201. /**
  2202. * lpfc_handler_fcp_err - FCP response handler
  2203. * @vport: The virtual port for which this call is being executed.
  2204. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  2205. * @rsp_iocb: The response IOCB which contains FCP error.
  2206. *
  2207. * This routine is called to process response IOCB with status field
  2208. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  2209. * based upon SCSI and FCP error.
  2210. **/
  2211. static void
  2212. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  2213. struct lpfc_iocbq *rsp_iocb)
  2214. {
  2215. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  2216. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  2217. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  2218. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  2219. uint32_t resp_info = fcprsp->rspStatus2;
  2220. uint32_t scsi_status = fcprsp->rspStatus3;
  2221. uint32_t *lp;
  2222. uint32_t host_status = DID_OK;
  2223. uint32_t rsplen = 0;
  2224. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  2225. /*
  2226. * If this is a task management command, there is no
  2227. * scsi packet associated with this lpfc_cmd. The driver
  2228. * consumes it.
  2229. */
  2230. if (fcpcmd->fcpCntl2) {
  2231. scsi_status = 0;
  2232. goto out;
  2233. }
  2234. if (resp_info & RSP_LEN_VALID) {
  2235. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  2236. if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
  2237. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2238. "2719 Invalid response length: "
  2239. "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
  2240. cmnd->device->id,
  2241. cmnd->device->lun, cmnd->cmnd[0],
  2242. rsplen);
  2243. host_status = DID_ERROR;
  2244. goto out;
  2245. }
  2246. if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
  2247. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2248. "2757 Protocol failure detected during "
  2249. "processing of FCP I/O op: "
  2250. "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
  2251. cmnd->device->id,
  2252. cmnd->device->lun, cmnd->cmnd[0],
  2253. fcprsp->rspInfo3);
  2254. host_status = DID_ERROR;
  2255. goto out;
  2256. }
  2257. }
  2258. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  2259. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  2260. if (snslen > SCSI_SENSE_BUFFERSIZE)
  2261. snslen = SCSI_SENSE_BUFFERSIZE;
  2262. if (resp_info & RSP_LEN_VALID)
  2263. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  2264. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  2265. }
  2266. lp = (uint32_t *)cmnd->sense_buffer;
  2267. if (!scsi_status && (resp_info & RESID_UNDER) &&
  2268. vport->cfg_log_verbose & LOG_FCP_UNDER)
  2269. logit = LOG_FCP_UNDER;
  2270. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  2271. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  2272. "Data: x%x x%x x%x x%x x%x\n",
  2273. cmnd->cmnd[0], scsi_status,
  2274. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  2275. be32_to_cpu(fcprsp->rspResId),
  2276. be32_to_cpu(fcprsp->rspSnsLen),
  2277. be32_to_cpu(fcprsp->rspRspLen),
  2278. fcprsp->rspInfo3);
  2279. scsi_set_resid(cmnd, 0);
  2280. if (resp_info & RESID_UNDER) {
  2281. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  2282. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
  2283. "9025 FCP Read Underrun, expected %d, "
  2284. "residual %d Data: x%x x%x x%x\n",
  2285. be32_to_cpu(fcpcmd->fcpDl),
  2286. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  2287. cmnd->underflow);
  2288. /*
  2289. * If there is an under run check if under run reported by
  2290. * storage array is same as the under run reported by HBA.
  2291. * If this is not same, there is a dropped frame.
  2292. */
  2293. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  2294. fcpi_parm &&
  2295. (scsi_get_resid(cmnd) != fcpi_parm)) {
  2296. lpfc_printf_vlog(vport, KERN_WARNING,
  2297. LOG_FCP | LOG_FCP_ERROR,
  2298. "9026 FCP Read Check Error "
  2299. "and Underrun Data: x%x x%x x%x x%x\n",
  2300. be32_to_cpu(fcpcmd->fcpDl),
  2301. scsi_get_resid(cmnd), fcpi_parm,
  2302. cmnd->cmnd[0]);
  2303. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  2304. host_status = DID_ERROR;
  2305. }
  2306. /*
  2307. * The cmnd->underflow is the minimum number of bytes that must
  2308. * be transferred for this command. Provided a sense condition
  2309. * is not present, make sure the actual amount transferred is at
  2310. * least the underflow value or fail.
  2311. */
  2312. if (!(resp_info & SNS_LEN_VALID) &&
  2313. (scsi_status == SAM_STAT_GOOD) &&
  2314. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  2315. < cmnd->underflow)) {
  2316. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2317. "9027 FCP command x%x residual "
  2318. "underrun converted to error "
  2319. "Data: x%x x%x x%x\n",
  2320. cmnd->cmnd[0], scsi_bufflen(cmnd),
  2321. scsi_get_resid(cmnd), cmnd->underflow);
  2322. host_status = DID_ERROR;
  2323. }
  2324. } else if (resp_info & RESID_OVER) {
  2325. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2326. "9028 FCP command x%x residual overrun error. "
  2327. "Data: x%x x%x\n", cmnd->cmnd[0],
  2328. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  2329. host_status = DID_ERROR;
  2330. /*
  2331. * Check SLI validation that all the transfer was actually done
  2332. * (fcpi_parm should be zero). Apply check only to reads.
  2333. */
  2334. } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  2335. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  2336. "9029 FCP Read Check Error Data: "
  2337. "x%x x%x x%x x%x x%x\n",
  2338. be32_to_cpu(fcpcmd->fcpDl),
  2339. be32_to_cpu(fcprsp->rspResId),
  2340. fcpi_parm, cmnd->cmnd[0], scsi_status);
  2341. switch (scsi_status) {
  2342. case SAM_STAT_GOOD:
  2343. case SAM_STAT_CHECK_CONDITION:
  2344. /* Fabric dropped a data frame. Fail any successful
  2345. * command in which we detected dropped frames.
  2346. * A status of good or some check conditions could
  2347. * be considered a successful command.
  2348. */
  2349. host_status = DID_ERROR;
  2350. break;
  2351. }
  2352. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  2353. }
  2354. out:
  2355. cmnd->result = ScsiResult(host_status, scsi_status);
  2356. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  2357. }
  2358. /**
  2359. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  2360. * @phba: The Hba for which this call is being executed.
  2361. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  2362. * @pIocbOut: The response IOCBQ for the scsi cmnd.
  2363. *
  2364. * This routine assigns scsi command result by looking into response IOCB
  2365. * status field appropriately. This routine handles QUEUE FULL condition as
  2366. * well by ramping down device queue depth.
  2367. **/
  2368. static void
  2369. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  2370. struct lpfc_iocbq *pIocbOut)
  2371. {
  2372. struct lpfc_scsi_buf *lpfc_cmd =
  2373. (struct lpfc_scsi_buf *) pIocbIn->context1;
  2374. struct lpfc_vport *vport = pIocbIn->vport;
  2375. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2376. struct lpfc_nodelist *pnode = rdata->pnode;
  2377. struct scsi_cmnd *cmd;
  2378. int result;
  2379. struct scsi_device *tmp_sdev;
  2380. int depth;
  2381. unsigned long flags;
  2382. struct lpfc_fast_path_event *fast_path_evt;
  2383. struct Scsi_Host *shost;
  2384. uint32_t queue_depth, scsi_id;
  2385. uint32_t logit = LOG_FCP;
  2386. /* Sanity check on return of outstanding command */
  2387. if (!(lpfc_cmd->pCmd))
  2388. return;
  2389. cmd = lpfc_cmd->pCmd;
  2390. shost = cmd->device->host;
  2391. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  2392. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  2393. /* pick up SLI4 exhange busy status from HBA */
  2394. lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
  2395. if (pnode && NLP_CHK_NODE_ACT(pnode))
  2396. atomic_dec(&pnode->cmd_pending);
  2397. if (lpfc_cmd->status) {
  2398. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  2399. (lpfc_cmd->result & IOERR_DRVR_MASK))
  2400. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  2401. else if (lpfc_cmd->status >= IOSTAT_CNT)
  2402. lpfc_cmd->status = IOSTAT_DEFAULT;
  2403. if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
  2404. && !lpfc_cmd->fcp_rsp->rspStatus3
  2405. && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
  2406. && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
  2407. logit = 0;
  2408. else
  2409. logit = LOG_FCP | LOG_FCP_UNDER;
  2410. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  2411. "9030 FCP cmd x%x failed <%d/%d> "
  2412. "status: x%x result: x%x Data: x%x x%x\n",
  2413. cmd->cmnd[0],
  2414. cmd->device ? cmd->device->id : 0xffff,
  2415. cmd->device ? cmd->device->lun : 0xffff,
  2416. lpfc_cmd->status, lpfc_cmd->result,
  2417. pIocbOut->iocb.ulpContext,
  2418. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  2419. switch (lpfc_cmd->status) {
  2420. case IOSTAT_FCP_RSP_ERROR:
  2421. /* Call FCP RSP handler to determine result */
  2422. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  2423. break;
  2424. case IOSTAT_NPORT_BSY:
  2425. case IOSTAT_FABRIC_BSY:
  2426. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2427. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2428. if (!fast_path_evt)
  2429. break;
  2430. fast_path_evt->un.fabric_evt.event_type =
  2431. FC_REG_FABRIC_EVENT;
  2432. fast_path_evt->un.fabric_evt.subcategory =
  2433. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  2434. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  2435. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2436. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  2437. &pnode->nlp_portname,
  2438. sizeof(struct lpfc_name));
  2439. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  2440. &pnode->nlp_nodename,
  2441. sizeof(struct lpfc_name));
  2442. }
  2443. fast_path_evt->vport = vport;
  2444. fast_path_evt->work_evt.evt =
  2445. LPFC_EVT_FASTPATH_MGMT_EVT;
  2446. spin_lock_irqsave(&phba->hbalock, flags);
  2447. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  2448. &phba->work_list);
  2449. spin_unlock_irqrestore(&phba->hbalock, flags);
  2450. lpfc_worker_wake_up(phba);
  2451. break;
  2452. case IOSTAT_LOCAL_REJECT:
  2453. case IOSTAT_REMOTE_STOP:
  2454. if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
  2455. lpfc_cmd->result ==
  2456. IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
  2457. lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
  2458. lpfc_cmd->result ==
  2459. IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
  2460. cmd->result = ScsiResult(DID_NO_CONNECT, 0);
  2461. break;
  2462. }
  2463. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  2464. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  2465. lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
  2466. lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
  2467. cmd->result = ScsiResult(DID_REQUEUE, 0);
  2468. break;
  2469. }
  2470. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  2471. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  2472. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  2473. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  2474. /*
  2475. * This is a response for a BG enabled
  2476. * cmd. Parse BG error
  2477. */
  2478. lpfc_parse_bg_err(phba, lpfc_cmd,
  2479. pIocbOut);
  2480. break;
  2481. } else {
  2482. lpfc_printf_vlog(vport, KERN_WARNING,
  2483. LOG_BG,
  2484. "9031 non-zero BGSTAT "
  2485. "on unprotected cmd\n");
  2486. }
  2487. }
  2488. if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
  2489. && (phba->sli_rev == LPFC_SLI_REV4)
  2490. && (pnode && NLP_CHK_NODE_ACT(pnode))) {
  2491. /* This IO was aborted by the target, we don't
  2492. * know the rxid and because we did not send the
  2493. * ABTS we cannot generate and RRQ.
  2494. */
  2495. lpfc_set_rrq_active(phba, pnode,
  2496. lpfc_cmd->cur_iocbq.sli4_xritag,
  2497. 0, 0);
  2498. }
  2499. /* else: fall through */
  2500. default:
  2501. cmd->result = ScsiResult(DID_ERROR, 0);
  2502. break;
  2503. }
  2504. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  2505. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  2506. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  2507. SAM_STAT_BUSY);
  2508. } else
  2509. cmd->result = ScsiResult(DID_OK, 0);
  2510. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  2511. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  2512. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2513. "0710 Iodone <%d/%d> cmd %p, error "
  2514. "x%x SNS x%x x%x Data: x%x x%x\n",
  2515. cmd->device->id, cmd->device->lun, cmd,
  2516. cmd->result, *lp, *(lp + 3), cmd->retries,
  2517. scsi_get_resid(cmd));
  2518. }
  2519. lpfc_update_stats(phba, lpfc_cmd);
  2520. result = cmd->result;
  2521. if (vport->cfg_max_scsicmpl_time &&
  2522. time_after(jiffies, lpfc_cmd->start_time +
  2523. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  2524. spin_lock_irqsave(shost->host_lock, flags);
  2525. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2526. if (pnode->cmd_qdepth >
  2527. atomic_read(&pnode->cmd_pending) &&
  2528. (atomic_read(&pnode->cmd_pending) >
  2529. LPFC_MIN_TGT_QDEPTH) &&
  2530. ((cmd->cmnd[0] == READ_10) ||
  2531. (cmd->cmnd[0] == WRITE_10)))
  2532. pnode->cmd_qdepth =
  2533. atomic_read(&pnode->cmd_pending);
  2534. pnode->last_change_time = jiffies;
  2535. }
  2536. spin_unlock_irqrestore(shost->host_lock, flags);
  2537. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2538. if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
  2539. time_after(jiffies, pnode->last_change_time +
  2540. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  2541. spin_lock_irqsave(shost->host_lock, flags);
  2542. depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
  2543. / 100;
  2544. depth = depth ? depth : 1;
  2545. pnode->cmd_qdepth += depth;
  2546. if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
  2547. pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
  2548. pnode->last_change_time = jiffies;
  2549. spin_unlock_irqrestore(shost->host_lock, flags);
  2550. }
  2551. }
  2552. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2553. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  2554. queue_depth = cmd->device->queue_depth;
  2555. scsi_id = cmd->device->id;
  2556. cmd->scsi_done(cmd);
  2557. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2558. /*
  2559. * If there is a thread waiting for command completion
  2560. * wake up the thread.
  2561. */
  2562. spin_lock_irqsave(shost->host_lock, flags);
  2563. lpfc_cmd->pCmd = NULL;
  2564. if (lpfc_cmd->waitq)
  2565. wake_up(lpfc_cmd->waitq);
  2566. spin_unlock_irqrestore(shost->host_lock, flags);
  2567. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2568. return;
  2569. }
  2570. if (!result)
  2571. lpfc_rampup_queue_depth(vport, queue_depth);
  2572. /*
  2573. * Check for queue full. If the lun is reporting queue full, then
  2574. * back off the lun queue depth to prevent target overloads.
  2575. */
  2576. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  2577. NLP_CHK_NODE_ACT(pnode)) {
  2578. shost_for_each_device(tmp_sdev, shost) {
  2579. if (tmp_sdev->id != scsi_id)
  2580. continue;
  2581. depth = scsi_track_queue_full(tmp_sdev,
  2582. tmp_sdev->queue_depth-1);
  2583. if (depth <= 0)
  2584. continue;
  2585. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2586. "0711 detected queue full - lun queue "
  2587. "depth adjusted to %d.\n", depth);
  2588. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  2589. pnode,
  2590. tmp_sdev->lun,
  2591. depth+1, depth);
  2592. }
  2593. }
  2594. /*
  2595. * If there is a thread waiting for command completion
  2596. * wake up the thread.
  2597. */
  2598. spin_lock_irqsave(shost->host_lock, flags);
  2599. lpfc_cmd->pCmd = NULL;
  2600. if (lpfc_cmd->waitq)
  2601. wake_up(lpfc_cmd->waitq);
  2602. spin_unlock_irqrestore(shost->host_lock, flags);
  2603. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2604. }
  2605. /**
  2606. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  2607. * @data: A pointer to the immediate command data portion of the IOCB.
  2608. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  2609. *
  2610. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  2611. * byte swapping the data to big endian format for transmission on the wire.
  2612. **/
  2613. static void
  2614. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  2615. {
  2616. int i, j;
  2617. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  2618. i += sizeof(uint32_t), j++) {
  2619. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  2620. }
  2621. }
  2622. /**
  2623. * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
  2624. * @vport: The virtual port for which this call is being executed.
  2625. * @lpfc_cmd: The scsi command which needs to send.
  2626. * @pnode: Pointer to lpfc_nodelist.
  2627. *
  2628. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  2629. * to transfer for device with SLI3 interface spec.
  2630. **/
  2631. static void
  2632. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  2633. struct lpfc_nodelist *pnode)
  2634. {
  2635. struct lpfc_hba *phba = vport->phba;
  2636. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2637. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2638. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2639. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  2640. int datadir = scsi_cmnd->sc_data_direction;
  2641. char tag[2];
  2642. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2643. return;
  2644. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  2645. /* clear task management bits */
  2646. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  2647. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  2648. &lpfc_cmd->fcp_cmnd->fcp_lun);
  2649. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  2650. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  2651. switch (tag[0]) {
  2652. case HEAD_OF_QUEUE_TAG:
  2653. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  2654. break;
  2655. case ORDERED_QUEUE_TAG:
  2656. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  2657. break;
  2658. default:
  2659. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  2660. break;
  2661. }
  2662. } else
  2663. fcp_cmnd->fcpCntl1 = 0;
  2664. /*
  2665. * There are three possibilities here - use scatter-gather segment, use
  2666. * the single mapping, or neither. Start the lpfc command prep by
  2667. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  2668. * data bde entry.
  2669. */
  2670. if (scsi_sg_count(scsi_cmnd)) {
  2671. if (datadir == DMA_TO_DEVICE) {
  2672. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  2673. if (phba->sli_rev < LPFC_SLI_REV4) {
  2674. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2675. iocb_cmd->ulpPU = 0;
  2676. } else
  2677. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2678. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  2679. phba->fc4OutputRequests++;
  2680. } else {
  2681. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  2682. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2683. fcp_cmnd->fcpCntl3 = READ_DATA;
  2684. phba->fc4InputRequests++;
  2685. }
  2686. } else {
  2687. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  2688. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2689. iocb_cmd->ulpPU = 0;
  2690. fcp_cmnd->fcpCntl3 = 0;
  2691. phba->fc4ControlRequests++;
  2692. }
  2693. if (phba->sli_rev == 3 &&
  2694. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2695. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  2696. /*
  2697. * Finish initializing those IOCB fields that are independent
  2698. * of the scsi_cmnd request_buffer
  2699. */
  2700. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  2701. if (phba->sli_rev == LPFC_SLI_REV4)
  2702. piocbq->iocb.ulpContext =
  2703. phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
  2704. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  2705. piocbq->iocb.ulpFCP2Rcvy = 1;
  2706. else
  2707. piocbq->iocb.ulpFCP2Rcvy = 0;
  2708. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  2709. piocbq->context1 = lpfc_cmd;
  2710. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2711. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  2712. piocbq->vport = vport;
  2713. }
  2714. /**
  2715. * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
  2716. * @vport: The virtual port for which this call is being executed.
  2717. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  2718. * @lun: Logical unit number.
  2719. * @task_mgmt_cmd: SCSI task management command.
  2720. *
  2721. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  2722. * for device with SLI-3 interface spec.
  2723. *
  2724. * Return codes:
  2725. * 0 - Error
  2726. * 1 - Success
  2727. **/
  2728. static int
  2729. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  2730. struct lpfc_scsi_buf *lpfc_cmd,
  2731. unsigned int lun,
  2732. uint8_t task_mgmt_cmd)
  2733. {
  2734. struct lpfc_iocbq *piocbq;
  2735. IOCB_t *piocb;
  2736. struct fcp_cmnd *fcp_cmnd;
  2737. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2738. struct lpfc_nodelist *ndlp = rdata->pnode;
  2739. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  2740. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  2741. return 0;
  2742. piocbq = &(lpfc_cmd->cur_iocbq);
  2743. piocbq->vport = vport;
  2744. piocb = &piocbq->iocb;
  2745. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2746. /* Clear out any old data in the FCP command area */
  2747. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2748. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  2749. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  2750. if (vport->phba->sli_rev == 3 &&
  2751. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2752. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  2753. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  2754. piocb->ulpContext = ndlp->nlp_rpi;
  2755. if (vport->phba->sli_rev == LPFC_SLI_REV4) {
  2756. piocb->ulpContext =
  2757. vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  2758. }
  2759. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  2760. piocb->ulpFCP2Rcvy = 1;
  2761. }
  2762. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  2763. /* ulpTimeout is only one byte */
  2764. if (lpfc_cmd->timeout > 0xff) {
  2765. /*
  2766. * Do not timeout the command at the firmware level.
  2767. * The driver will provide the timeout mechanism.
  2768. */
  2769. piocb->ulpTimeout = 0;
  2770. } else
  2771. piocb->ulpTimeout = lpfc_cmd->timeout;
  2772. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  2773. lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
  2774. return 1;
  2775. }
  2776. /**
  2777. * lpfc_scsi_api_table_setup - Set up scsi api function jump table
  2778. * @phba: The hba struct for which this call is being executed.
  2779. * @dev_grp: The HBA PCI-Device group number.
  2780. *
  2781. * This routine sets up the SCSI interface API function jump table in @phba
  2782. * struct.
  2783. * Returns: 0 - success, -ENODEV - failure.
  2784. **/
  2785. int
  2786. lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  2787. {
  2788. phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
  2789. phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
  2790. switch (dev_grp) {
  2791. case LPFC_PCI_DEV_LP:
  2792. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
  2793. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
  2794. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
  2795. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
  2796. break;
  2797. case LPFC_PCI_DEV_OC:
  2798. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
  2799. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
  2800. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
  2801. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
  2802. break;
  2803. default:
  2804. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2805. "1418 Invalid HBA PCI-device group: 0x%x\n",
  2806. dev_grp);
  2807. return -ENODEV;
  2808. break;
  2809. }
  2810. phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
  2811. phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2812. return 0;
  2813. }
  2814. /**
  2815. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  2816. * @phba: The Hba for which this call is being executed.
  2817. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  2818. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  2819. *
  2820. * This routine is IOCB completion routine for device reset and target reset
  2821. * routine. This routine release scsi buffer associated with lpfc_cmd.
  2822. **/
  2823. static void
  2824. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  2825. struct lpfc_iocbq *cmdiocbq,
  2826. struct lpfc_iocbq *rspiocbq)
  2827. {
  2828. struct lpfc_scsi_buf *lpfc_cmd =
  2829. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  2830. if (lpfc_cmd)
  2831. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2832. return;
  2833. }
  2834. /**
  2835. * lpfc_info - Info entry point of scsi_host_template data structure
  2836. * @host: The scsi host for which this call is being executed.
  2837. *
  2838. * This routine provides module information about hba.
  2839. *
  2840. * Reutrn code:
  2841. * Pointer to char - Success.
  2842. **/
  2843. const char *
  2844. lpfc_info(struct Scsi_Host *host)
  2845. {
  2846. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  2847. struct lpfc_hba *phba = vport->phba;
  2848. int len;
  2849. static char lpfcinfobuf[384];
  2850. memset(lpfcinfobuf,0,384);
  2851. if (phba && phba->pcidev){
  2852. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  2853. len = strlen(lpfcinfobuf);
  2854. snprintf(lpfcinfobuf + len,
  2855. 384-len,
  2856. " on PCI bus %02x device %02x irq %d",
  2857. phba->pcidev->bus->number,
  2858. phba->pcidev->devfn,
  2859. phba->pcidev->irq);
  2860. len = strlen(lpfcinfobuf);
  2861. if (phba->Port[0]) {
  2862. snprintf(lpfcinfobuf + len,
  2863. 384-len,
  2864. " port %s",
  2865. phba->Port);
  2866. }
  2867. len = strlen(lpfcinfobuf);
  2868. if (phba->sli4_hba.link_state.logical_speed) {
  2869. snprintf(lpfcinfobuf + len,
  2870. 384-len,
  2871. " Logical Link Speed: %d Mbps",
  2872. phba->sli4_hba.link_state.logical_speed * 10);
  2873. }
  2874. }
  2875. return lpfcinfobuf;
  2876. }
  2877. /**
  2878. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  2879. * @phba: The Hba for which this call is being executed.
  2880. *
  2881. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  2882. * The default value of cfg_poll_tmo is 10 milliseconds.
  2883. **/
  2884. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  2885. {
  2886. unsigned long poll_tmo_expires =
  2887. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  2888. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  2889. mod_timer(&phba->fcp_poll_timer,
  2890. poll_tmo_expires);
  2891. }
  2892. /**
  2893. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  2894. * @phba: The Hba for which this call is being executed.
  2895. *
  2896. * This routine starts the fcp_poll_timer of @phba.
  2897. **/
  2898. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  2899. {
  2900. lpfc_poll_rearm_timer(phba);
  2901. }
  2902. /**
  2903. * lpfc_poll_timeout - Restart polling timer
  2904. * @ptr: Map to lpfc_hba data structure pointer.
  2905. *
  2906. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  2907. * and FCP Ring interrupt is disable.
  2908. **/
  2909. void lpfc_poll_timeout(unsigned long ptr)
  2910. {
  2911. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2912. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2913. lpfc_sli_handle_fast_ring_event(phba,
  2914. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2915. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2916. lpfc_poll_rearm_timer(phba);
  2917. }
  2918. }
  2919. /**
  2920. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  2921. * @cmnd: Pointer to scsi_cmnd data structure.
  2922. * @done: Pointer to done routine.
  2923. *
  2924. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  2925. * This routine prepares an IOCB from scsi command and provides to firmware.
  2926. * The @done callback is invoked after driver finished processing the command.
  2927. *
  2928. * Return value :
  2929. * 0 - Success
  2930. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  2931. **/
  2932. static int
  2933. lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  2934. {
  2935. struct Scsi_Host *shost = cmnd->device->host;
  2936. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2937. struct lpfc_hba *phba = vport->phba;
  2938. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2939. struct lpfc_nodelist *ndlp;
  2940. struct lpfc_scsi_buf *lpfc_cmd;
  2941. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2942. int err;
  2943. err = fc_remote_port_chkready(rport);
  2944. if (err) {
  2945. cmnd->result = err;
  2946. goto out_fail_command;
  2947. }
  2948. ndlp = rdata->pnode;
  2949. if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
  2950. (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) ||
  2951. (phba->sli_rev == LPFC_SLI_REV4))) {
  2952. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2953. "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
  2954. " op:%02x str=%s without registering for"
  2955. " BlockGuard - Rejecting command\n",
  2956. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2957. dif_op_str[scsi_get_prot_op(cmnd)]);
  2958. goto out_fail_command;
  2959. }
  2960. /*
  2961. * Catch race where our node has transitioned, but the
  2962. * transport is still transitioning.
  2963. */
  2964. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  2965. cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
  2966. goto out_fail_command;
  2967. }
  2968. if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
  2969. goto out_tgt_busy;
  2970. lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
  2971. if (lpfc_cmd == NULL) {
  2972. lpfc_rampdown_queue_depth(phba);
  2973. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2974. "0707 driver's buffer pool is empty, "
  2975. "IO busied\n");
  2976. goto out_host_busy;
  2977. }
  2978. /*
  2979. * Store the midlayer's command structure for the completion phase
  2980. * and complete the command initialization.
  2981. */
  2982. lpfc_cmd->pCmd = cmnd;
  2983. lpfc_cmd->rdata = rdata;
  2984. lpfc_cmd->timeout = 0;
  2985. lpfc_cmd->start_time = jiffies;
  2986. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  2987. cmnd->scsi_done = done;
  2988. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2989. if (vport->phba->cfg_enable_bg) {
  2990. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2991. "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
  2992. "str=%s\n",
  2993. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2994. dif_op_str[scsi_get_prot_op(cmnd)]);
  2995. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2996. "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2997. "%02x %02x %02x %02x %02x\n",
  2998. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2999. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  3000. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  3001. cmnd->cmnd[9]);
  3002. if (cmnd->cmnd[0] == READ_10)
  3003. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3004. "9035 BLKGRD: READ @ sector %llu, "
  3005. "count %u\n",
  3006. (unsigned long long)scsi_get_lba(cmnd),
  3007. blk_rq_sectors(cmnd->request));
  3008. else if (cmnd->cmnd[0] == WRITE_10)
  3009. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3010. "9036 BLKGRD: WRITE @ sector %llu, "
  3011. "count %u cmd=%p\n",
  3012. (unsigned long long)scsi_get_lba(cmnd),
  3013. blk_rq_sectors(cmnd->request),
  3014. cmnd);
  3015. }
  3016. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  3017. } else {
  3018. if (vport->phba->cfg_enable_bg) {
  3019. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3020. "9038 BLKGRD: rcvd unprotected cmd:"
  3021. "%02x op:%02x str=%s\n",
  3022. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  3023. dif_op_str[scsi_get_prot_op(cmnd)]);
  3024. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3025. "9039 BLKGRD: CDB: %02x %02x %02x "
  3026. "%02x %02x %02x %02x %02x %02x %02x\n",
  3027. cmnd->cmnd[0], cmnd->cmnd[1],
  3028. cmnd->cmnd[2], cmnd->cmnd[3],
  3029. cmnd->cmnd[4], cmnd->cmnd[5],
  3030. cmnd->cmnd[6], cmnd->cmnd[7],
  3031. cmnd->cmnd[8], cmnd->cmnd[9]);
  3032. if (cmnd->cmnd[0] == READ_10)
  3033. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3034. "9040 dbg: READ @ sector %llu, "
  3035. "count %u\n",
  3036. (unsigned long long)scsi_get_lba(cmnd),
  3037. blk_rq_sectors(cmnd->request));
  3038. else if (cmnd->cmnd[0] == WRITE_10)
  3039. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3040. "9041 dbg: WRITE @ sector %llu, "
  3041. "count %u cmd=%p\n",
  3042. (unsigned long long)scsi_get_lba(cmnd),
  3043. blk_rq_sectors(cmnd->request), cmnd);
  3044. else
  3045. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3046. "9042 dbg: parser not implemented\n");
  3047. }
  3048. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  3049. }
  3050. if (err)
  3051. goto out_host_busy_free_buf;
  3052. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  3053. atomic_inc(&ndlp->cmd_pending);
  3054. err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
  3055. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  3056. if (err) {
  3057. atomic_dec(&ndlp->cmd_pending);
  3058. goto out_host_busy_free_buf;
  3059. }
  3060. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  3061. spin_unlock(shost->host_lock);
  3062. lpfc_sli_handle_fast_ring_event(phba,
  3063. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3064. spin_lock(shost->host_lock);
  3065. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3066. lpfc_poll_rearm_timer(phba);
  3067. }
  3068. return 0;
  3069. out_host_busy_free_buf:
  3070. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  3071. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3072. out_host_busy:
  3073. return SCSI_MLQUEUE_HOST_BUSY;
  3074. out_tgt_busy:
  3075. return SCSI_MLQUEUE_TARGET_BUSY;
  3076. out_fail_command:
  3077. done(cmnd);
  3078. return 0;
  3079. }
  3080. static DEF_SCSI_QCMD(lpfc_queuecommand)
  3081. /**
  3082. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  3083. * @cmnd: Pointer to scsi_cmnd data structure.
  3084. *
  3085. * This routine aborts @cmnd pending in base driver.
  3086. *
  3087. * Return code :
  3088. * 0x2003 - Error
  3089. * 0x2002 - Success
  3090. **/
  3091. static int
  3092. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  3093. {
  3094. struct Scsi_Host *shost = cmnd->device->host;
  3095. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3096. struct lpfc_hba *phba = vport->phba;
  3097. struct lpfc_iocbq *iocb;
  3098. struct lpfc_iocbq *abtsiocb;
  3099. struct lpfc_scsi_buf *lpfc_cmd;
  3100. IOCB_t *cmd, *icmd;
  3101. int ret = SUCCESS;
  3102. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  3103. ret = fc_block_scsi_eh(cmnd);
  3104. if (ret)
  3105. return ret;
  3106. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  3107. if (!lpfc_cmd) {
  3108. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3109. "2873 SCSI Layer I/O Abort Request IO CMPL Status "
  3110. "x%x ID %d LUN %d\n",
  3111. ret, cmnd->device->id, cmnd->device->lun);
  3112. return SUCCESS;
  3113. }
  3114. /*
  3115. * If pCmd field of the corresponding lpfc_scsi_buf structure
  3116. * points to a different SCSI command, then the driver has
  3117. * already completed this command, but the midlayer did not
  3118. * see the completion before the eh fired. Just return
  3119. * SUCCESS.
  3120. */
  3121. iocb = &lpfc_cmd->cur_iocbq;
  3122. if (lpfc_cmd->pCmd != cmnd)
  3123. goto out;
  3124. BUG_ON(iocb->context1 != lpfc_cmd);
  3125. abtsiocb = lpfc_sli_get_iocbq(phba);
  3126. if (abtsiocb == NULL) {
  3127. ret = FAILED;
  3128. goto out;
  3129. }
  3130. /*
  3131. * The scsi command can not be in txq and it is in flight because the
  3132. * pCmd is still pointig at the SCSI command we have to abort. There
  3133. * is no need to search the txcmplq. Just send an abort to the FW.
  3134. */
  3135. cmd = &iocb->iocb;
  3136. icmd = &abtsiocb->iocb;
  3137. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  3138. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  3139. if (phba->sli_rev == LPFC_SLI_REV4)
  3140. icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
  3141. else
  3142. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  3143. icmd->ulpLe = 1;
  3144. icmd->ulpClass = cmd->ulpClass;
  3145. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  3146. abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
  3147. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  3148. if (lpfc_is_link_up(phba))
  3149. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  3150. else
  3151. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  3152. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  3153. abtsiocb->vport = vport;
  3154. if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
  3155. IOCB_ERROR) {
  3156. lpfc_sli_release_iocbq(phba, abtsiocb);
  3157. ret = FAILED;
  3158. goto out;
  3159. }
  3160. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3161. lpfc_sli_handle_fast_ring_event(phba,
  3162. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3163. lpfc_cmd->waitq = &waitq;
  3164. /* Wait for abort to complete */
  3165. wait_event_timeout(waitq,
  3166. (lpfc_cmd->pCmd != cmnd),
  3167. (2*vport->cfg_devloss_tmo*HZ));
  3168. spin_lock_irq(shost->host_lock);
  3169. lpfc_cmd->waitq = NULL;
  3170. spin_unlock_irq(shost->host_lock);
  3171. if (lpfc_cmd->pCmd == cmnd) {
  3172. ret = FAILED;
  3173. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3174. "0748 abort handler timed out waiting "
  3175. "for abort to complete: ret %#x, ID %d, "
  3176. "LUN %d\n",
  3177. ret, cmnd->device->id, cmnd->device->lun);
  3178. }
  3179. out:
  3180. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3181. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  3182. "LUN %d\n", ret, cmnd->device->id,
  3183. cmnd->device->lun);
  3184. return ret;
  3185. }
  3186. static char *
  3187. lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
  3188. {
  3189. switch (task_mgmt_cmd) {
  3190. case FCP_ABORT_TASK_SET:
  3191. return "ABORT_TASK_SET";
  3192. case FCP_CLEAR_TASK_SET:
  3193. return "FCP_CLEAR_TASK_SET";
  3194. case FCP_BUS_RESET:
  3195. return "FCP_BUS_RESET";
  3196. case FCP_LUN_RESET:
  3197. return "FCP_LUN_RESET";
  3198. case FCP_TARGET_RESET:
  3199. return "FCP_TARGET_RESET";
  3200. case FCP_CLEAR_ACA:
  3201. return "FCP_CLEAR_ACA";
  3202. case FCP_TERMINATE_TASK:
  3203. return "FCP_TERMINATE_TASK";
  3204. default:
  3205. return "unknown";
  3206. }
  3207. }
  3208. /**
  3209. * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
  3210. * @vport: The virtual port for which this call is being executed.
  3211. * @rdata: Pointer to remote port local data
  3212. * @tgt_id: Target ID of remote device.
  3213. * @lun_id: Lun number for the TMF
  3214. * @task_mgmt_cmd: type of TMF to send
  3215. *
  3216. * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
  3217. * a remote port.
  3218. *
  3219. * Return Code:
  3220. * 0x2003 - Error
  3221. * 0x2002 - Success.
  3222. **/
  3223. static int
  3224. lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
  3225. unsigned tgt_id, unsigned int lun_id,
  3226. uint8_t task_mgmt_cmd)
  3227. {
  3228. struct lpfc_hba *phba = vport->phba;
  3229. struct lpfc_scsi_buf *lpfc_cmd;
  3230. struct lpfc_iocbq *iocbq;
  3231. struct lpfc_iocbq *iocbqrsp;
  3232. struct lpfc_nodelist *pnode = rdata->pnode;
  3233. int ret;
  3234. int status;
  3235. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  3236. return FAILED;
  3237. lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
  3238. if (lpfc_cmd == NULL)
  3239. return FAILED;
  3240. lpfc_cmd->timeout = 60;
  3241. lpfc_cmd->rdata = rdata;
  3242. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
  3243. task_mgmt_cmd);
  3244. if (!status) {
  3245. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3246. return FAILED;
  3247. }
  3248. iocbq = &lpfc_cmd->cur_iocbq;
  3249. iocbqrsp = lpfc_sli_get_iocbq(phba);
  3250. if (iocbqrsp == NULL) {
  3251. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3252. return FAILED;
  3253. }
  3254. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3255. "0702 Issue %s to TGT %d LUN %d "
  3256. "rpi x%x nlp_flag x%x Data: x%x x%x\n",
  3257. lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
  3258. pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
  3259. iocbq->iocb_flag);
  3260. status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
  3261. iocbq, iocbqrsp, lpfc_cmd->timeout);
  3262. if (status != IOCB_SUCCESS) {
  3263. if (status == IOCB_TIMEDOUT) {
  3264. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  3265. ret = TIMEOUT_ERROR;
  3266. } else
  3267. ret = FAILED;
  3268. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  3269. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3270. "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
  3271. "iocb_flag x%x\n",
  3272. lpfc_taskmgmt_name(task_mgmt_cmd),
  3273. tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
  3274. iocbqrsp->iocb.un.ulpWord[4],
  3275. iocbq->iocb_flag);
  3276. } else if (status == IOCB_BUSY)
  3277. ret = FAILED;
  3278. else
  3279. ret = SUCCESS;
  3280. lpfc_sli_release_iocbq(phba, iocbqrsp);
  3281. if (ret != TIMEOUT_ERROR)
  3282. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3283. return ret;
  3284. }
  3285. /**
  3286. * lpfc_chk_tgt_mapped -
  3287. * @vport: The virtual port to check on
  3288. * @cmnd: Pointer to scsi_cmnd data structure.
  3289. *
  3290. * This routine delays until the scsi target (aka rport) for the
  3291. * command exists (is present and logged in) or we declare it non-existent.
  3292. *
  3293. * Return code :
  3294. * 0x2003 - Error
  3295. * 0x2002 - Success
  3296. **/
  3297. static int
  3298. lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
  3299. {
  3300. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3301. struct lpfc_nodelist *pnode;
  3302. unsigned long later;
  3303. if (!rdata) {
  3304. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3305. "0797 Tgt Map rport failure: rdata x%p\n", rdata);
  3306. return FAILED;
  3307. }
  3308. pnode = rdata->pnode;
  3309. /*
  3310. * If target is not in a MAPPED state, delay until
  3311. * target is rediscovered or devloss timeout expires.
  3312. */
  3313. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  3314. while (time_after(later, jiffies)) {
  3315. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  3316. return FAILED;
  3317. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  3318. return SUCCESS;
  3319. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  3320. rdata = cmnd->device->hostdata;
  3321. if (!rdata)
  3322. return FAILED;
  3323. pnode = rdata->pnode;
  3324. }
  3325. if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
  3326. (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  3327. return FAILED;
  3328. return SUCCESS;
  3329. }
  3330. /**
  3331. * lpfc_reset_flush_io_context -
  3332. * @vport: The virtual port (scsi_host) for the flush context
  3333. * @tgt_id: If aborting by Target contect - specifies the target id
  3334. * @lun_id: If aborting by Lun context - specifies the lun id
  3335. * @context: specifies the context level to flush at.
  3336. *
  3337. * After a reset condition via TMF, we need to flush orphaned i/o
  3338. * contexts from the adapter. This routine aborts any contexts
  3339. * outstanding, then waits for their completions. The wait is
  3340. * bounded by devloss_tmo though.
  3341. *
  3342. * Return code :
  3343. * 0x2003 - Error
  3344. * 0x2002 - Success
  3345. **/
  3346. static int
  3347. lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
  3348. uint64_t lun_id, lpfc_ctx_cmd context)
  3349. {
  3350. struct lpfc_hba *phba = vport->phba;
  3351. unsigned long later;
  3352. int cnt;
  3353. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  3354. if (cnt)
  3355. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  3356. tgt_id, lun_id, context);
  3357. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  3358. while (time_after(later, jiffies) && cnt) {
  3359. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  3360. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  3361. }
  3362. if (cnt) {
  3363. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3364. "0724 I/O flush failure for context %s : cnt x%x\n",
  3365. ((context == LPFC_CTX_LUN) ? "LUN" :
  3366. ((context == LPFC_CTX_TGT) ? "TGT" :
  3367. ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
  3368. cnt);
  3369. return FAILED;
  3370. }
  3371. return SUCCESS;
  3372. }
  3373. /**
  3374. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  3375. * @cmnd: Pointer to scsi_cmnd data structure.
  3376. *
  3377. * This routine does a device reset by sending a LUN_RESET task management
  3378. * command.
  3379. *
  3380. * Return code :
  3381. * 0x2003 - Error
  3382. * 0x2002 - Success
  3383. **/
  3384. static int
  3385. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  3386. {
  3387. struct Scsi_Host *shost = cmnd->device->host;
  3388. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3389. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3390. struct lpfc_nodelist *pnode;
  3391. unsigned tgt_id = cmnd->device->id;
  3392. unsigned int lun_id = cmnd->device->lun;
  3393. struct lpfc_scsi_event_header scsi_event;
  3394. int status;
  3395. if (!rdata) {
  3396. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3397. "0798 Device Reset rport failure: rdata x%p\n", rdata);
  3398. return FAILED;
  3399. }
  3400. pnode = rdata->pnode;
  3401. status = fc_block_scsi_eh(cmnd);
  3402. if (status)
  3403. return status;
  3404. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3405. if (status == FAILED) {
  3406. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3407. "0721 Device Reset rport failure: rdata x%p\n", rdata);
  3408. return FAILED;
  3409. }
  3410. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3411. scsi_event.subcategory = LPFC_EVENT_LUNRESET;
  3412. scsi_event.lun = lun_id;
  3413. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3414. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3415. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3416. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3417. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3418. FCP_LUN_RESET);
  3419. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3420. "0713 SCSI layer issued Device Reset (%d, %d) "
  3421. "return x%x\n", tgt_id, lun_id, status);
  3422. /*
  3423. * We have to clean up i/o as : they may be orphaned by the TMF;
  3424. * or if the TMF failed, they may be in an indeterminate state.
  3425. * So, continue on.
  3426. * We will report success if all the i/o aborts successfully.
  3427. */
  3428. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3429. LPFC_CTX_LUN);
  3430. return status;
  3431. }
  3432. /**
  3433. * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
  3434. * @cmnd: Pointer to scsi_cmnd data structure.
  3435. *
  3436. * This routine does a target reset by sending a TARGET_RESET task management
  3437. * command.
  3438. *
  3439. * Return code :
  3440. * 0x2003 - Error
  3441. * 0x2002 - Success
  3442. **/
  3443. static int
  3444. lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
  3445. {
  3446. struct Scsi_Host *shost = cmnd->device->host;
  3447. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3448. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3449. struct lpfc_nodelist *pnode;
  3450. unsigned tgt_id = cmnd->device->id;
  3451. unsigned int lun_id = cmnd->device->lun;
  3452. struct lpfc_scsi_event_header scsi_event;
  3453. int status;
  3454. if (!rdata) {
  3455. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3456. "0799 Target Reset rport failure: rdata x%p\n", rdata);
  3457. return FAILED;
  3458. }
  3459. pnode = rdata->pnode;
  3460. status = fc_block_scsi_eh(cmnd);
  3461. if (status)
  3462. return status;
  3463. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3464. if (status == FAILED) {
  3465. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3466. "0722 Target Reset rport failure: rdata x%p\n", rdata);
  3467. return FAILED;
  3468. }
  3469. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3470. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  3471. scsi_event.lun = 0;
  3472. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3473. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3474. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3475. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3476. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3477. FCP_TARGET_RESET);
  3478. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3479. "0723 SCSI layer issued Target Reset (%d, %d) "
  3480. "return x%x\n", tgt_id, lun_id, status);
  3481. /*
  3482. * We have to clean up i/o as : they may be orphaned by the TMF;
  3483. * or if the TMF failed, they may be in an indeterminate state.
  3484. * So, continue on.
  3485. * We will report success if all the i/o aborts successfully.
  3486. */
  3487. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3488. LPFC_CTX_TGT);
  3489. return status;
  3490. }
  3491. /**
  3492. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  3493. * @cmnd: Pointer to scsi_cmnd data structure.
  3494. *
  3495. * This routine does target reset to all targets on @cmnd->device->host.
  3496. * This emulates Parallel SCSI Bus Reset Semantics.
  3497. *
  3498. * Return code :
  3499. * 0x2003 - Error
  3500. * 0x2002 - Success
  3501. **/
  3502. static int
  3503. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  3504. {
  3505. struct Scsi_Host *shost = cmnd->device->host;
  3506. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3507. struct lpfc_nodelist *ndlp = NULL;
  3508. struct lpfc_scsi_event_header scsi_event;
  3509. int match;
  3510. int ret = SUCCESS, status, i;
  3511. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3512. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  3513. scsi_event.lun = 0;
  3514. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  3515. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  3516. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3517. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3518. status = fc_block_scsi_eh(cmnd);
  3519. if (status)
  3520. return status;
  3521. /*
  3522. * Since the driver manages a single bus device, reset all
  3523. * targets known to the driver. Should any target reset
  3524. * fail, this routine returns failure to the midlayer.
  3525. */
  3526. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  3527. /* Search for mapped node by target ID */
  3528. match = 0;
  3529. spin_lock_irq(shost->host_lock);
  3530. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  3531. if (!NLP_CHK_NODE_ACT(ndlp))
  3532. continue;
  3533. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  3534. ndlp->nlp_sid == i &&
  3535. ndlp->rport) {
  3536. match = 1;
  3537. break;
  3538. }
  3539. }
  3540. spin_unlock_irq(shost->host_lock);
  3541. if (!match)
  3542. continue;
  3543. status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
  3544. i, 0, FCP_TARGET_RESET);
  3545. if (status != SUCCESS) {
  3546. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3547. "0700 Bus Reset on target %d failed\n",
  3548. i);
  3549. ret = FAILED;
  3550. }
  3551. }
  3552. /*
  3553. * We have to clean up i/o as : they may be orphaned by the TMFs
  3554. * above; or if any of the TMFs failed, they may be in an
  3555. * indeterminate state.
  3556. * We will report success if all the i/o aborts successfully.
  3557. */
  3558. status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
  3559. if (status != SUCCESS)
  3560. ret = FAILED;
  3561. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3562. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  3563. return ret;
  3564. }
  3565. /**
  3566. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  3567. * @sdev: Pointer to scsi_device.
  3568. *
  3569. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  3570. * globally available list of scsi buffers. This routine also makes sure scsi
  3571. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  3572. * of scsi buffer exists for the lifetime of the driver.
  3573. *
  3574. * Return codes:
  3575. * non-0 - Error
  3576. * 0 - Success
  3577. **/
  3578. static int
  3579. lpfc_slave_alloc(struct scsi_device *sdev)
  3580. {
  3581. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3582. struct lpfc_hba *phba = vport->phba;
  3583. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  3584. uint32_t total = 0;
  3585. uint32_t num_to_alloc = 0;
  3586. int num_allocated = 0;
  3587. uint32_t sdev_cnt;
  3588. if (!rport || fc_remote_port_chkready(rport))
  3589. return -ENXIO;
  3590. sdev->hostdata = rport->dd_data;
  3591. sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
  3592. /*
  3593. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  3594. * available list of scsi buffers. Don't allocate more than the
  3595. * HBA limit conveyed to the midlayer via the host structure. The
  3596. * formula accounts for the lun_queue_depth + error handlers + 1
  3597. * extra. This list of scsi bufs exists for the lifetime of the driver.
  3598. */
  3599. total = phba->total_scsi_bufs;
  3600. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  3601. /* If allocated buffers are enough do nothing */
  3602. if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
  3603. return 0;
  3604. /* Allow some exchanges to be available always to complete discovery */
  3605. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3606. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3607. "0704 At limitation of %d preallocated "
  3608. "command buffers\n", total);
  3609. return 0;
  3610. /* Allow some exchanges to be available always to complete discovery */
  3611. } else if (total + num_to_alloc >
  3612. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3613. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3614. "0705 Allocation request of %d "
  3615. "command buffers will exceed max of %d. "
  3616. "Reducing allocation request to %d.\n",
  3617. num_to_alloc, phba->cfg_hba_queue_depth,
  3618. (phba->cfg_hba_queue_depth - total));
  3619. num_to_alloc = phba->cfg_hba_queue_depth - total;
  3620. }
  3621. num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
  3622. if (num_to_alloc != num_allocated) {
  3623. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3624. "0708 Allocation request of %d "
  3625. "command buffers did not succeed. "
  3626. "Allocated %d buffers.\n",
  3627. num_to_alloc, num_allocated);
  3628. }
  3629. if (num_allocated > 0)
  3630. phba->total_scsi_bufs += num_allocated;
  3631. return 0;
  3632. }
  3633. /**
  3634. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  3635. * @sdev: Pointer to scsi_device.
  3636. *
  3637. * This routine configures following items
  3638. * - Tag command queuing support for @sdev if supported.
  3639. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  3640. *
  3641. * Return codes:
  3642. * 0 - Success
  3643. **/
  3644. static int
  3645. lpfc_slave_configure(struct scsi_device *sdev)
  3646. {
  3647. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3648. struct lpfc_hba *phba = vport->phba;
  3649. if (sdev->tagged_supported)
  3650. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  3651. else
  3652. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  3653. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  3654. lpfc_sli_handle_fast_ring_event(phba,
  3655. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3656. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3657. lpfc_poll_rearm_timer(phba);
  3658. }
  3659. return 0;
  3660. }
  3661. /**
  3662. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  3663. * @sdev: Pointer to scsi_device.
  3664. *
  3665. * This routine sets @sdev hostatdata filed to null.
  3666. **/
  3667. static void
  3668. lpfc_slave_destroy(struct scsi_device *sdev)
  3669. {
  3670. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3671. struct lpfc_hba *phba = vport->phba;
  3672. atomic_dec(&phba->sdev_cnt);
  3673. sdev->hostdata = NULL;
  3674. return;
  3675. }
  3676. struct scsi_host_template lpfc_template = {
  3677. .module = THIS_MODULE,
  3678. .name = LPFC_DRIVER_NAME,
  3679. .info = lpfc_info,
  3680. .queuecommand = lpfc_queuecommand,
  3681. .eh_abort_handler = lpfc_abort_handler,
  3682. .eh_device_reset_handler = lpfc_device_reset_handler,
  3683. .eh_target_reset_handler = lpfc_target_reset_handler,
  3684. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3685. .slave_alloc = lpfc_slave_alloc,
  3686. .slave_configure = lpfc_slave_configure,
  3687. .slave_destroy = lpfc_slave_destroy,
  3688. .scan_finished = lpfc_scan_finished,
  3689. .this_id = -1,
  3690. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3691. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3692. .use_clustering = ENABLE_CLUSTERING,
  3693. .shost_attrs = lpfc_hba_attrs,
  3694. .max_sectors = 0xFFFF,
  3695. .vendor_id = LPFC_NL_VENDOR_ID,
  3696. .change_queue_depth = lpfc_change_queue_depth,
  3697. };
  3698. struct scsi_host_template lpfc_vport_template = {
  3699. .module = THIS_MODULE,
  3700. .name = LPFC_DRIVER_NAME,
  3701. .info = lpfc_info,
  3702. .queuecommand = lpfc_queuecommand,
  3703. .eh_abort_handler = lpfc_abort_handler,
  3704. .eh_device_reset_handler = lpfc_device_reset_handler,
  3705. .eh_target_reset_handler = lpfc_target_reset_handler,
  3706. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3707. .slave_alloc = lpfc_slave_alloc,
  3708. .slave_configure = lpfc_slave_configure,
  3709. .slave_destroy = lpfc_slave_destroy,
  3710. .scan_finished = lpfc_scan_finished,
  3711. .this_id = -1,
  3712. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3713. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3714. .use_clustering = ENABLE_CLUSTERING,
  3715. .shost_attrs = lpfc_vport_attrs,
  3716. .max_sectors = 0xFFFF,
  3717. .change_queue_depth = lpfc_change_queue_depth,
  3718. };