lpfc_scsi.c 118 KB

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