lpfc_scsi.c 113 KB

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