fc_fcp.c 58 KB

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