libiscsi.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/mutex.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/delay.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. #define INVALID_SN_DELTA 0xffff
  47. int
  48. iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  49. {
  50. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  51. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  52. if (max_cmdsn < exp_cmdsn -1 &&
  53. max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
  54. return ISCSI_ERR_MAX_CMDSN;
  55. if (max_cmdsn > session->max_cmdsn ||
  56. max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
  57. session->max_cmdsn = max_cmdsn;
  58. if (exp_cmdsn > session->exp_cmdsn ||
  59. exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
  60. session->exp_cmdsn = exp_cmdsn;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
  64. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  65. struct iscsi_data *hdr,
  66. int transport_data_cnt)
  67. {
  68. struct iscsi_conn *conn = ctask->conn;
  69. memset(hdr, 0, sizeof(struct iscsi_data));
  70. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  71. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  72. ctask->unsol_datasn++;
  73. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  74. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  75. hdr->itt = ctask->hdr->itt;
  76. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  77. hdr->offset = cpu_to_be32(ctask->total_length -
  78. transport_data_cnt -
  79. ctask->unsol_count);
  80. if (ctask->unsol_count > conn->max_xmit_dlength) {
  81. hton24(hdr->dlength, conn->max_xmit_dlength);
  82. ctask->data_count = conn->max_xmit_dlength;
  83. hdr->flags = 0;
  84. } else {
  85. hton24(hdr->dlength, ctask->unsol_count);
  86. ctask->data_count = ctask->unsol_count;
  87. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  88. }
  89. }
  90. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  91. /**
  92. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  93. * @ctask: iscsi cmd task
  94. *
  95. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  96. * fields like dlength or final based on how much data it sends
  97. */
  98. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  99. {
  100. struct iscsi_conn *conn = ctask->conn;
  101. struct iscsi_session *session = conn->session;
  102. struct iscsi_cmd *hdr = ctask->hdr;
  103. struct scsi_cmnd *sc = ctask->sc;
  104. hdr->opcode = ISCSI_OP_SCSI_CMD;
  105. hdr->flags = ISCSI_ATTR_SIMPLE;
  106. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  107. hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
  108. (session->age << ISCSI_AGE_SHIFT);
  109. hdr->data_length = cpu_to_be32(sc->request_bufflen);
  110. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  111. session->cmdsn++;
  112. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  113. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  114. memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
  115. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  116. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  117. /*
  118. * Write counters:
  119. *
  120. * imm_count bytes to be sent right after
  121. * SCSI PDU Header
  122. *
  123. * unsol_count bytes(as Data-Out) to be sent
  124. * without R2T ack right after
  125. * immediate data
  126. *
  127. * r2t_data_count bytes to be sent via R2T ack's
  128. *
  129. * pad_count bytes to be sent as zero-padding
  130. */
  131. ctask->imm_count = 0;
  132. ctask->unsol_count = 0;
  133. ctask->unsol_datasn = 0;
  134. if (session->imm_data_en) {
  135. if (ctask->total_length >= session->first_burst)
  136. ctask->imm_count = min(session->first_burst,
  137. conn->max_xmit_dlength);
  138. else
  139. ctask->imm_count = min(ctask->total_length,
  140. conn->max_xmit_dlength);
  141. hton24(ctask->hdr->dlength, ctask->imm_count);
  142. } else
  143. zero_data(ctask->hdr->dlength);
  144. if (!session->initial_r2t_en)
  145. ctask->unsol_count = min(session->first_burst,
  146. ctask->total_length) - ctask->imm_count;
  147. if (!ctask->unsol_count)
  148. /* No unsolicit Data-Out's */
  149. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  150. } else {
  151. ctask->datasn = 0;
  152. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  153. zero_data(hdr->dlength);
  154. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  155. hdr->flags |= ISCSI_FLAG_CMD_READ;
  156. }
  157. conn->scsicmd_pdus_cnt++;
  158. }
  159. EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
  160. /**
  161. * iscsi_complete_command - return command back to scsi-ml
  162. * @session: iscsi session
  163. * @ctask: iscsi cmd task
  164. *
  165. * Must be called with session lock.
  166. * This function returns the scsi command to scsi-ml and returns
  167. * the cmd task to the pool of available cmd tasks.
  168. */
  169. static void iscsi_complete_command(struct iscsi_session *session,
  170. struct iscsi_cmd_task *ctask)
  171. {
  172. struct scsi_cmnd *sc = ctask->sc;
  173. ctask->sc = NULL;
  174. list_del_init(&ctask->running);
  175. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  176. sc->scsi_done(sc);
  177. }
  178. /**
  179. * iscsi_cmd_rsp - SCSI Command Response processing
  180. * @conn: iscsi connection
  181. * @hdr: iscsi header
  182. * @ctask: scsi command task
  183. * @data: cmd data buffer
  184. * @datalen: len of buffer
  185. *
  186. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  187. * then completes the command and task.
  188. **/
  189. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  190. struct iscsi_cmd_task *ctask, char *data,
  191. int datalen)
  192. {
  193. int rc;
  194. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  195. struct iscsi_session *session = conn->session;
  196. struct scsi_cmnd *sc = ctask->sc;
  197. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  198. if (rc) {
  199. sc->result = DID_ERROR << 16;
  200. goto out;
  201. }
  202. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  203. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  204. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  205. sc->result = DID_ERROR << 16;
  206. goto out;
  207. }
  208. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  209. int senselen;
  210. if (datalen < 2) {
  211. invalid_datalen:
  212. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  213. "invalid data buffer size of %d\n", datalen);
  214. sc->result = DID_BAD_TARGET << 16;
  215. goto out;
  216. }
  217. senselen = (data[0] << 8) | data[1];
  218. if (datalen < senselen)
  219. goto invalid_datalen;
  220. memcpy(sc->sense_buffer, data + 2,
  221. min(senselen, SCSI_SENSE_BUFFERSIZE));
  222. debug_scsi("copied %d bytes of sense\n",
  223. min(senselen, SCSI_SENSE_BUFFERSIZE));
  224. }
  225. if (sc->sc_data_direction == DMA_TO_DEVICE)
  226. goto out;
  227. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  228. int res_count = be32_to_cpu(rhdr->residual_count);
  229. if (res_count > 0 && res_count <= sc->request_bufflen)
  230. sc->resid = res_count;
  231. else
  232. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  233. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  234. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  235. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  236. sc->resid = be32_to_cpu(rhdr->residual_count);
  237. out:
  238. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  239. (long)sc, sc->result, ctask->itt);
  240. conn->scsirsp_pdus_cnt++;
  241. iscsi_complete_command(conn->session, ctask);
  242. return rc;
  243. }
  244. /**
  245. * __iscsi_complete_pdu - complete pdu
  246. * @conn: iscsi conn
  247. * @hdr: iscsi header
  248. * @data: data buffer
  249. * @datalen: len of data buffer
  250. *
  251. * Completes pdu processing by freeing any resources allocated at
  252. * queuecommand or send generic. session lock must be held and verify
  253. * itt must have been called.
  254. */
  255. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  256. char *data, int datalen)
  257. {
  258. struct iscsi_session *session = conn->session;
  259. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  260. struct iscsi_cmd_task *ctask;
  261. struct iscsi_mgmt_task *mtask;
  262. uint32_t itt;
  263. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
  264. itt = hdr->itt & ISCSI_ITT_MASK;
  265. else
  266. itt = hdr->itt;
  267. if (itt < session->cmds_max) {
  268. ctask = session->cmds[itt];
  269. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  270. opcode, conn->id, ctask->itt, datalen);
  271. switch(opcode) {
  272. case ISCSI_OP_SCSI_CMD_RSP:
  273. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  274. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  275. datalen);
  276. break;
  277. case ISCSI_OP_SCSI_DATA_IN:
  278. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  279. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  280. conn->scsirsp_pdus_cnt++;
  281. iscsi_complete_command(session, ctask);
  282. }
  283. break;
  284. case ISCSI_OP_R2T:
  285. /* LLD handles this for now */
  286. break;
  287. default:
  288. rc = ISCSI_ERR_BAD_OPCODE;
  289. break;
  290. }
  291. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  292. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  293. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  294. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  295. opcode, conn->id, mtask->itt, datalen);
  296. rc = iscsi_check_assign_cmdsn(session,
  297. (struct iscsi_nopin*)hdr);
  298. if (rc)
  299. goto done;
  300. switch(opcode) {
  301. case ISCSI_OP_LOGOUT_RSP:
  302. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  303. /* fall through */
  304. case ISCSI_OP_LOGIN_RSP:
  305. case ISCSI_OP_TEXT_RSP:
  306. /*
  307. * login related PDU's exp_statsn is handled in
  308. * userspace
  309. */
  310. rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
  311. list_del(&mtask->running);
  312. if (conn->login_mtask != mtask)
  313. __kfifo_put(session->mgmtpool.queue,
  314. (void*)&mtask, sizeof(void*));
  315. break;
  316. case ISCSI_OP_SCSI_TMFUNC_RSP:
  317. if (datalen) {
  318. rc = ISCSI_ERR_PROTO;
  319. break;
  320. }
  321. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  322. conn->tmfrsp_pdus_cnt++;
  323. if (conn->tmabort_state == TMABORT_INITIAL) {
  324. conn->tmabort_state =
  325. ((struct iscsi_tm_rsp *)hdr)->
  326. response == ISCSI_TMF_RSP_COMPLETE ?
  327. TMABORT_SUCCESS:TMABORT_FAILED;
  328. /* unblock eh_abort() */
  329. wake_up(&conn->ehwait);
  330. }
  331. break;
  332. case ISCSI_OP_NOOP_IN:
  333. if (hdr->ttt != ISCSI_RESERVED_TAG) {
  334. rc = ISCSI_ERR_PROTO;
  335. break;
  336. }
  337. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  338. rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
  339. list_del(&mtask->running);
  340. if (conn->login_mtask != mtask)
  341. __kfifo_put(session->mgmtpool.queue,
  342. (void*)&mtask, sizeof(void*));
  343. break;
  344. default:
  345. rc = ISCSI_ERR_BAD_OPCODE;
  346. break;
  347. }
  348. } else if (itt == ISCSI_RESERVED_TAG) {
  349. switch(opcode) {
  350. case ISCSI_OP_NOOP_IN:
  351. if (!datalen) {
  352. rc = iscsi_check_assign_cmdsn(session,
  353. (struct iscsi_nopin*)hdr);
  354. if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
  355. rc = iscsi_recv_pdu(conn->cls_conn,
  356. hdr, NULL, 0);
  357. } else
  358. rc = ISCSI_ERR_PROTO;
  359. break;
  360. case ISCSI_OP_REJECT:
  361. /* we need sth like iscsi_reject_rsp()*/
  362. case ISCSI_OP_ASYNC_EVENT:
  363. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  364. /* we need sth like iscsi_async_event_rsp() */
  365. rc = ISCSI_ERR_BAD_OPCODE;
  366. break;
  367. default:
  368. rc = ISCSI_ERR_BAD_OPCODE;
  369. break;
  370. }
  371. } else
  372. rc = ISCSI_ERR_BAD_ITT;
  373. done:
  374. return rc;
  375. }
  376. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  377. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  378. char *data, int datalen)
  379. {
  380. int rc;
  381. spin_lock(&conn->session->lock);
  382. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  383. spin_unlock(&conn->session->lock);
  384. return rc;
  385. }
  386. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  387. /* verify itt (itt encoding: age+cid+itt) */
  388. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  389. uint32_t *ret_itt)
  390. {
  391. struct iscsi_session *session = conn->session;
  392. struct iscsi_cmd_task *ctask;
  393. uint32_t itt;
  394. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  395. if ((hdr->itt & ISCSI_AGE_MASK) !=
  396. (session->age << ISCSI_AGE_SHIFT)) {
  397. printk(KERN_ERR "iscsi: received itt %x expected "
  398. "session age (%x)\n", hdr->itt,
  399. session->age & ISCSI_AGE_MASK);
  400. return ISCSI_ERR_BAD_ITT;
  401. }
  402. if ((hdr->itt & ISCSI_CID_MASK) !=
  403. (conn->id << ISCSI_CID_SHIFT)) {
  404. printk(KERN_ERR "iscsi: received itt %x, expected "
  405. "CID (%x)\n", hdr->itt, conn->id);
  406. return ISCSI_ERR_BAD_ITT;
  407. }
  408. itt = hdr->itt & ISCSI_ITT_MASK;
  409. } else
  410. itt = hdr->itt;
  411. if (itt < session->cmds_max) {
  412. ctask = session->cmds[itt];
  413. if (!ctask->sc) {
  414. printk(KERN_INFO "iscsi: dropping ctask with "
  415. "itt 0x%x\n", ctask->itt);
  416. /* force drop */
  417. return ISCSI_ERR_NO_SCSI_CMD;
  418. }
  419. if (ctask->sc->SCp.phase != session->age) {
  420. printk(KERN_ERR "iscsi: ctask's session age %d, "
  421. "expected %d\n", ctask->sc->SCp.phase,
  422. session->age);
  423. return ISCSI_ERR_SESSION_FAILED;
  424. }
  425. }
  426. *ret_itt = itt;
  427. return 0;
  428. }
  429. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  430. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  431. {
  432. struct iscsi_session *session = conn->session;
  433. unsigned long flags;
  434. spin_lock_irqsave(&session->lock, flags);
  435. if (session->conn_cnt == 1 || session->leadconn == conn)
  436. session->state = ISCSI_STATE_FAILED;
  437. spin_unlock_irqrestore(&session->lock, flags);
  438. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  439. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  440. iscsi_conn_error(conn->cls_conn, err);
  441. }
  442. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  443. /**
  444. * iscsi_data_xmit - xmit any command into the scheduled connection
  445. * @conn: iscsi connection
  446. *
  447. * Notes:
  448. * The function can return -EAGAIN in which case the caller must
  449. * re-schedule it again later or recover. '0' return code means
  450. * successful xmit.
  451. **/
  452. static int iscsi_data_xmit(struct iscsi_conn *conn)
  453. {
  454. struct iscsi_transport *tt;
  455. if (unlikely(conn->suspend_tx)) {
  456. debug_scsi("conn %d Tx suspended!\n", conn->id);
  457. return 0;
  458. }
  459. tt = conn->session->tt;
  460. /*
  461. * Transmit in the following order:
  462. *
  463. * 1) un-finished xmit (ctask or mtask)
  464. * 2) immediate control PDUs
  465. * 3) write data
  466. * 4) SCSI commands
  467. * 5) non-immediate control PDUs
  468. *
  469. * No need to lock around __kfifo_get as long as
  470. * there's one producer and one consumer.
  471. */
  472. BUG_ON(conn->ctask && conn->mtask);
  473. if (conn->ctask) {
  474. if (tt->xmit_cmd_task(conn, conn->ctask))
  475. goto again;
  476. /* done with this in-progress ctask */
  477. conn->ctask = NULL;
  478. }
  479. if (conn->mtask) {
  480. if (tt->xmit_mgmt_task(conn, conn->mtask))
  481. goto again;
  482. /* done with this in-progress mtask */
  483. conn->mtask = NULL;
  484. }
  485. /* process immediate first */
  486. if (unlikely(__kfifo_len(conn->immqueue))) {
  487. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  488. sizeof(void*))) {
  489. list_add_tail(&conn->mtask->running,
  490. &conn->mgmt_run_list);
  491. if (tt->xmit_mgmt_task(conn, conn->mtask))
  492. goto again;
  493. }
  494. /* done with this mtask */
  495. conn->mtask = NULL;
  496. }
  497. /* process command queue */
  498. while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask,
  499. sizeof(void*))) {
  500. /*
  501. * iscsi tcp may readd the task to the xmitqueue to send
  502. * write data
  503. */
  504. if (list_empty(&conn->ctask->running))
  505. list_add_tail(&conn->ctask->running, &conn->run_list);
  506. if (tt->xmit_cmd_task(conn, conn->ctask))
  507. goto again;
  508. }
  509. /* done with this ctask */
  510. conn->ctask = NULL;
  511. /* process the rest control plane PDUs, if any */
  512. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  513. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  514. sizeof(void*))) {
  515. list_add_tail(&conn->mtask->running,
  516. &conn->mgmt_run_list);
  517. if (tt->xmit_mgmt_task(conn, conn->mtask))
  518. goto again;
  519. }
  520. /* done with this mtask */
  521. conn->mtask = NULL;
  522. }
  523. return 0;
  524. again:
  525. if (unlikely(conn->suspend_tx))
  526. return 0;
  527. return -EAGAIN;
  528. }
  529. static void iscsi_xmitworker(void *data)
  530. {
  531. struct iscsi_conn *conn = data;
  532. /*
  533. * serialize Xmit worker on a per-connection basis.
  534. */
  535. mutex_lock(&conn->xmitmutex);
  536. if (iscsi_data_xmit(conn))
  537. scsi_queue_work(conn->session->host, &conn->xmitwork);
  538. mutex_unlock(&conn->xmitmutex);
  539. }
  540. enum {
  541. FAILURE_BAD_HOST = 1,
  542. FAILURE_SESSION_FAILED,
  543. FAILURE_SESSION_FREED,
  544. FAILURE_WINDOW_CLOSED,
  545. FAILURE_SESSION_TERMINATE,
  546. FAILURE_SESSION_RECOVERY_TIMEOUT,
  547. };
  548. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  549. {
  550. struct Scsi_Host *host;
  551. int reason = 0;
  552. struct iscsi_session *session;
  553. struct iscsi_conn *conn;
  554. struct iscsi_cmd_task *ctask = NULL;
  555. sc->scsi_done = done;
  556. sc->result = 0;
  557. host = sc->device->host;
  558. session = iscsi_hostdata(host->hostdata);
  559. spin_lock(&session->lock);
  560. if (session->state != ISCSI_STATE_LOGGED_IN) {
  561. if (session->recovery_failed) {
  562. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  563. goto fault;
  564. } else if (session->state == ISCSI_STATE_FAILED) {
  565. reason = FAILURE_SESSION_FAILED;
  566. goto reject;
  567. } else if (session->state == ISCSI_STATE_TERMINATE) {
  568. reason = FAILURE_SESSION_TERMINATE;
  569. goto fault;
  570. }
  571. reason = FAILURE_SESSION_FREED;
  572. goto fault;
  573. }
  574. /*
  575. * Check for iSCSI window and take care of CmdSN wrap-around
  576. */
  577. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  578. reason = FAILURE_WINDOW_CLOSED;
  579. goto reject;
  580. }
  581. conn = session->leadconn;
  582. __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  583. sc->SCp.phase = session->age;
  584. sc->SCp.ptr = (char *)ctask;
  585. ctask->mtask = NULL;
  586. ctask->conn = conn;
  587. ctask->sc = sc;
  588. INIT_LIST_HEAD(&ctask->running);
  589. ctask->total_length = sc->request_bufflen;
  590. iscsi_prep_scsi_cmd_pdu(ctask);
  591. session->tt->init_cmd_task(ctask);
  592. __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
  593. debug_scsi(
  594. "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
  595. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  596. conn->id, (long)sc, ctask->itt, sc->request_bufflen,
  597. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  598. spin_unlock(&session->lock);
  599. scsi_queue_work(host, &conn->xmitwork);
  600. return 0;
  601. reject:
  602. spin_unlock(&session->lock);
  603. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  604. return SCSI_MLQUEUE_HOST_BUSY;
  605. fault:
  606. spin_unlock(&session->lock);
  607. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  608. sc->cmnd[0], reason);
  609. sc->result = (DID_NO_CONNECT << 16);
  610. sc->resid = sc->request_bufflen;
  611. sc->scsi_done(sc);
  612. return 0;
  613. }
  614. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  615. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  616. {
  617. if (depth > ISCSI_MAX_CMD_PER_LUN)
  618. depth = ISCSI_MAX_CMD_PER_LUN;
  619. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  620. return sdev->queue_depth;
  621. }
  622. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  623. static int
  624. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  625. char *data, uint32_t data_size)
  626. {
  627. struct iscsi_session *session = conn->session;
  628. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  629. struct iscsi_mgmt_task *mtask;
  630. spin_lock_bh(&session->lock);
  631. if (session->state == ISCSI_STATE_TERMINATE) {
  632. spin_unlock_bh(&session->lock);
  633. return -EPERM;
  634. }
  635. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  636. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  637. /*
  638. * Login and Text are sent serially, in
  639. * request-followed-by-response sequence.
  640. * Same mtask can be used. Same ITT must be used.
  641. * Note that login_mtask is preallocated at conn_create().
  642. */
  643. mtask = conn->login_mtask;
  644. else {
  645. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  646. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  647. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  648. if (!__kfifo_get(session->mgmtpool.queue,
  649. (void*)&mtask, sizeof(void*))) {
  650. spin_unlock_bh(&session->lock);
  651. return -ENOSPC;
  652. }
  653. }
  654. /*
  655. * pre-format CmdSN for outgoing PDU.
  656. */
  657. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  658. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  659. (session->age << ISCSI_AGE_SHIFT);
  660. nop->cmdsn = cpu_to_be32(session->cmdsn);
  661. if (conn->c_stage == ISCSI_CONN_STARTED &&
  662. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  663. session->cmdsn++;
  664. } else
  665. /* do not advance CmdSN */
  666. nop->cmdsn = cpu_to_be32(session->cmdsn);
  667. if (data_size) {
  668. memcpy(mtask->data, data, data_size);
  669. mtask->data_count = data_size;
  670. } else
  671. mtask->data_count = 0;
  672. INIT_LIST_HEAD(&mtask->running);
  673. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  674. if (session->tt->init_mgmt_task)
  675. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  676. spin_unlock_bh(&session->lock);
  677. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  678. hdr->opcode, hdr->itt, data_size);
  679. /*
  680. * since send_pdu() could be called at least from two contexts,
  681. * we need to serialize __kfifo_put, so we don't have to take
  682. * additional lock on fast data-path
  683. */
  684. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  685. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  686. else
  687. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  688. scsi_queue_work(session->host, &conn->xmitwork);
  689. return 0;
  690. }
  691. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  692. char *data, uint32_t data_size)
  693. {
  694. struct iscsi_conn *conn = cls_conn->dd_data;
  695. int rc;
  696. mutex_lock(&conn->xmitmutex);
  697. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  698. mutex_unlock(&conn->xmitmutex);
  699. return rc;
  700. }
  701. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  702. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  703. {
  704. struct iscsi_session *session = class_to_transport_session(cls_session);
  705. struct iscsi_conn *conn = session->leadconn;
  706. spin_lock_bh(&session->lock);
  707. if (session->state != ISCSI_STATE_LOGGED_IN) {
  708. session->recovery_failed = 1;
  709. if (conn)
  710. wake_up(&conn->ehwait);
  711. }
  712. spin_unlock_bh(&session->lock);
  713. }
  714. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  715. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  716. {
  717. struct Scsi_Host *host = sc->device->host;
  718. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  719. struct iscsi_conn *conn = session->leadconn;
  720. int fail_session = 0;
  721. spin_lock_bh(&session->lock);
  722. if (session->state == ISCSI_STATE_TERMINATE) {
  723. failed:
  724. debug_scsi("failing host reset: session terminated "
  725. "[CID %d age %d]", conn->id, session->age);
  726. spin_unlock_bh(&session->lock);
  727. return FAILED;
  728. }
  729. if (sc->SCp.phase == session->age) {
  730. debug_scsi("failing connection CID %d due to SCSI host reset",
  731. conn->id);
  732. fail_session = 1;
  733. }
  734. spin_unlock_bh(&session->lock);
  735. /*
  736. * we drop the lock here but the leadconn cannot be destoyed while
  737. * we are in the scsi eh
  738. */
  739. if (fail_session) {
  740. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  741. /*
  742. * if userspace cannot respond then we must kick this off
  743. * here for it
  744. */
  745. iscsi_start_session_recovery(session, conn, STOP_CONN_RECOVER);
  746. }
  747. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  748. wait_event_interruptible(conn->ehwait,
  749. session->state == ISCSI_STATE_TERMINATE ||
  750. session->state == ISCSI_STATE_LOGGED_IN ||
  751. session->recovery_failed);
  752. if (signal_pending(current))
  753. flush_signals(current);
  754. spin_lock_bh(&session->lock);
  755. if (session->state == ISCSI_STATE_LOGGED_IN)
  756. printk(KERN_INFO "iscsi: host reset succeeded\n");
  757. else
  758. goto failed;
  759. spin_unlock_bh(&session->lock);
  760. return SUCCESS;
  761. }
  762. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  763. static void iscsi_tmabort_timedout(unsigned long data)
  764. {
  765. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  766. struct iscsi_conn *conn = ctask->conn;
  767. struct iscsi_session *session = conn->session;
  768. spin_lock(&session->lock);
  769. if (conn->tmabort_state == TMABORT_INITIAL) {
  770. conn->tmabort_state = TMABORT_TIMEDOUT;
  771. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  772. ctask->sc, ctask->itt);
  773. /* unblock eh_abort() */
  774. wake_up(&conn->ehwait);
  775. }
  776. spin_unlock(&session->lock);
  777. }
  778. /* must be called with the mutex lock */
  779. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  780. struct iscsi_cmd_task *ctask)
  781. {
  782. struct iscsi_conn *conn = ctask->conn;
  783. struct iscsi_session *session = conn->session;
  784. struct iscsi_tm *hdr = &conn->tmhdr;
  785. int rc;
  786. /*
  787. * ctask timed out but session is OK requests must be serialized.
  788. */
  789. memset(hdr, 0, sizeof(struct iscsi_tm));
  790. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  791. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  792. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  793. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  794. hdr->rtt = ctask->hdr->itt;
  795. hdr->refcmdsn = ctask->hdr->cmdsn;
  796. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  797. NULL, 0);
  798. if (rc) {
  799. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  800. debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
  801. return rc;
  802. }
  803. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  804. spin_lock_bh(&session->lock);
  805. ctask->mtask = (struct iscsi_mgmt_task *)
  806. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  807. ISCSI_MGMT_ITT_OFFSET];
  808. if (conn->tmabort_state == TMABORT_INITIAL) {
  809. conn->tmfcmd_pdus_cnt++;
  810. conn->tmabort_timer.expires = 10*HZ + jiffies;
  811. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  812. conn->tmabort_timer.data = (unsigned long)ctask;
  813. add_timer(&conn->tmabort_timer);
  814. debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
  815. }
  816. spin_unlock_bh(&session->lock);
  817. mutex_unlock(&conn->xmitmutex);
  818. /*
  819. * block eh thread until:
  820. *
  821. * 1) abort response
  822. * 2) abort timeout
  823. * 3) session is terminated or restarted or userspace has
  824. * given up on recovery
  825. */
  826. wait_event_interruptible(conn->ehwait,
  827. sc->SCp.phase != session->age ||
  828. session->state != ISCSI_STATE_LOGGED_IN ||
  829. conn->tmabort_state != TMABORT_INITIAL ||
  830. session->recovery_failed);
  831. if (signal_pending(current))
  832. flush_signals(current);
  833. del_timer_sync(&conn->tmabort_timer);
  834. mutex_lock(&conn->xmitmutex);
  835. return 0;
  836. }
  837. /*
  838. * xmit mutex and session lock must be held
  839. */
  840. #define iscsi_remove_task(tasktype) \
  841. static struct iscsi_##tasktype * \
  842. iscsi_remove_##tasktype(struct kfifo *fifo, uint32_t itt) \
  843. { \
  844. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*); \
  845. struct iscsi_##tasktype *task; \
  846. \
  847. debug_scsi("searching %d tasks\n", nr_tasks); \
  848. \
  849. for (i = 0; i < nr_tasks; i++) { \
  850. __kfifo_get(fifo, (void*)&task, sizeof(void*)); \
  851. debug_scsi("check task %u\n", task->itt); \
  852. \
  853. if (task->itt == itt) { \
  854. debug_scsi("matched task\n"); \
  855. break; \
  856. } \
  857. \
  858. __kfifo_put(fifo, (void*)&task, sizeof(void*)); \
  859. } \
  860. return NULL; \
  861. }
  862. iscsi_remove_task(mgmt_task);
  863. iscsi_remove_task(cmd_task);
  864. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  865. {
  866. struct iscsi_conn *conn = ctask->conn;
  867. struct iscsi_session *session = conn->session;
  868. if (!ctask->mtask)
  869. return -EINVAL;
  870. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  871. list_del(&ctask->mtask->running);
  872. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  873. sizeof(void*));
  874. ctask->mtask = NULL;
  875. return 0;
  876. }
  877. /*
  878. * session lock and xmitmutex must be held
  879. */
  880. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  881. int err)
  882. {
  883. struct scsi_cmnd *sc;
  884. conn->session->tt->cleanup_cmd_task(conn, ctask);
  885. iscsi_ctask_mtask_cleanup(ctask);
  886. sc = ctask->sc;
  887. if (!sc)
  888. return;
  889. sc->result = err;
  890. sc->resid = sc->request_bufflen;
  891. iscsi_complete_command(conn->session, ctask);
  892. }
  893. int iscsi_eh_abort(struct scsi_cmnd *sc)
  894. {
  895. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  896. struct iscsi_conn *conn = ctask->conn;
  897. struct iscsi_session *session = conn->session;
  898. struct iscsi_cmd_task *pending_ctask;
  899. int rc;
  900. conn->eh_abort_cnt++;
  901. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  902. mutex_lock(&conn->xmitmutex);
  903. spin_lock_bh(&session->lock);
  904. /*
  905. * If we are not logged in or we have started a new session
  906. * then let the host reset code handle this
  907. */
  908. if (session->state != ISCSI_STATE_LOGGED_IN ||
  909. sc->SCp.phase != session->age)
  910. goto failed;
  911. /* ctask completed before time out */
  912. if (!ctask->sc)
  913. goto success;
  914. /* what should we do here ? */
  915. if (conn->ctask == ctask) {
  916. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  917. "Failing abort\n", sc, ctask->itt);
  918. goto failed;
  919. }
  920. /* check for the easy pending cmd abort */
  921. pending_ctask = iscsi_remove_cmd_task(conn->xmitqueue, ctask->itt);
  922. if (pending_ctask) {
  923. /* iscsi_tcp queues write transfers on the xmitqueue */
  924. if (list_empty(&pending_ctask->running)) {
  925. debug_scsi("found pending task\n");
  926. goto success;
  927. } else
  928. __kfifo_put(conn->xmitqueue, (void*)&pending_ctask,
  929. sizeof(void*));
  930. }
  931. conn->tmabort_state = TMABORT_INITIAL;
  932. spin_unlock_bh(&session->lock);
  933. rc = iscsi_exec_abort_task(sc, ctask);
  934. spin_lock_bh(&session->lock);
  935. iscsi_ctask_mtask_cleanup(ctask);
  936. if (rc || sc->SCp.phase != session->age ||
  937. session->state != ISCSI_STATE_LOGGED_IN)
  938. goto failed;
  939. /* ctask completed before tmf abort response */
  940. if (!ctask->sc) {
  941. debug_scsi("sc completed while abort in progress\n");
  942. goto success;
  943. }
  944. if (conn->tmabort_state != TMABORT_SUCCESS) {
  945. spin_unlock_bh(&session->lock);
  946. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  947. spin_lock_bh(&session->lock);
  948. goto failed;
  949. }
  950. success:
  951. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  952. spin_unlock_bh(&session->lock);
  953. /*
  954. * clean up task if aborted. we have the xmitmutex so grab
  955. * the recv lock as a writer
  956. */
  957. write_lock_bh(conn->recv_lock);
  958. spin_lock(&session->lock);
  959. fail_command(conn, ctask, DID_ABORT << 16);
  960. spin_unlock(&session->lock);
  961. write_unlock_bh(conn->recv_lock);
  962. mutex_unlock(&conn->xmitmutex);
  963. return SUCCESS;
  964. failed:
  965. spin_unlock_bh(&session->lock);
  966. mutex_unlock(&conn->xmitmutex);
  967. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  968. return FAILED;
  969. }
  970. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  971. int
  972. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  973. {
  974. int i;
  975. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  976. if (*items == NULL)
  977. return -ENOMEM;
  978. q->max = max;
  979. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  980. if (q->pool == NULL) {
  981. kfree(*items);
  982. return -ENOMEM;
  983. }
  984. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  985. GFP_KERNEL, NULL);
  986. if (q->queue == ERR_PTR(-ENOMEM)) {
  987. kfree(q->pool);
  988. kfree(*items);
  989. return -ENOMEM;
  990. }
  991. for (i = 0; i < max; i++) {
  992. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  993. if (q->pool[i] == NULL) {
  994. int j;
  995. for (j = 0; j < i; j++)
  996. kfree(q->pool[j]);
  997. kfifo_free(q->queue);
  998. kfree(q->pool);
  999. kfree(*items);
  1000. return -ENOMEM;
  1001. }
  1002. memset(q->pool[i], 0, item_size);
  1003. (*items)[i] = q->pool[i];
  1004. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1005. }
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1009. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1010. {
  1011. int i;
  1012. for (i = 0; i < q->max; i++)
  1013. kfree(items[i]);
  1014. kfree(q->pool);
  1015. kfree(items);
  1016. }
  1017. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1018. /*
  1019. * iSCSI Session's hostdata organization:
  1020. *
  1021. * *------------------* <== hostdata_session(host->hostdata)
  1022. * | ptr to class sess|
  1023. * |------------------| <== iscsi_hostdata(host->hostdata)
  1024. * | iscsi_session |
  1025. * *------------------*
  1026. */
  1027. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1028. _sz % sizeof(unsigned long))
  1029. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1030. /**
  1031. * iscsi_session_setup - create iscsi cls session and host and session
  1032. * @scsit: scsi transport template
  1033. * @iscsit: iscsi transport template
  1034. * @initial_cmdsn: initial CmdSN
  1035. * @hostno: host no allocated
  1036. *
  1037. * This can be used by software iscsi_transports that allocate
  1038. * a session per scsi host.
  1039. **/
  1040. struct iscsi_cls_session *
  1041. iscsi_session_setup(struct iscsi_transport *iscsit,
  1042. struct scsi_transport_template *scsit,
  1043. int cmd_task_size, int mgmt_task_size,
  1044. uint32_t initial_cmdsn, uint32_t *hostno)
  1045. {
  1046. struct Scsi_Host *shost;
  1047. struct iscsi_session *session;
  1048. struct iscsi_cls_session *cls_session;
  1049. int cmd_i;
  1050. shost = scsi_host_alloc(iscsit->host_template,
  1051. hostdata_privsize(sizeof(*session)));
  1052. if (!shost)
  1053. return NULL;
  1054. shost->max_id = 1;
  1055. shost->max_channel = 0;
  1056. shost->max_lun = iscsit->max_lun;
  1057. shost->max_cmd_len = iscsit->max_cmd_len;
  1058. shost->transportt = scsit;
  1059. shost->transportt->create_work_queue = 1;
  1060. *hostno = shost->host_no;
  1061. session = iscsi_hostdata(shost->hostdata);
  1062. memset(session, 0, sizeof(struct iscsi_session));
  1063. session->host = shost;
  1064. session->state = ISCSI_STATE_FREE;
  1065. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1066. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1067. session->cmdsn = initial_cmdsn;
  1068. session->exp_cmdsn = initial_cmdsn + 1;
  1069. session->max_cmdsn = initial_cmdsn + 1;
  1070. session->max_r2t = 1;
  1071. session->tt = iscsit;
  1072. /* initialize SCSI PDU commands pool */
  1073. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1074. (void***)&session->cmds,
  1075. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1076. goto cmdpool_alloc_fail;
  1077. /* pre-format cmds pool with ITT */
  1078. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1079. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1080. if (cmd_task_size)
  1081. ctask->dd_data = &ctask[1];
  1082. ctask->itt = cmd_i;
  1083. }
  1084. spin_lock_init(&session->lock);
  1085. INIT_LIST_HEAD(&session->connections);
  1086. /* initialize immediate command pool */
  1087. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1088. (void***)&session->mgmt_cmds,
  1089. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1090. goto mgmtpool_alloc_fail;
  1091. /* pre-format immediate cmds pool with ITT */
  1092. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1093. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1094. if (mgmt_task_size)
  1095. mtask->dd_data = &mtask[1];
  1096. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1097. mtask->data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH,
  1098. GFP_KERNEL);
  1099. if (!mtask->data) {
  1100. int j;
  1101. for (j = 0; j < cmd_i; j++)
  1102. kfree(session->mgmt_cmds[j]->data);
  1103. goto immdata_alloc_fail;
  1104. }
  1105. }
  1106. if (scsi_add_host(shost, NULL))
  1107. goto add_host_fail;
  1108. cls_session = iscsi_create_session(shost, iscsit, 0);
  1109. if (!cls_session)
  1110. goto cls_session_fail;
  1111. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1112. return cls_session;
  1113. cls_session_fail:
  1114. scsi_remove_host(shost);
  1115. add_host_fail:
  1116. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
  1117. kfree(session->mgmt_cmds[cmd_i]->data);
  1118. immdata_alloc_fail:
  1119. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1120. mgmtpool_alloc_fail:
  1121. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1122. cmdpool_alloc_fail:
  1123. scsi_host_put(shost);
  1124. return NULL;
  1125. }
  1126. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1127. /**
  1128. * iscsi_session_teardown - destroy session, host, and cls_session
  1129. * shost: scsi host
  1130. *
  1131. * This can be used by software iscsi_transports that allocate
  1132. * a session per scsi host.
  1133. **/
  1134. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1135. {
  1136. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1137. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1138. int cmd_i;
  1139. scsi_remove_host(shost);
  1140. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
  1141. kfree(session->mgmt_cmds[cmd_i]->data);
  1142. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1143. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1144. iscsi_destroy_session(cls_session);
  1145. scsi_host_put(shost);
  1146. }
  1147. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1148. /**
  1149. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1150. * @cls_session: iscsi_cls_session
  1151. * @conn_idx: cid
  1152. **/
  1153. struct iscsi_cls_conn *
  1154. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1155. {
  1156. struct iscsi_session *session = class_to_transport_session(cls_session);
  1157. struct iscsi_conn *conn;
  1158. struct iscsi_cls_conn *cls_conn;
  1159. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1160. if (!cls_conn)
  1161. return NULL;
  1162. conn = cls_conn->dd_data;
  1163. memset(conn, 0, sizeof(*conn));
  1164. conn->session = session;
  1165. conn->cls_conn = cls_conn;
  1166. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1167. conn->id = conn_idx;
  1168. conn->exp_statsn = 0;
  1169. conn->tmabort_state = TMABORT_INITIAL;
  1170. INIT_LIST_HEAD(&conn->run_list);
  1171. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1172. /* initialize general xmit PDU commands queue */
  1173. conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*),
  1174. GFP_KERNEL, NULL);
  1175. if (conn->xmitqueue == ERR_PTR(-ENOMEM))
  1176. goto xmitqueue_alloc_fail;
  1177. /* initialize general immediate & non-immediate PDU commands queue */
  1178. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1179. GFP_KERNEL, NULL);
  1180. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1181. goto immqueue_alloc_fail;
  1182. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1183. GFP_KERNEL, NULL);
  1184. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1185. goto mgmtqueue_alloc_fail;
  1186. INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
  1187. /* allocate login_mtask used for the login/text sequences */
  1188. spin_lock_bh(&session->lock);
  1189. if (!__kfifo_get(session->mgmtpool.queue,
  1190. (void*)&conn->login_mtask,
  1191. sizeof(void*))) {
  1192. spin_unlock_bh(&session->lock);
  1193. goto login_mtask_alloc_fail;
  1194. }
  1195. spin_unlock_bh(&session->lock);
  1196. init_timer(&conn->tmabort_timer);
  1197. mutex_init(&conn->xmitmutex);
  1198. init_waitqueue_head(&conn->ehwait);
  1199. return cls_conn;
  1200. login_mtask_alloc_fail:
  1201. kfifo_free(conn->mgmtqueue);
  1202. mgmtqueue_alloc_fail:
  1203. kfifo_free(conn->immqueue);
  1204. immqueue_alloc_fail:
  1205. kfifo_free(conn->xmitqueue);
  1206. xmitqueue_alloc_fail:
  1207. iscsi_destroy_conn(cls_conn);
  1208. return NULL;
  1209. }
  1210. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1211. /**
  1212. * iscsi_conn_teardown - teardown iscsi connection
  1213. * cls_conn: iscsi class connection
  1214. *
  1215. * TODO: we may need to make this into a two step process
  1216. * like scsi-mls remove + put host
  1217. */
  1218. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1219. {
  1220. struct iscsi_conn *conn = cls_conn->dd_data;
  1221. struct iscsi_session *session = conn->session;
  1222. unsigned long flags;
  1223. mutex_lock(&conn->xmitmutex);
  1224. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1225. if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE) {
  1226. if (session->tt->suspend_conn_recv)
  1227. session->tt->suspend_conn_recv(conn);
  1228. session->tt->terminate_conn(conn);
  1229. }
  1230. spin_lock_bh(&session->lock);
  1231. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1232. if (session->leadconn == conn) {
  1233. /*
  1234. * leading connection? then give up on recovery.
  1235. */
  1236. session->state = ISCSI_STATE_TERMINATE;
  1237. wake_up(&conn->ehwait);
  1238. }
  1239. spin_unlock_bh(&session->lock);
  1240. mutex_unlock(&conn->xmitmutex);
  1241. /*
  1242. * Block until all in-progress commands for this connection
  1243. * time out or fail.
  1244. */
  1245. for (;;) {
  1246. spin_lock_irqsave(session->host->host_lock, flags);
  1247. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1248. spin_unlock_irqrestore(session->host->host_lock, flags);
  1249. break;
  1250. }
  1251. spin_unlock_irqrestore(session->host->host_lock, flags);
  1252. msleep_interruptible(500);
  1253. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1254. "host_failed %d\n", session->host->host_busy,
  1255. session->host->host_failed);
  1256. /*
  1257. * force eh_abort() to unblock
  1258. */
  1259. wake_up(&conn->ehwait);
  1260. }
  1261. spin_lock_bh(&session->lock);
  1262. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1263. sizeof(void*));
  1264. list_del(&conn->item);
  1265. if (list_empty(&session->connections))
  1266. session->leadconn = NULL;
  1267. if (session->leadconn && session->leadconn == conn)
  1268. session->leadconn = container_of(session->connections.next,
  1269. struct iscsi_conn, item);
  1270. if (session->leadconn == NULL)
  1271. /* no connections exits.. reset sequencing */
  1272. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1273. spin_unlock_bh(&session->lock);
  1274. kfifo_free(conn->xmitqueue);
  1275. kfifo_free(conn->immqueue);
  1276. kfifo_free(conn->mgmtqueue);
  1277. iscsi_destroy_conn(cls_conn);
  1278. }
  1279. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1280. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1281. {
  1282. struct iscsi_conn *conn = cls_conn->dd_data;
  1283. struct iscsi_session *session = conn->session;
  1284. if (session == NULL) {
  1285. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1286. return -EPERM;
  1287. }
  1288. spin_lock_bh(&session->lock);
  1289. conn->c_stage = ISCSI_CONN_STARTED;
  1290. session->state = ISCSI_STATE_LOGGED_IN;
  1291. switch(conn->stop_stage) {
  1292. case STOP_CONN_RECOVER:
  1293. /*
  1294. * unblock eh_abort() if it is blocked. re-try all
  1295. * commands after successful recovery
  1296. */
  1297. session->conn_cnt++;
  1298. conn->stop_stage = 0;
  1299. conn->tmabort_state = TMABORT_INITIAL;
  1300. session->age++;
  1301. session->recovery_failed = 0;
  1302. spin_unlock_bh(&session->lock);
  1303. iscsi_unblock_session(session_to_cls(session));
  1304. wake_up(&conn->ehwait);
  1305. return 0;
  1306. case STOP_CONN_TERM:
  1307. session->conn_cnt++;
  1308. conn->stop_stage = 0;
  1309. break;
  1310. case STOP_CONN_SUSPEND:
  1311. conn->stop_stage = 0;
  1312. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1313. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1314. break;
  1315. default:
  1316. break;
  1317. }
  1318. spin_unlock_bh(&session->lock);
  1319. return 0;
  1320. }
  1321. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1322. static void
  1323. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1324. {
  1325. struct iscsi_mgmt_task *mtask, *tmp;
  1326. /* handle pending */
  1327. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1328. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1329. if (mtask == conn->login_mtask)
  1330. continue;
  1331. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1332. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1333. sizeof(void*));
  1334. }
  1335. /* handle running */
  1336. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1337. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1338. list_del(&mtask->running);
  1339. if (mtask == conn->login_mtask)
  1340. continue;
  1341. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1342. sizeof(void*));
  1343. }
  1344. conn->mtask = NULL;
  1345. }
  1346. /* Fail commands. Mutex and session lock held and recv side suspended */
  1347. static void fail_all_commands(struct iscsi_conn *conn)
  1348. {
  1349. struct iscsi_cmd_task *ctask, *tmp;
  1350. /* flush pending */
  1351. while (__kfifo_get(conn->xmitqueue, (void*)&ctask, sizeof(void*))) {
  1352. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1353. ctask->itt);
  1354. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1355. }
  1356. /* fail all other running */
  1357. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1358. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1359. ctask->sc, ctask->itt);
  1360. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1361. }
  1362. conn->ctask = NULL;
  1363. }
  1364. void iscsi_start_session_recovery(struct iscsi_session *session,
  1365. struct iscsi_conn *conn, int flag)
  1366. {
  1367. int old_stop_stage;
  1368. spin_lock_bh(&session->lock);
  1369. if (conn->stop_stage == STOP_CONN_TERM) {
  1370. spin_unlock_bh(&session->lock);
  1371. return;
  1372. }
  1373. /*
  1374. * When this is called for the in_login state, we only want to clean
  1375. * up the login task and connection.
  1376. */
  1377. if (conn->stop_stage != STOP_CONN_RECOVER)
  1378. session->conn_cnt--;
  1379. old_stop_stage = conn->stop_stage;
  1380. conn->stop_stage = flag;
  1381. spin_unlock_bh(&session->lock);
  1382. if (session->tt->suspend_conn_recv)
  1383. session->tt->suspend_conn_recv(conn);
  1384. mutex_lock(&conn->xmitmutex);
  1385. spin_lock_bh(&session->lock);
  1386. conn->c_stage = ISCSI_CONN_STOPPED;
  1387. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1388. if (session->conn_cnt == 0 || session->leadconn == conn)
  1389. session->state = ISCSI_STATE_FAILED;
  1390. spin_unlock_bh(&session->lock);
  1391. session->tt->terminate_conn(conn);
  1392. /*
  1393. * flush queues.
  1394. */
  1395. spin_lock_bh(&session->lock);
  1396. fail_all_commands(conn);
  1397. flush_control_queues(session, conn);
  1398. spin_unlock_bh(&session->lock);
  1399. /*
  1400. * for connection level recovery we should not calculate
  1401. * header digest. conn->hdr_size used for optimization
  1402. * in hdr_extract() and will be re-negotiated at
  1403. * set_param() time.
  1404. */
  1405. if (flag == STOP_CONN_RECOVER) {
  1406. conn->hdrdgst_en = 0;
  1407. conn->datadgst_en = 0;
  1408. /*
  1409. * if this is called from the eh and and from userspace
  1410. * then we only need to block once.
  1411. */
  1412. if (session->state == ISCSI_STATE_FAILED &&
  1413. old_stop_stage != STOP_CONN_RECOVER)
  1414. iscsi_block_session(session_to_cls(session));
  1415. }
  1416. mutex_unlock(&conn->xmitmutex);
  1417. }
  1418. EXPORT_SYMBOL_GPL(iscsi_start_session_recovery);
  1419. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1420. {
  1421. struct iscsi_conn *conn = cls_conn->dd_data;
  1422. struct iscsi_session *session = conn->session;
  1423. switch (flag) {
  1424. case STOP_CONN_RECOVER:
  1425. case STOP_CONN_TERM:
  1426. iscsi_start_session_recovery(session, conn, flag);
  1427. break;
  1428. case STOP_CONN_SUSPEND:
  1429. if (session->tt->suspend_conn_recv)
  1430. session->tt->suspend_conn_recv(conn);
  1431. mutex_lock(&conn->xmitmutex);
  1432. spin_lock_bh(&session->lock);
  1433. conn->stop_stage = flag;
  1434. conn->c_stage = ISCSI_CONN_STOPPED;
  1435. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1436. spin_unlock_bh(&session->lock);
  1437. mutex_unlock(&conn->xmitmutex);
  1438. break;
  1439. default:
  1440. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1441. }
  1442. }
  1443. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1444. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1445. struct iscsi_cls_conn *cls_conn, int is_leading)
  1446. {
  1447. struct iscsi_session *session = class_to_transport_session(cls_session);
  1448. struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
  1449. /* lookup for existing connection */
  1450. spin_lock_bh(&session->lock);
  1451. list_for_each_entry(tmp, &session->connections, item) {
  1452. if (tmp == conn) {
  1453. if (conn->c_stage != ISCSI_CONN_STOPPED ||
  1454. conn->stop_stage == STOP_CONN_TERM) {
  1455. printk(KERN_ERR "iscsi: can't bind "
  1456. "non-stopped connection (%d:%d)\n",
  1457. conn->c_stage, conn->stop_stage);
  1458. spin_unlock_bh(&session->lock);
  1459. return -EIO;
  1460. }
  1461. break;
  1462. }
  1463. }
  1464. if (tmp != conn) {
  1465. /* bind new iSCSI connection to session */
  1466. conn->session = session;
  1467. list_add(&conn->item, &session->connections);
  1468. }
  1469. spin_unlock_bh(&session->lock);
  1470. if (is_leading)
  1471. session->leadconn = conn;
  1472. /*
  1473. * Unblock xmitworker(), Login Phase will pass through.
  1474. */
  1475. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1476. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1477. return 0;
  1478. }
  1479. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1480. MODULE_AUTHOR("Mike Christie");
  1481. MODULE_DESCRIPTION("iSCSI library functions");
  1482. MODULE_LICENSE("GPL");