libiscsi.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <asm/unaligned.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. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  40. int err);
  41. struct iscsi_session *
  42. class_to_transport_session(struct iscsi_cls_session *cls_session)
  43. {
  44. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  45. return iscsi_hostdata(shost->hostdata);
  46. }
  47. EXPORT_SYMBOL_GPL(class_to_transport_session);
  48. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  49. #define SNA32_CHECK 2147483648UL
  50. static int iscsi_sna_lt(u32 n1, u32 n2)
  51. {
  52. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  53. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  54. }
  55. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  56. static int iscsi_sna_lte(u32 n1, u32 n2)
  57. {
  58. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  59. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  60. }
  61. void
  62. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  63. {
  64. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  65. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  66. /*
  67. * standard specifies this check for when to update expected and
  68. * max sequence numbers
  69. */
  70. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  71. return;
  72. if (exp_cmdsn != session->exp_cmdsn &&
  73. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  74. session->exp_cmdsn = exp_cmdsn;
  75. if (max_cmdsn != session->max_cmdsn &&
  76. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  77. session->max_cmdsn = max_cmdsn;
  78. /*
  79. * if the window closed with IO queued, then kick the
  80. * xmit thread
  81. */
  82. if (!list_empty(&session->leadconn->xmitqueue) ||
  83. !list_empty(&session->leadconn->mgmtqueue))
  84. scsi_queue_work(session->host,
  85. &session->leadconn->xmitwork);
  86. }
  87. }
  88. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  89. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  90. struct iscsi_data *hdr)
  91. {
  92. struct iscsi_conn *conn = ctask->conn;
  93. memset(hdr, 0, sizeof(struct iscsi_data));
  94. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  95. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  96. ctask->unsol_datasn++;
  97. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  98. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  99. hdr->itt = ctask->hdr->itt;
  100. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  101. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  102. if (ctask->unsol_count > conn->max_xmit_dlength) {
  103. hton24(hdr->dlength, conn->max_xmit_dlength);
  104. ctask->data_count = conn->max_xmit_dlength;
  105. ctask->unsol_offset += ctask->data_count;
  106. hdr->flags = 0;
  107. } else {
  108. hton24(hdr->dlength, ctask->unsol_count);
  109. ctask->data_count = ctask->unsol_count;
  110. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  111. }
  112. }
  113. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  114. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  115. {
  116. unsigned exp_len = ctask->hdr_len + len;
  117. if (exp_len > ctask->hdr_max) {
  118. WARN_ON(1);
  119. return -EINVAL;
  120. }
  121. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  122. ctask->hdr_len = exp_len;
  123. return 0;
  124. }
  125. /**
  126. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  127. * @ctask: iscsi cmd task
  128. *
  129. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  130. * fields like dlength or final based on how much data it sends
  131. */
  132. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  133. {
  134. struct iscsi_conn *conn = ctask->conn;
  135. struct iscsi_session *session = conn->session;
  136. struct iscsi_cmd *hdr = ctask->hdr;
  137. struct scsi_cmnd *sc = ctask->sc;
  138. unsigned hdrlength;
  139. int rc;
  140. ctask->hdr_len = 0;
  141. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  142. if (rc)
  143. return rc;
  144. hdr->opcode = ISCSI_OP_SCSI_CMD;
  145. hdr->flags = ISCSI_ATTR_SIMPLE;
  146. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  147. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  148. hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
  149. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  150. session->cmdsn++;
  151. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  152. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  153. if (sc->cmd_len < MAX_COMMAND_SIZE)
  154. memset(&hdr->cdb[sc->cmd_len], 0,
  155. MAX_COMMAND_SIZE - sc->cmd_len);
  156. ctask->data_count = 0;
  157. ctask->imm_count = 0;
  158. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  159. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  160. /*
  161. * Write counters:
  162. *
  163. * imm_count bytes to be sent right after
  164. * SCSI PDU Header
  165. *
  166. * unsol_count bytes(as Data-Out) to be sent
  167. * without R2T ack right after
  168. * immediate data
  169. *
  170. * r2t_data_count bytes to be sent via R2T ack's
  171. *
  172. * pad_count bytes to be sent as zero-padding
  173. */
  174. ctask->unsol_count = 0;
  175. ctask->unsol_offset = 0;
  176. ctask->unsol_datasn = 0;
  177. if (session->imm_data_en) {
  178. if (scsi_bufflen(sc) >= session->first_burst)
  179. ctask->imm_count = min(session->first_burst,
  180. conn->max_xmit_dlength);
  181. else
  182. ctask->imm_count = min(scsi_bufflen(sc),
  183. conn->max_xmit_dlength);
  184. hton24(ctask->hdr->dlength, ctask->imm_count);
  185. } else
  186. zero_data(ctask->hdr->dlength);
  187. if (!session->initial_r2t_en) {
  188. ctask->unsol_count = min((session->first_burst),
  189. (scsi_bufflen(sc))) - ctask->imm_count;
  190. ctask->unsol_offset = ctask->imm_count;
  191. }
  192. if (!ctask->unsol_count)
  193. /* No unsolicit Data-Out's */
  194. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  195. } else {
  196. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  197. zero_data(hdr->dlength);
  198. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  199. hdr->flags |= ISCSI_FLAG_CMD_READ;
  200. }
  201. /* calculate size of additional header segments (AHSs) */
  202. hdrlength = ctask->hdr_len - sizeof(*hdr);
  203. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  204. hdrlength /= ISCSI_PAD_LEN;
  205. WARN_ON(hdrlength >= 256);
  206. hdr->hlength = hdrlength & 0xFF;
  207. conn->scsicmd_pdus_cnt++;
  208. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
  209. "cmdsn %d win %d]\n",
  210. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  211. conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
  212. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  213. return 0;
  214. }
  215. /**
  216. * iscsi_complete_command - return command back to scsi-ml
  217. * @ctask: iscsi cmd task
  218. *
  219. * Must be called with session lock.
  220. * This function returns the scsi command to scsi-ml and returns
  221. * the cmd task to the pool of available cmd tasks.
  222. */
  223. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  224. {
  225. struct iscsi_session *session = ctask->conn->session;
  226. struct scsi_cmnd *sc = ctask->sc;
  227. ctask->state = ISCSI_TASK_COMPLETED;
  228. ctask->sc = NULL;
  229. /* SCSI eh reuses commands to verify us */
  230. sc->SCp.ptr = NULL;
  231. list_del_init(&ctask->running);
  232. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  233. sc->scsi_done(sc);
  234. }
  235. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  236. {
  237. atomic_inc(&ctask->refcount);
  238. }
  239. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  240. {
  241. if (atomic_dec_and_test(&ctask->refcount))
  242. iscsi_complete_command(ctask);
  243. }
  244. /**
  245. * iscsi_cmd_rsp - SCSI Command Response processing
  246. * @conn: iscsi connection
  247. * @hdr: iscsi header
  248. * @ctask: scsi command task
  249. * @data: cmd data buffer
  250. * @datalen: len of buffer
  251. *
  252. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  253. * then completes the command and task.
  254. **/
  255. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  256. struct iscsi_cmd_task *ctask, char *data,
  257. int datalen)
  258. {
  259. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  260. struct iscsi_session *session = conn->session;
  261. struct scsi_cmnd *sc = ctask->sc;
  262. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  263. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  264. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  265. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  266. sc->result = DID_ERROR << 16;
  267. goto out;
  268. }
  269. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  270. uint16_t senselen;
  271. if (datalen < 2) {
  272. invalid_datalen:
  273. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  274. "invalid data buffer size of %d\n", datalen);
  275. sc->result = DID_BAD_TARGET << 16;
  276. goto out;
  277. }
  278. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  279. if (datalen < senselen)
  280. goto invalid_datalen;
  281. memcpy(sc->sense_buffer, data + 2,
  282. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  283. debug_scsi("copied %d bytes of sense\n",
  284. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  285. }
  286. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  287. ISCSI_FLAG_CMD_OVERFLOW)) {
  288. int res_count = be32_to_cpu(rhdr->residual_count);
  289. if (res_count > 0 &&
  290. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  291. res_count <= scsi_bufflen(sc)))
  292. scsi_set_resid(sc, res_count);
  293. else
  294. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  295. } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  296. ISCSI_FLAG_CMD_BIDI_OVERFLOW))
  297. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  298. out:
  299. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  300. (long)sc, sc->result, ctask->itt);
  301. conn->scsirsp_pdus_cnt++;
  302. __iscsi_put_ctask(ctask);
  303. }
  304. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  305. {
  306. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  307. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  308. conn->tmfrsp_pdus_cnt++;
  309. if (conn->tmf_state != TMF_QUEUED)
  310. return;
  311. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  312. conn->tmf_state = TMF_SUCCESS;
  313. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  314. conn->tmf_state = TMF_NOT_FOUND;
  315. else
  316. conn->tmf_state = TMF_FAILED;
  317. wake_up(&conn->ehwait);
  318. }
  319. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  320. char *data, int datalen)
  321. {
  322. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  323. struct iscsi_hdr rejected_pdu;
  324. uint32_t itt;
  325. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  326. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  327. if (ntoh24(reject->dlength) > datalen)
  328. return ISCSI_ERR_PROTO;
  329. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  330. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  331. itt = get_itt(rejected_pdu.itt);
  332. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  333. "due to DataDigest error.\n", itt,
  334. rejected_pdu.opcode);
  335. }
  336. }
  337. return 0;
  338. }
  339. /**
  340. * __iscsi_complete_pdu - complete pdu
  341. * @conn: iscsi conn
  342. * @hdr: iscsi header
  343. * @data: data buffer
  344. * @datalen: len of data buffer
  345. *
  346. * Completes pdu processing by freeing any resources allocated at
  347. * queuecommand or send generic. session lock must be held and verify
  348. * itt must have been called.
  349. */
  350. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  351. char *data, int datalen)
  352. {
  353. struct iscsi_session *session = conn->session;
  354. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  355. struct iscsi_cmd_task *ctask;
  356. struct iscsi_mgmt_task *mtask;
  357. uint32_t itt;
  358. if (hdr->itt != RESERVED_ITT)
  359. itt = get_itt(hdr->itt);
  360. else
  361. itt = ~0U;
  362. if (itt < session->cmds_max) {
  363. ctask = session->cmds[itt];
  364. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  365. opcode, conn->id, ctask->itt, datalen);
  366. switch(opcode) {
  367. case ISCSI_OP_SCSI_CMD_RSP:
  368. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  369. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  370. datalen);
  371. break;
  372. case ISCSI_OP_SCSI_DATA_IN:
  373. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  374. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  375. conn->scsirsp_pdus_cnt++;
  376. __iscsi_put_ctask(ctask);
  377. }
  378. break;
  379. case ISCSI_OP_R2T:
  380. /* LLD handles this for now */
  381. break;
  382. default:
  383. rc = ISCSI_ERR_BAD_OPCODE;
  384. break;
  385. }
  386. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  387. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  388. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  389. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  390. opcode, conn->id, mtask->itt, datalen);
  391. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  392. switch(opcode) {
  393. case ISCSI_OP_LOGOUT_RSP:
  394. if (datalen) {
  395. rc = ISCSI_ERR_PROTO;
  396. break;
  397. }
  398. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  399. /* fall through */
  400. case ISCSI_OP_LOGIN_RSP:
  401. case ISCSI_OP_TEXT_RSP:
  402. /*
  403. * login related PDU's exp_statsn is handled in
  404. * userspace
  405. */
  406. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  407. rc = ISCSI_ERR_CONN_FAILED;
  408. list_del_init(&mtask->running);
  409. if (conn->login_mtask != mtask)
  410. __kfifo_put(session->mgmtpool.queue,
  411. (void*)&mtask, sizeof(void*));
  412. break;
  413. case ISCSI_OP_SCSI_TMFUNC_RSP:
  414. if (datalen) {
  415. rc = ISCSI_ERR_PROTO;
  416. break;
  417. }
  418. iscsi_tmf_rsp(conn, hdr);
  419. break;
  420. case ISCSI_OP_NOOP_IN:
  421. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  422. rc = ISCSI_ERR_PROTO;
  423. break;
  424. }
  425. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  426. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  427. rc = ISCSI_ERR_CONN_FAILED;
  428. list_del_init(&mtask->running);
  429. __kfifo_put(session->mgmtpool.queue,
  430. (void*)&mtask, sizeof(void*));
  431. break;
  432. default:
  433. rc = ISCSI_ERR_BAD_OPCODE;
  434. break;
  435. }
  436. } else if (itt == ~0U) {
  437. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  438. switch(opcode) {
  439. case ISCSI_OP_NOOP_IN:
  440. if (datalen) {
  441. rc = ISCSI_ERR_PROTO;
  442. break;
  443. }
  444. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  445. break;
  446. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  447. rc = ISCSI_ERR_CONN_FAILED;
  448. break;
  449. case ISCSI_OP_REJECT:
  450. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  451. break;
  452. case ISCSI_OP_ASYNC_EVENT:
  453. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  454. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  455. rc = ISCSI_ERR_CONN_FAILED;
  456. break;
  457. default:
  458. rc = ISCSI_ERR_BAD_OPCODE;
  459. break;
  460. }
  461. } else
  462. rc = ISCSI_ERR_BAD_ITT;
  463. return rc;
  464. }
  465. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  466. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  467. char *data, int datalen)
  468. {
  469. int rc;
  470. spin_lock(&conn->session->lock);
  471. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  472. spin_unlock(&conn->session->lock);
  473. return rc;
  474. }
  475. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  476. /* verify itt (itt encoding: age+cid+itt) */
  477. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  478. uint32_t *ret_itt)
  479. {
  480. struct iscsi_session *session = conn->session;
  481. struct iscsi_cmd_task *ctask;
  482. uint32_t itt;
  483. if (hdr->itt != RESERVED_ITT) {
  484. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  485. (session->age << ISCSI_AGE_SHIFT)) {
  486. printk(KERN_ERR "iscsi: received itt %x expected "
  487. "session age (%x)\n", (__force u32)hdr->itt,
  488. session->age & ISCSI_AGE_MASK);
  489. return ISCSI_ERR_BAD_ITT;
  490. }
  491. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  492. (conn->id << ISCSI_CID_SHIFT)) {
  493. printk(KERN_ERR "iscsi: received itt %x, expected "
  494. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  495. return ISCSI_ERR_BAD_ITT;
  496. }
  497. itt = get_itt(hdr->itt);
  498. } else
  499. itt = ~0U;
  500. if (itt < session->cmds_max) {
  501. ctask = session->cmds[itt];
  502. if (!ctask->sc) {
  503. printk(KERN_INFO "iscsi: dropping ctask with "
  504. "itt 0x%x\n", ctask->itt);
  505. /* force drop */
  506. return ISCSI_ERR_NO_SCSI_CMD;
  507. }
  508. if (ctask->sc->SCp.phase != session->age) {
  509. printk(KERN_ERR "iscsi: ctask's session age %d, "
  510. "expected %d\n", ctask->sc->SCp.phase,
  511. session->age);
  512. return ISCSI_ERR_SESSION_FAILED;
  513. }
  514. }
  515. *ret_itt = itt;
  516. return 0;
  517. }
  518. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  519. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  520. {
  521. struct iscsi_session *session = conn->session;
  522. unsigned long flags;
  523. spin_lock_irqsave(&session->lock, flags);
  524. if (session->state == ISCSI_STATE_FAILED) {
  525. spin_unlock_irqrestore(&session->lock, flags);
  526. return;
  527. }
  528. if (conn->stop_stage == 0)
  529. session->state = ISCSI_STATE_FAILED;
  530. spin_unlock_irqrestore(&session->lock, flags);
  531. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  532. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  533. iscsi_conn_error(conn->cls_conn, err);
  534. }
  535. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  536. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  537. struct iscsi_mgmt_task *mtask)
  538. {
  539. struct iscsi_session *session = conn->session;
  540. struct iscsi_hdr *hdr = mtask->hdr;
  541. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  542. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  543. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  544. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  545. /*
  546. * pre-format CmdSN for outgoing PDU.
  547. */
  548. nop->cmdsn = cpu_to_be32(session->cmdsn);
  549. if (hdr->itt != RESERVED_ITT) {
  550. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  551. /*
  552. * TODO: We always use immediate, so we never hit this.
  553. * If we start to send tmfs or nops as non-immediate then
  554. * we should start checking the cmdsn numbers for mgmt tasks.
  555. */
  556. if (conn->c_stage == ISCSI_CONN_STARTED &&
  557. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  558. session->queued_cmdsn++;
  559. session->cmdsn++;
  560. }
  561. }
  562. if (session->tt->init_mgmt_task)
  563. session->tt->init_mgmt_task(conn, mtask);
  564. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  565. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  566. mtask->data_count);
  567. }
  568. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  569. {
  570. struct iscsi_hdr *hdr = conn->mtask->hdr;
  571. int rc, was_logout = 0;
  572. spin_unlock_bh(&conn->session->lock);
  573. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  574. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  575. iscsi_block_session(session_to_cls(conn->session));
  576. was_logout = 1;
  577. }
  578. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  579. spin_lock_bh(&conn->session->lock);
  580. if (rc)
  581. return rc;
  582. /* done with this in-progress mtask */
  583. conn->mtask = NULL;
  584. if (was_logout) {
  585. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  586. return -ENODATA;
  587. }
  588. return 0;
  589. }
  590. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  591. {
  592. struct iscsi_session *session = conn->session;
  593. /*
  594. * Check for iSCSI window and take care of CmdSN wrap-around
  595. */
  596. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  597. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  598. "CmdSN %u/%u\n", session->exp_cmdsn,
  599. session->max_cmdsn, session->cmdsn,
  600. session->queued_cmdsn);
  601. return -ENOSPC;
  602. }
  603. return 0;
  604. }
  605. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  606. {
  607. struct iscsi_cmd_task *ctask = conn->ctask;
  608. int rc;
  609. __iscsi_get_ctask(ctask);
  610. spin_unlock_bh(&conn->session->lock);
  611. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  612. spin_lock_bh(&conn->session->lock);
  613. __iscsi_put_ctask(ctask);
  614. if (!rc)
  615. /* done with this ctask */
  616. conn->ctask = NULL;
  617. return rc;
  618. }
  619. /**
  620. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  621. * @ctask: ctask to requeue
  622. *
  623. * LLDs that need to run a ctask from the session workqueue should call
  624. * this. The session lock must be held.
  625. */
  626. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  627. {
  628. struct iscsi_conn *conn = ctask->conn;
  629. list_move_tail(&ctask->running, &conn->requeue);
  630. scsi_queue_work(conn->session->host, &conn->xmitwork);
  631. }
  632. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  633. /**
  634. * iscsi_data_xmit - xmit any command into the scheduled connection
  635. * @conn: iscsi connection
  636. *
  637. * Notes:
  638. * The function can return -EAGAIN in which case the caller must
  639. * re-schedule it again later or recover. '0' return code means
  640. * successful xmit.
  641. **/
  642. static int iscsi_data_xmit(struct iscsi_conn *conn)
  643. {
  644. int rc = 0;
  645. spin_lock_bh(&conn->session->lock);
  646. if (unlikely(conn->suspend_tx)) {
  647. debug_scsi("conn %d Tx suspended!\n", conn->id);
  648. spin_unlock_bh(&conn->session->lock);
  649. return -ENODATA;
  650. }
  651. if (conn->ctask) {
  652. rc = iscsi_xmit_ctask(conn);
  653. if (rc)
  654. goto again;
  655. }
  656. if (conn->mtask) {
  657. rc = iscsi_xmit_mtask(conn);
  658. if (rc)
  659. goto again;
  660. }
  661. /*
  662. * process mgmt pdus like nops before commands since we should
  663. * only have one nop-out as a ping from us and targets should not
  664. * overflow us with nop-ins
  665. */
  666. check_mgmt:
  667. while (!list_empty(&conn->mgmtqueue)) {
  668. conn->mtask = list_entry(conn->mgmtqueue.next,
  669. struct iscsi_mgmt_task, running);
  670. iscsi_prep_mtask(conn, conn->mtask);
  671. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  672. rc = iscsi_xmit_mtask(conn);
  673. if (rc)
  674. goto again;
  675. }
  676. /* process pending command queue */
  677. while (!list_empty(&conn->xmitqueue)) {
  678. if (conn->tmf_state == TMF_QUEUED)
  679. break;
  680. conn->ctask = list_entry(conn->xmitqueue.next,
  681. struct iscsi_cmd_task, running);
  682. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  683. fail_command(conn, conn->ctask, DID_ABORT << 16);
  684. continue;
  685. }
  686. conn->session->tt->init_cmd_task(conn->ctask);
  687. conn->ctask->state = ISCSI_TASK_RUNNING;
  688. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  689. rc = iscsi_xmit_ctask(conn);
  690. if (rc)
  691. goto again;
  692. /*
  693. * we could continuously get new ctask requests so
  694. * we need to check the mgmt queue for nops that need to
  695. * be sent to aviod starvation
  696. */
  697. if (!list_empty(&conn->mgmtqueue))
  698. goto check_mgmt;
  699. }
  700. while (!list_empty(&conn->requeue)) {
  701. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  702. break;
  703. conn->ctask = list_entry(conn->requeue.next,
  704. struct iscsi_cmd_task, running);
  705. conn->ctask->state = ISCSI_TASK_RUNNING;
  706. list_move_tail(conn->requeue.next, &conn->run_list);
  707. rc = iscsi_xmit_ctask(conn);
  708. if (rc)
  709. goto again;
  710. if (!list_empty(&conn->mgmtqueue))
  711. goto check_mgmt;
  712. }
  713. spin_unlock_bh(&conn->session->lock);
  714. return -ENODATA;
  715. again:
  716. if (unlikely(conn->suspend_tx))
  717. rc = -ENODATA;
  718. spin_unlock_bh(&conn->session->lock);
  719. return rc;
  720. }
  721. static void iscsi_xmitworker(struct work_struct *work)
  722. {
  723. struct iscsi_conn *conn =
  724. container_of(work, struct iscsi_conn, xmitwork);
  725. int rc;
  726. /*
  727. * serialize Xmit worker on a per-connection basis.
  728. */
  729. do {
  730. rc = iscsi_data_xmit(conn);
  731. } while (rc >= 0 || rc == -EAGAIN);
  732. }
  733. enum {
  734. FAILURE_BAD_HOST = 1,
  735. FAILURE_SESSION_FAILED,
  736. FAILURE_SESSION_FREED,
  737. FAILURE_WINDOW_CLOSED,
  738. FAILURE_OOM,
  739. FAILURE_SESSION_TERMINATE,
  740. FAILURE_SESSION_IN_RECOVERY,
  741. FAILURE_SESSION_RECOVERY_TIMEOUT,
  742. };
  743. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  744. {
  745. struct Scsi_Host *host;
  746. int reason = 0;
  747. struct iscsi_session *session;
  748. struct iscsi_conn *conn;
  749. struct iscsi_cmd_task *ctask = NULL;
  750. sc->scsi_done = done;
  751. sc->result = 0;
  752. sc->SCp.ptr = NULL;
  753. host = sc->device->host;
  754. session = iscsi_hostdata(host->hostdata);
  755. spin_lock(&session->lock);
  756. /*
  757. * ISCSI_STATE_FAILED is a temp. state. The recovery
  758. * code will decide what is best to do with command queued
  759. * during this time
  760. */
  761. if (session->state != ISCSI_STATE_LOGGED_IN &&
  762. session->state != ISCSI_STATE_FAILED) {
  763. /*
  764. * to handle the race between when we set the recovery state
  765. * and block the session we requeue here (commands could
  766. * be entering our queuecommand while a block is starting
  767. * up because the block code is not locked)
  768. */
  769. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  770. reason = FAILURE_SESSION_IN_RECOVERY;
  771. goto reject;
  772. }
  773. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  774. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  775. else if (session->state == ISCSI_STATE_TERMINATE)
  776. reason = FAILURE_SESSION_TERMINATE;
  777. else
  778. reason = FAILURE_SESSION_FREED;
  779. goto fault;
  780. }
  781. conn = session->leadconn;
  782. if (!conn) {
  783. reason = FAILURE_SESSION_FREED;
  784. goto fault;
  785. }
  786. if (iscsi_check_cmdsn_window_closed(conn)) {
  787. reason = FAILURE_WINDOW_CLOSED;
  788. goto reject;
  789. }
  790. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  791. sizeof(void*))) {
  792. reason = FAILURE_OOM;
  793. goto reject;
  794. }
  795. session->queued_cmdsn++;
  796. sc->SCp.phase = session->age;
  797. sc->SCp.ptr = (char *)ctask;
  798. atomic_set(&ctask->refcount, 1);
  799. ctask->state = ISCSI_TASK_PENDING;
  800. ctask->conn = conn;
  801. ctask->sc = sc;
  802. INIT_LIST_HEAD(&ctask->running);
  803. list_add_tail(&ctask->running, &conn->xmitqueue);
  804. spin_unlock(&session->lock);
  805. scsi_queue_work(host, &conn->xmitwork);
  806. return 0;
  807. reject:
  808. spin_unlock(&session->lock);
  809. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  810. return SCSI_MLQUEUE_HOST_BUSY;
  811. fault:
  812. spin_unlock(&session->lock);
  813. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  814. sc->cmnd[0], reason);
  815. sc->result = (DID_NO_CONNECT << 16);
  816. scsi_set_resid(sc, scsi_bufflen(sc));
  817. sc->scsi_done(sc);
  818. return 0;
  819. }
  820. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  821. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  822. {
  823. if (depth > ISCSI_MAX_CMD_PER_LUN)
  824. depth = ISCSI_MAX_CMD_PER_LUN;
  825. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  826. return sdev->queue_depth;
  827. }
  828. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  829. static struct iscsi_mgmt_task *
  830. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  831. char *data, uint32_t data_size)
  832. {
  833. struct iscsi_session *session = conn->session;
  834. struct iscsi_mgmt_task *mtask;
  835. if (session->state == ISCSI_STATE_TERMINATE)
  836. return NULL;
  837. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  838. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  839. /*
  840. * Login and Text are sent serially, in
  841. * request-followed-by-response sequence.
  842. * Same mtask can be used. Same ITT must be used.
  843. * Note that login_mtask is preallocated at conn_create().
  844. */
  845. mtask = conn->login_mtask;
  846. else {
  847. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  848. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  849. if (!__kfifo_get(session->mgmtpool.queue,
  850. (void*)&mtask, sizeof(void*)))
  851. return NULL;
  852. }
  853. if (data_size) {
  854. memcpy(mtask->data, data, data_size);
  855. mtask->data_count = data_size;
  856. } else
  857. mtask->data_count = 0;
  858. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  859. INIT_LIST_HEAD(&mtask->running);
  860. list_add_tail(&mtask->running, &conn->mgmtqueue);
  861. return mtask;
  862. }
  863. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  864. char *data, uint32_t data_size)
  865. {
  866. struct iscsi_conn *conn = cls_conn->dd_data;
  867. struct iscsi_session *session = conn->session;
  868. int err = 0;
  869. spin_lock_bh(&session->lock);
  870. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  871. err = -EPERM;
  872. spin_unlock_bh(&session->lock);
  873. scsi_queue_work(session->host, &conn->xmitwork);
  874. return err;
  875. }
  876. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  877. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  878. {
  879. struct iscsi_session *session = class_to_transport_session(cls_session);
  880. spin_lock_bh(&session->lock);
  881. if (session->state != ISCSI_STATE_LOGGED_IN) {
  882. session->state = ISCSI_STATE_RECOVERY_FAILED;
  883. if (session->leadconn)
  884. wake_up(&session->leadconn->ehwait);
  885. }
  886. spin_unlock_bh(&session->lock);
  887. }
  888. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  889. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  890. {
  891. struct Scsi_Host *host = sc->device->host;
  892. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  893. struct iscsi_conn *conn = session->leadconn;
  894. spin_lock_bh(&session->lock);
  895. if (session->state == ISCSI_STATE_TERMINATE) {
  896. failed:
  897. debug_scsi("failing host reset: session terminated "
  898. "[CID %d age %d]\n", conn->id, session->age);
  899. spin_unlock_bh(&session->lock);
  900. return FAILED;
  901. }
  902. spin_unlock_bh(&session->lock);
  903. /*
  904. * we drop the lock here but the leadconn cannot be destoyed while
  905. * we are in the scsi eh
  906. */
  907. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  908. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  909. wait_event_interruptible(conn->ehwait,
  910. session->state == ISCSI_STATE_TERMINATE ||
  911. session->state == ISCSI_STATE_LOGGED_IN ||
  912. session->state == ISCSI_STATE_RECOVERY_FAILED);
  913. if (signal_pending(current))
  914. flush_signals(current);
  915. spin_lock_bh(&session->lock);
  916. if (session->state == ISCSI_STATE_LOGGED_IN)
  917. printk(KERN_INFO "iscsi: host reset succeeded\n");
  918. else
  919. goto failed;
  920. spin_unlock_bh(&session->lock);
  921. return SUCCESS;
  922. }
  923. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  924. static void iscsi_tmf_timedout(unsigned long data)
  925. {
  926. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  927. struct iscsi_session *session = conn->session;
  928. spin_lock(&session->lock);
  929. if (conn->tmf_state == TMF_QUEUED) {
  930. conn->tmf_state = TMF_TIMEDOUT;
  931. debug_scsi("tmf timedout\n");
  932. /* unblock eh_abort() */
  933. wake_up(&conn->ehwait);
  934. }
  935. spin_unlock(&session->lock);
  936. }
  937. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  938. struct iscsi_tm *hdr, int age)
  939. {
  940. struct iscsi_session *session = conn->session;
  941. struct iscsi_mgmt_task *mtask;
  942. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  943. NULL, 0);
  944. if (!mtask) {
  945. spin_unlock_bh(&session->lock);
  946. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  947. spin_lock_bh(&session->lock);
  948. debug_scsi("tmf exec failure\n");
  949. return -EPERM;
  950. }
  951. conn->tmfcmd_pdus_cnt++;
  952. conn->tmf_timer.expires = 30 * HZ + jiffies;
  953. conn->tmf_timer.function = iscsi_tmf_timedout;
  954. conn->tmf_timer.data = (unsigned long)conn;
  955. add_timer(&conn->tmf_timer);
  956. debug_scsi("tmf set timeout\n");
  957. spin_unlock_bh(&session->lock);
  958. mutex_unlock(&session->eh_mutex);
  959. scsi_queue_work(session->host, &conn->xmitwork);
  960. /*
  961. * block eh thread until:
  962. *
  963. * 1) tmf response
  964. * 2) tmf timeout
  965. * 3) session is terminated or restarted or userspace has
  966. * given up on recovery
  967. */
  968. wait_event_interruptible(conn->ehwait, age != session->age ||
  969. session->state != ISCSI_STATE_LOGGED_IN ||
  970. conn->tmf_state != TMF_QUEUED);
  971. if (signal_pending(current))
  972. flush_signals(current);
  973. del_timer_sync(&conn->tmf_timer);
  974. mutex_lock(&session->eh_mutex);
  975. spin_lock_bh(&session->lock);
  976. /* if the session drops it will clean up the mtask */
  977. if (age != session->age ||
  978. session->state != ISCSI_STATE_LOGGED_IN)
  979. return -ENOTCONN;
  980. if (!list_empty(&mtask->running)) {
  981. list_del_init(&mtask->running);
  982. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  983. sizeof(void*));
  984. }
  985. return 0;
  986. }
  987. /*
  988. * session lock must be held
  989. */
  990. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  991. int err)
  992. {
  993. struct scsi_cmnd *sc;
  994. sc = ctask->sc;
  995. if (!sc)
  996. return;
  997. if (ctask->state == ISCSI_TASK_PENDING)
  998. /*
  999. * cmd never made it to the xmit thread, so we should not count
  1000. * the cmd in the sequencing
  1001. */
  1002. conn->session->queued_cmdsn--;
  1003. else
  1004. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1005. sc->result = err;
  1006. scsi_set_resid(sc, scsi_bufflen(sc));
  1007. if (conn->ctask == ctask)
  1008. conn->ctask = NULL;
  1009. /* release ref from queuecommand */
  1010. __iscsi_put_ctask(ctask);
  1011. }
  1012. /*
  1013. * Fail commands. session lock held and recv side suspended and xmit
  1014. * thread flushed
  1015. */
  1016. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
  1017. {
  1018. struct iscsi_cmd_task *ctask, *tmp;
  1019. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1020. conn->ctask = NULL;
  1021. /* flush pending */
  1022. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1023. if (lun == ctask->sc->device->lun || lun == -1) {
  1024. debug_scsi("failing pending sc %p itt 0x%x\n",
  1025. ctask->sc, ctask->itt);
  1026. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1027. }
  1028. }
  1029. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1030. if (lun == ctask->sc->device->lun || lun == -1) {
  1031. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1032. ctask->sc, ctask->itt);
  1033. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1034. }
  1035. }
  1036. /* fail all other running */
  1037. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1038. if (lun == ctask->sc->device->lun || lun == -1) {
  1039. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1040. ctask->sc, ctask->itt);
  1041. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1042. }
  1043. }
  1044. }
  1045. static void iscsi_suspend_tx(struct iscsi_conn *conn)
  1046. {
  1047. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1048. scsi_flush_work(conn->session->host);
  1049. }
  1050. static void iscsi_start_tx(struct iscsi_conn *conn)
  1051. {
  1052. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1053. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1054. }
  1055. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1056. struct iscsi_tm *hdr)
  1057. {
  1058. memset(hdr, 0, sizeof(*hdr));
  1059. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1060. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1061. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1062. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1063. hdr->rtt = ctask->hdr->itt;
  1064. hdr->refcmdsn = ctask->hdr->cmdsn;
  1065. }
  1066. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1067. {
  1068. struct Scsi_Host *host = sc->device->host;
  1069. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1070. struct iscsi_conn *conn;
  1071. struct iscsi_cmd_task *ctask;
  1072. struct iscsi_tm *hdr;
  1073. int rc, age;
  1074. mutex_lock(&session->eh_mutex);
  1075. spin_lock_bh(&session->lock);
  1076. /*
  1077. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1078. * got the command.
  1079. */
  1080. if (!sc->SCp.ptr) {
  1081. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1082. spin_unlock_bh(&session->lock);
  1083. mutex_unlock(&session->eh_mutex);
  1084. return SUCCESS;
  1085. }
  1086. /*
  1087. * If we are not logged in or we have started a new session
  1088. * then let the host reset code handle this
  1089. */
  1090. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1091. sc->SCp.phase != session->age) {
  1092. spin_unlock_bh(&session->lock);
  1093. mutex_unlock(&session->eh_mutex);
  1094. return FAILED;
  1095. }
  1096. conn = session->leadconn;
  1097. conn->eh_abort_cnt++;
  1098. age = session->age;
  1099. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1100. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1101. /* ctask completed before time out */
  1102. if (!ctask->sc) {
  1103. debug_scsi("sc completed while abort in progress\n");
  1104. goto success;
  1105. }
  1106. if (ctask->state == ISCSI_TASK_PENDING) {
  1107. fail_command(conn, ctask, DID_ABORT << 16);
  1108. goto success;
  1109. }
  1110. /* only have one tmf outstanding at a time */
  1111. if (conn->tmf_state != TMF_INITIAL)
  1112. goto failed;
  1113. conn->tmf_state = TMF_QUEUED;
  1114. hdr = &conn->tmhdr;
  1115. iscsi_prep_abort_task_pdu(ctask, hdr);
  1116. if (iscsi_exec_task_mgmt_fn(conn, hdr, age)) {
  1117. rc = FAILED;
  1118. goto failed;
  1119. }
  1120. switch (conn->tmf_state) {
  1121. case TMF_SUCCESS:
  1122. spin_unlock_bh(&session->lock);
  1123. iscsi_suspend_tx(conn);
  1124. /*
  1125. * clean up task if aborted. grab the recv lock as a writer
  1126. */
  1127. write_lock_bh(conn->recv_lock);
  1128. spin_lock(&session->lock);
  1129. fail_command(conn, ctask, DID_ABORT << 16);
  1130. conn->tmf_state = TMF_INITIAL;
  1131. spin_unlock(&session->lock);
  1132. write_unlock_bh(conn->recv_lock);
  1133. iscsi_start_tx(conn);
  1134. goto success_unlocked;
  1135. case TMF_TIMEDOUT:
  1136. spin_unlock_bh(&session->lock);
  1137. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1138. goto failed_unlocked;
  1139. case TMF_NOT_FOUND:
  1140. if (!sc->SCp.ptr) {
  1141. conn->tmf_state = TMF_INITIAL;
  1142. /* ctask completed before tmf abort response */
  1143. debug_scsi("sc completed while abort in progress\n");
  1144. goto success;
  1145. }
  1146. /* fall through */
  1147. default:
  1148. conn->tmf_state = TMF_INITIAL;
  1149. goto failed;
  1150. }
  1151. success:
  1152. spin_unlock_bh(&session->lock);
  1153. success_unlocked:
  1154. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1155. mutex_unlock(&session->eh_mutex);
  1156. return SUCCESS;
  1157. failed:
  1158. spin_unlock_bh(&session->lock);
  1159. failed_unlocked:
  1160. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1161. ctask ? ctask->itt : 0);
  1162. mutex_unlock(&session->eh_mutex);
  1163. return FAILED;
  1164. }
  1165. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1166. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1167. {
  1168. memset(hdr, 0, sizeof(*hdr));
  1169. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1170. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1171. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1172. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1173. hdr->rtt = ISCSI_RESERVED_TAG;
  1174. }
  1175. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1176. {
  1177. struct Scsi_Host *host = sc->device->host;
  1178. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1179. struct iscsi_conn *conn;
  1180. struct iscsi_tm *hdr;
  1181. int rc = FAILED;
  1182. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1183. mutex_lock(&session->eh_mutex);
  1184. spin_lock_bh(&session->lock);
  1185. /*
  1186. * Just check if we are not logged in. We cannot check for
  1187. * the phase because the reset could come from a ioctl.
  1188. */
  1189. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1190. goto unlock;
  1191. conn = session->leadconn;
  1192. /* only have one tmf outstanding at a time */
  1193. if (conn->tmf_state != TMF_INITIAL)
  1194. goto unlock;
  1195. conn->tmf_state = TMF_QUEUED;
  1196. hdr = &conn->tmhdr;
  1197. iscsi_prep_lun_reset_pdu(sc, hdr);
  1198. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age)) {
  1199. rc = FAILED;
  1200. goto unlock;
  1201. }
  1202. switch (conn->tmf_state) {
  1203. case TMF_SUCCESS:
  1204. break;
  1205. case TMF_TIMEDOUT:
  1206. spin_unlock_bh(&session->lock);
  1207. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1208. goto done;
  1209. default:
  1210. conn->tmf_state = TMF_INITIAL;
  1211. goto unlock;
  1212. }
  1213. rc = SUCCESS;
  1214. spin_unlock_bh(&session->lock);
  1215. iscsi_suspend_tx(conn);
  1216. /* need to grab the recv lock then session lock */
  1217. write_lock_bh(conn->recv_lock);
  1218. spin_lock(&session->lock);
  1219. fail_all_commands(conn, sc->device->lun);
  1220. conn->tmf_state = TMF_INITIAL;
  1221. spin_unlock(&session->lock);
  1222. write_unlock_bh(conn->recv_lock);
  1223. iscsi_start_tx(conn);
  1224. goto done;
  1225. unlock:
  1226. spin_unlock_bh(&session->lock);
  1227. done:
  1228. debug_scsi("iscsi_eh_device_reset %s\n",
  1229. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1230. mutex_unlock(&session->eh_mutex);
  1231. return rc;
  1232. }
  1233. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1234. int
  1235. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1236. {
  1237. int i;
  1238. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1239. if (*items == NULL)
  1240. return -ENOMEM;
  1241. q->max = max;
  1242. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1243. if (q->pool == NULL) {
  1244. kfree(*items);
  1245. return -ENOMEM;
  1246. }
  1247. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1248. GFP_KERNEL, NULL);
  1249. if (q->queue == ERR_PTR(-ENOMEM)) {
  1250. kfree(q->pool);
  1251. kfree(*items);
  1252. return -ENOMEM;
  1253. }
  1254. for (i = 0; i < max; i++) {
  1255. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1256. if (q->pool[i] == NULL) {
  1257. int j;
  1258. for (j = 0; j < i; j++)
  1259. kfree(q->pool[j]);
  1260. kfifo_free(q->queue);
  1261. kfree(q->pool);
  1262. kfree(*items);
  1263. return -ENOMEM;
  1264. }
  1265. memset(q->pool[i], 0, item_size);
  1266. (*items)[i] = q->pool[i];
  1267. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1268. }
  1269. return 0;
  1270. }
  1271. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1272. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1273. {
  1274. int i;
  1275. for (i = 0; i < q->max; i++)
  1276. kfree(items[i]);
  1277. kfree(q->pool);
  1278. kfree(items);
  1279. }
  1280. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1281. /*
  1282. * iSCSI Session's hostdata organization:
  1283. *
  1284. * *------------------* <== hostdata_session(host->hostdata)
  1285. * | ptr to class sess|
  1286. * |------------------| <== iscsi_hostdata(host->hostdata)
  1287. * | iscsi_session |
  1288. * *------------------*
  1289. */
  1290. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1291. _sz % sizeof(unsigned long))
  1292. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1293. /**
  1294. * iscsi_session_setup - create iscsi cls session and host and session
  1295. * @scsit: scsi transport template
  1296. * @iscsit: iscsi transport template
  1297. * @cmds_max: scsi host can queue
  1298. * @qdepth: scsi host cmds per lun
  1299. * @cmd_task_size: LLD ctask private data size
  1300. * @mgmt_task_size: LLD mtask private data size
  1301. * @initial_cmdsn: initial CmdSN
  1302. * @hostno: host no allocated
  1303. *
  1304. * This can be used by software iscsi_transports that allocate
  1305. * a session per scsi host.
  1306. **/
  1307. struct iscsi_cls_session *
  1308. iscsi_session_setup(struct iscsi_transport *iscsit,
  1309. struct scsi_transport_template *scsit,
  1310. uint16_t cmds_max, uint16_t qdepth,
  1311. int cmd_task_size, int mgmt_task_size,
  1312. uint32_t initial_cmdsn, uint32_t *hostno)
  1313. {
  1314. struct Scsi_Host *shost;
  1315. struct iscsi_session *session;
  1316. struct iscsi_cls_session *cls_session;
  1317. int cmd_i;
  1318. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1319. if (qdepth != 0)
  1320. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1321. "Queue depth must be between 1 and %d.\n",
  1322. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1323. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1324. }
  1325. if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
  1326. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1327. if (cmds_max != 0)
  1328. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1329. "can_queue must be a power of 2 and between "
  1330. "2 and %d - setting to %d.\n", cmds_max,
  1331. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1332. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1333. }
  1334. shost = scsi_host_alloc(iscsit->host_template,
  1335. hostdata_privsize(sizeof(*session)));
  1336. if (!shost)
  1337. return NULL;
  1338. /* the iscsi layer takes one task for reserve */
  1339. shost->can_queue = cmds_max - 1;
  1340. shost->cmd_per_lun = qdepth;
  1341. shost->max_id = 1;
  1342. shost->max_channel = 0;
  1343. shost->max_lun = iscsit->max_lun;
  1344. shost->max_cmd_len = iscsit->max_cmd_len;
  1345. shost->transportt = scsit;
  1346. shost->transportt->create_work_queue = 1;
  1347. *hostno = shost->host_no;
  1348. session = iscsi_hostdata(shost->hostdata);
  1349. memset(session, 0, sizeof(struct iscsi_session));
  1350. session->host = shost;
  1351. session->state = ISCSI_STATE_FREE;
  1352. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1353. session->cmds_max = cmds_max;
  1354. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1355. session->exp_cmdsn = initial_cmdsn + 1;
  1356. session->max_cmdsn = initial_cmdsn + 1;
  1357. session->max_r2t = 1;
  1358. session->tt = iscsit;
  1359. mutex_init(&session->eh_mutex);
  1360. /* initialize SCSI PDU commands pool */
  1361. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1362. (void***)&session->cmds,
  1363. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1364. goto cmdpool_alloc_fail;
  1365. /* pre-format cmds pool with ITT */
  1366. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1367. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1368. if (cmd_task_size)
  1369. ctask->dd_data = &ctask[1];
  1370. ctask->itt = cmd_i;
  1371. ctask->hdr_max = sizeof(struct iscsi_cmd);
  1372. INIT_LIST_HEAD(&ctask->running);
  1373. }
  1374. spin_lock_init(&session->lock);
  1375. /* initialize immediate command pool */
  1376. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1377. (void***)&session->mgmt_cmds,
  1378. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1379. goto mgmtpool_alloc_fail;
  1380. /* pre-format immediate cmds pool with ITT */
  1381. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1382. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1383. if (mgmt_task_size)
  1384. mtask->dd_data = &mtask[1];
  1385. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1386. INIT_LIST_HEAD(&mtask->running);
  1387. }
  1388. if (scsi_add_host(shost, NULL))
  1389. goto add_host_fail;
  1390. if (!try_module_get(iscsit->owner))
  1391. goto cls_session_fail;
  1392. cls_session = iscsi_create_session(shost, iscsit, 0);
  1393. if (!cls_session)
  1394. goto module_put;
  1395. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1396. return cls_session;
  1397. module_put:
  1398. module_put(iscsit->owner);
  1399. cls_session_fail:
  1400. scsi_remove_host(shost);
  1401. add_host_fail:
  1402. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1403. mgmtpool_alloc_fail:
  1404. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1405. cmdpool_alloc_fail:
  1406. scsi_host_put(shost);
  1407. return NULL;
  1408. }
  1409. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1410. /**
  1411. * iscsi_session_teardown - destroy session, host, and cls_session
  1412. * shost: scsi host
  1413. *
  1414. * This can be used by software iscsi_transports that allocate
  1415. * a session per scsi host.
  1416. **/
  1417. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1418. {
  1419. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1420. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1421. struct module *owner = cls_session->transport->owner;
  1422. iscsi_unblock_session(cls_session);
  1423. scsi_remove_host(shost);
  1424. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1425. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1426. kfree(session->password);
  1427. kfree(session->password_in);
  1428. kfree(session->username);
  1429. kfree(session->username_in);
  1430. kfree(session->targetname);
  1431. kfree(session->netdev);
  1432. kfree(session->hwaddress);
  1433. kfree(session->initiatorname);
  1434. iscsi_destroy_session(cls_session);
  1435. scsi_host_put(shost);
  1436. module_put(owner);
  1437. }
  1438. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1439. /**
  1440. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1441. * @cls_session: iscsi_cls_session
  1442. * @conn_idx: cid
  1443. **/
  1444. struct iscsi_cls_conn *
  1445. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1446. {
  1447. struct iscsi_session *session = class_to_transport_session(cls_session);
  1448. struct iscsi_conn *conn;
  1449. struct iscsi_cls_conn *cls_conn;
  1450. char *data;
  1451. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1452. if (!cls_conn)
  1453. return NULL;
  1454. conn = cls_conn->dd_data;
  1455. memset(conn, 0, sizeof(*conn));
  1456. conn->session = session;
  1457. conn->cls_conn = cls_conn;
  1458. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1459. conn->id = conn_idx;
  1460. conn->exp_statsn = 0;
  1461. conn->tmf_state = TMF_INITIAL;
  1462. INIT_LIST_HEAD(&conn->run_list);
  1463. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1464. INIT_LIST_HEAD(&conn->mgmtqueue);
  1465. INIT_LIST_HEAD(&conn->xmitqueue);
  1466. INIT_LIST_HEAD(&conn->requeue);
  1467. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1468. /* allocate login_mtask used for the login/text sequences */
  1469. spin_lock_bh(&session->lock);
  1470. if (!__kfifo_get(session->mgmtpool.queue,
  1471. (void*)&conn->login_mtask,
  1472. sizeof(void*))) {
  1473. spin_unlock_bh(&session->lock);
  1474. goto login_mtask_alloc_fail;
  1475. }
  1476. spin_unlock_bh(&session->lock);
  1477. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1478. if (!data)
  1479. goto login_mtask_data_alloc_fail;
  1480. conn->login_mtask->data = conn->data = data;
  1481. init_timer(&conn->tmf_timer);
  1482. init_waitqueue_head(&conn->ehwait);
  1483. return cls_conn;
  1484. login_mtask_data_alloc_fail:
  1485. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1486. sizeof(void*));
  1487. login_mtask_alloc_fail:
  1488. iscsi_destroy_conn(cls_conn);
  1489. return NULL;
  1490. }
  1491. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1492. /**
  1493. * iscsi_conn_teardown - teardown iscsi connection
  1494. * cls_conn: iscsi class connection
  1495. *
  1496. * TODO: we may need to make this into a two step process
  1497. * like scsi-mls remove + put host
  1498. */
  1499. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1500. {
  1501. struct iscsi_conn *conn = cls_conn->dd_data;
  1502. struct iscsi_session *session = conn->session;
  1503. unsigned long flags;
  1504. spin_lock_bh(&session->lock);
  1505. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1506. if (session->leadconn == conn) {
  1507. /*
  1508. * leading connection? then give up on recovery.
  1509. */
  1510. session->state = ISCSI_STATE_TERMINATE;
  1511. wake_up(&conn->ehwait);
  1512. }
  1513. spin_unlock_bh(&session->lock);
  1514. /*
  1515. * Block until all in-progress commands for this connection
  1516. * time out or fail.
  1517. */
  1518. for (;;) {
  1519. spin_lock_irqsave(session->host->host_lock, flags);
  1520. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1521. spin_unlock_irqrestore(session->host->host_lock, flags);
  1522. break;
  1523. }
  1524. spin_unlock_irqrestore(session->host->host_lock, flags);
  1525. msleep_interruptible(500);
  1526. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1527. "host_failed %d\n", session->host->host_busy,
  1528. session->host->host_failed);
  1529. /*
  1530. * force eh_abort() to unblock
  1531. */
  1532. wake_up(&conn->ehwait);
  1533. }
  1534. /* flush queued up work because we free the connection below */
  1535. iscsi_suspend_tx(conn);
  1536. spin_lock_bh(&session->lock);
  1537. kfree(conn->data);
  1538. kfree(conn->persistent_address);
  1539. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1540. sizeof(void*));
  1541. if (session->leadconn == conn)
  1542. session->leadconn = NULL;
  1543. spin_unlock_bh(&session->lock);
  1544. iscsi_destroy_conn(cls_conn);
  1545. }
  1546. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1547. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1548. {
  1549. struct iscsi_conn *conn = cls_conn->dd_data;
  1550. struct iscsi_session *session = conn->session;
  1551. if (!session) {
  1552. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1553. return -EPERM;
  1554. }
  1555. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1556. session->first_burst > session->max_burst) {
  1557. printk("iscsi: invalid burst lengths: "
  1558. "first_burst %d max_burst %d\n",
  1559. session->first_burst, session->max_burst);
  1560. return -EINVAL;
  1561. }
  1562. spin_lock_bh(&session->lock);
  1563. conn->c_stage = ISCSI_CONN_STARTED;
  1564. session->state = ISCSI_STATE_LOGGED_IN;
  1565. session->queued_cmdsn = session->cmdsn;
  1566. switch(conn->stop_stage) {
  1567. case STOP_CONN_RECOVER:
  1568. /*
  1569. * unblock eh_abort() if it is blocked. re-try all
  1570. * commands after successful recovery
  1571. */
  1572. conn->stop_stage = 0;
  1573. conn->tmf_state = TMF_INITIAL;
  1574. session->age++;
  1575. spin_unlock_bh(&session->lock);
  1576. iscsi_unblock_session(session_to_cls(session));
  1577. wake_up(&conn->ehwait);
  1578. return 0;
  1579. case STOP_CONN_TERM:
  1580. conn->stop_stage = 0;
  1581. break;
  1582. default:
  1583. break;
  1584. }
  1585. spin_unlock_bh(&session->lock);
  1586. return 0;
  1587. }
  1588. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1589. static void
  1590. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1591. {
  1592. struct iscsi_mgmt_task *mtask, *tmp;
  1593. /* handle pending */
  1594. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1595. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1596. list_del_init(&mtask->running);
  1597. if (mtask == conn->login_mtask)
  1598. continue;
  1599. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1600. sizeof(void*));
  1601. }
  1602. /* handle running */
  1603. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1604. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1605. list_del_init(&mtask->running);
  1606. if (mtask == conn->login_mtask)
  1607. continue;
  1608. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1609. sizeof(void*));
  1610. }
  1611. conn->mtask = NULL;
  1612. }
  1613. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1614. struct iscsi_conn *conn, int flag)
  1615. {
  1616. int old_stop_stage;
  1617. mutex_lock(&session->eh_mutex);
  1618. spin_lock_bh(&session->lock);
  1619. if (conn->stop_stage == STOP_CONN_TERM) {
  1620. spin_unlock_bh(&session->lock);
  1621. mutex_unlock(&session->eh_mutex);
  1622. return;
  1623. }
  1624. /*
  1625. * The LLD either freed/unset the lock on us, or userspace called
  1626. * stop but did not create a proper connection (connection was never
  1627. * bound or it was unbound then stop was called).
  1628. */
  1629. if (!conn->recv_lock) {
  1630. spin_unlock_bh(&session->lock);
  1631. mutex_unlock(&session->eh_mutex);
  1632. return;
  1633. }
  1634. /*
  1635. * When this is called for the in_login state, we only want to clean
  1636. * up the login task and connection. We do not need to block and set
  1637. * the recovery state again
  1638. */
  1639. if (flag == STOP_CONN_TERM)
  1640. session->state = ISCSI_STATE_TERMINATE;
  1641. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1642. session->state = ISCSI_STATE_IN_RECOVERY;
  1643. old_stop_stage = conn->stop_stage;
  1644. conn->stop_stage = flag;
  1645. conn->c_stage = ISCSI_CONN_STOPPED;
  1646. spin_unlock_bh(&session->lock);
  1647. iscsi_suspend_tx(conn);
  1648. write_lock_bh(conn->recv_lock);
  1649. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1650. write_unlock_bh(conn->recv_lock);
  1651. /*
  1652. * for connection level recovery we should not calculate
  1653. * header digest. conn->hdr_size used for optimization
  1654. * in hdr_extract() and will be re-negotiated at
  1655. * set_param() time.
  1656. */
  1657. if (flag == STOP_CONN_RECOVER) {
  1658. conn->hdrdgst_en = 0;
  1659. conn->datadgst_en = 0;
  1660. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1661. old_stop_stage != STOP_CONN_RECOVER) {
  1662. debug_scsi("blocking session\n");
  1663. iscsi_block_session(session_to_cls(session));
  1664. }
  1665. }
  1666. /*
  1667. * flush queues.
  1668. */
  1669. spin_lock_bh(&session->lock);
  1670. fail_all_commands(conn, -1);
  1671. flush_control_queues(session, conn);
  1672. spin_unlock_bh(&session->lock);
  1673. mutex_unlock(&session->eh_mutex);
  1674. }
  1675. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1676. {
  1677. struct iscsi_conn *conn = cls_conn->dd_data;
  1678. struct iscsi_session *session = conn->session;
  1679. switch (flag) {
  1680. case STOP_CONN_RECOVER:
  1681. case STOP_CONN_TERM:
  1682. iscsi_start_session_recovery(session, conn, flag);
  1683. break;
  1684. default:
  1685. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1686. }
  1687. }
  1688. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1689. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1690. struct iscsi_cls_conn *cls_conn, int is_leading)
  1691. {
  1692. struct iscsi_session *session = class_to_transport_session(cls_session);
  1693. struct iscsi_conn *conn = cls_conn->dd_data;
  1694. spin_lock_bh(&session->lock);
  1695. if (is_leading)
  1696. session->leadconn = conn;
  1697. spin_unlock_bh(&session->lock);
  1698. /*
  1699. * Unblock xmitworker(), Login Phase will pass through.
  1700. */
  1701. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1702. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1703. return 0;
  1704. }
  1705. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1706. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1707. enum iscsi_param param, char *buf, int buflen)
  1708. {
  1709. struct iscsi_conn *conn = cls_conn->dd_data;
  1710. struct iscsi_session *session = conn->session;
  1711. uint32_t value;
  1712. switch(param) {
  1713. case ISCSI_PARAM_FAST_ABORT:
  1714. sscanf(buf, "%d", &session->fast_abort);
  1715. break;
  1716. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1717. sscanf(buf, "%d", &conn->max_recv_dlength);
  1718. break;
  1719. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1720. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1721. break;
  1722. case ISCSI_PARAM_HDRDGST_EN:
  1723. sscanf(buf, "%d", &conn->hdrdgst_en);
  1724. break;
  1725. case ISCSI_PARAM_DATADGST_EN:
  1726. sscanf(buf, "%d", &conn->datadgst_en);
  1727. break;
  1728. case ISCSI_PARAM_INITIAL_R2T_EN:
  1729. sscanf(buf, "%d", &session->initial_r2t_en);
  1730. break;
  1731. case ISCSI_PARAM_MAX_R2T:
  1732. sscanf(buf, "%d", &session->max_r2t);
  1733. break;
  1734. case ISCSI_PARAM_IMM_DATA_EN:
  1735. sscanf(buf, "%d", &session->imm_data_en);
  1736. break;
  1737. case ISCSI_PARAM_FIRST_BURST:
  1738. sscanf(buf, "%d", &session->first_burst);
  1739. break;
  1740. case ISCSI_PARAM_MAX_BURST:
  1741. sscanf(buf, "%d", &session->max_burst);
  1742. break;
  1743. case ISCSI_PARAM_PDU_INORDER_EN:
  1744. sscanf(buf, "%d", &session->pdu_inorder_en);
  1745. break;
  1746. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1747. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1748. break;
  1749. case ISCSI_PARAM_ERL:
  1750. sscanf(buf, "%d", &session->erl);
  1751. break;
  1752. case ISCSI_PARAM_IFMARKER_EN:
  1753. sscanf(buf, "%d", &value);
  1754. BUG_ON(value);
  1755. break;
  1756. case ISCSI_PARAM_OFMARKER_EN:
  1757. sscanf(buf, "%d", &value);
  1758. BUG_ON(value);
  1759. break;
  1760. case ISCSI_PARAM_EXP_STATSN:
  1761. sscanf(buf, "%u", &conn->exp_statsn);
  1762. break;
  1763. case ISCSI_PARAM_USERNAME:
  1764. kfree(session->username);
  1765. session->username = kstrdup(buf, GFP_KERNEL);
  1766. if (!session->username)
  1767. return -ENOMEM;
  1768. break;
  1769. case ISCSI_PARAM_USERNAME_IN:
  1770. kfree(session->username_in);
  1771. session->username_in = kstrdup(buf, GFP_KERNEL);
  1772. if (!session->username_in)
  1773. return -ENOMEM;
  1774. break;
  1775. case ISCSI_PARAM_PASSWORD:
  1776. kfree(session->password);
  1777. session->password = kstrdup(buf, GFP_KERNEL);
  1778. if (!session->password)
  1779. return -ENOMEM;
  1780. break;
  1781. case ISCSI_PARAM_PASSWORD_IN:
  1782. kfree(session->password_in);
  1783. session->password_in = kstrdup(buf, GFP_KERNEL);
  1784. if (!session->password_in)
  1785. return -ENOMEM;
  1786. break;
  1787. case ISCSI_PARAM_TARGET_NAME:
  1788. /* this should not change between logins */
  1789. if (session->targetname)
  1790. break;
  1791. session->targetname = kstrdup(buf, GFP_KERNEL);
  1792. if (!session->targetname)
  1793. return -ENOMEM;
  1794. break;
  1795. case ISCSI_PARAM_TPGT:
  1796. sscanf(buf, "%d", &session->tpgt);
  1797. break;
  1798. case ISCSI_PARAM_PERSISTENT_PORT:
  1799. sscanf(buf, "%d", &conn->persistent_port);
  1800. break;
  1801. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1802. /*
  1803. * this is the address returned in discovery so it should
  1804. * not change between logins.
  1805. */
  1806. if (conn->persistent_address)
  1807. break;
  1808. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1809. if (!conn->persistent_address)
  1810. return -ENOMEM;
  1811. break;
  1812. default:
  1813. return -ENOSYS;
  1814. }
  1815. return 0;
  1816. }
  1817. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1818. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1819. enum iscsi_param param, char *buf)
  1820. {
  1821. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1822. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1823. int len;
  1824. switch(param) {
  1825. case ISCSI_PARAM_FAST_ABORT:
  1826. len = sprintf(buf, "%d\n", session->fast_abort);
  1827. break;
  1828. case ISCSI_PARAM_INITIAL_R2T_EN:
  1829. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1830. break;
  1831. case ISCSI_PARAM_MAX_R2T:
  1832. len = sprintf(buf, "%hu\n", session->max_r2t);
  1833. break;
  1834. case ISCSI_PARAM_IMM_DATA_EN:
  1835. len = sprintf(buf, "%d\n", session->imm_data_en);
  1836. break;
  1837. case ISCSI_PARAM_FIRST_BURST:
  1838. len = sprintf(buf, "%u\n", session->first_burst);
  1839. break;
  1840. case ISCSI_PARAM_MAX_BURST:
  1841. len = sprintf(buf, "%u\n", session->max_burst);
  1842. break;
  1843. case ISCSI_PARAM_PDU_INORDER_EN:
  1844. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1845. break;
  1846. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1847. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1848. break;
  1849. case ISCSI_PARAM_ERL:
  1850. len = sprintf(buf, "%d\n", session->erl);
  1851. break;
  1852. case ISCSI_PARAM_TARGET_NAME:
  1853. len = sprintf(buf, "%s\n", session->targetname);
  1854. break;
  1855. case ISCSI_PARAM_TPGT:
  1856. len = sprintf(buf, "%d\n", session->tpgt);
  1857. break;
  1858. case ISCSI_PARAM_USERNAME:
  1859. len = sprintf(buf, "%s\n", session->username);
  1860. break;
  1861. case ISCSI_PARAM_USERNAME_IN:
  1862. len = sprintf(buf, "%s\n", session->username_in);
  1863. break;
  1864. case ISCSI_PARAM_PASSWORD:
  1865. len = sprintf(buf, "%s\n", session->password);
  1866. break;
  1867. case ISCSI_PARAM_PASSWORD_IN:
  1868. len = sprintf(buf, "%s\n", session->password_in);
  1869. break;
  1870. default:
  1871. return -ENOSYS;
  1872. }
  1873. return len;
  1874. }
  1875. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1876. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1877. enum iscsi_param param, char *buf)
  1878. {
  1879. struct iscsi_conn *conn = cls_conn->dd_data;
  1880. int len;
  1881. switch(param) {
  1882. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1883. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1884. break;
  1885. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1886. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1887. break;
  1888. case ISCSI_PARAM_HDRDGST_EN:
  1889. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1890. break;
  1891. case ISCSI_PARAM_DATADGST_EN:
  1892. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1893. break;
  1894. case ISCSI_PARAM_IFMARKER_EN:
  1895. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1896. break;
  1897. case ISCSI_PARAM_OFMARKER_EN:
  1898. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1899. break;
  1900. case ISCSI_PARAM_EXP_STATSN:
  1901. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1902. break;
  1903. case ISCSI_PARAM_PERSISTENT_PORT:
  1904. len = sprintf(buf, "%d\n", conn->persistent_port);
  1905. break;
  1906. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1907. len = sprintf(buf, "%s\n", conn->persistent_address);
  1908. break;
  1909. default:
  1910. return -ENOSYS;
  1911. }
  1912. return len;
  1913. }
  1914. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1915. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1916. char *buf)
  1917. {
  1918. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1919. int len;
  1920. switch (param) {
  1921. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1922. if (!session->netdev)
  1923. len = sprintf(buf, "%s\n", "default");
  1924. else
  1925. len = sprintf(buf, "%s\n", session->netdev);
  1926. break;
  1927. case ISCSI_HOST_PARAM_HWADDRESS:
  1928. if (!session->hwaddress)
  1929. len = sprintf(buf, "%s\n", "default");
  1930. else
  1931. len = sprintf(buf, "%s\n", session->hwaddress);
  1932. break;
  1933. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1934. if (!session->initiatorname)
  1935. len = sprintf(buf, "%s\n", "unknown");
  1936. else
  1937. len = sprintf(buf, "%s\n", session->initiatorname);
  1938. break;
  1939. default:
  1940. return -ENOSYS;
  1941. }
  1942. return len;
  1943. }
  1944. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1945. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1946. char *buf, int buflen)
  1947. {
  1948. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1949. switch (param) {
  1950. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1951. if (!session->netdev)
  1952. session->netdev = kstrdup(buf, GFP_KERNEL);
  1953. break;
  1954. case ISCSI_HOST_PARAM_HWADDRESS:
  1955. if (!session->hwaddress)
  1956. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1957. break;
  1958. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1959. if (!session->initiatorname)
  1960. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1961. break;
  1962. default:
  1963. return -ENOSYS;
  1964. }
  1965. return 0;
  1966. }
  1967. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1968. MODULE_AUTHOR("Mike Christie");
  1969. MODULE_DESCRIPTION("iSCSI library functions");
  1970. MODULE_LICENSE("GPL");