libiscsi.c 65 KB

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