zfcp_scsi.c 24 KB

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