fc_fcp.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <linux/slab.h>
  30. #include <scsi/scsi_tcq.h>
  31. #include <scsi/scsi.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <scsi/fc/fc_fc2.h>
  36. #include <scsi/libfc.h>
  37. #include <scsi/fc_encode.h>
  38. #include "fc_libfc.h"
  39. struct kmem_cache *scsi_pkt_cachep;
  40. /* SRB state definitions */
  41. #define FC_SRB_FREE 0 /* cmd is free */
  42. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  43. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  44. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  45. #define FC_SRB_ABORTED (1 << 3) /* abort acknowledged */
  46. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  47. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  48. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  49. #define FC_SRB_READ (1 << 1)
  50. #define FC_SRB_WRITE (1 << 0)
  51. /*
  52. * The SCp.ptr should be tested and set under the host lock. NULL indicates
  53. * that the command has been retruned to the scsi layer.
  54. */
  55. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  56. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  57. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  58. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  59. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  60. /**
  61. * struct fc_fcp_internal - FCP layer internal data
  62. * @scsi_pkt_pool: Memory pool to draw FCP packets from
  63. * @scsi_queue_lock: Protects the scsi_pkt_queue
  64. * @scsi_pkt_queue: Current FCP packets
  65. * @last_can_queue_ramp_down_time: ramp down time
  66. * @last_can_queue_ramp_up_time: ramp up time
  67. * @max_can_queue: max can_queue size
  68. */
  69. struct fc_fcp_internal {
  70. mempool_t *scsi_pkt_pool;
  71. spinlock_t scsi_queue_lock;
  72. struct list_head scsi_pkt_queue;
  73. unsigned long last_can_queue_ramp_down_time;
  74. unsigned long last_can_queue_ramp_up_time;
  75. int max_can_queue;
  76. };
  77. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  78. /*
  79. * function prototypes
  80. * FC scsi I/O related functions
  81. */
  82. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  83. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  84. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  85. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  86. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  87. static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
  88. static void fc_fcp_recovery(struct fc_fcp_pkt *);
  89. static void fc_fcp_timeout(unsigned long);
  90. static void fc_fcp_rec(struct fc_fcp_pkt *);
  91. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  92. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  93. static void fc_io_compl(struct fc_fcp_pkt *);
  94. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  95. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  96. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  97. /*
  98. * command status codes
  99. */
  100. #define FC_COMPLETE 0
  101. #define FC_CMD_ABORTED 1
  102. #define FC_CMD_RESET 2
  103. #define FC_CMD_PLOGO 3
  104. #define FC_SNS_RCV 4
  105. #define FC_TRANS_ERR 5
  106. #define FC_DATA_OVRRUN 6
  107. #define FC_DATA_UNDRUN 7
  108. #define FC_ERROR 8
  109. #define FC_HRD_ERROR 9
  110. #define FC_CMD_RECOVERY 10
  111. /*
  112. * Error recovery timeout values.
  113. */
  114. #define FC_SCSI_ER_TIMEOUT (10 * HZ)
  115. #define FC_SCSI_TM_TOV (10 * HZ)
  116. #define FC_SCSI_REC_TOV (2 * HZ)
  117. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  118. #define FC_CAN_QUEUE_PERIOD (60 * HZ)
  119. #define FC_MAX_ERROR_CNT 5
  120. #define FC_MAX_RECOV_RETRY 3
  121. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  122. /**
  123. * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
  124. * @lport: The local port that the FCP packet is for
  125. * @gfp: GFP flags for allocation
  126. *
  127. * Return value: fcp_pkt structure or null on allocation failure.
  128. * Context: Can be called from process context, no lock is required.
  129. */
  130. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
  131. {
  132. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  133. struct fc_fcp_pkt *fsp;
  134. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  135. if (fsp) {
  136. memset(fsp, 0, sizeof(*fsp));
  137. fsp->lp = lport;
  138. atomic_set(&fsp->ref_cnt, 1);
  139. init_timer(&fsp->timer);
  140. INIT_LIST_HEAD(&fsp->list);
  141. spin_lock_init(&fsp->scsi_pkt_lock);
  142. }
  143. return fsp;
  144. }
  145. /**
  146. * fc_fcp_pkt_release() - Release hold on a fcp_pkt
  147. * @fsp: The FCP packet to be released
  148. *
  149. * Context: Can be called from process or interrupt context,
  150. * no lock is required.
  151. */
  152. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  153. {
  154. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  155. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  156. mempool_free(fsp, si->scsi_pkt_pool);
  157. }
  158. }
  159. /**
  160. * fc_fcp_pkt_hold() - Hold a fcp_pkt
  161. * @fsp: The FCP packet to be held
  162. */
  163. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  164. {
  165. atomic_inc(&fsp->ref_cnt);
  166. }
  167. /**
  168. * fc_fcp_pkt_destory() - Release hold on a fcp_pkt
  169. * @seq: The sequence that the FCP packet is on (required by destructor API)
  170. * @fsp: The FCP packet to be released
  171. *
  172. * This routine is called by a destructor callback in the exch_seq_send()
  173. * routine of the libfc Transport Template. The 'struct fc_seq' is a required
  174. * argument even though it is not used by this routine.
  175. *
  176. * Context: No locking required.
  177. */
  178. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  179. {
  180. fc_fcp_pkt_release(fsp);
  181. }
  182. /**
  183. * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
  184. * @fsp: The FCP packet to be locked and incremented
  185. *
  186. * We should only return error if we return a command to SCSI-ml before
  187. * getting a response. This can happen in cases where we send a abort, but
  188. * do not wait for the response and the abort and command can be passing
  189. * each other on the wire/network-layer.
  190. *
  191. * Note: this function locks the packet and gets a reference to allow
  192. * callers to call the completion function while the lock is held and
  193. * not have to worry about the packets refcount.
  194. *
  195. * TODO: Maybe we should just have callers grab/release the lock and
  196. * have a function that they call to verify the fsp and grab a ref if
  197. * needed.
  198. */
  199. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  200. {
  201. spin_lock_bh(&fsp->scsi_pkt_lock);
  202. if (fsp->state & FC_SRB_COMPL) {
  203. spin_unlock_bh(&fsp->scsi_pkt_lock);
  204. return -EPERM;
  205. }
  206. fc_fcp_pkt_hold(fsp);
  207. return 0;
  208. }
  209. /**
  210. * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
  211. * reference count
  212. * @fsp: The FCP packet to be unlocked and decremented
  213. */
  214. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  215. {
  216. spin_unlock_bh(&fsp->scsi_pkt_lock);
  217. fc_fcp_pkt_release(fsp);
  218. }
  219. /**
  220. * fc_fcp_timer_set() - Start a timer for a fcp_pkt
  221. * @fsp: The FCP packet to start a timer for
  222. * @delay: The timeout period for the timer
  223. */
  224. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  225. {
  226. if (!(fsp->state & FC_SRB_COMPL))
  227. mod_timer(&fsp->timer, jiffies + delay);
  228. }
  229. /**
  230. * fc_fcp_send_abort() - Send an abort for exchanges associated with a
  231. * fcp_pkt
  232. * @fsp: The FCP packet to abort exchanges on
  233. */
  234. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  235. {
  236. if (!fsp->seq_ptr)
  237. return -EINVAL;
  238. fsp->state |= FC_SRB_ABORT_PENDING;
  239. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  240. }
  241. /**
  242. * fc_fcp_retry_cmd() - Retry a fcp_pkt
  243. * @fsp: The FCP packet to be retried
  244. *
  245. * Sets the status code to be FC_ERROR and then calls
  246. * fc_fcp_complete_locked() which in turn calls fc_io_compl().
  247. * fc_io_compl() will notify the SCSI-ml that the I/O is done.
  248. * The SCSI-ml will retry the command.
  249. */
  250. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  251. {
  252. if (fsp->seq_ptr) {
  253. fsp->lp->tt.exch_done(fsp->seq_ptr);
  254. fsp->seq_ptr = NULL;
  255. }
  256. fsp->state &= ~FC_SRB_ABORT_PENDING;
  257. fsp->io_status = 0;
  258. fsp->status_code = FC_ERROR;
  259. fc_fcp_complete_locked(fsp);
  260. }
  261. /**
  262. * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
  263. * @fsp: The FCP packet that will manage the DDP frames
  264. * @xid: The XID that will be used for the DDP exchange
  265. */
  266. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  267. {
  268. struct fc_lport *lport;
  269. lport = fsp->lp;
  270. if ((fsp->req_flags & FC_SRB_READ) &&
  271. (lport->lro_enabled) && (lport->tt.ddp_setup)) {
  272. if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
  273. scsi_sg_count(fsp->cmd)))
  274. fsp->xfer_ddp = xid;
  275. }
  276. }
  277. /**
  278. * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
  279. * DDP related resources for a fcp_pkt
  280. * @fsp: The FCP packet that DDP had been used on
  281. */
  282. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  283. {
  284. struct fc_lport *lport;
  285. if (!fsp)
  286. return;
  287. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  288. return;
  289. lport = fsp->lp;
  290. if (lport->tt.ddp_done) {
  291. fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
  292. fsp->xfer_ddp = FC_XID_UNKNOWN;
  293. }
  294. }
  295. /**
  296. * fc_fcp_can_queue_ramp_up() - increases can_queue
  297. * @lport: lport to ramp up can_queue
  298. *
  299. * Locking notes: Called with Scsi_Host lock held
  300. */
  301. static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
  302. {
  303. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  304. int can_queue;
  305. if (si->last_can_queue_ramp_up_time &&
  306. (time_before(jiffies, si->last_can_queue_ramp_up_time +
  307. FC_CAN_QUEUE_PERIOD)))
  308. return;
  309. if (time_before(jiffies, si->last_can_queue_ramp_down_time +
  310. FC_CAN_QUEUE_PERIOD))
  311. return;
  312. si->last_can_queue_ramp_up_time = jiffies;
  313. can_queue = lport->host->can_queue << 1;
  314. if (can_queue >= si->max_can_queue) {
  315. can_queue = si->max_can_queue;
  316. si->last_can_queue_ramp_down_time = 0;
  317. }
  318. lport->host->can_queue = can_queue;
  319. shost_printk(KERN_ERR, lport->host, "libfc: increased "
  320. "can_queue to %d.\n", can_queue);
  321. }
  322. /**
  323. * fc_fcp_can_queue_ramp_down() - reduces can_queue
  324. * @lport: lport to reduce can_queue
  325. *
  326. * If we are getting memory allocation failures, then we may
  327. * be trying to execute too many commands. We let the running
  328. * commands complete or timeout, then try again with a reduced
  329. * can_queue. Eventually we will hit the point where we run
  330. * on all reserved structs.
  331. *
  332. * Locking notes: Called with Scsi_Host lock held
  333. */
  334. static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
  335. {
  336. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  337. int can_queue;
  338. if (si->last_can_queue_ramp_down_time &&
  339. (time_before(jiffies, si->last_can_queue_ramp_down_time +
  340. FC_CAN_QUEUE_PERIOD)))
  341. return;
  342. si->last_can_queue_ramp_down_time = jiffies;
  343. can_queue = lport->host->can_queue;
  344. can_queue >>= 1;
  345. if (!can_queue)
  346. can_queue = 1;
  347. lport->host->can_queue = can_queue;
  348. shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
  349. "Reducing can_queue to %d.\n", can_queue);
  350. }
  351. /*
  352. * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
  353. * @lport: fc lport struct
  354. * @len: payload length
  355. *
  356. * Allocates fc_frame structure and buffer but if fails to allocate
  357. * then reduce can_queue.
  358. */
  359. static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
  360. size_t len)
  361. {
  362. struct fc_frame *fp;
  363. unsigned long flags;
  364. fp = fc_frame_alloc(lport, len);
  365. if (likely(fp))
  366. return fp;
  367. /* error case */
  368. spin_lock_irqsave(lport->host->host_lock, flags);
  369. fc_fcp_can_queue_ramp_down(lport);
  370. spin_unlock_irqrestore(lport->host->host_lock, flags);
  371. return NULL;
  372. }
  373. /**
  374. * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
  375. * @fsp: The FCP packet the data is on
  376. * @fp: The data frame
  377. */
  378. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  379. {
  380. struct scsi_cmnd *sc = fsp->cmd;
  381. struct fc_lport *lport = fsp->lp;
  382. struct fcoe_dev_stats *stats;
  383. struct fc_frame_header *fh;
  384. size_t start_offset;
  385. size_t offset;
  386. u32 crc;
  387. u32 copy_len = 0;
  388. size_t len;
  389. void *buf;
  390. struct scatterlist *sg;
  391. u32 nents;
  392. fh = fc_frame_header_get(fp);
  393. offset = ntohl(fh->fh_parm_offset);
  394. start_offset = offset;
  395. len = fr_len(fp) - sizeof(*fh);
  396. buf = fc_frame_payload_get(fp, 0);
  397. /*
  398. * if this I/O is ddped then clear it
  399. * and initiate recovery since data
  400. * frames are expected to be placed
  401. * directly in that case.
  402. */
  403. if (fsp->xfer_ddp != FC_XID_UNKNOWN) {
  404. fc_fcp_ddp_done(fsp);
  405. goto err;
  406. }
  407. if (offset + len > fsp->data_len) {
  408. /* this should never happen */
  409. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  410. fc_frame_crc_check(fp))
  411. goto crc_err;
  412. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  413. "data_len %x\n", len, offset, fsp->data_len);
  414. goto err;
  415. }
  416. if (offset != fsp->xfer_len)
  417. fsp->state |= FC_SRB_DISCONTIG;
  418. sg = scsi_sglist(sc);
  419. nents = scsi_sg_count(sc);
  420. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
  421. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  422. &offset, KM_SOFTIRQ0, NULL);
  423. } else {
  424. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  425. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  426. &offset, KM_SOFTIRQ0, &crc);
  427. buf = fc_frame_payload_get(fp, 0);
  428. if (len % 4)
  429. crc = crc32(crc, buf + len, 4 - (len % 4));
  430. if (~crc != le32_to_cpu(fr_crc(fp))) {
  431. crc_err:
  432. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  433. stats->ErrorFrames++;
  434. /* per cpu count, not total count, but OK for limit */
  435. if (stats->InvalidCRCCount++ < 5)
  436. printk(KERN_WARNING "libfc: CRC error on data "
  437. "frame for port (%6.6x)\n",
  438. lport->port_id);
  439. put_cpu();
  440. /*
  441. * Assume the frame is total garbage.
  442. * We may have copied it over the good part
  443. * of the buffer.
  444. * If so, we need to retry the entire operation.
  445. * Otherwise, ignore it.
  446. */
  447. if (fsp->state & FC_SRB_DISCONTIG)
  448. goto err;
  449. return;
  450. }
  451. }
  452. if (fsp->xfer_contig_end == start_offset)
  453. fsp->xfer_contig_end += copy_len;
  454. fsp->xfer_len += copy_len;
  455. /*
  456. * In the very rare event that this data arrived after the response
  457. * and completes the transfer, call the completion handler.
  458. */
  459. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  460. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  461. fc_fcp_complete_locked(fsp);
  462. return;
  463. err:
  464. fc_fcp_recovery(fsp);
  465. }
  466. /**
  467. * fc_fcp_send_data() - Send SCSI data to a target
  468. * @fsp: The FCP packet the data is on
  469. * @sp: The sequence the data is to be sent on
  470. * @offset: The starting offset for this data request
  471. * @seq_blen: The burst length for this data request
  472. *
  473. * Called after receiving a Transfer Ready data descriptor.
  474. * If the LLD is capable of sequence offload then send down the
  475. * seq_blen amount of data in single frame, otherwise send
  476. * multiple frames of the maximum frame payload supported by
  477. * the target port.
  478. */
  479. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  480. size_t offset, size_t seq_blen)
  481. {
  482. struct fc_exch *ep;
  483. struct scsi_cmnd *sc;
  484. struct scatterlist *sg;
  485. struct fc_frame *fp = NULL;
  486. struct fc_lport *lport = fsp->lp;
  487. struct page *page;
  488. size_t remaining;
  489. size_t t_blen;
  490. size_t tlen;
  491. size_t sg_bytes;
  492. size_t frame_offset, fh_parm_offset;
  493. size_t off;
  494. int error;
  495. void *data = NULL;
  496. void *page_addr;
  497. int using_sg = lport->sg_supp;
  498. u32 f_ctl;
  499. WARN_ON(seq_blen <= 0);
  500. if (unlikely(offset + seq_blen > fsp->data_len)) {
  501. /* this should never happen */
  502. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  503. "offset %zx\n", seq_blen, offset);
  504. fc_fcp_send_abort(fsp);
  505. return 0;
  506. } else if (offset != fsp->xfer_len) {
  507. /* Out of Order Data Request - no problem, but unexpected. */
  508. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  509. "seq_blen %zx offset %zx\n", seq_blen, offset);
  510. }
  511. /*
  512. * if LLD is capable of seq_offload then set transport
  513. * burst length (t_blen) to seq_blen, otherwise set t_blen
  514. * to max FC frame payload previously set in fsp->max_payload.
  515. */
  516. t_blen = fsp->max_payload;
  517. if (lport->seq_offload) {
  518. t_blen = min(seq_blen, (size_t)lport->lso_max);
  519. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  520. fsp, seq_blen, lport->lso_max, t_blen);
  521. }
  522. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
  523. if (t_blen > 512)
  524. t_blen &= ~(512 - 1); /* round down to block size */
  525. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
  526. sc = fsp->cmd;
  527. remaining = seq_blen;
  528. fh_parm_offset = frame_offset = offset;
  529. tlen = 0;
  530. seq = lport->tt.seq_start_next(seq);
  531. f_ctl = FC_FC_REL_OFF;
  532. WARN_ON(!seq);
  533. sg = scsi_sglist(sc);
  534. while (remaining > 0 && sg) {
  535. if (offset >= sg->length) {
  536. offset -= sg->length;
  537. sg = sg_next(sg);
  538. continue;
  539. }
  540. if (!fp) {
  541. tlen = min(t_blen, remaining);
  542. /*
  543. * TODO. Temporary workaround. fc_seq_send() can't
  544. * handle odd lengths in non-linear skbs.
  545. * This will be the final fragment only.
  546. */
  547. if (tlen % 4)
  548. using_sg = 0;
  549. fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
  550. if (!fp)
  551. return -ENOMEM;
  552. data = fc_frame_header_get(fp) + 1;
  553. fh_parm_offset = frame_offset;
  554. fr_max_payload(fp) = fsp->max_payload;
  555. }
  556. off = offset + sg->offset;
  557. sg_bytes = min(tlen, sg->length - offset);
  558. sg_bytes = min(sg_bytes,
  559. (size_t) (PAGE_SIZE - (off & ~PAGE_MASK)));
  560. page = sg_page(sg) + (off >> PAGE_SHIFT);
  561. if (using_sg) {
  562. get_page(page);
  563. skb_fill_page_desc(fp_skb(fp),
  564. skb_shinfo(fp_skb(fp))->nr_frags,
  565. page, off & ~PAGE_MASK, sg_bytes);
  566. fp_skb(fp)->data_len += sg_bytes;
  567. fr_len(fp) += sg_bytes;
  568. fp_skb(fp)->truesize += PAGE_SIZE;
  569. } else {
  570. /*
  571. * The scatterlist item may be bigger than PAGE_SIZE,
  572. * but we must not cross pages inside the kmap.
  573. */
  574. page_addr = kmap_atomic(page, KM_SOFTIRQ0);
  575. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  576. sg_bytes);
  577. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  578. data += sg_bytes;
  579. }
  580. offset += sg_bytes;
  581. frame_offset += sg_bytes;
  582. tlen -= sg_bytes;
  583. remaining -= sg_bytes;
  584. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  585. (tlen))
  586. continue;
  587. /*
  588. * Send sequence with transfer sequence initiative in case
  589. * this is last FCP frame of the sequence.
  590. */
  591. if (remaining == 0)
  592. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  593. ep = fc_seq_exch(seq);
  594. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  595. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  596. /*
  597. * send fragment using for a sequence.
  598. */
  599. error = lport->tt.seq_send(lport, seq, fp);
  600. if (error) {
  601. WARN_ON(1); /* send error should be rare */
  602. fc_fcp_retry_cmd(fsp);
  603. return 0;
  604. }
  605. fp = NULL;
  606. }
  607. fsp->xfer_len += seq_blen; /* premature count? */
  608. return 0;
  609. }
  610. /**
  611. * fc_fcp_abts_resp() - Send an ABTS response
  612. * @fsp: The FCP packet that is being aborted
  613. * @fp: The response frame
  614. */
  615. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  616. {
  617. int ba_done = 1;
  618. struct fc_ba_rjt *brp;
  619. struct fc_frame_header *fh;
  620. fh = fc_frame_header_get(fp);
  621. switch (fh->fh_r_ctl) {
  622. case FC_RCTL_BA_ACC:
  623. break;
  624. case FC_RCTL_BA_RJT:
  625. brp = fc_frame_payload_get(fp, sizeof(*brp));
  626. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  627. break;
  628. /* fall thru */
  629. default:
  630. /*
  631. * we will let the command timeout
  632. * and scsi-ml recover in this case,
  633. * therefore cleared the ba_done flag.
  634. */
  635. ba_done = 0;
  636. }
  637. if (ba_done) {
  638. fsp->state |= FC_SRB_ABORTED;
  639. fsp->state &= ~FC_SRB_ABORT_PENDING;
  640. if (fsp->wait_for_comp)
  641. complete(&fsp->tm_done);
  642. else
  643. fc_fcp_complete_locked(fsp);
  644. }
  645. }
  646. /**
  647. * fc_fcp_recv() - Reveive an FCP frame
  648. * @seq: The sequence the frame is on
  649. * @fp: The received frame
  650. * @arg: The related FCP packet
  651. *
  652. * Context: Called from Soft IRQ context. Can not be called
  653. * holding the FCP packet list lock.
  654. */
  655. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  656. {
  657. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  658. struct fc_lport *lport = fsp->lp;
  659. struct fc_frame_header *fh;
  660. struct fcp_txrdy *dd;
  661. u8 r_ctl;
  662. int rc = 0;
  663. if (IS_ERR(fp)) {
  664. fc_fcp_error(fsp, fp);
  665. return;
  666. }
  667. fh = fc_frame_header_get(fp);
  668. r_ctl = fh->fh_r_ctl;
  669. if (!(lport->state & LPORT_ST_READY))
  670. goto out;
  671. if (fc_fcp_lock_pkt(fsp))
  672. goto out;
  673. fsp->last_pkt_time = jiffies;
  674. if (fh->fh_type == FC_TYPE_BLS) {
  675. fc_fcp_abts_resp(fsp, fp);
  676. goto unlock;
  677. }
  678. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  679. goto unlock;
  680. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  681. /*
  682. * received XFER RDY from the target
  683. * need to send data to the target
  684. */
  685. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  686. dd = fc_frame_payload_get(fp, sizeof(*dd));
  687. WARN_ON(!dd);
  688. rc = fc_fcp_send_data(fsp, seq,
  689. (size_t) ntohl(dd->ft_data_ro),
  690. (size_t) ntohl(dd->ft_burst_len));
  691. if (!rc)
  692. seq->rec_data = fsp->xfer_len;
  693. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  694. /*
  695. * received a DATA frame
  696. * next we will copy the data to the system buffer
  697. */
  698. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  699. fc_fcp_recv_data(fsp, fp);
  700. seq->rec_data = fsp->xfer_contig_end;
  701. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  702. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  703. fc_fcp_resp(fsp, fp);
  704. } else {
  705. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  706. }
  707. unlock:
  708. fc_fcp_unlock_pkt(fsp);
  709. out:
  710. fc_frame_free(fp);
  711. }
  712. /**
  713. * fc_fcp_resp() - Handler for FCP responses
  714. * @fsp: The FCP packet the response is for
  715. * @fp: The response frame
  716. */
  717. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  718. {
  719. struct fc_frame_header *fh;
  720. struct fcp_resp *fc_rp;
  721. struct fcp_resp_ext *rp_ex;
  722. struct fcp_resp_rsp_info *fc_rp_info;
  723. u32 plen;
  724. u32 expected_len;
  725. u32 respl = 0;
  726. u32 snsl = 0;
  727. u8 flags = 0;
  728. plen = fr_len(fp);
  729. fh = (struct fc_frame_header *)fr_hdr(fp);
  730. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  731. goto len_err;
  732. plen -= sizeof(*fh);
  733. fc_rp = (struct fcp_resp *)(fh + 1);
  734. fsp->cdb_status = fc_rp->fr_status;
  735. flags = fc_rp->fr_flags;
  736. fsp->scsi_comp_flags = flags;
  737. expected_len = fsp->data_len;
  738. /* if ddp, update xfer len */
  739. fc_fcp_ddp_done(fsp);
  740. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  741. rp_ex = (void *)(fc_rp + 1);
  742. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  743. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  744. goto len_err;
  745. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  746. if (flags & FCP_RSP_LEN_VAL) {
  747. respl = ntohl(rp_ex->fr_rsp_len);
  748. if (respl != sizeof(*fc_rp_info))
  749. goto len_err;
  750. if (fsp->wait_for_comp) {
  751. /* Abuse cdb_status for rsp code */
  752. fsp->cdb_status = fc_rp_info->rsp_code;
  753. complete(&fsp->tm_done);
  754. /*
  755. * tmfs will not have any scsi cmd so
  756. * exit here
  757. */
  758. return;
  759. }
  760. }
  761. if (flags & FCP_SNS_LEN_VAL) {
  762. snsl = ntohl(rp_ex->fr_sns_len);
  763. if (snsl > SCSI_SENSE_BUFFERSIZE)
  764. snsl = SCSI_SENSE_BUFFERSIZE;
  765. memcpy(fsp->cmd->sense_buffer,
  766. (char *)fc_rp_info + respl, snsl);
  767. }
  768. }
  769. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  770. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  771. goto len_err;
  772. if (flags & FCP_RESID_UNDER) {
  773. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  774. /*
  775. * The cmnd->underflow is the minimum number of
  776. * bytes that must be transfered for this
  777. * command. Provided a sense condition is not
  778. * present, make sure the actual amount
  779. * transferred is at least the underflow value
  780. * or fail.
  781. */
  782. if (!(flags & FCP_SNS_LEN_VAL) &&
  783. (fc_rp->fr_status == 0) &&
  784. (scsi_bufflen(fsp->cmd) -
  785. fsp->scsi_resid) < fsp->cmd->underflow)
  786. goto err;
  787. expected_len -= fsp->scsi_resid;
  788. } else {
  789. fsp->status_code = FC_ERROR;
  790. }
  791. }
  792. }
  793. fsp->state |= FC_SRB_RCV_STATUS;
  794. /*
  795. * Check for missing or extra data frames.
  796. */
  797. if (unlikely(fsp->xfer_len != expected_len)) {
  798. if (fsp->xfer_len < expected_len) {
  799. /*
  800. * Some data may be queued locally,
  801. * Wait a at least one jiffy to see if it is delivered.
  802. * If this expires without data, we may do SRR.
  803. */
  804. fc_fcp_timer_set(fsp, 2);
  805. return;
  806. }
  807. fsp->status_code = FC_DATA_OVRRUN;
  808. FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, "
  809. "len %x, data len %x\n",
  810. fsp->rport->port_id,
  811. fsp->xfer_len, expected_len, fsp->data_len);
  812. }
  813. fc_fcp_complete_locked(fsp);
  814. return;
  815. len_err:
  816. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  817. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  818. err:
  819. fsp->status_code = FC_ERROR;
  820. fc_fcp_complete_locked(fsp);
  821. }
  822. /**
  823. * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
  824. * fcp_pkt lock held
  825. * @fsp: The FCP packet to be completed
  826. *
  827. * This function may sleep if a timer is pending. The packet lock must be
  828. * held, and the host lock must not be held.
  829. */
  830. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  831. {
  832. struct fc_lport *lport = fsp->lp;
  833. struct fc_seq *seq;
  834. struct fc_exch *ep;
  835. u32 f_ctl;
  836. if (fsp->state & FC_SRB_ABORT_PENDING)
  837. return;
  838. if (fsp->state & FC_SRB_ABORTED) {
  839. if (!fsp->status_code)
  840. fsp->status_code = FC_CMD_ABORTED;
  841. } else {
  842. /*
  843. * Test for transport underrun, independent of response
  844. * underrun status.
  845. */
  846. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  847. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  848. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  849. fsp->status_code = FC_DATA_UNDRUN;
  850. fsp->io_status = 0;
  851. }
  852. }
  853. seq = fsp->seq_ptr;
  854. if (seq) {
  855. fsp->seq_ptr = NULL;
  856. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  857. struct fc_frame *conf_frame;
  858. struct fc_seq *csp;
  859. csp = lport->tt.seq_start_next(seq);
  860. conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
  861. if (conf_frame) {
  862. f_ctl = FC_FC_SEQ_INIT;
  863. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  864. ep = fc_seq_exch(seq);
  865. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  866. ep->did, ep->sid,
  867. FC_TYPE_FCP, f_ctl, 0);
  868. lport->tt.seq_send(lport, csp, conf_frame);
  869. }
  870. }
  871. lport->tt.exch_done(seq);
  872. }
  873. fc_io_compl(fsp);
  874. }
  875. /**
  876. * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
  877. * @fsp: The FCP packet whose exchanges should be canceled
  878. * @error: The reason for the cancellation
  879. */
  880. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  881. {
  882. struct fc_lport *lport = fsp->lp;
  883. if (fsp->seq_ptr) {
  884. lport->tt.exch_done(fsp->seq_ptr);
  885. fsp->seq_ptr = NULL;
  886. }
  887. fsp->status_code = error;
  888. }
  889. /**
  890. * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
  891. * @lport: The local port whose exchanges should be canceled
  892. * @id: The target's ID
  893. * @lun: The LUN
  894. * @error: The reason for cancellation
  895. *
  896. * If lun or id is -1, they are ignored.
  897. */
  898. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
  899. unsigned int lun, int error)
  900. {
  901. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  902. struct fc_fcp_pkt *fsp;
  903. struct scsi_cmnd *sc_cmd;
  904. unsigned long flags;
  905. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  906. restart:
  907. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  908. sc_cmd = fsp->cmd;
  909. if (id != -1 && scmd_id(sc_cmd) != id)
  910. continue;
  911. if (lun != -1 && sc_cmd->device->lun != lun)
  912. continue;
  913. fc_fcp_pkt_hold(fsp);
  914. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  915. if (!fc_fcp_lock_pkt(fsp)) {
  916. fc_fcp_cleanup_cmd(fsp, error);
  917. fc_io_compl(fsp);
  918. fc_fcp_unlock_pkt(fsp);
  919. }
  920. fc_fcp_pkt_release(fsp);
  921. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  922. /*
  923. * while we dropped the lock multiple pkts could
  924. * have been released, so we have to start over.
  925. */
  926. goto restart;
  927. }
  928. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  929. }
  930. /**
  931. * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
  932. * @lport: The local port whose exchanges are to be aborted
  933. */
  934. static void fc_fcp_abort_io(struct fc_lport *lport)
  935. {
  936. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
  937. }
  938. /**
  939. * fc_fcp_pkt_send() - Send a fcp_pkt
  940. * @lport: The local port to send the FCP packet on
  941. * @fsp: The FCP packet to send
  942. *
  943. * Return: Zero for success and -1 for failure
  944. * Locks: Called without locks held
  945. */
  946. static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
  947. {
  948. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  949. unsigned long flags;
  950. int rc;
  951. fsp->cmd->SCp.ptr = (char *)fsp;
  952. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  953. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  954. int_to_scsilun(fsp->cmd->device->lun,
  955. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  956. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  957. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  958. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  959. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  960. rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
  961. if (unlikely(rc)) {
  962. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  963. list_del(&fsp->list);
  964. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  965. }
  966. return rc;
  967. }
  968. /**
  969. * fc_fcp_cmd_send() - Send a FCP command
  970. * @lport: The local port to send the command on
  971. * @fsp: The FCP packet the command is on
  972. * @resp: The handler for the response
  973. */
  974. static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  975. void (*resp)(struct fc_seq *,
  976. struct fc_frame *fp,
  977. void *arg))
  978. {
  979. struct fc_frame *fp;
  980. struct fc_seq *seq;
  981. struct fc_rport *rport;
  982. struct fc_rport_libfc_priv *rpriv;
  983. const size_t len = sizeof(fsp->cdb_cmd);
  984. int rc = 0;
  985. if (fc_fcp_lock_pkt(fsp))
  986. return 0;
  987. fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
  988. if (!fp) {
  989. rc = -1;
  990. goto unlock;
  991. }
  992. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  993. fr_fsp(fp) = fsp;
  994. rport = fsp->rport;
  995. fsp->max_payload = rport->maxframe_size;
  996. rpriv = rport->dd_data;
  997. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  998. rpriv->local_port->port_id, FC_TYPE_FCP,
  999. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1000. seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
  1001. fsp, 0);
  1002. if (!seq) {
  1003. rc = -1;
  1004. goto unlock;
  1005. }
  1006. fsp->last_pkt_time = jiffies;
  1007. fsp->seq_ptr = seq;
  1008. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  1009. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  1010. fc_fcp_timer_set(fsp,
  1011. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  1012. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  1013. unlock:
  1014. fc_fcp_unlock_pkt(fsp);
  1015. return rc;
  1016. }
  1017. /**
  1018. * fc_fcp_error() - Handler for FCP layer errors
  1019. * @fsp: The FCP packet the error is on
  1020. * @fp: The frame that has errored
  1021. */
  1022. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1023. {
  1024. int error = PTR_ERR(fp);
  1025. if (fc_fcp_lock_pkt(fsp))
  1026. return;
  1027. if (error == -FC_EX_CLOSED) {
  1028. fc_fcp_retry_cmd(fsp);
  1029. goto unlock;
  1030. }
  1031. /*
  1032. * clear abort pending, because the lower layer
  1033. * decided to force completion.
  1034. */
  1035. fsp->state &= ~FC_SRB_ABORT_PENDING;
  1036. fsp->status_code = FC_CMD_PLOGO;
  1037. fc_fcp_complete_locked(fsp);
  1038. unlock:
  1039. fc_fcp_unlock_pkt(fsp);
  1040. }
  1041. /**
  1042. * fc_fcp_pkt_abort() - Abort a fcp_pkt
  1043. * @fsp: The FCP packet to abort on
  1044. *
  1045. * Called to send an abort and then wait for abort completion
  1046. */
  1047. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  1048. {
  1049. int rc = FAILED;
  1050. if (fc_fcp_send_abort(fsp))
  1051. return FAILED;
  1052. init_completion(&fsp->tm_done);
  1053. fsp->wait_for_comp = 1;
  1054. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1055. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1056. spin_lock_bh(&fsp->scsi_pkt_lock);
  1057. fsp->wait_for_comp = 0;
  1058. if (!rc) {
  1059. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  1060. rc = FAILED;
  1061. } else if (fsp->state & FC_SRB_ABORTED) {
  1062. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1063. rc = SUCCESS;
  1064. fc_fcp_complete_locked(fsp);
  1065. }
  1066. return rc;
  1067. }
  1068. /**
  1069. * fc_lun_reset_send() - Send LUN reset command
  1070. * @data: The FCP packet that identifies the LUN to be reset
  1071. */
  1072. static void fc_lun_reset_send(unsigned long data)
  1073. {
  1074. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1075. struct fc_lport *lport = fsp->lp;
  1076. if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
  1077. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1078. return;
  1079. if (fc_fcp_lock_pkt(fsp))
  1080. return;
  1081. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1082. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1083. fc_fcp_unlock_pkt(fsp);
  1084. }
  1085. }
  1086. /**
  1087. * fc_lun_reset() - Send a LUN RESET command to a device
  1088. * and wait for the reply
  1089. * @lport: The local port to sent the comand on
  1090. * @fsp: The FCP packet that identifies the LUN to be reset
  1091. * @id: The SCSI command ID
  1092. * @lun: The LUN ID to be reset
  1093. */
  1094. static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1095. unsigned int id, unsigned int lun)
  1096. {
  1097. int rc;
  1098. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1099. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1100. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1101. fsp->wait_for_comp = 1;
  1102. init_completion(&fsp->tm_done);
  1103. fc_lun_reset_send((unsigned long)fsp);
  1104. /*
  1105. * wait for completion of reset
  1106. * after that make sure all commands are terminated
  1107. */
  1108. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1109. spin_lock_bh(&fsp->scsi_pkt_lock);
  1110. fsp->state |= FC_SRB_COMPL;
  1111. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1112. del_timer_sync(&fsp->timer);
  1113. spin_lock_bh(&fsp->scsi_pkt_lock);
  1114. if (fsp->seq_ptr) {
  1115. lport->tt.exch_done(fsp->seq_ptr);
  1116. fsp->seq_ptr = NULL;
  1117. }
  1118. fsp->wait_for_comp = 0;
  1119. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1120. if (!rc) {
  1121. FC_SCSI_DBG(lport, "lun reset failed\n");
  1122. return FAILED;
  1123. }
  1124. /* cdb_status holds the tmf's rsp code */
  1125. if (fsp->cdb_status != FCP_TMF_CMPL)
  1126. return FAILED;
  1127. FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
  1128. fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
  1129. return SUCCESS;
  1130. }
  1131. /**
  1132. * fc_tm_done() - Task Managment response handler
  1133. * @seq: The sequence that the response is on
  1134. * @fp: The response frame
  1135. * @arg: The FCP packet the response is for
  1136. */
  1137. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1138. {
  1139. struct fc_fcp_pkt *fsp = arg;
  1140. struct fc_frame_header *fh;
  1141. if (IS_ERR(fp)) {
  1142. /*
  1143. * If there is an error just let it timeout or wait
  1144. * for TMF to be aborted if it timedout.
  1145. *
  1146. * scsi-eh will escalate for when either happens.
  1147. */
  1148. return;
  1149. }
  1150. if (fc_fcp_lock_pkt(fsp))
  1151. return;
  1152. /*
  1153. * raced with eh timeout handler.
  1154. */
  1155. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1156. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1157. return;
  1158. }
  1159. fh = fc_frame_header_get(fp);
  1160. if (fh->fh_type != FC_TYPE_BLS)
  1161. fc_fcp_resp(fsp, fp);
  1162. fsp->seq_ptr = NULL;
  1163. fsp->lp->tt.exch_done(seq);
  1164. fc_frame_free(fp);
  1165. fc_fcp_unlock_pkt(fsp);
  1166. }
  1167. /**
  1168. * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
  1169. * @lport: The local port to be cleaned up
  1170. */
  1171. static void fc_fcp_cleanup(struct fc_lport *lport)
  1172. {
  1173. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
  1174. }
  1175. /**
  1176. * fc_fcp_timeout() - Handler for fcp_pkt timeouts
  1177. * @data: The FCP packet that has timed out
  1178. *
  1179. * If REC is supported then just issue it and return. The REC exchange will
  1180. * complete or time out and recovery can continue at that point. Otherwise,
  1181. * if the response has been received without all the data it has been
  1182. * ER_TIMEOUT since the response was received. If the response has not been
  1183. * received we see if data was received recently. If it has been then we
  1184. * continue waiting, otherwise, we abort the command.
  1185. */
  1186. static void fc_fcp_timeout(unsigned long data)
  1187. {
  1188. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1189. struct fc_rport *rport = fsp->rport;
  1190. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1191. if (fc_fcp_lock_pkt(fsp))
  1192. return;
  1193. if (fsp->cdb_cmd.fc_tm_flags)
  1194. goto unlock;
  1195. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1196. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1197. fc_fcp_rec(fsp);
  1198. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1199. jiffies))
  1200. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1201. else if (fsp->state & FC_SRB_RCV_STATUS)
  1202. fc_fcp_complete_locked(fsp);
  1203. else
  1204. fc_fcp_recovery(fsp);
  1205. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1206. unlock:
  1207. fc_fcp_unlock_pkt(fsp);
  1208. }
  1209. /**
  1210. * fc_fcp_rec() - Send a REC ELS request
  1211. * @fsp: The FCP packet to send the REC request on
  1212. */
  1213. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1214. {
  1215. struct fc_lport *lport;
  1216. struct fc_frame *fp;
  1217. struct fc_rport *rport;
  1218. struct fc_rport_libfc_priv *rpriv;
  1219. lport = fsp->lp;
  1220. rport = fsp->rport;
  1221. rpriv = rport->dd_data;
  1222. if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
  1223. fsp->status_code = FC_HRD_ERROR;
  1224. fsp->io_status = 0;
  1225. fc_fcp_complete_locked(fsp);
  1226. return;
  1227. }
  1228. fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
  1229. if (!fp)
  1230. goto retry;
  1231. fr_seq(fp) = fsp->seq_ptr;
  1232. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1233. rpriv->local_port->port_id, FC_TYPE_ELS,
  1234. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1235. if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
  1236. fc_fcp_rec_resp, fsp,
  1237. jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1238. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1239. return;
  1240. }
  1241. retry:
  1242. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1243. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1244. else
  1245. fc_fcp_recovery(fsp);
  1246. }
  1247. /**
  1248. * fc_fcp_rec_resp() - Handler for REC ELS responses
  1249. * @seq: The sequence the response is on
  1250. * @fp: The response frame
  1251. * @arg: The FCP packet the response is on
  1252. *
  1253. * If the response is a reject then the scsi layer will handle
  1254. * the timeout. If the response is a LS_ACC then if the I/O was not completed
  1255. * set the timeout and return. If the I/O was completed then complete the
  1256. * exchange and tell the SCSI layer.
  1257. */
  1258. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1259. {
  1260. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1261. struct fc_els_rec_acc *recp;
  1262. struct fc_els_ls_rjt *rjt;
  1263. u32 e_stat;
  1264. u8 opcode;
  1265. u32 offset;
  1266. enum dma_data_direction data_dir;
  1267. enum fc_rctl r_ctl;
  1268. struct fc_rport_libfc_priv *rpriv;
  1269. if (IS_ERR(fp)) {
  1270. fc_fcp_rec_error(fsp, fp);
  1271. return;
  1272. }
  1273. if (fc_fcp_lock_pkt(fsp))
  1274. goto out;
  1275. fsp->recov_retry = 0;
  1276. opcode = fc_frame_payload_op(fp);
  1277. if (opcode == ELS_LS_RJT) {
  1278. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1279. switch (rjt->er_reason) {
  1280. default:
  1281. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1282. "reason %d expl %d\n",
  1283. fsp->rport->port_id, rjt->er_reason,
  1284. rjt->er_explan);
  1285. /* fall through */
  1286. case ELS_RJT_UNSUP:
  1287. FC_FCP_DBG(fsp, "device does not support REC\n");
  1288. rpriv = fsp->rport->dd_data;
  1289. /*
  1290. * if we do not spport RECs or got some bogus
  1291. * reason then resetup timer so we check for
  1292. * making progress.
  1293. */
  1294. rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1295. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1296. break;
  1297. case ELS_RJT_LOGIC:
  1298. case ELS_RJT_UNAB:
  1299. /*
  1300. * If no data transfer, the command frame got dropped
  1301. * so we just retry. If data was transferred, we
  1302. * lost the response but the target has no record,
  1303. * so we abort and retry.
  1304. */
  1305. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1306. fsp->xfer_len == 0) {
  1307. fc_fcp_retry_cmd(fsp);
  1308. break;
  1309. }
  1310. fc_fcp_recovery(fsp);
  1311. break;
  1312. }
  1313. } else if (opcode == ELS_LS_ACC) {
  1314. if (fsp->state & FC_SRB_ABORTED)
  1315. goto unlock_out;
  1316. data_dir = fsp->cmd->sc_data_direction;
  1317. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1318. offset = ntohl(recp->reca_fc4value);
  1319. e_stat = ntohl(recp->reca_e_stat);
  1320. if (e_stat & ESB_ST_COMPLETE) {
  1321. /*
  1322. * The exchange is complete.
  1323. *
  1324. * For output, we must've lost the response.
  1325. * For input, all data must've been sent.
  1326. * We lost may have lost the response
  1327. * (and a confirmation was requested) and maybe
  1328. * some data.
  1329. *
  1330. * If all data received, send SRR
  1331. * asking for response. If partial data received,
  1332. * or gaps, SRR requests data at start of gap.
  1333. * Recovery via SRR relies on in-order-delivery.
  1334. */
  1335. if (data_dir == DMA_TO_DEVICE) {
  1336. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1337. } else if (fsp->xfer_contig_end == offset) {
  1338. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1339. } else {
  1340. offset = fsp->xfer_contig_end;
  1341. r_ctl = FC_RCTL_DD_SOL_DATA;
  1342. }
  1343. fc_fcp_srr(fsp, r_ctl, offset);
  1344. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1345. /*
  1346. * The remote port has the initiative, so just
  1347. * keep waiting for it to complete.
  1348. */
  1349. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1350. } else {
  1351. /*
  1352. * The exchange is incomplete, we have seq. initiative.
  1353. * Lost response with requested confirmation,
  1354. * lost confirmation, lost transfer ready or
  1355. * lost write data.
  1356. *
  1357. * For output, if not all data was received, ask
  1358. * for transfer ready to be repeated.
  1359. *
  1360. * If we received or sent all the data, send SRR to
  1361. * request response.
  1362. *
  1363. * If we lost a response, we may have lost some read
  1364. * data as well.
  1365. */
  1366. r_ctl = FC_RCTL_DD_SOL_DATA;
  1367. if (data_dir == DMA_TO_DEVICE) {
  1368. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1369. if (offset < fsp->data_len)
  1370. r_ctl = FC_RCTL_DD_DATA_DESC;
  1371. } else if (offset == fsp->xfer_contig_end) {
  1372. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1373. } else if (fsp->xfer_contig_end < offset) {
  1374. offset = fsp->xfer_contig_end;
  1375. }
  1376. fc_fcp_srr(fsp, r_ctl, offset);
  1377. }
  1378. }
  1379. unlock_out:
  1380. fc_fcp_unlock_pkt(fsp);
  1381. out:
  1382. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1383. fc_frame_free(fp);
  1384. }
  1385. /**
  1386. * fc_fcp_rec_error() - Handler for REC errors
  1387. * @fsp: The FCP packet the error is on
  1388. * @fp: The REC frame
  1389. */
  1390. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1391. {
  1392. int error = PTR_ERR(fp);
  1393. if (fc_fcp_lock_pkt(fsp))
  1394. goto out;
  1395. switch (error) {
  1396. case -FC_EX_CLOSED:
  1397. fc_fcp_retry_cmd(fsp);
  1398. break;
  1399. default:
  1400. FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n",
  1401. fsp, fsp->rport->port_id, error);
  1402. fsp->status_code = FC_CMD_PLOGO;
  1403. /* fall through */
  1404. case -FC_EX_TIMEOUT:
  1405. /*
  1406. * Assume REC or LS_ACC was lost.
  1407. * The exchange manager will have aborted REC, so retry.
  1408. */
  1409. FC_FCP_DBG(fsp, "REC fid %6.6x error error %d retry %d/%d\n",
  1410. fsp->rport->port_id, error, fsp->recov_retry,
  1411. FC_MAX_RECOV_RETRY);
  1412. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1413. fc_fcp_rec(fsp);
  1414. else
  1415. fc_fcp_recovery(fsp);
  1416. break;
  1417. }
  1418. fc_fcp_unlock_pkt(fsp);
  1419. out:
  1420. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1421. }
  1422. /**
  1423. * fc_fcp_recovery() - Handler for fcp_pkt recovery
  1424. * @fsp: The FCP pkt that needs to be aborted
  1425. */
  1426. static void fc_fcp_recovery(struct fc_fcp_pkt *fsp)
  1427. {
  1428. fsp->status_code = FC_CMD_RECOVERY;
  1429. fsp->cdb_status = 0;
  1430. fsp->io_status = 0;
  1431. /*
  1432. * if this fails then we let the scsi command timer fire and
  1433. * scsi-ml escalate.
  1434. */
  1435. fc_fcp_send_abort(fsp);
  1436. }
  1437. /**
  1438. * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
  1439. * @fsp: The FCP packet the SRR is to be sent on
  1440. * @r_ctl: The R_CTL field for the SRR request
  1441. * This is called after receiving status but insufficient data, or
  1442. * when expecting status but the request has timed out.
  1443. */
  1444. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1445. {
  1446. struct fc_lport *lport = fsp->lp;
  1447. struct fc_rport *rport;
  1448. struct fc_rport_libfc_priv *rpriv;
  1449. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1450. struct fc_seq *seq;
  1451. struct fcp_srr *srr;
  1452. struct fc_frame *fp;
  1453. u8 cdb_op;
  1454. rport = fsp->rport;
  1455. rpriv = rport->dd_data;
  1456. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1457. if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
  1458. rpriv->rp_state != RPORT_ST_READY)
  1459. goto retry; /* shouldn't happen */
  1460. fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
  1461. if (!fp)
  1462. goto retry;
  1463. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1464. memset(srr, 0, sizeof(*srr));
  1465. srr->srr_op = ELS_SRR;
  1466. srr->srr_ox_id = htons(ep->oxid);
  1467. srr->srr_rx_id = htons(ep->rxid);
  1468. srr->srr_r_ctl = r_ctl;
  1469. srr->srr_rel_off = htonl(offset);
  1470. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1471. rpriv->local_port->port_id, FC_TYPE_FCP,
  1472. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1473. seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
  1474. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1475. if (!seq)
  1476. goto retry;
  1477. fsp->recov_seq = seq;
  1478. fsp->xfer_len = offset;
  1479. fsp->xfer_contig_end = offset;
  1480. fsp->state &= ~FC_SRB_RCV_STATUS;
  1481. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1482. return;
  1483. retry:
  1484. fc_fcp_retry_cmd(fsp);
  1485. }
  1486. /**
  1487. * fc_fcp_srr_resp() - Handler for SRR response
  1488. * @seq: The sequence the SRR is on
  1489. * @fp: The SRR frame
  1490. * @arg: The FCP packet the SRR is on
  1491. */
  1492. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1493. {
  1494. struct fc_fcp_pkt *fsp = arg;
  1495. struct fc_frame_header *fh;
  1496. if (IS_ERR(fp)) {
  1497. fc_fcp_srr_error(fsp, fp);
  1498. return;
  1499. }
  1500. if (fc_fcp_lock_pkt(fsp))
  1501. goto out;
  1502. fh = fc_frame_header_get(fp);
  1503. /*
  1504. * BUG? fc_fcp_srr_error calls exch_done which would release
  1505. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1506. * then fc_exch_timeout would be sending an abort. The exch_done
  1507. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1508. * an abort response though.
  1509. */
  1510. if (fh->fh_type == FC_TYPE_BLS) {
  1511. fc_fcp_unlock_pkt(fsp);
  1512. return;
  1513. }
  1514. fsp->recov_seq = NULL;
  1515. switch (fc_frame_payload_op(fp)) {
  1516. case ELS_LS_ACC:
  1517. fsp->recov_retry = 0;
  1518. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1519. break;
  1520. case ELS_LS_RJT:
  1521. default:
  1522. fc_fcp_recovery(fsp);
  1523. break;
  1524. }
  1525. fc_fcp_unlock_pkt(fsp);
  1526. fsp->lp->tt.exch_done(seq);
  1527. out:
  1528. fc_frame_free(fp);
  1529. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1530. }
  1531. /**
  1532. * fc_fcp_srr_error() - Handler for SRR errors
  1533. * @fsp: The FCP packet that the SRR error is on
  1534. * @fp: The SRR frame
  1535. */
  1536. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1537. {
  1538. if (fc_fcp_lock_pkt(fsp))
  1539. goto out;
  1540. fsp->lp->tt.exch_done(fsp->recov_seq);
  1541. fsp->recov_seq = NULL;
  1542. switch (PTR_ERR(fp)) {
  1543. case -FC_EX_TIMEOUT:
  1544. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1545. fc_fcp_rec(fsp);
  1546. else
  1547. fc_fcp_recovery(fsp);
  1548. break;
  1549. case -FC_EX_CLOSED: /* e.g., link failure */
  1550. /* fall through */
  1551. default:
  1552. fc_fcp_retry_cmd(fsp);
  1553. break;
  1554. }
  1555. fc_fcp_unlock_pkt(fsp);
  1556. out:
  1557. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1558. }
  1559. /**
  1560. * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
  1561. * @lport: The local port to be checked
  1562. */
  1563. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
  1564. {
  1565. /* lock ? */
  1566. return (lport->state == LPORT_ST_READY) &&
  1567. lport->link_up && !lport->qfull;
  1568. }
  1569. /**
  1570. * fc_queuecommand() - The queuecommand function of the SCSI template
  1571. * @cmd: The scsi_cmnd to be executed
  1572. * @done: The callback function to be called when the scsi_cmnd is complete
  1573. *
  1574. * This is the i/o strategy routine, called by the SCSI layer. This routine
  1575. * is called with the host_lock held.
  1576. */
  1577. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1578. {
  1579. struct fc_lport *lport;
  1580. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1581. struct fc_fcp_pkt *fsp;
  1582. struct fc_rport_libfc_priv *rpriv;
  1583. int rval;
  1584. int rc = 0;
  1585. struct fcoe_dev_stats *stats;
  1586. lport = shost_priv(sc_cmd->device->host);
  1587. spin_unlock_irq(lport->host->host_lock);
  1588. rval = fc_remote_port_chkready(rport);
  1589. if (rval) {
  1590. sc_cmd->result = rval;
  1591. done(sc_cmd);
  1592. goto out;
  1593. }
  1594. if (!*(struct fc_remote_port **)rport->dd_data) {
  1595. /*
  1596. * rport is transitioning from blocked/deleted to
  1597. * online
  1598. */
  1599. sc_cmd->result = DID_IMM_RETRY << 16;
  1600. done(sc_cmd);
  1601. goto out;
  1602. }
  1603. rpriv = rport->dd_data;
  1604. if (!fc_fcp_lport_queue_ready(lport)) {
  1605. if (lport->qfull)
  1606. fc_fcp_can_queue_ramp_down(lport);
  1607. rc = SCSI_MLQUEUE_HOST_BUSY;
  1608. goto out;
  1609. }
  1610. fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
  1611. if (fsp == NULL) {
  1612. rc = SCSI_MLQUEUE_HOST_BUSY;
  1613. goto out;
  1614. }
  1615. /*
  1616. * build the libfc request pkt
  1617. */
  1618. fsp->cmd = sc_cmd; /* save the cmd */
  1619. fsp->lp = lport; /* save the softc ptr */
  1620. fsp->rport = rport; /* set the remote port ptr */
  1621. fsp->xfer_ddp = FC_XID_UNKNOWN;
  1622. sc_cmd->scsi_done = done;
  1623. /*
  1624. * set up the transfer length
  1625. */
  1626. fsp->data_len = scsi_bufflen(sc_cmd);
  1627. fsp->xfer_len = 0;
  1628. /*
  1629. * setup the data direction
  1630. */
  1631. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1632. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1633. fsp->req_flags = FC_SRB_READ;
  1634. stats->InputRequests++;
  1635. stats->InputMegabytes = fsp->data_len;
  1636. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1637. fsp->req_flags = FC_SRB_WRITE;
  1638. stats->OutputRequests++;
  1639. stats->OutputMegabytes = fsp->data_len;
  1640. } else {
  1641. fsp->req_flags = 0;
  1642. stats->ControlRequests++;
  1643. }
  1644. put_cpu();
  1645. fsp->tgt_flags = rpriv->flags;
  1646. init_timer(&fsp->timer);
  1647. fsp->timer.data = (unsigned long)fsp;
  1648. /*
  1649. * send it to the lower layer
  1650. * if we get -1 return then put the request in the pending
  1651. * queue.
  1652. */
  1653. rval = fc_fcp_pkt_send(lport, fsp);
  1654. if (rval != 0) {
  1655. fsp->state = FC_SRB_FREE;
  1656. fc_fcp_pkt_release(fsp);
  1657. rc = SCSI_MLQUEUE_HOST_BUSY;
  1658. }
  1659. out:
  1660. spin_lock_irq(lport->host->host_lock);
  1661. return rc;
  1662. }
  1663. EXPORT_SYMBOL(fc_queuecommand);
  1664. /**
  1665. * fc_io_compl() - Handle responses for completed commands
  1666. * @fsp: The FCP packet that is complete
  1667. *
  1668. * Translates fcp_pkt errors to a Linux SCSI errors.
  1669. * The fcp packet lock must be held when calling.
  1670. */
  1671. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1672. {
  1673. struct fc_fcp_internal *si;
  1674. struct scsi_cmnd *sc_cmd;
  1675. struct fc_lport *lport;
  1676. unsigned long flags;
  1677. /* release outstanding ddp context */
  1678. fc_fcp_ddp_done(fsp);
  1679. fsp->state |= FC_SRB_COMPL;
  1680. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1681. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1682. del_timer_sync(&fsp->timer);
  1683. spin_lock_bh(&fsp->scsi_pkt_lock);
  1684. }
  1685. lport = fsp->lp;
  1686. si = fc_get_scsi_internal(lport);
  1687. if (!fsp->cmd)
  1688. return;
  1689. /*
  1690. * if can_queue ramp down is done then try can_queue ramp up
  1691. * since commands are completing now.
  1692. */
  1693. if (si->last_can_queue_ramp_down_time)
  1694. fc_fcp_can_queue_ramp_up(lport);
  1695. sc_cmd = fsp->cmd;
  1696. fsp->cmd = NULL;
  1697. if (!sc_cmd->SCp.ptr)
  1698. return;
  1699. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1700. switch (fsp->status_code) {
  1701. case FC_COMPLETE:
  1702. if (fsp->cdb_status == 0) {
  1703. /*
  1704. * good I/O status
  1705. */
  1706. sc_cmd->result = DID_OK << 16;
  1707. if (fsp->scsi_resid)
  1708. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1709. } else {
  1710. /*
  1711. * transport level I/O was ok but scsi
  1712. * has non zero status
  1713. */
  1714. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1715. }
  1716. break;
  1717. case FC_ERROR:
  1718. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1719. "due to FC_ERROR\n");
  1720. sc_cmd->result = DID_ERROR << 16;
  1721. break;
  1722. case FC_DATA_UNDRUN:
  1723. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1724. /*
  1725. * scsi status is good but transport level
  1726. * underrun.
  1727. */
  1728. if (fsp->state & FC_SRB_RCV_STATUS) {
  1729. sc_cmd->result = DID_OK << 16;
  1730. } else {
  1731. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml"
  1732. " due to FC_DATA_UNDRUN (trans)\n");
  1733. sc_cmd->result = DID_ERROR << 16;
  1734. }
  1735. } else {
  1736. /*
  1737. * scsi got underrun, this is an error
  1738. */
  1739. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1740. "due to FC_DATA_UNDRUN (scsi)\n");
  1741. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1742. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1743. }
  1744. break;
  1745. case FC_DATA_OVRRUN:
  1746. /*
  1747. * overrun is an error
  1748. */
  1749. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1750. "due to FC_DATA_OVRRUN\n");
  1751. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1752. break;
  1753. case FC_CMD_ABORTED:
  1754. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1755. "due to FC_CMD_ABORTED\n");
  1756. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1757. break;
  1758. case FC_CMD_RECOVERY:
  1759. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1760. break;
  1761. case FC_CMD_RESET:
  1762. sc_cmd->result = (DID_RESET << 16);
  1763. break;
  1764. case FC_HRD_ERROR:
  1765. sc_cmd->result = (DID_NO_CONNECT << 16);
  1766. break;
  1767. default:
  1768. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1769. "due to unknown error\n");
  1770. sc_cmd->result = (DID_ERROR << 16);
  1771. break;
  1772. }
  1773. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1774. list_del(&fsp->list);
  1775. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1776. sc_cmd->SCp.ptr = NULL;
  1777. sc_cmd->scsi_done(sc_cmd);
  1778. /* release ref from initial allocation in queue command */
  1779. fc_fcp_pkt_release(fsp);
  1780. }
  1781. /**
  1782. * fc_eh_abort() - Abort a command
  1783. * @sc_cmd: The SCSI command to abort
  1784. *
  1785. * From SCSI host template.
  1786. * Send an ABTS to the target device and wait for the response.
  1787. */
  1788. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1789. {
  1790. struct fc_fcp_pkt *fsp;
  1791. struct fc_lport *lport;
  1792. int rc = FAILED;
  1793. unsigned long flags;
  1794. lport = shost_priv(sc_cmd->device->host);
  1795. if (lport->state != LPORT_ST_READY)
  1796. return rc;
  1797. else if (!lport->link_up)
  1798. return rc;
  1799. spin_lock_irqsave(lport->host->host_lock, flags);
  1800. fsp = CMD_SP(sc_cmd);
  1801. if (!fsp) {
  1802. /* command completed while scsi eh was setting up */
  1803. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1804. return SUCCESS;
  1805. }
  1806. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1807. fc_fcp_pkt_hold(fsp);
  1808. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1809. if (fc_fcp_lock_pkt(fsp)) {
  1810. /* completed while we were waiting for timer to be deleted */
  1811. rc = SUCCESS;
  1812. goto release_pkt;
  1813. }
  1814. rc = fc_fcp_pkt_abort(fsp);
  1815. fc_fcp_unlock_pkt(fsp);
  1816. release_pkt:
  1817. fc_fcp_pkt_release(fsp);
  1818. return rc;
  1819. }
  1820. EXPORT_SYMBOL(fc_eh_abort);
  1821. /**
  1822. * fc_eh_device_reset() - Reset a single LUN
  1823. * @sc_cmd: The SCSI command which identifies the device whose
  1824. * LUN is to be reset
  1825. *
  1826. * Set from SCSI host template.
  1827. */
  1828. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1829. {
  1830. struct fc_lport *lport;
  1831. struct fc_fcp_pkt *fsp;
  1832. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1833. int rc = FAILED;
  1834. int rval;
  1835. rval = fc_remote_port_chkready(rport);
  1836. if (rval)
  1837. goto out;
  1838. lport = shost_priv(sc_cmd->device->host);
  1839. if (lport->state != LPORT_ST_READY)
  1840. return rc;
  1841. FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id);
  1842. fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
  1843. if (fsp == NULL) {
  1844. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1845. goto out;
  1846. }
  1847. /*
  1848. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1849. * the sc passed in is not setup for execution like when sent
  1850. * through the queuecommand callout.
  1851. */
  1852. fsp->lp = lport; /* save the softc ptr */
  1853. fsp->rport = rport; /* set the remote port ptr */
  1854. /*
  1855. * flush outstanding commands
  1856. */
  1857. rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1858. fsp->state = FC_SRB_FREE;
  1859. fc_fcp_pkt_release(fsp);
  1860. out:
  1861. return rc;
  1862. }
  1863. EXPORT_SYMBOL(fc_eh_device_reset);
  1864. /**
  1865. * fc_eh_host_reset() - Reset a Scsi_Host.
  1866. * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
  1867. */
  1868. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1869. {
  1870. struct Scsi_Host *shost = sc_cmd->device->host;
  1871. struct fc_lport *lport = shost_priv(shost);
  1872. unsigned long wait_tmo;
  1873. FC_SCSI_DBG(lport, "Resetting host\n");
  1874. lport->tt.lport_reset(lport);
  1875. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1876. while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
  1877. wait_tmo))
  1878. msleep(1000);
  1879. if (fc_fcp_lport_queue_ready(lport)) {
  1880. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1881. "on port (%6.6x)\n", lport->port_id);
  1882. return SUCCESS;
  1883. } else {
  1884. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1885. "port (%6.6x) is not ready.\n",
  1886. lport->port_id);
  1887. return FAILED;
  1888. }
  1889. }
  1890. EXPORT_SYMBOL(fc_eh_host_reset);
  1891. /**
  1892. * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
  1893. * @sdev: The SCSI device that identifies the SCSI host
  1894. *
  1895. * Configures queue depth based on host's cmd_per_len. If not set
  1896. * then we use the libfc default.
  1897. */
  1898. int fc_slave_alloc(struct scsi_device *sdev)
  1899. {
  1900. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1901. if (!rport || fc_remote_port_chkready(rport))
  1902. return -ENXIO;
  1903. if (sdev->tagged_supported)
  1904. scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
  1905. else
  1906. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1907. FC_FCP_DFLT_QUEUE_DEPTH);
  1908. return 0;
  1909. }
  1910. EXPORT_SYMBOL(fc_slave_alloc);
  1911. /**
  1912. * fc_change_queue_depth() - Change a device's queue depth
  1913. * @sdev: The SCSI device whose queue depth is to change
  1914. * @qdepth: The new queue depth
  1915. * @reason: The resason for the change
  1916. */
  1917. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  1918. {
  1919. switch (reason) {
  1920. case SCSI_QDEPTH_DEFAULT:
  1921. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1922. break;
  1923. case SCSI_QDEPTH_QFULL:
  1924. scsi_track_queue_full(sdev, qdepth);
  1925. break;
  1926. case SCSI_QDEPTH_RAMP_UP:
  1927. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1928. break;
  1929. default:
  1930. return -EOPNOTSUPP;
  1931. }
  1932. return sdev->queue_depth;
  1933. }
  1934. EXPORT_SYMBOL(fc_change_queue_depth);
  1935. /**
  1936. * fc_change_queue_type() - Change a device's queue type
  1937. * @sdev: The SCSI device whose queue depth is to change
  1938. * @tag_type: Identifier for queue type
  1939. */
  1940. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1941. {
  1942. if (sdev->tagged_supported) {
  1943. scsi_set_tag_type(sdev, tag_type);
  1944. if (tag_type)
  1945. scsi_activate_tcq(sdev, sdev->queue_depth);
  1946. else
  1947. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1948. } else
  1949. tag_type = 0;
  1950. return tag_type;
  1951. }
  1952. EXPORT_SYMBOL(fc_change_queue_type);
  1953. /**
  1954. * fc_fcp_destory() - Tear down the FCP layer for a given local port
  1955. * @lport: The local port that no longer needs the FCP layer
  1956. */
  1957. void fc_fcp_destroy(struct fc_lport *lport)
  1958. {
  1959. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  1960. if (!list_empty(&si->scsi_pkt_queue))
  1961. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1962. "port (%6.6x)\n", lport->port_id);
  1963. mempool_destroy(si->scsi_pkt_pool);
  1964. kfree(si);
  1965. lport->scsi_priv = NULL;
  1966. }
  1967. EXPORT_SYMBOL(fc_fcp_destroy);
  1968. int fc_setup_fcp()
  1969. {
  1970. int rc = 0;
  1971. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1972. sizeof(struct fc_fcp_pkt),
  1973. 0, SLAB_HWCACHE_ALIGN, NULL);
  1974. if (!scsi_pkt_cachep) {
  1975. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1976. "module load failed!");
  1977. rc = -ENOMEM;
  1978. }
  1979. return rc;
  1980. }
  1981. void fc_destroy_fcp()
  1982. {
  1983. if (scsi_pkt_cachep)
  1984. kmem_cache_destroy(scsi_pkt_cachep);
  1985. }
  1986. /**
  1987. * fc_fcp_init() - Initialize the FCP layer for a local port
  1988. * @lport: The local port to initialize the exchange layer for
  1989. */
  1990. int fc_fcp_init(struct fc_lport *lport)
  1991. {
  1992. int rc;
  1993. struct fc_fcp_internal *si;
  1994. if (!lport->tt.fcp_cmd_send)
  1995. lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1996. if (!lport->tt.fcp_cleanup)
  1997. lport->tt.fcp_cleanup = fc_fcp_cleanup;
  1998. if (!lport->tt.fcp_abort_io)
  1999. lport->tt.fcp_abort_io = fc_fcp_abort_io;
  2000. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  2001. if (!si)
  2002. return -ENOMEM;
  2003. lport->scsi_priv = si;
  2004. si->max_can_queue = lport->host->can_queue;
  2005. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  2006. spin_lock_init(&si->scsi_queue_lock);
  2007. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  2008. if (!si->scsi_pkt_pool) {
  2009. rc = -ENOMEM;
  2010. goto free_internal;
  2011. }
  2012. return 0;
  2013. free_internal:
  2014. kfree(si);
  2015. return rc;
  2016. }
  2017. EXPORT_SYMBOL(fc_fcp_init);