zfcp_scsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. *
  3. * linux/drivers/s390/scsi/zfcp_scsi.c
  4. *
  5. * FCP adapter driver for IBM eServer zSeries
  6. *
  7. * (C) Copyright IBM Corp. 2002, 2004
  8. *
  9. * Author(s): Martin Peschke <mpeschke@de.ibm.com>
  10. * Raimund Schroeder <raimund.schroeder@de.ibm.com>
  11. * Aron Zeh
  12. * Wolfgang Taphorn
  13. * Stefan Bader <stefan.bader@de.ibm.com>
  14. * Heiko Carstens <heiko.carstens@de.ibm.com>
  15. * Andreas Herrmann <aherrman@de.ibm.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2, or (at your option)
  20. * any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  32. #include "zfcp_ext.h"
  33. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  34. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  35. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  36. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  37. void (*done) (struct scsi_cmnd *));
  38. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  39. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  40. static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
  41. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  42. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  43. struct scsi_cmnd *);
  44. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
  45. scsi_lun_t);
  46. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  47. struct scsi_transport_template *zfcp_transport_template;
  48. struct zfcp_data zfcp_data = {
  49. .scsi_host_template = {
  50. name: ZFCP_NAME,
  51. proc_name: "zfcp",
  52. proc_info: NULL,
  53. detect: NULL,
  54. slave_alloc: zfcp_scsi_slave_alloc,
  55. slave_configure: zfcp_scsi_slave_configure,
  56. slave_destroy: zfcp_scsi_slave_destroy,
  57. queuecommand: zfcp_scsi_queuecommand,
  58. eh_abort_handler: zfcp_scsi_eh_abort_handler,
  59. eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
  60. eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
  61. eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
  62. /* FIXME(openfcp): Tune */
  63. can_queue: 4096,
  64. this_id: -1,
  65. /*
  66. * FIXME:
  67. * one less? can zfcp_create_sbale cope with it?
  68. */
  69. sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
  70. cmd_per_lun: 1,
  71. unchecked_isa_dma: 0,
  72. use_clustering: 1,
  73. sdev_attrs: zfcp_sysfs_sdev_attrs,
  74. },
  75. .driver_version = ZFCP_VERSION,
  76. /* rest initialised with zeros */
  77. };
  78. /* Find start of Response Information in FCP response unit*/
  79. char *
  80. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  81. {
  82. char *fcp_rsp_info_ptr;
  83. fcp_rsp_info_ptr =
  84. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  85. return fcp_rsp_info_ptr;
  86. }
  87. /* Find start of Sense Information in FCP response unit*/
  88. char *
  89. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  90. {
  91. char *fcp_sns_info_ptr;
  92. fcp_sns_info_ptr =
  93. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  94. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  95. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  96. fcp_rsp_iu->fcp_rsp_len;
  97. return fcp_sns_info_ptr;
  98. }
  99. fcp_dl_t *
  100. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  101. {
  102. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  103. fcp_dl_t *fcp_dl_addr;
  104. fcp_dl_addr = (fcp_dl_t *)
  105. ((unsigned char *) fcp_cmd +
  106. sizeof (struct fcp_cmnd_iu) + additional_length);
  107. /*
  108. * fcp_dl_addr = start address of fcp_cmnd structure +
  109. * size of fixed part + size of dynamically sized add_dcp_cdb field
  110. * SEE FCP-2 documentation
  111. */
  112. return fcp_dl_addr;
  113. }
  114. fcp_dl_t
  115. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  116. {
  117. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  118. }
  119. void
  120. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  121. {
  122. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  123. }
  124. /*
  125. * note: it's a bit-or operation not an assignment
  126. * regarding the specified byte
  127. */
  128. static inline void
  129. set_byte(u32 * result, char status, char pos)
  130. {
  131. *result |= status << (pos * 8);
  132. }
  133. void
  134. set_host_byte(u32 * result, char status)
  135. {
  136. set_byte(result, status, 2);
  137. }
  138. void
  139. set_driver_byte(u32 * result, char status)
  140. {
  141. set_byte(result, status, 3);
  142. }
  143. /*
  144. * function: zfcp_scsi_slave_alloc
  145. *
  146. * purpose:
  147. *
  148. * returns:
  149. */
  150. static int
  151. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  152. {
  153. struct zfcp_adapter *adapter;
  154. struct zfcp_unit *unit;
  155. unsigned long flags;
  156. int retval = -ENXIO;
  157. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  158. if (!adapter)
  159. goto out;
  160. read_lock_irqsave(&zfcp_data.config_lock, flags);
  161. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  162. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  163. &unit->status)) {
  164. sdp->hostdata = unit;
  165. unit->device = sdp;
  166. zfcp_unit_get(unit);
  167. retval = 0;
  168. }
  169. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  170. out:
  171. return retval;
  172. }
  173. /*
  174. * function: zfcp_scsi_slave_destroy
  175. *
  176. * purpose:
  177. *
  178. * returns:
  179. */
  180. static void
  181. zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  182. {
  183. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  184. if (unit) {
  185. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  186. sdpnt->hostdata = NULL;
  187. unit->device = NULL;
  188. zfcp_unit_put(unit);
  189. } else {
  190. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  191. "address %p\n", sdpnt);
  192. }
  193. }
  194. /*
  195. * called from scsi midlayer to allow finetuning of a device.
  196. */
  197. static int
  198. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  199. {
  200. if (sdp->tagged_supported)
  201. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  202. else
  203. scsi_adjust_queue_depth(sdp, 0, 1);
  204. return 0;
  205. }
  206. /**
  207. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  208. * @scpnt: pointer to struct scsi_cmnd where result is set
  209. * @result: result to be set in scpnt (e.g. DID_ERROR)
  210. */
  211. static void
  212. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  213. {
  214. set_host_byte(&scpnt->result, result);
  215. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  216. zfcp_scsi_dbf_event_result("fail", 4,
  217. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  218. scpnt, NULL);
  219. /* return directly */
  220. scpnt->scsi_done(scpnt);
  221. }
  222. /**
  223. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  224. * zfcp_scsi_command_sync
  225. * @adapter: adapter where scsi command is issued
  226. * @unit: unit to which scsi command is sent
  227. * @scpnt: scsi command to be sent
  228. * @timer: timer to be started if request is successfully initiated
  229. *
  230. * Note: In scsi_done function must be set in scpnt.
  231. */
  232. int
  233. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  234. struct scsi_cmnd *scpnt, struct timer_list *timer)
  235. {
  236. int tmp;
  237. int retval;
  238. retval = 0;
  239. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  240. BUG_ON(scpnt->scsi_done == NULL);
  241. if (unlikely(NULL == unit)) {
  242. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  243. goto out;
  244. }
  245. if (unlikely(
  246. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  247. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  248. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  249. "0x%016Lx on adapter %s\n",
  250. unit->fcp_lun, unit->port->wwpn,
  251. zfcp_get_busid_by_adapter(adapter));
  252. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  253. goto out;
  254. }
  255. if (unlikely(
  256. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  257. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  258. "on port 0x%016Lx in recovery\n",
  259. zfcp_get_busid_by_unit(unit),
  260. unit->fcp_lun, unit->port->wwpn);
  261. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  262. goto out;
  263. }
  264. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
  265. ZFCP_REQ_AUTO_CLEANUP);
  266. if (unlikely(tmp < 0)) {
  267. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  268. retval = SCSI_MLQUEUE_HOST_BUSY;
  269. }
  270. out:
  271. return retval;
  272. }
  273. void
  274. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  275. {
  276. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  277. complete(wait);
  278. }
  279. /**
  280. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  281. * @unit: unit where command is sent to
  282. * @scpnt: scsi command to be sent
  283. * @timer: timer to be started if request is successfully initiated
  284. * Return: 0
  285. *
  286. * Errors are indicated in scpnt->result
  287. */
  288. int
  289. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  290. struct timer_list *timer)
  291. {
  292. int ret;
  293. DECLARE_COMPLETION(wait);
  294. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  295. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  296. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
  297. if (ret == 0)
  298. wait_for_completion(&wait);
  299. scpnt->SCp.ptr = NULL;
  300. return 0;
  301. }
  302. /*
  303. * function: zfcp_scsi_queuecommand
  304. *
  305. * purpose: enqueues a SCSI command to the specified target device
  306. *
  307. * returns: 0 - success, SCSI command enqueued
  308. * !0 - failure
  309. */
  310. int
  311. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  312. void (*done) (struct scsi_cmnd *))
  313. {
  314. struct zfcp_unit *unit;
  315. struct zfcp_adapter *adapter;
  316. /* reset the status for this request */
  317. scpnt->result = 0;
  318. scpnt->host_scribble = NULL;
  319. scpnt->scsi_done = done;
  320. /*
  321. * figure out adapter and target device
  322. * (stored there by zfcp_scsi_slave_alloc)
  323. */
  324. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  325. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  326. return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
  327. }
  328. /*
  329. * function: zfcp_unit_lookup
  330. *
  331. * purpose:
  332. *
  333. * returns:
  334. *
  335. * context:
  336. */
  337. static struct zfcp_unit *
  338. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
  339. scsi_lun_t lun)
  340. {
  341. struct zfcp_port *port;
  342. struct zfcp_unit *unit, *retval = NULL;
  343. list_for_each_entry(port, &adapter->port_list_head, list) {
  344. if (!port->rport || (id != port->rport->scsi_target_id))
  345. continue;
  346. list_for_each_entry(unit, &port->unit_list_head, list) {
  347. if (lun == unit->scsi_lun) {
  348. retval = unit;
  349. goto out;
  350. }
  351. }
  352. }
  353. out:
  354. return retval;
  355. }
  356. /**
  357. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  358. * @scpnt: pointer to scsi_cmnd to be aborted
  359. * Return: SUCCESS - command has been aborted and cleaned up in internal
  360. * bookkeeping, SCSI stack won't be called for aborted command
  361. * FAILED - otherwise
  362. *
  363. * We do not need to care for a SCSI command which completes normally
  364. * but late during this abort routine runs. We are allowed to return
  365. * late commands to the SCSI stack. It tracks the state of commands and
  366. * will handle late commands. (Usually, the normal completion of late
  367. * commands is ignored with respect to the running abort operation.)
  368. */
  369. int
  370. zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  371. {
  372. struct Scsi_Host *scsi_host;
  373. struct zfcp_adapter *adapter;
  374. struct zfcp_unit *unit;
  375. int retval = SUCCESS;
  376. struct zfcp_fsf_req *new_fsf_req = NULL;
  377. struct zfcp_fsf_req *old_fsf_req;
  378. unsigned long flags;
  379. scsi_host = scpnt->device->host;
  380. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  381. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  382. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  383. scpnt, zfcp_get_busid_by_adapter(adapter));
  384. /* avoid race condition between late normal completion and abort */
  385. write_lock_irqsave(&adapter->abort_lock, flags);
  386. /*
  387. * Check whether command has just completed and can not be aborted.
  388. * Even if the command has just been completed late, we can access
  389. * scpnt since the SCSI stack does not release it at least until
  390. * this routine returns. (scpnt is parameter passed to this routine
  391. * and must not disappear during abort even on late completion.)
  392. */
  393. old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
  394. if (!old_fsf_req) {
  395. write_unlock_irqrestore(&adapter->abort_lock, flags);
  396. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, NULL);
  397. retval = SUCCESS;
  398. goto out;
  399. }
  400. old_fsf_req->data = 0;
  401. old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  402. /* don't access old_fsf_req after releasing the abort_lock */
  403. write_unlock_irqrestore(&adapter->abort_lock, flags);
  404. /* call FSF routine which does the abort */
  405. new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
  406. adapter, unit, 0);
  407. if (!new_fsf_req) {
  408. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  409. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  410. old_fsf_req);
  411. retval = FAILED;
  412. goto out;
  413. }
  414. /* wait for completion of abort */
  415. __wait_event(new_fsf_req->completion_wq,
  416. new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  417. /* status should be valid since signals were not permitted */
  418. if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  419. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req,
  420. NULL);
  421. retval = SUCCESS;
  422. } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  423. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req,
  424. NULL);
  425. retval = SUCCESS;
  426. } else {
  427. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req,
  428. NULL);
  429. retval = FAILED;
  430. }
  431. zfcp_fsf_req_free(new_fsf_req);
  432. out:
  433. return retval;
  434. }
  435. /*
  436. * function: zfcp_scsi_eh_device_reset_handler
  437. *
  438. * purpose:
  439. *
  440. * returns:
  441. */
  442. int
  443. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  444. {
  445. int retval;
  446. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  447. if (!unit) {
  448. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  449. retval = SUCCESS;
  450. goto out;
  451. }
  452. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  453. /*
  454. * If we do not know whether the unit supports 'logical unit reset'
  455. * then try 'logical unit reset' and proceed with 'target reset'
  456. * if 'logical unit reset' fails.
  457. * If the unit is known not to support 'logical unit reset' then
  458. * skip 'logical unit reset' and try 'target reset' immediately.
  459. */
  460. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  461. &unit->status)) {
  462. retval = zfcp_task_management_function(unit,
  463. FCP_LOGICAL_UNIT_RESET,
  464. scpnt);
  465. if (retval) {
  466. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  467. if (retval == -ENOTSUPP)
  468. atomic_set_mask
  469. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  470. &unit->status);
  471. /* fall through and try 'target reset' next */
  472. } else {
  473. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  474. unit);
  475. /* avoid 'target reset' */
  476. retval = SUCCESS;
  477. goto out;
  478. }
  479. }
  480. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  481. if (retval) {
  482. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  483. retval = FAILED;
  484. } else {
  485. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  486. retval = SUCCESS;
  487. }
  488. out:
  489. return retval;
  490. }
  491. static int
  492. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  493. struct scsi_cmnd *scpnt)
  494. {
  495. struct zfcp_adapter *adapter = unit->port->adapter;
  496. struct zfcp_fsf_req *fsf_req;
  497. int retval = 0;
  498. /* issue task management function */
  499. fsf_req = zfcp_fsf_send_fcp_command_task_management
  500. (adapter, unit, tm_flags, 0);
  501. if (!fsf_req) {
  502. ZFCP_LOG_INFO("error: creation of task management request "
  503. "failed for unit 0x%016Lx on port 0x%016Lx on "
  504. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  505. zfcp_get_busid_by_adapter(adapter));
  506. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  507. retval = -ENOMEM;
  508. goto out;
  509. }
  510. __wait_event(fsf_req->completion_wq,
  511. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  512. /*
  513. * check completion status of task management function
  514. */
  515. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  516. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  517. retval = -EIO;
  518. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  519. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  520. retval = -ENOTSUPP;
  521. } else
  522. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  523. zfcp_fsf_req_free(fsf_req);
  524. out:
  525. return retval;
  526. }
  527. /**
  528. * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
  529. */
  530. int
  531. zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
  532. {
  533. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  534. struct zfcp_adapter *adapter = unit->port->adapter;
  535. ZFCP_LOG_NORMAL("bus reset because of problems with "
  536. "unit 0x%016Lx\n", unit->fcp_lun);
  537. zfcp_erp_adapter_reopen(adapter, 0);
  538. zfcp_erp_wait(adapter);
  539. return SUCCESS;
  540. }
  541. /**
  542. * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
  543. */
  544. int
  545. zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  546. {
  547. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  548. struct zfcp_adapter *adapter = unit->port->adapter;
  549. ZFCP_LOG_NORMAL("host reset because of problems with "
  550. "unit 0x%016Lx\n", unit->fcp_lun);
  551. zfcp_erp_adapter_reopen(adapter, 0);
  552. zfcp_erp_wait(adapter);
  553. return SUCCESS;
  554. }
  555. /*
  556. * function:
  557. *
  558. * purpose:
  559. *
  560. * returns:
  561. */
  562. int
  563. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  564. {
  565. int retval = 0;
  566. static unsigned int unique_id = 0;
  567. /* register adapter as SCSI host with mid layer of SCSI stack */
  568. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  569. sizeof (struct zfcp_adapter *));
  570. if (!adapter->scsi_host) {
  571. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  572. "for adapter %s ",
  573. zfcp_get_busid_by_adapter(adapter));
  574. retval = -EIO;
  575. goto out;
  576. }
  577. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  578. /* tell the SCSI stack some characteristics of this adapter */
  579. adapter->scsi_host->max_id = 1;
  580. adapter->scsi_host->max_lun = 1;
  581. adapter->scsi_host->max_channel = 0;
  582. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  583. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  584. adapter->scsi_host->transportt = zfcp_transport_template;
  585. /*
  586. * Reverse mapping of the host number to avoid race condition
  587. */
  588. adapter->scsi_host_no = adapter->scsi_host->host_no;
  589. /*
  590. * save a pointer to our own adapter data structure within
  591. * hostdata field of SCSI host data structure
  592. */
  593. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  594. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  595. scsi_host_put(adapter->scsi_host);
  596. retval = -EIO;
  597. goto out;
  598. }
  599. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  600. out:
  601. return retval;
  602. }
  603. /*
  604. * function:
  605. *
  606. * purpose:
  607. *
  608. * returns:
  609. */
  610. void
  611. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  612. {
  613. struct Scsi_Host *shost;
  614. struct zfcp_port *port;
  615. shost = adapter->scsi_host;
  616. if (!shost)
  617. return;
  618. read_lock_irq(&zfcp_data.config_lock);
  619. list_for_each_entry(port, &adapter->port_list_head, list)
  620. if (port->rport)
  621. port->rport = NULL;
  622. read_unlock_irq(&zfcp_data.config_lock);
  623. fc_remove_host(shost);
  624. scsi_remove_host(shost);
  625. scsi_host_put(shost);
  626. adapter->scsi_host = NULL;
  627. adapter->scsi_host_no = 0;
  628. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  629. return;
  630. }
  631. void
  632. zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
  633. {
  634. adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
  635. adapter->scsi_er_timer.data = (unsigned long) adapter;
  636. adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
  637. add_timer(&adapter->scsi_er_timer);
  638. }
  639. /*
  640. * Support functions for FC transport class
  641. */
  642. static struct fc_host_statistics*
  643. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  644. {
  645. struct fc_host_statistics *fc_stats;
  646. if (!adapter->fc_stats) {
  647. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  648. if (!fc_stats)
  649. return NULL;
  650. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  651. }
  652. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  653. return adapter->fc_stats;
  654. }
  655. static void
  656. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  657. struct fsf_qtcb_bottom_port *data,
  658. struct fsf_qtcb_bottom_port *old)
  659. {
  660. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  661. old->seconds_since_last_reset;
  662. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  663. fc_stats->tx_words = data->tx_words - old->tx_words;
  664. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  665. fc_stats->rx_words = data->rx_words - old->rx_words;
  666. fc_stats->lip_count = data->lip - old->lip;
  667. fc_stats->nos_count = data->nos - old->nos;
  668. fc_stats->error_frames = data->error_frames - old->error_frames;
  669. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  670. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  671. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  672. fc_stats->loss_of_signal_count = data->loss_of_signal -
  673. old->loss_of_signal;
  674. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  675. old->psp_error_counts;
  676. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  677. old->invalid_tx_words;
  678. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  679. fc_stats->fcp_input_requests = data->input_requests -
  680. old->input_requests;
  681. fc_stats->fcp_output_requests = data->output_requests -
  682. old->output_requests;
  683. fc_stats->fcp_control_requests = data->control_requests -
  684. old->control_requests;
  685. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  686. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  687. }
  688. static void
  689. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  690. struct fsf_qtcb_bottom_port *data)
  691. {
  692. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  693. fc_stats->tx_frames = data->tx_frames;
  694. fc_stats->tx_words = data->tx_words;
  695. fc_stats->rx_frames = data->rx_frames;
  696. fc_stats->rx_words = data->rx_words;
  697. fc_stats->lip_count = data->lip;
  698. fc_stats->nos_count = data->nos;
  699. fc_stats->error_frames = data->error_frames;
  700. fc_stats->dumped_frames = data->dumped_frames;
  701. fc_stats->link_failure_count = data->link_failure;
  702. fc_stats->loss_of_sync_count = data->loss_of_sync;
  703. fc_stats->loss_of_signal_count = data->loss_of_signal;
  704. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  705. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  706. fc_stats->invalid_crc_count = data->invalid_crcs;
  707. fc_stats->fcp_input_requests = data->input_requests;
  708. fc_stats->fcp_output_requests = data->output_requests;
  709. fc_stats->fcp_control_requests = data->control_requests;
  710. fc_stats->fcp_input_megabytes = data->input_mb;
  711. fc_stats->fcp_output_megabytes = data->output_mb;
  712. }
  713. /**
  714. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  715. *
  716. * assumption: scsi_transport_fc synchronizes calls of
  717. * get_fc_host_stats and reset_fc_host_stats
  718. * (XXX to be checked otherwise introduce locking)
  719. */
  720. static struct fc_host_statistics *
  721. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  722. {
  723. struct zfcp_adapter *adapter;
  724. struct fc_host_statistics *fc_stats;
  725. struct fsf_qtcb_bottom_port *data;
  726. int ret;
  727. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  728. fc_stats = zfcp_init_fc_host_stats(adapter);
  729. if (!fc_stats)
  730. return NULL;
  731. data = kmalloc(sizeof(*data), GFP_KERNEL);
  732. if (!data)
  733. return NULL;
  734. memset(data, 0, sizeof(*data));
  735. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  736. if (ret) {
  737. kfree(data);
  738. return NULL; /* XXX return zeroed fc_stats? */
  739. }
  740. if (adapter->stats_reset &&
  741. ((jiffies/HZ - adapter->stats_reset) <
  742. data->seconds_since_last_reset)) {
  743. zfcp_adjust_fc_host_stats(fc_stats, data,
  744. adapter->stats_reset_data);
  745. } else
  746. zfcp_set_fc_host_stats(fc_stats, data);
  747. kfree(data);
  748. return fc_stats;
  749. }
  750. static void
  751. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  752. {
  753. struct zfcp_adapter *adapter;
  754. struct fsf_qtcb_bottom_port *data, *old_data;
  755. int ret;
  756. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  757. data = kmalloc(sizeof(*data), GFP_KERNEL);
  758. if (!data)
  759. return;
  760. memset(data, 0, sizeof(*data));
  761. ret = zfcp_fsf_exchange_port_data(NULL, adapter, data);
  762. if (ret == 0) {
  763. adapter->stats_reset = jiffies/HZ;
  764. old_data = adapter->stats_reset_data;
  765. adapter->stats_reset_data = data; /* finally freed in
  766. adater_dequeue */
  767. kfree(old_data);
  768. }
  769. }
  770. struct fc_function_template zfcp_transport_functions = {
  771. .show_starget_port_id = 1,
  772. .show_starget_port_name = 1,
  773. .show_starget_node_name = 1,
  774. .show_rport_supported_classes = 1,
  775. .show_host_node_name = 1,
  776. .show_host_port_name = 1,
  777. .show_host_permanent_port_name = 1,
  778. .show_host_supported_classes = 1,
  779. .show_host_supported_speeds = 1,
  780. .show_host_maxframe_size = 1,
  781. .show_host_serial_number = 1,
  782. .get_fc_host_stats = zfcp_get_fc_host_stats,
  783. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  784. /* no functions registered for following dynamic attributes but
  785. directly set by LLDD */
  786. .show_host_port_type = 1,
  787. .show_host_speed = 1,
  788. .show_host_port_id = 1,
  789. };
  790. /**
  791. * ZFCP_DEFINE_SCSI_ATTR
  792. * @_name: name of show attribute
  793. * @_format: format string
  794. * @_value: value to print
  795. *
  796. * Generates attribute for a unit.
  797. */
  798. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  799. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  800. char *buf) \
  801. { \
  802. struct scsi_device *sdev; \
  803. struct zfcp_unit *unit; \
  804. \
  805. sdev = to_scsi_device(dev); \
  806. unit = sdev->hostdata; \
  807. return sprintf(buf, _format, _value); \
  808. } \
  809. \
  810. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  811. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  812. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  813. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  814. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  815. &dev_attr_fcp_lun,
  816. &dev_attr_wwpn,
  817. &dev_attr_hba_id,
  818. NULL
  819. };
  820. #undef ZFCP_LOG_AREA