fc_fcp.c 57 KB

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