lpfc_scsi.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <asm/unaligned.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_eh.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include "lpfc_version.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_nl.h"
  35. #include "lpfc_disc.h"
  36. #include "lpfc_scsi.h"
  37. #include "lpfc.h"
  38. #include "lpfc_logmsg.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_vport.h"
  41. #define LPFC_RESET_WAIT 2
  42. #define LPFC_ABORT_WAIT 2
  43. int _dump_buf_done;
  44. static char *dif_op_str[] = {
  45. "SCSI_PROT_NORMAL",
  46. "SCSI_PROT_READ_INSERT",
  47. "SCSI_PROT_WRITE_STRIP",
  48. "SCSI_PROT_READ_STRIP",
  49. "SCSI_PROT_WRITE_INSERT",
  50. "SCSI_PROT_READ_PASS",
  51. "SCSI_PROT_WRITE_PASS",
  52. "SCSI_PROT_READ_CONVERT",
  53. "SCSI_PROT_WRITE_CONVERT"
  54. };
  55. static void
  56. lpfc_debug_save_data(struct scsi_cmnd *cmnd)
  57. {
  58. void *src, *dst;
  59. struct scatterlist *sgde = scsi_sglist(cmnd);
  60. if (!_dump_buf_data) {
  61. printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
  62. __func__);
  63. return;
  64. }
  65. if (!sgde) {
  66. printk(KERN_ERR "BLKGRD ERROR: data scatterlist is null\n");
  67. return;
  68. }
  69. dst = (void *) _dump_buf_data;
  70. while (sgde) {
  71. src = sg_virt(sgde);
  72. memcpy(dst, src, sgde->length);
  73. dst += sgde->length;
  74. sgde = sg_next(sgde);
  75. }
  76. }
  77. static void
  78. lpfc_debug_save_dif(struct scsi_cmnd *cmnd)
  79. {
  80. void *src, *dst;
  81. struct scatterlist *sgde = scsi_prot_sglist(cmnd);
  82. if (!_dump_buf_dif) {
  83. printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
  84. __func__);
  85. return;
  86. }
  87. if (!sgde) {
  88. printk(KERN_ERR "BLKGRD ERROR: prot scatterlist is null\n");
  89. return;
  90. }
  91. dst = _dump_buf_dif;
  92. while (sgde) {
  93. src = sg_virt(sgde);
  94. memcpy(dst, src, sgde->length);
  95. dst += sgde->length;
  96. sgde = sg_next(sgde);
  97. }
  98. }
  99. /**
  100. * lpfc_update_stats - Update statistical data for the command completion
  101. * @phba: Pointer to HBA object.
  102. * @lpfc_cmd: lpfc scsi command object pointer.
  103. *
  104. * This function is called when there is a command completion and this
  105. * function updates the statistical data for the command completion.
  106. **/
  107. static void
  108. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  109. {
  110. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  111. struct lpfc_nodelist *pnode = rdata->pnode;
  112. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  113. unsigned long flags;
  114. struct Scsi_Host *shost = cmd->device->host;
  115. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  116. unsigned long latency;
  117. int i;
  118. if (cmd->result)
  119. return;
  120. latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
  121. spin_lock_irqsave(shost->host_lock, flags);
  122. if (!vport->stat_data_enabled ||
  123. vport->stat_data_blocked ||
  124. !pnode->lat_data ||
  125. (phba->bucket_type == LPFC_NO_BUCKET)) {
  126. spin_unlock_irqrestore(shost->host_lock, flags);
  127. return;
  128. }
  129. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  130. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  131. phba->bucket_step;
  132. /* check array subscript bounds */
  133. if (i < 0)
  134. i = 0;
  135. else if (i >= LPFC_MAX_BUCKET_COUNT)
  136. i = LPFC_MAX_BUCKET_COUNT - 1;
  137. } else {
  138. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  139. if (latency <= (phba->bucket_base +
  140. ((1<<i)*phba->bucket_step)))
  141. break;
  142. }
  143. pnode->lat_data[i].cmd_count++;
  144. spin_unlock_irqrestore(shost->host_lock, flags);
  145. }
  146. /**
  147. * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
  148. * @phba: Pointer to HBA context object.
  149. * @vport: Pointer to vport object.
  150. * @ndlp: Pointer to FC node associated with the target.
  151. * @lun: Lun number of the scsi device.
  152. * @old_val: Old value of the queue depth.
  153. * @new_val: New value of the queue depth.
  154. *
  155. * This function sends an event to the mgmt application indicating
  156. * there is a change in the scsi device queue depth.
  157. **/
  158. static void
  159. lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
  160. struct lpfc_vport *vport,
  161. struct lpfc_nodelist *ndlp,
  162. uint32_t lun,
  163. uint32_t old_val,
  164. uint32_t new_val)
  165. {
  166. struct lpfc_fast_path_event *fast_path_evt;
  167. unsigned long flags;
  168. fast_path_evt = lpfc_alloc_fast_evt(phba);
  169. if (!fast_path_evt)
  170. return;
  171. fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
  172. FC_REG_SCSI_EVENT;
  173. fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
  174. LPFC_EVENT_VARQUEDEPTH;
  175. /* Report all luns with change in queue depth */
  176. fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
  177. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  178. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
  179. &ndlp->nlp_portname, sizeof(struct lpfc_name));
  180. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
  181. &ndlp->nlp_nodename, sizeof(struct lpfc_name));
  182. }
  183. fast_path_evt->un.queue_depth_evt.oldval = old_val;
  184. fast_path_evt->un.queue_depth_evt.newval = new_val;
  185. fast_path_evt->vport = vport;
  186. fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  187. spin_lock_irqsave(&phba->hbalock, flags);
  188. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  189. spin_unlock_irqrestore(&phba->hbalock, flags);
  190. lpfc_worker_wake_up(phba);
  191. return;
  192. }
  193. /**
  194. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  195. * @phba: The Hba for which this call is being executed.
  196. *
  197. * This routine is called when there is resource error in driver or firmware.
  198. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  199. * posts at most 1 event each second. This routine wakes up worker thread of
  200. * @phba to process WORKER_RAM_DOWN_EVENT event.
  201. *
  202. * This routine should be called with no lock held.
  203. **/
  204. void
  205. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  206. {
  207. unsigned long flags;
  208. uint32_t evt_posted;
  209. spin_lock_irqsave(&phba->hbalock, flags);
  210. atomic_inc(&phba->num_rsrc_err);
  211. phba->last_rsrc_error_time = jiffies;
  212. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  213. spin_unlock_irqrestore(&phba->hbalock, flags);
  214. return;
  215. }
  216. phba->last_ramp_down_time = jiffies;
  217. spin_unlock_irqrestore(&phba->hbalock, flags);
  218. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  219. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  220. if (!evt_posted)
  221. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  222. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  223. if (!evt_posted)
  224. lpfc_worker_wake_up(phba);
  225. return;
  226. }
  227. /**
  228. * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
  229. * @phba: The Hba for which this call is being executed.
  230. *
  231. * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
  232. * post at most 1 event every 5 minute after last_ramp_up_time or
  233. * last_rsrc_error_time. This routine wakes up worker thread of @phba
  234. * to process WORKER_RAM_DOWN_EVENT event.
  235. *
  236. * This routine should be called with no lock held.
  237. **/
  238. static inline void
  239. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  240. uint32_t queue_depth)
  241. {
  242. unsigned long flags;
  243. struct lpfc_hba *phba = vport->phba;
  244. uint32_t evt_posted;
  245. atomic_inc(&phba->num_cmd_success);
  246. if (vport->cfg_lun_queue_depth <= queue_depth)
  247. return;
  248. spin_lock_irqsave(&phba->hbalock, flags);
  249. if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
  250. ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
  251. spin_unlock_irqrestore(&phba->hbalock, flags);
  252. return;
  253. }
  254. phba->last_ramp_up_time = jiffies;
  255. spin_unlock_irqrestore(&phba->hbalock, flags);
  256. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  257. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  258. if (!evt_posted)
  259. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  260. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  261. if (!evt_posted)
  262. lpfc_worker_wake_up(phba);
  263. return;
  264. }
  265. /**
  266. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  267. * @phba: The Hba for which this call is being executed.
  268. *
  269. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  270. * thread.This routine reduces queue depth for all scsi device on each vport
  271. * associated with @phba.
  272. **/
  273. void
  274. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  275. {
  276. struct lpfc_vport **vports;
  277. struct Scsi_Host *shost;
  278. struct scsi_device *sdev;
  279. unsigned long new_queue_depth, old_queue_depth;
  280. unsigned long num_rsrc_err, num_cmd_success;
  281. int i;
  282. struct lpfc_rport_data *rdata;
  283. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  284. num_cmd_success = atomic_read(&phba->num_cmd_success);
  285. vports = lpfc_create_vport_work_array(phba);
  286. if (vports != NULL)
  287. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  288. shost = lpfc_shost_from_vport(vports[i]);
  289. shost_for_each_device(sdev, shost) {
  290. new_queue_depth =
  291. sdev->queue_depth * num_rsrc_err /
  292. (num_rsrc_err + num_cmd_success);
  293. if (!new_queue_depth)
  294. new_queue_depth = sdev->queue_depth - 1;
  295. else
  296. new_queue_depth = sdev->queue_depth -
  297. new_queue_depth;
  298. old_queue_depth = sdev->queue_depth;
  299. if (sdev->ordered_tags)
  300. scsi_adjust_queue_depth(sdev,
  301. MSG_ORDERED_TAG,
  302. new_queue_depth);
  303. else
  304. scsi_adjust_queue_depth(sdev,
  305. MSG_SIMPLE_TAG,
  306. new_queue_depth);
  307. rdata = sdev->hostdata;
  308. if (rdata)
  309. lpfc_send_sdev_queuedepth_change_event(
  310. phba, vports[i],
  311. rdata->pnode,
  312. sdev->lun, old_queue_depth,
  313. new_queue_depth);
  314. }
  315. }
  316. lpfc_destroy_vport_work_array(phba, vports);
  317. atomic_set(&phba->num_rsrc_err, 0);
  318. atomic_set(&phba->num_cmd_success, 0);
  319. }
  320. /**
  321. * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
  322. * @phba: The Hba for which this call is being executed.
  323. *
  324. * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
  325. * thread.This routine increases queue depth for all scsi device on each vport
  326. * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
  327. * num_cmd_success to zero.
  328. **/
  329. void
  330. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  331. {
  332. struct lpfc_vport **vports;
  333. struct Scsi_Host *shost;
  334. struct scsi_device *sdev;
  335. int i;
  336. struct lpfc_rport_data *rdata;
  337. vports = lpfc_create_vport_work_array(phba);
  338. if (vports != NULL)
  339. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  340. shost = lpfc_shost_from_vport(vports[i]);
  341. shost_for_each_device(sdev, shost) {
  342. if (vports[i]->cfg_lun_queue_depth <=
  343. sdev->queue_depth)
  344. continue;
  345. if (sdev->ordered_tags)
  346. scsi_adjust_queue_depth(sdev,
  347. MSG_ORDERED_TAG,
  348. sdev->queue_depth+1);
  349. else
  350. scsi_adjust_queue_depth(sdev,
  351. MSG_SIMPLE_TAG,
  352. sdev->queue_depth+1);
  353. rdata = sdev->hostdata;
  354. if (rdata)
  355. lpfc_send_sdev_queuedepth_change_event(
  356. phba, vports[i],
  357. rdata->pnode,
  358. sdev->lun,
  359. sdev->queue_depth - 1,
  360. sdev->queue_depth);
  361. }
  362. }
  363. lpfc_destroy_vport_work_array(phba, vports);
  364. atomic_set(&phba->num_rsrc_err, 0);
  365. atomic_set(&phba->num_cmd_success, 0);
  366. }
  367. /**
  368. * lpfc_scsi_dev_block - set all scsi hosts to block state
  369. * @phba: Pointer to HBA context object.
  370. *
  371. * This function walks vport list and set each SCSI host to block state
  372. * by invoking fc_remote_port_delete() routine. This function is invoked
  373. * with EEH when device's PCI slot has been permanently disabled.
  374. **/
  375. void
  376. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  377. {
  378. struct lpfc_vport **vports;
  379. struct Scsi_Host *shost;
  380. struct scsi_device *sdev;
  381. struct fc_rport *rport;
  382. int i;
  383. vports = lpfc_create_vport_work_array(phba);
  384. if (vports != NULL)
  385. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  386. shost = lpfc_shost_from_vport(vports[i]);
  387. shost_for_each_device(sdev, shost) {
  388. rport = starget_to_rport(scsi_target(sdev));
  389. fc_remote_port_delete(rport);
  390. }
  391. }
  392. lpfc_destroy_vport_work_array(phba, vports);
  393. }
  394. /**
  395. * lpfc_new_scsi_buf - Scsi buffer allocator
  396. * @vport: The virtual port for which this call being executed.
  397. *
  398. * This routine allocates a scsi buffer, which contains all the necessary
  399. * information needed to initiate a SCSI I/O. The non-DMAable buffer region
  400. * contains information to build the IOCB. The DMAable region contains
  401. * memory for the FCP CMND, FCP RSP, and the initial BPL. In addition to
  402. * allocating memory, the FCP CMND and FCP RSP BDEs are setup in the BPL
  403. * and the BPL BDE is setup in the IOCB.
  404. *
  405. * Return codes:
  406. * NULL - Error
  407. * Pointer to lpfc_scsi_buf data structure - Success
  408. **/
  409. static struct lpfc_scsi_buf *
  410. lpfc_new_scsi_buf(struct lpfc_vport *vport)
  411. {
  412. struct lpfc_hba *phba = vport->phba;
  413. struct lpfc_scsi_buf *psb;
  414. struct ulp_bde64 *bpl;
  415. IOCB_t *iocb;
  416. dma_addr_t pdma_phys_fcp_cmd;
  417. dma_addr_t pdma_phys_fcp_rsp;
  418. dma_addr_t pdma_phys_bpl;
  419. uint16_t iotag;
  420. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  421. if (!psb)
  422. return NULL;
  423. /*
  424. * Get memory from the pci pool to map the virt space to pci bus space
  425. * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
  426. * struct fcp_rsp and the number of bde's necessary to support the
  427. * sg_tablesize.
  428. */
  429. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
  430. &psb->dma_handle);
  431. if (!psb->data) {
  432. kfree(psb);
  433. return NULL;
  434. }
  435. /* Initialize virtual ptrs to dma_buf region. */
  436. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  437. /* Allocate iotag for psb->cur_iocbq. */
  438. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  439. if (iotag == 0) {
  440. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  441. psb->data, psb->dma_handle);
  442. kfree (psb);
  443. return NULL;
  444. }
  445. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  446. psb->fcp_cmnd = psb->data;
  447. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  448. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  449. sizeof(struct fcp_rsp);
  450. /* Initialize local short-hand pointers. */
  451. bpl = psb->fcp_bpl;
  452. pdma_phys_fcp_cmd = psb->dma_handle;
  453. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  454. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  455. sizeof(struct fcp_rsp);
  456. /*
  457. * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
  458. * list bdes. Initialize the first two and leave the rest for
  459. * queuecommand.
  460. */
  461. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  462. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  463. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  464. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  465. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  466. /* Setup the physical region for the FCP RSP */
  467. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  468. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  469. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  470. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  471. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  472. /*
  473. * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
  474. * initialize it with all known data now.
  475. */
  476. iocb = &psb->cur_iocbq.iocb;
  477. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  478. if ((phba->sli_rev == 3) &&
  479. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  480. /* fill in immediate fcp command BDE */
  481. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  482. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  483. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  484. unsli3.fcp_ext.icd);
  485. iocb->un.fcpi64.bdl.addrHigh = 0;
  486. iocb->ulpBdeCount = 0;
  487. iocb->ulpLe = 0;
  488. /* fill in responce BDE */
  489. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  490. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  491. sizeof(struct fcp_rsp);
  492. iocb->unsli3.fcp_ext.rbde.addrLow =
  493. putPaddrLow(pdma_phys_fcp_rsp);
  494. iocb->unsli3.fcp_ext.rbde.addrHigh =
  495. putPaddrHigh(pdma_phys_fcp_rsp);
  496. } else {
  497. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  498. iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  499. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
  500. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
  501. iocb->ulpBdeCount = 1;
  502. iocb->ulpLe = 1;
  503. }
  504. iocb->ulpClass = CLASS3;
  505. return psb;
  506. }
  507. /**
  508. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list list of Hba
  509. * @phba: The Hba for which this call is being executed.
  510. *
  511. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  512. * and returns to caller.
  513. *
  514. * Return codes:
  515. * NULL - Error
  516. * Pointer to lpfc_scsi_buf - Success
  517. **/
  518. static struct lpfc_scsi_buf*
  519. lpfc_get_scsi_buf(struct lpfc_hba * phba)
  520. {
  521. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  522. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  523. unsigned long iflag = 0;
  524. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  525. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  526. if (lpfc_cmd) {
  527. lpfc_cmd->seg_cnt = 0;
  528. lpfc_cmd->nonsg_phys = 0;
  529. lpfc_cmd->prot_seg_cnt = 0;
  530. }
  531. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  532. return lpfc_cmd;
  533. }
  534. /**
  535. * lpfc_release_scsi_buf - Return a scsi buffer back to hba's lpfc_scsi_buf_list
  536. * @phba: The Hba for which this call is being executed.
  537. * @psb: The scsi buffer which is being released.
  538. *
  539. * This routine releases @psb scsi buffer by adding it to tail of @phba
  540. * lpfc_scsi_buf_list list.
  541. **/
  542. static void
  543. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  544. {
  545. unsigned long iflag = 0;
  546. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  547. psb->pCmd = NULL;
  548. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  549. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  550. }
  551. /**
  552. * lpfc_scsi_prep_dma_buf - Routine to do DMA mapping for scsi buffer
  553. * @phba: The Hba for which this call is being executed.
  554. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  555. *
  556. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  557. * field of @lpfc_cmd. This routine scans through sg elements and format the
  558. * bdea. This routine also initializes all IOCB fields which are dependent on
  559. * scsi command request buffer.
  560. *
  561. * Return codes:
  562. * 1 - Error
  563. * 0 - Success
  564. **/
  565. static int
  566. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  567. {
  568. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  569. struct scatterlist *sgel = NULL;
  570. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  571. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  572. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  573. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  574. dma_addr_t physaddr;
  575. uint32_t num_bde = 0;
  576. int nseg, datadir = scsi_cmnd->sc_data_direction;
  577. /*
  578. * There are three possibilities here - use scatter-gather segment, use
  579. * the single mapping, or neither. Start the lpfc command prep by
  580. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  581. * data bde entry.
  582. */
  583. bpl += 2;
  584. if (scsi_sg_count(scsi_cmnd)) {
  585. /*
  586. * The driver stores the segment count returned from pci_map_sg
  587. * because this a count of dma-mappings used to map the use_sg
  588. * pages. They are not guaranteed to be the same for those
  589. * architectures that implement an IOMMU.
  590. */
  591. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  592. scsi_sg_count(scsi_cmnd), datadir);
  593. if (unlikely(!nseg))
  594. return 1;
  595. lpfc_cmd->seg_cnt = nseg;
  596. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  597. printk(KERN_ERR "%s: Too many sg segments from "
  598. "dma_map_sg. Config %d, seg_cnt %d\n",
  599. __func__, phba->cfg_sg_seg_cnt,
  600. lpfc_cmd->seg_cnt);
  601. scsi_dma_unmap(scsi_cmnd);
  602. return 1;
  603. }
  604. /*
  605. * The driver established a maximum scatter-gather segment count
  606. * during probe that limits the number of sg elements in any
  607. * single scsi command. Just run through the seg_cnt and format
  608. * the bde's.
  609. * When using SLI-3 the driver will try to fit all the BDEs into
  610. * the IOCB. If it can't then the BDEs get added to a BPL as it
  611. * does for SLI-2 mode.
  612. */
  613. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  614. physaddr = sg_dma_address(sgel);
  615. if (phba->sli_rev == 3 &&
  616. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  617. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  618. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  619. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  620. data_bde->addrLow = putPaddrLow(physaddr);
  621. data_bde->addrHigh = putPaddrHigh(physaddr);
  622. data_bde++;
  623. } else {
  624. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  625. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  626. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  627. bpl->addrLow =
  628. le32_to_cpu(putPaddrLow(physaddr));
  629. bpl->addrHigh =
  630. le32_to_cpu(putPaddrHigh(physaddr));
  631. bpl++;
  632. }
  633. }
  634. }
  635. /*
  636. * Finish initializing those IOCB fields that are dependent on the
  637. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  638. * explicitly reinitialized and for SLI-3 the extended bde count is
  639. * explicitly reinitialized since all iocb memory resources are reused.
  640. */
  641. if (phba->sli_rev == 3 &&
  642. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  643. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  644. /*
  645. * The extended IOCB format can only fit 3 BDE or a BPL.
  646. * This I/O has more than 3 BDE so the 1st data bde will
  647. * be a BPL that is filled in here.
  648. */
  649. physaddr = lpfc_cmd->dma_handle;
  650. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  651. data_bde->tus.f.bdeSize = (num_bde *
  652. sizeof(struct ulp_bde64));
  653. physaddr += (sizeof(struct fcp_cmnd) +
  654. sizeof(struct fcp_rsp) +
  655. (2 * sizeof(struct ulp_bde64)));
  656. data_bde->addrHigh = putPaddrHigh(physaddr);
  657. data_bde->addrLow = putPaddrLow(physaddr);
  658. /* ebde count includes the responce bde and data bpl */
  659. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  660. } else {
  661. /* ebde count includes the responce bde and data bdes */
  662. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  663. }
  664. } else {
  665. iocb_cmd->un.fcpi64.bdl.bdeSize =
  666. ((num_bde + 2) * sizeof(struct ulp_bde64));
  667. }
  668. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  669. /*
  670. * Due to difference in data length between DIF/non-DIF paths,
  671. * we need to set word 4 of IOCB here
  672. */
  673. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  674. return 0;
  675. }
  676. /*
  677. * Given a scsi cmnd, determine the BlockGuard profile to be used
  678. * with the cmd
  679. */
  680. static int
  681. lpfc_sc_to_sli_prof(struct scsi_cmnd *sc)
  682. {
  683. uint8_t guard_type = scsi_host_get_guard(sc->device->host);
  684. uint8_t ret_prof = LPFC_PROF_INVALID;
  685. if (guard_type == SHOST_DIX_GUARD_IP) {
  686. switch (scsi_get_prot_op(sc)) {
  687. case SCSI_PROT_READ_INSERT:
  688. case SCSI_PROT_WRITE_STRIP:
  689. ret_prof = LPFC_PROF_AST2;
  690. break;
  691. case SCSI_PROT_READ_STRIP:
  692. case SCSI_PROT_WRITE_INSERT:
  693. ret_prof = LPFC_PROF_A1;
  694. break;
  695. case SCSI_PROT_READ_CONVERT:
  696. case SCSI_PROT_WRITE_CONVERT:
  697. ret_prof = LPFC_PROF_AST1;
  698. break;
  699. case SCSI_PROT_READ_PASS:
  700. case SCSI_PROT_WRITE_PASS:
  701. case SCSI_PROT_NORMAL:
  702. default:
  703. printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
  704. scsi_get_prot_op(sc), guard_type);
  705. break;
  706. }
  707. } else if (guard_type == SHOST_DIX_GUARD_CRC) {
  708. switch (scsi_get_prot_op(sc)) {
  709. case SCSI_PROT_READ_STRIP:
  710. case SCSI_PROT_WRITE_INSERT:
  711. ret_prof = LPFC_PROF_A1;
  712. break;
  713. case SCSI_PROT_READ_PASS:
  714. case SCSI_PROT_WRITE_PASS:
  715. ret_prof = LPFC_PROF_C1;
  716. break;
  717. case SCSI_PROT_READ_CONVERT:
  718. case SCSI_PROT_WRITE_CONVERT:
  719. case SCSI_PROT_READ_INSERT:
  720. case SCSI_PROT_WRITE_STRIP:
  721. case SCSI_PROT_NORMAL:
  722. default:
  723. printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
  724. scsi_get_prot_op(sc), guard_type);
  725. break;
  726. }
  727. } else {
  728. /* unsupported format */
  729. BUG();
  730. }
  731. return ret_prof;
  732. }
  733. struct scsi_dif_tuple {
  734. __be16 guard_tag; /* Checksum */
  735. __be16 app_tag; /* Opaque storage */
  736. __be32 ref_tag; /* Target LBA or indirect LBA */
  737. };
  738. static inline unsigned
  739. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  740. {
  741. return sc->device->sector_size;
  742. }
  743. /**
  744. * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
  745. * @sc: in: SCSI command
  746. * @apptagmask: out: app tag mask
  747. * @apptagval: out: app tag value
  748. * @reftag: out: ref tag (reference tag)
  749. *
  750. * Description:
  751. * Extract DIF paramters from the command if possible. Otherwise,
  752. * use default paratmers.
  753. *
  754. **/
  755. static inline void
  756. lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
  757. uint16_t *apptagval, uint32_t *reftag)
  758. {
  759. struct scsi_dif_tuple *spt;
  760. unsigned char op = scsi_get_prot_op(sc);
  761. unsigned int protcnt = scsi_prot_sg_count(sc);
  762. static int cnt;
  763. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  764. op == SCSI_PROT_WRITE_PASS ||
  765. op == SCSI_PROT_WRITE_CONVERT)) {
  766. cnt++;
  767. spt = page_address(sg_page(scsi_prot_sglist(sc))) +
  768. scsi_prot_sglist(sc)[0].offset;
  769. *apptagmask = 0;
  770. *apptagval = 0;
  771. *reftag = cpu_to_be32(spt->ref_tag);
  772. } else {
  773. /* SBC defines ref tag to be lower 32bits of LBA */
  774. *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
  775. *apptagmask = 0;
  776. *apptagval = 0;
  777. }
  778. }
  779. /*
  780. * This function sets up buffer list for protection groups of
  781. * type LPFC_PG_TYPE_NO_DIF
  782. *
  783. * This is usually used when the HBA is instructed to generate
  784. * DIFs and insert them into data stream (or strip DIF from
  785. * incoming data stream)
  786. *
  787. * The buffer list consists of just one protection group described
  788. * below:
  789. * +-------------------------+
  790. * start of prot group --> | PDE_1 |
  791. * +-------------------------+
  792. * | Data BDE |
  793. * +-------------------------+
  794. * |more Data BDE's ... (opt)|
  795. * +-------------------------+
  796. *
  797. * @sc: pointer to scsi command we're working on
  798. * @bpl: pointer to buffer list for protection groups
  799. * @datacnt: number of segments of data that have been dma mapped
  800. *
  801. * Note: Data s/g buffers have been dma mapped
  802. */
  803. static int
  804. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  805. struct ulp_bde64 *bpl, int datasegcnt)
  806. {
  807. struct scatterlist *sgde = NULL; /* s/g data entry */
  808. struct lpfc_pde *pde1 = NULL;
  809. dma_addr_t physaddr;
  810. int i = 0, num_bde = 0;
  811. int datadir = sc->sc_data_direction;
  812. int prof = LPFC_PROF_INVALID;
  813. unsigned blksize;
  814. uint32_t reftag;
  815. uint16_t apptagmask, apptagval;
  816. pde1 = (struct lpfc_pde *) bpl;
  817. prof = lpfc_sc_to_sli_prof(sc);
  818. if (prof == LPFC_PROF_INVALID)
  819. goto out;
  820. /* extract some info from the scsi command for PDE1*/
  821. blksize = lpfc_cmd_blksize(sc);
  822. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  823. /* setup PDE1 with what we have */
  824. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  825. BG_EC_STOP_ERR);
  826. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  827. num_bde++;
  828. bpl++;
  829. /* assumption: caller has already run dma_map_sg on command data */
  830. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  831. physaddr = sg_dma_address(sgde);
  832. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  833. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  834. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  835. if (datadir == DMA_TO_DEVICE)
  836. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  837. else
  838. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  839. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  840. bpl++;
  841. num_bde++;
  842. }
  843. out:
  844. return num_bde;
  845. }
  846. /*
  847. * This function sets up buffer list for protection groups of
  848. * type LPFC_PG_TYPE_DIF_BUF
  849. *
  850. * This is usually used when DIFs are in their own buffers,
  851. * separate from the data. The HBA can then by instructed
  852. * to place the DIFs in the outgoing stream. For read operations,
  853. * The HBA could extract the DIFs and place it in DIF buffers.
  854. *
  855. * The buffer list for this type consists of one or more of the
  856. * protection groups described below:
  857. * +-------------------------+
  858. * start of first prot group --> | PDE_1 |
  859. * +-------------------------+
  860. * | PDE_3 (Prot BDE) |
  861. * +-------------------------+
  862. * | Data BDE |
  863. * +-------------------------+
  864. * |more Data BDE's ... (opt)|
  865. * +-------------------------+
  866. * start of new prot group --> | PDE_1 |
  867. * +-------------------------+
  868. * | ... |
  869. * +-------------------------+
  870. *
  871. * @sc: pointer to scsi command we're working on
  872. * @bpl: pointer to buffer list for protection groups
  873. * @datacnt: number of segments of data that have been dma mapped
  874. * @protcnt: number of segment of protection data that have been dma mapped
  875. *
  876. * Note: It is assumed that both data and protection s/g buffers have been
  877. * mapped for DMA
  878. */
  879. static int
  880. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  881. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  882. {
  883. struct scatterlist *sgde = NULL; /* s/g data entry */
  884. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  885. struct lpfc_pde *pde1 = NULL;
  886. struct ulp_bde64 *prot_bde = NULL;
  887. dma_addr_t dataphysaddr, protphysaddr;
  888. unsigned short curr_data = 0, curr_prot = 0;
  889. unsigned int split_offset, protgroup_len;
  890. unsigned int protgrp_blks, protgrp_bytes;
  891. unsigned int remainder, subtotal;
  892. int prof = LPFC_PROF_INVALID;
  893. int datadir = sc->sc_data_direction;
  894. unsigned char pgdone = 0, alldone = 0;
  895. unsigned blksize;
  896. uint32_t reftag;
  897. uint16_t apptagmask, apptagval;
  898. int num_bde = 0;
  899. sgpe = scsi_prot_sglist(sc);
  900. sgde = scsi_sglist(sc);
  901. if (!sgpe || !sgde) {
  902. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  903. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  904. sgpe, sgde);
  905. return 0;
  906. }
  907. prof = lpfc_sc_to_sli_prof(sc);
  908. if (prof == LPFC_PROF_INVALID)
  909. goto out;
  910. /* extract some info from the scsi command for PDE1*/
  911. blksize = lpfc_cmd_blksize(sc);
  912. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  913. split_offset = 0;
  914. do {
  915. /* setup the first PDE_1 */
  916. pde1 = (struct lpfc_pde *) bpl;
  917. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  918. BG_EC_STOP_ERR);
  919. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  920. num_bde++;
  921. bpl++;
  922. /* setup the first BDE that points to protection buffer */
  923. prot_bde = (struct ulp_bde64 *) bpl;
  924. protphysaddr = sg_dma_address(sgpe);
  925. prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  926. prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  927. protgroup_len = sg_dma_len(sgpe);
  928. /* must be integer multiple of the DIF block length */
  929. BUG_ON(protgroup_len % 8);
  930. protgrp_blks = protgroup_len / 8;
  931. protgrp_bytes = protgrp_blks * blksize;
  932. prot_bde->tus.f.bdeSize = protgroup_len;
  933. if (datadir == DMA_TO_DEVICE)
  934. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  935. else
  936. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  937. prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
  938. curr_prot++;
  939. num_bde++;
  940. /* setup BDE's for data blocks associated with DIF data */
  941. pgdone = 0;
  942. subtotal = 0; /* total bytes processed for current prot grp */
  943. while (!pgdone) {
  944. if (!sgde) {
  945. printk(KERN_ERR "%s Invalid data segment\n",
  946. __func__);
  947. return 0;
  948. }
  949. bpl++;
  950. dataphysaddr = sg_dma_address(sgde) + split_offset;
  951. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  952. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  953. remainder = sg_dma_len(sgde) - split_offset;
  954. if ((subtotal + remainder) <= protgrp_bytes) {
  955. /* we can use this whole buffer */
  956. bpl->tus.f.bdeSize = remainder;
  957. split_offset = 0;
  958. if ((subtotal + remainder) == protgrp_bytes)
  959. pgdone = 1;
  960. } else {
  961. /* must split this buffer with next prot grp */
  962. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  963. split_offset += bpl->tus.f.bdeSize;
  964. }
  965. subtotal += bpl->tus.f.bdeSize;
  966. if (datadir == DMA_TO_DEVICE)
  967. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  968. else
  969. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  970. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  971. num_bde++;
  972. curr_data++;
  973. if (split_offset)
  974. break;
  975. /* Move to the next s/g segment if possible */
  976. sgde = sg_next(sgde);
  977. }
  978. /* are we done ? */
  979. if (curr_prot == protcnt) {
  980. alldone = 1;
  981. } else if (curr_prot < protcnt) {
  982. /* advance to next prot buffer */
  983. sgpe = sg_next(sgpe);
  984. bpl++;
  985. /* update the reference tag */
  986. reftag += protgrp_blks;
  987. } else {
  988. /* if we're here, we have a bug */
  989. printk(KERN_ERR "BLKGRD: bug in %s\n", __func__);
  990. }
  991. } while (!alldone);
  992. out:
  993. return num_bde;
  994. }
  995. /*
  996. * Given a SCSI command that supports DIF, determine composition of protection
  997. * groups involved in setting up buffer lists
  998. *
  999. * Returns:
  1000. * for DIF (for both read and write)
  1001. * */
  1002. static int
  1003. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1004. {
  1005. int ret = LPFC_PG_TYPE_INVALID;
  1006. unsigned char op = scsi_get_prot_op(sc);
  1007. switch (op) {
  1008. case SCSI_PROT_READ_STRIP:
  1009. case SCSI_PROT_WRITE_INSERT:
  1010. ret = LPFC_PG_TYPE_NO_DIF;
  1011. break;
  1012. case SCSI_PROT_READ_INSERT:
  1013. case SCSI_PROT_WRITE_STRIP:
  1014. case SCSI_PROT_READ_PASS:
  1015. case SCSI_PROT_WRITE_PASS:
  1016. case SCSI_PROT_WRITE_CONVERT:
  1017. case SCSI_PROT_READ_CONVERT:
  1018. ret = LPFC_PG_TYPE_DIF_BUF;
  1019. break;
  1020. default:
  1021. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1022. "9021 Unsupported protection op:%d\n", op);
  1023. break;
  1024. }
  1025. return ret;
  1026. }
  1027. /*
  1028. * This is the protection/DIF aware version of
  1029. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  1030. * two functions eventually, but for now, it's here
  1031. */
  1032. static int
  1033. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
  1034. struct lpfc_scsi_buf *lpfc_cmd)
  1035. {
  1036. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1037. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1038. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1039. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1040. uint32_t num_bde = 0;
  1041. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  1042. int prot_group_type = 0;
  1043. int diflen, fcpdl;
  1044. unsigned blksize;
  1045. /*
  1046. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  1047. * fcp_rsp regions to the first data bde entry
  1048. */
  1049. bpl += 2;
  1050. if (scsi_sg_count(scsi_cmnd)) {
  1051. /*
  1052. * The driver stores the segment count returned from pci_map_sg
  1053. * because this a count of dma-mappings used to map the use_sg
  1054. * pages. They are not guaranteed to be the same for those
  1055. * architectures that implement an IOMMU.
  1056. */
  1057. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  1058. scsi_sglist(scsi_cmnd),
  1059. scsi_sg_count(scsi_cmnd), datadir);
  1060. if (unlikely(!datasegcnt))
  1061. return 1;
  1062. lpfc_cmd->seg_cnt = datasegcnt;
  1063. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1064. printk(KERN_ERR "%s: Too many sg segments from "
  1065. "dma_map_sg. Config %d, seg_cnt %d\n",
  1066. __func__, phba->cfg_sg_seg_cnt,
  1067. lpfc_cmd->seg_cnt);
  1068. scsi_dma_unmap(scsi_cmnd);
  1069. return 1;
  1070. }
  1071. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  1072. switch (prot_group_type) {
  1073. case LPFC_PG_TYPE_NO_DIF:
  1074. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  1075. datasegcnt);
  1076. /* we shoud have 2 or more entries in buffer list */
  1077. if (num_bde < 2)
  1078. goto err;
  1079. break;
  1080. case LPFC_PG_TYPE_DIF_BUF:{
  1081. /*
  1082. * This type indicates that protection buffers are
  1083. * passed to the driver, so that needs to be prepared
  1084. * for DMA
  1085. */
  1086. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  1087. scsi_prot_sglist(scsi_cmnd),
  1088. scsi_prot_sg_count(scsi_cmnd), datadir);
  1089. if (unlikely(!protsegcnt)) {
  1090. scsi_dma_unmap(scsi_cmnd);
  1091. return 1;
  1092. }
  1093. lpfc_cmd->prot_seg_cnt = protsegcnt;
  1094. if (lpfc_cmd->prot_seg_cnt
  1095. > phba->cfg_prot_sg_seg_cnt) {
  1096. printk(KERN_ERR "%s: Too many prot sg segments "
  1097. "from dma_map_sg. Config %d,"
  1098. "prot_seg_cnt %d\n", __func__,
  1099. phba->cfg_prot_sg_seg_cnt,
  1100. lpfc_cmd->prot_seg_cnt);
  1101. dma_unmap_sg(&phba->pcidev->dev,
  1102. scsi_prot_sglist(scsi_cmnd),
  1103. scsi_prot_sg_count(scsi_cmnd),
  1104. datadir);
  1105. scsi_dma_unmap(scsi_cmnd);
  1106. return 1;
  1107. }
  1108. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  1109. datasegcnt, protsegcnt);
  1110. /* we shoud have 3 or more entries in buffer list */
  1111. if (num_bde < 3)
  1112. goto err;
  1113. break;
  1114. }
  1115. case LPFC_PG_TYPE_INVALID:
  1116. default:
  1117. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1118. "9022 Unexpected protection group %i\n",
  1119. prot_group_type);
  1120. return 1;
  1121. }
  1122. }
  1123. /*
  1124. * Finish initializing those IOCB fields that are dependent on the
  1125. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  1126. * reinitialized since all iocb memory resources are used many times
  1127. * for transmit, receive, and continuation bpl's.
  1128. */
  1129. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  1130. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  1131. iocb_cmd->ulpBdeCount = 1;
  1132. iocb_cmd->ulpLe = 1;
  1133. fcpdl = scsi_bufflen(scsi_cmnd);
  1134. if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
  1135. /*
  1136. * We are in DIF Type 1 mode
  1137. * Every data block has a 8 byte DIF (trailer)
  1138. * attached to it. Must ajust FCP data length
  1139. */
  1140. blksize = lpfc_cmd_blksize(scsi_cmnd);
  1141. diflen = (fcpdl / blksize) * 8;
  1142. fcpdl += diflen;
  1143. }
  1144. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  1145. /*
  1146. * Due to difference in data length between DIF/non-DIF paths,
  1147. * we need to set word 4 of IOCB here
  1148. */
  1149. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  1150. return 0;
  1151. err:
  1152. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1153. "9023 Could not setup all needed BDE's"
  1154. "prot_group_type=%d, num_bde=%d\n",
  1155. prot_group_type, num_bde);
  1156. return 1;
  1157. }
  1158. /*
  1159. * This function checks for BlockGuard errors detected by
  1160. * the HBA. In case of errors, the ASC/ASCQ fields in the
  1161. * sense buffer will be set accordingly, paired with
  1162. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  1163. * detected corruption.
  1164. *
  1165. * Returns:
  1166. * 0 - No error found
  1167. * 1 - BlockGuard error found
  1168. * -1 - Internal error (bad profile, ...etc)
  1169. */
  1170. static int
  1171. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  1172. struct lpfc_iocbq *pIocbOut)
  1173. {
  1174. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1175. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  1176. int ret = 0;
  1177. uint32_t bghm = bgf->bghm;
  1178. uint32_t bgstat = bgf->bgstat;
  1179. uint64_t failing_sector = 0;
  1180. printk(KERN_ERR "BG ERROR in cmd 0x%x lba 0x%llx blk cnt 0x%lx "
  1181. "bgstat=0x%x bghm=0x%x\n",
  1182. cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
  1183. cmd->request->nr_sectors, bgstat, bghm);
  1184. spin_lock(&_dump_buf_lock);
  1185. if (!_dump_buf_done) {
  1186. printk(KERN_ERR "Saving Data for %u blocks to debugfs\n",
  1187. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1188. lpfc_debug_save_data(cmd);
  1189. /* If we have a prot sgl, save the DIF buffer */
  1190. if (lpfc_prot_group_type(phba, cmd) ==
  1191. LPFC_PG_TYPE_DIF_BUF) {
  1192. printk(KERN_ERR "Saving DIF for %u blocks to debugfs\n",
  1193. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1194. lpfc_debug_save_dif(cmd);
  1195. }
  1196. _dump_buf_done = 1;
  1197. }
  1198. spin_unlock(&_dump_buf_lock);
  1199. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  1200. cmd->result = ScsiResult(DID_ERROR, 0);
  1201. printk(KERN_ERR "Invalid BlockGuard profile. bgstat:0x%x\n",
  1202. bgstat);
  1203. ret = (-1);
  1204. goto out;
  1205. }
  1206. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  1207. cmd->result = ScsiResult(DID_ERROR, 0);
  1208. printk(KERN_ERR "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
  1209. bgstat);
  1210. ret = (-1);
  1211. goto out;
  1212. }
  1213. if (lpfc_bgs_get_guard_err(bgstat)) {
  1214. ret = 1;
  1215. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1216. 0x10, 0x1);
  1217. cmd->result = DRIVER_SENSE << 24
  1218. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1219. phba->bg_guard_err_cnt++;
  1220. printk(KERN_ERR "BLKGRD: guard_tag error\n");
  1221. }
  1222. if (lpfc_bgs_get_reftag_err(bgstat)) {
  1223. ret = 1;
  1224. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1225. 0x10, 0x3);
  1226. cmd->result = DRIVER_SENSE << 24
  1227. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1228. phba->bg_reftag_err_cnt++;
  1229. printk(KERN_ERR "BLKGRD: ref_tag error\n");
  1230. }
  1231. if (lpfc_bgs_get_apptag_err(bgstat)) {
  1232. ret = 1;
  1233. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1234. 0x10, 0x2);
  1235. cmd->result = DRIVER_SENSE << 24
  1236. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1237. phba->bg_apptag_err_cnt++;
  1238. printk(KERN_ERR "BLKGRD: app_tag error\n");
  1239. }
  1240. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  1241. /*
  1242. * setup sense data descriptor 0 per SPC-4 as an information
  1243. * field, and put the failing LBA in it
  1244. */
  1245. cmd->sense_buffer[8] = 0; /* Information */
  1246. cmd->sense_buffer[9] = 0xa; /* Add. length */
  1247. bghm /= cmd->device->sector_size;
  1248. failing_sector = scsi_get_lba(cmd);
  1249. failing_sector += bghm;
  1250. put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
  1251. }
  1252. if (!ret) {
  1253. /* No error was reported - problem in FW? */
  1254. cmd->result = ScsiResult(DID_ERROR, 0);
  1255. printk(KERN_ERR "BLKGRD: no errors reported!\n");
  1256. }
  1257. out:
  1258. return ret;
  1259. }
  1260. /**
  1261. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  1262. * @phba: Pointer to hba context object.
  1263. * @vport: Pointer to vport object.
  1264. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  1265. * @rsp_iocb: Pointer to response iocb object which reported error.
  1266. *
  1267. * This function posts an event when there is a SCSI command reporting
  1268. * error from the scsi device.
  1269. **/
  1270. static void
  1271. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  1272. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  1273. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  1274. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  1275. uint32_t resp_info = fcprsp->rspStatus2;
  1276. uint32_t scsi_status = fcprsp->rspStatus3;
  1277. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  1278. struct lpfc_fast_path_event *fast_path_evt = NULL;
  1279. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  1280. unsigned long flags;
  1281. /* If there is queuefull or busy condition send a scsi event */
  1282. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  1283. (cmnd->result == SAM_STAT_BUSY)) {
  1284. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1285. if (!fast_path_evt)
  1286. return;
  1287. fast_path_evt->un.scsi_evt.event_type =
  1288. FC_REG_SCSI_EVENT;
  1289. fast_path_evt->un.scsi_evt.subcategory =
  1290. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  1291. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  1292. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  1293. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  1294. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1295. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  1296. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1297. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  1298. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  1299. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1300. if (!fast_path_evt)
  1301. return;
  1302. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  1303. FC_REG_SCSI_EVENT;
  1304. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  1305. LPFC_EVENT_CHECK_COND;
  1306. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  1307. cmnd->device->lun;
  1308. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  1309. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1310. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  1311. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1312. fast_path_evt->un.check_cond_evt.sense_key =
  1313. cmnd->sense_buffer[2] & 0xf;
  1314. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  1315. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  1316. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  1317. fcpi_parm &&
  1318. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  1319. ((scsi_status == SAM_STAT_GOOD) &&
  1320. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  1321. /*
  1322. * If status is good or resid does not match with fcp_param and
  1323. * there is valid fcpi_parm, then there is a read_check error
  1324. */
  1325. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1326. if (!fast_path_evt)
  1327. return;
  1328. fast_path_evt->un.read_check_error.header.event_type =
  1329. FC_REG_FABRIC_EVENT;
  1330. fast_path_evt->un.read_check_error.header.subcategory =
  1331. LPFC_EVENT_FCPRDCHKERR;
  1332. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  1333. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1334. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  1335. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1336. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  1337. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  1338. fast_path_evt->un.read_check_error.fcpiparam =
  1339. fcpi_parm;
  1340. } else
  1341. return;
  1342. fast_path_evt->vport = vport;
  1343. spin_lock_irqsave(&phba->hbalock, flags);
  1344. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  1345. spin_unlock_irqrestore(&phba->hbalock, flags);
  1346. lpfc_worker_wake_up(phba);
  1347. return;
  1348. }
  1349. /**
  1350. * lpfc_scsi_unprep_dma_buf - Routine to un-map DMA mapping of scatter gather
  1351. * @phba: The Hba for which this call is being executed.
  1352. * @psb: The scsi buffer which is going to be un-mapped.
  1353. *
  1354. * This routine does DMA un-mapping of scatter gather list of scsi command
  1355. * field of @lpfc_cmd.
  1356. **/
  1357. static void
  1358. lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
  1359. {
  1360. /*
  1361. * There are only two special cases to consider. (1) the scsi command
  1362. * requested scatter-gather usage or (2) the scsi command allocated
  1363. * a request buffer, but did not request use_sg. There is a third
  1364. * case, but it does not require resource deallocation.
  1365. */
  1366. if (psb->seg_cnt > 0)
  1367. scsi_dma_unmap(psb->pCmd);
  1368. if (psb->prot_seg_cnt > 0)
  1369. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  1370. scsi_prot_sg_count(psb->pCmd),
  1371. psb->pCmd->sc_data_direction);
  1372. }
  1373. /**
  1374. * lpfc_handler_fcp_err - FCP response handler
  1375. * @vport: The virtual port for which this call is being executed.
  1376. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  1377. * @rsp_iocb: The response IOCB which contains FCP error.
  1378. *
  1379. * This routine is called to process response IOCB with status field
  1380. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  1381. * based upon SCSI and FCP error.
  1382. **/
  1383. static void
  1384. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  1385. struct lpfc_iocbq *rsp_iocb)
  1386. {
  1387. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  1388. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  1389. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  1390. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  1391. uint32_t resp_info = fcprsp->rspStatus2;
  1392. uint32_t scsi_status = fcprsp->rspStatus3;
  1393. uint32_t *lp;
  1394. uint32_t host_status = DID_OK;
  1395. uint32_t rsplen = 0;
  1396. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  1397. /*
  1398. * If this is a task management command, there is no
  1399. * scsi packet associated with this lpfc_cmd. The driver
  1400. * consumes it.
  1401. */
  1402. if (fcpcmd->fcpCntl2) {
  1403. scsi_status = 0;
  1404. goto out;
  1405. }
  1406. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  1407. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  1408. if (snslen > SCSI_SENSE_BUFFERSIZE)
  1409. snslen = SCSI_SENSE_BUFFERSIZE;
  1410. if (resp_info & RSP_LEN_VALID)
  1411. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  1412. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  1413. }
  1414. lp = (uint32_t *)cmnd->sense_buffer;
  1415. if (!scsi_status && (resp_info & RESID_UNDER))
  1416. logit = LOG_FCP;
  1417. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  1418. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  1419. "Data: x%x x%x x%x x%x x%x\n",
  1420. cmnd->cmnd[0], scsi_status,
  1421. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  1422. be32_to_cpu(fcprsp->rspResId),
  1423. be32_to_cpu(fcprsp->rspSnsLen),
  1424. be32_to_cpu(fcprsp->rspRspLen),
  1425. fcprsp->rspInfo3);
  1426. if (resp_info & RSP_LEN_VALID) {
  1427. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  1428. if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
  1429. (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
  1430. host_status = DID_ERROR;
  1431. goto out;
  1432. }
  1433. }
  1434. scsi_set_resid(cmnd, 0);
  1435. if (resp_info & RESID_UNDER) {
  1436. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  1437. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1438. "9025 FCP Read Underrun, expected %d, "
  1439. "residual %d Data: x%x x%x x%x\n",
  1440. be32_to_cpu(fcpcmd->fcpDl),
  1441. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  1442. cmnd->underflow);
  1443. /*
  1444. * If there is an under run check if under run reported by
  1445. * storage array is same as the under run reported by HBA.
  1446. * If this is not same, there is a dropped frame.
  1447. */
  1448. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  1449. fcpi_parm &&
  1450. (scsi_get_resid(cmnd) != fcpi_parm)) {
  1451. lpfc_printf_vlog(vport, KERN_WARNING,
  1452. LOG_FCP | LOG_FCP_ERROR,
  1453. "9026 FCP Read Check Error "
  1454. "and Underrun Data: x%x x%x x%x x%x\n",
  1455. be32_to_cpu(fcpcmd->fcpDl),
  1456. scsi_get_resid(cmnd), fcpi_parm,
  1457. cmnd->cmnd[0]);
  1458. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  1459. host_status = DID_ERROR;
  1460. }
  1461. /*
  1462. * The cmnd->underflow is the minimum number of bytes that must
  1463. * be transfered for this command. Provided a sense condition
  1464. * is not present, make sure the actual amount transferred is at
  1465. * least the underflow value or fail.
  1466. */
  1467. if (!(resp_info & SNS_LEN_VALID) &&
  1468. (scsi_status == SAM_STAT_GOOD) &&
  1469. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  1470. < cmnd->underflow)) {
  1471. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1472. "9027 FCP command x%x residual "
  1473. "underrun converted to error "
  1474. "Data: x%x x%x x%x\n",
  1475. cmnd->cmnd[0], scsi_bufflen(cmnd),
  1476. scsi_get_resid(cmnd), cmnd->underflow);
  1477. host_status = DID_ERROR;
  1478. }
  1479. } else if (resp_info & RESID_OVER) {
  1480. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1481. "9028 FCP command x%x residual overrun error. "
  1482. "Data: x%x x%x \n", cmnd->cmnd[0],
  1483. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  1484. host_status = DID_ERROR;
  1485. /*
  1486. * Check SLI validation that all the transfer was actually done
  1487. * (fcpi_parm should be zero). Apply check only to reads.
  1488. */
  1489. } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
  1490. (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  1491. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  1492. "9029 FCP Read Check Error Data: "
  1493. "x%x x%x x%x x%x\n",
  1494. be32_to_cpu(fcpcmd->fcpDl),
  1495. be32_to_cpu(fcprsp->rspResId),
  1496. fcpi_parm, cmnd->cmnd[0]);
  1497. host_status = DID_ERROR;
  1498. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  1499. }
  1500. out:
  1501. cmnd->result = ScsiResult(host_status, scsi_status);
  1502. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  1503. }
  1504. /**
  1505. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  1506. * @phba: The Hba for which this call is being executed.
  1507. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  1508. * @pIocbOut: The response IOCBQ for the scsi cmnd .
  1509. *
  1510. * This routine assigns scsi command result by looking into response IOCB
  1511. * status field appropriately. This routine handles QUEUE FULL condition as
  1512. * well by ramping down device queue depth.
  1513. **/
  1514. static void
  1515. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  1516. struct lpfc_iocbq *pIocbOut)
  1517. {
  1518. struct lpfc_scsi_buf *lpfc_cmd =
  1519. (struct lpfc_scsi_buf *) pIocbIn->context1;
  1520. struct lpfc_vport *vport = pIocbIn->vport;
  1521. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  1522. struct lpfc_nodelist *pnode = rdata->pnode;
  1523. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1524. int result;
  1525. struct scsi_device *tmp_sdev;
  1526. int depth = 0;
  1527. unsigned long flags;
  1528. struct lpfc_fast_path_event *fast_path_evt;
  1529. struct Scsi_Host *shost = cmd->device->host;
  1530. uint32_t queue_depth, scsi_id;
  1531. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  1532. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  1533. if (pnode && NLP_CHK_NODE_ACT(pnode))
  1534. atomic_dec(&pnode->cmd_pending);
  1535. if (lpfc_cmd->status) {
  1536. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  1537. (lpfc_cmd->result & IOERR_DRVR_MASK))
  1538. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1539. else if (lpfc_cmd->status >= IOSTAT_CNT)
  1540. lpfc_cmd->status = IOSTAT_DEFAULT;
  1541. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1542. "9030 FCP cmd x%x failed <%d/%d> "
  1543. "status: x%x result: x%x Data: x%x x%x\n",
  1544. cmd->cmnd[0],
  1545. cmd->device ? cmd->device->id : 0xffff,
  1546. cmd->device ? cmd->device->lun : 0xffff,
  1547. lpfc_cmd->status, lpfc_cmd->result,
  1548. pIocbOut->iocb.ulpContext,
  1549. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  1550. switch (lpfc_cmd->status) {
  1551. case IOSTAT_FCP_RSP_ERROR:
  1552. /* Call FCP RSP handler to determine result */
  1553. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  1554. break;
  1555. case IOSTAT_NPORT_BSY:
  1556. case IOSTAT_FABRIC_BSY:
  1557. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  1558. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1559. if (!fast_path_evt)
  1560. break;
  1561. fast_path_evt->un.fabric_evt.event_type =
  1562. FC_REG_FABRIC_EVENT;
  1563. fast_path_evt->un.fabric_evt.subcategory =
  1564. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  1565. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  1566. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  1567. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  1568. &pnode->nlp_portname,
  1569. sizeof(struct lpfc_name));
  1570. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  1571. &pnode->nlp_nodename,
  1572. sizeof(struct lpfc_name));
  1573. }
  1574. fast_path_evt->vport = vport;
  1575. fast_path_evt->work_evt.evt =
  1576. LPFC_EVT_FASTPATH_MGMT_EVT;
  1577. spin_lock_irqsave(&phba->hbalock, flags);
  1578. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  1579. &phba->work_list);
  1580. spin_unlock_irqrestore(&phba->hbalock, flags);
  1581. lpfc_worker_wake_up(phba);
  1582. break;
  1583. case IOSTAT_LOCAL_REJECT:
  1584. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  1585. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  1586. lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
  1587. cmd->result = ScsiResult(DID_REQUEUE, 0);
  1588. break;
  1589. }
  1590. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  1591. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  1592. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  1593. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  1594. /*
  1595. * This is a response for a BG enabled
  1596. * cmd. Parse BG error
  1597. */
  1598. lpfc_parse_bg_err(phba, lpfc_cmd,
  1599. pIocbOut);
  1600. break;
  1601. } else {
  1602. lpfc_printf_vlog(vport, KERN_WARNING,
  1603. LOG_BG,
  1604. "9031 non-zero BGSTAT "
  1605. "on unprotected cmd");
  1606. }
  1607. }
  1608. /* else: fall through */
  1609. default:
  1610. cmd->result = ScsiResult(DID_ERROR, 0);
  1611. break;
  1612. }
  1613. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  1614. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  1615. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  1616. SAM_STAT_BUSY);
  1617. } else {
  1618. cmd->result = ScsiResult(DID_OK, 0);
  1619. }
  1620. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  1621. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  1622. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1623. "0710 Iodone <%d/%d> cmd %p, error "
  1624. "x%x SNS x%x x%x Data: x%x x%x\n",
  1625. cmd->device->id, cmd->device->lun, cmd,
  1626. cmd->result, *lp, *(lp + 3), cmd->retries,
  1627. scsi_get_resid(cmd));
  1628. }
  1629. lpfc_update_stats(phba, lpfc_cmd);
  1630. result = cmd->result;
  1631. if (vport->cfg_max_scsicmpl_time &&
  1632. time_after(jiffies, lpfc_cmd->start_time +
  1633. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  1634. spin_lock_irqsave(shost->host_lock, flags);
  1635. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  1636. if (pnode->cmd_qdepth >
  1637. atomic_read(&pnode->cmd_pending) &&
  1638. (atomic_read(&pnode->cmd_pending) >
  1639. LPFC_MIN_TGT_QDEPTH) &&
  1640. ((cmd->cmnd[0] == READ_10) ||
  1641. (cmd->cmnd[0] == WRITE_10)))
  1642. pnode->cmd_qdepth =
  1643. atomic_read(&pnode->cmd_pending);
  1644. pnode->last_change_time = jiffies;
  1645. }
  1646. spin_unlock_irqrestore(shost->host_lock, flags);
  1647. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  1648. if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
  1649. time_after(jiffies, pnode->last_change_time +
  1650. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  1651. spin_lock_irqsave(shost->host_lock, flags);
  1652. pnode->cmd_qdepth += pnode->cmd_qdepth *
  1653. LPFC_TGTQ_RAMPUP_PCENT / 100;
  1654. if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
  1655. pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
  1656. pnode->last_change_time = jiffies;
  1657. spin_unlock_irqrestore(shost->host_lock, flags);
  1658. }
  1659. }
  1660. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  1661. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  1662. queue_depth = cmd->device->queue_depth;
  1663. scsi_id = cmd->device->id;
  1664. cmd->scsi_done(cmd);
  1665. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1666. /*
  1667. * If there is a thread waiting for command completion
  1668. * wake up the thread.
  1669. */
  1670. spin_lock_irqsave(shost->host_lock, flags);
  1671. lpfc_cmd->pCmd = NULL;
  1672. if (lpfc_cmd->waitq)
  1673. wake_up(lpfc_cmd->waitq);
  1674. spin_unlock_irqrestore(shost->host_lock, flags);
  1675. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1676. return;
  1677. }
  1678. if (!result)
  1679. lpfc_rampup_queue_depth(vport, queue_depth);
  1680. if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
  1681. ((jiffies - pnode->last_ramp_up_time) >
  1682. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  1683. ((jiffies - pnode->last_q_full_time) >
  1684. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  1685. (vport->cfg_lun_queue_depth > queue_depth)) {
  1686. shost_for_each_device(tmp_sdev, shost) {
  1687. if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
  1688. if (tmp_sdev->id != scsi_id)
  1689. continue;
  1690. if (tmp_sdev->ordered_tags)
  1691. scsi_adjust_queue_depth(tmp_sdev,
  1692. MSG_ORDERED_TAG,
  1693. tmp_sdev->queue_depth+1);
  1694. else
  1695. scsi_adjust_queue_depth(tmp_sdev,
  1696. MSG_SIMPLE_TAG,
  1697. tmp_sdev->queue_depth+1);
  1698. pnode->last_ramp_up_time = jiffies;
  1699. }
  1700. }
  1701. lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
  1702. 0xFFFFFFFF,
  1703. queue_depth , queue_depth + 1);
  1704. }
  1705. /*
  1706. * Check for queue full. If the lun is reporting queue full, then
  1707. * back off the lun queue depth to prevent target overloads.
  1708. */
  1709. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  1710. NLP_CHK_NODE_ACT(pnode)) {
  1711. pnode->last_q_full_time = jiffies;
  1712. shost_for_each_device(tmp_sdev, shost) {
  1713. if (tmp_sdev->id != scsi_id)
  1714. continue;
  1715. depth = scsi_track_queue_full(tmp_sdev,
  1716. tmp_sdev->queue_depth - 1);
  1717. }
  1718. /*
  1719. * The queue depth cannot be lowered any more.
  1720. * Modify the returned error code to store
  1721. * the final depth value set by
  1722. * scsi_track_queue_full.
  1723. */
  1724. if (depth == -1)
  1725. depth = shost->cmd_per_lun;
  1726. if (depth) {
  1727. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1728. "0711 detected queue full - lun queue "
  1729. "depth adjusted to %d.\n", depth);
  1730. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  1731. pnode, 0xFFFFFFFF,
  1732. depth+1, depth);
  1733. }
  1734. }
  1735. /*
  1736. * If there is a thread waiting for command completion
  1737. * wake up the thread.
  1738. */
  1739. spin_lock_irqsave(shost->host_lock, flags);
  1740. lpfc_cmd->pCmd = NULL;
  1741. if (lpfc_cmd->waitq)
  1742. wake_up(lpfc_cmd->waitq);
  1743. spin_unlock_irqrestore(shost->host_lock, flags);
  1744. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1745. }
  1746. /**
  1747. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  1748. * @data: A pointer to the immediate command data portion of the IOCB.
  1749. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  1750. *
  1751. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  1752. * byte swapping the data to big endian format for transmission on the wire.
  1753. **/
  1754. static void
  1755. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  1756. {
  1757. int i, j;
  1758. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  1759. i += sizeof(uint32_t), j++) {
  1760. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  1761. }
  1762. }
  1763. /**
  1764. * lpfc_scsi_prep_cmnd - Routine to convert scsi cmnd to FCP information unit
  1765. * @vport: The virtual port for which this call is being executed.
  1766. * @lpfc_cmd: The scsi command which needs to send.
  1767. * @pnode: Pointer to lpfc_nodelist.
  1768. *
  1769. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  1770. * to transfer.
  1771. **/
  1772. static void
  1773. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  1774. struct lpfc_nodelist *pnode)
  1775. {
  1776. struct lpfc_hba *phba = vport->phba;
  1777. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1778. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1779. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1780. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  1781. int datadir = scsi_cmnd->sc_data_direction;
  1782. char tag[2];
  1783. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  1784. return;
  1785. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  1786. /* clear task management bits */
  1787. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  1788. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  1789. &lpfc_cmd->fcp_cmnd->fcp_lun);
  1790. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  1791. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  1792. switch (tag[0]) {
  1793. case HEAD_OF_QUEUE_TAG:
  1794. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  1795. break;
  1796. case ORDERED_QUEUE_TAG:
  1797. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  1798. break;
  1799. default:
  1800. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  1801. break;
  1802. }
  1803. } else
  1804. fcp_cmnd->fcpCntl1 = 0;
  1805. /*
  1806. * There are three possibilities here - use scatter-gather segment, use
  1807. * the single mapping, or neither. Start the lpfc command prep by
  1808. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1809. * data bde entry.
  1810. */
  1811. if (scsi_sg_count(scsi_cmnd)) {
  1812. if (datadir == DMA_TO_DEVICE) {
  1813. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  1814. iocb_cmd->un.fcpi.fcpi_parm = 0;
  1815. iocb_cmd->ulpPU = 0;
  1816. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  1817. phba->fc4OutputRequests++;
  1818. } else {
  1819. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  1820. iocb_cmd->ulpPU = PARM_READ_CHECK;
  1821. fcp_cmnd->fcpCntl3 = READ_DATA;
  1822. phba->fc4InputRequests++;
  1823. }
  1824. } else {
  1825. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  1826. iocb_cmd->un.fcpi.fcpi_parm = 0;
  1827. iocb_cmd->ulpPU = 0;
  1828. fcp_cmnd->fcpCntl3 = 0;
  1829. phba->fc4ControlRequests++;
  1830. }
  1831. if (phba->sli_rev == 3 &&
  1832. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  1833. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  1834. /*
  1835. * Finish initializing those IOCB fields that are independent
  1836. * of the scsi_cmnd request_buffer
  1837. */
  1838. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  1839. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  1840. piocbq->iocb.ulpFCP2Rcvy = 1;
  1841. else
  1842. piocbq->iocb.ulpFCP2Rcvy = 0;
  1843. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  1844. piocbq->context1 = lpfc_cmd;
  1845. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  1846. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  1847. piocbq->vport = vport;
  1848. }
  1849. /**
  1850. * lpfc_scsi_prep_task_mgmt_cmnd - Convert scsi TM cmnd to FCP information unit
  1851. * @vport: The virtual port for which this call is being executed.
  1852. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  1853. * @lun: Logical unit number.
  1854. * @task_mgmt_cmd: SCSI task management command.
  1855. *
  1856. * This routine creates FCP information unit corresponding to @task_mgmt_cmd.
  1857. *
  1858. * Return codes:
  1859. * 0 - Error
  1860. * 1 - Success
  1861. **/
  1862. static int
  1863. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  1864. struct lpfc_scsi_buf *lpfc_cmd,
  1865. unsigned int lun,
  1866. uint8_t task_mgmt_cmd)
  1867. {
  1868. struct lpfc_iocbq *piocbq;
  1869. IOCB_t *piocb;
  1870. struct fcp_cmnd *fcp_cmnd;
  1871. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  1872. struct lpfc_nodelist *ndlp = rdata->pnode;
  1873. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  1874. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  1875. return 0;
  1876. piocbq = &(lpfc_cmd->cur_iocbq);
  1877. piocbq->vport = vport;
  1878. piocb = &piocbq->iocb;
  1879. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1880. /* Clear out any old data in the FCP command area */
  1881. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  1882. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  1883. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  1884. if (vport->phba->sli_rev == 3 &&
  1885. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  1886. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  1887. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  1888. piocb->ulpContext = ndlp->nlp_rpi;
  1889. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  1890. piocb->ulpFCP2Rcvy = 1;
  1891. }
  1892. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  1893. /* ulpTimeout is only one byte */
  1894. if (lpfc_cmd->timeout > 0xff) {
  1895. /*
  1896. * Do not timeout the command at the firmware level.
  1897. * The driver will provide the timeout mechanism.
  1898. */
  1899. piocb->ulpTimeout = 0;
  1900. } else {
  1901. piocb->ulpTimeout = lpfc_cmd->timeout;
  1902. }
  1903. return 1;
  1904. }
  1905. /**
  1906. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  1907. * @phba: The Hba for which this call is being executed.
  1908. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  1909. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  1910. *
  1911. * This routine is IOCB completion routine for device reset and target reset
  1912. * routine. This routine release scsi buffer associated with lpfc_cmd.
  1913. **/
  1914. static void
  1915. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  1916. struct lpfc_iocbq *cmdiocbq,
  1917. struct lpfc_iocbq *rspiocbq)
  1918. {
  1919. struct lpfc_scsi_buf *lpfc_cmd =
  1920. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  1921. if (lpfc_cmd)
  1922. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1923. return;
  1924. }
  1925. /**
  1926. * lpfc_scsi_tgt_reset - Target reset handler
  1927. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure
  1928. * @vport: The virtual port for which this call is being executed.
  1929. * @tgt_id: Target ID.
  1930. * @lun: Lun number.
  1931. * @rdata: Pointer to lpfc_rport_data.
  1932. *
  1933. * This routine issues a TARGET RESET iocb to reset a target with @tgt_id ID.
  1934. *
  1935. * Return Code:
  1936. * 0x2003 - Error
  1937. * 0x2002 - Success.
  1938. **/
  1939. static int
  1940. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
  1941. unsigned tgt_id, unsigned int lun,
  1942. struct lpfc_rport_data *rdata)
  1943. {
  1944. struct lpfc_hba *phba = vport->phba;
  1945. struct lpfc_iocbq *iocbq;
  1946. struct lpfc_iocbq *iocbqrsp;
  1947. int ret;
  1948. int status;
  1949. if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
  1950. return FAILED;
  1951. lpfc_cmd->rdata = rdata;
  1952. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
  1953. FCP_TARGET_RESET);
  1954. if (!status)
  1955. return FAILED;
  1956. iocbq = &lpfc_cmd->cur_iocbq;
  1957. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1958. if (!iocbqrsp)
  1959. return FAILED;
  1960. /* Issue Target Reset to TGT <num> */
  1961. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1962. "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
  1963. tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  1964. status = lpfc_sli_issue_iocb_wait(phba,
  1965. &phba->sli.ring[phba->sli.fcp_ring],
  1966. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1967. if (status != IOCB_SUCCESS) {
  1968. if (status == IOCB_TIMEDOUT) {
  1969. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1970. ret = TIMEOUT_ERROR;
  1971. } else
  1972. ret = FAILED;
  1973. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1974. } else {
  1975. ret = SUCCESS;
  1976. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  1977. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  1978. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  1979. (lpfc_cmd->result & IOERR_DRVR_MASK))
  1980. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1981. }
  1982. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1983. return ret;
  1984. }
  1985. /**
  1986. * lpfc_info - Info entry point of scsi_host_template data structure
  1987. * @host: The scsi host for which this call is being executed.
  1988. *
  1989. * This routine provides module information about hba.
  1990. *
  1991. * Reutrn code:
  1992. * Pointer to char - Success.
  1993. **/
  1994. const char *
  1995. lpfc_info(struct Scsi_Host *host)
  1996. {
  1997. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  1998. struct lpfc_hba *phba = vport->phba;
  1999. int len;
  2000. static char lpfcinfobuf[384];
  2001. memset(lpfcinfobuf,0,384);
  2002. if (phba && phba->pcidev){
  2003. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  2004. len = strlen(lpfcinfobuf);
  2005. snprintf(lpfcinfobuf + len,
  2006. 384-len,
  2007. " on PCI bus %02x device %02x irq %d",
  2008. phba->pcidev->bus->number,
  2009. phba->pcidev->devfn,
  2010. phba->pcidev->irq);
  2011. len = strlen(lpfcinfobuf);
  2012. if (phba->Port[0]) {
  2013. snprintf(lpfcinfobuf + len,
  2014. 384-len,
  2015. " port %s",
  2016. phba->Port);
  2017. }
  2018. }
  2019. return lpfcinfobuf;
  2020. }
  2021. /**
  2022. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  2023. * @phba: The Hba for which this call is being executed.
  2024. *
  2025. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  2026. * The default value of cfg_poll_tmo is 10 milliseconds.
  2027. **/
  2028. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  2029. {
  2030. unsigned long poll_tmo_expires =
  2031. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  2032. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  2033. mod_timer(&phba->fcp_poll_timer,
  2034. poll_tmo_expires);
  2035. }
  2036. /**
  2037. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  2038. * @phba: The Hba for which this call is being executed.
  2039. *
  2040. * This routine starts the fcp_poll_timer of @phba.
  2041. **/
  2042. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  2043. {
  2044. lpfc_poll_rearm_timer(phba);
  2045. }
  2046. /**
  2047. * lpfc_poll_timeout - Restart polling timer
  2048. * @ptr: Map to lpfc_hba data structure pointer.
  2049. *
  2050. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  2051. * and FCP Ring interrupt is disable.
  2052. **/
  2053. void lpfc_poll_timeout(unsigned long ptr)
  2054. {
  2055. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2056. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2057. lpfc_sli_poll_fcp_ring (phba);
  2058. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2059. lpfc_poll_rearm_timer(phba);
  2060. }
  2061. }
  2062. /**
  2063. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  2064. * @cmnd: Pointer to scsi_cmnd data structure.
  2065. * @done: Pointer to done routine.
  2066. *
  2067. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  2068. * This routine prepares an IOCB from scsi command and provides to firmware.
  2069. * The @done callback is invoked after driver finished processing the command.
  2070. *
  2071. * Return value :
  2072. * 0 - Success
  2073. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  2074. **/
  2075. static int
  2076. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  2077. {
  2078. struct Scsi_Host *shost = cmnd->device->host;
  2079. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2080. struct lpfc_hba *phba = vport->phba;
  2081. struct lpfc_sli *psli = &phba->sli;
  2082. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2083. struct lpfc_nodelist *ndlp = rdata->pnode;
  2084. struct lpfc_scsi_buf *lpfc_cmd;
  2085. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2086. int err;
  2087. err = fc_remote_port_chkready(rport);
  2088. if (err) {
  2089. cmnd->result = err;
  2090. goto out_fail_command;
  2091. }
  2092. if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  2093. scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2094. printk(KERN_ERR "BLKGRD ERROR: rcvd protected cmd:%02x op:%02x "
  2095. "str=%s without registering for BlockGuard - "
  2096. "Rejecting command\n",
  2097. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2098. dif_op_str[scsi_get_prot_op(cmnd)]);
  2099. goto out_fail_command;
  2100. }
  2101. /*
  2102. * Catch race where our node has transitioned, but the
  2103. * transport is still transitioning.
  2104. */
  2105. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  2106. cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2107. goto out_fail_command;
  2108. }
  2109. if (vport->cfg_max_scsicmpl_time &&
  2110. (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
  2111. goto out_host_busy;
  2112. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2113. if (lpfc_cmd == NULL) {
  2114. lpfc_rampdown_queue_depth(phba);
  2115. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2116. "0707 driver's buffer pool is empty, "
  2117. "IO busied\n");
  2118. goto out_host_busy;
  2119. }
  2120. /*
  2121. * Store the midlayer's command structure for the completion phase
  2122. * and complete the command initialization.
  2123. */
  2124. lpfc_cmd->pCmd = cmnd;
  2125. lpfc_cmd->rdata = rdata;
  2126. lpfc_cmd->timeout = 0;
  2127. lpfc_cmd->start_time = jiffies;
  2128. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  2129. cmnd->scsi_done = done;
  2130. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2131. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2132. "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
  2133. "str=%s\n",
  2134. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2135. dif_op_str[scsi_get_prot_op(cmnd)]);
  2136. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2137. "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2138. "%02x %02x %02x %02x %02x \n",
  2139. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2140. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  2141. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  2142. cmnd->cmnd[9]);
  2143. if (cmnd->cmnd[0] == READ_10)
  2144. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2145. "9035 BLKGRD: READ @ sector %llu, "
  2146. "count %lu\n",
  2147. (unsigned long long)scsi_get_lba(cmnd),
  2148. cmnd->request->nr_sectors);
  2149. else if (cmnd->cmnd[0] == WRITE_10)
  2150. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2151. "9036 BLKGRD: WRITE @ sector %llu, "
  2152. "count %lu cmd=%p\n",
  2153. (unsigned long long)scsi_get_lba(cmnd),
  2154. cmnd->request->nr_sectors,
  2155. cmnd);
  2156. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  2157. } else {
  2158. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2159. "9038 BLKGRD: rcvd unprotected cmd:%02x op:%02x"
  2160. " str=%s\n",
  2161. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2162. dif_op_str[scsi_get_prot_op(cmnd)]);
  2163. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2164. "9039 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2165. "%02x %02x %02x %02x %02x \n",
  2166. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2167. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  2168. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  2169. cmnd->cmnd[9]);
  2170. if (cmnd->cmnd[0] == READ_10)
  2171. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2172. "9040 dbg: READ @ sector %llu, "
  2173. "count %lu\n",
  2174. (unsigned long long)scsi_get_lba(cmnd),
  2175. cmnd->request->nr_sectors);
  2176. else if (cmnd->cmnd[0] == WRITE_10)
  2177. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2178. "9041 dbg: WRITE @ sector %llu, "
  2179. "count %lu cmd=%p\n",
  2180. (unsigned long long)scsi_get_lba(cmnd),
  2181. cmnd->request->nr_sectors, cmnd);
  2182. else
  2183. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2184. "9042 dbg: parser not implemented\n");
  2185. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  2186. }
  2187. if (err)
  2188. goto out_host_busy_free_buf;
  2189. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  2190. atomic_inc(&ndlp->cmd_pending);
  2191. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  2192. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  2193. if (err) {
  2194. atomic_dec(&ndlp->cmd_pending);
  2195. goto out_host_busy_free_buf;
  2196. }
  2197. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2198. lpfc_sli_poll_fcp_ring(phba);
  2199. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2200. lpfc_poll_rearm_timer(phba);
  2201. }
  2202. return 0;
  2203. out_host_busy_free_buf:
  2204. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2205. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2206. out_host_busy:
  2207. return SCSI_MLQUEUE_HOST_BUSY;
  2208. out_fail_command:
  2209. done(cmnd);
  2210. return 0;
  2211. }
  2212. /**
  2213. * lpfc_block_error_handler - Routine to block error handler
  2214. * @cmnd: Pointer to scsi_cmnd data structure.
  2215. *
  2216. * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
  2217. **/
  2218. static void
  2219. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  2220. {
  2221. struct Scsi_Host *shost = cmnd->device->host;
  2222. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2223. spin_lock_irq(shost->host_lock);
  2224. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  2225. spin_unlock_irq(shost->host_lock);
  2226. msleep(1000);
  2227. spin_lock_irq(shost->host_lock);
  2228. }
  2229. spin_unlock_irq(shost->host_lock);
  2230. return;
  2231. }
  2232. /**
  2233. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  2234. * @cmnd: Pointer to scsi_cmnd data structure.
  2235. *
  2236. * This routine aborts @cmnd pending in base driver.
  2237. *
  2238. * Return code :
  2239. * 0x2003 - Error
  2240. * 0x2002 - Success
  2241. **/
  2242. static int
  2243. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  2244. {
  2245. struct Scsi_Host *shost = cmnd->device->host;
  2246. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2247. struct lpfc_hba *phba = vport->phba;
  2248. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  2249. struct lpfc_iocbq *iocb;
  2250. struct lpfc_iocbq *abtsiocb;
  2251. struct lpfc_scsi_buf *lpfc_cmd;
  2252. IOCB_t *cmd, *icmd;
  2253. int ret = SUCCESS;
  2254. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  2255. lpfc_block_error_handler(cmnd);
  2256. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  2257. BUG_ON(!lpfc_cmd);
  2258. /*
  2259. * If pCmd field of the corresponding lpfc_scsi_buf structure
  2260. * points to a different SCSI command, then the driver has
  2261. * already completed this command, but the midlayer did not
  2262. * see the completion before the eh fired. Just return
  2263. * SUCCESS.
  2264. */
  2265. iocb = &lpfc_cmd->cur_iocbq;
  2266. if (lpfc_cmd->pCmd != cmnd)
  2267. goto out;
  2268. BUG_ON(iocb->context1 != lpfc_cmd);
  2269. abtsiocb = lpfc_sli_get_iocbq(phba);
  2270. if (abtsiocb == NULL) {
  2271. ret = FAILED;
  2272. goto out;
  2273. }
  2274. /*
  2275. * The scsi command can not be in txq and it is in flight because the
  2276. * pCmd is still pointig at the SCSI command we have to abort. There
  2277. * is no need to search the txcmplq. Just send an abort to the FW.
  2278. */
  2279. cmd = &iocb->iocb;
  2280. icmd = &abtsiocb->iocb;
  2281. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  2282. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  2283. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  2284. icmd->ulpLe = 1;
  2285. icmd->ulpClass = cmd->ulpClass;
  2286. if (lpfc_is_link_up(phba))
  2287. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  2288. else
  2289. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  2290. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  2291. abtsiocb->vport = vport;
  2292. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  2293. lpfc_sli_release_iocbq(phba, abtsiocb);
  2294. ret = FAILED;
  2295. goto out;
  2296. }
  2297. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2298. lpfc_sli_poll_fcp_ring (phba);
  2299. lpfc_cmd->waitq = &waitq;
  2300. /* Wait for abort to complete */
  2301. wait_event_timeout(waitq,
  2302. (lpfc_cmd->pCmd != cmnd),
  2303. (2*vport->cfg_devloss_tmo*HZ));
  2304. spin_lock_irq(shost->host_lock);
  2305. lpfc_cmd->waitq = NULL;
  2306. spin_unlock_irq(shost->host_lock);
  2307. if (lpfc_cmd->pCmd == cmnd) {
  2308. ret = FAILED;
  2309. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2310. "0748 abort handler timed out waiting "
  2311. "for abort to complete: ret %#x, ID %d, "
  2312. "LUN %d, snum %#lx\n",
  2313. ret, cmnd->device->id, cmnd->device->lun,
  2314. cmnd->serial_number);
  2315. }
  2316. out:
  2317. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2318. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  2319. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  2320. cmnd->device->lun, cmnd->serial_number);
  2321. return ret;
  2322. }
  2323. /**
  2324. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  2325. * @cmnd: Pointer to scsi_cmnd data structure.
  2326. *
  2327. * This routine does a device reset by sending a TARGET_RESET task management
  2328. * command.
  2329. *
  2330. * Return code :
  2331. * 0x2003 - Error
  2332. * 0x2002 - Success
  2333. **/
  2334. static int
  2335. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  2336. {
  2337. struct Scsi_Host *shost = cmnd->device->host;
  2338. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2339. struct lpfc_hba *phba = vport->phba;
  2340. struct lpfc_scsi_buf *lpfc_cmd;
  2341. struct lpfc_iocbq *iocbq, *iocbqrsp;
  2342. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2343. struct lpfc_nodelist *pnode = rdata->pnode;
  2344. unsigned long later;
  2345. int ret = SUCCESS;
  2346. int status;
  2347. int cnt;
  2348. struct lpfc_scsi_event_header scsi_event;
  2349. lpfc_block_error_handler(cmnd);
  2350. /*
  2351. * If target is not in a MAPPED state, delay the reset until
  2352. * target is rediscovered or devloss timeout expires.
  2353. */
  2354. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2355. while (time_after(later, jiffies)) {
  2356. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2357. return FAILED;
  2358. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  2359. break;
  2360. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  2361. rdata = cmnd->device->hostdata;
  2362. if (!rdata)
  2363. break;
  2364. pnode = rdata->pnode;
  2365. }
  2366. scsi_event.event_type = FC_REG_SCSI_EVENT;
  2367. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  2368. scsi_event.lun = 0;
  2369. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  2370. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2371. fc_host_post_vendor_event(shost,
  2372. fc_get_event_number(),
  2373. sizeof(scsi_event),
  2374. (char *)&scsi_event,
  2375. LPFC_NL_VENDOR_ID);
  2376. if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  2377. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2378. "0721 LUN Reset rport "
  2379. "failure: msec x%x rdata x%p\n",
  2380. jiffies_to_msecs(jiffies - later), rdata);
  2381. return FAILED;
  2382. }
  2383. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2384. if (lpfc_cmd == NULL)
  2385. return FAILED;
  2386. lpfc_cmd->timeout = 60;
  2387. lpfc_cmd->rdata = rdata;
  2388. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
  2389. cmnd->device->lun,
  2390. FCP_TARGET_RESET);
  2391. if (!status) {
  2392. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2393. return FAILED;
  2394. }
  2395. iocbq = &lpfc_cmd->cur_iocbq;
  2396. /* get a buffer for this IOCB command response */
  2397. iocbqrsp = lpfc_sli_get_iocbq(phba);
  2398. if (iocbqrsp == NULL) {
  2399. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2400. return FAILED;
  2401. }
  2402. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2403. "0703 Issue target reset to TGT %d LUN %d "
  2404. "rpi x%x nlp_flag x%x\n", cmnd->device->id,
  2405. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  2406. status = lpfc_sli_issue_iocb_wait(phba,
  2407. &phba->sli.ring[phba->sli.fcp_ring],
  2408. iocbq, iocbqrsp, lpfc_cmd->timeout);
  2409. if (status == IOCB_TIMEDOUT) {
  2410. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  2411. ret = TIMEOUT_ERROR;
  2412. } else {
  2413. if (status != IOCB_SUCCESS)
  2414. ret = FAILED;
  2415. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2416. }
  2417. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2418. "0713 SCSI layer issued device reset (%d, %d) "
  2419. "return x%x status x%x result x%x\n",
  2420. cmnd->device->id, cmnd->device->lun, ret,
  2421. iocbqrsp->iocb.ulpStatus,
  2422. iocbqrsp->iocb.un.ulpWord[4]);
  2423. lpfc_sli_release_iocbq(phba, iocbqrsp);
  2424. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
  2425. LPFC_CTX_TGT);
  2426. if (cnt)
  2427. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  2428. cmnd->device->id, cmnd->device->lun,
  2429. LPFC_CTX_TGT);
  2430. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2431. while (time_after(later, jiffies) && cnt) {
  2432. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  2433. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
  2434. cmnd->device->lun, LPFC_CTX_TGT);
  2435. }
  2436. if (cnt) {
  2437. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2438. "0719 device reset I/O flush failure: "
  2439. "cnt x%x\n", cnt);
  2440. ret = FAILED;
  2441. }
  2442. return ret;
  2443. }
  2444. /**
  2445. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  2446. * @cmnd: Pointer to scsi_cmnd data structure.
  2447. *
  2448. * This routine does target reset to all target on @cmnd->device->host.
  2449. *
  2450. * Return Code:
  2451. * 0x2003 - Error
  2452. * 0x2002 - Success
  2453. **/
  2454. static int
  2455. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  2456. {
  2457. struct Scsi_Host *shost = cmnd->device->host;
  2458. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2459. struct lpfc_hba *phba = vport->phba;
  2460. struct lpfc_nodelist *ndlp = NULL;
  2461. int match;
  2462. int ret = SUCCESS, status = SUCCESS, i;
  2463. int cnt;
  2464. struct lpfc_scsi_buf * lpfc_cmd;
  2465. unsigned long later;
  2466. struct lpfc_scsi_event_header scsi_event;
  2467. scsi_event.event_type = FC_REG_SCSI_EVENT;
  2468. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  2469. scsi_event.lun = 0;
  2470. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  2471. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  2472. fc_host_post_vendor_event(shost,
  2473. fc_get_event_number(),
  2474. sizeof(scsi_event),
  2475. (char *)&scsi_event,
  2476. LPFC_NL_VENDOR_ID);
  2477. lpfc_block_error_handler(cmnd);
  2478. /*
  2479. * Since the driver manages a single bus device, reset all
  2480. * targets known to the driver. Should any target reset
  2481. * fail, this routine returns failure to the midlayer.
  2482. */
  2483. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  2484. /* Search for mapped node by target ID */
  2485. match = 0;
  2486. spin_lock_irq(shost->host_lock);
  2487. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  2488. if (!NLP_CHK_NODE_ACT(ndlp))
  2489. continue;
  2490. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  2491. ndlp->nlp_sid == i &&
  2492. ndlp->rport) {
  2493. match = 1;
  2494. break;
  2495. }
  2496. }
  2497. spin_unlock_irq(shost->host_lock);
  2498. if (!match)
  2499. continue;
  2500. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2501. if (lpfc_cmd) {
  2502. lpfc_cmd->timeout = 60;
  2503. status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
  2504. cmnd->device->lun,
  2505. ndlp->rport->dd_data);
  2506. if (status != TIMEOUT_ERROR)
  2507. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2508. }
  2509. if (!lpfc_cmd || status != SUCCESS) {
  2510. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2511. "0700 Bus Reset on target %d failed\n",
  2512. i);
  2513. ret = FAILED;
  2514. }
  2515. }
  2516. /*
  2517. * All outstanding txcmplq I/Os should have been aborted by
  2518. * the targets. Unfortunately, some targets do not abide by
  2519. * this forcing the driver to double check.
  2520. */
  2521. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  2522. if (cnt)
  2523. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  2524. 0, 0, LPFC_CTX_HOST);
  2525. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2526. while (time_after(later, jiffies) && cnt) {
  2527. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  2528. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  2529. }
  2530. if (cnt) {
  2531. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2532. "0715 Bus Reset I/O flush failure: "
  2533. "cnt x%x left x%x\n", cnt, i);
  2534. ret = FAILED;
  2535. }
  2536. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2537. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  2538. return ret;
  2539. }
  2540. /**
  2541. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  2542. * @sdev: Pointer to scsi_device.
  2543. *
  2544. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  2545. * globally available list of scsi buffers. This routine also makes sure scsi
  2546. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  2547. * of scsi buffer exists for the lifetime of the driver.
  2548. *
  2549. * Return codes:
  2550. * non-0 - Error
  2551. * 0 - Success
  2552. **/
  2553. static int
  2554. lpfc_slave_alloc(struct scsi_device *sdev)
  2555. {
  2556. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  2557. struct lpfc_hba *phba = vport->phba;
  2558. struct lpfc_scsi_buf *scsi_buf = NULL;
  2559. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2560. uint32_t total = 0, i;
  2561. uint32_t num_to_alloc = 0;
  2562. unsigned long flags;
  2563. if (!rport || fc_remote_port_chkready(rport))
  2564. return -ENXIO;
  2565. sdev->hostdata = rport->dd_data;
  2566. /*
  2567. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  2568. * available list of scsi buffers. Don't allocate more than the
  2569. * HBA limit conveyed to the midlayer via the host structure. The
  2570. * formula accounts for the lun_queue_depth + error handlers + 1
  2571. * extra. This list of scsi bufs exists for the lifetime of the driver.
  2572. */
  2573. total = phba->total_scsi_bufs;
  2574. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  2575. /* Allow some exchanges to be available always to complete discovery */
  2576. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  2577. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2578. "0704 At limitation of %d preallocated "
  2579. "command buffers\n", total);
  2580. return 0;
  2581. /* Allow some exchanges to be available always to complete discovery */
  2582. } else if (total + num_to_alloc >
  2583. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  2584. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2585. "0705 Allocation request of %d "
  2586. "command buffers will exceed max of %d. "
  2587. "Reducing allocation request to %d.\n",
  2588. num_to_alloc, phba->cfg_hba_queue_depth,
  2589. (phba->cfg_hba_queue_depth - total));
  2590. num_to_alloc = phba->cfg_hba_queue_depth - total;
  2591. }
  2592. for (i = 0; i < num_to_alloc; i++) {
  2593. scsi_buf = lpfc_new_scsi_buf(vport);
  2594. if (!scsi_buf) {
  2595. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2596. "0706 Failed to allocate "
  2597. "command buffer\n");
  2598. break;
  2599. }
  2600. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  2601. phba->total_scsi_bufs++;
  2602. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  2603. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  2604. }
  2605. return 0;
  2606. }
  2607. /**
  2608. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  2609. * @sdev: Pointer to scsi_device.
  2610. *
  2611. * This routine configures following items
  2612. * - Tag command queuing support for @sdev if supported.
  2613. * - Dev loss time out value of fc_rport.
  2614. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  2615. *
  2616. * Return codes:
  2617. * 0 - Success
  2618. **/
  2619. static int
  2620. lpfc_slave_configure(struct scsi_device *sdev)
  2621. {
  2622. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  2623. struct lpfc_hba *phba = vport->phba;
  2624. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  2625. if (sdev->tagged_supported)
  2626. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  2627. else
  2628. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  2629. /*
  2630. * Initialize the fc transport attributes for the target
  2631. * containing this scsi device. Also note that the driver's
  2632. * target pointer is stored in the starget_data for the
  2633. * driver's sysfs entry point functions.
  2634. */
  2635. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  2636. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2637. lpfc_sli_poll_fcp_ring(phba);
  2638. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2639. lpfc_poll_rearm_timer(phba);
  2640. }
  2641. return 0;
  2642. }
  2643. /**
  2644. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  2645. * @sdev: Pointer to scsi_device.
  2646. *
  2647. * This routine sets @sdev hostatdata filed to null.
  2648. **/
  2649. static void
  2650. lpfc_slave_destroy(struct scsi_device *sdev)
  2651. {
  2652. sdev->hostdata = NULL;
  2653. return;
  2654. }
  2655. struct scsi_host_template lpfc_template = {
  2656. .module = THIS_MODULE,
  2657. .name = LPFC_DRIVER_NAME,
  2658. .info = lpfc_info,
  2659. .queuecommand = lpfc_queuecommand,
  2660. .eh_abort_handler = lpfc_abort_handler,
  2661. .eh_device_reset_handler= lpfc_device_reset_handler,
  2662. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  2663. .slave_alloc = lpfc_slave_alloc,
  2664. .slave_configure = lpfc_slave_configure,
  2665. .slave_destroy = lpfc_slave_destroy,
  2666. .scan_finished = lpfc_scan_finished,
  2667. .this_id = -1,
  2668. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  2669. .cmd_per_lun = LPFC_CMD_PER_LUN,
  2670. .use_clustering = ENABLE_CLUSTERING,
  2671. .shost_attrs = lpfc_hba_attrs,
  2672. .max_sectors = 0xFFFF,
  2673. };
  2674. struct scsi_host_template lpfc_vport_template = {
  2675. .module = THIS_MODULE,
  2676. .name = LPFC_DRIVER_NAME,
  2677. .info = lpfc_info,
  2678. .queuecommand = lpfc_queuecommand,
  2679. .eh_abort_handler = lpfc_abort_handler,
  2680. .eh_device_reset_handler= lpfc_device_reset_handler,
  2681. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  2682. .slave_alloc = lpfc_slave_alloc,
  2683. .slave_configure = lpfc_slave_configure,
  2684. .slave_destroy = lpfc_slave_destroy,
  2685. .scan_finished = lpfc_scan_finished,
  2686. .this_id = -1,
  2687. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  2688. .cmd_per_lun = LPFC_CMD_PER_LUN,
  2689. .use_clustering = ENABLE_CLUSTERING,
  2690. .shost_attrs = lpfc_vport_attrs,
  2691. .max_sectors = 0xFFFF,
  2692. };