lpfc_scsi.c 111 KB

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