lpfc_scsi.c 116 KB

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