libiscsi.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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->state = ISCSI_TASK_COMPLETED;
  174. ctask->sc = NULL;
  175. list_del_init(&ctask->running);
  176. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  177. sc->scsi_done(sc);
  178. }
  179. /**
  180. * iscsi_cmd_rsp - SCSI Command Response processing
  181. * @conn: iscsi connection
  182. * @hdr: iscsi header
  183. * @ctask: scsi command task
  184. * @data: cmd data buffer
  185. * @datalen: len of buffer
  186. *
  187. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  188. * then completes the command and task.
  189. **/
  190. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  191. struct iscsi_cmd_task *ctask, char *data,
  192. int datalen)
  193. {
  194. int rc;
  195. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  196. struct iscsi_session *session = conn->session;
  197. struct scsi_cmnd *sc = ctask->sc;
  198. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  199. if (rc) {
  200. sc->result = DID_ERROR << 16;
  201. goto out;
  202. }
  203. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  204. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  205. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  206. sc->result = DID_ERROR << 16;
  207. goto out;
  208. }
  209. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  210. int senselen;
  211. if (datalen < 2) {
  212. invalid_datalen:
  213. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  214. "invalid data buffer size of %d\n", datalen);
  215. sc->result = DID_BAD_TARGET << 16;
  216. goto out;
  217. }
  218. senselen = (data[0] << 8) | data[1];
  219. if (datalen < senselen)
  220. goto invalid_datalen;
  221. memcpy(sc->sense_buffer, data + 2,
  222. min(senselen, SCSI_SENSE_BUFFERSIZE));
  223. debug_scsi("copied %d bytes of sense\n",
  224. min(senselen, SCSI_SENSE_BUFFERSIZE));
  225. }
  226. if (sc->sc_data_direction == DMA_TO_DEVICE)
  227. goto out;
  228. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  229. int res_count = be32_to_cpu(rhdr->residual_count);
  230. if (res_count > 0 && res_count <= sc->request_bufflen)
  231. sc->resid = res_count;
  232. else
  233. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  234. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  235. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  236. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  237. sc->resid = be32_to_cpu(rhdr->residual_count);
  238. out:
  239. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  240. (long)sc, sc->result, ctask->itt);
  241. conn->scsirsp_pdus_cnt++;
  242. iscsi_complete_command(conn->session, ctask);
  243. return rc;
  244. }
  245. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  246. {
  247. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  248. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  249. conn->tmfrsp_pdus_cnt++;
  250. if (conn->tmabort_state != TMABORT_INITIAL)
  251. return;
  252. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  253. conn->tmabort_state = TMABORT_SUCCESS;
  254. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  255. conn->tmabort_state = TMABORT_NOT_FOUND;
  256. else
  257. conn->tmabort_state = TMABORT_FAILED;
  258. wake_up(&conn->ehwait);
  259. }
  260. /**
  261. * __iscsi_complete_pdu - complete pdu
  262. * @conn: iscsi conn
  263. * @hdr: iscsi header
  264. * @data: data buffer
  265. * @datalen: len of data buffer
  266. *
  267. * Completes pdu processing by freeing any resources allocated at
  268. * queuecommand or send generic. session lock must be held and verify
  269. * itt must have been called.
  270. */
  271. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  272. char *data, int datalen)
  273. {
  274. struct iscsi_session *session = conn->session;
  275. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  276. struct iscsi_cmd_task *ctask;
  277. struct iscsi_mgmt_task *mtask;
  278. uint32_t itt;
  279. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
  280. itt = hdr->itt & ISCSI_ITT_MASK;
  281. else
  282. itt = hdr->itt;
  283. if (itt < session->cmds_max) {
  284. ctask = session->cmds[itt];
  285. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  286. opcode, conn->id, ctask->itt, datalen);
  287. switch(opcode) {
  288. case ISCSI_OP_SCSI_CMD_RSP:
  289. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  290. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  291. datalen);
  292. break;
  293. case ISCSI_OP_SCSI_DATA_IN:
  294. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  295. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  296. conn->scsirsp_pdus_cnt++;
  297. iscsi_complete_command(session, ctask);
  298. }
  299. break;
  300. case ISCSI_OP_R2T:
  301. /* LLD handles this for now */
  302. break;
  303. default:
  304. rc = ISCSI_ERR_BAD_OPCODE;
  305. break;
  306. }
  307. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  308. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  309. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  310. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  311. opcode, conn->id, mtask->itt, datalen);
  312. rc = iscsi_check_assign_cmdsn(session,
  313. (struct iscsi_nopin*)hdr);
  314. if (rc)
  315. goto done;
  316. switch(opcode) {
  317. case ISCSI_OP_LOGOUT_RSP:
  318. if (datalen) {
  319. rc = ISCSI_ERR_PROTO;
  320. break;
  321. }
  322. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  323. /* fall through */
  324. case ISCSI_OP_LOGIN_RSP:
  325. case ISCSI_OP_TEXT_RSP:
  326. /*
  327. * login related PDU's exp_statsn is handled in
  328. * userspace
  329. */
  330. rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
  331. list_del(&mtask->running);
  332. if (conn->login_mtask != mtask)
  333. __kfifo_put(session->mgmtpool.queue,
  334. (void*)&mtask, sizeof(void*));
  335. break;
  336. case ISCSI_OP_SCSI_TMFUNC_RSP:
  337. if (datalen) {
  338. rc = ISCSI_ERR_PROTO;
  339. break;
  340. }
  341. iscsi_tmf_rsp(conn, hdr);
  342. break;
  343. case ISCSI_OP_NOOP_IN:
  344. if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
  345. rc = ISCSI_ERR_PROTO;
  346. break;
  347. }
  348. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  349. rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
  350. list_del(&mtask->running);
  351. if (conn->login_mtask != mtask)
  352. __kfifo_put(session->mgmtpool.queue,
  353. (void*)&mtask, sizeof(void*));
  354. break;
  355. default:
  356. rc = ISCSI_ERR_BAD_OPCODE;
  357. break;
  358. }
  359. } else if (itt == ISCSI_RESERVED_TAG) {
  360. switch(opcode) {
  361. case ISCSI_OP_NOOP_IN:
  362. if (!datalen) {
  363. rc = iscsi_check_assign_cmdsn(session,
  364. (struct iscsi_nopin*)hdr);
  365. if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
  366. rc = iscsi_recv_pdu(conn->cls_conn,
  367. hdr, NULL, 0);
  368. } else
  369. rc = ISCSI_ERR_PROTO;
  370. break;
  371. case ISCSI_OP_REJECT:
  372. /* we need sth like iscsi_reject_rsp()*/
  373. case ISCSI_OP_ASYNC_EVENT:
  374. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  375. /* we need sth like iscsi_async_event_rsp() */
  376. rc = ISCSI_ERR_BAD_OPCODE;
  377. break;
  378. default:
  379. rc = ISCSI_ERR_BAD_OPCODE;
  380. break;
  381. }
  382. } else
  383. rc = ISCSI_ERR_BAD_ITT;
  384. done:
  385. return rc;
  386. }
  387. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  388. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  389. char *data, int datalen)
  390. {
  391. int rc;
  392. spin_lock(&conn->session->lock);
  393. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  394. spin_unlock(&conn->session->lock);
  395. return rc;
  396. }
  397. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  398. /* verify itt (itt encoding: age+cid+itt) */
  399. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  400. uint32_t *ret_itt)
  401. {
  402. struct iscsi_session *session = conn->session;
  403. struct iscsi_cmd_task *ctask;
  404. uint32_t itt;
  405. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  406. if ((hdr->itt & ISCSI_AGE_MASK) !=
  407. (session->age << ISCSI_AGE_SHIFT)) {
  408. printk(KERN_ERR "iscsi: received itt %x expected "
  409. "session age (%x)\n", hdr->itt,
  410. session->age & ISCSI_AGE_MASK);
  411. return ISCSI_ERR_BAD_ITT;
  412. }
  413. if ((hdr->itt & ISCSI_CID_MASK) !=
  414. (conn->id << ISCSI_CID_SHIFT)) {
  415. printk(KERN_ERR "iscsi: received itt %x, expected "
  416. "CID (%x)\n", hdr->itt, conn->id);
  417. return ISCSI_ERR_BAD_ITT;
  418. }
  419. itt = hdr->itt & ISCSI_ITT_MASK;
  420. } else
  421. itt = hdr->itt;
  422. if (itt < session->cmds_max) {
  423. ctask = session->cmds[itt];
  424. if (!ctask->sc) {
  425. printk(KERN_INFO "iscsi: dropping ctask with "
  426. "itt 0x%x\n", ctask->itt);
  427. /* force drop */
  428. return ISCSI_ERR_NO_SCSI_CMD;
  429. }
  430. if (ctask->sc->SCp.phase != session->age) {
  431. printk(KERN_ERR "iscsi: ctask's session age %d, "
  432. "expected %d\n", ctask->sc->SCp.phase,
  433. session->age);
  434. return ISCSI_ERR_SESSION_FAILED;
  435. }
  436. }
  437. *ret_itt = itt;
  438. return 0;
  439. }
  440. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  441. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  442. {
  443. struct iscsi_session *session = conn->session;
  444. unsigned long flags;
  445. spin_lock_irqsave(&session->lock, flags);
  446. if (session->state == ISCSI_STATE_FAILED) {
  447. spin_unlock_irqrestore(&session->lock, flags);
  448. return;
  449. }
  450. if (conn->stop_stage == 0)
  451. session->state = ISCSI_STATE_FAILED;
  452. spin_unlock_irqrestore(&session->lock, flags);
  453. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  454. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  455. iscsi_conn_error(conn->cls_conn, err);
  456. }
  457. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  458. /**
  459. * iscsi_data_xmit - xmit any command into the scheduled connection
  460. * @conn: iscsi connection
  461. *
  462. * Notes:
  463. * The function can return -EAGAIN in which case the caller must
  464. * re-schedule it again later or recover. '0' return code means
  465. * successful xmit.
  466. **/
  467. static int iscsi_data_xmit(struct iscsi_conn *conn)
  468. {
  469. struct iscsi_transport *tt;
  470. int rc = 0;
  471. if (unlikely(conn->suspend_tx)) {
  472. debug_scsi("conn %d Tx suspended!\n", conn->id);
  473. return -ENODATA;
  474. }
  475. tt = conn->session->tt;
  476. /*
  477. * Transmit in the following order:
  478. *
  479. * 1) un-finished xmit (ctask or mtask)
  480. * 2) immediate control PDUs
  481. * 3) write data
  482. * 4) SCSI commands
  483. * 5) non-immediate control PDUs
  484. *
  485. * No need to lock around __kfifo_get as long as
  486. * there's one producer and one consumer.
  487. */
  488. BUG_ON(conn->ctask && conn->mtask);
  489. if (conn->ctask) {
  490. rc = tt->xmit_cmd_task(conn, conn->ctask);
  491. if (rc)
  492. goto again;
  493. /* done with this in-progress ctask */
  494. conn->ctask = NULL;
  495. }
  496. if (conn->mtask) {
  497. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  498. if (rc)
  499. goto again;
  500. /* done with this in-progress mtask */
  501. conn->mtask = NULL;
  502. }
  503. /* process immediate first */
  504. if (unlikely(__kfifo_len(conn->immqueue))) {
  505. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  506. sizeof(void*))) {
  507. spin_lock_bh(&conn->session->lock);
  508. list_add_tail(&conn->mtask->running,
  509. &conn->mgmt_run_list);
  510. spin_unlock_bh(&conn->session->lock);
  511. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  512. if (rc)
  513. goto again;
  514. }
  515. /* done with this mtask */
  516. conn->mtask = NULL;
  517. }
  518. /* process command queue */
  519. spin_lock_bh(&conn->session->lock);
  520. while (!list_empty(&conn->xmitqueue)) {
  521. /*
  522. * iscsi tcp may readd the task to the xmitqueue to send
  523. * write data
  524. */
  525. conn->ctask = list_entry(conn->xmitqueue.next,
  526. struct iscsi_cmd_task, running);
  527. conn->ctask->state = ISCSI_TASK_RUNNING;
  528. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  529. spin_unlock_bh(&conn->session->lock);
  530. rc = tt->xmit_cmd_task(conn, conn->ctask);
  531. if (rc)
  532. goto again;
  533. spin_lock_bh(&conn->session->lock);
  534. }
  535. spin_unlock_bh(&conn->session->lock);
  536. /* done with this ctask */
  537. conn->ctask = NULL;
  538. /* process the rest control plane PDUs, if any */
  539. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  540. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  541. sizeof(void*))) {
  542. spin_lock_bh(&conn->session->lock);
  543. list_add_tail(&conn->mtask->running,
  544. &conn->mgmt_run_list);
  545. spin_unlock_bh(&conn->session->lock);
  546. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  547. if (rc)
  548. goto again;
  549. }
  550. /* done with this mtask */
  551. conn->mtask = NULL;
  552. }
  553. return -ENODATA;
  554. again:
  555. if (unlikely(conn->suspend_tx))
  556. return -ENODATA;
  557. return rc;
  558. }
  559. static void iscsi_xmitworker(void *data)
  560. {
  561. struct iscsi_conn *conn = data;
  562. int rc;
  563. /*
  564. * serialize Xmit worker on a per-connection basis.
  565. */
  566. mutex_lock(&conn->xmitmutex);
  567. do {
  568. rc = iscsi_data_xmit(conn);
  569. } while (rc >= 0 || rc == -EAGAIN);
  570. mutex_unlock(&conn->xmitmutex);
  571. }
  572. enum {
  573. FAILURE_BAD_HOST = 1,
  574. FAILURE_SESSION_FAILED,
  575. FAILURE_SESSION_FREED,
  576. FAILURE_WINDOW_CLOSED,
  577. FAILURE_SESSION_TERMINATE,
  578. FAILURE_SESSION_IN_RECOVERY,
  579. FAILURE_SESSION_RECOVERY_TIMEOUT,
  580. };
  581. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  582. {
  583. struct Scsi_Host *host;
  584. int reason = 0;
  585. struct iscsi_session *session;
  586. struct iscsi_conn *conn;
  587. struct iscsi_cmd_task *ctask = NULL;
  588. sc->scsi_done = done;
  589. sc->result = 0;
  590. host = sc->device->host;
  591. session = iscsi_hostdata(host->hostdata);
  592. spin_lock(&session->lock);
  593. /*
  594. * ISCSI_STATE_FAILED is a temp. state. The recovery
  595. * code will decide what is best to do with command queued
  596. * during this time
  597. */
  598. if (session->state != ISCSI_STATE_LOGGED_IN &&
  599. session->state != ISCSI_STATE_FAILED) {
  600. /*
  601. * to handle the race between when we set the recovery state
  602. * and block the session we requeue here (commands could
  603. * be entering our queuecommand while a block is starting
  604. * up because the block code is not locked)
  605. */
  606. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  607. reason = FAILURE_SESSION_IN_RECOVERY;
  608. goto reject;
  609. }
  610. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  611. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  612. else if (session->state == ISCSI_STATE_TERMINATE)
  613. reason = FAILURE_SESSION_TERMINATE;
  614. else
  615. reason = FAILURE_SESSION_FREED;
  616. goto fault;
  617. }
  618. /*
  619. * Check for iSCSI window and take care of CmdSN wrap-around
  620. */
  621. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  622. reason = FAILURE_WINDOW_CLOSED;
  623. goto reject;
  624. }
  625. conn = session->leadconn;
  626. __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  627. sc->SCp.phase = session->age;
  628. sc->SCp.ptr = (char *)ctask;
  629. ctask->state = ISCSI_TASK_PENDING;
  630. ctask->mtask = NULL;
  631. ctask->conn = conn;
  632. ctask->sc = sc;
  633. INIT_LIST_HEAD(&ctask->running);
  634. ctask->total_length = sc->request_bufflen;
  635. iscsi_prep_scsi_cmd_pdu(ctask);
  636. session->tt->init_cmd_task(ctask);
  637. list_add_tail(&ctask->running, &conn->xmitqueue);
  638. debug_scsi(
  639. "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
  640. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  641. conn->id, (long)sc, ctask->itt, sc->request_bufflen,
  642. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  643. spin_unlock(&session->lock);
  644. scsi_queue_work(host, &conn->xmitwork);
  645. return 0;
  646. reject:
  647. spin_unlock(&session->lock);
  648. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  649. return SCSI_MLQUEUE_HOST_BUSY;
  650. fault:
  651. spin_unlock(&session->lock);
  652. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  653. sc->cmnd[0], reason);
  654. sc->result = (DID_NO_CONNECT << 16);
  655. sc->resid = sc->request_bufflen;
  656. sc->scsi_done(sc);
  657. return 0;
  658. }
  659. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  660. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  661. {
  662. if (depth > ISCSI_MAX_CMD_PER_LUN)
  663. depth = ISCSI_MAX_CMD_PER_LUN;
  664. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  665. return sdev->queue_depth;
  666. }
  667. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  668. static int
  669. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  670. char *data, uint32_t data_size)
  671. {
  672. struct iscsi_session *session = conn->session;
  673. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  674. struct iscsi_mgmt_task *mtask;
  675. spin_lock_bh(&session->lock);
  676. if (session->state == ISCSI_STATE_TERMINATE) {
  677. spin_unlock_bh(&session->lock);
  678. return -EPERM;
  679. }
  680. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  681. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  682. /*
  683. * Login and Text are sent serially, in
  684. * request-followed-by-response sequence.
  685. * Same mtask can be used. Same ITT must be used.
  686. * Note that login_mtask is preallocated at conn_create().
  687. */
  688. mtask = conn->login_mtask;
  689. else {
  690. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  691. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  692. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  693. if (!__kfifo_get(session->mgmtpool.queue,
  694. (void*)&mtask, sizeof(void*))) {
  695. spin_unlock_bh(&session->lock);
  696. return -ENOSPC;
  697. }
  698. }
  699. /*
  700. * pre-format CmdSN for outgoing PDU.
  701. */
  702. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  703. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  704. (session->age << ISCSI_AGE_SHIFT);
  705. nop->cmdsn = cpu_to_be32(session->cmdsn);
  706. if (conn->c_stage == ISCSI_CONN_STARTED &&
  707. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  708. session->cmdsn++;
  709. } else
  710. /* do not advance CmdSN */
  711. nop->cmdsn = cpu_to_be32(session->cmdsn);
  712. if (data_size) {
  713. memcpy(mtask->data, data, data_size);
  714. mtask->data_count = data_size;
  715. } else
  716. mtask->data_count = 0;
  717. INIT_LIST_HEAD(&mtask->running);
  718. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  719. if (session->tt->init_mgmt_task)
  720. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  721. spin_unlock_bh(&session->lock);
  722. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  723. hdr->opcode, hdr->itt, data_size);
  724. /*
  725. * since send_pdu() could be called at least from two contexts,
  726. * we need to serialize __kfifo_put, so we don't have to take
  727. * additional lock on fast data-path
  728. */
  729. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  730. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  731. else
  732. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  733. scsi_queue_work(session->host, &conn->xmitwork);
  734. return 0;
  735. }
  736. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  737. char *data, uint32_t data_size)
  738. {
  739. struct iscsi_conn *conn = cls_conn->dd_data;
  740. int rc;
  741. mutex_lock(&conn->xmitmutex);
  742. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  743. mutex_unlock(&conn->xmitmutex);
  744. return rc;
  745. }
  746. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  747. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  748. {
  749. struct iscsi_session *session = class_to_transport_session(cls_session);
  750. struct iscsi_conn *conn = session->leadconn;
  751. spin_lock_bh(&session->lock);
  752. if (session->state != ISCSI_STATE_LOGGED_IN) {
  753. session->state = ISCSI_STATE_RECOVERY_FAILED;
  754. if (conn)
  755. wake_up(&conn->ehwait);
  756. }
  757. spin_unlock_bh(&session->lock);
  758. }
  759. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  760. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  761. {
  762. struct Scsi_Host *host = sc->device->host;
  763. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  764. struct iscsi_conn *conn = session->leadconn;
  765. int fail_session = 0;
  766. spin_lock_bh(&session->lock);
  767. if (session->state == ISCSI_STATE_TERMINATE) {
  768. failed:
  769. debug_scsi("failing host reset: session terminated "
  770. "[CID %d age %d]", conn->id, session->age);
  771. spin_unlock_bh(&session->lock);
  772. return FAILED;
  773. }
  774. if (sc->SCp.phase == session->age) {
  775. debug_scsi("failing connection CID %d due to SCSI host reset",
  776. conn->id);
  777. fail_session = 1;
  778. }
  779. spin_unlock_bh(&session->lock);
  780. /*
  781. * we drop the lock here but the leadconn cannot be destoyed while
  782. * we are in the scsi eh
  783. */
  784. if (fail_session)
  785. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  786. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  787. wait_event_interruptible(conn->ehwait,
  788. session->state == ISCSI_STATE_TERMINATE ||
  789. session->state == ISCSI_STATE_LOGGED_IN ||
  790. session->state == ISCSI_STATE_RECOVERY_FAILED);
  791. if (signal_pending(current))
  792. flush_signals(current);
  793. spin_lock_bh(&session->lock);
  794. if (session->state == ISCSI_STATE_LOGGED_IN)
  795. printk(KERN_INFO "iscsi: host reset succeeded\n");
  796. else
  797. goto failed;
  798. spin_unlock_bh(&session->lock);
  799. return SUCCESS;
  800. }
  801. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  802. static void iscsi_tmabort_timedout(unsigned long data)
  803. {
  804. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  805. struct iscsi_conn *conn = ctask->conn;
  806. struct iscsi_session *session = conn->session;
  807. spin_lock(&session->lock);
  808. if (conn->tmabort_state == TMABORT_INITIAL) {
  809. conn->tmabort_state = TMABORT_TIMEDOUT;
  810. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  811. ctask->sc, ctask->itt);
  812. /* unblock eh_abort() */
  813. wake_up(&conn->ehwait);
  814. }
  815. spin_unlock(&session->lock);
  816. }
  817. /* must be called with the mutex lock */
  818. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  819. struct iscsi_cmd_task *ctask)
  820. {
  821. struct iscsi_conn *conn = ctask->conn;
  822. struct iscsi_session *session = conn->session;
  823. struct iscsi_tm *hdr = &conn->tmhdr;
  824. int rc;
  825. /*
  826. * ctask timed out but session is OK requests must be serialized.
  827. */
  828. memset(hdr, 0, sizeof(struct iscsi_tm));
  829. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  830. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  831. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  832. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  833. hdr->rtt = ctask->hdr->itt;
  834. hdr->refcmdsn = ctask->hdr->cmdsn;
  835. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  836. NULL, 0);
  837. if (rc) {
  838. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  839. debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
  840. return rc;
  841. }
  842. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  843. spin_lock_bh(&session->lock);
  844. ctask->mtask = (struct iscsi_mgmt_task *)
  845. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  846. ISCSI_MGMT_ITT_OFFSET];
  847. if (conn->tmabort_state == TMABORT_INITIAL) {
  848. conn->tmfcmd_pdus_cnt++;
  849. conn->tmabort_timer.expires = 10*HZ + jiffies;
  850. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  851. conn->tmabort_timer.data = (unsigned long)ctask;
  852. add_timer(&conn->tmabort_timer);
  853. debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
  854. }
  855. spin_unlock_bh(&session->lock);
  856. mutex_unlock(&conn->xmitmutex);
  857. /*
  858. * block eh thread until:
  859. *
  860. * 1) abort response
  861. * 2) abort timeout
  862. * 3) session is terminated or restarted or userspace has
  863. * given up on recovery
  864. */
  865. wait_event_interruptible(conn->ehwait,
  866. sc->SCp.phase != session->age ||
  867. session->state != ISCSI_STATE_LOGGED_IN ||
  868. conn->tmabort_state != TMABORT_INITIAL);
  869. if (signal_pending(current))
  870. flush_signals(current);
  871. del_timer_sync(&conn->tmabort_timer);
  872. mutex_lock(&conn->xmitmutex);
  873. return 0;
  874. }
  875. /*
  876. * xmit mutex and session lock must be held
  877. */
  878. static struct iscsi_mgmt_task *
  879. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  880. {
  881. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  882. struct iscsi_mgmt_task *task;
  883. debug_scsi("searching %d tasks\n", nr_tasks);
  884. for (i = 0; i < nr_tasks; i++) {
  885. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  886. debug_scsi("check task %u\n", task->itt);
  887. if (task->itt == itt) {
  888. debug_scsi("matched task\n");
  889. return task;
  890. }
  891. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  892. }
  893. return NULL;
  894. }
  895. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  896. {
  897. struct iscsi_conn *conn = ctask->conn;
  898. struct iscsi_session *session = conn->session;
  899. if (!ctask->mtask)
  900. return -EINVAL;
  901. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  902. list_del(&ctask->mtask->running);
  903. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  904. sizeof(void*));
  905. ctask->mtask = NULL;
  906. return 0;
  907. }
  908. /*
  909. * session lock and xmitmutex must be held
  910. */
  911. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  912. int err)
  913. {
  914. struct scsi_cmnd *sc;
  915. sc = ctask->sc;
  916. if (!sc)
  917. return;
  918. conn->session->tt->cleanup_cmd_task(conn, ctask);
  919. iscsi_ctask_mtask_cleanup(ctask);
  920. sc->result = err;
  921. sc->resid = sc->request_bufflen;
  922. iscsi_complete_command(conn->session, ctask);
  923. }
  924. int iscsi_eh_abort(struct scsi_cmnd *sc)
  925. {
  926. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  927. struct iscsi_conn *conn = ctask->conn;
  928. struct iscsi_session *session = conn->session;
  929. int rc;
  930. conn->eh_abort_cnt++;
  931. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  932. mutex_lock(&conn->xmitmutex);
  933. spin_lock_bh(&session->lock);
  934. /*
  935. * If we are not logged in or we have started a new session
  936. * then let the host reset code handle this
  937. */
  938. if (session->state != ISCSI_STATE_LOGGED_IN ||
  939. sc->SCp.phase != session->age)
  940. goto failed;
  941. /* ctask completed before time out */
  942. if (!ctask->sc) {
  943. spin_unlock_bh(&session->lock);
  944. debug_scsi("sc completed while abort in progress\n");
  945. goto success_rel_mutex;
  946. }
  947. /* what should we do here ? */
  948. if (conn->ctask == ctask) {
  949. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  950. "Failing abort\n", sc, ctask->itt);
  951. goto failed;
  952. }
  953. if (ctask->state == ISCSI_TASK_PENDING)
  954. goto success_cleanup;
  955. conn->tmabort_state = TMABORT_INITIAL;
  956. spin_unlock_bh(&session->lock);
  957. rc = iscsi_exec_abort_task(sc, ctask);
  958. spin_lock_bh(&session->lock);
  959. if (rc || sc->SCp.phase != session->age ||
  960. session->state != ISCSI_STATE_LOGGED_IN)
  961. goto failed;
  962. iscsi_ctask_mtask_cleanup(ctask);
  963. switch (conn->tmabort_state) {
  964. case TMABORT_SUCCESS:
  965. goto success_cleanup;
  966. case TMABORT_NOT_FOUND:
  967. if (!ctask->sc) {
  968. /* ctask completed before tmf abort response */
  969. spin_unlock_bh(&session->lock);
  970. debug_scsi("sc completed while abort in progress\n");
  971. goto success_rel_mutex;
  972. }
  973. /* fall through */
  974. default:
  975. /* timedout or failed */
  976. spin_unlock_bh(&session->lock);
  977. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  978. spin_lock_bh(&session->lock);
  979. goto failed;
  980. }
  981. success_cleanup:
  982. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  983. spin_unlock_bh(&session->lock);
  984. /*
  985. * clean up task if aborted. we have the xmitmutex so grab
  986. * the recv lock as a writer
  987. */
  988. write_lock_bh(conn->recv_lock);
  989. spin_lock(&session->lock);
  990. fail_command(conn, ctask, DID_ABORT << 16);
  991. spin_unlock(&session->lock);
  992. write_unlock_bh(conn->recv_lock);
  993. success_rel_mutex:
  994. mutex_unlock(&conn->xmitmutex);
  995. return SUCCESS;
  996. failed:
  997. spin_unlock_bh(&session->lock);
  998. mutex_unlock(&conn->xmitmutex);
  999. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1000. return FAILED;
  1001. }
  1002. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1003. int
  1004. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1005. {
  1006. int i;
  1007. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1008. if (*items == NULL)
  1009. return -ENOMEM;
  1010. q->max = max;
  1011. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1012. if (q->pool == NULL) {
  1013. kfree(*items);
  1014. return -ENOMEM;
  1015. }
  1016. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1017. GFP_KERNEL, NULL);
  1018. if (q->queue == ERR_PTR(-ENOMEM)) {
  1019. kfree(q->pool);
  1020. kfree(*items);
  1021. return -ENOMEM;
  1022. }
  1023. for (i = 0; i < max; i++) {
  1024. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1025. if (q->pool[i] == NULL) {
  1026. int j;
  1027. for (j = 0; j < i; j++)
  1028. kfree(q->pool[j]);
  1029. kfifo_free(q->queue);
  1030. kfree(q->pool);
  1031. kfree(*items);
  1032. return -ENOMEM;
  1033. }
  1034. memset(q->pool[i], 0, item_size);
  1035. (*items)[i] = q->pool[i];
  1036. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1037. }
  1038. return 0;
  1039. }
  1040. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1041. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1042. {
  1043. int i;
  1044. for (i = 0; i < q->max; i++)
  1045. kfree(items[i]);
  1046. kfree(q->pool);
  1047. kfree(items);
  1048. }
  1049. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1050. /*
  1051. * iSCSI Session's hostdata organization:
  1052. *
  1053. * *------------------* <== hostdata_session(host->hostdata)
  1054. * | ptr to class sess|
  1055. * |------------------| <== iscsi_hostdata(host->hostdata)
  1056. * | iscsi_session |
  1057. * *------------------*
  1058. */
  1059. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1060. _sz % sizeof(unsigned long))
  1061. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1062. /**
  1063. * iscsi_session_setup - create iscsi cls session and host and session
  1064. * @scsit: scsi transport template
  1065. * @iscsit: iscsi transport template
  1066. * @initial_cmdsn: initial CmdSN
  1067. * @hostno: host no allocated
  1068. *
  1069. * This can be used by software iscsi_transports that allocate
  1070. * a session per scsi host.
  1071. **/
  1072. struct iscsi_cls_session *
  1073. iscsi_session_setup(struct iscsi_transport *iscsit,
  1074. struct scsi_transport_template *scsit,
  1075. int cmd_task_size, int mgmt_task_size,
  1076. uint32_t initial_cmdsn, uint32_t *hostno)
  1077. {
  1078. struct Scsi_Host *shost;
  1079. struct iscsi_session *session;
  1080. struct iscsi_cls_session *cls_session;
  1081. int cmd_i;
  1082. shost = scsi_host_alloc(iscsit->host_template,
  1083. hostdata_privsize(sizeof(*session)));
  1084. if (!shost)
  1085. return NULL;
  1086. shost->max_id = 1;
  1087. shost->max_channel = 0;
  1088. shost->max_lun = iscsit->max_lun;
  1089. shost->max_cmd_len = iscsit->max_cmd_len;
  1090. shost->transportt = scsit;
  1091. shost->transportt->create_work_queue = 1;
  1092. *hostno = shost->host_no;
  1093. session = iscsi_hostdata(shost->hostdata);
  1094. memset(session, 0, sizeof(struct iscsi_session));
  1095. session->host = shost;
  1096. session->state = ISCSI_STATE_FREE;
  1097. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1098. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1099. session->cmdsn = initial_cmdsn;
  1100. session->exp_cmdsn = initial_cmdsn + 1;
  1101. session->max_cmdsn = initial_cmdsn + 1;
  1102. session->max_r2t = 1;
  1103. session->tt = iscsit;
  1104. /* initialize SCSI PDU commands pool */
  1105. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1106. (void***)&session->cmds,
  1107. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1108. goto cmdpool_alloc_fail;
  1109. /* pre-format cmds pool with ITT */
  1110. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1111. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1112. if (cmd_task_size)
  1113. ctask->dd_data = &ctask[1];
  1114. ctask->itt = cmd_i;
  1115. INIT_LIST_HEAD(&ctask->running);
  1116. }
  1117. spin_lock_init(&session->lock);
  1118. INIT_LIST_HEAD(&session->connections);
  1119. /* initialize immediate command pool */
  1120. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1121. (void***)&session->mgmt_cmds,
  1122. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1123. goto mgmtpool_alloc_fail;
  1124. /* pre-format immediate cmds pool with ITT */
  1125. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1126. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1127. if (mgmt_task_size)
  1128. mtask->dd_data = &mtask[1];
  1129. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1130. INIT_LIST_HEAD(&mtask->running);
  1131. }
  1132. if (scsi_add_host(shost, NULL))
  1133. goto add_host_fail;
  1134. if (!try_module_get(iscsit->owner))
  1135. goto cls_session_fail;
  1136. cls_session = iscsi_create_session(shost, iscsit, 0);
  1137. if (!cls_session)
  1138. goto module_put;
  1139. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1140. return cls_session;
  1141. module_put:
  1142. module_put(iscsit->owner);
  1143. cls_session_fail:
  1144. scsi_remove_host(shost);
  1145. add_host_fail:
  1146. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1147. mgmtpool_alloc_fail:
  1148. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1149. cmdpool_alloc_fail:
  1150. scsi_host_put(shost);
  1151. return NULL;
  1152. }
  1153. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1154. /**
  1155. * iscsi_session_teardown - destroy session, host, and cls_session
  1156. * shost: scsi host
  1157. *
  1158. * This can be used by software iscsi_transports that allocate
  1159. * a session per scsi host.
  1160. **/
  1161. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1162. {
  1163. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1164. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1165. struct module *owner = cls_session->transport->owner;
  1166. scsi_remove_host(shost);
  1167. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1168. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1169. iscsi_destroy_session(cls_session);
  1170. scsi_host_put(shost);
  1171. module_put(owner);
  1172. }
  1173. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1174. /**
  1175. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1176. * @cls_session: iscsi_cls_session
  1177. * @conn_idx: cid
  1178. **/
  1179. struct iscsi_cls_conn *
  1180. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1181. {
  1182. struct iscsi_session *session = class_to_transport_session(cls_session);
  1183. struct iscsi_conn *conn;
  1184. struct iscsi_cls_conn *cls_conn;
  1185. char *data;
  1186. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1187. if (!cls_conn)
  1188. return NULL;
  1189. conn = cls_conn->dd_data;
  1190. memset(conn, 0, sizeof(*conn));
  1191. conn->session = session;
  1192. conn->cls_conn = cls_conn;
  1193. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1194. conn->id = conn_idx;
  1195. conn->exp_statsn = 0;
  1196. conn->tmabort_state = TMABORT_INITIAL;
  1197. INIT_LIST_HEAD(&conn->run_list);
  1198. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1199. INIT_LIST_HEAD(&conn->xmitqueue);
  1200. /* initialize general immediate & non-immediate PDU commands queue */
  1201. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1202. GFP_KERNEL, NULL);
  1203. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1204. goto immqueue_alloc_fail;
  1205. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1206. GFP_KERNEL, NULL);
  1207. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1208. goto mgmtqueue_alloc_fail;
  1209. INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
  1210. /* allocate login_mtask used for the login/text sequences */
  1211. spin_lock_bh(&session->lock);
  1212. if (!__kfifo_get(session->mgmtpool.queue,
  1213. (void*)&conn->login_mtask,
  1214. sizeof(void*))) {
  1215. spin_unlock_bh(&session->lock);
  1216. goto login_mtask_alloc_fail;
  1217. }
  1218. spin_unlock_bh(&session->lock);
  1219. data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
  1220. if (!data)
  1221. goto login_mtask_data_alloc_fail;
  1222. conn->login_mtask->data = conn->data = data;
  1223. init_timer(&conn->tmabort_timer);
  1224. mutex_init(&conn->xmitmutex);
  1225. init_waitqueue_head(&conn->ehwait);
  1226. return cls_conn;
  1227. login_mtask_data_alloc_fail:
  1228. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1229. sizeof(void*));
  1230. login_mtask_alloc_fail:
  1231. kfifo_free(conn->mgmtqueue);
  1232. mgmtqueue_alloc_fail:
  1233. kfifo_free(conn->immqueue);
  1234. immqueue_alloc_fail:
  1235. iscsi_destroy_conn(cls_conn);
  1236. return NULL;
  1237. }
  1238. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1239. /**
  1240. * iscsi_conn_teardown - teardown iscsi connection
  1241. * cls_conn: iscsi class connection
  1242. *
  1243. * TODO: we may need to make this into a two step process
  1244. * like scsi-mls remove + put host
  1245. */
  1246. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1247. {
  1248. struct iscsi_conn *conn = cls_conn->dd_data;
  1249. struct iscsi_session *session = conn->session;
  1250. unsigned long flags;
  1251. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1252. mutex_lock(&conn->xmitmutex);
  1253. spin_lock_bh(&session->lock);
  1254. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1255. if (session->leadconn == conn) {
  1256. /*
  1257. * leading connection? then give up on recovery.
  1258. */
  1259. session->state = ISCSI_STATE_TERMINATE;
  1260. wake_up(&conn->ehwait);
  1261. }
  1262. spin_unlock_bh(&session->lock);
  1263. mutex_unlock(&conn->xmitmutex);
  1264. /*
  1265. * Block until all in-progress commands for this connection
  1266. * time out or fail.
  1267. */
  1268. for (;;) {
  1269. spin_lock_irqsave(session->host->host_lock, flags);
  1270. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1271. spin_unlock_irqrestore(session->host->host_lock, flags);
  1272. break;
  1273. }
  1274. spin_unlock_irqrestore(session->host->host_lock, flags);
  1275. msleep_interruptible(500);
  1276. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1277. "host_failed %d\n", session->host->host_busy,
  1278. session->host->host_failed);
  1279. /*
  1280. * force eh_abort() to unblock
  1281. */
  1282. wake_up(&conn->ehwait);
  1283. }
  1284. spin_lock_bh(&session->lock);
  1285. kfree(conn->data);
  1286. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1287. sizeof(void*));
  1288. list_del(&conn->item);
  1289. if (list_empty(&session->connections))
  1290. session->leadconn = NULL;
  1291. if (session->leadconn && session->leadconn == conn)
  1292. session->leadconn = container_of(session->connections.next,
  1293. struct iscsi_conn, item);
  1294. if (session->leadconn == NULL)
  1295. /* no connections exits.. reset sequencing */
  1296. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1297. spin_unlock_bh(&session->lock);
  1298. kfifo_free(conn->immqueue);
  1299. kfifo_free(conn->mgmtqueue);
  1300. iscsi_destroy_conn(cls_conn);
  1301. }
  1302. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1303. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1304. {
  1305. struct iscsi_conn *conn = cls_conn->dd_data;
  1306. struct iscsi_session *session = conn->session;
  1307. if (session == NULL) {
  1308. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1309. return -EPERM;
  1310. }
  1311. spin_lock_bh(&session->lock);
  1312. conn->c_stage = ISCSI_CONN_STARTED;
  1313. session->state = ISCSI_STATE_LOGGED_IN;
  1314. switch(conn->stop_stage) {
  1315. case STOP_CONN_RECOVER:
  1316. /*
  1317. * unblock eh_abort() if it is blocked. re-try all
  1318. * commands after successful recovery
  1319. */
  1320. conn->stop_stage = 0;
  1321. conn->tmabort_state = TMABORT_INITIAL;
  1322. session->age++;
  1323. spin_unlock_bh(&session->lock);
  1324. iscsi_unblock_session(session_to_cls(session));
  1325. wake_up(&conn->ehwait);
  1326. return 0;
  1327. case STOP_CONN_TERM:
  1328. conn->stop_stage = 0;
  1329. break;
  1330. default:
  1331. break;
  1332. }
  1333. spin_unlock_bh(&session->lock);
  1334. return 0;
  1335. }
  1336. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1337. static void
  1338. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1339. {
  1340. struct iscsi_mgmt_task *mtask, *tmp;
  1341. /* handle pending */
  1342. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1343. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1344. if (mtask == conn->login_mtask)
  1345. continue;
  1346. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1347. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1348. sizeof(void*));
  1349. }
  1350. /* handle running */
  1351. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1352. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1353. list_del(&mtask->running);
  1354. if (mtask == conn->login_mtask)
  1355. continue;
  1356. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1357. sizeof(void*));
  1358. }
  1359. conn->mtask = NULL;
  1360. }
  1361. /* Fail commands. Mutex and session lock held and recv side suspended */
  1362. static void fail_all_commands(struct iscsi_conn *conn)
  1363. {
  1364. struct iscsi_cmd_task *ctask, *tmp;
  1365. /* flush pending */
  1366. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1367. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1368. ctask->itt);
  1369. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1370. }
  1371. /* fail all other running */
  1372. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1373. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1374. ctask->sc, ctask->itt);
  1375. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1376. }
  1377. conn->ctask = NULL;
  1378. }
  1379. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1380. struct iscsi_conn *conn, int flag)
  1381. {
  1382. int old_stop_stage;
  1383. spin_lock_bh(&session->lock);
  1384. if (conn->stop_stage == STOP_CONN_TERM) {
  1385. spin_unlock_bh(&session->lock);
  1386. return;
  1387. }
  1388. /*
  1389. * When this is called for the in_login state, we only want to clean
  1390. * up the login task and connection. We do not need to block and set
  1391. * the recovery state again
  1392. */
  1393. if (flag == STOP_CONN_TERM)
  1394. session->state = ISCSI_STATE_TERMINATE;
  1395. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1396. session->state = ISCSI_STATE_IN_RECOVERY;
  1397. old_stop_stage = conn->stop_stage;
  1398. conn->stop_stage = flag;
  1399. conn->c_stage = ISCSI_CONN_STOPPED;
  1400. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1401. spin_unlock_bh(&session->lock);
  1402. write_lock_bh(conn->recv_lock);
  1403. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1404. write_unlock_bh(conn->recv_lock);
  1405. mutex_lock(&conn->xmitmutex);
  1406. /*
  1407. * for connection level recovery we should not calculate
  1408. * header digest. conn->hdr_size used for optimization
  1409. * in hdr_extract() and will be re-negotiated at
  1410. * set_param() time.
  1411. */
  1412. if (flag == STOP_CONN_RECOVER) {
  1413. conn->hdrdgst_en = 0;
  1414. conn->datadgst_en = 0;
  1415. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1416. old_stop_stage != STOP_CONN_RECOVER) {
  1417. debug_scsi("blocking session\n");
  1418. iscsi_block_session(session_to_cls(session));
  1419. }
  1420. }
  1421. /*
  1422. * flush queues.
  1423. */
  1424. spin_lock_bh(&session->lock);
  1425. fail_all_commands(conn);
  1426. flush_control_queues(session, conn);
  1427. spin_unlock_bh(&session->lock);
  1428. mutex_unlock(&conn->xmitmutex);
  1429. }
  1430. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1431. {
  1432. struct iscsi_conn *conn = cls_conn->dd_data;
  1433. struct iscsi_session *session = conn->session;
  1434. switch (flag) {
  1435. case STOP_CONN_RECOVER:
  1436. case STOP_CONN_TERM:
  1437. iscsi_start_session_recovery(session, conn, flag);
  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. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1481. enum iscsi_param param, char *buf, int buflen)
  1482. {
  1483. struct iscsi_conn *conn = cls_conn->dd_data;
  1484. struct iscsi_session *session = conn->session;
  1485. uint32_t value;
  1486. switch(param) {
  1487. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1488. sscanf(buf, "%d", &conn->max_recv_dlength);
  1489. break;
  1490. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1491. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1492. break;
  1493. case ISCSI_PARAM_HDRDGST_EN:
  1494. sscanf(buf, "%d", &conn->hdrdgst_en);
  1495. break;
  1496. case ISCSI_PARAM_DATADGST_EN:
  1497. sscanf(buf, "%d", &conn->datadgst_en);
  1498. break;
  1499. case ISCSI_PARAM_INITIAL_R2T_EN:
  1500. sscanf(buf, "%d", &session->initial_r2t_en);
  1501. break;
  1502. case ISCSI_PARAM_MAX_R2T:
  1503. sscanf(buf, "%d", &session->max_r2t);
  1504. break;
  1505. case ISCSI_PARAM_IMM_DATA_EN:
  1506. sscanf(buf, "%d", &session->imm_data_en);
  1507. break;
  1508. case ISCSI_PARAM_FIRST_BURST:
  1509. sscanf(buf, "%d", &session->first_burst);
  1510. break;
  1511. case ISCSI_PARAM_MAX_BURST:
  1512. sscanf(buf, "%d", &session->max_burst);
  1513. break;
  1514. case ISCSI_PARAM_PDU_INORDER_EN:
  1515. sscanf(buf, "%d", &session->pdu_inorder_en);
  1516. break;
  1517. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1518. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1519. break;
  1520. case ISCSI_PARAM_ERL:
  1521. sscanf(buf, "%d", &session->erl);
  1522. break;
  1523. case ISCSI_PARAM_IFMARKER_EN:
  1524. sscanf(buf, "%d", &value);
  1525. BUG_ON(value);
  1526. break;
  1527. case ISCSI_PARAM_OFMARKER_EN:
  1528. sscanf(buf, "%d", &value);
  1529. BUG_ON(value);
  1530. break;
  1531. case ISCSI_PARAM_EXP_STATSN:
  1532. sscanf(buf, "%u", &conn->exp_statsn);
  1533. break;
  1534. case ISCSI_PARAM_TARGET_NAME:
  1535. /* this should not change between logins */
  1536. if (session->targetname)
  1537. break;
  1538. session->targetname = kstrdup(buf, GFP_KERNEL);
  1539. if (!session->targetname)
  1540. return -ENOMEM;
  1541. break;
  1542. case ISCSI_PARAM_TPGT:
  1543. sscanf(buf, "%d", &session->tpgt);
  1544. break;
  1545. case ISCSI_PARAM_PERSISTENT_PORT:
  1546. sscanf(buf, "%d", &conn->persistent_port);
  1547. break;
  1548. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1549. /*
  1550. * this is the address returned in discovery so it should
  1551. * not change between logins.
  1552. */
  1553. if (conn->persistent_address)
  1554. break;
  1555. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1556. if (!conn->persistent_address)
  1557. return -ENOMEM;
  1558. break;
  1559. default:
  1560. return -ENOSYS;
  1561. }
  1562. return 0;
  1563. }
  1564. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1565. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1566. enum iscsi_param param, char *buf)
  1567. {
  1568. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1569. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1570. int len;
  1571. switch(param) {
  1572. case ISCSI_PARAM_INITIAL_R2T_EN:
  1573. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1574. break;
  1575. case ISCSI_PARAM_MAX_R2T:
  1576. len = sprintf(buf, "%hu\n", session->max_r2t);
  1577. break;
  1578. case ISCSI_PARAM_IMM_DATA_EN:
  1579. len = sprintf(buf, "%d\n", session->imm_data_en);
  1580. break;
  1581. case ISCSI_PARAM_FIRST_BURST:
  1582. len = sprintf(buf, "%u\n", session->first_burst);
  1583. break;
  1584. case ISCSI_PARAM_MAX_BURST:
  1585. len = sprintf(buf, "%u\n", session->max_burst);
  1586. break;
  1587. case ISCSI_PARAM_PDU_INORDER_EN:
  1588. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1589. break;
  1590. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1591. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1592. break;
  1593. case ISCSI_PARAM_ERL:
  1594. len = sprintf(buf, "%d\n", session->erl);
  1595. break;
  1596. case ISCSI_PARAM_TARGET_NAME:
  1597. len = sprintf(buf, "%s\n", session->targetname);
  1598. break;
  1599. case ISCSI_PARAM_TPGT:
  1600. len = sprintf(buf, "%d\n", session->tpgt);
  1601. break;
  1602. default:
  1603. return -ENOSYS;
  1604. }
  1605. return len;
  1606. }
  1607. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1608. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1609. enum iscsi_param param, char *buf)
  1610. {
  1611. struct iscsi_conn *conn = cls_conn->dd_data;
  1612. int len;
  1613. switch(param) {
  1614. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1615. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1616. break;
  1617. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1618. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1619. break;
  1620. case ISCSI_PARAM_HDRDGST_EN:
  1621. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1622. break;
  1623. case ISCSI_PARAM_DATADGST_EN:
  1624. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1625. break;
  1626. case ISCSI_PARAM_IFMARKER_EN:
  1627. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1628. break;
  1629. case ISCSI_PARAM_OFMARKER_EN:
  1630. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1631. break;
  1632. case ISCSI_PARAM_EXP_STATSN:
  1633. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1634. break;
  1635. case ISCSI_PARAM_PERSISTENT_PORT:
  1636. len = sprintf(buf, "%d\n", conn->persistent_port);
  1637. break;
  1638. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1639. len = sprintf(buf, "%s\n", conn->persistent_address);
  1640. break;
  1641. default:
  1642. return -ENOSYS;
  1643. }
  1644. return len;
  1645. }
  1646. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1647. MODULE_AUTHOR("Mike Christie");
  1648. MODULE_DESCRIPTION("iSCSI library functions");
  1649. MODULE_LICENSE("GPL");